-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.ml
219 lines (206 loc) · 10.7 KB
/
closure.ml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
type closure = { entry : Id.l; actual_fv : Id.t list }
type t = (* クロージャ変換後の式 (caml2html: closure_t) *)
| Unit
| Int of int
| Float of float
| Neg of Id.t
| Add of Id.t * Id.t
| Sub of Id.t * Id.t
| Mul of Id.t * Id.t
| Div of Id.t * Id.t
| FNeg of Id.t
| FAdd of Id.t * Id.t
| FSub of Id.t * Id.t
| FMul of Id.t * Id.t
| FDiv of Id.t * Id.t
| IfEq of Id.t * Id.t * t * t
| IfNEq of Id.t * Id.t * t * t
| IfLE of Id.t * Id.t * t * t
| IfLT of Id.t * Id.t * t * t
| Let of (Id.t * Type.t) * t * t
| Var of Id.t
| MakeCls of (Id.t * Type.t) * closure * t
| AppCls of Id.t * Id.t list
| AppDir of Id.l * Id.t list
| Tuple of Id.t list
| LetTuple of (Id.t * Type.t) list * Id.t * t
| Get of Id.t * Id.t
| Put of Id.t * Id.t * Id.t
| ExtArray of Id.l
| ExprWithPos of t * Syntax.pos (* Oct. 14. 2021 *)
type fundef = { name : Id.l * Type.t;
args : (Id.t * Type.t) list;
formal_fv : (Id.t * Type.t) list;
body : t }
type prog = Prog of fundef list * t
let rec fv = function
| Unit | Int(_) | Float(_) | ExtArray(_) -> S.empty
| Neg(x) | FNeg(x) -> S.singleton x
| Add(x, y) | Sub(x, y) | Mul(x, y) | Div(x, y) | FAdd(x, y) | FSub(x, y) | FMul(x, y) | FDiv(x, y) | Get(x, y) -> S.of_list [x; y]
| IfEq(x, y, e1, e2) | IfNEq(x, y, e1, e2) | IfLE(x, y, e1, e2) | IfLT(x, y, e1, e2) -> S.add x (S.add y (S.union (fv e1) (fv e2)))
| Let((x, t), e1, e2) -> S.union (fv e1) (S.remove x (fv e2))
| Var(x) -> S.singleton x
| MakeCls((x, t), { entry = l; actual_fv = ys }, e) -> S.remove x (S.union (S.of_list ys) (fv e))
| AppCls(x, ys) -> S.of_list (x :: ys)
| AppDir(_, xs) | Tuple(xs) -> S.of_list xs
| LetTuple(xts, y, e) -> S.add y (S.diff (fv e) (S.of_list (List.map fst xts)))
| Put(x, y, z) -> S.of_list [x; y; z]
| ExprWithPos(e, p) -> fv e (* Oct. 14. 2021 *)
let toplevel : fundef list ref = ref []
let rec g env known = function (* クロージャ変換ルーチン本体 (caml2html: closure_g) *)
| KNormal.Unit -> Unit
| KNormal.Int(i) -> Int(i)
| KNormal.Float(d) -> Float(d)
| KNormal.Neg(x) -> Neg(x)
| KNormal.Add(x, y) -> Add(x, y)
| KNormal.Sub(x, y) -> Sub(x, y)
| KNormal.Mul(x, y) -> Mul(x, y)
| KNormal.Div(x, y) -> Div(x, y)
| KNormal.FNeg(x) -> FNeg(x)
| KNormal.FAdd(x, y) -> FAdd(x, y)
| KNormal.FSub(x, y) -> FSub(x, y)
| KNormal.FMul(x, y) -> FMul(x, y)
| KNormal.FDiv(x, y) -> FDiv(x, y)
| KNormal.IfEq(x, y, e1, e2) -> IfEq(x, y, g env known e1, g env known e2)
| KNormal.IfNEq(x, y, e1, e2) -> IfNEq(x, y, g env known e1, g env known e2)
| KNormal.IfLE(x, y, e1, e2) -> IfLE(x, y, g env known e1, g env known e2)
| KNormal.IfLT(x, y, e1, e2) -> IfLT(x, y, g env known e1, g env known e2)
| KNormal.Let((x, t), e1, e2) -> Let((x, t), g env known e1, g (M.add x t env) known e2)
| KNormal.Var(x) -> Var(x)
| KNormal.LetRec({ KNormal.name = (x, t); KNormal.args = yts; KNormal.body = e1 }, e2) -> (* 関数定義の場合 (caml2html: closure_letrec) *)
(* 関数定義let rec x y1 ... yn = e1 in e2の場合は、
xに自由変数がない(closureを介さずdirectに呼び出せる)
と仮定し、knownに追加してe1をクロージャ変換してみる *)
let toplevel_backup = !toplevel in
let env' = M.add x t env in
let known' = S.add x known in
let e1' = g (M.add_list yts env') known' e1 in
(* 本当に自由変数がなかったか、変換結果e1'を確認する *)
(* 注意: e1'にx自身が変数として出現する場合はclosureが必要!
(thanks to nuevo-namasute and azounoman; test/cls-bug2.ml参照) *)
let zs = S.diff (fv e1') (S.of_list (List.map fst yts)) in
let known', e1' =
if S.is_empty zs then known', e1' else
(* 駄目だったら状態(toplevelの値)を戻して、クロージャ変換をやり直す *)
(Format.eprintf "free variable(s) %s found in function %s@." (Id.pp_list (S.elements zs)) x;
Format.eprintf "function %s cannot be directly applied in fact@." x;
toplevel := toplevel_backup;
let e1' = g (M.add_list yts env') known e1 in
known, e1') in
let zs = S.elements (S.diff (fv e1') (S.add x (S.of_list (List.map fst yts)))) in (* 自由変数のリスト *)
let zts = List.map (fun z -> (z, M.find z env')) zs in (* ここで自由変数zの型を引くために引数envが必要 *)
toplevel := { name = (Id.L(x), t); args = yts; formal_fv = zts; body = e1' } :: !toplevel; (* トップレベル関数を追加 *)
let e2' = g env' known' e2 in
if S.mem x (fv e2') then (* xが変数としてe2'に出現するか *)
MakeCls((x, t), { entry = Id.L(x); actual_fv = zs }, e2') (* 出現していたら削除しない *)
else
(Format.eprintf "eliminating closure(s) %s@." x;
e2') (* 出現しなければMakeClsを削除 *)
| KNormal.App(x, ys) when S.mem x known -> (* 関数適用の場合 (caml2html: closure_app) *)
Format.eprintf "directly applying %s@." x;
AppDir(Id.L(x), ys)
| KNormal.App(f, xs) -> AppCls(f, xs)
| KNormal.Tuple(xs) -> Tuple(xs)
| KNormal.LetTuple(xts, y, e) -> LetTuple(xts, y, g (M.add_list xts env) known e)
| KNormal.Get(x, y) -> Get(x, y)
| KNormal.Put(x, y, z) -> Put(x, y, z)
| KNormal.ExtArray(x) -> ExtArray(Id.L(x))
| KNormal.ExtFunApp(x, ys) -> AppDir(Id.L("min_caml_" ^ x), ys)
| KNormal.ExprWithPos(e, p) -> ExprWithPos(g env known e, p) (* Oct. 14. 2021 *)
let f e =
toplevel := [];
let e' = g M.empty S.empty e in
Prog(List.rev !toplevel, e')
let rec xts2String list acc =
match list with
| [] -> acc
| item :: res -> let (x, t) = item in xts2String res (("("^ x ^ "," ^ (Type.type2string t) ^"),")^acc)
and fundef2String fundef =
let (name, args, formal_fv, body) = (fundef.name, fundef.args, fundef.formal_fv, fundef.body) in
"{name:"
^ (let (L(s1), s2) = name in "(" ^ s1 ^ "," ^ (Type.type2string s2) ^"),")
^ "args:"
^ ("[" ^ (xts2String args "") ^ "],")
^ "formal_fv:"
^ ("[" ^ (xts2String formal_fv "") ^ "],")
^ "body:"
(* ^ (closure2String body) ^ "}" *)
^ (closure2StringWithoutPos body) ^ "}" (*debug用にposをはずしている*)
and fundefList2String list acc =
match list with
| [] -> acc
| item :: res -> fundefList2String res ((fundef2String item) ^ "," ^ acc)
and tList2String list acc =
match list with
| [] -> acc
| item :: res -> tList2String res (item ^ "," ^ acc)
and lList2String list acc =
match list with
| [] -> acc
| Id.L(l) :: res -> lList2String res (l ^ "," ^ acc)
and closure2String t =
match t with
| Unit -> "Unit"
| Int(i) -> "Int(" ^ (Printf.sprintf "%d" i) ^ ")"
| Float(f) -> "Float(" ^ (Printf.sprintf "%f" f) ^ ")"
| Neg(x) -> "Neg(" ^ x ^ ")"
| Add(x, y) -> "Add(" ^ x ^ "," ^ y ^ ")"
| Sub(x, y) -> "Sub(" ^ x ^ "," ^ y ^ ")"
| Mul(x, y) -> "Mul(" ^ x ^ "," ^ y ^ ")"
| Div(x, y) -> "Div(" ^ x ^ "," ^ y ^ ")"
| FNeg(x) -> "FNeg(" ^ x ^ ")"
| FAdd(x, y) -> "FAdd(" ^ x ^ "," ^ y ^ ")"
| FSub(x, y) -> "FSub(" ^ x ^ "," ^ y ^ ")"
| FMul(x, y) -> "FMul(" ^ x ^ "," ^ y ^ ")"
| FDiv(x, y) -> "FDiv(" ^ x ^ "," ^ y ^ ")"
| IfEq(x, y, e1, e2) -> "IfEq(" ^ x ^ "," ^ y ^ "," ^ (closure2String e1) ^ "," ^ (closure2String e2) ^ ")"
| IfNEq(x, y, e1, e2) -> "IfNEq(" ^ x ^ "," ^ y ^ "," ^ (closure2String e1) ^ "," ^ (closure2String e2) ^ ")"
| IfLE(x, y, e1, e2) -> "IfLE(" ^ x ^ "," ^ y ^ "," ^ (closure2String e1) ^ "," ^ (closure2String e2) ^ ")"
| IfLT(x, y, e1, e2) -> "IfLT(" ^ x ^ "," ^ y ^ "," ^ (closure2String e1) ^ "," ^ (closure2String e2) ^ ")"
| Let((x,t), e1, e2) -> "Let((" ^ x ^ "," ^ (Type.type2string t) ^ ")," ^ (closure2String e1) ^ "," ^ (closure2String e2) ^ ")"
| Var(x) -> "Var(" ^ x ^ ")"
| MakeCls((x,t), cls, e) -> let (L(l), list) = (cls.entry, cls.actual_fv) in "MakeCls((" ^ x ^ "," ^ (Type.type2string t) ^ "),{entry:" ^ l ^ ",actual_fv:[" ^ (tList2String list "") ^ "]}," ^ (closure2String e) ^ ")"
| AppCls(x, list) -> "AppCls(" ^ x ^ ",[" ^ (tList2String list "") ^ "])"
| AppDir(Id.L(l), list) -> "AppDir(" ^ l ^ ",[" ^ (tList2String list "") ^ "])"
| Tuple(list) -> "Tuple([" ^ (tList2String list "") ^ "])"
| LetTuple(list, x, e) -> "LetTuple([" ^ (xts2String list "") ^ "]," ^ x ^ "," ^ (closure2String e) ^ ")"
| Get(x, y) -> "Get(" ^ x ^ "," ^ y ^ ")"
| Put(x, y, z) -> "Put(" ^ x ^ "," ^ y ^ "," ^ z ^ ")"
| ExtArray(Id.L(l)) -> "ExtArray(" ^ l ^ ")"
| ExprWithPos(e1, pos) -> "ExprWithPos(" ^ (closure2String e1) ^ ",pos(" ^ (Printf.sprintf "%d" (fst pos)) ^ "," ^ (Printf.sprintf "%d" (snd pos)) ^ "))"
and closureProg2String prog =
let Prog(fundef_list, t) = prog in
"fundef list:\n[" ^ (fundefList2String fundef_list "") ^ "]\n" ^ "t:\n" ^ (closure2String t)
and closure2StringWithoutPos t =
match t with
| Unit -> "Unit"
| Int(i) -> "Int(" ^ (Printf.sprintf "%d" i) ^ ")"
| Float(f) -> "Float(" ^ (Printf.sprintf "%f" f) ^ ")"
| Neg(x) -> "Neg(" ^ x ^ ")"
| Add(x, y) -> "Add(" ^ x ^ "," ^ y ^ ")"
| Sub(x, y) -> "Sub(" ^ x ^ "," ^ y ^ ")"
| Mul(x, y) -> "Mul(" ^ x ^ "," ^ y ^ ")"
| Div(x, y) -> "Div(" ^ x ^ "," ^ y ^ ")"
| FNeg(x) -> "FNeg(" ^ x ^ ")"
| FAdd(x, y) -> "FAdd(" ^ x ^ "," ^ y ^ ")"
| FSub(x, y) -> "FSub(" ^ x ^ "," ^ y ^ ")"
| FMul(x, y) -> "FMul(" ^ x ^ "," ^ y ^ ")"
| FDiv(x, y) -> "FDiv(" ^ x ^ "," ^ y ^ ")"
| IfEq(x, y, e1, e2) -> "IfEq(" ^ x ^ "," ^ y ^ "," ^ (closure2StringWithoutPos e1) ^ "," ^ (closure2StringWithoutPos e2) ^ ")"
| IfNEq(x, y, e1, e2) -> "IfNEq(" ^ x ^ "," ^ y ^ "," ^ (closure2StringWithoutPos e1) ^ "," ^ (closure2StringWithoutPos e2) ^ ")"
| IfLE(x, y, e1, e2) -> "IfLE(" ^ x ^ "," ^ y ^ "," ^ (closure2StringWithoutPos e1) ^ "," ^ (closure2StringWithoutPos e2) ^ ")"
| IfLT(x, y, e1, e2) -> "IfLT(" ^ x ^ "," ^ y ^ "," ^ (closure2StringWithoutPos e1) ^ "," ^ (closure2StringWithoutPos e2) ^ ")"
| Let((x,t), e1, e2) -> "Let((" ^ x ^ "," ^ (Type.type2string t) ^ ")," ^ (closure2StringWithoutPos e1) ^ "," ^ (closure2StringWithoutPos e2) ^ ")"
| Var(x) -> "Var(" ^ x ^ ")"
| MakeCls((x,t), cls, e) -> let (L(l), list) = (cls.entry, cls.actual_fv) in "MakeCls((" ^ x ^ "," ^ (Type.type2string t) ^ "),{entry:" ^ l ^ ",actual_fv:[" ^ (tList2String list "") ^ "]}," ^ (closure2StringWithoutPos e) ^ ")"
| AppCls(x, list) -> "AppCls(" ^ x ^ ",[" ^ (tList2String list "") ^ "])"
| AppDir(Id.L(l), list) -> "AppDir(" ^ l ^ ",[" ^ (tList2String list "") ^ "])"
| Tuple(list) -> "Tuple([" ^ (tList2String list "") ^ "])"
| LetTuple(list, x, e) -> "LetTuple([" ^ (xts2String list "") ^ "]," ^ x ^ "," ^ (closure2StringWithoutPos e) ^ ")"
| Get(x, y) -> "Get(" ^ x ^ "," ^ y ^ ")"
| Put(x, y, z) -> "Put(" ^ x ^ "," ^ y ^ "," ^ z ^ ")"
| ExtArray(Id.L(l)) -> "ExtArray(" ^ l ^ ")"
| ExprWithPos(e1, pos) -> closure2StringWithoutPos e1
let closureProg2StringWithoutPos prog =
let Prog(fundef_list, t) = prog in
"fundef list:\n[" ^ (fundefList2String fundef_list "") ^ "]\n" ^ "t:\n" ^ (closure2StringWithoutPos t)