[Haskell-cafe] representing Haskell objects in a uniform way
Gregory Crosswhite
gcross at phys.washington.edu
Wed Nov 4 16:16:10 EST 2009
The problem lies in the definition of your class:
> class AFun f where
> afun :: Data a => f -> ([Dynamic] -> a)
You are saying that afun can return any type "a" that the user wants
as long as it is an instance of "Data", whereas here
> instance Data v => AFun v where
> afun f [] = f
> afun _ _ = error "Too many arguments"
you are restricting the type returned by afun.
What you probably want is a class that looks more like
class Data r => AFun f r where
afun :: f -> ([Dynamic] -> r)
so that the return type is explicitly included in the definition of
the type.
Also, you might consider requiring that the argument and result be
instances of Binary rather than Data, since this gives you fast binary
serialization for free. Then you could write a class like
class (Binary a, Binary b) => AFun a b where
afun :: f -> (a -> b)
And your code can take care of the serialization/deserialization and
then just hand the values of the correct type over to afun.
Cheers,
Greg
More information about the Haskell-Cafe
mailing list