1 {-# OPTIONS_GHC -Wall #-}
8 eval :: ExprT -> Integer
12 eval (Add e1 e2) = eval e1 + eval e2
13 eval (Mul e1 e2) = eval e1 * eval e2
16 evalStr :: String -> Maybe Integer
17 -- evalStr s = case (parseExp Lit Add Mul s) of
18 -- Just e -> Just (eval e)
20 evalStr s = fmap eval (parseExp Lit Add Mul s)
27 instance Expr ExprT where
32 reify :: ExprT -> ExprT
35 instance Expr Integer where
40 instance Expr Bool where
41 lit x | x <= 0 = False
46 newtype MinMax = MinMax Integer deriving (Show, Eq)
48 instance Expr MinMax where
50 add (MinMax x) (MinMax y) = MinMax (max x y)
51 mul (MinMax x) (MinMax y) = MinMax (min x y)
53 newtype Mod7 = Mod7 Integer deriving (Show, Eq)
55 instance Expr Mod7 where
56 lit x = Mod7 (x `mod` 7)
57 add (Mod7 x) (Mod7 y) = Mod7 ((x+y) `mod` 7)
58 mul (Mod7 x) (Mod7 y) = Mod7 ((x*y) `mod` 7)