I have added | Lit Int and | Add Exp Exp to the data type as seen below, along with the evaluation. However I get an error "Couldn't match expected type ‘Var’ with actual type ‘Int’".
data Exp = V Var
| B Bool
| L Exp
| A Exp Exp
| Lit Int
| Add Exp Exp
data Var = VZ |VS Var
eval:: Exp -> Var
eval (Lit n) = n
eval (Add e1 e2) = eval e1 + eval e2
How can I add Int and Add to the data type, along with the evaluation, but maintain the following code as is. Is this possible?
data Exp = V Var
| B Bool
| L Exp
| A Exp Exp
data Var = VZ |VS Var
I have added an instance to resolve this, as seen below, but now I have the error "Pattern bindings (except simple variables) not allowed in instance declaration: Add e1 e2 = (Lit e1) + (Lit e2)":
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
data Exp = V Var
| B Bool
| L Exp
| A Exp Exp
| Lit Int
| Add Exp Exp
data Var = VZ |VS Var
eval:: Exp -> Var
eval (Lit n) = n
eval (Add e1 e2) = eval e1 + eval e2
instance Num Var where
Lit e = e
instance Num Var where
Add e1 e2 = (Lit e1) + (Lit e2)
eval
has typeExp -> Var
, so that means thateval e1
is aVar
. But you can not add twoVar
s together, or at least not without defining how to do that. – Willem Van Onseminstance
ofNum
, soinstance Num Var where ...
. – Willem Van Onsem