Hi, I'm trying to use functionally dependent typeclasses to create an overloaded (+=) function. A simplified example of a first bit of code is... class PlusEq a b c | a b -> c where (+=) :: a -> b -> c instance (MArray a e m, Num e, Ix i) => PlusEq (a i e) (a i e) (m ()) where (+=) x y = let updateX i = do xi <- readArray x i yi <- readArray y i writeArray x i (xi + yi) in sequence_ . map updateX $ indices x I keep getting this error in GHCi: Illegal instance declaration for `PlusEq (a i e) (a i e) (m ())' (the instance types do not agree with the functional dependencies of the class) In the instance declaration for `PlusEq (a i e) (a i e) (m ())' Failed, modules loaded: none. Looking at GHC's documentation for MArray, the definition starts out class (HasBounds a, Monad m) => MArray a e m where ... It seems to me if MArray were written using fundeps (something like MArray a e m | a e -> m) things may work out. Is there a reason it's not written this way? If so, is there another way to do what I'm trying to do? Thanks. Chad Scherrer Computational Mathematics Group Pacific Northwest National Laboratory "Time flies like an arrow; fruit flies like a banana." -- Groucho Marx
On Mon, Sep 05, 2005 at 11:31:00AM -0700, Scherrer, Chad wrote:
I keep getting this error in GHCi:
Illegal instance declaration for `PlusEq (a i e) (a i e) (m ())' (the instance types do not agree with the functional dependencies of the class) In the instance declaration for `PlusEq (a i e) (a i e) (m ())' Failed, modules loaded: none.
Looking at GHC's documentation for MArray, the definition starts out
class (HasBounds a, Monad m) => MArray a e m where ...
It seems to me if MArray were written using fundeps (something like MArray a e m | a e -> m) things may work out. Is there a reason it's not written this way? If so, is there another way to do what I'm trying to do? Thanks.
I don't know the actual reason, but if it were MArray a e m | a e -> m it would be impossible to define an MArray instance of STArray for both the strict and the lazy ST monad. (There isn't currently an instance for the lazy ST monad, but right now it can easily be defined: http://www.mail-archive.com/haskell-cafe@haskell.org/msg09404.html) Groetjes, Remi -- Nobody can be exactly like me. Even I have trouble doing it.
participants (2)
-
Remi Turk -
Scherrer, Chad