forked from Soonad/Moonad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comp.fm
89 lines (80 loc) · 1.83 KB
/
Comp.fm
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Comp.app: Comp -> Comp -> Comp
(func) (argm)
<> () () (app) () () ()
app(func)(argm)
Comp.fal: Comp
<> () () () () () (fal)
fal
// Untyped, compilation-ready intermediate language
Comp: Type
comp<P: Comp -> Type> ->
(var:
(name: String) ->
P(Comp.var(name))) ->
(lam:
(name: String) ->
(body: Comp) ->
P(Comp.lam(name)(body))) ->
(app:
(func: Comp) ->
(argm: Comp) ->
P(Comp.app(func)(argm))) ->
(let:
(name: String) ->
(expr: Comp) ->
(body: Comp) ->
P(Comp.let(name)(expr)(body))) ->
(tru:
P(Comp.tru)) ->
(fal:
P(Comp.fal)) ->
P(comp)
Comp.lam: String -> Comp -> Comp
(name) (body)
<> () (lam) () () () ()
lam(name)(body)
Comp.let: String -> Comp -> Comp -> Comp
(name) (expr) (body)
<> () () () (Let) () ()
Let(name)(expr)(body)
Comp.stringify: Comp -> Stringifier
(comp)
let lit = String.to_stringifier
let rec = Comp.stringify
case comp:
|#var# (term.name)
lit(term.name);
|#lam# (term.name) (term.body)
let lpar = lit("((")
let name = lit(term.name)
let rpar = lit(") ")
let body = rec(term.body)
let lend = lit(")")
(x) lpar(name(rpar(body(lend(x)))));
|#app# (term.func) (term.argm)
let func = rec(term.func)
let lpar = lit("(")
let argm = rec(term.argm)
let rpar = lit(")")
(x) func(lpar(argm(rpar(x))));
|#let# (term.name) (term.expr) (term.body)
let llet = lit("let ")
let name = lit(term.name)
let leql = lit(" = ")
let expr = rec(term.expr)
let spac = lit("; ")
let body = rec(term.body)
(x) llet(name(leql(expr(spac(body(x))))));
|#tru#
let lval = lit("true")
(x) lval(x);
|#fal#
let lval = lit("false")
(x) lval(x);
Comp.tru: Comp
<> () () () () (tru) ()
tru
Comp.var: String -> Comp
(name)
<P> (var) () () () () ()
var(name)