-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcamlPtreeConversionScript.sml
3432 lines (3264 loc) · 106 KB
/
camlPtreeConversionScript.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
(*
A theory for converting OCaml parse trees to abstract syntax.
*)
open preamble caml_lexTheory camlPEGTheory astTheory;
open precparserTheory;
local open cmlParseTheory lexer_implTheory in end
val _ = new_theory "camlPtreeConversion";
val _ = set_grammar_ancestry [
"misc", "pegexec", "caml_lex", "camlPEG", "ast", "precparser", "sum"];
(* -------------------------------------------------------------------------
* Sum monad syntax
* ------------------------------------------------------------------------- *)
Definition bind_def[simp]:
bind (INL e) f = INL e ∧
bind (INR x) f = f x
End
Definition ignore_bind_def[simp]:
ignore_bind m1 m2 = bind m1 (λu. m2)
End
Definition choice_def[simp]:
choice (INL e) b = b ∧
choice (INR x) b = INR x
End
Definition return_def[simp]:
return = INR
End
Definition fail_def[simp]:
fail = INL
End
val sum_monadinfo : monadinfo = {
bind = “bind”,
ignorebind = SOME “ignore_bind”,
unit = “return”,
fail = SOME “fail”,
choice = SOME “choice”,
guard = NONE
};
val _ = declare_monad ("sum", sum_monadinfo);
val _ = enable_monadsyntax ();
val _ = enable_monad "sum";
Definition mapM_def:
mapM f [] = return [] : 'a + 'b list ∧
mapM f (x::xs) =
do
y <- f x;
ys <- mapM f xs;
return (y::ys)
od
End
Theorem mapM_cong[defncong]:
∀xs ys f g.
xs = ys ∧
(∀x. MEM x xs ⇒ f x = g x) ⇒
mapM f xs: 'a + 'b list = mapM g ys
Proof
Induct \\ rw [mapM_def]
\\ Cases_on ‘g h’ \\ fs [mapM_def]
\\ ‘mapM f xs = mapM g xs’ suffices_by simp_tac std_ss []
\\ first_x_assum irule \\ fs []
QED
Definition option_def[simp]:
option NONE = INL (unknown_loc, «option») ∧
option (SOME x) = INR x
End
Definition fmap_def[simp]:
fmap f (INR x) = INR (f x) ∧
fmap f (INL err) = INL err
End
(* Builds the n-ary cartesian product of n lists (of any lengths).
*)
Definition list_cart_prod_def:
list_cart_prod [] = [[]] ∧
list_cart_prod (xs::xss) =
FLAT (MAP (λx. MAP (λy. x::y) (list_cart_prod xss)) xs)
End
(* -------------------------------------------------------------------------
* Compatibility layer
* ------------------------------------------------------------------------- *)
(* Resolving precedences and lifting or-patterns to the top at the same time
* is just too annoying. Until the CakeML syntax supports the latter, we can
* use this pre-pattern type.
*
* Pp_prod and Pp_as correspond to Pp_con NONE and Pp_alias, and are used to
* trick the precparser into producing n-ary tuples and suffixes (i.e. the
* as-patterns).
*)
Datatype:
ppat = Pp_any
| Pp_var varN
| Pp_lit lit
| Pp_con ((modN, conN) id option) (ppat list)
| Pp_record ((modN, conN) id) (varN list)
| Pp_prod (ppat list)
| Pp_or ppat ppat
| Pp_tannot ppat ast_t
| Pp_alias ppat (varN list)
| Pp_as ppat varN
End
Definition ppat_size'_def:
ppat_size' Pp_any = 0 ∧
ppat_size' (Pp_var a) = 1 ∧
ppat_size' (Pp_lit a) = 1 ∧
ppat_size' (Pp_con x xs) = (1 + list_size ppat_size' xs) ∧
ppat_size' (Pp_record x xs) = (1 + list_size (list_size char_size) xs) ∧
ppat_size' (Pp_prod xs) = (1 + list_size ppat_size' xs) ∧
ppat_size' (Pp_or x y) = (1 + ppat_size' x + ppat_size' y) ∧
ppat_size' (Pp_tannot x y) = (1 + ppat_size' x) ∧
ppat_size' (Pp_alias x y) = (1 + ppat_size' x) ∧
ppat_size' (Pp_as x y) = (1 + ppat_size' x)
Termination
WF_REL_TAC ‘measure ppat_size’
End
(* Convert ppat patterns to CakeML patterns by distributing or-patterns at the
* top-level (returning a list of patterns).
*
* Record patterns don't fit this very well, as there is no syntax for them. To
* make things simple, we only allow them at the top-level, and this function
* will bail out if it finds one.
*)
Definition ppat_to_pat_def:
ppat_to_pat Pp_any =
return [Pany] ∧
ppat_to_pat (Pp_var v) =
return [Pvar v] ∧
ppat_to_pat (Pp_lit l) =
return [Plit l] ∧
ppat_to_pat (Pp_tannot pp t) =
fmap (MAP (λp. Ptannot p t)) (ppat_to_pat pp) ∧
ppat_to_pat (Pp_con id pps) =
do
qs <- ppat_to_pats pps;
return (MAP (λps. Pcon id ps) (list_cart_prod qs))
od ∧
ppat_to_pat (Pp_record id fs) =
fail (unknown_loc, «») ∧
ppat_to_pat (Pp_prod pps) =
do
qs <- (ppat_to_pats pps);
return (MAP (λps. Pcon NONE ps) (list_cart_prod qs))
od ∧
ppat_to_pat (Pp_alias pp ns) =
fmap (MAP (λp. FOLDL Pas p ns)) (ppat_to_pat pp) ∧
ppat_to_pat (Pp_as pp n) =
fmap (MAP (λp. Pas p n)) (ppat_to_pat pp) ∧
ppat_to_pat (Pp_or p1 p2) =
do
ps1 <- ppat_to_pat p1;
ps2 <- ppat_to_pat p2;
return (ps1 ++ ps2)
od ∧
ppat_to_pats [] = return [] ∧
ppat_to_pats (p::ps) =
do
p1 <- ppat_to_pat p;
ps1 <- ppat_to_pats ps;
return (p1::ps1)
od
Termination
WF_REL_TAC ‘measure sum_size ppat_size (list_size ppat_size)’
End
(* Fix some constructor applications that needs to be in CakeML's curried
* style. These are things from basis and Candle; all new constructors will
* get tupled arguments.
*)
Definition compatCurryP_def:
compatCurryP id pat =
case id of
Long mn vn =>
if mn = "PrettyPrinter" ∧ vn = Short "PP_Data" then
case pat of
Pp_con NONE ps => Pp_con (SOME id) ps
| _ => Pp_con (SOME id) [pat]
else
Pp_con (SOME id) [pat]
| Short nm =>
if nm = "Abs" ∨ nm = "Var" ∨ nm = "Const" ∨ nm = "Comb" ∨
nm = "Tyapp" ∨ nm = "Sequent" ∨ nm = "Append" then
case pat of
| Pp_any => Pp_con (SOME id) [Pp_any; Pp_any]
| Pp_con NONE ps => Pp_con (SOME id) ps
| _ => Pp_con (SOME id) [pat]
else
Pp_con (SOME id) [pat]
End
Definition compatCurryE_def:
compatCurryE id exp =
case id of
Long mn vn =>
if mn = "PrettyPrinter" ∧ vn = Short "PP_Data" then
case exp of
Con NONE xs => Con (SOME id) xs
| _ => Con (SOME id) [exp]
else
Con (SOME id) [exp]
| Short nm =>
if nm = "Abs" ∨ nm = "Var" ∨ nm = "Const" ∨ nm = "Comb" ∨
nm = "Tyapp" ∨ nm = "Sequent" ∨ nm = "Append" then
case exp of
Con NONE xs => Con (SOME id) xs
| _ => Con (SOME id) [exp]
else if nm = "Ref" then
App Opref [exp]
else
Con (SOME id) [exp]
End
(* Rename some constructors from basis that Candle needs (but otherwise cannot
* parse).
*)
Definition compatCons_def:
compatCons cn =
if cn = "Bad_file_name" then "BadFileName"
else if cn = "Pp_data" then "PP_Data"
else cn
End
(* Rename some module names from basis that Candle needs (but otherwise cannot
* parse).
*)
Definition compatModName_def:
compatModName mn =
if mn = "Text_io" then "TextIO"
else if mn = "Pretty_printer" then "PrettyPrinter"
else if mn = "Command_line" then "CommandLine"
else if mn = "Word8_array" then "Word8Array"
else mn
End
(* -------------------------------------------------------------------------
* Parse tree conversion
* ------------------------------------------------------------------------- *)
Overload psize[local] = “parsetree_size (K 0) (K 0) (K 0)”;
Overload p1size[local] = “parsetree1_size (K 0) (K 0) (K 0)”;
Theorem parsetree_size_lemma[local]:
p1size = list_size psize
Proof
rw [FUN_EQ_THM]
\\ Induct_on ‘x’ \\ rw [list_size_def, grammarTheory.parsetree_size_def]
QED
Definition destLf_def:
destLf (Lf x) = return x ∧
destLf (Nd (nterm, locs) _) = fail (locs, «destLf»)
End
Definition expect_tok_def:
expect_tok symb (token: token) =
do
lf <- destLf symb;
tk <- option $ destTOK lf;
if tk = token then
return tk
else
fail (SND lf, «Unexpected token»)
od
End
Definition path_to_ns_def:
path_to_ns locs [] = fail (locs, «Empty path») ∧
path_to_ns locs [i] = return (Short i) ∧
path_to_ns locs (m::ms) =
do
id <- path_to_ns locs ms;
return $ Long m id
od
End
Definition ptree_Ident_def:
ptree_Ident (Lf (_, locs)) = fail (locs, «Expected ident non-terminal») ∧
ptree_Ident (Nd (nterm, locs) args) =
if nterm = INL nIdent then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
option $ destIdent tk
od
| _ => fail (locs, «Impossible: nIdent»)
else
fail (locs, «Expected ident non-terminal»)
End
(* Returns a (function) name corresponding to the operator.
*)
Definition ptree_Op_def:
ptree_Op (Lf (_, locs)) =
fail (locs, «Expected operator non-terminal») ∧
ptree_Op (Nd (nterm, locs) args) =
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
if nterm = INL nShiftOp then
if tk = LslT then
return "lsl"
else if tk = LsrT then
return "lsr"
else if tk = AsrT then
return "asr"
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nShiftOp»)
else if nterm = INL nMultOp then
if tk = StarT then
return "*"
else if tk = ModT then
return "mod"
else if tk = LandT then
return "land"
else if tk = LorT then
return "lor"
else if tk = LxorT then
return "lxor"
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nMultOp»)
else if nterm = INL nAddOp then
if tk = PlusT then
return "+"
else if tk = MinusT then
return "-"
else if tk = MinusFT then
return "-."
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nAddOp»)
else if nterm = INL nRelOp then
if tk = LessT then
return "<"
else if tk = GreaterT then
return ">"
else if tk = EqualT then
return "="
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nRelOp»)
else if nterm = INL nAndOp then
if tk = AndalsoT then
return "&&"
else if tk = AmpT then
return "&"
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nAndOp»)
else if nterm = INL nOrOp then
if tk = OrelseT then
return "||"
else if tk = OrT then
return "|"
else if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nOrOp»)
else if nterm = INL nHolInfixOp then
if tk = FuncompT then
return "o"
else if tk = F_FT then
return "F_F"
else if tk = THEN_T then
return "THEN"
else if tk = THENC_T then
return "THENC"
else if tk = THENL_T then
return "THENL"
else if tk = THEN_TCL_T then
return "THEN_TCL"
else if tk = ORELSE_T then
return "ORELSE"
else if tk = ORELSEC_T then
return "ORELSEC"
else if tk = ORELSE_TCL_T then
return "ORELSE_TCL"
else
fail (locs, «Impossible: nHolInfixOp»)
else if nterm = INL nCatOp then
if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nCatOp»)
else if nterm = INL nPrefixOp then
if isSymbol tk then
let s = THE (destSymbol tk) in
return s
else
fail (locs, «Impossible: nPrefixOp»)
else if nterm = INL nAssignOp then
if tk = LarrowT then
return "<-"
else if tk = UpdateT then
return ":="
else
fail (locs, «Impossible: nAssignOp»)
else
fail (locs, «Expected operator non-terminal»)
od
| _ => fail (locs, «Expected operator non-terminal»)
End
Definition ptree_OperatorName_def:
ptree_OperatorName (Lf (_, locs)) =
fail (locs, «Expected operator-name non-terminal») ∧
ptree_OperatorName (Nd (nterm, locs) args) =
if nterm = INL nOperatorName then
case args of
[arg] => ptree_Op arg
| _ => fail (locs, «Impossible: nOperatorName»)
else
fail (locs, «Expected operator-name non-terminal»)
End
Definition ptree_ValueName_def:
ptree_ValueName (Lf (_, locs)) =
fail (locs, «Expected value-name non-terminal») ∧
ptree_ValueName (Nd (nterm, locs) args) =
if nterm = INL nValueName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
option $ destIdent tk
od
| [lpar; opn; rpar] =>
do
expect_tok lpar LparT;
expect_tok rpar RparT;
ptree_OperatorName opn
od
| _ => fail (locs, «Impossible: nValueName»)
else
fail (locs, «Expected value-name non-terminal»)
End
Definition ptree_ConstrName_def:
ptree_ConstrName (Lf (_, locs)) =
fail (locs, «Expected constr-name non-terminal») ∧
ptree_ConstrName (Nd (nterm, locs) args) =
if nterm = INL nConstrName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
fmap compatCons $ option $ destIdent tk
od
| _ => fail (locs, «Impossible: nConstrName»)
else
fail (locs, «Expected constr-name non-terminal»)
End
Definition ptree_TypeConstrName_def:
ptree_TypeConstrName (Lf (_, locs)) =
fail (locs, «Expected typeconstr-name non-terminal») ∧
ptree_TypeConstrName (Nd (nterm, locs) args) =
if nterm = INL nTypeConstrName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
option $ destIdent tk
od
| _ => fail (locs, «Impossible: nTypeConstrName»)
else
fail (locs, «Expected typeconstr-name non-terminal»)
End
Definition ptree_FieldName_def:
ptree_FieldName (Lf (_, locs)) =
fail (locs, «Expected fieldname non-terminal») ∧
ptree_FieldName (Nd (nterm, locs) args) =
if nterm = INL nFieldName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
option $ destIdent tk
od
| _ => fail (locs, «Impossible: nFieldName»)
else
fail (locs, «Expected fieldname non-terminal»)
End
Definition ptree_ModuleName_def:
ptree_ModuleName (Lf (_, locs)) =
fail (locs, «Expected modulename non-terminal») ∧
ptree_ModuleName (Nd (nterm, locs) args) =
if nterm = INL nModuleName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
fmap compatModName $ option $ destIdent tk
od
| _ => fail (locs, «Impossible: nModuleName»)
else
fail (locs, «Expected modulename non-terminal»)
End
Definition ptree_ModTypeName_def:
ptree_ModTypeName (Lf (_, locs)) =
fail (locs, «Expected modtypename non-terminal») ∧
ptree_ModTypeName (Nd (nterm, locs) args) =
if nterm = INL nModTypeName then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
option $ destIdent tk
od
| _ => fail (locs, «Impossible: nModTypeName»)
else
fail (locs, «Expected modtypename non-terminal»)
End
Definition ptree_ModulePath_def:
ptree_ModulePath (Lf (_, locs)) =
fail (locs, «Expected module-path non-terminal») ∧
ptree_ModulePath (Nd (nterm, locs) args) =
if nterm = INL nModulePath then
case args of
[arg] => fmap (λx. [x]) $ ptree_ModuleName arg
| [arg; dot; path] =>
do
expect_tok dot DotT;
vp <- ptree_ModulePath path;
vn <- ptree_ModuleName arg;
return (vn::vp)
od
| _ => fail (locs, «Impossible: nModulePath»)
else
fail (locs, «Expected module-path non-terminal»)
End
Definition ptree_ValuePath_def:
ptree_ValuePath (Lf (_, locs)) =
fail (locs, «Expected value-path non-terminal») ∧
ptree_ValuePath (Nd (nterm, locs) args) =
if nterm = INL nValuePath then
case args of
[arg] => fmap (λx. [x]) $ ptree_ValueName arg
| [path; dot; arg] =>
do
expect_tok dot DotT;
vp <- ptree_ModulePath path;
vn <- ptree_ValueName arg;
return (vp ++ [vn])
od
| _ => fail (locs, «Impossible: nValuePath»)
else
fail (locs, «Expected value-path non-terminal»)
End
Definition ptree_Constr_def:
ptree_Constr (Lf (_, locs)) = fail (locs, «Expected constr non-terminal») ∧
ptree_Constr (Nd (nterm, locs) args) =
if nterm = INL nConstr then
case args of
[arg] => fmap (λx. [x]) $ ptree_ConstrName arg
| [name; dot; rest] =>
do
expect_tok dot DotT;
vp <- ptree_ModuleName name;
vn <- ptree_Constr rest;
return (vp::vn)
od
| _ => fail (locs, «Impossible: nConstr»)
else
fail (locs, «Expected constr non-terminal»)
End
Definition ptree_TypeConstr_def:
ptree_TypeConstr (Lf (_, locs)) =
fail (locs, «Expected typeconstr non-terminal») ∧
ptree_TypeConstr (Nd (nterm, locs) args) =
if nterm = INL nTypeConstr then
case args of
[arg] => fmap (λx. [x]) $ ptree_TypeConstrName arg
| [path; dot; arg] =>
do
expect_tok dot DotT;
vp <- ptree_ModulePath path;
vn <- ptree_TypeConstrName arg;
return (vp ++ [vn])
od
| _ => fail (locs, «Impossible: nTypeConstr»)
else
fail (locs, «Expected typeconstr non-terminal»)
End
Definition ptree_ModTypePath_def:
ptree_ModTypePath (Lf (_, locs)) =
fail (locs, «Expected modtypepath non-terminal») ∧
ptree_ModTypePath (Nd (nterm, locs) args) =
if nterm = INL nModTypePath then
case args of
[arg] => fmap (λvn. [vn]) $ ptree_ModTypeName arg
| [name; dot; rest] =>
do
expect_tok dot DotT;
vm <- ptree_ModuleName name;
vp <- ptree_ModTypePath rest;
return (vm :: vp)
od
| _ => fail (locs, «Impossible: nModTypePath»)
else
fail (locs, «Expected typeconstr non-terminal»)
End
Definition ptree_TVar_def:
ptree_TVar (Lf (_, locs)) =
fail (locs, «Expected type variable non-terminal») ∧
ptree_TVar (Nd (nterm, locs) args) =
if nterm = INL nTVar then
case args of
[tick; id] =>
do
expect_tok tick TickT;
nm <- ptree_Ident id;
return (Atvar nm)
od
| _ => fail (locs, «Impossible: nTVar»)
else
fail (locs, «Expected type variable non-terminal»)
End
Definition ptree_Type_def:
(ptree_Type (Lf (_, locs)) =
fail (locs, «Expected a type non-terminal»)) ∧
(ptree_Type (Nd (nterm, locs) args) =
if nterm = INL nType then
case args of
[ty] => ptree_Type ty
| _ => fail (locs, «Impossible: nType»)
else if nterm = INL nTBase then
case args of
[lpar; args; rpar; ctor] =>
do
expect_tok lpar LparT;
expect_tok rpar RparT;
ts <- ptree_TypeList args;
nm <- ptree_TypeConstr ctor;
ns <- path_to_ns locs nm;
return (Atapp ts ns)
od
| [lpar; arg; rpar] =>
do
expect_tok lpar LparT;
expect_tok rpar RparT;
ptree_Type arg
od
| [arg] =>
ptree_TVar arg
| _ => fail (locs, «Impossible: nTBase»)
else if nterm = INL nTConstr then
case args of
arg::rest =>
do
ty <- ptree_Type arg;
ids <- mapM ptree_TypeConstr rest;
cns <- mapM (path_to_ns locs) ids;
return (FOLDL (λt id. Atapp [t] id) ty cns)
od ++
do
id <- ptree_TypeConstr arg;
cn <- path_to_ns locs id;
ids <- mapM ptree_TypeConstr rest;
cns <- mapM (path_to_ns locs) ids;
return (FOLDL (λt id. Atapp [t] id) (Atapp [] cn) cns)
od
| _ => fail (locs, «Impossible: nTConstr»)
else if nterm = INL nTProd then
case args of
[arg] => ptree_Type arg
| arg::rest =>
do
ty <- ptree_Type arg;
ts <- ptree_StarTypes rest;
return (Attup (ty::ts))
od
| _ => fail (locs, «Impossible: nTProd»)
else if nterm = INL nTFun then
case args of
[arg] => ptree_Type arg
| [arg;rarrow;fun] =>
do
expect_tok rarrow RarrowT;
ty1 <- ptree_Type arg;
ty2 <- ptree_Type fun;
return (Atfun ty1 ty2)
od
| _ => fail (locs, «Impossible: nTFun»)
else
fail (locs, «Expected type non-terminal»)) ∧
(ptree_TypeList (Lf (_, locs)) =
fail (locs, «Expected a type list non-terminal»)) ∧
(ptree_TypeList (Nd (nterm, locs) args) =
if nterm = INL nTypeList then
case args of
[typ;comma;tlist] =>
do
t <- ptree_Type typ;
expect_tok comma CommaT;
ts <- ptree_TypeList tlist;
return (t::ts)
od
| _ => fail (locs, «Impossible: nTypeList»)
else if nterm = INL nTypeLists then
case args of
[typ;comma;tlist] =>
do
t <- ptree_Type typ;
expect_tok comma CommaT;
ts <- ptree_TypeList tlist;
return (t::ts)
od
| [typ] => fmap (λt. [t]) $ ptree_Type typ
| _ => fail (locs, «Impossible: nTypeLists»)
else
fail (locs, «Expected a type list non-terminal»)) ∧
(ptree_StarTypes [] = return []) ∧
(ptree_StarTypes (x::xs) =
do
expect_tok x StarT;
ptree_StarTypes xs
od ++
do
t <- ptree_Type x;
ts <- ptree_StarTypes xs;
return (t::ts)
od)
Termination
WF_REL_TAC ‘measure $ sum_size psize $
sum_size psize (list_size psize)’
\\ simp [parsetree_size_lemma]
End
Definition ptree_Literal_def:
ptree_Literal (Lf (_, locs)) =
fail (locs, «Expected a literal non-terminal») ∧
ptree_Literal (Nd (nterm, locs) args) =
if nterm = INL nLiteral then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
if isInt tk then
return $ IntLit $ THE $ destInt tk
else if isChar tk then
return $ Char $ THE $ destChar tk
else if isString tk then
return $ StrLit $ THE $ destString tk
else
fail (locs, «Impossible: nLiteral»)
od
| _ => fail (locs, «Impossible: nLiteral»)
else
fail (locs, «Expected a literal non-terminal»)
End
Definition bool2id_def:
bool2id b = Short (if b then "True" else "False")
End
Definition ptree_Bool_def:
ptree_Bool (Lf (_, locs)) =
fail (locs, «Expected a boolean literal non-terminal») ∧
ptree_Bool (Nd (nterm, locs) args) =
if nterm = INL nLiteral then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
if tk = TrueT ∨ tk = FalseT then
return (bool2id (tk = TrueT))
else
fail (locs, «not a boolean literal»)
od
| _ => fail (locs, «Impossible: nLiteral (bool)»)
else
fail (locs, «Expected a boolean literal non-terminal»)
End
Definition ptree_Double_def:
ptree_Double (Lf (_, locs)) =
fail (locs, «Expected a float literal non-terminal») ∧
ptree_Double (Nd (nterm, locs) args) =
if nterm = INL nLiteral then
case args of
[arg] =>
do
lf <- destLf arg;
tk <- option $ destTOK lf;
if isFloat tk then
return $ Lit $ StrLit $ THE $ destFloat tk
else
fail (locs, «not a float literal»)
od
| _ => fail (locs, «Impossible: nLiteral (float)»)
else
fail (locs, «Expected a float literal non-terminal»)
End
Definition nterm_of_def:
nterm_of (Lf (_, locs)) = fail (locs, «nterm_of: Not a parsetree node») ∧
nterm_of (Nd (nterm, _) args) = return nterm
End
(* This code was adapted from the following parser code in the pure repository:
* github.com/CakeML/pure/blob/master/compiler/parsing/cst_to_astScript.sml
*)
val _ = enable_monad "option";
Datatype:
prec = Left | Right | NonAssoc
End
Datatype:
pp_op = po_cons | po_prod | po_or | po_alias
End
Definition tokprec_def:
tokprec op: (num # prec) option = SOME $
case op of
po_cons => (3, Right)
| po_prod => (2, Left)
| po_or => (1, Left)
| po_alias => (0, NonAssoc)
End
Definition tok_action_def:
tok_action (INL stktok, INL inptok) =
do
(stkprec, stka) <- tokprec stktok;
(inpprec, inpa) <- tokprec inptok;
if stka = NonAssoc then SOME Reduce (* only po_alias *)
else if inpprec < stkprec then SOME Reduce
else if stkprec < inpprec then SOME Shift
else if stka ≠ inpa ∨ stka = NonAssoc then NONE
else if stka = Left then SOME Reduce
else SOME Shift
od ∧
tok_action _ = NONE
End
val _ = disable_monad "option";
(* Alias-patterns collapse into a pattern, an operator, and an identifier list.
*)
Definition dest_alias_def:
dest_alias pp =
case pp of
Pp_alias pp ns => SOME (pp, ns)
| _ => NONE
End
Definition tok2ppo:
tok2ppo pt =
do
lf <- destLf pt;
tk <- option $ destTOK lf;
if tk = ColonsT then
return po_cons
else if tk = CommaT then
return po_prod
else if tk = BarT then
return po_or
else
fail (unknown_loc, «ppatOp: impossible»)
od
End
(* tok: pp_op + (ppat + varN list)
* trm: ppat + varN list
*)
Definition ppat_close_def:
ppat_close (Pp_prod pps) = Pp_con NONE pps ∧
ppat_close (Pp_alias pp vs) = FOLDL Pp_as pp vs ∧
ppat_close pps = pps
End
Definition ppat_reduce_def:
ppat_reduce a op b =
case op of
INL po_or =>
(case (a, b) of
(INL x, INL y) => SOME (INL (Pp_or (ppat_close x) y))
| _ => NONE)
| INL po_prod =>
(case (a, b) of
(INL (Pp_prod ps), INL y) => SOME (INL (Pp_prod (ps ++ [y])))
| (INL x, INL y) => SOME (INL (Pp_prod [x; y]))
| _ => NONE)
| INL po_cons =>
(case (a, b) of
(INL x, INL y) =>
SOME (INL (Pp_con (SOME (Short "::")) [ppat_close x; y]))
| _ => NONE)
| INL po_alias =>
(case (a, b) of
(INL x, INR y) => SOME (INL (Pp_alias (ppat_close x) y))
| _ => NONE)
| _ => NONE
End
Definition resolve_precs_def:
resolve_precs xs =
case precparser$precparse <|
rules := (λx. tok_action x);
reduce := (λx op y. ppat_reduce x op y);
(* All x's should be INR's *)
lift := (λx. case x of INR y => y | _ => INR []);
isOp := (λx. ISL x);
mkApp := (λx y. NONE);
|> ([], xs) of
SOME (INL ppat) => return $ ppat_close ppat
| _ => fail (unknown_loc, «resolve_precs»)
End
Definition ptree_AsIds_def:
ptree_AsIds [] = return [] ∧
ptree_AsIds [_] = fail (unknown_loc, «Impossible: ptree_AsIds») ∧
ptree_AsIds (x::y::xs) =
do
expect_tok x AsT;
n <- ptree_Ident y;
ns <- ptree_AsIds xs;
return (n::ns)
od
End
(* Turns a list literal pattern “[x; y; z]” into the
* constructor pattern “x::y::z::[]”.
*)
Definition build_list_ppat_def:
build_list_ppat =
FOLDR (λt p. Pp_con (SOME (Short "::")) [t; p])
(Pp_con (SOME (Short "[]")) [])
End
Definition ptree_FieldsList_def:
(ptree_FieldsList [rbrace] =
do
expect_tok rbrace RbraceT;
return []