-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertorCore.sml
1216 lines (1085 loc) · 51.5 KB
/
convertorCore.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
(* (c) SMLtoCoq
* We refer to important sections in the following four documents:
* Documentation (doc): in the doc directory
* SML's Definition (def): https://smlfamily.github.io/sml97-defn.pdf
* Gallina's Documentation (gallina): https://coq.inria.fr/refman/language/core/index.html
* HaMLet's Documentation (hamlet): https://people.mpi-sws.org/~rossberg/hamlet/hamlet-2.0.0.pdf
*)
(*
* Converts SML abstract core syntax (doc, section 3).
* For each translation rule of the form s => g, we define a corresponding function s2g (some exceptions may apply).
* We make use of three contexts: record local context, record global context, and tyvar local context (doc, sections 3.2 and 3.3)
* The full grammar can be found in SML's Definition (def, appendix B)
*)
(*
* For each main translation function we provide the following:
* EXAMPLE: an SML code example
* FROM: Section/Figure number in SML's Definition
* TO SECTION: Section name in Gallina's documentation
* NOTES: additional notes
*)
structure ConvertorCore =
struct
structure D = DynamicObjectsCore
structure F = FunctionChecker
structure T = TyvarResolver
local
structure G = Gallina
infix @@
infix ==>
in
exception WildCard
(* Local record context, doc: section 3.2 *)
val recordContext = ref [] : (G.sentence list) ref
val infixContext = ref [] : (G.sentence list) ref
(* Global record context, doc: section 3.2 *)
val recordTracker = ref (ConvertorUtil.LT.empty) : (G.ident ConvertorUtil.LT.dict) ref
val infixTracker = ref (VIdMap.empty) : ((Infix.InfStatus * bool) VIdMap.map) ref
(* Local tyvar context, doc: section 3.3 *)
val tyvarCtx = ref (ConvertorUtil.TT.empty)
(* functions' names that use obligations *)
val funH = ref [] : (string list) ref
val exFNum = ref 0 : int ref
val inThm = ref false : bool ref
structure AE = AnnotationExtractor(val recordContext = recordContext; val recordTracker = recordTracker)
open AE
(* EXAMPLES: 5, "a", #'c'
* FROM: Section 2.2
* TO: Essential Vocabulary: term
* KEYWORD: term
*)
fun scon2term (SCon.STRING s : SCon.SCon) : G.term = G.StringTerm s
| scon2term (SCon.CHAR s) = G.CharTerm s
| scon2term (SCon.REAL s) = G.RealTerm s
| scon2term (SCon.INT (b, s)) = if b = SCon.DEC then G.NumTerm s else G.HexTerm s
(* Need to check what's a word constant *)
| scon2term (SCon.WORD (b, s)) = if b = SCon.DEC then G.WordTerm (s, G.Dec) else G.WordTerm (s, G.Hex)
structure PF = PrecondsFinder(struct val scon2term = scon2term end)
fun isExhaustive A : bool =
case (try (exhaustive A)) of
SOME S.Exhaustive => true
| _ => false
(* FROM: Scon.sml: 15 -> 20
* TO: Gallina.sml : 97 -> 115
* FROM SECTION: -
* TO SECTION: 4.1.3 page 115
* KEYWORD: pattern
*)
fun scon2pattern (SCon.STRING s : SCon.SCon) : G.pattern = G.StringPat s
| scon2pattern (SCon.CHAR s) = G.CharPat s
| scon2pattern (SCon.REAL s) = G.RealPat s
| scon2pattern (SCon.INT (b, s)) = if b = SCon.DEC then G.NumPat s else G.HexPat s
(* Need to check what's a word constant *)
| scon2pattern (SCon.WORD (b, s)) = if b = SCon.DEC then G.WordPat (s, G.Dec) else G.WordPat (s, G.Hex)
and tyrow2body (TyRow(lab, ty, tyrow') @@ _ : TyRow) : (G.ident * Ty') list = (~lab, ~ty) :: ?tyrow2body tyrow'
and tybody2labs (body : (G.ident * Ty') list) = Sort.sort String.compare (#1(ListPair.unzip body))
and status2modifiers ((Infix.LEFT, level) : Infix.InfStatus) : G.modifier list =
[G.LeftAssoc, if level <= 71 then G.Level (level + 29) else G.Level level]
| status2modifiers ((Infix.RIGHT, level)) =
[G.RightAssoc, if level <= 72 then G.Level (level + 28) else G.Level level]
and exprow2body (ExpRow(lab, exp, exprow') @@ _) = (~lab, ~exp) :: ?exprow2body exprow'
and expbody2fields ident body = let
fun expbody2field (lab, exp) = G.FieldDef {id = ident ^ "_" ^ (checkLegal lab), binders = [], term = exp2term exp }
in List.map expbody2field body end
and patrow2body (labels : Lab.Lab list) (patrow) =
let
fun patrow2body' (FIELDPatRow(lab, pat, patrow') @@ _) =
let
val (lab, pat) = (~lab, pat2pattern (pat))
val body = (lab, pat) :: (?patrow2body' patrow')
in
body
end
| patrow2body' _ = []
val body = LabMap.fromList (patrow2body' patrow)
val body' = LabMap.fromList (List.map (fn l => (l, G.WildcardPat)) labels)
val labs = orderLabs labels
val res = LabMap.listItemsi (LabMap.unionWith (fn (a, _) => a) (body, body'))
in
case LT.find (!recordTracker) labs of
SOME id => (id, res)
| NONE => let
val id = genIdent ()
val typs = gentyps (List.length labs)
val _ = recordTracker := LT.insert (!recordTracker) labs id
val _ = recordContext := mkRecord (labs, id, typs) :: !recordContext
in
(id, res)
end
end
and patbody2fields ident body = let
fun patbody2field (lab, pat) = G.FieldPat {id = ident ^ "_" ^ checkLegal lab, binders = [], pat = pat}
in List.map patbody2field body end
(* EXAMPLE: type class = (id, name) hashtable :(id, name) forms a tyseq
* FROM: SyntaxCoreFn.sml: 159 -> 164
* TO: Gallina.sml : 55
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: tyseq
* TO SECTION: 4.1.3 page 115
* KEYWORD: arg
*)
and tyseq2args (Seq tys : Ty seq') : G.arg list = % ty2arg tys
(* EXAMPLE: check tyseq2args
* FROM: SyntaxCoreFn.sml: 159 -> 164
* TO: Gallina.sml : 55
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: tyseq
* TO SECTION: 4.1.3 page 115
* KEYWORD: arg
* NOTES: Associativity: Might solve this by explicitly adding
* parenthesis or by requiring the user to explicitly put parenthesis?
*)
and ty2arg (ty : Ty') : G.arg = G.Arg (ty2term ty)
(* Helper function to extract the list of names of abstract variables
in a type (check datbind2indprinciple) *)
and ty2list (VARTy tyvar) =
[checkLegal(TyVar.toString (~tyvar))]
| ty2list (TUPLETyX tys) =
List.concat(% (ty2list) tys)
| ty2list (PARTy ty) =
ty2list (~ty)
| ty2list _ = raise Fail "Unsupported abstract type for datatype decl"
(* EXAMPLE: type age = int (ty encodes int)
* FROM: SyntaxCoreFn.sml: 159 -> 164
* TO: Gallina.sml : 16 -> 53
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: ty
* TO SECTION: 4.1.3 page 115
* KEYWORD: term
*)
(* VARty is type variable, e.g. 'a list *)
and ty2term ((VARTy tyvar) : Ty') : G.term =
let val typ = (checkLegal(TyVar.toString (~tyvar)))
in
G.IdentTypTerm (typ)
end
(* in scope term because the operator "*" is overloaded *)
| ty2term (TUPLETyX (tys)) =
if List.length(tys) > 1 then
G.InScopeTerm (G.ProductTerm (% ty2term tys), "type")
else
(* G.ParensTerm *) (List.nth ((% ty2term tys), 0))
(* CONTy is constructor type, e.g. int *)
| ty2term (CONTy (tyseq, tycon)) = let
val terms = % ty2term ($(~tyseq))
val tycon = ltycon2id (~tycon)
in
(case terms of [] => G.IdentTypTerm tycon | _ => G.ExplicitTerm(tycon, terms))
end
(* PARTy is parenthesis type, e.g. (int) *)
| ty2term (PARTy ty) = G.ParensTerm (ty2term (~ty))
(* ARROWTy is arrow type, e.g. int -> int *)
| ty2term (ARROWTy(ty1, ty2)) = G.ArrowTerm(ty2term (~ty1), ty2term (~ty2))
| ty2term (RECORDTy recBody) = let
val body = ?tyrow2body recBody
val labs = tybody2labs body
val names = case recBody of NONE => []
| SOME(_@@A) => (case !(elab A) of NONE => []
| SOME rowtyp => T.getTyvars' (T.S.RowType(rowtyp)))
in
case LT.find (!recordTracker) labs of
SOME ident => G.IdentTypTerm (ident)
| _ => let
val id = genIdent ()
val _ = recordTracker := LT.insert (!recordTracker) labs id
val _ = recordContext := tyrow2sent (names, body, id) :: !recordContext
in
G.IdentTypTerm (id)
end
end
(* FROM: SyntaxCoreFn.sml: 103 -> 104
* TO: Gallina.sml: 95
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: mrule
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: equation
*)
and mrule2equation (Mrule(pat, exp) : Mrule') : G.equation =
let
val term = exp2term (~ exp)
val pattern = pat2pattern (pat)
in
G.Equation(pattern, term)
end
(* FROM: SyntaxCoreFn.sml: 84 -> 94
* TO: Gallina.sml: 89
* FROM SECTION: Appendix C.1 page 102
* KEYWORD: exp
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: match_item
*)
and exp2matchitem (exp : Exp') : G.matchItem = G.MatchItem (exp2term(exp))
(* FROM: SyntaxCoreFn.sml: 100 -> 101
* TO: Gallina.sml: 95
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: match
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: equation
*)
and match2equations (Match(mrule, match2) @@ A : Match) : G.equation list =
let
fun match2equations' (Match(mrule, match2)@@_ : Match) =
(mrule2equation (~mrule)) :: (?match2equations' match2)
val equation2 = if isExhaustive A then []
else [G.Equation(G.WildcardPat, G.Axiom G.PatternFailure)]
val equations = (mrule2equation (~mrule)) :: (?match2equations' match2) @ equation2
in
equations
end
(* FROM: SyntaxCoreFn.sml: 71 -> 79
* TO: Gallina.sml: 64
* FROM SECTION: Appendix C.1 page 102
* KEYWORD: atexp
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: arg
*)
and atexp2args (atexp : AtExp) : G.arg list = [G.Arg (atexp2term atexp)]
(* Helper function doesn't have corresponding sections, check atexp2term *)
and sentterm2letTerm ((G.DefinitionSentence (G.DefinitionDef sent)) : G.sentence, term : G.term) =
G.LetTerm {id = #id sent, binders = #binders sent, typ = #typ sent,
body = #body sent, inBody = term}
| sentterm2letTerm (G.SeqSentences sents, term) =
let
fun nested([] : G.sentence list) = term
| nested (s :: sL) =
sentterm2letTerm (s, (nested sL))
in
nested sents
end
| sentterm2letTerm _ = raise Fail "Translating this sentence to let is invalid/Unimplemented \n"
(* FROM: SyntaxCoreFn.sml: 71 -> 79
* TO: Gallina.sml: 21 -> 62
* FROM SECTION: Appendix C.1 page 102
* KEYWORD: atexp
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: term
*)
and atexp2term (SCONAtExp(scon)@@_ : AtExp) : G.term = scon2term (~scon)
| atexp2term (IDAtExp(opVal, longvid)@@A) =
let
val vid = lvid2id (~longvid)
val opVal = case opVal of
NONE => false
| SOME _ => (case VIdMap.find (!infixTracker, VId.fromString vid) of
NONE => false
| SOME (_, true) => true
| SOME (status, false) =>
(infixContext := (mkInfix(vid, status2modifiers status) @ !infixContext);
infixTracker := VIdMap.insert(!infixTracker, VId.fromString vid, (status, true));
true))
in
(case T.resolveTyvars tyvarCtx (!(elab A)) of
NONE => G.IdentTerm (opetize opVal vid)
| SOME typ => G.HasTypeTerm(G.IdentTerm (opetize opVal (vid)), typ))
end
| atexp2term (RECORDAtExp(recBody)@@_) = let
val body = ?exprow2body recBody
val labs = orderLabs (#1 (ListPair.unzip body))
in
case LT.find (!recordTracker) labs of
SOME ident => G.RecordTerm (expbody2fields ident body)
| NONE => let
val id = genIdent ()
val typs = gentyps (List.length labs)
val _ = recordTracker := LT.insert (!recordTracker) labs id
val _ = recordContext := mkRecord (labs, id, typs) :: !recordContext
in
G.RecordTerm (expbody2fields id body)
end
end
| atexp2term (LETAtExp(dec, exp)@@_) =
let
val sent = dec2sent dec
val term = exp2term (~exp)
in
sentterm2letTerm (sent, term)
end
| atexp2term (PARAtExp(exp)@@_) = G.ParensTerm (exp2term (~exp))
| atexp2term (UNITAtExpX@@ _) = G.UnitTerm
(* in scope term because the operator "*" is overloaded *)
| atexp2term (TUPLEAtExpX(exps)@@_) = G.TupleTerm(% exp2term exps)
| atexp2term (LISTAtExpX(exps)@@A) =
if length exps > 0 then G.ListTerm(% exp2term exps)
else (* For empty lists with undetermined types, we need to add an explicit type in Gallina *)
case T.resolveTyvars tyvarCtx (!(elab A)) of
NONE => G.ListTerm(% exp2term exps)
| SOME typ => G.HasTypeTerm(G.ListTerm(% exp2term exps), typ)
(* FROM: SyntaxCoreFn.sml: 84 -> 94
* TO: Gallina.sml: 21 -> 62
* FROM SECTION: Appendix C.1 page 102
* KEYWORD: exp
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: term
*)
and exp2term (ATExp atexp : Exp') : G.term = atexp2term (atexp)
| exp2term (APPExp (exp, atexp)) = (* Changed, (might need to be changed in the future) *)
let
val result =
case exp of
ATExp(IDAtExp(opr, lvid)@@_)@@_ =>
(case List.find (fn x => x = (lvid2id (~lvid))) (!funH) of
SOME x => G.ExplicitTerm(x, if (!inThm) then (atexp2term atexp)::[G.IdentTerm("H")]
else (atexp2term atexp)::[G.IdentTerm("_")]) before (exFNum := 1 + !exFNum)
| NONE => G.ApplyTerm(exp2term (~exp), atexp2args (atexp)))
| _ => G.ApplyTerm(exp2term (~exp), atexp2args (atexp))
in
result (*G.ApplyTerm(exp2term (~exp), atexp2args (atexp))*)
end
| exp2term (COLONExp (exp, ty)) =
let val typ = ty2term (~ty)
val exp = exp2term (~exp)
in G.HasTypeTerm(exp, typ) end
| exp2term (FNExp(match as Match(Mrule(pat, exp)@@_, _)@@A)) =
let
val expand = isExhaustive A
val ty = extractTypFromPat (~pat)
val binders = pat2binders (pat) false
in
(case (expand, ty) of
(false, NONE) =>
let val binders = pat2binders (pat) false
val body = exp2term (~exp)
in G.FunTerm (binders, body) end
| (false , SOME typ) =>
(case binders of
[G.SingleBinder {name = name, ... }] =>
let val binders = G.SingleBinder {name = name, typ = SOME typ, inferred = false}
val body = exp2term (~exp)
in G.FunTerm ([binders], body) end
| _ =>
let val id= vid2id (VId.invent())
val binders = G.SingleBinder {name = G.Name id, typ = NONE, inferred= false}
val equations = match2equations match
val body = G.MatchTerm { matchItems = [G.MatchItem (G.HasTypeTerm (G.IdentTerm (id), typ))], body = equations}
in G.FunTerm ([binders], body) end)
| (_, SOME typ) =>
let val id= vid2id (VId.invent())
val binders = G.SingleBinder {name = G.Name id, typ = NONE, inferred= false}
val equations = match2equations match
val body = G.MatchTerm { matchItems = [G.MatchItem(G.HasTypeTerm (G.IdentTerm (id), typ))], body = equations}
in G.FunTerm ([binders], body) end
| (_, NONE) =>
let val id= vid2id (VId.invent())
val binders = G.SingleBinder {name = G.Name id, typ = NONE, inferred= false}
val equations = match2equations match
val body = G.MatchTerm { matchItems = [G.MatchItem(G.IdentTerm (id))], body = equations}
in G.FunTerm ([binders], body) end)
end
| exp2term (CASEExpX (exp, match)) =
let
val matchItems = [exp2matchitem (~exp)]
val equations = match2equations match
in
G.MatchTerm {matchItems = matchItems, body = equations}
end
| exp2term (IFExpX (exp1, exp2, exp3)) = let
val exp1' = exp2term (~exp1)
val exp2' = exp2term (~exp2)
val exp3' = exp2term (~exp3)
in
G.IfTerm {test = exp1', thenTerm = exp2', elseTerm = exp3'}
end
| exp2term (ANDALSOExpX (exp1, exp2)) = let
val exp1' = exp2term (~exp1)
val exp2' = exp2term (~exp2)
in
G.AndTerm (exp1', exp2')
end
| exp2term (ORELSEExpX (exp1, exp2)) = let
val exp1' = exp2term (~exp1)
val exp2' = exp2term (~exp2)
in
G.OrTerm (exp1', exp2')
end
| exp2term (INFIXExpX (exp, atexp)) =
G.InfixTerm (exp2term (~exp), atexp2args (atexp))
and extractTypFromAtPat (RECORDAtPat(NONE)) : G.term option = NONE
| extractTypFromAtPat (RECORDAtPat(SOME patrow)) = raise Fail "Constructor Pattern shouldn't have interior types\n"
| extractTypFromAtPat (PARAtPat pat) = extractTypFromPat (~pat)
| extractTypFromAtPat (TUPLEAtPatX pats) =
let
val typs = % extractTypFromPat pats
in
if List.exists (Option.isSome) typs then
SOME (G.ProductTerm (List.map (fn x => case x of NONE => G.WildcardTerm
| SOME ty => ty) typs))
else NONE
end
| extractTypFromAtPat (LISTAtPatX pats) =
let
val typs = % extractTypFromPat pats
in
(case List.find (Option.isSome) typs of
NONE => NONE
| SOME (SOME typ) => SOME (G.ExplicitTerm ("list", [typ])))
end
| extractTypFromAtPat _ = NONE
and extractTypFromPat (ATPat atpat) = extractTypFromAtPat (~atpat)
| extractTypFromPat (CONPat(_, longvid, atpat)) =
(case extractTypFromAtPat(~atpat) of
NONE => NONE
| SOME _ => raise Fail "Constructor Pattern shouldn't have interior types\n")
| extractTypFromPat (COLONPat (pat, ty)) = SOME (ty2term(~ty))
| extractTypFromPat (ASPat (_, _, SOME ty, _)) = SOME (ty2term(~ty))
| extractTypFromPat (ASPat (_, _, NONE, pat)) = extractTypFromPat (~pat)
| extractTypFromPat (INFIXPatX(_, _, atpat)) = extractTypFromAtPat (~atpat)
and atpat2binders (WILDCARDAtPat@@_ : AtPat) withTy : G.binder list =
[G.SingleBinder {name = G.WildcardName, typ = NONE, inferred = false}]
| atpat2binders ((SCONAtPat scon)@@_) withTy = raise Fail "Invalid Pattern!\n"
| atpat2binders (IDAtPat(_, longvid)@@_) withTy =
[G.SingleBinder {name = mkName (lvid2id (~longvid)), typ = NONE, inferred = false}]
| atpat2binders (PARAtPat(pat)@@_) withTy = pat2binders (pat) withTy
| atpat2binders p withTy = [G.PatternBinder (atpat2pattern p)]
and pat2binders (ATPat(atpat)@@_ : Pat) (withTy) : G.binder list =
atpat2binders (atpat) withTy
| pat2binders (COLONPat(pat, ty)@@_) (true) =
(case pat of
(ATPat(IDAtPat(_, longvid)@@_)@@_) =>
[G.GenericBinder {name = mkName (lvid2id (~longvid)), typ = SOME (typ2typ tyvarCtx (ty2type ty)), inferred = false}]
| _ => pat2binders (pat) true)
| pat2binders (COLONPat(pat, ty)@@_) (withTy) = pat2binders (pat) withTy
| pat2binders (p) (_) = [G.PatternBinder (pat2pattern p)]
(* FROM: SyntaxCoreFn.sml: 144 -> 152
* TO: Gallina.sml : 96 -> 114
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: atpat
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: pattern
*)
and atpat2pattern (WILDCARDAtPat@@_ : AtPat) : G.pattern = G.WildcardPat
| atpat2pattern (SCONAtPat(scon)@@_) = scon2pattern (~scon)
(* ignoring Op for now *)
| atpat2pattern (IDAtPat (_, longvid)@@_) = G.QualidPat (lvid2id (~longvid))
| atpat2pattern (RECORDAtPat(recBody)@@A) =
let
val SOME (_, labs) = !(hd (tl A))
val labs = (case !labs of
S.RowType (l, _) => l
| S.Determined (ref (S.RowType (l, _))) => l (* For {...} patterns *)
| _ => raise Fail "Error fetching labels from record patterns."
)
val labs = #1 (ListPair.unzip (LabMap.listItemsi labs))
val (ident, body) = case recBody of NONE => ("", [])
| SOME recBody => patrow2body labs recBody
in
if List.length body = 0 then G.WildcardPat
else G.RecPat (patbody2fields ident body)
end
| atpat2pattern (PARAtPat(pat)@@_) = G.ParPat (pat2pattern (pat))
| atpat2pattern (UNITAtPatX@@_) = G.UnitPat
| atpat2pattern (TUPLEAtPatX(pats)@@_) = G.TuplePat (List.map pat2pattern pats)
| atpat2pattern (LISTAtPatX(pats)@@_) = G.ListPat (List.map pat2pattern pats)
(* FROM: SyntaxCoreFn.sml: 159 -> 163
* TO: Gallina.sml : 96 -> 114
* FROM SECTION: Appendix C.1 page 103
* KEYWORD: pat
* TO SECTION: Section 4.1.3 Page 115
* KEYWORD: pattern
*)
and pat2pattern (ATPat(atpat)@@_ : Pat) : G.pattern = atpat2pattern atpat
| pat2pattern (CONPat (opVal, longvid, atpat)@@_) = G.ArgsPat (opetize false (lvid2id (~longvid)), [atpat2pattern (atpat)])
| pat2pattern (COLONPat (pat, ty)@@_) =
let val _ = print "Coq doesn't support type casting in patterns!\n"
in pat2pattern(pat) end
(* Can ASPat ever has a non-empty Op? *)
| pat2pattern (ASPat(_, vid, ty, pat)@@_) =
let val _ = if Option.isSome ty then print "Coq doesn't support type casting in patterns!\n"
else ()
in G.AsPat(pat2pattern(pat), vid2id (~vid)) end
(* Can INFIXPatX ever has a non-empty Op? *)
| pat2pattern (INFIXPatX (_, longvid, atpat)@@_) = G.InfixPat (lvid2id (~longvid), [atpat2pattern (atpat)])
(* Helper function doesn't have corresponding sections, check valBind2sent *)
and patBody2sents (G.QualidPat ident : G.pattern, body : G.term) (_ : bool): G.sentence list =
[G.DefinitionSentence
(G.DefinitionDef
{localbool = false, id = ident, binders = T.clearTyvars true tyvarCtx, typ = NONE, body = body})]
(* As patterns are split into two definitions:
val x as y = 1 becomes Defintion x := 1; Definition y := 1 *)
| patBody2sents (G.AsPat (pat, id), body) exhaustive =
(patBody2sents (G.QualidPat id, body) exhaustive) @ (patBody2sents (pat, body) exhaustive)
(* Wildcard patterns are ignored because apart from side effects, they cannot change the context *)
| patBody2sents (G.WildcardPat, _) (_) = []
(* Parenthesis patterns are ignored (e.g. val (x) = 1) *)
| patBody2sents (G.ParPat pat, body) exhaustive = patBody2sents (pat, body) exhaustive
(* N-ary Tuple patterms are split into N-ary definitions:
val (x, y ) = (1, 2) becomes Definition x := 1; Definition y := 2 *)
| patBody2sents (pat as G.TuplePat pats, body) exhaustive =
List.concat (List.map (patBody2sents' (pat, body, exhaustive)) pats)
(* N-ary Tuple patterms are split into N-ary definitions:
val (x, y) = (1, 2) becomes Definition x := 1; Definition y := 2 *)
| patBody2sents (pat as G.ListPat pats, body) exhaustive =
List.concat (List.map (patBody2sents' (pat, body, exhaustive)) pats)
| patBody2sents (pat as G.ArgsPat (id, pats), body) exhaustive =
List.concat (List.map (patBody2sents' (pat, body, exhaustive)) pats)
| patBody2sents (pat as G.RecPat fieldpats, body) exhaustive =
List.concat (List.map (patBody2sents' (pat, body, exhaustive))
(List.map (fn (G.FieldPat {pat = pat, ...}) => pat) fieldpats))
| patBody2sents (pat as G.InfixPat (id, pats), body) exhaustive =
List.concat (List.map (patBody2sents' (pat, body, exhaustive)) pats)
| patBody2sents _ _ = raise Fail "Invalid pattern! \n"
(* Helper function doesn't have corresponding sections, check valBind2sent *)
and patBody2sents' (matchees : G.pattern, matcher : G.term, exhaustive : bool) (pat as G.QualidPat ident : G.pattern) : G.sentence list =
patBody2sents(pat, mkMatchNotationTerm (G.MatchItem matcher, matchees) (G.IdentTerm ident, exhaustive)) exhaustive
| patBody2sents' (matchees, matcher, exhaustive) (G.AsPat (pat, id)) =
(patBody2sents' (matchees, matcher, exhaustive) pat) @
(patBody2sents (
G.QualidPat id,
mkMatchNotationTerm (G.MatchItem matcher, matchees) (G.IdentTerm id , exhaustive)) exhaustive)
| patBody2sents' (matchees, matcher, exhaustive) (G.ArgsPat(id, pats)) =
List.concat (List.map (patBody2sents' (matchees, matcher, exhaustive)) pats)
| patBody2sents' (matchees, matcher, exhaustive) (G.TuplePat pats) =
List.concat (List.map (patBody2sents' (matchees, matcher, exhaustive)) pats)
| patBody2sents' (matchees, matcher, exhaustive) (G.ListPat pats) =
List.concat (List.map (patBody2sents' (matchees, matcher, exhaustive)) pats)
| patBody2sents' (matchees, matcher, exhaustive) (G.ParPat pat) =
patBody2sents' (matchees, matcher, exhaustive) pat
| patBody2sents' _ G.WildcardPat = []
| patBody2sents' (matchees, matcher, exhaustive) (G.InfixPat(id, pats)) =
List.concat (List.map (patBody2sents' (matchees, matcher, exhaustive)) pats)
| patBody2sents' _ (G.NumPat _) = []
| patBody2sents' _ (G.StringPat _) = []
| patBody2sents' _ (G.CharPat _) = []
| patBody2sents' _ _ = raise Fail "Invalid pattern!"
(* EXAMPLE: type ('a, 'b) age = 'a * 'b (the lhs ('a, 'b))
* FROM: TyVar.sml: 25 -> 29
* TO: Gallina.sml : 59
* FROM SECTION: -
* KEYWORD: -
* TO SECTION: 4.1.3 page 115
* KEYWORD: binder
* NOTES:
* inferredVal is always false because the variable is always explicit
*)
and tyvarseq2binder (tyvars: TyVar.TyVar list) : G.binder list =
let
fun tyvar2binder tyvars =
let
val nameVal = mkName (TyVar.toString tyvars)
val typVal = SOME (mkSortTerm 1)
val inferredVal = true
in
G.SingleBinder {name = nameVal, typ = typVal, inferred = inferredVal}
end
in
List.map tyvar2binder tyvars
end
(*
This conversion function handles the translation of the
abstract type variables in a datatype into the corresponding variables
in the induction principle
*)
and tyvarseq2binder2 (tyvars: TyVar.TyVar list) : G.binder =
G.MultipleBinders{names = List.map (fn x => mkName (TyVar.toString x)) tyvars, typ = (mkSortTerm 1),
inferred = false}
and names2binder ([] : G.name list) : G.binder list = []
| names2binder names = [G.MultipleBinders {names = names, typ = mkSortTerm 1, inferred = true}]
(* EXAMPLE: datatype cards = Hearts | Spades | Clubs | Diamonds (rhs is conbind)
* FROM: SyntaxCoreFn.sml: 130
* TO: Gallina.sml : 147
* FROM SECTION: Appendix C.1 page 104
* KEYWORD: conbind
* TO SECTION: 4.1.4 page 120
* KEYWORD: indbody (rhs is a clause list)
* NOTES:
* ignoring Op for now, check SyntaxCore for more info
*)
and conbind2clauses(cons @@ _ : ConBind) : G.clause list =
let
val ConBind(_, tycon, ty, conbind2) = cons
val idVal = vid2id (~tycon)
val binderVal = []
val typVal = (case ty of
SOME ty' => SOME (ty2term (~ty'))
| _ => NONE)
val clauses = ?conbind2clauses conbind2
val clause = G.Clause (idVal, binderVal, typVal)
in
clause :: ?conbind2clauses conbind2
end
and tyrow2sent (names : G.name list, body : (G.ident * Ty') list, ident : G.ident) : G.sentence =
G.RecordSentence [G.RecordBody {id = ident, binders = names2binder names, typ = NONE,
consName = NONE, body = [tyrow2field ident body] }]
and tyrow2field ident (body : (G.ident * Ty') list) : G.field =
G.Field (List.map (fn (id, ty) => (ident ^ "_" ^ checkLegal id, ty2term ty)) body)
and fnexp2funbody (FNExp(Match(Mrule(ATPat(IDAtPat(_, longvid)@@_)@@_, exp)@@_,_)@@_)) : G.binder list * G.term =
let val (binders, body) = fnexp2funbody (~exp)
val binders = (T.clearTyvars true tyvarCtx) @ binders
val name = mkName (lvid2id(~longvid))
val binder = G.SingleBinder {name = name, typ = NONE, inferred = false}
in
(binder :: binders, body)
end
| fnexp2funbody (exp) = ([], exp2term exp)
and valbind2fixbodies (PLAINValBind(
ATPat(IDAtPat(_, longvid)@@_)@@_,
exp@@_,
valbind2)@@_) : G.fixbody list =
let val ident = lvid2id (~longvid)
val (binders, body) = fnexp2funbody exp
val typ = NONE
val decArg = NONE
val fixBody = G.Fixbody {id = ident, typ = typ, decArg = decArg, binders = binders, body = body }
in
fixBody :: (?valbind2fixbodies valbind2)
end
(* EXAMPLE: type age = int
* FROM: SyntaxCoreFn.sml: 123 -> 124
* TO: Gallina.sml : 100 -> 108
* FROM SECTION: Appendix C.1 page 104
* KEYWORD: typbind/typdesc ---there is a typo in the reference manual
and typbind is replaced by typdesc
* TO SECTION: 4.1.4 page 120
* KEYWORD: definition
* NOTES:
* returns sentence list because one typbind can encode multiple
* gallina definitions e.g. type age = int and name = string
*)
and typbind2sent(typbind: TypBind) : G.sentence =
let fun typbind2sents (typbind @@ _) =
let
val TypBind(tyvars, tycon, ty, typbind2) = typbind
val localboolVal = false
val idVal = tycon2id (~tycon)
val parametersVal = tyvarseq2binder (List.map ~ ($(~tyvars)))
val typVal = NONE
val bodyVal = ty2term (~ty)
val definition = G.DefinitionDef
{localbool = localboolVal, id = idVal, binders = parametersVal, typ = typVal, body = bodyVal}
val res = G.DefinitionSentence (definition)
in
res :: ?typbind2sents typbind2
end
val sents = typbind2sents typbind
val sent = G.SeqSentences (!recordContext @ (typbind2sents typbind))
in (recordContext := []; sent) end
(* EXAMPLE: datatype cards = Hearts | Spades | Clubs | Diamonds
* FROM: SyntaxCoreFn.sml: 126 -> 127
* TO: Gallina.sml : 100 -> 108
* FROM SECTION: Appendix C.1 page 104
* KEYWORD: datbind
* TO SECTION: 4.1.4 page 120
* KEYWORD: definition
*)
and datbind2sent(datbind : DatBind) : G.sentence =
let fun datbind2indbodies (datbind @@ _: DatBind) : G.indBody list =
let
val DatBind(tyvars, tycon, cons, datbind2) = datbind
val idVal = tycon2id (~tycon)
val parametersVal = tyvarseq2binder (List.map ~ ($(~tyvars)))
val typVal = mkSortTerm 1
val clauses = conbind2clauses(cons)
val clauses = List.map (updateTerm idVal parametersVal) clauses
val indBody = G.IndBody {id = idVal, bind = parametersVal, typ = typVal, clauses = clauses}
in
indBody :: (?datbind2indbodies (datbind2))
end
val sent = G.InductiveSentence(G.Inductive(datbind2indbodies datbind))
val recordC = !recordContext
val _ = recordContext := []
in
if recordC = [] then sent else G.SeqSentences (recordC @ [sent])
end
(* This function automatically takes in a datatype declaration
* generates a proof object for its corresponding induction principle
* taking into account that the arguments to a datatype
* constructor are in the form of tuples which Gallina cannot account for.
*
*)
and datbind2indproofobj (datbind : DatBind) : G.sentence =
let
fun generateArgVars (ty, r) =
case ty of
VARTy tyvar => (r := !r + 1; [(G.IdentTerm("p"^Int.toString (!r)),
checkLegal(TyVar.toString (~tyvar)), [])])
| PARTy typvar => generateArgVars(~typvar, r)
| CONTy (tyseq, tycon) => let
val terms = List.concat (% (ty2list) ($(~tyseq)))
val tycon = ltycon2id (~tycon)
in
(r := !r + 1; [(G.IdentTerm("p"^Int.toString (!r)), tycon, terms)])
end
| TUPLETyX tlistt => List.concat (List.map (fn x => generateArgVars(~x,r)) tlistt)
fun createarrowtype (argVars, typname, typparams, consName) : G.term =
let
val test = List.concat (List.map (fn (id, typ, vals) => if vals = typparams andalso typ = typname then
[G.ApplyTerm(G.IdentTerm("P"), [G.Arg(id)]) ] else []) argVars)
val lastterm = G.ApplyTerm(G.IdentTerm("P"),
[G.Arg(G.TupleTerm( [G.ApplyTerm(G.IdentTerm(consName),
[G.Arg(G.TupleTerm(List.map (fn x => #1 x) argVars))])]))])
in
List.foldr (fn (x,y) => G.ArrowTerm(x,y)) lastterm (test)
end
fun returnRecursiveArgs (argVars, typname, typparams, consName) : G.term list =
let
val test = List.concat (List.map (fn (id, typ, vals) => if vals = typparams andalso typ = typname then
[id] else []) argVars)
in
test
end
fun cons2cases (cons @@ _ : ConBind, parentID, parenTY, recCall) : (Gallina.binder * Gallina.equation) list =
let
val ConBind(_, tycon, ty, conbind2) = cons
val idVal = vid2id (~tycon)
val binderVal = []
val hasparams = (case ty of SOME _ => true | _ => false)
val typVal = (case ty of
SOME ty' => SOME (ty2term (~ty'))
| _ => NONE)
val argCount = ref 0
val argVars = case ty of SOME ty' => generateArgVars(~ty', argCount) | _ => []
val argNames = List.map (fn (G.IdentTerm(x),_,_) => x) argVars
(* remember to add recursive calls as tuple args *)
val argList = if hasparams then List.map (fn (x,_,_) => G.Arg(x)) argVars else []
val recArgs = returnRecursiveArgs (argVars, parentID, parenTY, idVal)
val indprincipname = if recCall <> [] then List.hd recCall else ""
val remainingArgs = if recCall <> [] then List.drop(recCall, 1) else []
val appTerm = List.map (fn x => G.Arg(G.TupleTerm([G.ApplyTerm(G.IdentTerm(indprincipname),
(List.map (fn y => G.Arg(G.IdentTerm(y))) remainingArgs) @ [G.Arg(x)])])) ) recArgs
val argList = argList @ appTerm
val caseEq = if hasparams then G.Equation(G.ArgsPat(idVal,
[G.TuplePat (List.map (fn x => G.QualidPat (x)) argNames)]),
G.ApplyTerm (G.IdentTerm("Hyp_" ^ idVal), argList)) else
G.Equation(G.QualidPat(idVal) , G.IdentTerm("Hyp_" ^ idVal))
in
case hasparams of
false => (G.SingleBinder {name = G.Name("Hyp_" ^ idVal) ,typ = SOME (G.ApplyTerm(G.IdentTerm("P"),
[G.Arg(G.IdentTerm(idVal))])), inferred = false }, caseEq) ::
? (fn x => cons2cases (x,parentID,parenTY, recCall)) conbind2
| _ =>
let
val fralltm = G.SingleBinder {name = G.Name ("Hyp_" ^ idVal), inferred = false,
typ = SOME (G.ForallTerm(List.map
(fn (G.IdentTerm(v),_,_) => G.SingleBinder{name = mkName v,
typ = NONE, inferred = false}) argVars,
createarrowtype(argVars, parentID, parenTY, idVal ))) }
in
(fralltm, caseEq) :: ?(fn x => cons2cases (x,parentID,parenTY, recCall)) conbind2
end
end
fun datbind2fixpoint (datbind @@ _ : DatBind) : G.fixpoint =
let
val DatBind(tyvars, tycon, cons, datbind2) = datbind
(* type name *)
val idVal = tycon2id (~tycon)
(* abstract type vars*)
val filterTyvars = List.map ~ ($(~tyvars))
val varNames = List.map (fn v => checkLegal(TyVar.toString v))
filterTyvars
val typVars1 = tyvarseq2binder2 (filterTyvars)
val explicit = filterTyvars <> []
val lhsProp = if explicit then G.ExplicitTerm(idVal,
List.map (fn x => G.IdentTypTerm(checkLegal(TyVar.toString x))) filterTyvars)
else G.IdentTerm(idVal)
val propBinder = G.SingleBinder {name = G.Name("P"),
typ = SOME(G.ArrowTerm(lhsProp , G.SortTerm(G.Prop))), inferred = false}
val consRes = cons2cases (cons, idVal, varNames, [])
val binderList = if explicit then [typVars1, propBinder] else [propBinder]
val binderList = binderList @ (List.map (fn (x,_) => x) consRes)
@ [G.SingleBinder {name = G.Name(idVal ^ "_obj"), typ = SOME(lhsProp), inferred = false}]
val binderNames = (idVal ^ "_ind_princip_proof") ::
List.concat (List.map (fn G.SingleBinder{name = G.Name(x), ...} => [x] |
G.MultipleBinders{names = ls, ...} => List.map (fn G.Name(x) => x) ls) binderList)
val binderNames = List.take(binderNames, (List.length binderNames) - 1)
val consRes = cons2cases (cons, idVal, varNames, binderNames)
val funbody = G.MatchTerm {matchItems = [G.MatchItem (G.IdentTerm(idVal ^ "_obj"))],
body = List.map (fn (_,x) => x) consRes}
in
G.Fixpoint([G.Fixbody {id = idVal ^ "_ind_princip_proof" , binders = binderList,
decArg = NONE, typ = SOME(G.ApplyTerm(G.IdentTerm("P"),
[G.Arg (G.IdentTerm(idVal ^ "_obj"))])), body = funbody}])
end
in
G.FixpointSentence(datbind2fixpoint(datbind))
end
(* This function automatically generates an induction principle for
* a given datatype declaration taking into account that
* the arguments to a constructor are in the form of tuples which
* Gallina cannot account for.
*
* EXAMPLE SML INPUT: datatype 'a tree = Leaf of 'a | Node of 'a tree * 'a tree
* CORRESPONDING GALLINA OUTPUT:
* Theorem tree_ind_princip : forall (a : Type) (P : @tree a), (forall p1, P (Leaf p1))
* -> (forall p1 p2, P p1 -> P p2 -> P (Node (p1,p2))) -> (forall tree_obj, P (tree_obj))
*)
and datbind2indprinciple(datbind : DatBind) : G.sentence =
let
fun generateArgVars (ty, r) =
case ty of
VARTy tyvar => (r := !r + 1; [(G.IdentTerm("p"^Int.toString (!r)),
checkLegal(TyVar.toString (~tyvar)), [])])
| PARTy typvar => generateArgVars(~typvar, r)
| CONTy (tyseq, tycon) => let
val terms = List.concat (% (ty2list) ($(~tyseq)))
val tycon = ltycon2id (~tycon)
in
(r := !r + 1; [(G.IdentTerm("p"^Int.toString (!r)), tycon, terms)])
end
| TUPLETyX tlistt => List.concat (List.map (fn x => generateArgVars(~x,r)) tlistt)
fun createarrowtype (argVars, typname, typparams, consName) : G.term =
let
val test = List.concat (List.map (fn (id, typ, vals) => if vals = typparams andalso typ = typname then
[G.ApplyTerm(G.IdentTerm("P"), [G.Arg(id)]) ] else []) argVars)
val lastterm = G.ApplyTerm(G.IdentTerm("P"),
[G.Arg(G.TupleTerm( [G.ApplyTerm(G.IdentTerm(consName),
[G.Arg(G.TupleTerm(List.map (fn x => #1 x) argVars))])]))])
in
List.foldr (fn (x,y) => G.ArrowTerm(x,y)) lastterm (test)
end
fun cons2cases (cons @@ _ : ConBind, parentID, parenTY) : G.term list =
let
val ConBind(_, tycon, ty, conbind2) = cons
val idVal = vid2id (~tycon)
val binderVal = []
val hasparams = (case ty of SOME _ => true | _ => false)
val typVal = (case ty of
SOME ty' => SOME (ty2term (~ty'))
| _ => NONE)
val argCount = ref 0
val argVars = case ty of SOME ty' => generateArgVars(~ty', argCount) | _ => []
in
case hasparams of
false => G.ApplyTerm(G.IdentTerm("P"),
[G.Arg(G.IdentTerm(idVal))]) :: ? (fn x => cons2cases (x,parentID,parenTY)) conbind2
| _ =>
let
val fralltm = G.ForallTerm(List.map
(fn (G.IdentTerm(v),_,_) => G.SingleBinder{name = mkName v,
typ = NONE, inferred = false}) argVars,
createarrowtype(argVars, parentID, parenTY, idVal ))
in
fralltm :: ?(fn x => cons2cases (x,parentID,parenTY)) conbind2
end
end
fun datbind2forall (datbind @@ _ : DatBind) : G.theorem =
let
val DatBind(tyvars, tycon, cons, datbind2) = datbind
val idVal = tycon2id (~tycon)
val filterTyvars = List.map ~ ($(~tyvars))
val varNames = List.map (fn v => checkLegal(TyVar.toString v)) filterTyvars
val typVars1 = tyvarseq2binder2 (filterTyvars)
val explicit = filterTyvars <> []
val lhsProp = if explicit then G.ExplicitTerm(idVal,
List.map (fn x => G.IdentTypTerm(checkLegal(TyVar.toString x))) filterTyvars)
else G.IdentTerm(idVal)
val propBinder = G.SingleBinder {name = G.Name("P"),
typ = SOME(G.ArrowTerm(lhsProp , G.SortTerm(G.Prop))), inferred = false}
val binderList = if explicit then [typVars1, propBinder] else [propBinder]
val lastsent = G.ForallTerm([G.SingleBinder{name = G.Name(idVal ^ "_obj"),
typ = NONE, inferred = false}],
G.ApplyTerm(G.IdentTerm("P"), [G.Arg (G.IdentTerm(idVal ^ "_obj"))]))
val sent2 = List.foldr (fn (x,y) => G.ArrowTerm(x,y))
lastsent (cons2cases (cons, idVal, varNames))
val sent = G.ForallTerm(binderList,sent2)
in
G.Theorem(idVal ^ "_ind_princip", sent)
end
in
G.TheoremSentence(datbind2forall(datbind))
end
(* EXAMPLE: 1. val x = 5
* 2. val (x, y) = (1, 2)
* 3. val x :: l = [1, 2, 3]
* FROM: SyntaxCoreFn.sml: 124 -> 126
* TO: Gallina.sml : 137 -> 138
* FROM SECTION: Appendix C.1 page 104
* KEYWORD: valbind
* TO SECTION: Section 4.1.4 page 120
* KEYWORD : definition
*)
and valbind2sent(valbind: ValBind): G.sentence =
let fun valbind2sents (PLAINValBind(pat, exp, valbind2) @@ A) =
let
val exhaustive = isExhaustive A
val body = exp2term (~exp)
val pat = pat2pattern (pat)
val sents = patBody2sents (pat, body) exhaustive
val sents' = ?valbind2sents valbind2
in
!recordContext @ !infixContext @ sents @ sents'
end
| valbind2sents (RECValBind(PLAINValBind(
ATPat(IDAtPat(_, longvid)@@_)@@_,
exp@@_,