forked from metamath/metamath-exe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmunif.c
1734 lines (1567 loc) · 65.9 KB
/
mmunif.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) 2017 NORMAN MEGILL nm at alum.mit.edu */
/* License terms: GNU General Public License */
/*****************************************************************************/
/*34567890123456 (79-character line to adjust editor window) 2345678901234567*/
/* mmunif.c - Unifications for proof assistant (note: unifications for normal
proof verification is done in mmveri.c) */
/*
This module deals with an object called the stateVector, which is a pntrString
of 16 pointers (called entries 0 through 15 below) to either other pntrStrings
or to nmbrStrings. In the description, data in stateVector may be referred to
by the local C variable the data is typically assigned to, such as
"unkVarsLen". The word "variable" in the context of scheme content refers
to temporary (or "work" or "dummy") variables $1, $2, etc. The entries are not
organized in logical order for historical reasons, e.g. entry 11 logically
comes first.
Entry 11 is a nmbrString of length 4 holding individual parameters.
11[0] is the total number of variables ($1, $2, etc.) in schemeA and schemeB,
i.e. the two schemes being unified.
unkVarsLen = ((nmbrString *)((*stateVector)[11]))[0];
11[1] or stackTop is the number of variables (minus 1) that will require
substitutions in order to perform the unification. Warning: stackTop may be
-1, which could be confused with "end of nmbrString" by some nmbrString
functions.
stackTop = ((nmbrString *)((*stateVector)[11]))[1];
11[2] is the number of variables in schemeA, used by oneDirUnif() (only).
schemeAUnkVarsLen = ((nmbrString *)((*stateVector)[11]))[2];
11[3] is the number of entries in the "Henty filter", used by unifyH() (only).
hentyFilterSize = ((nmbrString *)((*stateVector)[11]))[3];
Entry 8 is the result of unifying schemeA and schemeB, which are the two
schemes being unified.
unifiedScheme = (nmbrString *)((*stateVector)[8]);
Entries 0 through 3 each have length unkVarsLen. Entry 1 is a list of token
numbers for the temporary variables substituted in the unification.
Entry 0 has all variables ($1, $2, etc.) in schemeA and schemeB.
unkVars = (nmbrString *)((*stateVector)[0]);
In entries 1 through 3, only variables 0 through stackTop (inclusive) have
meaning. These entries, along with unifiedScheme, determine what variables
were substituted and there substitutions.
Entry 1 is the list of variables that were substituted.
Entry 2 is the location of the substitution in unifiedScheme, for each variable
in entry 1.
Entry 3 is the length of the substitution for each variable in entry 1.
stackUnkVar = (nmbrString *)((*stateVector)[1]);
stackUnkVarStart = (nmbrString *)((*stateVector)[2]);
stackUnkVarLen = (nmbrString *)((*stateVector)[3]);
Entries 4 thru 7 each point to unkVarsLen nmbrString's. These entries save the
data needed to resume unification at any point. Entries 4 and 5 are
nmbrString's of length unkVarsLen. Entries 6 and 7 will have variable length.
Only the first stackTop+1 nmbrString's have meaning. Note that stackTop may be
-1.
stackSaveUnkVarStart = (pntrString *)((*stateVector)[4]);
stackSaveUnkVarLen = (pntrString *)((*stateVector)[5]);
stackSaveSchemeA = (pntrString *)((*stateVector)[6]);
stackSaveSchemeB = (pntrString *)((*stateVector)[7]);
Entries 9 and 10 save the contents of 2 and 3 in oneDirUnif (only)
nmbrLet((nmbrString **)(&(*stateVector)[9]),
(nmbrString *)((*stateVector)[2]));
nmbrLet((nmbrString **)(&(*stateVector)[10]),
(nmbrString *)((*stateVector)[3]));
Entries 12 through 15 hold the "Henty filter", i.e. a list of all "normalized"
unifications so far. Used by unifyH() (only). Each entry 12 through 15 is a
list of pointers of length hentyFilterSize, each pointing to hentyFilterSize
nmbrString's. The Henty filter eliminates redundant equivalent unifications.
Entry 12[i] is a list of variables substituted by the normalized unification.
Entry 13[i] is the start of each substitution in hentySubstList.
Entry 14[i] is the length of each substitution in hentySubstList.
Entry 15[i] is the unified scheme that resulted from the particular unification.
Note: i = 0 through hentyFilterSize-1 below.
hentyVars = (nmbrString *)(((pntrString *)((*stateVector)[12]))[i]);
hentyVarStart = (nmbrString *)(((pntrString *)((*stateVector)[13]))[i]);
hentyVarLen = (nmbrString *)(((pntrString *)((*stateVector)[14]))[i]);
hentySubstList = (nmbrString *)(((pntrString *)((*stateVector)[15]))[i]);
*/
#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" /* 8/28/99 For proveStatement global variable */
/*long minSubstLen = 0;*/ /* User-settable value - 0 or 1 */
long minSubstLen = 1; /* It was decided to disallow empty subst. by default
since most formal systems don't need it */
long userMaxUnifTrials = 100000; /* Initial value */
/* User-defined upper limit (# backtracks) for unification trials */
/* 1-Jun-04 nm Changed userMaxUnifTrials from 1000 to 100000, which
is not a problem with today's faster computers. This results in
fewer annoying "Unification timed out" messages, but the drawback
is that (rarely) there may be hundreds of unification
choices for the user (which the user can quit from though). */
long unifTrialCount = 0;
/* 0 means don't time out; 1 means start counting trials */
long unifTimeouts = 0; /* Number of timeouts so far for this command */
flag hentyFilter = 1; /* Default to ON (turn OFF for debugging). */
flag bracketMatchInit = 0; /* Global so eraseSource() (mmcmds.c) can clr it */
/* Additional local prototypes */
void hentyNormalize(nmbrString **hentyVars, nmbrString **hentyVarStart,
nmbrString **hentyVarLen, nmbrString **hentySubstList,
pntrString **stateVector);
flag hentyMatch(nmbrString *hentyVars, nmbrString *hentyVarStart,
/*nmbrString *hentyVarLen,*/ nmbrString *hentySubstList,
pntrString **stateVector);
void hentyAdd(nmbrString *hentyVars, nmbrString *hentyVarStart,
nmbrString *hentyVarLen, nmbrString *hentySubstList,
pntrString **stateVector);
/* For heuristics */
int maxNestingLevel = -1;
int nestingLevel = 0;
/* 8/29/99 For improving rejection of impossible substitutions */
/* 1-Oct-2017 nm Made firstConst global so eraseSource() can clear it */
/* 2-Oct-2017 nm Made them all global so valgrind won't complain */
nmbrString *firstConst = NULL_NMBRSTRING;
nmbrString *lastConst = NULL_NMBRSTRING;
nmbrString *oneConst = NULL_NMBRSTRING;
/* Typical call:
nmbrStringXxx = makeSubstUnif(&newVarFlag,trialScheme,
stateVector);
Call this after calling unify().
trialScheme should have the same unknown variable names as were in
the schemes given to unify().
nmbrStringXxx will have these unknown variables substituted with
the result of the unification.
newVarFlag is 1 if there are new $nn variables in nmbrStringXxx.
The caller must deallocate the returned nmbrString.
*/
nmbrString *makeSubstUnif(flag *newVarFlag,
nmbrString *trialScheme, pntrString *stateVector)
{
long p,q,i,j,k,m,tokenNum;
long schemeLen;
nmbrString *result = NULL_NMBRSTRING;
nmbrString *stackUnkVar = NULL_NMBRSTRING;
nmbrString *unifiedScheme; /* Pointer only - not allocated */
nmbrString *stackUnkVarLen; /* Pointer only - not allocated */
nmbrString *stackUnkVarStart; /* Pointer only - not allocated */
long stackTop;
/*E*/long d;
/*E*/vstring tmpStr = "";
/*E*/let(&tmpStr,tmpStr);
stackTop = ((nmbrString *)(stateVector[11]))[1];
nmbrLet(&stackUnkVar,nmbrLeft((nmbrString *)(stateVector[1]), stackTop + 1));
stackUnkVarStart = (nmbrString *)(stateVector[2]); /* stackUnkVarStart */
stackUnkVarLen = (nmbrString *)(stateVector[3]); /* stackUnkVarLen */
unifiedScheme = (nmbrString *)(stateVector[8]);
/*E*/if(db7)print2("Entered makeSubstUnif.\n");
/*E*/if(db7)printLongLine(cat("unifiedScheme is ",
/*E*/ nmbrCvtMToVString(unifiedScheme), NULL), "", " ");
/*E*/if(db7)printLongLine(cat("trialScheme is ",
/*E*/ nmbrCvtMToVString(trialScheme), NULL), "", " ");
/*E*/if(db7)print2("stackTop is %ld.\n",stackTop);
/*E*/for (d = 0; d <= stackTop; d++) {
/*E*/ if(db7)print2("Unknown var %ld is %s.\n",d,
/*E*/ mathToken[stackUnkVar[d]].tokenName);
/*E*/ if(db7)print2(" Its start is %ld; its length is %ld.\n",
/*E*/ stackUnkVarStart[d],stackUnkVarLen[d]);
/*E*/}
schemeLen = nmbrLen(trialScheme);
/* Make the substitutions into trialScheme. */
/* First, calculate the length of the final result */
q = 0;
*newVarFlag = 0; /* Flag that there are new variables in the output string */
/*E*/if(db7)print2("schemeLen is %ld.\n",schemeLen);
for (p = 0; p < schemeLen; p++) {
/*E*/if(db7)print2("p is %ld.\n",p);
tokenNum = trialScheme[p];
/*E*/if(db7)print2("token is %s, tokenType is %ld\n",mathToken[tokenNum].tokenName,
/*E*/ (long)mathToken[tokenNum].tokenType);
if (mathToken[tokenNum].tokenType == (char)con_) {
q++;
} else {
if (tokenNum > mathTokens) {
/* It's a candidate for substitution */
m = nmbrElementIn(1,stackUnkVar,tokenNum);
/*E*/if(db7)print2("token is %s, m is %ld\n",mathToken[tokenNum].tokenName,m);
if (m) {
/* It will be substituted */
q = q + stackUnkVarLen[m - 1];
/* Flag the token position */
mathToken[tokenNum].tmp = m - 1;
} else {
/* It will not be substituted */
*newVarFlag = 1; /* The result will contain an "unknown" variable */
q++;
/* Flag the token position */
mathToken[tokenNum].tmp = -1;
}
} else {
/* It's not an "unknown" variable, so it won't be substituted */
q++;
}
}
}
/* Allocate space for the final result */
nmbrLet(&result, nmbrSpace(q));
/* Assign the final result */
q = 0;
for (p = 0; p < schemeLen; p++) {
tokenNum = trialScheme[p];
if (mathToken[tokenNum].tokenType == (char)con_) {
result[q] = tokenNum;
q++;
} else {
if (tokenNum > mathTokens) {
/* It's a candidate for substitution */
k = mathToken[tokenNum].tmp; /* Position in stackUnkVar */
if (k != -1) {
/* It will be substituted */
m = stackUnkVarStart[k]; /* Start of substitution */
j = stackUnkVarLen[k]; /* Length of substitition */
for (i = 0; i < j; i++) {
result[q + i] = unifiedScheme[m + i];
}
q = q + j;
} else {
/* It will not be substituted */
result[q] = tokenNum;
q++;
}
} else {
/* It's not an "unknown" variable, so it won't be substituted */
result[q] = tokenNum;
q++;
}
} /* end "if a constant" */
}
/*E*/if(db7)print2("after newVarFlag %d\n",(int)*newVarFlag);
/*E*/if(db7)print2("final len is %ld\n",q);
/*E*/if(db7)printLongLine(cat("result ",nmbrCvtMToVString(result),NULL),""," ");
nmbrLet(&stackUnkVar, NULL_NMBRSTRING); /* Deallocate */
return (result);
} /* makeSubstUnif */
char unify(
nmbrString *schemeA,
nmbrString *schemeB,
/* nmbrString **unifiedScheme, */ /* stateVector[8] holds this */
pntrString **stateVector,
long reEntryFlag)
{
/* This function unifies two math token strings, schemeA and
schemeB. The result is contained in unifiedScheme.
0 is returned if no assignment is possible, 1 if an assignment was
found, and 2 if the unification timed out.
If reEntryFlag is 1, the next possible set of assignments, if any,
is returned. (*stateVector) contains the state of the previous
call. It is the caller's responsibility to deallocate the
contents of (*stateVector) when done, UNLESS a 0 is returned.
The caller must assign (*stateVector) to a legal pntrString
(e.g. NULL_PNTRSTRING) before calling.
All variables with a tokenNum > mathTokens are assumed
to be "unknown" variables that can be assigned; all other
variables are treated like constants in the unification
algorithm.
The "unknown" variable assignments are contained in (*stateVector)
(which is a complex structure, described above). Some "unknown"
variables may have no assignment, in which case they will
remain "unknown", and others may have assignments which include
"unknown" variables. The (*stateVector) entries 9 and 10 are used
by oneDirUnif() only.
*/
long stackTop;
nmbrString *unkVars; /* List of all unknown vars */
long unkVarsLen;
long schemeAUnkVarsLen;
nmbrString *stackUnkVar; /* Location of stacked var in unkVars */
nmbrString *stackUnkVarStart; /* Start of stacked var in unifiedScheme*/
nmbrString *stackUnkVarLen; /* Length of stacked var assignment */
pntrString *stackSaveUnkVarStart; /* stackUnkVarStart at the time a
variable was first stacked */
pntrString *stackSaveUnkVarLen; /* stackUnkVarLen at the time a variable
was first stacked */
pntrString *stackSaveSchemeA; /* Pointer to saved schemeA at the time
the variable was first stacked */
pntrString *stackSaveSchemeB; /* Pointer to saved schemeB at the time
the variable was first stacked */
nmbrString *unifiedScheme; /* Final result */
long p; /* Current position in schemeA or schemeB */
long substToken; /* Token from schemeA or schemeB that will be substituted */
nmbrString *substitution = NULL_NMBRSTRING;
/* String to be subst. for substToken */
nmbrString *nmbrTmpPtr; /* temp pointer only */
pntrString *pntrTmpPtr; /* temp pointer only */
nmbrString *schA = NULL_NMBRSTRING; /* schemeA with dummy token at end */
nmbrString *schB = NULL_NMBRSTRING; /* schemeB with dummy token at end */
long i,j,k,m, pairingMismatches;
flag breakFlag;
flag schemeAFlag;
flag timeoutAbortFlag = 0;
vstring mToken; /* Pointer only; not allocated */
/* 8/28/99 For detection of simple impossible unifications */
flag impossible;
long stmt;
/* 26-Sep-2010 nm For bracket matching heuristic for set.mm */
static char bracketMatchOn; /* 26-Sep-2010 nm Default is 'on' */
/* static char bracketMatchInit = 0; */ /* Global so ERASE can init it */
long bracketScanStart, bracketScanStop; /* For one-time $a scan */
flag bracketMismatchFound;
/*E*/long d;
/*E*/vstring tmpStr = "";
/*E*/let(&tmpStr,tmpStr);
/*E*/if(db5)print2("Entering unify() with reEntryFlag = %ld.\n",
/*E*/ (long)reEntryFlag);
/*E*/if(db5)printLongLine(cat("schemeA is ",
/*E*/ nmbrCvtMToVString(schemeA),".",NULL)," "," ");
/*E*/if(db5)printLongLine(cat("schemeB is ",
/*E*/ nmbrCvtMToVString(schemeB),".",NULL)," "," ");
/* Initialization to avoid compiler warning (should not be theoretically
necessary) */
p = 0;
bracketMismatchFound = 0;
/* Fast early exit -- first or last constants of schemes don't match */
if (mathToken[schemeA[0]].tokenType == (char)con_) {
if (mathToken[schemeB[0]].tokenType == (char)con_) {
if (schemeA[0] != schemeB[0]) {
return (0);
}
}
}
/* (j and k are used below also) */
j = nmbrLen(schemeA);
k = nmbrLen(schemeB);
if (!j || !k) bug(1901);
if (mathToken[schemeA[j-1]].tokenType == (char)con_) {
if (mathToken[schemeB[k-1]].tokenType == (char)con_) {
if (schemeA[j-1] != schemeB[k-1]) {
return (0);
}
}
}
/* Add dummy token to end of schemeA and schemeB */
/* Use one beyond the last mathTokenArray entry for this */
nmbrLet(&schA, nmbrAddElement(schemeA, mathTokens));
nmbrLet(&schB, nmbrAddElement(schemeB, mathTokens));
/* 8/29/99 Initialize the usage of constants as the first, last,
only constant in a $a statement - for rejecting some simple impossible
substitutions - Speed-up: this is now done once and never deallocated*/
/* 1-Oct-2017 nm firstConst is now cleared in eraseSource.c() (mmcmds.c)
to trigger this initialization after "erase" */
if (!nmbrLen(firstConst)) {
/* nmbrSpace() sets all entries to 0, not 32 (ASCII space) */
nmbrLet(&firstConst, nmbrSpace(mathTokens));
nmbrLet(&lastConst, nmbrSpace(mathTokens));
nmbrLet(&oneConst, nmbrSpace(mathTokens));
/*for (stmt = 1; stmt < proveStatement; stmt++) {*/
/* Do it for all statements since we do it once permanently now */
for (stmt = 1; stmt <= statements; stmt++) {
if (statement[stmt].type != (char)a_)
continue; /* Not $a */
if (statement[stmt].mathStringLen < 2) continue;
/* Look at first symbol after variable type symbol */
if (mathToken[(statement[stmt].mathString)[1]].tokenType == (char)con_) {
firstConst[(statement[stmt].mathString)[1]] = 1; /* Set flag */
if (statement[stmt].mathStringLen == 2) {
oneConst[(statement[stmt].mathString)[1]] = 1; /* Set flag */
}
}
/* Look at last symbol */
if (mathToken[(statement[stmt].mathString)[
statement[stmt].mathStringLen - 1]].tokenType == (char)con_) {
lastConst[(statement[stmt].mathString)[
statement[stmt].mathStringLen - 1]] = 1; /* Set flag for const */
}
} /* Next stmt */
}
if (!reEntryFlag) {
/* First time called */
p = 0;
/* Collect the list of "unknown" variables */
/* (Pre-allocate max. length) */
/* (Note j and k assignment above) */
unkVars = NULL_NMBRSTRING;
nmbrLet(&unkVars, nmbrSpace(j + k));
unkVarsLen = 0;
for (i = 0; i < j; i++) {
if (schemeA[i] > mathTokens) {
/* It's an "unknown" variable */
breakFlag = 0;
for (m = 0; m < unkVarsLen; m++) {
if (unkVars[m] == schemeA[i]) {
/* It's already been added to the list */
breakFlag = 1;
}
}
if (!breakFlag) {
/* Add the new "unknown" var */
unkVars[unkVarsLen++] = schemeA[i];
}
}
}
/* Save the length of the list of unknown variables in schemeA */
schemeAUnkVarsLen = unkVarsLen;
for (i = 0; i < k; i++) {
if (schemeB[i] > mathTokens) {
/* It's an "unknown" variable */
breakFlag = 0;
for (m = 0; m < unkVarsLen; m++) {
if (unkVars[m] == schemeB[i]) {
/* It's already been added to the list */
breakFlag = 1;
}
}
if (!breakFlag) {
/* Add the new "unknown" var */
unkVars[unkVarsLen++] = schemeB[i];
}
}
}
/* Deallocate old (*stateVector) assignments */
if (pntrLen(*stateVector)) {
/*if (((nmbrString *)((*stateVector)[11]))[0] != -1) { */ /*???Change to nmbrLen?*/
/* If (*stateVector) not an empty nmbrString */
for (i = 4; i <= 7; i++) {
pntrTmpPtr = (pntrString *)((*stateVector)[i]);
for (j = 0; j < ((nmbrString *)((*stateVector)[11]))[0]; j++) {
nmbrLet((nmbrString **)(&pntrTmpPtr[j]),
NULL_NMBRSTRING);
}
pntrLet((pntrString **)(&(*stateVector)[i]),
NULL_PNTRSTRING);
}
for (i = 0; i <= 3; i++) {
nmbrLet((nmbrString **)(&(*stateVector)[i]),
NULL_NMBRSTRING);
}
for (i = 8; i <= 10; i++) {
nmbrLet((nmbrString **)(&(*stateVector)[i]),
NULL_NMBRSTRING);
}
k = pntrLen((pntrString *)((*stateVector)[12]));
for (i = 12; i < 16; i++) {
pntrTmpPtr = (pntrString *)((*stateVector)[i]);
for (j = 0; j < k; j++) {
nmbrLet((nmbrString **)(&pntrTmpPtr[j]),
NULL_NMBRSTRING);
}
pntrLet((pntrString **)(&(*stateVector)[i]),
NULL_PNTRSTRING);
}
/* Leave [11] pre-allocated to length 4 */
} else {
/* It was never allocated before -- do it now */
/* Allocate stateVector - it will be assigned upon exiting */
pntrLet(&(*stateVector), pntrPSpace(16));
nmbrLet((nmbrString **)(&(*stateVector)[11]), nmbrSpace(4));
}
/* Pre-allocate the (*stateVector) structure */
stackTop = -1;
stackUnkVar = NULL_NMBRSTRING;
stackUnkVarStart = NULL_NMBRSTRING;
stackUnkVarLen = NULL_NMBRSTRING;
stackSaveUnkVarStart = NULL_PNTRSTRING;
stackSaveUnkVarLen = NULL_PNTRSTRING;
stackSaveSchemeA = NULL_PNTRSTRING;
stackSaveSchemeB = NULL_PNTRSTRING;
unifiedScheme = NULL_NMBRSTRING;
nmbrLet(&stackUnkVar, nmbrSpace(unkVarsLen));
nmbrLet(&stackUnkVarStart, stackUnkVar);
nmbrLet(&stackUnkVarLen, stackUnkVar);
/* These next 4 hold pointers to nmbrStrings */
pntrLet(&stackSaveUnkVarStart, pntrNSpace(unkVarsLen));
pntrLet(&stackSaveUnkVarLen, stackSaveUnkVarStart);
pntrLet(&stackSaveSchemeA, stackSaveUnkVarStart);
pntrLet(&stackSaveSchemeB, stackSaveUnkVarStart);
for (i = 0; i < unkVarsLen; i++) {
/* Preallocate the stack space for these */
nmbrLet((nmbrString **)(&stackSaveUnkVarStart[i]),
stackUnkVar);
nmbrLet((nmbrString **)(&stackSaveUnkVarLen[i]),
stackUnkVar);
}
/* Set a flag that the "unknown" variables are not on the stack yet */
/* (Otherwise this will be the position on the stack) */
for (i = 0; i < unkVarsLen; i++) {
mathToken[unkVars[i]].tmp = -1;
}
} else { /* reEntryFlag != 0 */
/* We are re-entering to get the next possible assignment. */
/* Restore the (*stateVector) variables */
unkVarsLen = ((nmbrString *)((*stateVector)[11]))[0];
unkVars = (nmbrString *)((*stateVector)[0]);
stackTop = ((nmbrString *)((*stateVector)[11]))[1];
stackUnkVar = (nmbrString *)((*stateVector)[1]);
stackUnkVarStart = (nmbrString *)((*stateVector)[2]);
stackUnkVarLen = (nmbrString *)((*stateVector)[3]);
stackSaveUnkVarStart = (pntrString *)((*stateVector)[4]);
stackSaveUnkVarLen = (pntrString *)((*stateVector)[5]);
stackSaveSchemeA = (pntrString *)((*stateVector)[6]);
stackSaveSchemeB = (pntrString *)((*stateVector)[7]);
unifiedScheme = (nmbrString *)((*stateVector)[8]);
schemeAUnkVarsLen = ((nmbrString *)((*stateVector)[11]))[2];
/* Used by oneDirUnif() */
/* Set the location of the "unknown" variables on the stack */
/* (This may have been corrupted outside this function) */
for (i = 0; i < unkVarsLen; i++) {
mathToken[unkVars[i]].tmp = -1; /* Not on the stack */
}
for (i = 0; i <= stackTop; i++) {
mathToken[stackUnkVar[i]].tmp = i;
}
/* Force a backtrack to the next assignment */
goto backtrack;
reEntry1: /* goto backtrack will come back here if reEntryFlag is set */
reEntryFlag = 0;
}
/* Perform the unification */
scan:
/*E*/if(db6)print2("Entered scan: p=%ld\n",p);
/*E*/if(db6)print2("Enter scn sbA %s\n",nmbrCvtMToVString(schA));
/*E*/if(db6)print2("Enter scn sbB %s\n",nmbrCvtMToVString(schB));
/*E*/if(db6)let(&tmpStr,tmpStr);
while (schA[p] == schB[p] &&
schA[p + 1] != -1) {
p++;
}
/*E*/if(db6)print2("First mismatch: p=%ld\n",p);
if (schA[p] == mathTokens
|| schB[p] == mathTokens) {
/* One of the strings is at the end. */
if (schA[p] != schB[p]) {
/* But one is longer than the other. */
if (schA[p] <= mathTokens &&
schB[p] <= mathTokens) {
/* Neither token is an unknown variable. (Otherwise we might be able
to assign the unknown variable to a null string, thus making
the schemes match, so we shouldn't backtrack.) */
/*E*/if(db6)print2("Backtracked because end-of-string\n");
goto backtrack;
}
} else {
if (schA[p + 1] == -1) {
/* End of schA; a successful unification occurred */
goto done;
}
/* Otherwise, we are in the middle of several schemes being unified
simultaneously, so just continue. */
/* (mathTokens should be used by the caller to separate
schemes that are joined together for simultaneous unification) */
}
}
/* This test, combined with switchover to schemeB in backtrack,
prevents variable lockup, for example where
schemeA = ?1 B C, schemeB = ?2 A B C. Without this test, schemeB
becomes ?1 A B C, and then it can never match schemeA. This should be
checked out further: what about more than 2 variables? This kind of
"variable lockup" may be a more serious problem. */
/* A test case:
schemeA is $$ |- ( ( ?463 -> -. -. ph ) -> ( -. ph -> ( ph -> -. -. ph ) ) ).
schemeB is $$ |- ( ?464 -> ( ?465 -> ?464 ) ).
*/
if (schB[p] > mathTokens && schA[p] > mathTokens) {
/* Both scheme A and scheme B have variables in the match position.
Which one to use? */
/* If neither A nor B is on the stack, use A. Backtrack will put B
on the stack when A's possibilities are exhausted. */
/* If A is on the stack, use A. */
/* If B is on the stack, use B. */
/* If A and B are on the stack, bug. */
/* In other words: if B is not on the stack, use A. */
if (mathToken[schB[p]].tmp == -1) {
/* B is not on the stack */
goto schAUnk;
} else {
if (mathToken[schA[p]].tmp != -1) bug(1902); /* Both are on the stack */
goto schBUnk;
}
}
schBUnk:
if (schB[p] > mathTokens) {
/*E*/if(db6)print2("schB has unknown variable\n");
/* An "unknown" variable is in scheme B */
schemeAFlag = 0;
substToken = schB[p];
if (mathToken[substToken].tmp == -1) {
/* The "unknown" variable is not on the stack; add it */
stackTop++;
stackUnkVar[stackTop] = substToken;
mathToken[substToken].tmp = stackTop;
stackUnkVarStart[stackTop] = p;
/* Start with a variable length of 0 or 1 */
stackUnkVarLen[stackTop] = minSubstLen;
/* Save the rest of the current state for backtracking */
nmbrTmpPtr = (nmbrString *)(stackSaveUnkVarStart[stackTop]);
for (i = 0; i <= stackTop; i++) {
nmbrTmpPtr[i] = stackUnkVarStart[i];
}
nmbrTmpPtr = (nmbrString *)(stackSaveUnkVarLen[stackTop]);
for (i = 0; i <= stackTop; i++) {
nmbrTmpPtr[i] = stackUnkVarLen[i];
}
nmbrLet((nmbrString **)(&stackSaveSchemeA[stackTop]),
schA);
nmbrLet((nmbrString **)(&stackSaveSchemeB[stackTop]),
schB);
}
if (substToken != stackUnkVar[stackTop]) {
print2("PROGRAM BUG #1903\n");
print2("substToken is %s\n", mathToken[substToken].tokenName);
print2("stackTop %ld\n", stackTop);
print2("p %ld stackUnkVar[stackTop] %s\n", p,
mathToken[stackUnkVar[stackTop]].tokenName);
print2("schA %s\nschB %s\n", nmbrCvtMToVString(schA),
nmbrCvtMToVString(schB));
bug(1903);
}
nmbrLet(&substitution, nmbrMid(schA, p + 1,
stackUnkVarLen[stackTop]));
goto substitute;
}
schAUnk:
if (schA[p] > mathTokens) {
/*E*/if(db6)print2("schA has unknown variable\n");
/* An "unknown" variable is in scheme A */
schemeAFlag = 1;
substToken = schA[p];
if (mathToken[substToken].tmp == -1) {
/* The "unknown" variable is not on the stack; add it */
stackTop++;
stackUnkVar[stackTop] = substToken;
mathToken[substToken].tmp = stackTop;
stackUnkVarStart[stackTop] = p;
/* Start with a variable length of 0 or 1 */
stackUnkVarLen[stackTop] = minSubstLen;
/* Save the rest of the current state for backtracking */
nmbrTmpPtr = (nmbrString *)(stackSaveUnkVarStart[stackTop]);
for (i = 0; i <= stackTop; i++) {
nmbrTmpPtr[i] = stackUnkVarStart[i];
}
nmbrTmpPtr = (nmbrString *)(stackSaveUnkVarLen[stackTop]);
for (i = 0; i <= stackTop; i++) {
nmbrTmpPtr[i] = stackUnkVarLen[i];
}
nmbrLet((nmbrString **)(&stackSaveSchemeA[stackTop]),
schA);
nmbrLet((nmbrString **)(&stackSaveSchemeB[stackTop]),
schB);
}
if (substToken != stackUnkVar[stackTop]) {
/*E*/print2("PROGRAM BUG #1904\n");
/*E*/print2("\nsubstToken is %s\n",mathToken[substToken].tokenName);
/*E*/print2("stack top %ld\n",stackTop);
/*E*/print2("p %ld stUnV[stakTop] %s\n",p,
/*E*/mathToken[stackUnkVar[stackTop]].tokenName);
/*E*/print2("schA %s\nschB %s\n",nmbrCvtMToVString(schA),nmbrCvtMToVString(schB));
bug(1904);
}
nmbrLet(&substitution, nmbrMid(schB, p + 1,
stackUnkVarLen[stackTop]));
goto substitute;
}
/* Neither scheme has an unknown variable; unification with current assignment
failed, so backtrack */
/*E*/if(db6)print2("Neither scheme has unknown variable\n");
goto backtrack;
substitute:
/*E*/if(db6)print2("Entering substitute...\n");
/*E*/for (d = 0; d <= stackTop; d++) {
/*E*/ if(db6)print2("Unknown var %ld is %s.\n",d,
/*E*/ mathToken[stackUnkVar[d]].tokenName);
/*E*/ if(db6)print2(" Its start is %ld; its length is %ld.\n",
/*E*/ stackUnkVarStart[d],stackUnkVarLen[d]);
/*E*/}
/* Subst. all occurrences of substToken with substitution in schA and schB */
/*???We could speed things up by making substitutions before the pointer to
???to the unifiedScheme only, and keeping track of 3 pointers (A, B, &
???unified schemes); unifiedScheme would hold only stuff before pointer */
/* First, we must make sure that the substToken doesn't occur in the
substutition. */
if (nmbrElementIn(1, substitution, substToken)) {
/*E*/if(db6)print2("Substituted token occurs in substitution string\n");
goto backtrack;
}
/* Next, we must make sure that the end of string doesn't occur in the
substutition. */
/* (This takes care of the case where the unknown variable aligns with
end of string character; in this case, only a null substitution is
permissable. If the substitution length is 1 or greater, this "if"
statement will detect it.) */
if (substitution[0] == mathTokens) {
/*E*/if(db6)print2("End of string token occurs in substitution string\n");
/* We must pop the stack here rather than in backtrack, because we
are already one token beyond the end of a scheme, and backtrack
would therefore test one token beyond that, missing the fact that
the substitution has overflowed beyond the end of a scheme. */
/* Set the flag that it's not on the stack and pop stack */
mathToken[stackUnkVar[stackTop]].tmp = -1;
stackTop--;
goto backtrack;
}
/************* Start of 26-Sep-2010 *************/
/* Bracket matching is customized to set.mm to result in fewer ambiguous
unifications. */
/* 26-Sep-2010 nm Automatically disable bracket matching if any $a has
unmatched brackets */
/* The static variable bracketMatchInit tells us to check all $a's
if it is 0; if 1, skip the $a checking. Make sure that the RESET
command sets bracketMatchInit=0. */
/* ??? To do: put individual bracket type checks into a loop or
function call for code efficiency (but don't slow down program); maybe
read the bracket types to check from a list; maybe refine so that only
the mismatched bracket types found in the $a scan are skipped, but
matched one are not */
for (i = bracketMatchInit; i <= 1; i++) {
/* This loop has 2 passes (0 and 1) if bracketMatchInit=0 to set
bracketMatchOn = 0 or 1, and 1 pass otherwise */
bracketMismatchFound = 0; /* Don't move down; needed for break below */
if (bracketMatchInit == 0) { /* Initialization pass */
if (i != 0) bug(1908);
/* Scan all ($a) statements */
bracketScanStart = 1;
bracketScanStop = statements;
} else { /* Normal pass */
if (i != 1) bug(1909);
if (!bracketMatchOn) break; /* Skip the whole bracket check because a
mismatched bracket was found in some $a in the initialization pass */
/* Set dummy parameters to force a single loop pass */
bracketScanStart = 0;
bracketScanStop = 0;
}
for (m = bracketScanStart; m <= bracketScanStop; m++) {
if (bracketMatchInit == 0) { /* Initialization pass */
if (statement[m].type != a_) continue;
nmbrTmpPtr = statement[m].mathString;
} else { /* Normal pass */
nmbrTmpPtr = substitution;
}
j = nmbrLen(nmbrTmpPtr);
/* Make sure left and right parentheses match */
pairingMismatches = 0; /* Counter of parens: + for "(" and - for ")" */
for (k = 0; k < j; k++) {
mToken = mathToken[nmbrTmpPtr[k]].tokenName;
if (mToken[0] == '(' && mToken[1] == 0 ) {
pairingMismatches++;
} else if (mToken[0] == ')' && mToken[1] == 0 ) {
pairingMismatches--;
if (pairingMismatches < 0) break; /* Detect wrong order */
}
} /* Next k */
if (pairingMismatches != 0) {
bracketMismatchFound = 1;
break;
}
/* Make sure left and right braces match */
pairingMismatches = 0; /* Counter of braces: + for "{" and - for "}" */
for (k = 0; k < j; k++) {
mToken = mathToken[nmbrTmpPtr[k]].tokenName;
if (mToken[0] == '{' && mToken[1] == 0 ) pairingMismatches++;
else
if (mToken[0] == '}' && mToken[1] == 0 ) {
pairingMismatches--;
if (pairingMismatches < 0) break; /* Detect wrong order */
}
} /* Next k */
if (pairingMismatches != 0) {
bracketMismatchFound = 1;
break;
}
/* Make sure left and right brackets match */ /* Added 12-Nov-05 nm */
pairingMismatches = 0; /* Counter of brackets: + for "[" and - for "]" */
for (k = 0; k < j; k++) {
mToken = mathToken[nmbrTmpPtr[k]].tokenName;
if (mToken[0] == '[' && mToken[1] == 0 )
pairingMismatches++;
else
if (mToken[0] == ']' && mToken[1] == 0 ) {
pairingMismatches--;
if (pairingMismatches < 0) break; /* Detect wrong order */
}
} /* Next k */
if (pairingMismatches != 0) {
bracketMismatchFound = 1;
break;
}
/* Make sure left and right triangle brackets match */
pairingMismatches = 0; /* Counter of brackets: + for "<.", - for ">." */
for (k = 0; k < j; k++) {
mToken = mathToken[nmbrTmpPtr[k]].tokenName;
if (mToken[1] == 0) continue;
if (mToken[0] == '<' && mToken[1] == '.' && mToken[2] == 0 )
pairingMismatches++;
else
if (mToken[0] == '>' && mToken[1] == '.' && mToken[2] == 0 ) {
pairingMismatches--;
if (pairingMismatches < 0) break; /* Detect wrong order */
}
} /* Next k */
if (pairingMismatches != 0) {
bracketMismatchFound = 1;
break;
}
/* Make sure underlined brackets match */ /* Added 12-Nov-05 nm */
pairingMismatches = 0; /* Counter of brackets: + for "[_", - for "]_" */
for (k = 0; k < j; k++) {
mToken = mathToken[nmbrTmpPtr[k]].tokenName;
if (mToken[1] == 0) continue;
if (mToken[0] == '[' && mToken[1] == '_' && mToken[2] == 0 )
pairingMismatches++;
else
if (mToken[0] == ']' && mToken[1] == '_' && mToken[2] == 0 ) {
pairingMismatches--;
if (pairingMismatches < 0) break; /* Detect wrong order */
}
} /* Next k */
if (pairingMismatches != 0) {
bracketMismatchFound = 1;
break;
}
} /* next m */
if (bracketMatchInit == 0) { /* Initialization pass */
/* We've finished the one-time $a scan. Set flags accordingly. */
if (bracketMismatchFound) { /* Some $a has a bracket mismatch */
if (m < 1 || m > statements) bug(1910);
printLongLine(cat("The bracket matching unification heuristic was",
" turned off for this database because of a bracket mismatch in",
" statement \"",
/* (m should be accurate due to break above) */
statement[m].labelName,
"\".", NULL),
" ", " ");
/*
printLongLine(cat("The bracket matching unification heuristic was",
" turned off for this database.", NULL),
" ", " ");
*/
bracketMatchOn = 0; /* Turn off static flag for this database */
} else { /* Normal pass */
bracketMatchOn = 1; /* Turn it on */
}
/*E*/if(db6)print2("bracketMatchOn = %ld\n", (long)bracketMatchOn);
bracketMatchInit = 1; /* We're done with the one-time $a scan */
}
} /* next i */
if (bracketMismatchFound) goto backtrack;
/************* End of 26-Sep-2010 *************/
j = nmbrLen(substitution);
/* 8/29/99 - Quick scan to reject some impossible unifications: If the
first symbol in a substitution is a constant, it must match
the 2nd constant of some earlier $a statement (e.g. "<" matches
"class <", "Ord (/)" matches "class Ord A"). Same applies to
last symbol. */
/* 10/12/02 - This prefilter is too aggressive when empty substitutions
are allowed. Therefore added "minSubstLen > 0" to fix miu.mm theorem1
Proof Assistant failure reported by Josh Purinton. */
if (j/*subst len*/ > 0 && minSubstLen > 0) {
impossible = 0;
if (mathToken[substitution[0]].tokenType == (char)con_) {
if (!firstConst[substitution[0]]
|| (j == 1 && !oneConst[substitution[0]])) {
impossible = 1;
}
}
if (mathToken[substitution[j - 1]].tokenType == (char)con_) {
if (!lastConst[substitution[j - 1]]) {
impossible = 1;
}
}
if (impossible) {
/*E*/if(db6)print2("Impossible subst: %s\n", nmbrCvtMToVString(substitution));
goto backtrack;
}
}
/* Now perform the substitutions */
/*E*/if(db6)print2("Substitution is '%s'\n",nmbrCvtMToVString(substitution));
k = 1;
while (1) {
/* Perform the substitutions into scheme A */
k = nmbrElementIn(k, schA, substToken);
if (!k) break;
if (schemeAFlag) {
/* The token to be substituted was in scheme A */
/* Adjust position and earlier var. starts and lengths */
if (k - 1 <= p) {
if (k <= p) {
/* Adjust assignments in stack */
for (i = 0; i <= stackTop; i++) {
if (k - 1 < stackUnkVarStart[i]) {
stackUnkVarStart[i] = stackUnkVarStart[i] + j-1;
} else {
if (k <= stackUnkVarStart[i] +
stackUnkVarLen[i]) {
stackUnkVarLen[i] = stackUnkVarLen[i] + j - 1;
}
}
}
}
p = p + j - 1; /* Adjust scan position */
/*E*/if(db6)print2("Scheme A adjusted p=%ld\n",p);
}
}
nmbrLet(&schA, nmbrCat(
nmbrLeft(schA, k - 1), substitution, nmbrRight(schA, k + 1), NULL));
k = k + j - 1;
}
k = 1;
while (1) {
/* Perform the substitutions into scheme B */
k = nmbrElementIn(k, schB, substToken);
if (!k) break;