
Hi, When I define my data as fellow, data Moo a = Moo a deriving (Show) instance Functor Moo where fmap f (Moo a) = Moo (f a) GHC gives me no problem. Then I add something, data (Num a) => Moo a = Moo a deriving (Show) instance Functor Moo where fmap f (Moo a) = Moo (f a) Now GHC gives me the follow error - see bellow. What is the reason behind this? What should I do to correct this? thx, // matFun_v2.hs:16:12: Could not deduce (Num a) from the context () arising from a use of `Moo' at matFun_v2.hs:16:12-14 Possible fix: add (Num a) to the context of the type signature for `fmap' In the pattern: Moo a In the definition of `fmap': fmap f (Moo a) = Moo (f a) In the instance declaration for `Functor Moo' matFun_v2.hs:16:21: Could not deduce (Num b) from the context () arising from a use of `Moo' at matFun_v2.hs:16:21-29 Possible fix: add (Num b) to the context of the type signature for `fmap' In the expression: Moo (f a) In the definition of `fmap': fmap f (Moo a) = Moo (f a) In the instance declaration for `Functor Moo'

Functor must be defined for all types -- not some restriction thereof (hence you see talk of indexed functors). Thus `fmap` needs to be typeable for any `a -> b` but the construcor `Moo` is only typeable at `a, b` such that we have both `Num a, Num b`. -- Jason Dusek
participants (2)
-
Jason Dusek
-
Matthew Wong