-
Notifications
You must be signed in to change notification settings - Fork 2
/
Expr.elm
66 lines (55 loc) · 1.33 KB
/
Expr.elm
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
module Brands.Expr exposing (..)
import Brands.Leibniz exposing (..)
type Expr a
= Val Int (Eq Int a)
| Boolean Bool (Eq Bool a)
| Add (Expr Int) (Expr Int) (Eq Int a)
| Eq (Expr Int) (Expr Int) (Eq Bool a)
| If (Expr Bool) (Expr a) (Expr a) (Eq a a)
val : Int -> Expr Int
val n =
Val n refl
boolean : Bool -> Expr Bool
boolean b =
Boolean b refl
add : Expr Int -> Expr Int -> Expr Int
add e1 e2 =
Add e1 e2 refl
eq : Expr Int -> Expr Int -> Expr Bool
eq e1 e2 =
Eq e1 e2 refl
if' : Expr Bool -> b -> Expr a -> b -> Expr a -> Expr a
if' b _ true _ false =
If b true false refl
then' : ()
then' =
()
else' : ()
else' =
()
eval : Expr a -> a
eval expr =
case expr of
Val n prf ->
cast prf n
Boolean b prf ->
cast prf b
Add e1 e2 prf ->
cast prf (eval e1 + eval e2)
Eq e1 e2 prf ->
cast prf (eval e1 == eval e2)
If b true false prf ->
cast prf (if eval b then eval true else eval false)
pretty : Expr a -> String
pretty expr =
case expr of
Val n prf ->
toString (cast prf n)
Boolean b prf ->
toString (cast prf b)
Add e1 e2 _ ->
"(" ++ pretty e1 ++ " + " ++ pretty e2 ++ ")"
Eq e1 e2 _ ->
"(" ++ pretty e1 ++ " == " ++ pretty e2 ++ ")"
If b true false _ ->
"if " ++ pretty b ++ " then " ++ pretty true ++ " else " ++ pretty false