-
Notifications
You must be signed in to change notification settings - Fork 125
/
ocamlWalk.ml
669 lines (556 loc) · 18.6 KB
/
ocamlWalk.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
(*
Copyright © 2011 MLstate
This file is part of Opa.
Opa is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
Opa is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with Opa. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)
(* depends *)
module List = Base.List
(* refactoring in progress *)
module OcamlAst = Ocaml
(* shorthands *)
module O = OcamlAst
(* alias *)
(* -- *)
module Ty_Subs : TraverseInterface.S2
with type 'a t = OcamlAst.type_expr constraint 'a = _ * _ * _ =
struct
type 'a t = OcamlAst.type_expr constraint 'a = _ * _ * _
let foldmap tra acc ty =
match ty with
| O.TypeVar _ ->
acc, ty
| O.TypeName (params, type_name) ->
let acc, f_params = List.fold_left_map_stable tra acc params in
acc,
if params == f_params then ty else
O.TypeName (f_params, type_name)
| O.TypeConst _ ->
acc, ty
| O.TypeRef tr ->
let acc, f_tr = tra acc tr in
acc,
if tr == f_tr then ty else
O.TypeRef f_tr
| O.TypeTuple tyl ->
let acc, f_tyl = List.fold_left_map_stable tra acc tyl in
acc,
if tyl == f_tyl then ty else
O.TypeTuple f_tyl
| O.TypeRecord fields ->
let fmap acc ((bool, field, ty) as tpl) =
let acc, fty = tra acc ty in
acc,
if ty = fty then tpl else (bool, field, fty)
in
let acc, f_fields = List.fold_left_map_stable fmap acc fields in
acc,
if fields == f_fields then ty else
O.TypeRecord f_fields
| O.TypeConstructor ctl ->
let fmap acc ((k, opt) as cpl) =
let acc, f_opt = Option.foldmap_stable tra acc opt in
acc,
if opt == f_opt then cpl else (k, f_opt)
in
let acc, fctl = List.fold_left_map_stable fmap acc ctl in
acc,
if ctl == fctl then ty else
O.TypeConstructor fctl
| O.TypeArrow (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then ty else
O.TypeArrow (fa, fb)
| O.TypeLabel (bool, label, tb) ->
let acc, ftb = tra acc tb in
acc,
if tb == ftb then ty else
O.TypeLabel (bool, label, ftb)
| O.TypeVerbatim _ ->
acc, ty
let iter x = Traverse.Unoptimized.iter foldmap x
let map x = Traverse.Unoptimized.map foldmap x
let fold x = Traverse.Unoptimized.fold foldmap x
end
module Ty = Traverse.Make2 ( Ty_Subs )
module Pat_Subs : TraverseInterface.S2
with type 'a t = OcamlAst.pattern constraint 'a = _ * _ * _ =
struct
type 'a t = OcamlAst.pattern constraint 'a = _ * _ * _
let foldmap tra acc pat =
match pat with
| O.PatVar _ ->
acc, pat
| O.PatList (hd, tl) ->
let acc, fhd = tra acc hd in
let acc, ftl = tra acc tl in
acc,
if hd == fhd && tl == ftl then pat else
O.PatList (fhd, ftl)
| O.PatEmptyList ->
acc, pat
| O.PatRecord fields ->
let fmap acc ((label, pat) as cpl) =
let acc, fpat = tra acc pat in
acc,
if pat == fpat then cpl else (label, fpat)
in
let acc, f_fields = List.fold_left_map_stable fmap acc fields in
acc,
if fields == f_fields then pat else
O.PatRecord f_fields
| O.PatConstructor (ident, ptl) ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatConstructor (ident, f_ptl)
| O.PatVariant (ident, ptl) ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatVariant (ident, f_ptl)
| O.PatPVariant (ident, ptl) ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatPVariant (ident, f_ptl)
| O.PatConst _ ->
acc, pat
| O.PatAny ->
acc, pat
| O.PatAnnot (pa, ty) ->
let acc, fpa = tra acc pa in
acc,
if pa == fpa then pat else
O.PatAnnot (fpa, ty)
| O.PatTuple ptl ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatTuple f_ptl
| O.PatAs (pa, ident) ->
let acc, fpa = tra acc pa in
acc,
if pa == fpa then pat else
O.PatAs (fpa, ident)
| O.PatArray ptl ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatArray f_ptl
| O.PatLazy p ->
let acc, fp = tra acc p in
acc,
if p == fp then pat else
O.PatLazy fp
| O.PatOr ptl ->
let acc, f_ptl = List.fold_left_map_stable tra acc ptl in
acc,
if ptl == f_ptl then pat else
O.PatOr f_ptl
let iter x = Traverse.Unoptimized.iter foldmap x
let map x = Traverse.Unoptimized.map foldmap x
let fold x = Traverse.Unoptimized.fold foldmap x
end
module Pat = Traverse.Make2 ( Pat_Subs )
module Expr_Subs : TraverseInterface.S2
with type 'a t = OcamlAst.expr constraint 'a = _ * _ * _ =
struct
type 'a t = OcamlAst.expr constraint 'a = _ * _ * _
let rec foldmap tra acc expr =
match expr with
| O.Type _ ->
acc, expr
| O.Val _ ->
acc, expr
| O.Open _ ->
acc, expr
| O.Module (name, expr2, code, expr3) ->
let acc, fexpr2 = Option.foldmap_stable tra acc expr2 in
let acc, fcode = foldmap_code tra acc code in
let acc, fexpr3 = Option.foldmap_stable tra acc expr3 in
acc,
if expr2 == fexpr2 && code == fcode && expr3 == fexpr3 then expr else
O.Module (name, fexpr2, fcode, fexpr3)
| O.ModuleType (name, code) ->
let acc, fcode = foldmap_code tra acc code in
acc,
if code == fcode then expr else
O.ModuleType (name, fcode)
| O.Structure code ->
let acc, fcode = foldmap_code tra acc code in
acc,
if code == fcode then expr else
O.Structure fcode
| O.Signature signature ->
let acc, fsignature = foldmap_signature tra acc signature in
acc,
if signature == fsignature then expr else
O.Signature fsignature
| O.DeclareFunctor (name, seol, eo, e) ->
let fmap acc ((s, (eo : O.expr option)) as cpl) =
let acc, feo = Option.foldmap_stable tra acc eo in
acc,
if eo == feo then cpl else (s, feo)
in
let acc, fseol = List.fold_left_map_stable fmap acc seol in
let acc, feo = Option.foldmap_stable tra acc eo in
let acc, fe = tra acc e in
acc,
if seol == fseol && eo == feo && e == fe then expr else
O.DeclareFunctor (name, fseol, feo, fe)
| O.Constructor (ident, el) ->
let acc, fel = List.fold_left_map_stable tra acc el in
acc,
if el == fel then expr else
O.Constructor (ident, fel)
| O.ConstructorPV (ident, el) ->
let acc, fel = List.fold_left_map_stable tra acc el in
acc,
if el == fel then expr else
O.ConstructorPV (ident, fel)
| O.Const _ ->
acc, expr
| O.Var ep ->
let acc, fep = foldmap_effective_param tra acc ep in
acc,
if ep == fep then expr else
O.Var fep
| O.MakeRef e ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.MakeRef fe
| O.GetRef e ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.GetRef fe
| O.SetRef (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then expr else
O.SetRef (fa, fb)
| O.SetMutable (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then expr else
O.SetMutable (fa, fb)
| O.Lazy e ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.Lazy fe
| O.Tuple el ->
let acc, fel = List.fold_left_map_stable tra acc el in
acc,
if el == fel then expr else
O.Tuple fel
| O.Cons (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then expr else
O.Cons (fa, fb)
| O.EmptyList ->
acc, expr
| O.Cond (a, b, c) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
let acc, fc = tra acc c in
acc,
if a == fa && b == fb && c == fc then expr else
O.Cond (a, b, c)
| O.App (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then expr else
O.App (fa, fb)
| O.Abs (fps, e) ->
let acc, f_fps = List.fold_left_map_stable (foldmap_formal_param tra) acc fps in
let acc, fe = tra acc e in
acc,
if fps == f_fps && e == fe then expr else
O.Abs (f_fps, fe)
| O.Let bind ->
let fmap acc ((fp, e) as cpl) =
let acc, f_fp = foldmap_formal_param tra acc fp in
let acc, fe = tra acc e in
acc,
if fp == f_fp && e == fe then cpl else (f_fp, fe)
in
let acc, fbind = List.fold_left_map_stable fmap acc bind in
acc,
if bind == fbind then expr else
O.Let fbind
| O.Letrec bind ->
let fmap acc ((fp, e) as cpl) =
let acc, f_fp = foldmap_formal_param tra acc fp in
let acc, fe = tra acc e in
acc,
if fp == f_fp && e == fe then cpl else (f_fp, fe)
in
let acc, fbind = List.fold_left_map_stable fmap acc bind in
acc,
if bind == fbind then expr else
O.Letrec fbind
| O.Letin (bind, e) ->
let fmap acc ((fp, e) as cpl) =
let acc, f_fp = foldmap_formal_param tra acc fp in
let acc, fe = tra acc e in
acc,
if fp == f_fp && e == fe then cpl else (f_fp, fe)
in
let acc, fbind = List.fold_left_map_stable fmap acc bind in
let acc, fe = tra acc e in
acc,
if bind == fbind && e == fe then expr else
O.Letin (fbind, fe)
| O.Letrecin (bind, e) ->
let fmap acc ((fp, e) as cpl) =
let acc, f_fp = foldmap_formal_param tra acc fp in
let acc, fe = tra acc e in
acc,
if fp == f_fp && e == fe then cpl else (f_fp, fe)
in
let acc, fbind = List.fold_left_map_stable fmap acc bind in
let acc, fe = tra acc e in
acc,
if bind == fbind && e == fe then expr else
O.Letrecin (fbind, fe)
| O.Record (rec_opt, fields) ->
let fmap acc ((f, e) as cpl) =
let acc, fe = tra acc e in
acc,
if e == fe then cpl else (f, fe)
in
let acc, f_fields = List.fold_left_map_stable fmap acc fields in
acc,
if fields == f_fields then expr else
O.Record (rec_opt, f_fields)
| O.Dot (e, f) ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.Dot (fe, f)
| O.Match (e, pl) ->
let fmap acc ( (p, g, e) as tpl )=
let acc, fg = Option.foldmap_stable tra acc g in
let acc, fe = tra acc e in
acc,
if g == fg && e == fe then tpl else (p, fg, fe)
in
let acc, fe = tra acc e in
let acc, fpl = List.fold_left_map_stable fmap acc pl in
acc,
if e == fe && pl == fpl then expr else
O.Match (fe, fpl)
| O.Sequence (a, b) ->
let acc, fa = tra acc a in
let acc, fb = tra acc b in
acc,
if a == fa && b == fb then expr else
O.Sequence (fa, fb)
| O.Annot (e, ty) ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.Annot (fe, ty)
| O.Function fpel ->
let fmap acc ( (p, g, e) as tpl )=
let acc, fg = Option.foldmap_stable tra acc g in
let acc, fe = tra acc e in
acc,
if g == fg && e == fe then tpl else (p, fg, fe)
in
let acc, f_fpel = List.fold_left_map_stable fmap acc fpel in
acc,
if fpel == f_fpel then expr else
O.Function f_fpel
| O.Exception _ ->
acc, expr
| O.Raise (ident, eo) ->
let acc, feo = Option.foldmap_stable tra acc eo in
acc,
if eo == feo then expr else
O.Raise (ident, feo)
| O.Try (e, pl) ->
let fmap acc ( (p, g, e) as tpl )=
let acc, fg = Option.foldmap_stable tra acc g in
let acc, fe = tra acc e in
acc,
if g == fg && e == fe then tpl else (p, fg, fe)
in
let acc, fe = tra acc e in
let acc, fpl = List.fold_left_map_stable fmap acc pl in
acc,
if e == fe && pl == fpl then expr else
O.Try (fe, fpl)
| O.AnArray el ->
let acc, fel = List.fold_left_map_stable tra acc el in
acc,
if el == fel then expr else
O.AnArray fel
| O.Comment _ ->
acc, expr
| O.LineAnnot (i, s, e) ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.LineAnnot (i, s, fe)
| O.Comments (s, e) ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.Comments (s, fe)
| O.Assert e ->
let acc, fe = tra acc e in
acc,
if e == fe then expr else
O.Assert fe
| O.Verbatim _ ->
acc, expr
and foldmap_formal_param tra acc fp =
match fp with
| O.Label _ ->
acc, fp
| O.Opt (label, ty, expr) ->
let acc, fexpr = Option.foldmap_stable tra acc expr in
acc,
if expr == fexpr then fp else
O.Opt (label, ty, fexpr)
| O.Pat _ ->
acc, fp
and foldmap_effective_param tra acc ep =
match ep with
| O.Labeled (label, expr) ->
let acc, fexpr = Option.foldmap_stable tra acc expr in
acc,
if expr == fexpr then ep else
O.Labeled (label, fexpr)
| O.Pated _ ->
acc, ep
and foldmap_code tra acc code = List.fold_left_map_stable tra acc code
and foldmap_signature tra acc sign =
match sign with
| O.Inlined code ->
let acc, fcode = foldmap_code tra acc code in
acc,
if code == fcode then sign else
O.Inlined fcode
| O.Referenced _ ->
acc, sign
let iter x = Traverse.Unoptimized.iter foldmap x
let map x = Traverse.Unoptimized.map foldmap x
let fold x = Traverse.Unoptimized.fold foldmap x
end
module Expr = Traverse.Make2 ( Expr_Subs )
module PatExpr =
struct
let formal_param_pat_non_rec f_pat acc fp =
match fp with
| O.Label (s, pat, t) ->
let acc, f_pat = Option.foldmap_stable f_pat acc pat in
acc,
if pat == f_pat then fp else
O.Label (s, f_pat, t)
| O.Opt _ ->
acc, fp
| O.Pat pat ->
let acc, f_pat = f_pat acc pat in
acc,
if pat == f_pat then fp else
O.Pat f_pat
let fmap_fp_e f_pat acc ((fp, e) as cpl) =
let acc, f_fp = formal_param_pat_non_rec f_pat acc fp in
acc,
if fp == f_fp then cpl else (f_fp, e)
let fmap_pge f_pat acc ((p, g, e) as tpl) =
let acc, fp = f_pat acc p in
acc,
if p == fp then tpl else (fp, g, e)
let foldmap_expr_pat_non_rec f_expr f_pat acc expr =
let foldmap acc expr =
let acc, expr =
match expr with
| O.Abs (fpl, e) ->
let acc, f_fpl = List.fold_left_map_stable (formal_param_pat_non_rec f_pat) acc fpl in
acc,
if fpl == f_fpl then expr else
O.Abs (f_fpl, e)
| O.Let fpel ->
let acc, f_fpel = List.fold_left_map_stable (fmap_fp_e f_pat) acc fpel in
acc,
if fpel == f_fpel then expr else
O.Let f_fpel
| O.Letrec fpel ->
let acc, f_fpel = List.fold_left_map_stable (fmap_fp_e f_pat) acc fpel in
acc,
if fpel == f_fpel then expr else
O.Letrec f_fpel
| O.Letin (fpel, e) ->
let acc, f_fpel = List.fold_left_map_stable (fmap_fp_e f_pat) acc fpel in
acc,
if fpel == f_fpel then expr else
O.Letin (f_fpel, e)
| O.Letrecin (fpel, e) ->
let acc, f_fpel = List.fold_left_map_stable (fmap_fp_e f_pat) acc fpel in
acc,
if fpel == f_fpel then expr else
O.Letrecin (f_fpel, e)
| O.Match (e, pgel) ->
let acc, f_pgel = List.fold_left_map_stable (fmap_pge f_pat) acc pgel in
acc,
if pgel == f_pgel then expr else
O.Match (e, f_pgel)
| O.Function pgel ->
let acc, f_pgel = List.fold_left_map_stable (fmap_pge f_pat) acc pgel in
acc,
if pgel == f_pgel then expr else
O.Function f_pgel
| O.Try (e, pgel) ->
let acc, f_pgel = List.fold_left_map_stable (fmap_pge f_pat) acc pgel in
acc,
if pgel == f_pgel then expr else
O.Try (e, f_pgel)
| _ -> acc, expr
in
f_expr acc expr
in
Expr.foldmap foldmap acc expr
let foldmap f_expr f_pat acc expr =
let f_pat acc pat = Pat.foldmap f_pat acc pat in
foldmap_expr_pat_non_rec f_expr f_pat acc expr
let fold f_expr f_pat acc expr =
let f_expr acc expr = f_expr acc expr, expr in
let f_pat acc pat = f_pat acc pat, pat in
let acc, _ = foldmap f_expr f_pat acc expr in
acc
let map f_expr f_pat expr =
let f_expr () expr = (), f_expr expr in
let f_pat () pat = (), f_pat pat in
let (), expr = foldmap f_expr f_pat () expr in
expr
let iter f_expr f_pat expr =
let f_expr expr = let () = f_expr expr in expr in
let f_pat pat = let () = f_pat pat in pat in
let _ = map f_expr f_pat expr in
()
let foldmap_code f_expr f_pat acc code = List.fold_left_map_stable (foldmap f_expr f_pat) acc code
let fold_code f_expr f_pat acc code = List.fold_left (fold f_expr f_pat) acc code
let map_code f_expr f_pat code = List.map_stable (map f_expr f_pat) code
let iter_code f_expr f_pat code = List.iter (iter f_expr f_pat) code
end