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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
(** Copyright 2025-2026, Georgiy Belyanin, Ignat Sergeev *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
module Scope = Set.Make (String)
module Ctx = struct
type t =
{ captured : string list
; locals : Scope.t
; globals : Scope.t
; recs : Scope.t
}
let rec of_pattern = function
| Ast.PUnit | Plug -> Scope.empty
| Ident s -> Scope.singleton s
| PTuple patterns ->
List.map of_pattern patterns |> List.fold_left Scope.union Scope.empty
;;
let mem s (ctx : t) =
Scope.mem s ctx.locals || Scope.mem s ctx.recs || Scope.mem s ctx.globals
;;
let capture name (ctx : t) =
if mem name ctx
then ctx
else { ctx with locals = Scope.add name ctx.locals; captured = name :: ctx.captured }
;;
let extend pattern (ctx : t) =
let names = of_pattern pattern in
{ ctx with locals = Scope.union ctx.locals names }
;;
let global pattern (ctx : t) =
let names = of_pattern pattern in
{ ctx with globals = Scope.union ctx.globals names }
;;
let rec_ pattern (ctx : t) =
let names = of_pattern pattern in
{ ctx with locals = Scope.union ctx.locals names; recs = names }
;;
let nonrec_ pattern (ctx : t) =
let names = of_pattern pattern in
{ ctx with locals = Scope.union ctx.locals names; recs = Scope.empty }
;;
let up (ctx' : t) (ctx : t) =
let captured =
ctx.captured
|> List.filter (fun local ->
(not (Scope.mem local ctx'.locals)) && not (List.mem local ctx'.captured))
in
let captured = captured @ ctx'.captured in
{ ctx' with captured }
;;
let empty =
let scope =
Scope.of_list (Builtin.all |> List.map (fun (builtin : Builtin.t) -> builtin.name))
in
let locals = scope in
let captured = [] in
{ locals; captured; globals = Scope.empty; recs = Scope.empty }
;;
let of_args args (ctx : t) =
let scope =
Scope.of_list (Builtin.all |> List.map (fun (builtin : Builtin.t) -> builtin.name))
in
let locals =
List.map of_pattern args |> List.fold_left Scope.union scope |> Scope.union ctx.recs
in
let captured = [] in
{ ctx with recs = Scope.empty; locals; captured }
;;
end
module State = struct
include State.M (Ctx)
let empty = Ctx.empty
let of_args v ctx = get (Ctx.of_args v ctx)
let extend v ctx = put (Ctx.extend v ctx) ()
let global v ctx = put (Ctx.global v ctx) ()
let rec_ v ctx = put (Ctx.rec_ v ctx) ()
let nonrec_ v ctx = put (Ctx.nonrec_ v ctx) ()
let capture v ctx = put (Ctx.capture v ctx) ()
let up v ctx = put (Ctx.up v ctx) ()
end
open State
let rec cc = function
| Ast.Const _ as c -> return c
| Var name as v ->
let* () = capture name in
return v
| Tuple exprs ->
let rec cc_list = function
| [] -> return []
| hd :: tl ->
let* hd = cc hd in
let* tl = cc_list tl in
return (hd :: tl)
in
let* exprs = cc_list exprs in
return (Ast.tuple exprs)
| Fun (args', body') ->
let* ctx' = get in
let body'', ctx'' = cc body' (of_args args' ctx' |> fst) in
let f = Ast.fun_ args' body'' in
let captured = ctx''.captured in
(match captured with
| [] -> return f
| captured ->
let args = List.map Ast.ident captured in
let f = Ast.fun_ args f in
List.fold_left
(fun f v ->
let* f = f in
let* v = cc (Ast.var v) in
Ast.app f v |> return)
(return f)
captured)
| App (f, g) ->
let* f = cc f in
let* g = cc g in
let a = Ast.app f g in
return a
| Let (rec_flag, pattern, bind, body) ->
let* bind =
let* ctx = get in
let* () =
match rec_flag with
| Ast.Rec -> rec_ pattern
| Ast.NonRec -> nonrec_ pattern
in
let* bind = cc bind in
let* () = up ctx in
return bind
in
let* () = extend pattern in
let* body = cc body in
let l = Ast.let_ rec_flag pattern bind body in
return l
| Ite (cond_, then_, else_) ->
let* cond_ = cc cond_ in
let* then_ = cc then_ in
let* else_ = cc else_ in
let ite = Ast.ite cond_ then_ else_ in
return ite
;;
let cc asts =
List.fold_left
(fun acc (Ast.LetDecl (rec_flag, pattern, body)) ->
let* acc = acc in
let* body =
let* ctx = get in
let* () =
match rec_flag with
| Ast.Rec -> rec_ pattern
| Ast.NonRec -> nonrec_ pattern
in
let* bind = cc body in
let* () = up ctx in
return bind
in
let* () = global pattern in
let ret = Ast.letdecl rec_flag pattern body in
return (ret :: acc))
(return [])
asts
empty
|> fst
|> List.rev
;;
let%expect_test "basic" =
let ast =
Fe.parse
{|
let f = fun a b c ->
let g = fun d -> d + a in
let h = fun e -> e + b in
(g c) + (h c)
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline Ast.pp_top_level)
(cc ast);
[%expect
{|
let f =
fun a b c ->
let g =
(fun a d -> (+) d a) a
in
let h =
(fun b e -> (+) e b) b
in
(+) (g c) (h c)
;;
|}]
;;
let%expect_test "basic 2" =
let ast =
Fe.parse
{|
let f = fun a b c ->
let g = fun d -> a + d in
let h = fun e -> b + g e in
let i = fun f -> c + h f + g f + p in
i (a + b + c + q)
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline Ast.pp_top_level)
(cc ast);
[%expect
{|
let f =
(fun q p a b c ->
let g =
(fun a d -> (+) a d) a
in
let h =
(fun g b e -> (+) b (g e)) g b
in
let i =
(fun p g h c f -> (+) ((+) ((+) c (h f)) (g f)) p) p g h c
in
i ((+) ((+) ((+) a b) c) q)) q p
;;
|}]
;;
let%expect_test "recursion" =
let ast =
Fe.parse
{|
let rec f = fun a b ->
let p = f a in
let rec g = fun n -> if (n = 1) then f (p n) n else g (f a b) in
g (a + b)
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline Ast.pp_top_level)
(cc ast);
[%expect
{|
let rec f =
fun a b ->
let p =
f a
in
let rec g =
(fun b a p f n -> if (=) n 1 then f (p n) n else g (f a b)) b a p f
in
g ((+) a b)
;;
|}]
;;
let%expect_test "multiple" =
let ast =
Fe.parse
{|
let rec f = fun n ->
if n = 1 then 1
else (f (n - 1)) * n
;;
let main = fun () ->
let _ = print_int (f 1) in
let _ = print_int (f 2) in
let _ = print_int (f 5) in
print_int (f 8)
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline Ast.pp_top_level)
(cc ast);
[%expect
{|
let rec f = fun n -> if (=) n 1 then 1 else (*) (f ((-) n 1)) n;;
let main =
fun () ->
let _ =
print_int (f 1)
in
let _ =
print_int (f 2)
in
let _ =
print_int (f 5)
in
print_int (f 8)
;;
|}]
;;
let%expect_test "multiple 2" =
let ast =
Fe.parse
{|
let rec f = fun n ->
if n = 1 then 1
else (f (n - 1)) * n
;;
let b = 1;;
let main = fun () ->
let c = 3 in
let f = fun () -> print_int (f a) in
let f = fun () -> print_int (f (a + b)) in
let f = fun () -> print_int (f (a + c)) in
print_int (f 8)
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline Ast.pp_top_level)
(cc ast);
[%expect
{|
let rec f = fun n -> if (=) n 1 then 1 else (*) (f ((-) n 1)) n;;
let b = 1;;
let main =
(fun a () ->
let c =
3
in
let f =
(fun a () -> print_int (f a)) a
in
let f =
(fun a () -> print_int (f ((+) a b))) a
in
let f =
(fun c a () -> print_int (f ((+) a c))) c a
in
print_int (f 8)) a
;;
|}]
;;