2
votes

I'm learning Haskell and I've been given following assignment - I have a newtype consisting of two mixed data types, and I have to make it an instance of Eq without using deriving. Here's what I have:

data Number = One | Two | Three deriving Eq
data Character = A | B | C deriving Eq
newtype Combo = Combo ((Character, Character),(Number, Number))

instance Eq Combo where
    Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)

However, Hugs goes all

ERROR "testing.hs":5 - Ambiguous class occurrence "Eq"
*** Could refer to: Hugs.Prelude.Eq Main.Eq Main.Eq Main.Eq Main.Eq Main.Eq 

How do I fix this? I can't really import Eq hiding something either since I need it to check if a given member of Number or Character is equal.

1
If that's really all code you have, then it might be a bug in Hugs. Also, please note that Hugs is no longer in development. - user824425
Yeah, but I have to use it, unfortunately. - skulpt
are you sure you have not redefined class Eq somewhere in your file (or the grader ... oh wait it's FP101x right? So it probably has no grader) - Random Dev
btw: there is probably an Eq instance for 2-tuples already so you could make this a bit shorter ... something like Combo c == Combo c' = c == c' ;) - Random Dev

1 Answers

3
votes

Changing

instance Eq Combo where
Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)

to

instance Main.Eq Combo where
Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)

apparently fixed that error.