[Haskell-beginners] Mixed typeclasses

Mateusz Neumann mateusz at neumanny.net
Fri Feb 8 13:32:24 CET 2013


Hi

I came across a problem, which I deeply believe, might be solved in
Haskell in much nicer way (than I did it).  I have:

class (Eq a) => ThatsMyProblem a where
   fromMyProblem :: a -> Int
   toMyProblem :: Int -> a

data MyType1
    = MyType1_1
    | MyType1_2
    | MyType1_3 Int
  deriving (Show)
instance Eq MyType1 where
    (==) a b = fromMyProblem a == fromMyProblem b
instance ThatsMyProblem MyType1 where
    [...]

data MyType2
    = MyType2_1
    | MyType2_2 Int
  deriving (Show)
instance Eq MyType2 where
    (==) a b = fromMyProblem a == fromMyProblem b
instance ThatsMyProblem MyType2 where
    [...]

data MyType3
    = MyType3_1
    | MyType3_2
    | MyType3_3 Int
  deriving (Show)
instance Eq MyType3 where
    (==) a b = fromMyProblem a == fromMyProblem b
instance ThatsMyProblem MyType3 where
    [...]


I would very much like to create one single instance like this:

instance (FutureVal a) => Eq a where
    (==) x y = fromFVal x == fromFVal y

but that does not seem to work, as I get an error stating “Illegal
instance declaration for `Eq a' (All instance types must be of the form
(T a1 ... an) where a1 ... an are *distinct type variables*, and each
type variable appears at most once in the instance head. Use
-XFlexibleInstances if you want to disable this.) In the instance
declaration for `Eq a'”

Could you please point me out my mistake and/or direct me to some
documentation?

-- 
Mateusz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 230 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130208/3b197522/attachment.pgp>


More information about the Beginners mailing list