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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
(** Copyright 2025-2026, Georgiy Belyanin, Ignat Sergeev *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
type immexpr =
| ImmNum of int
| ImmUnit
| ImmId of string
| ImmTuple of immexpr list
[@@deriving variants]
let rec pp_immexpr ppf = function
| ImmNum d -> Format.fprintf ppf "%d" d
| ImmUnit -> Format.fprintf ppf "()"
| ImmId s -> Format.fprintf ppf "%s" s
| ImmTuple s ->
Format.fprintf
ppf
"(%a)"
(Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf ", ") pp_immexpr)
s
;;
type cexpr =
| CImm of immexpr
| CIte of immexpr * aexpr * aexpr
| CApp of string * immexpr list
and aexpr =
| ALet of string * cexpr * aexpr
| AExpr of cexpr
let cimm imm = CImm imm
let cite cond_ then_ else_ = CIte (cond_, then_, else_)
let capp f args = CApp (f, args)
let alet bind v body = ALet (bind, v, body)
let aexpr cexpr = AExpr cexpr
let rec pp_cexpr ppf = function
| CImm imm -> Format.fprintf ppf "%a" pp_immexpr imm
| CIte (cond_, then_, else_) ->
Format.fprintf
ppf
"if %a then@;<1 2>@[<hv>%a@]@;<1 0>else@;<1 2>@[<hv>%a@]"
pp_immexpr
cond_
pp_aexpr
then_
pp_aexpr
else_
| CApp (s, immexprs) ->
Format.fprintf
ppf
"(%s) %a"
s
(Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf " ") pp_immexpr)
immexprs
and pp_aexpr ppf = function
| ALet (name, cexpr, aexpr) ->
Format.fprintf
ppf
"let %s =@;<1 2>@[<hv>%a@]@;<1 0>in@;<1 0>%a"
name
pp_cexpr
cexpr
pp_aexpr
aexpr
| AExpr cexpr -> Format.fprintf ppf "%a" pp_cexpr cexpr
;;
type decl = Decl of Ast.rec_flag * string * string list * aexpr [@@deriving variants]
let pp_decl ppf = function
| Decl (rec_flag, name, args, body) ->
let rec_flag =
match rec_flag with
| Rec -> "rec "
| NonRec -> ""
in
Format.fprintf
ppf
"@[<hv>let %s%s %a =@;<1 2>@[<hv>%a@]@;<0 0>;;@]@."
rec_flag
name
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf " ")
Format.pp_print_string)
args
pp_aexpr
body
;;
module Ctx = struct
type t = { syms : string list }
let addsym v (ctx : t) = { syms = v :: ctx.syms }
let gensym ?prefix () (ctx : t) =
let prefix = Option.value prefix ~default:"sup" in
let rec aux i =
let v = String.cat prefix (Int.to_string i) in
if List.mem v ctx.syms then aux (i + 1) else v
in
let v = aux (List.length ctx.syms) in
v, addsym v ctx
;;
end
module State = struct
include State.M (Ctx)
let addsym sym ctx = put (Ctx.addsym sym ctx) ()
let gensym = Ctx.gensym
end
open State
let rec arg = function
| Ast.PUnit -> return ("()", [])
| Ast.Plug -> return ("_", [])
| Ast.Ident name -> return (name, [])
| Ast.PTuple els ->
let* els =
List.fold_right
(fun a acc ->
let* acc = acc in
let* a = arg a in
return (a :: acc))
els
(return [])
in
let* sym = gensym () in
let lets =
List.mapi (fun i (name, _) -> name, capp "tuple_nth" [ immid sym; immnum i ]) els
in
let lets = lets @ List.concat_map snd els in
return (sym, lets)
;;
let rec anf (k : immexpr -> Ctx.t -> aexpr * Ctx.t) = function
| Ast.Const d ->
let imm =
match d with
| Ast.CInt d -> immnum d
| Ast.CUnit -> ImmUnit
in
let* ret = k imm in
return ret
| Var s ->
let* () = addsym s in
let* ret = k (immid s) in
return ret
| Tuple exprs ->
let rec anf_list immexprs = function
| [] ->
let* tsym = gensym () in
let* expr = k (immid tsym) in
return (alet tsym (cimm (immtuple (List.rev immexprs))) expr)
| hd :: tl -> anf (fun immhd -> anf_list (immhd :: immexprs) tl) hd
in
anf_list [] exprs
| App _ as app ->
let rec aux immexprs = function
| Ast.Var s ->
let* sym = gensym () in
let* expr = k (immid sym) in
let ret = alet sym (capp s immexprs) expr in
return ret
| App (f', expr') ->
anf
(fun immexpr ->
let* f' = aux (immexpr :: immexprs) f' in
return f')
expr'
| f ->
anf
(fun immf ->
let* sym = gensym () in
let* sym' = gensym () in
let* expr = k (immid sym') in
return (alet sym (cimm immf) (alet sym' (capp sym immexprs) expr)))
f
in
aux [] app
| Let (_rec, name, bind, expr) ->
let* name, lets = arg name in
let* () = addsym name in
let* ret =
anf
(fun immbind ->
let* expr = anf k expr in
let expr =
List.fold_right (fun (name, bind) acc -> alet name bind acc) lets expr
in
let ret = alet name (cimm immbind) expr in
return ret)
bind
in
return ret
| Ite (cond_, then_, else_) ->
let* ret =
anf
(fun immcond ->
let* then_ = anf (fun imm -> return (aexpr (cimm imm))) then_ in
let* else_ = anf (fun imm -> return (aexpr (cimm imm))) else_ in
let* sym = gensym ~prefix:"ite" () in
let* expr = k (immid sym) in
let ret = alet sym (cite immcond then_ else_) expr in
return ret)
cond_
in
return ret
| Fun _ -> failwith "should be CC/LL first"
;;
let anf program =
List.fold_right
(fun a acc ->
let* acc = acc in
match a with
| Ast.LetDecl (rec_flag, name, Fun (args, body)) ->
let* args =
List.fold_right
(fun a acc ->
let* acc = acc in
let* a = arg a in
return (a :: acc))
args
(return [])
in
let args, lets = List.split args in
let lets = List.concat lets in
let* body = anf (fun imm -> return (aexpr (cimm imm))) body in
let* name, lets' = arg name in
let body =
List.fold_right (fun (name, bind) acc -> alet name bind acc) lets body
in
let ret = decl rec_flag name args body in
let lets' =
List.map (fun (name, bind) -> decl Ast.nonrec_ name [] (aexpr bind)) lets'
in
return ((ret :: lets') @ acc)
| Ast.LetDecl (rec_flag, name, v) ->
let* v = anf (fun imm -> return (aexpr (cimm imm))) v in
let* name, lets = arg name in
let v = List.fold_right (fun (name, bind) acc -> alet name bind acc) lets v in
let ret = decl rec_flag name [] v in
return (ret :: acc))
program
(return [])
{ syms = [] }
|> fst
;;
let%expect_test "basic" =
let ast =
Fe.parse
{|
let f =
let q = f ((g + sup0) * (2 * i)) in
q ;;
|}
|> Result.get_ok
in
Format.printf
"%a"
(Format.pp_print_list ~pp_sep:Format.pp_print_newline pp_decl)
(anf ast);
[%expect
{|
let f =
let sup2 =
(*) 2 i
in
let sup5 =
(+) g sup0
in
let sup6 =
(*) sup5 sup2
in
let sup7 =
(f) sup6
in
let q =
sup7
in
q
;;
|}]
;;
let%expect_test "ite" =
let ast =
Fe.parse
{|
let rec fac =
if (k = 1) then (1) else (fac (k - 1) * k)
;;
|}
|> 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 pp_decl)
(anf ast);
[%expect
{|
let rec fac =
let sup1 =
(=) k 1
in
let ite7 =
if sup1 then
1
else
let sup4 =
(-) k 1
in
let sup5 =
(fac) sup4
in
let sup6 =
(*) sup5 k
in
sup6
in
ite7
;;
|}]
;;
let%expect_test "task 2" =
let asts =
Fe.parse
{|
let main =
let x = if (if 0 then 1 else 2)
then 0 else 1 in
large x
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
|> Cc.cc
|> Ll.ll
|> anf
in
Format.printf "%a" (Format.pp_print_list ~pp_sep:Format.pp_print_newline pp_decl) asts;
[%expect
{|
let main =
let ite1 =
if 0 then 1 else 2
in
let ite2 =
if ite1 then 0 else 1
in
let x =
ite2
in
let sup4 =
(large) x
in
sup4
;;
|}]
;;
let%expect_test "task 3" =
let asts =
Fe.parse
{|
let (f, g) = fun a (b, c) ->
let (b1, b2) = b in
let (c1, c2) = c in
b1 + b2 + c1 + c2
;;
|}
|> Result.map_error (fun err -> Format.printf "Error %s" err)
|> Result.get_ok
|> Cc.cc
|> Ll.ll
|> anf
in
Format.printf "%a" (Format.pp_print_list ~pp_sep:Format.pp_print_newline pp_decl) asts;
[%expect
{|
let sup14 a sup0 =
let b =
(tuple_nth) sup0 0
in
let c =
(tuple_nth) sup0 1
in
let sup1 =
b
in
let b1 =
(tuple_nth) sup1 0
in
let b2 =
(tuple_nth) sup1 1
in
let sup4 =
c
in
let c1 =
(tuple_nth) sup4 0
in
let c2 =
(tuple_nth) sup4 1
in
let sup11 =
(+) b1 b2
in
let sup12 =
(+) sup11 c1
in
let sup13 =
(+) sup12 c2
in
sup13
;;
let f = (tuple_nth) sup14 0;;
let g = (tuple_nth) sup14 1;;
|}]
;;