/
builtin_exprs.cpp
1376 lines (1278 loc) · 52.8 KB
/
builtin_exprs.cpp
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
/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <string>
#include "util/sexpr/option_declarations.h"
#include "util/sstream.h"
#include "kernel/abstract.h"
#include "kernel/instantiate.h"
#include "library/annotation.h"
#include "library/placeholder.h"
#include "library/explicit.h"
#include "library/aliases.h"
#include "library/scoped_ext.h"
#include "library/typed_expr.h"
#include "library/choice.h"
#include "library/constants.h"
#include "library/quote.h"
#include "library/string.h"
#include "library/trace.h"
#include "library/kernel_serializer.h"
#include "library/tactic/elaborate.h"
#include "library/tactic/smt/hinst_lemmas.h"
#include "library/equations_compiler/equations.h"
#include "frontends/lean/builtin_exprs.h"
#include "frontends/lean/decl_cmds.h"
#include "frontends/lean/token_table.h"
#include "frontends/lean/calc.h"
#include "frontends/lean/parser.h"
#include "frontends/lean/util.h"
#include "frontends/lean/tokens.h"
#include "frontends/lean/match_expr.h"
#include "frontends/lean/decl_util.h"
#include "frontends/lean/brackets.h"
#include "frontends/lean/tactic_notation.h"
#include "frontends/lean/elaborator.h"
#ifndef LEAN_DEFAULT_PARSER_CHECKPOINT_HAVE
#define LEAN_DEFAULT_PARSER_CHECKPOINT_HAVE true
#endif
namespace lean {
static name * g_parser_checkpoint_have = nullptr;
bool get_parser_checkpoint_have(options const & opts) {
return opts.get_bool(*g_parser_checkpoint_have, LEAN_DEFAULT_PARSER_CHECKPOINT_HAVE);
}
using namespace notation; // NOLINT
expr mk_annotation_with_pos(parser & p, name const & a, expr const & e, pos_info const & pos) {
expr r = mk_annotation(a, e);
r.set_tag(nulltag); // mk_annotation copies e's tag
return p.save_pos(r, pos);
}
static name * g_no_universe_annotation = nullptr;
bool is_sort_wo_universe(expr const & e) {
return is_annotation(e, *g_no_universe_annotation);
}
expr mk_sort_wo_universe(parser & p, ast_data & data, bool is_type) {
level l = is_type ? mk_level_one() : mk_level_zero();
expr r = p.save_pos((scoped_expr_caching(false), mk_sort(l)), data.m_start);
p.finalize_ast(data.m_id, r);
r = mk_annotation_with_pos(p, *g_no_universe_annotation, r, data.m_start);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_Type(parser & p, unsigned, expr const *, pos_info const & pos) {
auto& data = p.new_ast("Type", pos);
if (p.curr_is_token(get_llevel_curly_tk())) {
p.next();
ast_id id; level l;
std::tie(id, l) = p.parse_level();
p.check_token_next(get_rcurly_tk(), "invalid Type expression, '}' expected");
l = mk_succ(l);
expr r = p.save_pos((scoped_expr_caching(false), mk_sort(l)), pos);
p.finalize_ast(data.push(id).m_id, r);
return r;
} else {
return mk_sort_wo_universe(p, data.push(0), true);
}
}
static expr parse_Sort(parser & p, unsigned, expr const *, pos_info const & pos) {
auto& data = p.new_ast("Sort", pos);
if (p.curr_is_token(get_llevel_curly_tk())) {
p.next();
ast_id id; level l;
std::tie(id, l) = p.parse_level();
p.check_token_next(get_rcurly_tk(), "invalid Sort expression, '}' expected");
expr r = p.save_pos((scoped_expr_caching(false), mk_sort(l)), pos);
p.finalize_ast(data.push(id).m_id, r);
return r;
} else {
return mk_sort_wo_universe(p, data.push(0), false);
}
}
static expr parse_Type_star(parser & p, unsigned, expr const *, pos_info const & pos) {
level l = mk_succ(mk_level_placeholder());
expr r = p.save_pos((scoped_expr_caching(false), mk_sort(l)), pos);
p.finalize_ast(p.new_ast("Type*", pos).m_id, r);
return r;
}
static expr parse_Sort_star(parser & p, unsigned, expr const *, pos_info const & pos) {
level l = mk_level_placeholder();
expr r = p.save_pos((scoped_expr_caching(false), mk_sort(l)), pos);
p.finalize_ast(p.new_ast("Sort*", pos).m_id, r);
return r;
}
static name * g_let_match_name = nullptr;
static pair<ast_id, expr> parse_let(parser & p, pos_info const & pos, ast_data & group, bool in_do_block);
static expr parse_do(parser & p, ast_data & group, bool has_braces);
static pair<ast_id, expr> parse_let_body(parser & p, pos_info const & pos, ast_id last, ast_data & group, bool in_do_block) {
if (in_do_block) {
if (p.curr_is_token(get_in_tk())) {
p.next();
ast_id args = p.new_ast("args", pos).push(last).m_id;
expr e = p.parse_expr();
ast_id let = p.new_ast(get_let_tk(), pos).push(args).push(p.get_id(e)).m_id;
group.push(p.new_ast("eval", pos).push(let).m_id);
return {0, e};
} else {
p.check_token_next(get_comma_tk(), "invalid 'do' block 'let' declaration, ',' or 'in' expected");
group.push(p.new_ast(get_let_tk(), pos).push(last).m_id);
if (p.curr_is_token(get_let_tk())) {
p.next();
return parse_let(p, pos, group, in_do_block);
} else {
return {0, parse_do(p, group, false)};
}
}
} else {
group.push(last);
if (p.curr_is_token(get_comma_tk())) {
p.next();
return parse_let(p, pos, group, in_do_block);
} else if (p.curr_is_token(get_in_tk())) {
p.next();
} else {
p.maybe_throw_error({"invalid let declaration, 'in' or ',' expected", p.pos()});
}
expr e = p.parse_expr();
return {p.new_ast(get_let_tk(), pos).push(group.m_id).push(p.get_id(e)).m_id, e};
}
}
// Distribute mk_typed_expr over choice expression.
// see issue #768
static expr mk_typed_expr_distrib_choice(parser & p, expr const & type, expr const & value, pos_info const & pos) {
if (is_choice(value)) {
buffer<expr> new_choices;
for (unsigned i = 0; i < get_num_choices(value); i++) {
new_choices.push_back(mk_typed_expr_distrib_choice(p, type, get_choice(value, i), pos));
}
return p.save_pos(mk_choice(new_choices.size(), new_choices.data()), pos);
} else {
return p.save_pos(mk_typed_expr(type, value), pos);
}
}
static pair<ast_id, expr> parse_let(parser & p, pos_info const & pos, ast_data & group, bool in_do_block) {
parser::local_scope scope1(p);
ast_id nota;
if (!in_do_block && (nota = p.parse_local_notation_decl())) {
return parse_let_body(p, pos, nota, group, in_do_block);
} else if (p.curr_is_identifier()) {
auto id_pos = p.pos();
ast_id id_ast; name id;
std::tie(id_ast, id) = p.check_atomic_id_next("invalid let declaration, atomic identifier expected");
auto& var = p.new_ast("var", id_pos).push(id_ast);
expr type;
expr value;
if (p.curr_is_token(get_assign_tk())) {
p.next();
type = p.save_pos(mk_expr_placeholder(), id_pos);
value = p.parse_expr();
var.push(0).push(0).push(p.get_id(value));
} else if (p.curr_is_token(get_colon_tk())) {
p.next();
type = p.parse_expr();
p.check_token_next(get_assign_tk(), "invalid declaration, ':=' expected");
value = p.parse_expr();
var.push(0).push(p.get_id(type)).push(p.get_id(value));
} else {
parser::local_scope scope2(p);
buffer<expr> ps;
unsigned rbp = 0;
auto& bis = p.new_ast("binders", p.pos());
auto lenv = p.parse_binders(&bis, ps, rbp);
var.push(bis.m_id);
if (p.curr_is_token(get_colon_tk())) {
p.next();
type = p.parse_scoped_expr(ps, lenv);
var.push(p.get_id(type));
type = Pi(ps, type, p);
} else {
type = p.save_pos(mk_expr_placeholder(), id_pos);
var.push(0);
}
p.check_token_next(get_assign_tk(), "invalid let declaration, ':=' expected");
value = p.parse_scoped_expr(ps, lenv);
var.push(p.get_id(value));
value = Fun(ps, value, p);
}
expr x = p.save_pos(mk_local(id, type), id_pos);
p.add_local_expr(id, x);
auto r = parse_let_body(p, pos, var.m_id, group, in_do_block);
r.second = p.save_pos(mk_let(id, type, value, abstract_local(r.second, x)), pos);
return r;
} else {
buffer<expr> new_locals;
auto& var = p.new_ast("pat", p.pos());
expr lhs = p.parse_pattern(new_locals);
p.check_token_next(get_assign_tk(), "invalid let declaration, ':=' expected");
expr value = p.parse_expr();
var.push(p.get_id(lhs)).push(p.get_id(value));
for (expr const & l : new_locals)
p.add_local(l);
ast_id ast; expr body;
std::tie(ast, body) = parse_let_body(p, pos, var.m_id, group, in_do_block);
match_definition_scope match_scope(p.env());
expr fn = p.save_pos(mk_local(p.next_name(), *g_let_match_name, mk_expr_placeholder(), mk_rec_info(true)), pos);
expr eqn = Fun(fn, Fun(new_locals, p.save_pos(mk_equation(p.rec_save_pos(mk_app(fn, lhs), pos), body), pos), p), p);
equations_header h = mk_match_header(match_scope.get_name(), match_scope.get_actual_name());
expr eqns = p.save_pos(mk_equations(h, 1, &eqn), pos);
return {ast, p.save_pos(mk_app(eqns, value), pos)};
}
}
static expr parse_let_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
bool in_do_block = false;
auto& group = p.new_ast("args", p.pos());
ast_id id; expr r;
std::tie(id, r) = parse_let(p, pos, group, in_do_block);
p.finalize_ast(id, r);
return r;
}
static name * g_do_match_name = nullptr;
static std::tuple<optional<expr>, expr, expr, optional<expr>>
parse_do_action(parser & p, ast_data & parent, buffer<expr> & new_locals) {
auto lhs_pos = p.pos();
optional<expr> lhs = some(p.parse_pattern_or_expr());
ast_id lhs_id = p.get_id(*lhs);
expr type, curr;
optional<expr> else_case;
if (p.curr_is_token(get_colon_tk())) {
p.next();
type = p.parse_expr();
if (is_placeholder(*lhs)) {
lhs = mk_local("_x", type);
}
if (!is_local(*lhs)) {
p.maybe_throw_error({"invalid 'do' block, unexpected ':' the left hand side is a pattern", lhs_pos});
lhs = mk_local("_x", type);
}
lhs = p.save_pos(mk_local(mlocal_pp_name(*lhs), type), lhs_pos);
new_locals.clear();
new_locals.push_back(*lhs);
p.check_token_next(get_larrow_tk(), "invalid 'do' block, '←' expected");
curr = p.parse_expr();
parent.push(p.new_ast(get_larrow_tk(), lhs_pos)
.push(lhs_id).push(p.get_id(type)).push(p.get_id(curr)).push(0).m_id);
} else if (p.curr_is_token(get_larrow_tk())) {
p.next();
type = p.save_pos(mk_expr_placeholder(), lhs_pos);
bool skip_main_fn = false;
lhs = p.patexpr_to_pattern(*lhs, skip_main_fn, new_locals);
curr = p.parse_expr();
ast_id else_id = 0;
if (p.curr_is_token(get_bar_tk())) {
p.next();
else_case = p.parse_expr();
else_id = p.get_id(*else_case);
}
parent.push(p.new_ast(get_larrow_tk(), lhs_pos)
.push(lhs_id).push(0).push(p.get_id(curr)).push(else_id).m_id);
} else {
curr = p.patexpr_to_expr(*lhs);
type = p.save_pos(mk_expr_placeholder(), lhs_pos);
lhs = none_expr();
parent.push(p.new_ast("eval", lhs_pos).push(lhs_id).m_id);
}
return std::make_tuple(lhs, type, curr, else_case);
}
static expr mk_bind_fn(parser & p) {
return mk_no_info(p.id_to_expr("bind", p.new_ast("bind", {}), /* resolve_only */ true));
}
static name * g_do_failure_eq = nullptr;
/* Mark for automatic failure equation when pattern matching in do-expressions. */
expr mark_do_failure_eq(expr const & e) {
return mk_annotation(*g_do_failure_eq, e);
}
bool is_do_failure_eq(expr const & e) {
auto it = e;
while (is_lambda(it))
it = binding_body(it);
if (!is_equation(it)) return false;
return is_annotation(equation_rhs(it), *g_do_failure_eq);
}
static expr parse_do(parser & p, ast_data & group, bool has_braces) {
parser::local_scope scope(p);
buffer<expr> es;
buffer<pos_info> ps;
buffer<optional<expr>> lhss;
buffer<optional<expr>> else_cases;
buffer<list<expr>> lhss_locals;
while (true) {
if (p.curr_is_token(get_let_tk())) {
auto pos = p.pos();
p.next();
bool in_do_block = true;
es.push_back(parse_let(p, pos, group, in_do_block).second);
break;
} else {
auto lhs_pos = p.pos();
ps.push_back(lhs_pos);
buffer<expr> new_locals;
optional<expr> lhs, else_case;
expr type, curr;
std::tie(lhs, type, curr, else_case) = parse_do_action(p, group, new_locals);
es.push_back(curr);
if (p.curr_is_token(get_comma_tk())) {
p.next();
for (expr const & l : new_locals)
p.add_local(l);
if (lhs && !is_local(*lhs)) {
// if lhs is a pattern, we need to save the locals to create the match
lhss_locals.push_back(to_list(new_locals));
} else {
lhss_locals.push_back(list<expr>());
}
lhss.push_back(lhs);
else_cases.push_back(else_case);
} else {
if (lhs) {
p.maybe_throw_error({"the last statement in a 'do' block must be an expression", lhs_pos});
}
break;
}
}
}
if (has_braces) {
p.check_token_next(get_rcurly_tk(), "invalid 'do' block, '}' expected");
}
lean_assert(!es.empty());
lean_assert(es.size() == lhss.size() + 1);
if (es.size() == 1)
return es[0];
unsigned i = es.size();
--i;
expr r = es[i];
while (i > 0) {
--i;
if (auto lhs = lhss[i]) {
if (is_local(*lhs)) {
r = p.rec_save_pos(mk_app(p.save_pos(mk_bind_fn(p), ps[i]), es[i], Fun(*lhs, r, p)), ps[i]);
} else {
// must introduce a "fake" match
auto pos = ps[i];
match_definition_scope match_scope(p.env());
expr fn = p.save_pos(mk_local(p.next_name(), *g_do_match_name, mk_expr_placeholder(), mk_rec_info(true)), pos);
buffer<expr> locals;
to_buffer(lhss_locals[i], locals);
buffer<expr> eqs;
eqs.push_back(Fun(fn, Fun(locals, p.save_pos(mk_equation(p.rec_save_pos(mk_app(fn, *lhs), pos), r), pos), p), p));
expr else_case;
bool ignore_if_unused;
if (optional<expr> r = else_cases[i]) {
else_case = *r;
ignore_if_unused = false;
} else {
else_case = p.save_pos(mark_do_failure_eq(p.save_pos(mk_constant(get_match_failed_name()), pos)), pos);
ignore_if_unused = true;
}
// add case
// _ := else_case
expr x = mk_local(p.next_name(), "_x", mk_expr_placeholder(), binder_info());
expr else_eq = Fun(fn, Fun(x, p.save_pos(mk_equation(p.rec_save_pos(mk_app(fn, x), pos),
else_case,
ignore_if_unused),
pos), p), p);
eqs.push_back(else_eq);
equations_header h = mk_match_header(match_scope.get_name(), match_scope.get_actual_name());
expr eqns = p.save_pos(mk_equations(h, eqs.size(), eqs.data()), pos);
expr local = mk_local("_p", mk_expr_placeholder());
expr match = p.mk_app(eqns, local, pos);
r = p.rec_save_pos(mk_app(p.save_pos(mk_bind_fn(p), ps[i]),
es[i],
p.save_pos(Fun(local, match, p), pos)),
pos);
}
} else {
r = p.rec_save_pos(mk_app(p.save_pos(mk_bind_fn(p), ps[i]),
es[i],
p.save_pos(mk_lambda("_x", mk_expr_placeholder(), r), p.pos_of(r))),
ps[i]);
}
}
return r;
}
static expr parse_do_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
bool has_braces = false;
auto& data = p.new_ast("do", pos);
if (p.curr_is_token(get_lcurly_tk())) {
has_braces = true;
data.m_value = "{";
p.next();
}
expr r = parse_do(p, data, has_braces);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_unit(parser & p, unsigned, expr const *, pos_info const & pos) {
auto e = p.save_pos(mk_constant(get_unit_star_name()), pos);
p.finalize_ast(p.new_ast("()", pos).m_id, e);
return e;
}
static pair<ast_id, expr> parse_proof(parser & p) {
expr e;
if (p.curr_is_token(get_from_tk())) {
auto& data = p.new_ast(get_from_tk(), p.pos());
// parse: 'from' expr
p.next();
e = p.parse_expr();
data.push(p.get_id(e));
return {data.m_id, e};
} else if (p.curr_is_token(get_begin_tk())) {
auto pos = p.pos();
e = parse_begin_end_expr(p, pos);
} else if (p.curr_is_token(get_lcurly_tk())) {
auto pos = p.pos();
e = parse_curly_begin_end_expr(p, pos);
} else if (p.curr_is_token(get_by_tk())) {
auto pos = p.pos();
e = parse_by(p, 0, nullptr, pos);
} else {
e = p.parser_error_or_expr({"invalid expression, 'by', 'begin', '{', or 'from' expected", p.pos()});
}
return {p.get_id(e), e};
}
static expr parse_have(parser & p, unsigned, expr const *, pos_info const & pos) {
name id;
expr prop;
auto& data = p.new_ast("have", pos);
if (p.curr_is_identifier()) {
id = p.get_name_val();
auto& id_ast = p.new_ast("ident", p.pos(), id);
p.next();
if (p.curr_is_token(get_colon_tk())) {
p.next();
prop = p.parse_expr();
data.push(id_ast.m_id).push(p.get_id(prop));
} else {
expr left = p.id_to_expr(id, id_ast);
id = get_this_tk();
unsigned rbp = 0;
while (rbp < p.curr_lbp()) {
left = p.parse_led(left);
}
prop = left;
data.push(0).push(p.get_id(prop));
}
} else {
id = get_this_tk();
prop = p.parse_expr();
data.push(0).push(p.get_id(prop));
}
expr proof;
if (p.curr_is_token(get_assign_tk())) {
auto& ast = p.new_ast(get_assign_tk(), p.pos());
p.next();
proof = p.parse_expr();
data.push(ast.push(p.get_id(proof)).m_id);
} else {
p.check_token_next(get_comma_tk(), "invalid 'have' declaration, ',' expected");
ast_id id;
std::tie(id, proof) = parse_proof(p);
data.push(id);
}
p.check_token_next(get_comma_tk(), "invalid 'have' declaration, ',' expected");
parser::local_scope scope(p);
expr l = p.save_pos(mk_local(id, prop), pos);
p.add_local(l);
expr body = p.parse_expr();
data.push(p.get_id(body));
body = abstract(body, l);
if (get_parser_checkpoint_have(p.get_options()))
body = mk_checkpoint_annotation(body);
expr r = p.save_pos(mk_have_annotation(p.save_pos(mk_lambda(id, prop, body), pos)), pos);
r = p.mk_app(r, proof, pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_show(parser & p, unsigned, expr const *, pos_info const & pos) {
expr prop = p.parse_expr();
p.check_token_next(get_comma_tk(), "invalid 'show' declaration, ',' expected");
ast_id id; expr proof;
std::tie(id, proof) = parse_proof(p);
expr b = p.save_pos(mk_lambda(get_this_tk(), prop, Var(0)), pos);
expr r = p.mk_app(b, proof, pos);
r = p.save_pos(mk_show_annotation(r), pos);
p.finalize_ast(p.new_ast("show", pos).push(p.get_id(prop)).push(id).m_id,
r);
return r;
}
static expr parse_suffices(parser & p, unsigned, expr const *, pos_info const & pos) {
auto prop_pos = p.pos();
name id;
expr from;
auto& data = p.new_ast("suffices", pos);
if (p.curr_is_identifier()) {
id = p.get_name_val();
auto& id_ast = p.new_ast("ident", prop_pos, id);
p.next();
if (p.curr_is_token(get_colon_tk())) {
p.next();
from = p.parse_expr();
data.push(id_ast.m_id).push(p.get_id(from));
} else {
expr left = p.id_to_expr(id, id_ast);
id = get_this_tk();
unsigned rbp = 0;
while (rbp < p.curr_lbp()) {
left = p.parse_led(left);
}
from = left;
data.push(0).push(p.get_id(from));
}
} else {
id = get_this_tk();
from = p.parse_expr();
data.push(0).push(p.get_id(from));
}
expr local = p.save_pos(mk_local(id, from), prop_pos);
p.check_token_next(get_comma_tk(), "invalid 'suffices' declaration, ',' expected");
expr body;
{
parser::local_scope scope(p);
p.add_local(local);
ast_id id;
std::tie(id, body) = parse_proof(p);
data.push(id);
}
expr proof = p.save_pos(Fun(local, body, p), pos);
p.check_token_next(get_comma_tk(), "invalid 'suffices' declaration, ',' expected");
expr rest = p.parse_expr();
data.push(p.get_id(rest));
expr r = p.mk_app(proof, rest, pos);
r = p.save_pos(mk_suffices_annotation(r), pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr * g_not = nullptr;
static unsigned g_then_else_prec = 0;
static expr parse_ite(parser & p, ast_data & parent, expr const & c, pos_info const & pos) {
if (!p.env().find(get_ite_name()))
throw parser_error("invalid use of 'if-then-else' expression, environment does not contain 'ite' definition", pos);
p.check_token_next(get_then_tk(), "invalid 'if-then-else' expression, 'then' expected");
expr t = p.parse_expr(g_then_else_prec);
p.check_token_next(get_else_tk(), "invalid 'if-then-else' expression, 'else' expected");
expr e = p.parse_expr(g_then_else_prec);
parent.push(p.get_id(t)).push(p.get_id(e));
return p.save_pos(mk_app(mk_constant(get_ite_name()), c, t, e), pos);
}
static expr parse_dite(parser & p, ast_data & parent, name const & H_name, expr const & c, pos_info const & pos) {
p.check_token_next(get_then_tk(), "invalid 'if-then-else' expression, 'then' expected");
expr t, e;
{
parser::local_scope scope(p);
expr H = mk_local(H_name, c);
p.add_local(H);
auto pos = p.pos();
t = p.parse_expr(g_then_else_prec);
parent.push(p.get_id(t));
t = p.save_pos(Fun(H, t, p), pos);
}
p.check_token_next(get_else_tk(), "invalid 'if-then-else' expression, 'else' expected");
{
parser::local_scope scope(p);
expr H = mk_local(H_name, mk_app(*g_not, c));
p.add_local(H);
auto pos = p.pos();
e = p.parse_expr(g_then_else_prec);
parent.push(p.get_id(e));
e = p.save_pos(Fun(H, e, p), pos);
}
return p.save_pos(mk_app(p.save_pos(mk_constant(get_dite_name()), pos), c, t, e), pos);
}
static expr parse_if_then_else(parser & p, unsigned, expr const *, pos_info const & pos) {
ast_id id; optional<name> hyp; expr cond;
std::tie(id, hyp, cond) = p.parse_qualified_expr();
auto& data = p.new_ast("if", pos).push(id).push(p.get_id(cond));
auto r = hyp ? parse_dite(p, data, *hyp, cond, pos) : parse_ite(p, data, cond, pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_calc_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
return parse_calc(p, pos);
}
static expr parse_explicit_core(parser & p, pos_info const & pos, bool partial) {
auto sym = partial ? "@@" : "@";
if (!p.curr_is_identifier())
return p.parser_error_or_expr({sstream() << "invalid '" << sym << "', identifier expected", p.pos()});
expr fn = p.parse_id(/* allow_field_notation */ false);
if (is_choice(fn)) {
sstream s;
s << "invalid '" << sym << "', function is overloaded, use fully qualified names (overloads: ";
for (unsigned i = 0; i < get_num_choices(fn); i++) {
if (i > 0) s << ", ";
expr const & c = get_choice(fn, i);
if (is_constant(c))
s << const_name(c);
else if (is_local(c))
s << mlocal_pp_name(c);
else
s << "[other]";
}
s << ")";
return p.parser_error_or_expr({s, pos});
} else if (!is_as_atomic(fn) && !is_constant(fn) && !is_local(fn)) {
return p.parser_error_or_expr({sstream() << "invalid '" << sym << "', function must be a constant or variable", pos});
}
expr r;
if (partial)
r = p.save_pos(mk_partial_explicit(fn), pos);
else
r = p.save_pos(mk_explicit(fn), pos);
p.finalize_ast(p.new_ast(sym, pos).push(p.get_id(fn)).m_id, r);
return r;
}
static expr parse_explicit_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
return parse_explicit_core(p, pos, false);
}
static expr parse_partial_explicit_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
return parse_explicit_core(p, pos, true);
}
static expr parse_sorry(parser & p, unsigned, expr const *, pos_info const & pos) {
auto r = p.mk_sorry(pos);
p.finalize_ast(p.new_ast("sorry", pos).m_id, r);
return r;
}
static expr parse_pattern(parser & p, unsigned, expr const * args, pos_info const & pos) {
auto r = p.save_pos(mk_pattern_hint(args[0]), pos);
p.finalize_ast(p.new_ast("(:", pos).push(p.get_id(args[0])).m_id, r);
return r;
}
static expr parse_lazy_quoted_pexpr(parser & p, unsigned, expr const *, pos_info const & pos) {
if (p.in_quote())
return p.parser_error_or_expr({"invalid nested quoted expression", pos});
parser::quote_scope scope1(p, true);
restore_decl_meta_scope scope2;
expr e = p.parse_expr();
auto& data = p.new_ast("```()", pos);
if (p.curr_is_token(get_colon_tk())) {
p.next();
expr t = p.parse_expr();
ast_id id = p.new_ast(get_colon_tk(), p.pos_of(e)).push(p.get_id(e)).push(p.get_id(t)).m_id;
e = mk_typed_expr_distrib_choice(p, t, e, pos);
p.finalize_ast(id, e);
data.push(id);
} else {
data.push(p.get_id(e));
}
p.check_token_next(get_rparen_tk(), "invalid quoted expression, `)` expected");
auto r = p.save_pos(mk_pexpr_quote_and_substs(e, /* is_strict */ false), pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_quoted_pexpr(parser & p, unsigned, expr const *, pos_info const & pos) {
if (p.in_quote())
return p.parser_error_or_expr({"invalid nested quoted expression", pos});
parser::quote_scope scope1(p, true, id_behavior::ErrorIfUndef);
restore_decl_meta_scope scope2;
expr e = p.parse_expr();
auto& data = p.new_ast("``()", pos);
if (p.curr_is_token(get_colon_tk())) {
p.next();
expr t = p.parse_expr();
ast_id id = p.new_ast(get_colon_tk(), p.pos_of(e)).push(p.get_id(e)).push(p.get_id(t)).m_id;
e = mk_typed_expr_distrib_choice(p, t, e, pos);
p.finalize_ast(id, e);
data.push(id);
} else {
data.push(p.get_id(e));
}
p.check_token_next(get_rparen_tk(), "invalid quoted expression, `)` expected");
auto r = p.save_pos(mk_pexpr_quote_and_substs(e, /* is_strict */ true), pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_quoted_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
if (p.in_quote())
return p.parser_error_or_expr({"invalid nested quoted expression", pos});
auto& data = p.new_ast("`()", pos);
expr e;
{
parser::quote_scope scope1(p, true, id_behavior::ErrorIfUndef);
restore_decl_meta_scope scope2;
e = p.parse_expr();
if (p.curr_is_token(get_colon_tk())) {
p.next();
expr t = p.parse_expr();
ast_id id = p.new_ast(get_colon_tk(), p.pos_of(e)).push(p.get_id(e)).push(p.get_id(t)).m_id;
e = mk_typed_expr_distrib_choice(p, t, e, pos);
p.finalize_ast(id, e);
data.push(id);
} else {
data.push(p.get_id(e));
}
p.check_token_next(get_rparen_tk(), "invalid quoted expression, `)` expected");
}
auto r = p.save_pos(mk_unelaborated_expr_quote(e), pos);
p.finalize_ast(data.m_id, r);
return r;
}
static expr parse_antiquote_expr(parser & p, unsigned, expr const *, pos_info const & pos) {
if (!p.in_quote())
return p.parser_error_or_expr({"invalid antiquotation, occurs outside of quoted expressions", pos});
parser::quote_scope scope(p, false);
expr e = p.parse_expr(get_max_prec());
ast_id id = p.new_ast("%%", pos).push(p.get_id(e)).m_id;
e = p.save_pos(mk_antiquote(e), pos);
p.finalize_ast(id, e);
return e;
}
static expr parse_quoted_name(parser & p, unsigned, expr const *, pos_info const & pos) {
bool resolve = false;
name id;
auto& data = p.new_ast("`", pos);
if (p.curr_is_token(get_placeholder_tk())) {
p.next();
id = "_";
} else {
if (p.curr_is_token(get_backtick_tk())) {
data.m_type = "``";
p.next();
resolve = true;
}
if (p.curr_is_keyword() || p.curr_is_command()) {
if (resolve)
return p.parser_error_or_expr({"invalid resolved quote symbol, identifier is a keyword/command", pos});
id = p.get_token_info().token();
p.next();
} else {
id = p.check_id_next("invalid quoted name, identifier expected").second;
}
}
data.m_value = id;
if (resolve) {
parser::error_if_undef_scope scope(p);
bool resolve_only = true;
expr e = p.id_to_expr(id, p.new_ast("ident", pos, id), resolve_only);
if (is_constant(e)) {
id = const_name(e);
} else if (is_local(e)) {
id = mlocal_pp_name(e);
} else if (is_choice(e)) {
sstream ss;
ss << "invalid resolved quoted symbol, it is ambiguous, possible interpretations:";
for (unsigned i = 0; i < get_num_choices(e); i++)
ss << " " << get_choice(e, i);
ss << " (solution: use fully qualified names)";
return p.parser_error_or_expr({ss, pos});
} else {
return p.parser_error_or_expr({"invalid quoted symbol, failed to resolve it "
"(solution: use `<identifier> to bypass name resolution)", pos});
}
}
lean_assert(id.is_string());
expr e = quote(id);
e = p.rec_save_pos(e, pos);
p.finalize_ast(data.m_id, e);
return e;
}
static name * g_anonymous_constructor = nullptr;
expr mk_anonymous_constructor(expr const & e) { return mk_annotation(*g_anonymous_constructor, e); }
bool is_anonymous_constructor(expr const & e) { return is_annotation(e, *g_anonymous_constructor); }
expr const & get_anonymous_constructor_arg(expr const & e) {
lean_assert(is_anonymous_constructor(e));
return get_annotation_arg(e);
}
static expr parse_constructor_core(parser & p, pos_info const & pos) {
buffer<expr> args;
auto& data = p.new_ast(get_langle_tk(), pos);
while (!p.curr_is_token(get_rangle_tk())) {
args.push_back(p.parse_expr());
data.push(p.get_id(args.back()));
if (p.curr_is_token(get_comma_tk())) {
p.next();
} else {
break;
}
}
p.check_token_next(get_rangle_tk(), "invalid constructor, `⟩` expected");
expr fn = p.save_pos(mk_expr_placeholder(), pos);
expr e = p.save_pos(mk_anonymous_constructor(p.save_pos(mk_app(fn, args), pos)), pos);
p.finalize_ast(data.m_id, e);
return e;
}
static expr parse_constructor(parser & p, unsigned, expr const *, pos_info const & pos) {
return parse_constructor_core(p, pos);
}
static expr parse_lambda_core(parser & p, ast_data & fun, ast_data & bis, pos_info const & pos);
static expr parse_lambda_binder(parser & p, ast_data & fun, ast_data & bis, pos_info const & pos) {
parser::local_scope scope1(p);
buffer<expr> locals;
auto new_env = p.parse_binders(&bis, locals, 0);
for (expr const & local : locals)
p.add_local(local);
parser::local_scope scope2(p, new_env);
expr body;
if (p.curr_is_token(get_comma_tk())) {
p.next();
body = p.parse_expr();
fun.push(p.get_id(body));
} else if (p.curr_is_token(get_langle_tk())) {
body = parse_lambda_core(p, fun, bis, pos);
} else {
p.maybe_throw_error({"invalid lambda expression, ',' or '⟨' expected", p.pos()});
body = p.parse_expr();
fun.push(p.get_id(body));
}
bool use_cache = false;
return p.rec_save_pos(Fun(locals, body, use_cache), pos);
}
static name * g_lambda_match_name = nullptr;
static expr parse_lambda_constructor(parser & p, ast_data & fun, ast_data & bis, pos_info const & ini_pos) {
lean_assert(p.curr_is_token(get_langle_tk()));
parser::local_scope scope(p);
auto pos = p.pos();
p.next();
buffer<expr> locals;
expr pattern = p.parse_pattern([&](parser & p) { return parse_constructor_core(p, pos); }, locals);
bis.push(p.get_id(pattern));
for (expr const & local : locals)
p.add_local(local);
expr body;
if (p.curr_is_token(get_comma_tk())) {
p.next();
body = p.parse_expr();
fun.push(p.get_id(body));
} else {
body = parse_lambda_core(p, fun, bis, ini_pos);
}
match_definition_scope match_scope(p.env());
expr fn = p.save_pos(mk_local(p.next_name(), *g_lambda_match_name, mk_expr_placeholder(), mk_rec_info(true)), pos);
expr eqn = Fun(fn, Fun(locals, p.save_pos(mk_equation(p.rec_save_pos(mk_app(fn, pattern), pos), body), pos), p), p);
equations_header h = mk_match_header(match_scope.get_name(), match_scope.get_actual_name());
expr x = p.rec_save_pos(mk_local(p.next_name(), "_x", mk_expr_placeholder(), binder_info()), pos);
bool use_cache = false;
return p.rec_save_pos(Fun(x, mk_app(mk_equations(h, 1, &eqn), x), use_cache), pos);
}
static expr parse_lambda_core(parser & p, ast_data & fun, ast_data & bis, pos_info const & pos) {
if (p.curr_is_token(get_langle_tk())) {
return parse_lambda_constructor(p, fun, bis, pos);
} else {
return parse_lambda_binder(p, fun, bis, pos);
}
}
static expr parse_lambda(parser & p, unsigned, expr const *, pos_info const & pos) {
auto& bis = p.new_ast("binders", p.pos());
auto& fun = p.new_ast("fun", pos).push(bis.m_id);
expr r = parse_lambda_core(p, fun, bis, pos);
p.finalize_ast(fun.m_id, r);
return r;
}
static expr parse_assume(parser & p, unsigned, expr const *, pos_info const & pos) {
ast_id id; expr r;
if (p.curr_is_token(get_colon_tk())) {
// anonymous `assume`
p.next();
expr prop = p.parse_expr();
auto& binder = p.new_ast(name("binder").append_after(0U), p.pos())
.push(0).push(0).push(p.get_id(prop));
auto& bis = p.new_ast("binders", p.pos()).push(binder.m_id);
p.check_token_next(get_comma_tk(), "invalid 'assume', ',' expected");
parser::local_scope scope(p);
expr l = p.save_pos(mk_local(get_this_tk(), prop), pos);
p.add_local(l);
expr body = p.parse_expr();
id = p.new_ast("assume", pos).push(bis.m_id).push(p.get_id(body)).m_id;
r = p.save_pos(Fun(l, body, p), pos);
} else {
auto& bis = p.new_ast("binders", p.pos());
auto& fun = p.new_ast("assume", pos).push(bis.m_id);
id = fun.m_id;
r = parse_lambda_core(p, fun, bis, pos);
}
p.finalize_ast(id, r);
return r;
}
static void consume_rparen(parser & p) {
p.check_token_next(get_rparen_tk(), "invalid expression, `)` expected");
}
static name * g_infix_function = nullptr;
bool is_infix_function(expr const & e) {
return is_annotation(e, *g_infix_function);
}
expr mk_infix_function(expr const & e) {
return mk_annotation(*g_infix_function, e);
}
/* Check whether notation such as (<) (+ 10) is applicable.
Like Haskell,
(<) is notation for (fun x y, x < y)
(+ 10) is notation for (fun x, x + 10)
This kind of notation is applicable when
1- The current token is a keyword.
2- There is no entry for this token in the nud table
3- There is an entry for this token in the led table,
and exactly one Expr transition to an accepting state.
If the notation is applicable, this function returns the accepting list.
*/
static list<notation::accepting> is_infix_paren_notation(parser & p) {
if (p.curr_is_keyword() &&
!p.nud().find(p.get_token_info().value())) {
list<pair<transition, parse_table>> ts = p.led().find(p.get_token_info().value());
if (ts && !tail(ts) && head(ts).second.is_accepting()) {
notation::action const & act = head(ts).first.get_action();
if (act.kind() == notation::action_kind::Expr) {
return head(ts).second.is_accepting();
}
}
}
return list<notation::accepting>();
}
static expr parse_infix_paren(parser & p, list<notation::accepting> const & accs, pos_info const & pos) {
expr args[2];
buffer<expr> vars;
bool fixed_second_arg = false;
args[0] = mk_local(p.next_name(), "_x", mk_expr_placeholder(), binder_info());
vars.push_back(args[0]);
p.next();
auto& data = p.new_ast(*g_infix_function, pos).push(0);
if (p.curr_is_token(get_rparen_tk())) {
data.push(0);
p.next();
args[1] = mk_local(p.next_name(), "_y", mk_expr_placeholder(), binder_info());
vars.push_back(args[1]);
} else {
fixed_second_arg = true;
args[1] = p.parse_expr();