-
Notifications
You must be signed in to change notification settings - Fork 5
/
abbot_impl.sml
1479 lines (1387 loc) · 50.1 KB
/
abbot_impl.sml
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Abbot: implementating abstract binding trees (___.abt.impl.sml) *)
structure AbbotImpl =
struct
open Util
infixr 0 >>
open Analysis
open AbbotCore
open SmlSyntax
fun with_index f x =
let val index = ref 0
in f (fn () => (index := !index + 1; Int.toString (!index))) x
end
fun create_recursors
{name : string,
args : string list,
sym_usef : symbol * EXP -> EXP,
sort_usef : sort * EXP -> EXP,
sym_bindingf : symbol * EXP -> EXP,
sort_bindingf : sort * EXP -> EXP,
prodf : EXP list -> EXP,
listf : EXP -> EXP,
extf : ext * EXP * (PAT * EXP) list -> EXP,
abtf : abt -> EXP,
dotf : EXP * EXP -> EXP,
oper_patf : string * PAT option -> PAT,
operf : string * EXP option -> EXP} =
let
val index =
let val index = ref 0 in
(fn str =>
(index := !index + 1;
str ^ "'" ^ Int.toString (!index)))
end
val (create_abt_arity_rec, create_sort_arity_rec) =
create_abt_and_sort_fun
{sym_usef =
fn sym =>
let val str = index (sym_to_string sym)
in (VarPat str, sym_usef (sym, ExpVar str))
end,
sort_usef =
fn sort =>
let val str = index (sort_to_string sort)
in (VarPat str, sort_usef (sort, ExpVar str))
end,
sym_bindingf =
fn sym =>
let val str = index (sym_to_string sym)
in (VarPat str, sym_bindingf (sym, ExpVar str))
end,
sort_bindingf =
fn sort =>
let val str = index (sort_to_string sort)
in (VarPat str, sort_bindingf (sort, ExpVar str))
end,
prodf =
fn patexps =>
let val (pats, exps) = ListPair.unzip patexps
in (TuplePat pats, prodf exps)
end,
listf =
fn patexp =>
let val str = index "list" in
(VarPat str,
listf
(SeqExp [ExpVar "List.map", LamExp [patexp], ExpVar str]))
end,
extf =
fn (ext, patexps) =>
let val str = index (ext_to_string ext)
in (VarPat str, extf (ext, ExpVar str, patexps))
end,
abtf =
fn (abt, patexps) =>
let val str = index (abt_to_string abt) in
(VarPat str,
SeqExp
(abtf abt
:: List.map (fn patexp => LamExp [patexp]) patexps
@ List.map ExpVar args
@ [ExpVar str]))
end,
dotf =
fn ((binding_pat, binding_exp), (arity_pat, arity_exp)) =>
(TuplePat [binding_pat, arity_pat],
dotf (binding_exp, arity_exp))}
in
(fn (abt, abt_args, opers) =>
let
val abt_arg_names =
StringTable.fromSeq
(StringTable.Seq.fromList
(List.map (fn arg => (arg, index arg)) abt_args))
fun paramf str =
let val str' = index str in
(VarPat str',
SeqExp
[ExpVar (valOf (StringTable.find abt_arg_names str)),
ExpVar str'])
end
in
(name,
List.map (VarPat o valOf o StringTable.find abt_arg_names) abt_args
@ List.map VarPat args
@ [VarPat (abt_to_string abt)],
NONE : TYPE option,
CaseExp
(ExpVar (abt_to_string abt),
List.map
(fn (oper, arity_opt) =>
case arity_opt of
NONE => (oper_patf (oper, NONE), operf (oper, NONE))
| SOME arity =>
let
val (arity_pat, arity_exp) =
create_abt_arity_rec paramf arity
in
(oper_patf (oper, SOME arity_pat),
operf (oper, SOME arity_exp))
end)
opers))
end,
fn (sort, opers) =>
(name,
List.map VarPat args @ [VarPat (sort_to_string sort)],
NONE : TYPE option,
CaseExp
(ExpVar (sort_to_string sort),
List.map
(fn (oper, arity_opt) =>
case arity_opt of
NONE => (oper_patf (oper, NONE), operf (oper, NONE))
| SOME arity =>
let
val (arity_pat, arity_exp) =
create_sort_arity_rec arity
in
(oper_patf (oper, SOME arity_pat),
operf (oper, SOME arity_exp))
end)
opers)))
end
val create_symbol_structures =
List.map
(fn sym =>
StructureDefn
(Big (sym_to_string sym),
NONE,
StructVar "Temp"))
fun internal_oper abt_or_sort oper =
Big (abt_or_sort_to_string abt_or_sort) ^ "'" ^ oper
local
fun aequiv_code (ana : ana) abt_or_sort internal_abt =
let
val index =
let val index = ref 0 in
(fn str =>
(index := !index + 1;
str ^ "'" ^ Int.toString (!index)))
end
in
create_abt_and_sort_fun
{sym_usef =
fn sym =>
let
val str1 = index (sym_to_string sym)
val str2 = index (sym_to_string sym)
val aequiv =
if internal_abt then
SeqExp
[ExpVar "Abt.aequiv",
LamExp [(Wild, ExpVar "true")],
TupleExp [ExpVar str1, ExpVar str2]]
else
SeqExp
[ExpVar (Big (sym_to_string sym) ^ ".equal"),
TupleExp [ExpVar str1, ExpVar str2]]
in
(VarPat str1,
VarPat str2,
aequiv)
end,
sort_usef =
fn sort =>
let
val str1 = index (sort_to_string sort)
val str2 = index (sort_to_string sort)
val oper_aequiv =
if #mutualwith ana abt_or_sort (Sort sort)
then
SeqExp
[ExpVar "Abt.aequiv",
ExpVar (sort_to_string sort ^ "_oper_aequiv")]
else ExpVar (Big (sort_to_string sort) ^ ".aequiv")
in
(VarPat str1,
VarPat str2,
SeqExp
[oper_aequiv,
TupleExp [ExpVar str1, ExpVar str2]])
end,
sym_bindingf =
fn sym => (Wild, Wild, ExpVar "true"),
sort_bindingf =
fn sort => (Wild, Wild, ExpVar "true"),
prodf =
fn patexps =>
let
val (pats1, pats2, exps) =
unzip3 patexps
in
(TuplePat pats1,
TuplePat pats2,
SeqExp (interleave BoolAnd exps))
end,
listf =
fn (pat1, pat2, exp) =>
let
val str1 = index "list"
val str2 = index "list"
in
(VarPat str1,
VarPat str2,
SeqExp
[ExpVar "ListPair.allEq",
LamExp [(TuplePat [pat1, pat2], exp)],
TupleExp [ExpVar str1, ExpVar str2]])
end,
extf =
fn (ext, patexps) =>
let
val str1 = index (ext_to_string ext)
val str2 = index (ext_to_string ext)
in
(VarPat str1,
VarPat str2,
SeqExp
(ExpVar (Big (ext_to_string ext) ^ ".equal")
:: List.map
(fn (pat1, pat2, exp) =>
LamExp [(TuplePat [pat1, pat2], exp)])
patexps
@ [TupleExp [ExpVar str1, ExpVar str2]]))
end,
abtf =
fn (abt, patexps) =>
let
val str1 = index (abt_to_string abt)
val str2 = index (abt_to_string abt)
val aequiv_str =
if internal_abt
then "internal_aequiv"
else "aequiv"
in
(VarPat str1,
VarPat str2,
if #mutualwith ana abt_or_sort (Abt abt)
then
SeqExp
[ExpVar (abt_to_string abt ^ "_" ^ aequiv_str),
TupleExp [ExpVar str1, ExpVar str2]]
else
SeqExp
[ExpVar (Big (abt_to_string abt) ^ "." ^ aequiv_str),
TupleExp [ExpVar str1, ExpVar str2]])
end,
dotf =
fn ((patl1, patr1, exp1), (patl2, patr2, exp2)) =>
(TuplePat [patl1, patl2],
TuplePat [patr1, patr2],
SeqExp [exp1, BoolAnd, exp2])}
end
fun paramf str = str ^ "_aequiv"
in
fun abt_aequiv ana internal (abt, (args, opers)) =
(abt_to_string abt ^
(if internal
then "_internal_aequiv"
else "_aequiv"),
List.map (VarPat o paramf) args
@ [TuplePat
[VarPat (abt_to_string abt ^ "1"),
VarPat (abt_to_string abt ^ "2")]],
NONE,
CaseExp
(TupleExp
[ExpVar (abt_to_string abt ^ "1"),
ExpVar (abt_to_string abt ^ "2")],
List.map
(fn (oper, arity_opt) =>
let
val oper' =
if internal
then internal_oper (Abt abt) oper
else oper
in
case arity_opt of
NONE =>
(TuplePat [VarPat oper', VarPat oper'],
ExpVar "true")
| SOME arity =>
let
val (pat1, pat2, exp) =
#1 (aequiv_code ana (Abt abt) internal)
(fn str =>
(VarPat (str ^ "1"),
VarPat (str ^ "2"),
SeqExp
[ExpVar (paramf str),
TupleExp
[ExpVar (str ^ "1"),
ExpVar (str ^ "2")]]))
arity
in
(TuplePat
[InjPat (oper', pat1),
InjPat (oper', pat2)],
exp)
end
end)
opers
@ [(Wild, ExpVar "false")]))
fun sort_aequiv ana (sort, opers) =
(sort_to_string sort ^ "_oper_aequiv",
[TuplePat
[VarPat (sort_to_string sort ^ "1"),
VarPat (sort_to_string sort ^ "2")]],
NONE,
CaseExp
(TupleExp
[ExpVar (sort_to_string sort ^ "1"),
ExpVar (sort_to_string sort ^ "2")],
List.map
(fn (oper, arity_opt) =>
case arity_opt of
NONE =>
(TuplePat
[VarPat (internal_oper (Sort sort) oper),
VarPat (internal_oper (Sort sort) oper)],
ExpVar "true")
| SOME arity =>
let
val (pat1, pat2, exp) =
#2 (aequiv_code ana (Sort sort) true) arity
in
(TuplePat
[InjPat (internal_oper (Sort sort) oper, pat1),
InjPat (internal_oper (Sort sort) oper, pat2)],
exp)
end)
opers
@ [(Wild, ExpVar "false")]))
end
fun create_mutual_ops (ana : ana) (abts, sorts) =
let
(*??? should actually use this or similar.
fun scoped_sort srt srt' =
if #mutualwith ana srt srt'
then sort_to_string srt'
else ops_name ana srt' ^ "." ^ sort_to_string srt'
*)
val abt_datatypes =
List.map
(fn (abt, (args, opers)) =>
(abt_to_string abt,
List.map (fn arg => "'" ^ arg) args,
List.map
(fn (oper, arity_opt) =>
(Big (abt_to_string abt) ^ "'" ^ oper,
case arity_opt of
NONE => NONE
| SOME arity =>
SOME (abt_arity_to_type ana true abt arity)))
opers))
abts
val sort_datatypes =
List.map
(fn (sort, opers) =>
(sort_to_string sort ^ "_oper",
[],
List.map
(fn (oper, arity_opt) =>
(Big (sort_to_string sort) ^ "'" ^ oper,
case arity_opt of
NONE => NONE
| SOME arity =>
SOME (sort_arity_to_type ana true sort arity)))
opers))
sorts
val withtypes =
List.map
(fn (sort, _) =>
(sort_to_string sort,
[],
AppType
([TypeVar (sort_to_string sort ^ "_oper")],
ModProjType (StructVar "Abt", "t"))))
sorts
local
val args = ["x", "i"]
fun sym_usef name (sym, exp) =
SeqExp
[ExpVar ("Abt." ^ name),
LamExp
[(VarPat "_",
LamExp
[(VarPat "_",
LamExp [(VarPat "t", ExpVar "t")])])],
ExpVar "x",
ExpVar "i",
exp]
fun sort_usef name abt_or_sort (sort, exp) =
SeqExp
[ExpVar ("Abt." ^ name),
ExpVar
((if #mutualwith ana abt_or_sort (Sort sort)
then sort_to_string sort
else
ops_name ana (Sort sort)
^ "." ^ sort_to_string sort)
^ "_oper_" ^ name),
ExpVar "x",
ExpVar "i",
exp]
fun sym_bindingf (sym, exp) = exp
fun sort_bindingf (sort, exp) = exp
val prodf = TupleExp
fun extf (ext, exp, patexps) =
case patexps of
[] => exp
| _ =>
SeqExp
[LamExp [(TuplePat [VarPat "x", Wild], ExpVar "x")],
SeqExp
(ExpVar (Big (ext_to_string ext) ^ ".iter")
:: List.map
(fn (pat, exp) =>
LamExp
[(TuplePat [pat, TuplePat []],
TupleExp [exp, TupleExp []])])
patexps
@ [TupleExp [exp, TupleExp []]])]
fun abtf name abt_or_sort abt =
if #mutualwith ana abt_or_sort (Abt abt)
then ExpVar (abt_to_string abt ^ "_" ^ name)
else
ExpVar
(ops_name ana (Abt abt) ^ "."
^ abt_to_string abt ^ "_" ^ name)
fun oper_patf abt_or_sort (oper, pat_opt) =
case pat_opt of
NONE =>
VarPat (internal_oper abt_or_sort oper)
| SOME pat =>
InjPat
(internal_oper abt_or_sort oper, pat)
fun operf abt_or_sort (oper, exp_opt) =
case exp_opt of
NONE =>
ExpVar (internal_oper abt_or_sort oper)
| SOME exp =>
SeqExp
[ExpVar
(internal_oper abt_or_sort oper),
exp]
fun ident l = l (* make this more global ??? *)
fun recs name abt_or_sort =
create_recursors
{name =
case abt_or_sort of
Abt abt => abt_to_string abt ^ "_" ^ name
| Sort sort => sort_to_string sort ^ "_oper_" ^ name,
args = args,
sym_usef = sym_usef name,
sort_usef = sort_usef name abt_or_sort,
sym_bindingf = sym_bindingf,
sort_bindingf = sort_bindingf,
prodf = TupleExp,
listf = ident,
extf = extf,
abtf = abtf name abt_or_sort,
dotf =
fn (binding_exp, arity_exp) =>
TupleExp [binding_exp, arity_exp],
oper_patf = oper_patf abt_or_sort,
operf = operf abt_or_sort}
in
fun abt_rec name (abt, (abt_args, opers)) =
#1 (recs name (Abt abt)) (abt, abt_args, opers)
fun sort_rec name (sort, opers) =
#2 (recs name (Sort sort)) (sort, opers)
end
in
StructureDefn
(String.concat
(List.map (Big o abt_to_string o #1) abts
@ List.map (Big o sort_to_string o #1) sorts)
^ "Ops",
NONE,
StructBody
([TypeDefns
{datatypes = abt_datatypes @ sort_datatypes,
aliases = withtypes},
BlankDefn,
FunDefns
(List.map (abt_rec "bind") abts
@ List.map (sort_rec "bind") sorts),
BlankDefn,
FunDefns
(List.map (abt_rec "unbind") abts
@ List.map (sort_rec "unbind") sorts),
BlankDefn,
FunDefns
(List.map (abt_aequiv ana true) abts
@ List.map (sort_aequiv ana) sorts)]))
end
(* Code duplication??? *)
fun create_view_datatype_defn (ana : ana) (srt, opers) =
let
val oper_branches =
List.map
(fn (oper, arity_opt) =>
(oper,
case arity_opt of
NONE => NONE
| SOME arity =>
SOME (sort_arity_to_type ana false srt arity)))
opers
val body =
if #hasvar ana srt
then ("Var", SOME (TypeVar (sort_to_var_type srt))) :: oper_branches
else oper_branches
in
TypeDefns {datatypes = [("view", [], body)], aliases = []}
end
fun bind_single oper_bind exp =
TupleExp
[SeqExp
[ExpVar "List.foldl",
LamExp
[(TuplePat [VarPat "x", VarPat "acc"],
SeqExp
[ExpVar "Abt.into",
oper_bind,
SeqExp
[ExpVar ("Abt.Binding"),
TupleExp [ExpVar "x", ExpVar "acc"]]])],
exp,
ExpVar "vars"],
ListExp []]
fun unbind_single oper_unbind exp =
SeqExp
[ExpVar "List.foldr",
LamExp
[(TuplePat [VarPat "x", VarPat "acc"],
LetExp
([ValDefn
(InjPat ("Abt.Binding", TuplePat [Wild, VarPat "acc'"]),
SeqExp
[ExpVar "Abt.out",
oper_unbind,
SeqExp
[ExpVar "Abt.unbind",
oper_unbind,
ExpVar "x",
IntExp ~1, (* This violates every invariant... *)
ExpVar "acc"]])],
ExpVar "acc'"))],
exp,
ExpVar "vars"]
fun view_in_code (ana : ana) abt_or_sort =
create_recursors
{name =
case abt_or_sort of
Abt abt => abt_to_string abt ^ "_view_in"
| Sort _ => "view_in",
args = ["vars"],
sym_usef =
fn (sym, exp) =>
bind_single
(LamExp
[(VarPat "_",
LamExp
[(VarPat "_",
LamExp [(VarPat "t", ExpVar "t")])])])
(SeqExp
[ExpVar "Abt.into",
LamExp
[(VarPat "_",
LamExp
[(VarPat "_",
LamExp [(VarPat "t", ExpVar "t")])])],
SeqExp [ExpVar "Abt.Var", exp]]),
sort_usef =
fn (sort, exp) =>
bind_single
(ExpVar
(ops_name ana (Sort sort) ^ "."
^ sort_to_string sort ^ "_oper_bind"))
exp,
sym_bindingf =
fn (sym, exp) =>
LetExp
([ValDefn (VarPat "var", exp)],
TupleExp
[SeqExp [ExpVar "Temp.toUserString", ExpVar "var"],
ListExp [ExpVar "var"]]),
sort_bindingf =
fn (sort, exp) =>
LetExp
([ValDefn (VarPat "var", exp)],
TupleExp
[SeqExp [ExpVar "Temp.toUserString", ExpVar "var"],
ListExp [ExpVar "var"]]),
prodf =
fn exps =>
let
val (val_decls, ts, vars) =
unzip3
(mapi
(fn (i, exp) =>
(ValDefn
(TuplePat
[VarPat ("t" ^ Int.toString i),
VarPat ("vars'" ^ Int.toString i)],
exp),
ExpVar ("t" ^ Int.toString i),
ExpVar ("vars'" ^ Int.toString i)))
exps)
in
(LetExp
(val_decls,
TupleExp
[TupleExp ts,
SeqExp (interleave (ExpVar "@") vars)]))
end,
listf =
fn exp =>
LetExp
([ValDefn
(TuplePat [VarPat "ts", VarPat "vars'"],
SeqExp [ExpVar "ListPair.unzip", exp])],
TupleExp
[ExpVar "ts",
SeqExp [ExpVar "List.concat", ExpVar "vars'"]]),
extf =
fn (ext, exp, patexps) =>
case patexps of
[] => TupleExp [exp, ListExp []]
| _ =>
SeqExp
(ExpVar (Big (ext_to_string ext) ^ ".iter")
:: List.map
(fn (pat, exp) =>
LamExp
[(TuplePat [pat, VarPat "vars'"],
LetExp
([ValDefn
(TuplePat [VarPat "t", VarPat "vars''"],
exp)],
TupleExp
[ExpVar "t",
SeqExp
[ExpVar "vars''",
ExpVar "@",
ExpVar "vars'"]]))])
patexps
@ [TupleExp [exp, ListExp []]]),
abtf = fn abt => ExpVar (abt_to_string abt ^ "_view_in"),
dotf =
fn (binding_exp, arity_exp) =>
LetExp
([ValDefn
(TuplePat [VarPat "t", VarPat "vars'"],
binding_exp),
ValDefn
(VarPat "vars",
SeqExp [ExpVar "vars'", ExpVar "@", ExpVar "vars"]),
ValDefn
(TuplePat [VarPat "t'", VarPat "vars'"],
arity_exp)],
TupleExp
[TupleExp [ExpVar "t", ExpVar "t'"],
ExpVar "vars'"]),
oper_patf =
fn (oper, pat_opt) =>
case pat_opt of
NONE => VarPat oper
| SOME pat => InjPat (oper, pat),
operf =
case abt_or_sort of
Abt abt =>
(fn (oper, exp_opt) =>
case exp_opt of
NONE =>
TupleExp
[ExpVar
(ops_name ana (Abt abt) ^ "."
^ Big (abt_to_string abt) ^ "'" ^ oper),
ListExp []]
| SOME exp =>
LetExp
([ValDefn (TuplePat [VarPat "t", VarPat "vars'"], exp)],
TupleExp
[SeqExp
[ExpVar
(ops_name ana (Abt abt) ^ "."
^ Big (abt_to_string abt) ^ "'" ^ oper),
ExpVar "t"],
ExpVar "vars'"]))
| Sort sort =>
(fn (oper, exp_opt) =>
case exp_opt of
NONE =>
SeqExp
[ExpVar "Abt.Oper",
ExpVar
(ops_name ana (Sort sort) ^ "."
^ Big (sort_to_string sort) ^ "'" ^ oper)]
| SOME exp =>
SeqExp
[ExpVar "Abt.Oper",
SeqExp
[ExpVar
(ops_name ana (Sort sort) ^ "."
^ Big (sort_to_string sort) ^ "'" ^ oper),
SeqExp
[LamExp [(TuplePat [VarPat "x", Wild], ExpVar "x")],
exp]]])}
fun view_out_code (ana : ana) abt_or_sort =
create_recursors
{name =
case abt_or_sort of
Abt abt => abt_to_string abt ^ "_view_out"
| Sort _ => "oper_view_out",
args = ["vars"],
sym_usef =
fn (sym, exp) =>
(LetExp
([ValDefn
(InjPat ("Abt.Var", VarPat "t"),
SeqExp
[ExpVar "Abt.out",
LamExp
[(VarPat "_",
LamExp
[(VarPat "_",
LamExp [(VarPat "t", ExpVar "t")])])],
unbind_single
(LamExp
[(VarPat "_",
LamExp
[(VarPat "_",
LamExp [(VarPat "t", ExpVar "t")])])])
exp])],
TupleExp [ExpVar "t", ListExp []])),
sort_usef =
fn (sort, exp) =>
TupleExp
[unbind_single
(ExpVar
(ops_name ana (Sort sort) ^ "."
^ sort_to_string sort ^ "_oper_unbind"))
exp,
ListExp []],
sym_bindingf =
fn (sym, exp) =>
LetExp
([ValDefn (VarPat "x", SeqExp [ExpVar "Temp.new", exp])],
TupleExp [ExpVar "x", ListExp [ExpVar "x"]]),
sort_bindingf =
fn (sort, exp) =>
LetExp
([ValDefn (VarPat "x", SeqExp [ExpVar "Temp.new", exp])],
TupleExp [ExpVar "x", ListExp [ExpVar "x"]]),
prodf =
fn exps =>
let
val (val_decls, ts, vars) =
unzip3
(mapi
(fn (i, exp) =>
(ValDefn
(TuplePat
[VarPat ("t" ^ Int.toString i),
VarPat ("vars'" ^ Int.toString i)],
exp),
ExpVar ("t" ^ Int.toString i),
ExpVar ("vars'" ^ Int.toString i)))
exps)
in
(LetExp
(val_decls,
TupleExp
[TupleExp ts,
SeqExp (interleave (ExpVar "@") vars)]))
end,
listf =
fn exp =>
LetExp
([ValDefn
(TuplePat [VarPat "ts", VarPat "vars'"],
SeqExp [ExpVar "ListPair.unzip", exp])],
TupleExp
[ExpVar "ts",
SeqExp [ExpVar "List.concat", ExpVar "vars'"]]),
abtf = fn abt => ExpVar (abt_to_string abt ^ "_view_out"),
extf =
fn (ext, exp, patexps) =>
case patexps of
[] => TupleExp [exp, ListExp []]
| _ =>
SeqExp
(ExpVar (Big (ext_to_string ext) ^ ".iter")
:: List.map
(fn (pat, exp) =>
LamExp
[(TuplePat [pat, VarPat "vars'"],
LetExp
([ValDefn
(TuplePat [VarPat "t", VarPat "vars''"],
exp)],
TupleExp
[ExpVar "t",
SeqExp
[ExpVar "vars''",
ExpVar "@",
ExpVar "vars'"]]))])
patexps
@ [TupleExp [exp, ListExp []]]),
dotf =
fn (binding_exp, arity_exp) =>
LetExp
([ValDefn
(TuplePat [VarPat "t", VarPat "vars'"],
binding_exp),
ValDefn
(VarPat "vars",
SeqExp [ExpVar "vars'", ExpVar "@", ExpVar "vars"]),
ValDefn
(TuplePat [VarPat "t'", VarPat "vars'"],
arity_exp)],
TupleExp
[TupleExp [ExpVar "t", ExpVar "t'"],
ExpVar "vars'"]),
oper_patf =
fn (oper, pat_opt) =>
case pat_opt of
NONE =>
VarPat
(ops_name ana abt_or_sort ^ "."
^ Big (abt_or_sort_to_string abt_or_sort) ^ "'" ^ oper)
| SOME pat =>
InjPat
(ops_name ana abt_or_sort ^ "."
^ Big (abt_or_sort_to_string abt_or_sort) ^ "'" ^ oper,
pat),
operf =
case abt_or_sort of
Abt _ =>
(fn (oper, exp_opt) =>
case exp_opt of
NONE =>
TupleExp [ExpVar oper, ListExp []]
| SOME exp =>
LetExp
([ValDefn (TuplePat [VarPat "t", VarPat "vars'"], exp)],
TupleExp
[SeqExp [ExpVar oper, ExpVar "t"],
ExpVar "vars'"]))
| Sort _ =>
(fn (oper, exp_opt) =>
case exp_opt of
NONE => ExpVar oper
| SOME exp =>
SeqExp
[ExpVar oper,
SeqExp
[LamExp [(TuplePat [VarPat "x", Wild], ExpVar "x")],
exp]])}
fun create_mutual_abt_view_datatype ana abts =
case abts of
[] => []
| (abt, _)::_ =>
interleave BlankDefn
(List.map TypeDefns (#2 (create_mutual_types ana (Abt abt)))
@ [TypeDefns
{datatypes=List.map (abt_datatype ana) abts,
aliases=[]},
FunDefns
(List.map
(fn (abt, (args, opers)) =>
#1 (view_in_code ana (Abt abt)) (abt, args, opers))
abts),
FunDefns
(List.map
(fn (abt, (args, opers)) =>
#1 (view_out_code ana (Abt abt)) (abt, args, opers))
abts)])
fun create_abt_structure_defn ana (abt, (args, opers)) =
StructureDefn
(Big (abt_to_string abt),
NONE,
StructBody
(concatWith [BlankDefn]
[List.map TypeDefns (op@ (create_mutual_types ana (Abt abt))),
[DatatypeCopy (abt_to_string abt, TypeVar (abt_to_string abt)),
BlankDefn,
TypeDefns
{aliases = [("t", args, TypeVar (abt_to_string abt))],
datatypes = []}]]))
fun create_sort_structure_defn (ana : ana) (sort, opers) =
let
val var_structure_defn =
if #hasvar ana sort
then [StructureDefn ("Var", NONE, StructVar "Temp")]
else []
val convenient_contructors =
let
val oper_constructors =
List.map
(fn (oper, arity_opt) =>
ValDefn
(VarPat (oper ^ "'"),
(case arity_opt of
NONE => SeqExp [ExpVar "into", ExpVar oper]
| SOME _ =>