forked from jsiek/deduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_desc_parser.py
1390 lines (1251 loc) · 47.8 KB
/
rec_desc_parser.py
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
# The motivation for this recursive-descent version of the parser
# is to provide better error messages. -Jeremy
from abstract_syntax import *
from lark import Lark, Token, logger, exceptions, tree
from error import *
from edit_distance import edit_distance
filename = '???'
def set_filename(fname):
global filename
filename = fname
def get_filename():
global filename
return filename
deduce_directory = '???'
def set_deduce_directory(dir):
global deduce_directory
deduce_directory = dir
def get_deduce_directory():
global deduce_directory
return deduce_directory
mult_operators = {'*', '/', '%', '∘', '.o.'}
add_operators = {'+', '-', '∪', '|', '∩', '&', '⨄', '.+.', '++' }
compare_operators = {'<', '>', '≤', '<=', '>', '≥', '>=', '⊆', '(=', '∈', 'in'}
equal_operators = {'=', '≠', '!='}
iff_operators = {'iff', "<=>", "⇔"}
to_unicode = {'.o.': '∘', '|': '∪', '&': '∩', '.+.': '⨄', '<=': '≤', '>=': '≥',
'(=': '⊆', 'in': '∈', '.0.': '∅', '<=>': '⇔', 'iff': '⇔'}
lark_parser = None
def init_parser():
global lark_parser
lark_file = get_deduce_directory() + "/Deduce.lark"
lark_parser = Lark(open(lark_file, encoding="utf-8").read(),
start='program', parser='lalr',
debug=True, propagate_positions=True)
def parse(program_text, trace = False, error_expected = False):
lexed = lark_parser.lex(program_text)
token_list = []
for token in lexed:
if trace:
print(repr(token))
token_list.append(token)
stmts = []
i = 0
while i < len(token_list):
stmt, i = parse_statement(token_list, i)
stmts.append(stmt)
return stmts
def parse_identifier(token_list, i):
token = token_list[i]
if token.type == 'IDENT':
return (token.value, i + 1)
elif token.value == 'operator':
i = i + 1
return (to_unicode.get(token_list[i].value, token_list[i].value), i + 1)
else:
error(meta_from_tokens(token, token),
'expected an identifier, not ' + token.value)
def meta_from_tokens(start_token, end_token):
meta = Meta()
meta.filename = get_filename()
meta.line = start_token.line
meta.column = start_token.column
meta.start_pos = start_token.start_pos
meta.end_line = end_token.end_line
meta.end_column = end_token.end_column
meta.end_pos = end_token.end_pos
return meta
def parse_term_hi(token_list, i):
token = token_list[i]
if token.type == 'ALL':
i = i + 1
vars, i = parse_var_list(token_list, i)
if token_list[i].type != 'DOT':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `.` after parameters of `all`, not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing\n' \
+ '\tterm ::= "all" var_list "." term')
i = i + 1
body, i = parse_term(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
result = body
for j, var in enumerate(reversed(vars)):
result = All(meta, None, var, (j, len(vars)), result)
return (result, i)
elif token.type == 'AT':
i = i + 1
subject, i = parse_term_hi(token_list, i)
if token_list[i].type != 'LESSTHAN':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected `<` after subject of instantiation (`@`), not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing\n' \
+ '\tterm ::= "@" term "<" type_list ">"')
i = i + 1
type_args, i = parse_type_list(token_list, i)
if token_list[i].type != 'MORETHAN':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected closing `>` after type arguments of instantiation (`@`)' \
+ ' , not\n\t' + token_list[i].value \
+ '\nwhile parsing\n' \
+ '\tterm ::= "@" term "<" type_list ">"')
i = i + 1
meta = meta_from_tokens(token, token_list[i-1])
return (TermInst(meta, None, subject, type_args), i)
elif token.type == 'FALSE':
return (Bool(meta_from_tokens(token_list[i],token_list[i]),
None, False), i + 1)
elif token.type == 'INT' or token.value == '0':
return (intToNat(meta_from_tokens(token,token), int(token.value)), i + 1)
elif token.type == 'PLUS' or token.type == 'MINUS':
i = i + 1
intToken = token_list[i]
if intToken.type == 'INT' or intToken.value == '0':
return (intToDeduceInt(meta_from_tokens(intToken,intToken),
int(intToken.value), token.type),
i + 1)
else:
error(meta_from_tokens(token_list[i],token_list[i]),
'expected an integer not\n\t' + token_list[i].value)
elif token.type == 'IF':
i = i + 1
prem, i = parse_term(token_list, i)
if token_list[i].type != 'THEN':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected keyword `then` after premise of `if` formula, not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing\n' \
+ '\tformula ::= "if" formula "then" formula')
i = i + 1
conc, i = parse_term(token_list, i)
if token_list[i].type == 'ELSE':
i = i + 1
els, i = parse_term(token_list, i)
return (Conditional(meta_from_tokens(token, token_list[i-1]), None,
prem, conc, els), i)
else:
return (IfThen(meta_from_tokens(token, token_list[i-1]),
None, prem, conc), i)
elif token.value == '∅' or token.value == '.0.':
i = i + 1
meta = meta_from_tokens(token, token)
return (Call(meta, None,
Var(meta, None, 'char_fun'),
[Lambda(meta, None, [('_',None)], Bool(meta, None, False))],
False), i)
elif token.type == 'FUN' or token.type == 'Λ':
start = i
i = i + 1
params, i = parse_var_list(token_list, i)
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected `{` after parameters of fun, not\n\t' \
+ token_list[i].value)
i = i + 1
body, i = parse_term(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token, token_list[i-1]),
'expected `}` after body of `fun`, not\n\t' + token_list[i].value)
i = i + 1
return (Lambda(meta_from_tokens(token, token_list[i-1]),
None, params, body), i)
elif token.type == 'GENERIC':
i = i + 1
params, i = parse_ident_list(token_list, i)
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected `{` after parameters of `generic`, not\n\t' \
+ token_list[i].value)
i = i + 1
body, i = parse_term(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token, token_list[i]),
'expected `}` after body of `generic`, not\n\t' \
+ token_list[i].value)
i = i + 1
meta = meta_from_tokens(token, token_list[i-1])
return (Generic(meta, None, params, body), i)
elif token.type == 'LESSTHAN':
i = i + 1
type_params, i = parse_ident_list(token_list, i)
if token_list[i].type != 'MORETHAN':
error(meta_from_tokens(token, token_list[i-1]),
'expected closing `>` after type parameters')
i = i + 1
body, i = parse_term(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
result = body
for j, ty in enumerate(reversed(type_params)):
result = All(meta, None, (ty, TypeType(meta)), (j, len(type_params)), result)
return (result, i)
elif token.type == 'LPAR':
i = i + 1
term, i = parse_term(token_list, i)
if token_list[i].type != 'RPAR':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected closing parentheses, not\n\t' \
+ token_list[i].value)
i = i + 1
return (term, i)
elif token.type == 'HASH':
i = i + 1
term, i = parse_term(token_list, i)
if token_list[i].type != 'HASH':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected closing parentheses, not\n\t' \
+ token_list[i].value)
i = i + 1
meta = meta_from_tokens(token, token_list[i-1])
return (Mark(meta, None, term), i)
elif token.value == '─':
i = i + 1
meta = meta_from_tokens(token,token)
return (Omitted(meta, None), i)
elif token.type == 'NOT':
i = i + 1
subject, i = parse_term_equal(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (IfThen(meta, None, subject, Bool(meta, None, False)), i)
elif token.type == 'QMARK':
i = i + 1
meta = meta_from_tokens(token,token)
return (Hole(meta, None), i)
elif token.type == 'SOME':
i = i + 1
vars, i = parse_var_list(token_list, i)
if token_list[i].type != 'DOT':
error(meta_from_tokens(token, token_list[i]),
'expected `.` after parameters of `some`, not\n\t' \
+ token_list[i].value)
i = i + 1
body, i = parse_term(token_list, i)
return (Some(meta_from_tokens(token, token_list[i-1]),
None, vars, body), i)
elif token.type == 'SWITCH':
start = i
i = i + 1
subject, i = parse_term(token_list, i)
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `{` after subject of switch, not\n\t' \
+ token_list[i].value)
i = i + 1
cases = []
while token_list[i].type == 'CASE':
switch_case, i = parse_switch_case(token_list, i)
cases.append(switch_case)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected `}` after last case of switch, not\n\t' \
+ token_list[i].value)
i = i + 1
return (Switch(meta_from_tokens(token, token_list[i-1]), None,
subject, cases), i)
elif token.type == 'TRUE':
return (Bool(meta_from_tokens(token_list[i],token_list[i]),
None, True), i + 1)
elif token.type == 'LSQB':
i = i + 1
if token_list[i].type == 'RSQB':
return (listToNodeList(meta_from_tokens(token,token), []), i + 1)
lst_terms = []
term, i = parse_term(token_list, i)
lst_terms.append(term)
token = token_list[i]
while token.type == 'COMMA':
i = i + 1
term, i = parse_term(token_list, i)
lst_terms.append(term)
token = token_list[i]
if token.type != 'RSQB':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected closing bracket \']\', not\n\t' + token_list[i].value)
return (listToNodeList(meta_from_tokens(token,token), lst_terms), i + 1)
else:
try:
start = i
name, i = parse_identifier(token_list, i)
meta = meta_from_tokens(token_list[start], token_list[i-1])
var = Var(meta, None, name)
return (var, i)
except Exception as e:
error(meta_from_tokens(token,token_list[i]),
'expected a term\n' + str(e))
def parse_call(token_list, i):
term, i = parse_term_hi(token_list, i)
while i < len(token_list) and token_list[i].type == 'LPAR':
start = i
i = i + 1
args, i = parse_term_list(token_list, i)
if token_list[i].type != 'RPAR':
error(meta_from_tokens(token_list[start], token_list[i-1]),
'expected closing parenthesis `)`')
i = i + 1
term = Call(meta_from_tokens(token_list[start], token_list[i-1]), None,
term, args, False)
return (term, i)
def parse_term_mult(token_list, i):
term, i = parse_call(token_list, i)
while i < len(token_list) and token_list[i].value in mult_operators:
start = i
rator = Var(meta_from_tokens(token_list[i], token_list[i]),
None, to_unicode.get(token_list[i].value,token_list[i].value))
i = i + 1
right, i = parse_term_mult(token_list, i)
term = Call(meta_from_tokens(token_list[start], token_list[i-1]), None,
rator, [term,right], True)
return (term, i)
def parse_term_add(token_list, i):
token = token_list[i]
term, i = parse_term_mult(token_list, i)
while i < len(token_list) and token_list[i].value in add_operators:
rator = Var(meta_from_tokens(token_list[i], token_list[i]),
None, to_unicode.get(token_list[i].value, token_list[i].value))
i = i + 1
right, i = parse_term_add(token_list, i)
term = Call(meta_from_tokens(token, token_list[i-1]), None,
rator, [term,right], True)
return (term, i)
def parse_term_compare(token_list, i):
token = token_list[i]
term, i = parse_term_add(token_list, i)
while i < len(token_list) and token_list[i].value in compare_operators:
rator = Var(meta_from_tokens(token_list[i], token_list[i]),
None, to_unicode.get(token_list[i].value, token_list[i].value))
i = i + 1
right, i = parse_term_compare(token_list, i)
term = Call(meta_from_tokens(token, token_list[i-1]), None,
rator, [term,right], True)
return term, i
def parse_term_equal(token_list, i):
token = token_list[i]
term, i = parse_term_compare(token_list, i)
while i < len(token_list) and token_list[i].value in equal_operators:
meta = meta_from_tokens(token_list[i], token_list[i])
opr = token_list[i].value
eq = Var(meta, None, '=')
i = i + 1
right, i = parse_term_equal(token_list, i)
if opr == '=':
term = Call(meta_from_tokens(token, token_list[i-1]), None,
eq, [term,right], True)
elif opr == '≠' or opr == '!=':
term = IfThen(meta, None,
Call(meta, None, eq, [term,right], True),
Bool(meta, None, False))
return term, i
def parse_term_logic(token_list, i):
token = token_list[i]
term, i = parse_term_equal(token_list, i)
while i < len(token_list) and (token_list[i].type == 'AND'
or token_list[i].type == 'OR'):
opr = token_list[i].type
i = i + 1
right, i = parse_term_logic(token_list, i)
if opr == 'AND':
term = And(meta_from_tokens(token, token_list[i-1]), None,
extract_and(term) + extract_and(right))
elif opr == 'OR':
term = Or(meta_from_tokens(token, token_list[i-1]), None,
extract_or(term) + extract_or(right))
if i < len(token_list) and token_list[i].type == 'COLON':
i = i + 1
typ, i = parse_type(token_list, i)
term = TAnnote(meta_from_tokens(token, token_list[i-1]), None,
term, typ)
return term, i
def parse_term(token_list, i):
token = token_list[i]
if token.type == 'DEFINE':
i = i + 1
name, i = parse_identifier(token_list, i)
if token_list[i].type != 'EQUAL':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected `=` after name in `define`, not\n\t' \
+ token_list[i].value)
i = i + 1
rhs, i = parse_term_logic(token_list, i)
if token_list[i].type != 'SEMICOLON':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected `;` after term of `define`, not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing\n' \
+ '\tterm ::= "define" IDENT "=" term ";" term')
i = i + 1
meta = meta_from_tokens(token, token_list[i-1])
body, i = parse_term(token_list, i)
return (TLet(meta, None, name, rhs, body), i)
else:
term, i = parse_term_logic(token_list, i)
if i < len(token_list) and (token_list[i].value in iff_operators):
i = i + 1
right, i = parse_term_logic(token_list, i)
loc = meta_from_tokens(token, token_list[i-1])
left_right = IfThen(loc, None, term.copy(), right.copy())
right_left = IfThen(loc, None, right.copy(), term.copy())
term = And(loc, None, [left_right, right_left])
if i < len(token_list) and token_list[i].type == 'COLON':
i = i + 1
typ, i = parse_type(token_list, i)
term = TAnnote(meta_from_tokens(token, token_list[i-1]), None,
term, typ)
return term, i
def parse_assumption(token_list, i):
if token_list[i].type == 'COLON':
label = '_'
else:
label,i = parse_identifier(token_list, i)
if token_list[i].type == 'COLON':
i = i + 1
premise, i = parse_term(token_list, i)
return label,premise,i
else:
return label,None,i
proof_keywords = {'apply', 'arbitrary',
'choose', 'conclude', 'conjunct',
'definition',
'enable', 'equations', 'extensionality',
'have', 'induction', 'obtain',
'reflexive', 'rewrite',
'suffices', 'suppose', 'switch', 'symmetric',
'transitive'}
def parse_definition_proof(token_list, i):
token = token_list[i]
i = i + 1
if token_list[i].type == 'LBRACE':
i = i + 1
defs, i = parse_ident_list(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected closing `}`, not\n\t' + token_list[i].value)
i = i + 1
else:
defn, i = parse_identifier(token_list, i)
defs = [defn]
if token_list[i].type == 'AND':
i = i + 1
if token_list[i].type != 'REWRITE':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected `rewrite` after `and` and `definition`, not\n\t' \
+ token_list[i].value)
i = i + 1
eqns, i = parse_proof_list(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (ApplyDefsGoal(meta,
[Var(meta, None, t) for t in defs],
Rewrite(meta, eqns)), i)
elif token_list[i].type == 'IN':
i = i + 1
subject, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (ApplyDefsFact(meta, [Var(meta, None, t) for t in defs],
subject), i)
else:
meta = meta_from_tokens(token, token_list[i-1])
return (ApplyDefs(meta, [Var(meta, None, n) for n in defs]), i)
def parse_recall(token_list, i):
start = i
i = i + 1
facts,i = parse_term_list(token_list, i)
meta = meta_from_tokens(token_list[start], token_list[i-1])
return (PRecall(meta, facts), i)
def parse_proof_hi(token_list, i):
token = token_list[i]
if token.type == 'APPLY':
i = i + 1
imp,i = parse_proof(token_list, i)
if token_list[i].type != 'TO':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `to` after implication part of `apply`, not\n\t' \
+ token_list[i].value)
i = i + 1
arg,i = parse_proof(token_list, i)
return ModusPonens(meta_from_tokens(token, token_list[i-1]), imp, arg), i
elif token.type == 'ARBITRARY':
i = i + 1
vars, i = parse_var_list(token_list, i)
body, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
result = body
for j, var in enumerate(reversed(vars)):
result = AllIntro(meta, var, (j, len(vars)), result)
return (result, i)
elif token.type == 'CASES':
i = i + 1
subject, i = parse_proof(token_list, i)
cases = []
while i < len(token_list) and token_list[i].type == 'CASE':
c, i = parse_case(token_list, i)
cases.append(c)
meta = meta_from_tokens(token, token_list[i-1])
return (Cases(meta, subject, cases), i)
elif token.type == 'CHOOSE':
i = i + 1
witnesses, i = parse_term_list(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
body, i = parse_proof(token_list, i)
return (SomeIntro(meta, witnesses, body), i)
elif token.type == 'CONCLUDE':
i = i + 1
claim, i = parse_term(token_list, i)
if token_list[i].type == 'BY':
i = i + 1
reason, i = parse_proof(token_list, i)
else:
error(meta_from_tokens(token_list[i], token_list[i]),
'expected the keyword `by` after formula of `conclude`, '\
+ 'not\n\t' + token_list[i].value)
return (PAnnot(meta_from_tokens(token, token_list[i-1]),
claim, reason), i)
elif token.type == 'CONJUNCT':
i = i + 1
meta = meta_from_tokens(token_list[i],token_list[i])
index = int(token_list[i].value)
i = i + 1
if token_list[i].type != 'OF':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected keyword `of` after index of `conjunct`, not\n\t' \
+ token_list[i].value)
i = i + 1
subject, i = parse_proof(token_list, i)
meta = meta_from_tokens(token,token_list[i-1])
return (PAndElim(meta, index, subject), i)
elif token.type == 'DEFINE':
i = i + 1
name, i = parse_identifier(token_list, i)
if token_list[i].type != 'EQUAL':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected `=` after name in `define`, not\n\t' + token_list[i].value)
i = i + 1
rhs, i = parse_term(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
body, i = parse_proof(token_list, i)
return (PTLetNew(meta, name, rhs, body), i)
elif token.type == 'DEFINITION':
return parse_definition_proof(token_list, i)
elif token.type == 'DOT':
i = i + 1
return PTrue(meta_from_tokens(token, token)), i
elif token.type == 'ENABLE':
i = i + 1
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `{` after `enable`')
i = i + 1
defs, i = parse_ident_list(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected closing `}` in `enable`')
i = i + 1
body, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (EnableDefs(meta,
[Var(meta, None, x) for x in defs],
body), i)
elif token.type == 'EQUATIONS':
i = i + 1
first, i = parse_equation(token_list, i)
rest, i = parse_equation_list(token_list, i)
eqs = [first]
for (lhs, rhs, reason) in rest:
if lhs == None:
lhs = eqs[-1][1].copy()
eqs.append((lhs, rhs, reason))
result = None
meta = meta_from_tokens(token, token)
for (lhs, rhs, reason) in reversed(eqs):
num_marks = count_marks(lhs) + count_marks(rhs)
if num_marks == 0 and get_default_mark_LHS():
new_lhs = Mark(meta, None, lhs)
else:
new_lhs = lhs
eq_proof = PAnnot(meta, mkEqual(meta, new_lhs, rhs), reason)
if result == None:
result = eq_proof
else:
result = PTransitive(meta, eq_proof, result)
return result, i
elif token.type == 'EXTENSIONALITY':
i = i + 1
body, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (PExtensionality(meta, body), i)
elif token.type == 'RECALL':
return parse_recall(token_list, i)
elif token.type == 'HAVE':
i = i + 1
if token_list[i].type != 'COLON':
label,i = parse_identifier(token_list, i)
else:
label = '_'
if token_list[i].type != 'COLON':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected a colon after label of `have`, not\n\t' \
+ token_list[i].value)
i = i + 1
proved,i = parse_term(token_list, i)
if token_list[i].type == 'BY':
i = i + 1
because,i = parse_proof(token_list, i)
else:
error(meta_from_tokens(token_list[i], token_list[i]),
'expected the keyword `by` after formula of `have`, ' \
+ 'not\n\t' + token_list[i].value)
try:
body,i = parse_proof(token_list, i)
except Exception as e:
raise Exception(str(e) + '\nwhile parsing: ' \
+ '"have" label ":" formula "by" proof proof\n' \
+ ' ^^^^^')
return PLet(meta_from_tokens(token, token_list[i-1]),
label, proved, because, body), i
elif token.type == 'INDUCTION':
i = i + 1
typ, i = parse_type(token_list, i)
cases = []
while token_list[i].type == 'CASE':
try:
c, i = parse_induction_case(token_list, i)
except Exception as e:
raise Exception(str(e) + '\nwhile parsing: ' \
+ '\t"case" pattern "{" proof "}"\n'\
+ '\t ^^^^^')
cases.append(c)
return (Induction(meta_from_tokens(token, token_list[i-1]), typ, cases), i)
elif token.type == 'INJECTIVE':
i = i + 1
constr, i = parse_term(token_list, i)
body, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (PInjective(meta, constr, body), i)
elif token.type == 'LPAR':
i = i + 1
proof, i = parse_proof(token_list, i)
if token_list[i].type != 'RPAR':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected a closing parentheses, not\n\t' \
+ token_list[i].value)
i = i + 1
return proof, i
elif token.type == 'LBRACE':
i = i + 1
proof, i = parse_proof(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected a closing `}`, not\n\t' \
+ token_list[i].value)
i = i + 1
return proof, i
elif token.type == 'OBTAIN':
i = i + 1
witnesses, i = parse_ident_list(token_list, i)
if token_list[i].type != 'WHERE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `where` after variables of `obtain`, not\n\t' \
+ token_list[i].value)
i = i + 1
label, premise, i = parse_assumption(token_list, i)
if token_list[i].type != 'FROM':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `from` after `where` part of `obtain`, not\n\t' \
+ token_list[i].value)
i = i + 1
some, i = parse_proof(token_list, i)
body, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (SomeElim(meta, witnesses, label, premise, some, body), i)
elif token.type == 'QMARK':
i = i + 1
meta = meta_from_tokens(token, token)
return (PHole(meta), i)
elif token.type == 'SORRY':
i = i + 1
meta = meta_from_tokens(token,token)
return (PSorry(meta), i)
elif token.type == 'HELP':
i = i + 1
subject, i = parse_proof(token_list, i)
meta = meta_from_tokens(token,token_list[i-1])
return (PHelpUse(meta, subject), i)
elif token.type == 'REFLEXIVE':
i = i + 1
meta = meta_from_tokens(token, token)
return (PReflexive(meta), i)
elif token.type == 'REWRITE':
i = i + 1
proofs, i = parse_proof_list(token_list, i)
if token_list[i].type == 'IN':
i = i + 1
subject, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
return (RewriteFact(meta, subject, proofs), i)
else:
meta = meta_from_tokens(token, token_list[i-1])
return (Rewrite(meta, proofs), i)
elif token.type == 'SUFFICES':
i = i + 1
formula, i = parse_term(token_list, i)
if token_list[i].type != 'BY':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected the keyword `by` after formula of `suffices`, not\n\t' \
+ token_list[i].value)
i = i + 1
proof, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token_list[i-1])
try:
body, i = parse_proof(token_list, i)
except Exception as e:
raise Exception(str(e) + '\nwhile parsing: ' \
+ '"suffices" formula "by" proof proof\n' \
+ ' ^^^^^')
return (Suffices(meta, formula, proof, body), i)
elif token.type == 'SUPPOSE' or token.type == 'ASSUME':
start = i
i = i + 1
try:
label,premise,i = parse_assumption(token_list, i)
except Exception as e:
error(meta_from_tokens(token, token_list[i]),
'expected an assumption:\n\t"assume" label ":" formula\n' \
+ str(e))
meta = meta_from_tokens(token,token_list[i-1])
body,i = parse_proof(token_list, i)
return ImpIntro(meta, label, premise, body), i
elif token.type == 'SWITCH':
i = i + 1
subject, i = parse_term(token_list, i)
if token_list[i].type == 'FOR':
i = i + 1
defs, i = parse_ident_list(token_list, i)
else:
defs = []
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `{` after subject of `switch`')
i = i + 1
cases = []
while token_list[i].type == 'CASE':
c, i = parse_proof_switch_case(token_list, i)
cases.append(c)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected closing `}` after cases of `switch`')
i = i + 1
meta = meta_from_tokens(token, token_list[i-1])
if len(defs) == 0:
return (SwitchProof(meta, subject, cases), i)
else:
return (ApplyDefsGoal(meta, [Var(meta, None, t) for t in defs],
SwitchProof(meta, subject, cases)), i)
elif token.type == 'SYMMETRIC':
i = i + 1
eq, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token)
return (PSymmetric(meta, eq), i)
elif token.type == 'TRANSITIVE':
i = i + 1
eq1, i = parse_proof(token_list, i)
eq2, i = parse_proof(token_list, i)
meta = meta_from_tokens(token, token)
return (PTransitive(meta, eq1, eq2), i)
else:
for kw in proof_keywords:
if edit_distance(token.value, kw) <= 2:
error(meta_from_tokens(token, token),
'did you mean `' + kw \
+ '` instead of `' + token_list[i].value + '`?')
try:
name, i = parse_identifier(token_list, i)
except Exception as e:
error(meta_from_tokens(token, token_list[i]),
'expected a proof, not `' + token_list[i].value + '`')
return (PVar(meta_from_tokens(token, token), name), i)
def parse_proof_list(token_list, i):
proof_list = []
proof, i = parse_proof(token_list, i)
proof_list.append(proof)
while i < len(token_list) and token_list[i].value == '|':
i = i + 1
proof, i = parse_proof(token_list, i)
proof_list.append(proof)
return (proof_list, i)
def parse_case(token_list, i):
start = i
i = i + 1
label,premise, i = parse_assumption(token_list, i)
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected a `{` after assumption of `case`, not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing:\n\t"case" label ":" formula "{" proof "}"')
i = i + 1
body, i = parse_proof(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected a `}` after body of `case`, not\n\t' + token_list[i].value)
i = i + 1
return ((label,premise,body), i)
def parse_proof_switch_case(token_list, i):
start = i
i = i + 1
pat, i = parse_pattern(token_list, i)
if token_list[i].type == 'SUPPOSE' or token_list[i].type == 'ASSUME':
i = i + 1
assumptions = []
label,asm, i = parse_assumption(token_list, i)
assumptions.append((label,asm))
while token_list[i].type == 'COMMA':
i = i + 1
label,asm, i = parse_assumption(token_list, i)
assumptions.append((label,asm))
else:
assumptions = []
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected a `{` after assumption of `case`, not\n\t' \
+ token_list[i].value \
+ '\nwhile parsing one of the following\n' \
+ '\tswitch_proof_case ::= "case" pattern "{" proof "}"\n' \
+ '\tswitch_proof_case ::= "case" pattern "assume" assumption_list "{" proof "}"')
i = i + 1
body, i = parse_proof(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected a `}` after body of case, not\n\t' + token_list[i].value)
i = i + 1
meta = meta_from_tokens(token_list[start],token_list[i-1])
return (SwitchProofCase(meta, pat, assumptions, body), i)
def parse_proof_med(token_list, i):
start = i
proof, i = parse_proof_hi(token_list, i)
while i < len(token_list) and token_list[i].type == 'LESSTHAN':
i = i + 1
type_list, i = parse_type_list(token_list, i)
if token_list[i].type != 'MORETHAN':
error(meta_from_tokens(token_list[start],token_list[i]),
'expected a closing `>`, not\n\t' + token_list[i].value + '\n'\
+ 'while trying to parse type arguments for instantiation of an `all` formula:\n\t'\
+ 'proof ::= proof "<" type_list ">"')
i = i + 1
meta = meta_from_tokens(token_list[start], token_list[i-1])
for j, ty in enumerate(type_list):
proof = AllElimTypes(meta, proof, ty, (j, len(type_list)))
while i < len(token_list) and token_list[i].type == 'LSQB':
i = i + 1
term_list, i = parse_term_list(token_list, i)
if token_list[i].type != 'RSQB':
error(meta_from_tokens(token_list[i],token_list[i]),
'expected a closing `]`, not\n\t' + token_list[i].value)
i = i + 1
meta = meta_from_tokens(token_list[start], token_list[i-1])
for j, term in enumerate(term_list):
proof = AllElim(meta, proof, term, (j, len(term_list)))
return (proof, i)
def parse_proof(token_list, i):
start = i
proof, i = parse_proof_med(token_list, i)
while token_list[i].type == 'COMMA':
i = i + 1
right, i = parse_proof(token_list, i)
proof = PTuple(meta_from_tokens(token_list[start], token_list[i-1]),
[proof, right])
return proof, i
def parse_induction_case(token_list, i):
start = i
i = i + 1
pat, i = parse_pattern(token_list, i)
ind_hyps = []
if token_list[i].type == 'SUPPOSE' or token_list[i].type == 'ASSUME':
i = i + 1
label,ih, i = parse_assumption(token_list, i)
ind_hyps.append((label,ih))
while token_list[i].type == 'COMMA':
i = i + 1
label,ih, i = parse_assumption(token_list, i)
ind_hyps.append((label,ih))
if token_list[i].type != 'LBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `{` after pattern of `case`, not\n\t' \
+ token_list[i].value)
i = i + 1
body, i = parse_proof(token_list, i)
if token_list[i].type != 'RBRACE':
error(meta_from_tokens(token_list[i], token_list[i]),
'expected `}` after body of induction case, not\n\t' \
+ token_list[i].value)
i = i + 1
return (IndCase(meta_from_tokens(token_list[start], token_list[i-1]),
pat, ind_hyps, body), i)
def parse_equation(token_list, i):
lhs, i = parse_term_compare(token_list, i)
if token_list[i].type != 'EQUAL':