-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmpfas.c
3654 lines (3241 loc) · 138 KB
/
mmpfas.c
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) 2016 NORMAN MEGILL nm at alum.mit.edu */
/* License terms: GNU General Public License */
/*****************************************************************************/
/*34567890123456 (79-character line to adjust editor window) 2345678901234567*/
/* mmpfas.c - Proof assistant module */
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <setjmp.h>
#include <time.h>
#include "mmvstr.h"
#include "mmdata.h"
#include "mminou.h"
#include "mmpars.h"
#include "mmunif.h"
#include "mmpfas.h"
/* Allow user to define INLINE as "inline". lcc doesn't support inline. */
#ifndef INLINE
#define INLINE
#endif
long proveStatement = 0; /* The statement to be proved - global */
flag proofChangedFlag; /* Flag to push 'undo' stack - global */
/* 4-Aug-2011 nm Changed from 25000 to 50000 */
/* 11-Dec-2010 nm Changed from 10000 to 25000 to accomodate df-plig in set.mm
(which needs >= 23884 to generate with 'show statement / html'). */
/* userMaxProveFloat can be overridden by user with SET SEARCH_LIMIT */
long userMaxProveFloat = /*10000*/ 50000; /* Upper limit for proveFloating */
long dummyVars = 0; /* Total number of dummy variables declared */
long pipDummyVars = 0; /* Number of dummy variables used by proof in progress */
/* Structure for holding a proof in progress. */
/* This structure should be deallocated after use. */
struct pip_struct proofInProgress = {
NULL_NMBRSTRING, NULL_PNTRSTRING, NULL_PNTRSTRING, NULL_PNTRSTRING };
/* Interactively select statement assignments that match */
/* maxEssential is the maximum number of essential hypotheses that a
statement may have in order to be included in the matched list.
If -1, there is no limit. */
void interactiveMatch(long step, long maxEssential)
{
long matchCount = 0;
long timeoutCount = 0;
long essHypCount, hyp;
vstring matchFlags = "";
vstring timeoutFlags = "";
char unifFlag;
vstring tmpStr1 = "";
vstring tmpStr4 = "";
vstring tmpStr2 = "";
vstring tmpStr3 = "";
nmbrString *matchList = NULL_NMBRSTRING;
nmbrString *timeoutList = NULL_NMBRSTRING;
long stmt, matchListPos, timeoutListPos;
printLongLine(cat("Step ", str((double)step + 1), ": ", nmbrCvtMToVString(
(proofInProgress.target)[step]), NULL), " ", " ");
if (nmbrLen((proofInProgress.user)[step])) {
printLongLine(cat("Step ", str((double)step + 1), "(user): ", nmbrCvtMToVString(
(proofInProgress.user)[step]), NULL), " ", " ");
}
/* Allocate a flag for each step to be tested */
/* 1 means no match, 2 means match */
let(&matchFlags, string(proveStatement, 1));
/* 1 means no timeout, 2 means timeout */
let(&timeoutFlags, string(proveStatement, 1));
for (stmt = 1; stmt < proveStatement; stmt++) {
if (statement[stmt].type != (char)e_ &&
statement[stmt].type != (char)f_ &&
statement[stmt].type != (char)a_ &&
statement[stmt].type != (char)p_) continue;
/* See if the maximum number of requested essential hypotheses is
exceeded */
if (maxEssential != -1) {
essHypCount = 0;
for (hyp = 0; hyp < statement[stmt].numReqHyp; hyp++) {
if (statement[statement[stmt].reqHypList[hyp]].type == (char)e_) {
essHypCount++;
if (essHypCount > maxEssential) break;
}
}
if (essHypCount > maxEssential) continue;
}
unifFlag = checkStmtMatch(stmt, step);
if (unifFlag) {
if (unifFlag == 1) {
matchFlags[stmt] = 2;
matchCount++;
} else { /* unifFlag = 2 */
timeoutFlags[stmt] = 2;
timeoutCount++;
}
}
}
if (matchCount == 0 && timeoutCount == 0) {
print2("No statements match step %ld. The proof has an error.\n",
(long)(step + 1));
let(&matchFlags, "");
let(&timeoutFlags, "");
return;
}
#define MATCH_LIMIT 100
if (matchCount > MATCH_LIMIT) {
let(&tmpStr1, cat("There are ", str((double)matchCount), " matches for step ",
str((double)step + 1), ". View them (Y, N) <N>? ", NULL));
tmpStr2 = cmdInput1(tmpStr1);
let(&tmpStr1, "");
if (tmpStr2[0] != 'Y' && tmpStr2[0] != 'y') {
let(&tmpStr2, "");
let(&matchFlags, "");
let(&timeoutFlags, "");
return;
}
}
nmbrLet(&matchList, nmbrSpace(matchCount));
matchListPos = 0;
for (stmt = 1; stmt < proveStatement; stmt++) {
if (matchFlags[stmt] == 2) {
matchList[matchListPos] = stmt;
matchListPos++;
}
}
nmbrLet(&timeoutList, nmbrSpace(timeoutCount));
timeoutListPos = 0;
for (stmt = 1; stmt < proveStatement; stmt++) {
if (timeoutFlags[stmt] == 2) {
timeoutList[timeoutListPos] = stmt;
timeoutListPos++;
}
}
let(&tmpStr1, nmbrCvtRToVString(matchList,
/* 25-Jan-2016 nm */
0, /*explicitTargets*/
0 /*statemNum, used only if explicitTargets*/));
let(&tmpStr4, nmbrCvtRToVString(timeoutList,
/* 25-Jan-2016 nm */
0, /*explicitTargets*/
0 /*statemNum, used only if explicitTargets*/));
printLongLine(cat("Step ", str((double)step + 1), " matches statements: ", tmpStr1,
NULL), " ", " ");
if (timeoutCount) {
printLongLine(cat("In addition, there were unification timeouts with the",
" following steps, which may or may not match: ", tmpStr4, NULL),
" ", " ");
}
if (matchCount == 1 && timeoutCount == 0 && maxEssential == -1) {
/* Assign it automatically */
matchListPos = 0;
stmt = matchList[matchListPos];
print2("Step %ld was assigned statement %s since it is the only match.\n",
(long)(step + 1),
statement[stmt].labelName);
} else {
while (1) {
let(&tmpStr3, cat("What statement to select for step ", str((double)step + 1),
" (<return> to bypass)? ", NULL));
tmpStr2 = cmdInput1(tmpStr3);
let(&tmpStr3, "");
if (tmpStr2[0] == 0) {
let(&tmpStr1, "");
let(&tmpStr4, "");
let(&tmpStr2, "");
let(&matchFlags, "");
let(&timeoutFlags, "");
nmbrLet(&matchList, NULL_NMBRSTRING);
nmbrLet(&timeoutList, NULL_NMBRSTRING);
return;
}
if (!instr(1, cat(" ", tmpStr1, " ", tmpStr4, " ", NULL),
cat(" ", tmpStr2, " ", NULL))) {
print2("\"%s\" is not one of the choices. Try again.\n", tmpStr2);
} else {
break;
}
}
for (matchListPos = 0; matchListPos < matchCount; matchListPos++) {
if (!strcmp(tmpStr2, statement[matchList[matchListPos]].labelName)) break;
}
if (matchListPos < matchCount) {
stmt = matchList[matchListPos];
} else {
for (timeoutListPos = 0; timeoutListPos < timeoutCount;
timeoutListPos++) {
if (!strcmp(tmpStr2, statement[timeoutList[timeoutListPos]].labelName))
break;
} /* Next timeoutListPos */
if (timeoutListPos == timeoutCount) bug(1801);
stmt = timeoutList[timeoutListPos];
}
print2("Step %ld was assigned statement %s.\n",
(long)(step + 1),
statement[stmt].labelName);
} /* End if matchCount == 1 */
/* Add to statement to the proof */
assignStatement(matchList[matchListPos], step);
proofChangedFlag = 1; /* Flag for 'undo' stack */
let(&tmpStr1, "");
let(&tmpStr4, "");
let(&tmpStr2, "");
let(&matchFlags, "");
let(&timeoutFlags, "");
nmbrLet(&matchList, NULL_NMBRSTRING);
nmbrLet(&timeoutList, NULL_NMBRSTRING);
return;
} /* interactiveMatch */
/* Assign a statement to an unknown proof step */
void assignStatement(long statemNum, long step)
{
long hyp;
nmbrString *hypList = NULL_NMBRSTRING;
if ((proofInProgress.proof)[step] != -(long)'?') bug(1802);
/* Add the statement to the proof */
nmbrLet(&hypList, nmbrSpace(statement[statemNum].numReqHyp + 1));
for (hyp = 0; hyp < statement[statemNum].numReqHyp; hyp++) {
/* A hypothesis of the added statement */
hypList[hyp] = -(long)'?';
}
hypList[statement[statemNum].numReqHyp] = statemNum; /* The added statement */
addSubProof(hypList, step);
initStep(step + statement[statemNum].numReqHyp);
nmbrLet(&hypList, NULL_NMBRSTRING);
return;
} /* assignStatement */
/* Find proof of formula by using the replaceStatement() algorithm i.e.
see if any statement matches current step AND each of its hypotheses
matches a proof in progress hypothesis or some step already in the proof.
If a proof is found, it is returned, otherwise an empty (length 0) proof is
returned. */
/* The caller must deallocate the returned nmbrString. */
nmbrString *proveByReplacement(long prfStmt,
long prfStep, /* 0 means step 1 */
flag noDistinct, /* 1 means don't try statements with $d's */
flag dummyVarFlag, /* 0 means no dummy vars are in prfStmt */
flag searchMethod, /* 1 means to try proveFloating on $e's also */
long improveDepth, /* depth for proveFloating() */
/* 3-May-2016 nm */
flag overrideFlag /* 1 means to override usage locks */
)
{
long trialStmt;
nmbrString *prfMath;
nmbrString *trialPrf = NULL_NMBRSTRING;
prfMath = (proofInProgress.target)[prfStep];
for (trialStmt = 1; trialStmt < prfStmt; trialStmt++) {
if (quickMatchFilter(trialStmt, prfMath, dummyVarFlag) == 0) continue;
/* 3-May-2016 nm */
/* Skip statements with discouraged usage (the above skips non-$a,p) */
if (overrideFlag == 0 && getMarkupFlag(trialStmt, USAGE_DISCOURAGED)) {
continue;
}
/* noDistinct is set by NO_DISTICT qualifier in IMPROVE */
if (noDistinct) {
/* Skip the statement if it has a $d requirement. This option
prevents illegal proofs that would violate $d requirements
since the Proof Assistant does not check for $d violations. */
if (nmbrLen(statement[trialStmt].reqDisjVarsA)) {
continue;
}
}
trialPrf = replaceStatement(trialStmt, prfStep,
prfStmt, 0,/*scan whole proof to maximize chance of a match*/
noDistinct,
searchMethod,
improveDepth,
overrideFlag /* 3-May-2016 nm */
);
if (nmbrLen(trialPrf) > 0) {
/* A proof for the step was found. */
/* 3-May-2016 nm */
/* Inform user that we're using a statement with discouraged usage */
if (overrideFlag == 1 && getMarkupFlag(trialStmt, USAGE_DISCOURAGED)) {
/* print2("\n"); */ /* Enable for more emphasis */
print2(
">>> ?Warning: Overriding discouraged usage of statement \"%s\".\n",
statement[trialStmt].labelName);
/* print2("\n"); */ /* Enable for more emphasis */
}
return trialPrf;
}
/* nmbrLet(&trialPrf, NULL_NMBRSTRING); */
/* Don't need to do this because it is already null */
}
return trialPrf; /* Proof not found - return empty proof */
}
nmbrString *replaceStatement(long replStatemNum, long prfStep,
long provStmtNum,
flag subProofFlag, /* If 1, then scan only subproof at prfStep to look for
matches, instead of whole proof, for faster speed (used by MINIMIZE_WITH) */
flag noDistinct, /* 1 means proveFloating won't try statements with $d's */
flag searchMethod, /* 1 means to try proveFloating on $e's also */
long improveDepth, /* Depth for proveFloating */
/* 3-May-2016 nm */
flag overrideFlag /* 1 means to override statement usage locks */
) {
nmbrString *prfMath; /* Pointer only */
long reqHyps;
long hyp, sym, var, i, j, k, trialStep;
nmbrString *proof = NULL_NMBRSTRING;
nmbrString *scheme = NULL_NMBRSTRING;
pntrString *hypList = NULL_PNTRSTRING;
nmbrString *hypSortMap = NULL_NMBRSTRING; /* Order remapping for speedup */
pntrString *hypProofList = NULL_PNTRSTRING;
pntrString *stateVector = NULL_PNTRSTRING;
nmbrString *replStmtSchemePtr;
nmbrString *hypSchemePtr;
nmbrString *hypProofPtr;
nmbrString *makeSubstPtr;
pntrString *hypMakeSubstList = NULL_PNTRSTRING;
pntrString *hypStateVectorList = NULL_PNTRSTRING;
vstring hypReEntryFlagList = "";
nmbrString *hypStepList = NULL_NMBRSTRING;
flag reEntryFlag;
flag tmpFlag;
flag noHypMatch;
flag foundTrialStepMatch;
long replStmtSchemeLen, schemeVars, schReqHyps, hypLen, reqVars,
schEHyps, subPfLen;
long saveUnifTrialCount;
flag reenterFFlag;
flag dummyVarFlag; /* Flag that replacement hypothesis under consideration has
dummy variables */
nmbrString *hypTestPtr; /* Points to what we are testing hyp. against */
flag hypOrSubproofFlag; /* 0 means testing against hyp., 1 against proof*/
vstring indepKnownSteps = ""; /* 'Y' = ok to try step; 'N' = not ok */
/* 22-Aug-2012 nm */
long pfLen; /* 22-Aug-2012 nm */
long scanLen; /* 22-Aug-2012 nm */
long scanUpperBound; /* 22-Aug-2012 nm */
long scanLowerBound; /* 22-Aug-2012 nm */
vstring hasFloatingProof = ""; /* 'N' or 'Y' for $e hyps */
/* 4-Sep-2012 nm */
vstring tryFloatingProofLater = ""; /* 'N' or 'Y' */ /* 4-Sep-2012 nm */
flag hasDummyVar; /* 4-Sep-2012 nm */
/* 3-May-2016 nm */
/* If we are overriding discouraged usage, a warning has already been
printed. If we are not, then we should never get here. */
if (overrideFlag == 0 && getMarkupFlag(replStatemNum, USAGE_DISCOURAGED)) {
bug(1868);
}
/* Initialization to avoid compiler warning (should not be theoretically
necessary) */
trialStep = 0;
prfMath = (proofInProgress.target)[prfStep];
if (subProofFlag) {
/* Get length of the existing subproof at the replacement step. The
existing subproof will be scanned to see if there is a match to
the $e hypotheses of the replacement statement. */
subPfLen = subproofLen(proofInProgress.proof, prfStep);
scanLen = subPfLen;
scanUpperBound = prfStep;
scanLowerBound = scanUpperBound - scanLen + 1;
} else {
/* Treat the whole proof as a "subproof" and get its length. The whole
existing proof will be scanned to see if there is a match to
the $e hypotheses of the replacement statement. */
pfLen = nmbrLen(proofInProgress.proof);
/* scanLen = pfLen; */ /* 28-Sep-2013 never used */
scanUpperBound = pfLen - 1; /* Last proof step (0=1st step, 1=2nd, etc. */
scanLowerBound = 0; /* scanUpperBound - scanLen + 1; */
}
/* Note: the variables subPfLen, pfLen, and scanLen aren't
used again. They could be eliminated above if we wanted. */
if (statement[replStatemNum].type != (char)a_ &&
statement[replStatemNum].type != (char)p_)
bug(1822); /* Not $a or $p */
schReqHyps = statement[replStatemNum].numReqHyp;
reqVars = nmbrLen(statement[replStatemNum].reqVarList);
/* hasFloatingProof is used only when searchMethod=1 */
let(&hasFloatingProof, string(schReqHyps, ' ')); /* 4-Sep-2012 nm Init */
let(&tryFloatingProofLater, string(schReqHyps, ' ')); /* 4-Sep-2012 nm Init */
replStmtSchemePtr = statement[replStatemNum].mathString;
replStmtSchemeLen = nmbrLen(replStmtSchemePtr);
/* Change all variables in the statement to dummy vars for unification */
nmbrLet(&scheme, replStmtSchemePtr);
schemeVars = reqVars;
if (schemeVars + pipDummyVars/*global*/ > dummyVars/*global*/) {
/* Declare more dummy vars if necessary */
declareDummyVars(schemeVars + pipDummyVars - dummyVars);
}
for (var = 0; var < schemeVars; var++) {
/* Put dummy var mapping into mathToken[].tmp field */
mathToken[statement[replStatemNum].reqVarList[var]].tmp
= mathTokens/*global*/ + 1 + pipDummyVars/*global*/ + var;
}
for (sym = 0; sym < replStmtSchemeLen; sym++) {
if (mathToken[replStmtSchemePtr[sym]].tokenType != (char)var_) continue;
/* Use dummy var mapping from mathToken[].tmp field */
scheme[sym] = mathToken[replStmtSchemePtr[sym]].tmp;
}
/* Change all variables in the statement's hyps to dummy vars for subst. */
pntrLet(&hypList, pntrNSpace(schReqHyps));
nmbrLet(&hypSortMap, nmbrSpace(schReqHyps));
pntrLet(&hypProofList, pntrNSpace(schReqHyps));
for (hyp = 0; hyp < schReqHyps; hyp++) {
hypSchemePtr = NULL_NMBRSTRING;
nmbrLet(&hypSchemePtr,
statement[statement[replStatemNum].reqHypList[hyp]].mathString);
hypLen = nmbrLen(hypSchemePtr);
for (sym = 0; sym < hypLen; sym++) {
if (mathToken[hypSchemePtr[sym]].tokenType
!= (char)var_) continue;
/* Use dummy var mapping from mathToken[].tmp field */
hypSchemePtr[sym] = mathToken[hypSchemePtr[sym]].tmp;
}
hypList[hyp] = hypSchemePtr;
hypSortMap[hyp] = hyp;
}
/* Move all $e's to front of hypothesis list */
schEHyps = 0;
for (hyp = 0; hyp < schReqHyps; hyp++) {
if (statement[statement[replStatemNum].reqHypList[hypSortMap[hyp]]].type
== (char)e_) {
j = hypSortMap[hyp];
hypSortMap[hyp] = hypSortMap[schEHyps];
hypSortMap[schEHyps] = j;
schEHyps++;
}
}
/* 9/2/99 - Speedup - sort essential hyp's according to decreasing length to
maximize the chance of early rejection */
for (hyp = 0; hyp < schEHyps; hyp++) {
/* Bubble sort - but should be OK for typically small # of hyp's */
for (i = hyp + 1; i < schEHyps; i++) {
if (nmbrLen(hypList[hypSortMap[i]]) > nmbrLen(hypList[hypSortMap[hyp]])) {
j = hypSortMap[hyp];
hypSortMap[hyp] = hypSortMap[i];
hypSortMap[i] = j;
}
}
}
/* If we are just scanning the subproof, all subproof steps are independent,
so the getIndepKnownSteps scan would be redundant. */
if (!subProofFlag) {
/* 22-Aug-2012 nm - if subProofFlag is not set, we scan the whole proof,
not just the subproof starting at prfStep, to find additional possible
matches. */
/* Get a list of the possible steps to look at that are not dependent
on the prfStep. A value of 'Y' means we can try the step. */
let(&indepKnownSteps, "");
indepKnownSteps = getIndepKnownSteps(provStmtNum, prfStep);
}
/* Initialize state vector list for hypothesis unifications */
/* (We will really only use up to schEHyp entries, but allocate all
for possible future use) */
pntrLet(&hypStateVectorList, pntrPSpace(schReqHyps));
/* Initialize unification reentry flags for hypothesis unifications */
/* (1 means 0, and 2 means 1, because 0 means end-of-character-string.) */
/* (3 means previous proveFloating call found proof) */
let(&hypReEntryFlagList, string(schReqHyps, 1));
/* Initialize starting subproof step to scan for each hypothesis */
nmbrLet(&hypStepList, nmbrSpace(schReqHyps));
/* Initialize list of hypotheses after substitutions made */
pntrLet(&hypMakeSubstList, pntrNSpace(schReqHyps));
unifTrialCount = 1; /* Reset unification timeout */
reEntryFlag = 0; /* For unifyH() */
/* Number of required hypotheses of statement we're proving */
reqHyps = statement[provStmtNum].numReqHyp;
while (1) { /* Try all possible unifications */
tmpFlag = unifyH(scheme, prfMath, &stateVector, reEntryFlag);
if (!tmpFlag) break; /* (Next) unification not possible */
if (tmpFlag == 2) {
print2(
"Unification timed out. Larger SET UNIFICATION_TIMEOUT may improve results.\n");
unifTrialCount = 1; /* Reset unification timeout */
break; /* Treat timeout as if unification not possible */
}
reEntryFlag = 1; /* For next unifyH() */
/* Make substitutions into each hypothesis, and try to prove that
hypothesis */
nmbrLet(&proof, NULL_NMBRSTRING);
noHypMatch = 0;
for (hyp = 0; hyp < schReqHyps; hyp++) {
/* Make substitutions from replacement statement's stateVector */
nmbrLet((nmbrString **)(&(hypMakeSubstList[hypSortMap[hyp]])),
NULL_NMBRSTRING); /* Deallocate previous pass if any */
hypMakeSubstList[hypSortMap[hyp]] =
makeSubstUnif(&dummyVarFlag, hypList[hypSortMap[hyp]],
stateVector);
/* Initially, a $e has no proveFloating proof */
hasFloatingProof[hyp] = 'N'; /* Init for this pass */ /* 4-Sep-2012 nm */
tryFloatingProofLater[hyp] = 'N'; /* Init for this pass */ /* 4-Sep-2012 nm */
/* Make substitutions from each earlier hypothesis unification */
for (i = 0; i < hyp; i++) {
/* Only do substitutions for $e's -- the $f's will have no
dummy vars., and they have no stateVector
since they were found with proveFloating below */
if (i >= schEHyps) break;
/* If it is an essential hypothesis with a proveFloating proof,
we don't want to make substitutions since it has no
stateVector. (This is the only place we look
at hasFloatingProof.) */
if (hasFloatingProof[i] == 'Y') continue; /* 4-Sep-2012 nm */
makeSubstPtr = makeSubstUnif(&dummyVarFlag,
hypMakeSubstList[hypSortMap[hyp]],
hypStateVectorList[hypSortMap[i]]);
nmbrLet((nmbrString **)(&(hypMakeSubstList[hypSortMap[hyp]])),
NULL_NMBRSTRING);
hypMakeSubstList[hypSortMap[hyp]] = makeSubstPtr;
}
if (hyp < schEHyps) {
/* It's a $e hypothesis */
if (statement[statement[replStatemNum].reqHypList[hypSortMap[hyp]]
].type != (char)e_) bug (1823);
} else {
/* It's a $f hypothesis */
if (statement[statement[replStatemNum].reqHypList[hypSortMap[hyp]]
].type != (char)f_) bug(1824);
/* At this point there should be no dummy variables in $f
hypotheses */
/* 22-Aug-2012 nm This can now occur with new algorithm. For now,
let it continue; I'm not sure if there are adverse side effects. */
/*
if (dummyVarFlag) {
printSubst(stateVector);
bug(1825);
}
*/
}
/* Scan all known steps of existing subproof to find a hypothesis
match */
foundTrialStepMatch = 0;
reenterFFlag = 0;
if (hypReEntryFlagList[hypSortMap[hyp]] == 2) {
/* Reentry flag is set; we're continuing with a previously unified
subproof step */
trialStep = hypStepList[hypSortMap[hyp]];
/* If we are re-entering the unification for a $f, it means we
backtracked from a later failure, and there won't be another
unification possible. In this case we should bypass the
proveFloating call to force a further backtrack. (Otherwise
we will have an infinite loop.) Note that for $f's, all
variables will be known so there will only be one unification
anyway. */
if (hyp >= schEHyps
|| hasFloatingProof[hyp] == 'Y' /* 5-Sep-2012 nm */
) {
reenterFFlag = 1;
}
} else {
if (hypReEntryFlagList[hypSortMap[hyp]] == 1) {
/* Start at the beginning of the proof */
/* trialStep = prfStep - subPfLen + 1; */ /* obsolete */
trialStep = scanLowerBound; /* 22-Aug-2012 nm */
/* Later enhancement: start at required hypotheses */
/* (Here we use the trick of shifting down the starting
trialStep to below the real subproof start) */
trialStep = trialStep - reqHyps;
} else {
if (hypReEntryFlagList[hypSortMap[hyp]] == 3) {
/* This is the case where proveFloating previously found a
proof for the step, and we've backtracked. In this case,
we want to backtrack further - no scan or proveFloating
call again. */
hypReEntryFlagList[hypSortMap[hyp]] = 1;
reenterFFlag = 1; /* Skip proveFloating call */
/*trialStep = prfStep; old */ /* Skip loop */
trialStep = scanUpperBound; /* Skip loop */
} else {
bug(1826);
}
}
}
/* for (trialStep = trialStep; trialStep < prfStep; trialStep++) { old */
for (trialStep = trialStep + 0; trialStep < scanUpperBound;
trialStep++) { /* 22-Aug-2012 nm */
/* Note that step scanUpperBound is not scanned since that is
the statement we want to replace (subProofFlag = 1) or the
last statement of the proof (subProofFlag = 0), neither of
which would be useful for a replacement step subproof. */
/* if (trialStep < prfStep - subPfLen + 1) { */ /* obsolete */
if (trialStep < scanLowerBound) { /* 22-Aug-2012 nm */
/* We're scanning required hypotheses */
hypOrSubproofFlag = 0;
/* Point to what we are testing hyp. against */
/* (Note offset to trialStep needed to compensate for trick) */
hypTestPtr =
statement[statement[provStmtNum].reqHypList[
/* trialStep - (prfStep - subPfLen + 1 - reqHyps)]].mathString;*/
trialStep - (scanLowerBound - reqHyps)]].mathString;
/* 22-Aug-2012 nm */
} else {
/* We're scanning the subproof */
hypOrSubproofFlag = 1;
/* Point to what we are testing hyp. against */
hypTestPtr = (proofInProgress.target)[trialStep];
/* Do not consider unknown subproof steps or those with
unknown variables */
/* Break flag */
/***** obsolete 22-Aug-2012 nm - the new IMPROVE allows non-shared dummy
vars - but we'll keep the commented-out code for a while in case
it's needed for debugging
j = 0;
i = nmbrLen(hypTestPtr);
if (i == 0) bug(1824);
for (i = i - 1; i >= 0; i--) {
if (((nmbrString *)hypTestPtr)[i] >
mathTokens) {
j = 1;
break;
}
}
if (j) continue;
*******/
/* Subproof step has dummy var.; don't use it */
if (!subProofFlag) {
if (indepKnownSteps[trialStep] != 'Y') {
if (indepKnownSteps[trialStep] != 'N') bug(1836);
continue; /* Don't use the step */
}
}
}
/* Speedup - skip if no dummy vars in hyp and statements not equal */
if (!dummyVarFlag) {
if (!nmbrEq(hypTestPtr, hypMakeSubstList[hypSortMap[hyp]])) {
continue;
}
}
/* Speedup - skip if 1st symbols are constants and don't match */
/* First symbol */
i = hypTestPtr[0];
j = ((nmbrString *)(hypMakeSubstList[hypSortMap[hyp]]))[0];
if (mathToken[i].tokenType == (char)con_) {
if (mathToken[j].tokenType == (char)con_) {
if (i != j) {
continue;
}
}
}
/* unifTrialCount = 1; */ /* ??Don't reset it here in order to
detect exponential blowup in hypotheses trials */
unifTrialCount = 1; /* Reset unification timeout counter */
if (hypReEntryFlagList[hypSortMap[hyp]] < 1
|| hypReEntryFlagList[hypSortMap[hyp]] > 2)
bug(1851);
tmpFlag = unifyH(hypMakeSubstList[hypSortMap[hyp]],
hypTestPtr,
(pntrString **)(&(hypStateVectorList[hypSortMap[hyp]])),
/* (Remember: 1 = false, 2 = true in hypReEntryFlagList) */
hypReEntryFlagList[hypSortMap[hyp]] - 1);
if (!tmpFlag || tmpFlag == 2) {
/* (Next) unification not possible or timeout */
if (tmpFlag == 2) {
print2(
"Unification timed out. SET UNIFICATION_TIMEOUT larger for better results.\n");
unifTrialCount = 1; /* Reset unification timeout */
/* Deallocate unification state vector */
purgeStateVector(
(pntrString **)(&(hypStateVectorList[hypSortMap[hyp]])));
}
/* If this is a reenter, and there are no dummy vars in replacement
hypothesis, we have already backtracked from a unique exact
match that didn't work. There is no point in continuing to
look for another exact match for this hypothesis, so we'll just
skip the rest of the subproof scan. */
/* (Note that we could in principle bypass the redundant unification
above since we know it will fail, but it will clear out our
stateVector for us.) */
if (!dummyVarFlag) {
if (hypReEntryFlagList[hypSortMap[hyp]] - 1 == 1) {
/* There are no dummy variables, so previous match
was exact. Force the trialStep loop to terminate as
if nothing further was found. (If we don't do this,
there could be, say 50 more matches for "var x",
so we might need a factor of 50 greater runtime for each
replacement hypothesis having this situation.) */
/* trialStep = prfStep - 1; old version */
trialStep = scanUpperBound - 1;
/* Make this the last loop pass */ /* 22-Aug-2012 nm */
}
}
hypReEntryFlagList[hypSortMap[hyp]] = 1;
continue;
} else {
/* tmpFlag = 1: a unification was found */
if (tmpFlag != 1) bug(1828);
/* (Speedup) */
/* If this subproof step has previously occurred in a hypothesis
or an earlier subproof step, don't consider it since that
would be redundant. */
if (hypReEntryFlagList[hypSortMap[hyp]] == 1
/* && 0 */ /* To skip this for testing */
) {
j = 0; /* Break flag */
/*for (i = prfStep - subPfLen + 1 - reqHyps; i < trialStep; i++) {*/
for (i = scanLowerBound - reqHyps; i < trialStep; i++) {
/* 22-Aug-2012 nm */
/* if (i < prfStep - subPfLen + 1) { */
if (i < scanLowerBound) { /* 22-Aug-2012 nm */
/* A required hypothesis */
if (nmbrEq(hypTestPtr,
statement[statement[provStmtNum].reqHypList[
/*i - (prfStep - subPfLen + 1 - reqHyps)]].mathString)) { */
i - (scanLowerBound - reqHyps)]].mathString)) {
/* 22-Aug-2012 nm */
j = 1;
break;
}
} else {
/* A subproof step */
if (nmbrEq(hypTestPtr,
(proofInProgress.target)[i])) {
j = 1;
break;
}
}
} /* next i */
if (j) {
/* This subproof step was already considered earlier, so
we can skip considering it again. */
/* Deallocate unification state vector */
purgeStateVector(
(pntrString **)(&(hypStateVectorList[hypSortMap[hyp]])));
continue;
}
} /* end if not reentry */
/* (End speedup) */
hypReEntryFlagList[hypSortMap[hyp]] = 2; /* For next unifyH() */
hypStepList[hypSortMap[hyp]] = trialStep;
if (!hypOrSubproofFlag) {
/* We're scanning required hypotheses */
nmbrLet((nmbrString **)(&hypProofList[hypSortMap[hyp]]),
nmbrAddElement(NULL_NMBRSTRING,
statement[provStmtNum].reqHypList[
/* trialStep - (prfStep - subPfLen + 1 - reqHyps)])); */
trialStep - (scanLowerBound - reqHyps)])); /* 22-Aug-2012 nm */
} else {
/* We're scanning the subproof */
i = subproofLen(proofInProgress.proof, trialStep);
nmbrLet((nmbrString **)(&hypProofList[hypSortMap[hyp]]),
nmbrSeg(proofInProgress.proof, trialStep - i + 2,
trialStep + 1));
}
foundTrialStepMatch = 1;
break;
} /* end if (!tmpFlag || tmpFlag = 2) */
} /* next trialStep */
if (!foundTrialStepMatch) {
hasDummyVar = 0;
hypLen = nmbrLen(hypMakeSubstList[hypSortMap[hyp]]);
for (sym = 0; sym < hypLen; sym++) {
k = ((nmbrString *)(hypMakeSubstList[hypSortMap[hyp]]))[sym];
if (k > mathTokens/*global*/) {
hasDummyVar = 1;
break;
}
}
/* There was no (completely known) step in the (sub)proof that
matched the hypothesis. If it's a $f hypothesis, we will try
to prove it by itself. */
/* (However, if this is 2nd pass of backtrack, i.e. reenterFFlag is
set, we already got an exact $f match earlier and don't need this
scan, and shouldn't do it to prevent inf. loop.) */
/* if (hyp >= schEHyps */ /* old */
if ((hyp >= schEHyps || searchMethod == 1) /* 4-Sep-2012 */
&& !reenterFFlag
/*&& !hasDummyVar*/) { /* 25-Aug-2012 nm (Do we need this?) */
/* It's a $f hypothesis, or any hypothesis when searchMethod=1 */
if (hasDummyVar) {
/* If it's a $f and we have dummy vars, that is bad so we leave
foundTrialStepMatch = 0 to backtrack */
if (hyp < schEHyps) {
/* It's a $e with dummy vars, so we flag it to try later in
case further matches get rid of the dummy vars */
tryFloatingProofLater[hyp] = 'Y';
/* Unify the hypothesis with itself to initialize the
stateVector to allow further substitutions */
tmpFlag = unifyH(hypMakeSubstList[hypSortMap[hyp]],
hypMakeSubstList[hypSortMap[hyp]],
(pntrString **)(&(hypStateVectorList[hypSortMap[hyp]])),
/* (Remember: 1 = false, 2 = true in hypReEntryFlagList) */
hypReEntryFlagList[hypSortMap[hyp]] - 1);
if (tmpFlag != 1) bug (1849); /* This should be a trivial
unification, so it should never fail */
foundTrialStepMatch = 1; /* So we can continue */
}
} else {
saveUnifTrialCount = unifTrialCount; /* Save unification timeout */
hypProofPtr =
proveFloating(hypMakeSubstList[hypSortMap[hyp]],
provStmtNum, improveDepth, prfStep, noDistinct,
overrideFlag /* 3-May-2016 nm */
);
unifTrialCount = saveUnifTrialCount; /* Restore unif. timeout */
if (nmbrLen(hypProofPtr)) { /* Proof was found */
nmbrLet((nmbrString **)(&hypProofList[hypSortMap[hyp]]),
NULL_NMBRSTRING);
hypProofList[hypSortMap[hyp]] = hypProofPtr;
foundTrialStepMatch = 1;
hypReEntryFlagList[hypSortMap[hyp]] = 3;
/* Set flag so that we won't attempt subst. on $e w/ float prf */
hasFloatingProof[hyp] = 'Y'; /* 4-Sep-2012 */
}
}
} /* end if $f, or $e and searchMethod 1 */
}
if (hyp == schEHyps - 1 && foundTrialStepMatch) {
/* Scan all the postponed $e hypotheses in case they are known now */
for (i = 0; i < schEHyps; i++) {
if (tryFloatingProofLater[i] == 'Y') {
/* Incorporate substitutions of all later hypotheses
into this one (only earlier ones were done in main scan) */
for (j = i + 1; j < schEHyps; j++) {
if (hasFloatingProof[j] == 'Y') continue;
makeSubstPtr = makeSubstUnif(&dummyVarFlag,
hypMakeSubstList[hypSortMap[i]],
hypStateVectorList[hypSortMap[j]]);
nmbrLet((nmbrString **)(&(hypMakeSubstList[hypSortMap[i]])),
NULL_NMBRSTRING);
hypMakeSubstList[hypSortMap[i]] = makeSubstPtr;
}
hasDummyVar = 0;
hypLen = nmbrLen(hypMakeSubstList[hypSortMap[i]]);
for (sym = 0; sym < hypLen; sym++) {
k = ((nmbrString *)(hypMakeSubstList[hypSortMap[i]]))[sym];
if (k > mathTokens/*global*/) {
hasDummyVar = 1;
break;
}
}
if (hasDummyVar) {
foundTrialStepMatch = 0; /* Force backtrack */
/* If we don't have a proof at this point, we didn't save
enough info to backtrack easily, so we'll break out to
the top-most next unification (if any). */
hyp = 0; /* Force breakout below */
break;
}
saveUnifTrialCount = unifTrialCount; /* Save unification timeout */
hypProofPtr =
proveFloating(hypMakeSubstList[hypSortMap[i]],
provStmtNum, improveDepth, prfStep, noDistinct,
overrideFlag /* 3-May-2016 nm */
);
unifTrialCount = saveUnifTrialCount; /* Restore unif. timeout */
if (nmbrLen(hypProofPtr)) { /* Proof was found */
nmbrLet((nmbrString **)(&hypProofList[hypSortMap[i]]),
NULL_NMBRSTRING);
hypProofList[hypSortMap[i]] = hypProofPtr;
foundTrialStepMatch = 1;
hypReEntryFlagList[hypSortMap[i]] = 3;
/* Set flag so that we won't attempt subst. on $e w/ float prf */
hasFloatingProof[i] = 'Y'; /* 4-Sep-2012 */
} else { /* Proof not found */
foundTrialStepMatch = 0; /* Force backtrack */
/* If we don't have a proof at this point, we didn't save
enough info to backtrack easily, so we'll break out to
the top-most next unification (if any). */
hyp = 0; /* Force breakout below */
break;
} /* if (nmbrLen(hypProofPtr)) */
} /* if (tryFloatingProofLater[i] == 'Y') */
} /* for (i = 0; i < schEHyps; i++) */
} /* if (hyp == schEHyps - 1 && foundTrialStepMatch) */
if (!foundTrialStepMatch) {
/* We must backtrack */
/* 16-Sep-2012 nm fix infinite loop under weird conditions */
/* Backtrack through all of postponed hypotheses with
dummy variables whose proof wasn't found yet. If we
don't do this, we could end up with an infinite loop since we
would just repeat the postponement and move forward again. */
for (i = hyp - 1; i >=0; i--) {
if (tryFloatingProofLater[i] == 'N') break;
if (tryFloatingProofLater[i] != 'Y') bug(1853);
hyp--;
}
if (hyp == 0) {
/* No more possibilities to try */
noHypMatch = 1;
break;
}
hyp = hyp - 2; /* Go back one interation (subtract 2 to offset
end of loop increment) */
} /* if (!foundTrialStepMatch) */
} /* next hyp */
if (noHypMatch) {
/* Proof was not found for some hypothesis. */
continue; /* Get next unification */
} /* End if noHypMatch */
/* Proofs were found for all hypotheses */
/* Build the proof */
for (hyp = 0; hyp < schReqHyps; hyp++) {
if (nmbrLen(hypProofList[hyp]) == 0) bug(1852); /* Should have proof */
nmbrLet(&proof, nmbrCat(proof, hypProofList[hyp], NULL));
}
nmbrLet(&proof, nmbrAddElement(proof, replStatemNum));
/* Complete the proof */
/* Deallocate hypothesis schemes and proofs */
/* 25-Jun-2014 This is now done after returnPoint (why was it incomplete?)
for (hyp = 0; hyp < schReqHyps; hyp++) {
nmbrLet((nmbrString **)(&hypList[hyp]), NULL_NMBRSTRING);
nmbrLet((nmbrString **)(&hypProofList[hyp]), NULL_NMBRSTRING);
}
*/
goto returnPoint;
} /* End while (next unifyH() call for main replacement statement) */
nmbrLet(&proof, NULL_NMBRSTRING); /* Proof not possible */