-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctcheckmm-std.cpp
1396 lines (1202 loc) · 40.2 KB
/
ctcheckmm-std.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
// Compile-time Metamath database verifier
// Paul Keir, University of the West of Scotland
//
// This code is released to the public domain under the
// Creative Commons "CC0 1.0 Universal" Public Domain Dedication:
//
// http://creativecommons.org/publicdomain/zero/1.0/
//
// This is a C++23 standalone verifier for Metamath database files based on
// Eric Schmidt's original version (available at
// http://us.metamath.org/downloads/checkmm.cpp).
// To verify a database at runtime, the program may be run with a single file
// path as the parameter. In addition, to verify a database at compile-time,
// compile the program with MMFILEPATH defined as the path to a file containing
// a Metamath database encoded as a C++11 style raw string literal. The
// trivial delimit.sh bash script is provided to help convert database files to
// this format. The C'est library is at https://github.com/pkeir/cest
// wget https://raw.githubusercontent.com/metamath/set.mm/develop/peano.mm
// bash delimit.sh peano.mm
// g++ -std=c++23 -Winvalid-constexpr -Wl,-rpath,"$CEST2_ROOT/lib64:$LD_LIBRARY_PATH" -I $CEST2_ROOT/constexpr-std-headers/include/c++/14.0.0 -I $CEST2_ROOT/constexpr-std-headers/include/c++/14.0.0/x86_64-pc-linux-gnu -L $CEST2_ROOT/lib64 -D_GLIBCXX_CEST_CONSTEXPR=constexpr -D_GLIBCXX_CEST_VERSION=1 -fsanitize=address -static-libasan -fconstexpr-ops-limit=2147483647 -fconstexpr-loop-limit=2147483647 -DMMFILEPATH=peano.mm.raw ctcheckmm-std.cpp
// or...
// clang++ -std=c++2b -Winvalid-constexpr -Wl,-rpath,"$CEST2_ROOT/lib64:$LD_LIBRARY_PATH" -I $CEST2_ROOT/constexpr-std-headers/include/c++/14.0.0 -I $CEST2_ROOT/constexpr-std-headers/include/c++/14.0.0/x86_64-pc-linux-gnu -L $CEST2_ROOT/lib64 -D_GLIBCXX_CEST_CONSTEXPR=constexpr -D_GLIBCXX_CEST_VERSION=1 -fsanitize=address -fconstexpr-steps=2147483647 -DMMFILEPATH=peano.mm.raw ctcheckmm-std.cpp
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <utility>
struct checkmm
{
std::queue<std::string> tokens;
std::set<std::string> constants;
typedef std::vector<std::string> Expression;
// The first parameter is the statement of the hypothesis, the second is
// true iff the hypothesis is floating.
typedef std::pair<Expression, bool> Hypothesis;
std::map<std::string, Hypothesis> hypotheses;
std::set<std::string> variables;
// An axiom or a theorem.
struct Assertion
{
// Hypotheses of this axiom or theorem.
std::deque<std::string> hypotheses;
std::set<std::pair<std::string, std::string> > disjvars;
// Statement of axiom or theorem.
Expression expression;
};
std::map<std::string, Assertion> assertions;
struct Scope
{
std::set<std::string> activevariables;
// Labels of active hypotheses
std::vector<std::string> activehyp;
std::vector<std::set<std::string> > disjvars;
// Map from variable to label of active floating hypothesis
std::map<std::string, std::string> floatinghyp;
};
std::vector<Scope> scopes;
// Determine if a string is used as a label
constexpr bool labelused(std::string const label)
{
return hypotheses.find(label) != hypotheses.end()
|| assertions.find(label) != assertions.end();
}
// Find active floating hypothesis corresponding to variable, or empty string
// if there isn't one.
constexpr std::string getfloatinghyp(std::string const var)
{
for (std::vector<Scope>::const_iterator iter(scopes.begin());
iter != scopes.end(); ++iter)
{
std::map<std::string, std::string>::const_iterator const loc
(iter->floatinghyp.find(var));
if (loc != iter->floatinghyp.end())
return loc->second;
}
return std::string();
}
// Determine if a string is an active variable.
constexpr bool isactivevariable(std::string const str)
{
for (std::vector<Scope>::const_iterator iter(scopes.begin());
iter != scopes.end(); ++iter)
{
if (iter->activevariables.find(str) != iter->activevariables.end())
return true;
}
return false;
}
// Determine if a string is the label of an active hypothesis.
constexpr bool isactivehyp(std::string const str)
{
for (std::vector<Scope>::const_iterator iter(scopes.begin());
iter != scopes.end(); ++iter)
{
if (std::find(iter->activehyp.begin(), iter->activehyp.end(), str)
!= iter->activehyp.end())
return true;
}
return false;
}
// Determine if there is an active disjoint variable restriction on
// two different variables.
constexpr bool isdvr(std::string var1, std::string var2)
{
if (var1 == var2)
return false;
for (std::vector<Scope>::const_iterator iter(scopes.begin());
iter != scopes.end(); ++iter)
{
for (std::vector<std::set<std::string> >::const_iterator iter2
(iter->disjvars.begin()); iter2 != iter->disjvars.end(); ++iter2)
{
if ( iter2->find(var1) != iter2->end()
&& iter2->find(var2) != iter2->end())
return true;
}
}
return false;
}
// Determine if a character is white space in Metamath.
constexpr bool ismmws(char const ch)
{
// This doesn't include \v ("vertical tab"), as the spec omits it.
return ch == ' ' || ch == '\n' || ch == '\t' || ch == '\f' || ch == '\r';
}
// Determine if a token is a label token.
constexpr bool islabeltoken(std::string const token)
{
for (std::string::const_iterator iter(token.begin()); iter != token.end();
++iter)
{
unsigned char const ch(*iter);
if (!(std::isalnum(ch) || ch == '.' || ch == '-' || ch == '_'))
return false;
}
return true;
}
// Determine if a token is a math symbol token.
constexpr bool ismathsymboltoken(std::string const token)
{
return token.find('$') == std::string::npos;
}
// Determine if a token consists solely of upper-case letters or question marks
constexpr bool containsonlyupperorq(std::string token)
{
for (std::string::const_iterator iter(token.begin()); iter != token.end();
++iter)
{
if (!std::isupper(*iter) && *iter != '?')
return false;
}
return true;
}
constexpr std::string nexttoken(std::istream & input)
{
char ch;
std::string token;
// Skip whitespace
while (input.get(ch) && ismmws(ch)) { }
if (input.good())
input.unget();
// Get token
while (input.get(ch) && !ismmws(ch))
{
if (ch < '!' || ch > '~')
{
std::cerr << "Invalid character read with code 0x";
std::cerr << std::hex << (unsigned int)(unsigned char)ch
<< std::endl;
return std::string();
}
token += ch;
}
if (!input.eof() && input.fail())
return std::string();
return token;
}
std::set<std::string> names;
constexpr bool readtokens(std::string filename, std::string const &text = "")
{
//static std::set<std::string> names;
bool const alreadyencountered(!names.insert(filename).second);
if (alreadyencountered)
return true;
std::istringstream in;
if (!text.empty())
{
in.str(text);
}
else
{
std::ifstream file(filename.c_str());
if (!file)
{
std::cerr << "Could not open " << filename << std::endl;
return false;
}
std::string str((std::istreambuf_iterator(file)), {});
in.str(str);
}
bool incomment(false);
bool infileinclusion(false);
std::string newfilename;
std::string token;
while (!(token = nexttoken(in)).empty())
{
if (incomment)
{
if (token == "$)")
{
incomment = false;
continue;
}
if (token.find("$(") != std::string::npos)
{
std::cerr << "Characters $( found in a comment" << std::endl;
return false;
}
if (token.find("$)") != std::string::npos)
{
std::cerr << "Characters $) found in a comment" << std::endl;
return false;
}
continue;
}
// Not in comment
if (token == "$(")
{
incomment = true;
continue;
}
if (infileinclusion)
{
if (newfilename.empty())
{
if (token.find('$') != std::string::npos)
{
std::cerr << "Filename " << token << " contains a $"
<< std::endl;
return false;
}
newfilename = token;
continue;
}
else
{
if (token != "$]")
{
std::cerr << "Didn't find closing file inclusion delimiter"
<< std::endl;
return false;
}
bool const okay(readtokens(newfilename));
if (!okay)
return false;
infileinclusion = false;
newfilename.clear();
continue;
}
}
if (token == "$[")
{
if (std::is_constant_evaluated()) {
throw std::runtime_error("File inclusion unsupported within constexpr evaluation.");
}
infileinclusion = true;
continue;
}
tokens.push(token);
}
if (!in.eof())
{
if (in.fail())
std::cerr << "Error reading from " << filename << std::endl;
return false;
}
if (incomment)
{
std::cerr << "Unclosed comment" << std::endl;
return false;
}
if (infileinclusion)
{
std::cerr << "Unfinished file inclusion command" << std::endl;
return false;
}
return true;
}
// Construct an Assertion from an Expression. That is, determine the
// mandatory hypotheses and disjoint variable restrictions.
// The Assertion is inserted into the assertions collection,
// and is returned by reference.
constexpr Assertion & constructassertion
(std::string const label, Expression const & exp)
{
Assertion & assertion
(assertions.insert(std::make_pair(label, Assertion())).first->second);
assertion.expression = exp;
std::set<std::string> varsused;
// Determine variables used and find mandatory hypotheses
for (Expression::const_iterator iter(exp.begin()); iter != exp.end();
++iter)
{
if (variables.find(*iter) != variables.end())
varsused.insert(*iter);
}
for (std::vector<Scope>::const_reverse_iterator iter(scopes.rbegin());
iter != scopes.rend(); ++iter)
{
std::vector<std::string> const & hypvec(iter->activehyp);
for (std::vector<std::string>::const_reverse_iterator iter2
(hypvec.rbegin()); iter2 != hypvec.rend(); ++iter2)
{
Hypothesis const & hyp(hypotheses.find(*iter2)->second);
if (hyp.second && varsused.find(hyp.first[1]) != varsused.end())
{
// Mandatory floating hypothesis
assertion.hypotheses.push_front(*iter2);
}
else if (!hyp.second)
{
// Essential hypothesis
assertion.hypotheses.push_front(*iter2);
for (Expression::const_iterator iter3(hyp.first.begin());
iter3 != hyp.first.end(); ++iter3)
{
if (variables.find(*iter3) != variables.end())
varsused.insert(*iter3);
}
}
}
}
// Determine mandatory disjoint variable restrictions
for (std::vector<Scope>::const_iterator iter(scopes.begin());
iter != scopes.end(); ++iter)
{
std::vector<std::set<std::string> > const & disjvars(iter->disjvars);
for (std::vector<std::set<std::string> >::const_iterator iter2
(disjvars.begin()); iter2 != disjvars.end(); ++iter2)
{
std::set<std::string> dset;
std::set_intersection
(iter2->begin(), iter2->end(),
varsused.begin(), varsused.end(),
std::inserter(dset, dset.end()));
for (std::set<std::string>::const_iterator diter(dset.begin());
diter != dset.end(); ++diter)
{
std::set<std::string>::const_iterator diter2(diter);
++diter2;
for (; diter2 != dset.end(); ++diter2)
assertion.disjvars.insert(std::make_pair(*diter, *diter2));
}
}
}
return assertion;
}
// Read an expression from the token stream. Returns true iff okay.
constexpr bool readexpression
( char stattype, std::string label, std::string terminator,
Expression * exp)
{
if (tokens.empty())
{
std::cerr << "Unfinished $" << stattype << " statement " << label
<< std::endl;
return false;
}
std::string type(tokens.front());
if (constants.find(type) == constants.end())
{
std::cerr << "First symbol in $" << stattype << " statement " << label
<< " is " << type << " which is not a constant" << std::endl;
return false;
}
tokens.pop();
exp->push_back(type);
std::string token;
while (!tokens.empty() && (token = tokens.front()) != terminator)
{
tokens.pop();
if (constants.find(token) == constants.end()
&& getfloatinghyp(token).empty())
{
std::cerr << "In $" << stattype << " statement " << label
<< " token " << token
<< " found which is not a constant or variable in an"
" active $f statement" << std::endl;
return false;
}
exp->push_back(token);
}
if (tokens.empty())
{
std::cerr << "Unfinished $" << stattype << " statement " << label
<< std::endl;
return false;
}
tokens.pop(); // Discard terminator token
return true;
}
// Make a substitution of variables. The result is put in "destination",
// which should be empty.
constexpr void makesubstitution
(Expression const & original, std::map<std::string, Expression> substmap,
Expression * destination
)
{
for (Expression::const_iterator iter(original.begin());
iter != original.end(); ++iter)
{
std::map<std::string, Expression>::const_iterator const iter2
(substmap.find(*iter));
if (iter2 == substmap.end())
{
// Constant
destination->push_back(*iter);
}
else
{
// Variable
std::copy(iter2->second.begin(), iter2->second.end(),
std::back_inserter(*destination));
}
}
}
// Get the raw numbers from compressed proof format.
// The letter Z is translated as 0.
constexpr bool getproofnumbers(std::string label, std::string proof,
std::vector<std::size_t> * proofnumbers)
{
std::size_t const size_max(std::numeric_limits<std::size_t>::max());
std::size_t num(0u);
bool justgotnum(false);
for (std::string::const_iterator iter(proof.begin()); iter != proof.end();
++iter)
{
if (*iter <= 'T')
{
std::size_t const addval(*iter - ('A' - 1));
if (num > size_max / 20 || 20 * num > size_max - addval)
{
std::cerr << "Overflow computing numbers in compressed proof "
"of " << label << std::endl;
return false;
}
proofnumbers->push_back(20 * num + addval);
num = 0;
justgotnum = true;
}
else if (*iter <= 'Y')
{
std::size_t const addval(*iter - 'T');
if (num > size_max / 5 || 5 * num > size_max - addval)
{
std::cerr << "Overflow computing numbers in compressed proof "
"of " << label << std::endl;
return false;
}
num = 5 * num + addval;
justgotnum = false;
}
else // It must be Z
{
if (!justgotnum)
{
std::cerr << "Stray Z found in compressed proof of "
<< label << std::endl;
return false;
}
proofnumbers->push_back(0);
justgotnum = false;
}
}
if (num != 0)
{
std::cerr << "Compressed proof of theorem " << label
<< " ends in unfinished number" << std::endl;
return false;
}
return true;
}
// Subroutine for proof verification. Verify a proof step referencing an
// assertion (i.e., not a hypothesis).
constexpr bool verifyassertionref
(std::string thlabel, std::string reflabel, std::vector<Expression> * stack)
{
Assertion const & assertion(assertions.find(reflabel)->second);
if (stack->size() < assertion.hypotheses.size())
{
std::cerr << "In proof of theorem " << thlabel
<< " not enough items found on stack" << std::endl;
return false;
}
std::vector<Expression>::size_type const base
(stack->size() - assertion.hypotheses.size());
std::map<std::string, Expression> substitutions;
// Determine substitutions and check that we can unify
for (std::deque<std::string>::size_type i(0);
i < assertion.hypotheses.size(); ++i)
{
Hypothesis const & hypothesis
(hypotheses.find(assertion.hypotheses[i])->second);
if (hypothesis.second)
{
// Floating hypothesis of the referenced assertion
if (hypothesis.first[0] != (*stack)[base + i][0])
{
std::cout << "In proof of theorem " << thlabel
<< " unification failed" << std::endl;
return false;
}
Expression & subst(substitutions.insert
(std::make_pair(hypothesis.first[1],
Expression())).first->second);
std::copy((*stack)[base + i].begin() + 1, (*stack)[base + i].end(),
std::back_inserter(subst));
}
else
{
// Essential hypothesis
Expression dest;
makesubstitution(hypothesis.first, substitutions, &dest);
if (dest != (*stack)[base + i])
{
std::cerr << "In proof of theorem " << thlabel
<< " unification failed" << std::endl;
return false;
}
}
}
// Remove hypotheses from stack
stack->erase(stack->begin() + base, stack->end());
// Verify disjoint variable conditions
for (std::set<std::pair<std::string, std::string> >::const_iterator
iter(assertion.disjvars.begin());
iter != assertion.disjvars.end(); ++iter)
{
Expression const & exp1(substitutions.find(iter->first)->second);
Expression const & exp2(substitutions.find(iter->second)->second);
std::set<std::string> exp1vars;
for (Expression::const_iterator exp1iter(exp1.begin());
exp1iter != exp1.end(); ++exp1iter)
{
if (variables.find(*exp1iter) != variables.end())
exp1vars.insert(*exp1iter);
}
std::set<std::string> exp2vars;
for (Expression::const_iterator exp2iter(exp2.begin());
exp2iter != exp2.end(); ++exp2iter)
{
if (variables.find(*exp2iter) != variables.end())
exp2vars.insert(*exp2iter);
}
for (std::set<std::string>::const_iterator exp1iter
(exp1vars.begin()); exp1iter != exp1vars.end(); ++exp1iter)
{
for (std::set<std::string>::const_iterator exp2iter
(exp2vars.begin()); exp2iter != exp2vars.end(); ++exp2iter)
{
if (!isdvr(*exp1iter, *exp2iter))
{
std::cerr << "In proof of theorem " << thlabel
<< " disjoint variable restriction violated"
<< std::endl;
return false;
}
}
}
}
// Done verification of this step. Insert new statement onto stack.
Expression dest;
makesubstitution(assertion.expression, substitutions, &dest);
stack->push_back(dest);
return true;
}
// Verify a regular proof. The "proof" argument should be a non-empty sequence
// of valid labels. Return true iff the proof is correct.
constexpr bool verifyregularproof
(std::string label, Assertion const & theorem,
std::vector<std::string> const & proof
)
{
std::vector<Expression> stack;
for (std::vector<std::string>::const_iterator proofstep(proof.begin());
proofstep != proof.end(); ++proofstep)
{
// If step is a hypothesis, just push it onto the stack.
std::map<std::string, Hypothesis>::const_iterator hyp
(hypotheses.find(*proofstep));
if (hyp != hypotheses.end())
{
stack.push_back(hyp->second.first);
continue;
}
// It must be an axiom or theorem
bool const okay(verifyassertionref(label, *proofstep, &stack));
if (!okay)
return false;
}
if (stack.size() != 1)
{
std::cerr << "Proof of theorem " << label
<< " does not end with only one item on the stack"
<< std::endl;
return false;
}
if (stack[0] != theorem.expression)
{
std::cerr << "Proof of theorem " << label << " proves wrong statement"
<< std::endl;
}
return true;
}
// Verify a compressed proof
constexpr bool verifycompressedproof
(std::string label, Assertion const & theorem,
std::vector<std::string> const & labels,
std::vector<std::size_t> const & proofnumbers)
{
std::vector<Expression> stack;
std::size_t const mandhypt(theorem.hypotheses.size());
std::size_t const labelt(mandhypt + labels.size());
std::vector<Expression> savedsteps;
for (std::vector<std::size_t>::const_iterator iter(proofnumbers.begin());
iter != proofnumbers.end(); ++iter)
{
// Save the last proof step if 0
if (*iter == 0)
{
savedsteps.push_back(stack.back());
continue;
}
// If step is a mandatory hypothesis, just push it onto the stack.
if (*iter <= mandhypt)
{
stack.push_back
(hypotheses.find(theorem.hypotheses[*iter - 1])->second.first);
}
else if (*iter <= labelt)
{
std::string const proofstep(labels[*iter - mandhypt - 1]);
// If step is a (non-mandatory) hypothesis,
// just push it onto the stack.
std::map<std::string, Hypothesis>::const_iterator hyp
(hypotheses.find(proofstep));
if (hyp != hypotheses.end())
{
stack.push_back(hyp->second.first);
continue;
}
// It must be an axiom or theorem
bool const okay(verifyassertionref(label, proofstep, &stack));
if (!okay)
return false;
}
else // Must refer to saved step
{
if (*iter > labelt + savedsteps.size())
{
std::cerr << "Number in compressed proof of " << label
<< " is too high" << std::endl;
return false;
}
stack.push_back(savedsteps[*iter - labelt - 1]);
}
}
if (stack.size() != 1)
{
std::cerr << "Proof of theorem " << label
<< " does not end with only one item on the stack"
<< std::endl;
return false;
}
if (stack[0] != theorem.expression)
{
std::cerr << "Proof of theorem " << label << " proves wrong statement"
<< std::endl;
}
return true;
}
// Parse $p statement. Return true iff okay.
constexpr bool parsep(std::string label)
{
Expression newtheorem;
bool const okay(readexpression('p', label, "$=", &newtheorem));
if (!okay)
{
return false;
}
Assertion const & assertion(constructassertion(label, newtheorem));
// Now for the proof
if (tokens.empty())
{
std::cerr << "Unfinished $p statement " << label << std::endl;
return false;
}
if (tokens.front() == "(")
{
// Compressed proof
tokens.pop();
// Get labels
std::vector<std::string> labels;
std::string token;
while (!tokens.empty() && (token = tokens.front()) != ")")
{
tokens.pop();
labels.push_back(token);
if (token == label)
{
std::cerr << "Proof of theorem " << label
<< " refers to itself" << std::endl;
return false;
}
else if (std::find
(assertion.hypotheses.begin(), assertion.hypotheses.end(),
token) != assertion.hypotheses.end())
{
std::cerr << "Compressed proof of theorem " << label
<< " has mandatory hypothesis " << token
<< " in label list" << std::endl;
return false;
}
else if (assertions.find(token) == assertions.end()
&& !isactivehyp(token))
{
std::cerr << "Proof of theorem " << label << " refers to "
<< token << " which is not an active statement"
<< std::endl;
return false;
}
}
if (tokens.empty())
{
std::cerr << "Unfinished $p statement " << label << std::endl;
return false;
}
tokens.pop(); // Discard ) token
// Get proof steps
std::string proof;
while (!tokens.empty() && (token = tokens.front()) != "$.")
{
tokens.pop();
proof += token;
if (!containsonlyupperorq(token))
{
std::cerr << "Bogus character found in compressed proof of "
<< label << std::endl;
return false;
}
}
if (tokens.empty())
{
std::cerr << "Unfinished $p statement " << label << std::endl;
return false;
}
if (proof.empty())
{
std::cerr << "Theorem " << label << " has no proof" << std::endl;
return false;
}
tokens.pop(); // Discard $. token
if (proof.find('?') != std::string::npos)
{
std::cerr << "Warning: Proof of theorem " << label
<< " is incomplete" << std::endl;
return true; // Continue processing file
}
std::vector<std::size_t> proofnumbers;
proofnumbers.reserve(proof.size()); // Preallocate for efficiency
bool okay(getproofnumbers(label, proof, &proofnumbers));
if (!okay)
return false;
okay = verifycompressedproof(label, assertion, labels, proofnumbers);
if (!okay)
return false;
}
else
{
// Regular (uncompressed proof)
std::vector<std::string> proof;
bool incomplete(false);
std::string token;
while (!tokens.empty() && (token = tokens.front()) != "$.")
{
tokens.pop();
proof.push_back(token);
if (token == "?")
incomplete = true;
else if (token == label)
{
std::cerr << "Proof of theorem " << label
<< " refers to itself" << std::endl;
return false;
}
else if (assertions.find(token) == assertions.end()
&& !isactivehyp(token))
{
std::cerr << "Proof of theorem " << label << " refers to "
<< token << " which is not an active statement"
<< std::endl;
return false;
}
}
if (tokens.empty())
{
std::cerr << "Unfinished $p statement " << label << std::endl;
return false;
}
if (proof.empty())
{
std::cerr << "Theorem " << label << " has no proof" << std::endl;
return false;
}
tokens.pop(); // Discard $. token
if (incomplete)
{
std::cerr << "Warning: Proof of theorem " << label
<< " is incomplete" << std::endl;
return true; // Continue processing file
}
bool okay(verifyregularproof(label, assertion, proof));
if (!okay)
return false;
}
return true;
}
// Parse $e statement. Return true iff okay.
constexpr bool parsee(std::string label)
{
Expression newhyp;
bool const okay(readexpression('e', label, "$.", &newhyp));
if (!okay)
{
return false;
}
// Create new essential hypothesis
hypotheses.insert(std::make_pair(label, std::make_pair(newhyp, false)));
scopes.back().activehyp.push_back(label);
return true;
}
// Parse $a statement. Return true iff okay.
constexpr bool parsea(std::string label)
{
Expression newaxiom;
bool const okay(readexpression('a', label, "$.", &newaxiom));
if (!okay)
{
return false;
}
constructassertion(label, newaxiom);
return true;
}
// Parse $f statement. Return true iff okay.