1
votes
data BoolLit = T | F
instance Eq BoolLit where
   b1 == b2 = True

data BExp = BoolLit |
            Or BExp BExp

bEval :: BExp -> BoolLit
bEval T = T

I'm getting the following syntax error:

Couldn't match expected type 'BExp' with actual type 'BoolLit'   
In the pattern: T   
In an equation for 'bEval': bEval T = T

The data declaration has declared that BoolLit is a BExp.
So, I don't understand why Haskell is giving an error.
I'd like to know why and how to correct it.
Thanks.

1
That's a type error, not a syntax error. - Reid Barton

1 Answers

4
votes

You have declared the type BoolLit with constructors T :: BoolLit and F :: BoolLit, and you have also declared the type BExp with constructors BoolLit :: BExp and Or :: BExp -> BExp -> BExp. If you want to wrap the BoolLit type, you'll need to express that in BExp's constructors:

data BExp = BoolLit BoolLit | Or BExpr BExpr

Then you can write bEval as

bEval (BoolLit T) = T

Also note that you've defined your Eq instance to always return T, no matter what the arguments to == are. You need to pattern match here:

instance Eq BoolLit where
    T == T = True
    F == F = True
    _ == _ = False

Alternatively you can let GHC figure it out for you using deriving:

data BoolLit = T | F deriving (Eq)