Classes with no data type
Hi, I've met an interesting problem in terms of how to type a data structure and the functions that operate upon it. The problem centres around a single data type. This data type can be constructed in multiple ways using different functions, depending on the options the user specifies. That's all simple enough. The problem really comes later on. Depending on the function used generate the data structure I want to use different functions later on for example, to display the data. Thus I have a typical classes problem, in that I have several implementations of essentially the same function for different circumstances. The problem is, they must all operate on the same data type, so I cannot define them as seperate instances. Anyone got any ideas how to type this? Bob
Use case to pattern match against the constructors of the datatype? This is assuming that you've made them explicit. If not, you'll at least want to extend your datatype with some information that allows you to determine what should be done with the values later. On 09/10/06, Thomas Davie <tatd2@kent.ac.uk> wrote:
Hi, I've met an interesting problem in terms of how to type a data structure and the functions that operate upon it.
The problem centres around a single data type. This data type can be constructed in multiple ways using different functions, depending on the options the user specifies. That's all simple enough. The problem really comes later on. Depending on the function used generate the data structure I want to use different functions later on for example, to display the data.
Thus I have a typical classes problem, in that I have several implementations of essentially the same function for different circumstances. The problem is, they must all operate on the same data type, so I cannot define them as seperate instances.
Anyone got any ideas how to type this?
Bob _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Thomas Davie wrote:
Hi, I've met an interesting problem in terms of how to type a data structure and the functions that operate upon it.
The problem centres around a single data type. This data type can be constructed in multiple ways using different functions, depending on the options the user specifies. That's all simple enough. The problem really comes later on. Depending on the function used generate the data structure I want to use different functions later on for example, to display the data.
Thus I have a typical classes problem, in that I have several implementations of essentially the same function for different circumstances. The problem is, they must all operate on the same data type, so I cannot define them as seperate instances.
Anyone got any ideas how to type this?
You could add a field to the data type to store a record of closures to operate on it eg data T = T {_ops :: Ops, ...} data Ops = Ops{_print :: IO ()} print :: T -> IO () print = _print . _ops so the different smart constructors for T would fill in the Ops member as required - in other words you can make your own dictionary explicitly to get more flexibility instead of using the instance/class mechanism. Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com
|> Thus I have a typical classes problem, in that I have several |> implementations of essentially the same function for different |> circumstances. The problem is, they must all operate on the same |> data type, so I cannot define them as seperate instances. |> |> Anyone got any ideas how to type this? Annotate the data type using a GADT:
{-# OPTIONS_GHC -fglasgow-exts #-} module Foo where
data One data Two
data MyData a where MyCon :: MyData a
con1 :: MyData One con1 = MyCon
con2 :: MyData Two con2 = MyCon
class Display a where display :: MyData a -> String
instance Display One where display _ = "one"
instance Display Two where display _ = "two"
The annotations will tell you which constructor was used to create the data, and you can implement destructor functions that can tell the difference. /Niklas
participants (4)
-
Brian Hulley -
Cale Gibbard -
Niklas Broberg -
Thomas Davie