-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108207329_108207329-Assignment4-1.hs
More file actions
41 lines (40 loc) · 1.14 KB
/
108207329_108207329-Assignment4-1.hs
File metadata and controls
41 lines (40 loc) · 1.14 KB
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
type Name = String
type Env a = [(String,a)]
data Error a = S a
| Error String
deriving (Eq, Show)
data Expr = Var Name
| Num Int --constant
| Expr :+: Expr
| Expr :*: Expr
| Let [Name] Expr Expr
deriving (Eq, Show)
---find
find :: String -> Env Int -> Maybe Int
find t ((var,val):env) =
if t == var then Just val else find t env
find t [] = Nothing
---ev1
ev1 :: Env Int -> Expr -> Error Int
ev1 env (Num x) = S x
ev1 env (l :+: r) = case (ev1 env l) of
S x -> case (ev1 env r) of
S y -> S(x+y)
Error s1 -> Error s1
Error s -> Error s
ev1 env (l :*: r) = case (ev1 env l) of
S x -> case (ev1 env r) of
S y -> S(x*y)
Error s1 -> Error s1
Error s -> Error s
ev1 env (Let [x] e be) = case (ev1 env e) of
S v -> ev1 ([(x,v)]++env) be
Error s -> Error s
ev1 env (Var x) = case (find x env) of
Just x -> S x
Nothing -> Error ("unbounded variable : " ++ x)
main = do
let global = [("x", 1), ("y", 2)] :: [(Name, Int)] in
pretest "ev1" $ ev1 global (Let ["z"] (Num 3) (Num 6 :+: Var "z"))
where
pretest p a = putStrLn $ p ++ " = " ++ show a