-
Notifications
You must be signed in to change notification settings - Fork 0
/
e1.mana
70 lines (55 loc) · 2.21 KB
/
e1.mana
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
69
70
------------------------------------------------------------------------------
--
-- Integer/Bool Expression
--
------------------------------------------------------------------------------
namespace E1
import Core
------------------------------------------------------------------------------
-- E - expressions
------------------------------------------------------------------------------
type E =
EInt Int
EBool Bool
ENot E
EEqual E E
EAdd E E
EMul E E
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
instance FromInteger E where fromInteger = EInt . fromInteger
instance Add E where (+) = EAdd
instance Multiply E where (*) = EMul
(===) = EEqual
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
type Value = VInt Int | VBool Bool
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
eval : E -> Maybe Value
eval (EInt x ) = Some (VInt x)
eval (EBool x ) = Some (VBool x)
eval (ENot e ) = evalBool e \x -> Some (VBool (not x))
eval (EEqual e d) = evalInt e \x -> evalInt d \y -> Some (VBool (x == y))
eval (EAdd e d) = evalInt e \x -> evalInt d \y -> Some (VInt (x + y ))
eval (EMul e d) = evalInt e \x -> evalInt d \y -> Some (VInt (x * y ))
evalInt : E -> (Int -> Maybe Value) -> Maybe Value
evalInt e f =
case eval e of
Some (VInt x) -> f x
_ -> None
evalBool : E -> (Bool -> Maybe Value) -> Maybe Value
evalBool e f =
case eval e of
Some (VBool x) -> f x
_ -> None
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
main = do
print <| eval <| 5 + 3
print <| eval <| 5 === 3
print <| eval <| ENot (5 === 3)