-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_interpS.hs
66 lines (54 loc) · 1.44 KB
/
core_interpS.hs
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
module CoreInterp where
import StateMonad
type Name = String
data Term = Var Name
| ConI Int
| ConS String
| ConC Char
| ConL [Value]
| Add Term Term
| Cat Term Term
| Lam Name Term
| App Term Term
data Value = Num Int
| Str String
| Ch Char
| List [Value]
| Fun (Value -> ST Value)
instance Show Value where
show (Num n) = show n
show (Str s) = show s
show (Ch c) = show c
show (List l) = show l
show (Fun f) = "<function>"
type Environment = [(Name, Value)]
interp :: Term -> Environment -> ST Value
interp (Var x) e = look_up x e
interp (ConI i) e = return (Num i)
interp (ConS s) e = return (Str s)
interp (ConC c) e = return (Ch c)
interp (ConL l) e = return (List l)
interp (Add u v) e = do
a <- interp u e
b <- interp v e
add a b
interp (Lam x v) e = return (Fun (\a -> interp v ((x,a):e)))
interp (App t u) e = do
f <- interp t e
a <- interp u e
apply f a
interp (Cat u v) e = do
s1 <- interp u e
s2 <- interp v e
cat s1 s2
look_up :: Name -> Environment -> ST Value
look_up x ((y,b):e) = if x == y
then return b
else look_up x e
add :: Value -> Value -> ST Value
add (Num i) (Num j) = return (Num (i + j)) >>= tickS
apply :: Value -> Value -> ST Value
apply (Fun k) a = k a
cat :: Value -> Value -> ST Value
cat (Str s1) (Str s2) = return (Str (s1 ++ s2)) >>= tickS
cat (List l1) (List l2) = return (List (l1 ++ l2)) >>= tickS