summaryrefslogtreecommitdiff
path: root/lec05/Calc.hs
blob: 7502b5f786cde80b844d1861f7090239f1383b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Calc where

import ExprT
import Parser

-- Exercise 1

eval :: ExprT -> Integer
eval (Lit n) = n
eval (Add l r) = eval l + eval r
eval (Mul l r) = eval l * eval r

-- Exercise 2

evalStr :: String -> Maybe Integer
evalStr s = case parseExp Lit Add Mul s of
                 Nothing -> Nothing
                 Just t  -> Just (eval t)

-- Exercise 3

class Expr a where
      lit :: Integer -> a
      add :: a -> a -> a
      mul :: a -> a -> a

instance Expr ExprT where
         lit = Lit
         add = Add
         mul = Mul

reify :: ExprT -> ExprT
reify = id

-- Exercise 4

instance Expr Integer where
         lit x   = x
         add l r = eval (Add (Lit l) (Lit r))
         mul l r = eval (Mul (Lit l) (Lit r))

instance Expr Bool where
         lit x
             | x <= 0    = False
             | otherwise = True
         add = (||)
         mul = (&&)

newtype MinMax = MinMax Integer deriving (Eq, Show)
newtype Mod7   = Mod7 Integer deriving (Eq, Show)

instance Expr MinMax where
         lit = MinMax
         add (MinMax l) (MinMax r) = MinMax (max l r)
         mul (MinMax l) (MinMax r) = MinMax (min l r)

instance Expr Mod7 where
         lit x = Mod7 (mod x 7)
         add (Mod7 l) (Mod7 r) = Mod7 (mod (l + r) 7)
         mul (Mod7 l) (Mod7 r) = Mod7 (mod (l * r) 7)

testExp :: Expr a => Maybe a
testExp = parseExp lit add mul "(3 * -4) + 5"

testInteger = testExp :: Maybe Integer
testBool    = testExp :: Maybe Bool
testMM      = testExp :: Maybe MinMax
testSat     = testExp :: Maybe Mod7