-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathprover.js
1802 lines (1704 loc) · 72.2 KB
/
prover.js
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
function Prover(initFormulas, parser, accessibilityConstraints) {
/**
* A Prover object collects functions and properties used to find either a
* tableau proof or a countermodel. <initFormulas> is the list of formulas
* with which the tableau begins (all premises, if any, plus the negated
* conclusion); <parser> is the Parser object used to parse these formulas;
* <accessibilityConstraints> is a list of words like 'reflexivity'
* determinining the relevant modal system.
*/
log("*** initializing prover");
parser = parser.copy();
this.parser = parser;
this.initFormulas = initFormulas; // formulas as entered, with conclusion negated
this.initFormulasNonModal = initFormulas;
this.accessibilityRules = [];
if (parser.isModal) {
// convert modal formulas into two-sorted first-order formulas:
this.initFormulasNonModal = initFormulas.map(function(f) {
return parser.translateFromModal(f);
});
if (accessibilityConstraints) {
this.s5 = accessibilityConstraints.includes('universality');
if (!this.s5) {
this.accessibilityRules = accessibilityConstraints.map(function(s) {
return Prover[s];
});
}
}
}
this.initFormulasNNF = this.initFormulasNonModal.map(function(f){
return f.nnf();
});
// These are the formulas that we'll use on the internal tableaux.
log('initFormulas in NNF: '+this.initFormulasNNF);
// init tableau prover:
this.pauseLength = 5; // ms pause between calculations
log('increasing pauseLength to '+(this.pauseLength = 10));
this.computationLength = 20; // ms before setTimeout pause
this.step = 0; // counter of calculation steps
this.tree = new Tree(this);
this.depthLimit = 2; // how far to explore a tree before backtracking
this.alternatives = [this.tree]; // alternative trees for backtracking
this.curAlternativeIndex = 0;
this.tree.addInitNodes(this.initFormulasNNF)
// init modelfinder:
log("initializing modelfinder")
var mfParser = parser.copy();
if (accessibilityConstraints) {
var name2fla = {
"universality": "∀v∀uRvu",
"reflexivity": "∀vRvv",
"symmetry": "∀v∀u(Rvu→Ruv)",
"transitivity": "∀v∀u∀t(Rvu→(Rut→Rvt))",
"euclidity": "∀v∀u∀t(Rvu→(Rvt→Rut))",
"seriality": "∀v∃uRvu"
};
var accessibilityFormluas = accessibilityConstraints.map(function(s) {
return mfParser.parseAccessibilityFormula(name2fla[s]).nnf();
});
// todo: strip redundant constraints
this.modelfinder = new ModelFinder(
this.initFormulasNNF,
mfParser,
accessibilityFormluas,
this.s5
);
}
else {
this.modelfinder = new ModelFinder(this.initFormulasNNF, mfParser);
}
this.counterModel = null;
this.start = function() {
this.lastBreakTime = performance.now();
this.nextStep();
};
this.stop = function() {
this.stopTimeout = true;
};
this.onfinished = function(treeClosed) {}; // to be overwritten
this.status = function(str) {}; // to be overwritten
}
Prover.prototype.nextStep = function() {
/**
* expand the next node on the left-most open branch; initializes
* backtracking if limit is reached; also search for a countermodel. This
* function calls itself again until the proof is complete.
*/
this.step++;
log('*** prover step '+this.step+' alternative '+this.curAlternativeIndex+' (max '+(this.alternatives.length-1)+')');
log(this.tree);
if (this.tree.openBranches.length == 0) {
log('tree closed');
return this.onfinished(1);
}
this.status('step '+this.step+' alternative '+this.curAlternativeIndex+', '
+this.tree.numNodes+' nodes, model size '
+this.modelfinder.model.domain.length
+(this.tree.parser.isModal ? '/'+this.modelfinder.model.worlds.length : ''));
if (this.limitReached()) {
log(" * limit "+this.depthLimit+" reached");
if (this.curAlternativeIndex < this.alternatives.length-1) {
this.curAlternativeIndex++;
log(" * trying stored alternative");
}
else {
// this.depthLimit += Math.ceil(this.alternatives.length/20);
this.depthLimit += 2 + Math.floor(this.step/500);
this.curAlternativeIndex = 0;
log(" * increasing to "+this.depthLimit);
}
this.tree = this.alternatives[this.curAlternativeIndex];
return this.nextStep(); // need to check if alternative is also at limit
}
var todo = this.tree.openBranches[0].todoList.shift();
if (todo) {
log(this.step+'. Expanding '+todo.args+' on alternative '+this.curAlternativeIndex, 'for debug=trace');
todo.nextRule(this.tree.openBranches[0], todo.args);
}
else if (this.alternatives.length) {
// If we reason with equality, todoList may be empty even though the tree isn't finished
// because we consider trees without equality reasoning.
log("nothing left to do");
this.discardCurrentAlternative();
}
// search for a countermodel:
if (this.modelfinder.nextStep()) {
this.counterModel = this.modelfinder.model;
return this.onfinished(0);
}
var timeSinceBreak = performance.now() - this.lastBreakTime;
if (this.stopTimeout) {
// proof manually interrupted
this.stopTimeout = false;
}
else if (this.pauseLength && timeSinceBreak > this.computationLength) {
// continue with next step after short break to display status message
// and not get killed by browsers
setTimeout(function(){
this.lastBreakTime = performance.now();
this.nextStep();
}.bind(this), this.pauseLength+this.tree.numNodes/1000);
}
else {
this.nextStep();
}
}
Prover.prototype.limitReached = function() {
/**
* check if the current tree has been explored up to depthLimit
*/
var complexity = this.tree.getNumNodes() - this.tree.priority;
if (this.tree.openBranches[0].todoList[0] &&
this.tree.openBranches[0].todoList[0].nextRule == Prover.equalityReasoner) {
if (!this.equalityComputationStep) this.equalityComputationStep = 1;
else if (++this.equalityComputationStep == 100) {
this.equalityComputationStep = 0;
return true;
}
}
// complexity += this.curAlternativeIndex/100;
return complexity >= this.depthLimit;
}
Prover.prototype.useTree = function(tree, index) {
/**
* replace currently active tree alternative by <tree>; if <index> is given,
* inserts <tree> at <index> in this.alternatives
*/
if (index !== undefined) {
this.alternatives.splice(index, 0, tree);
this.curAlternativeIndex = index;
}
else {
this.alternatives[this.curAlternativeIndex] = tree;
}
this.tree = tree
}
Prover.prototype.switchToAlternative = function(altTree) {
/**
* continue proof search with <altTree> (member of this.alternatives)
*/
this.curAlternativeIndex = this.alternatives.indexOf(altTree);
this.tree = this.alternatives[this.curAlternativeIndex];
}
Prover.prototype.storeAlternatives = function(altTrees) {
/**
* add <altTrees> as alternatives to be explored after the current tree;
* remove redundant alternatives
*/
log("storing "+altTrees.length+" alternatives");
var insertPosition = this.curAlternativeIndex+1;
for (var i=0; i<altTrees.length; i++) {
this.alternatives.splice(insertPosition, 0, altTrees[i]);
this.pruneAlternatives(altTrees[i]);
if (!altTrees[i].removed) {
insertPosition++;
}
}
log("There are now "+this.alternatives.length+' alternatives');
log(this.alternatives.map(function(t,i) { return "alternative "+i+":"+t }).join('<br>'));
}
Prover.prototype.pruneAlternatives = function(tree) {
/**
* discard elements in this.alternatives that have become redundant after <tree>
* has been added or altered; might remove <tree> itself if it is found redundant;
* removed trees get attribute 'removed'
*/
log("pruning alternatives after adding<br>"+tree);
for (var i=0; i<this.alternatives.length; i++) {
if (this.alternatives[i] == tree) continue;
var keepWhich = this.keepWhichTree(tree, this.alternatives[i]);
var keepTree = keepWhich[0];
var keepAlt = keepWhich[1];
if (!keepTree) {
log("removing tree<br>"+tree+"<br>in favour of alternative<br>"+this.alternatives[i]);
this.removeAlternative(this.alternatives.indexOf(tree));
return;
}
else if (!keepAlt) {
log("removing alternative<br>"+this.alternatives[i]+"<br>in favour of<br>"+tree);
this.removeAlternative(i);
i--;
}
}
}
Prover.prototype.keepWhichTree = function(tree, altTree) {
/**
* compare <tree> and <altTree> from this.alternatives for redundancy; return
* [Boolean1, Boolean2], where Boolean1 indicates if <tree> should be kept and
* Boolean2 if <altTree> should be kept.
*/
// We first check if the trees have the same open branches and the same todo
// items, in which case it is pointless to keep both.
if (altTree.string == tree.string) { // same open branches
if (tree.openBranches[0].todoList[0].nextRule != altTree.openBranches[0].todoList[0].nextRule ||
tree.openBranches[0].todoList[0].args != altTree.openBranches[0].todoList[0].args) {
// different todo items
return [true, true];
}
else if (tree.numNodes < altTree.numNodes) {
log('alternative has same open branches and is larger; removing');
return [true, false];
}
else {
log('tree has same open branches as alternative; removing');
return [false, true];
}
}
// Next, we check if one tree's open branches are a proper subset of the
// other's. This often happens if tree1 has managed to close a branch which
// tree2 tries to tackle in a different way. No point keeping tree2.
var tol = tree.openBranches.length;
var atol = altTree.openBranches.length;
if (tol == 0) {
log('tree has no open branches; removing alternative');
return [true, false];
}
if (atol == 0) {
log('alternative has no open branches; removing tree');
return [false, true];
}
// We compare the open branches from the end.
var offset = 1;
while (tree.openBranches[tol-offset].string == altTree.openBranches[atol-offset].string) {
if (offset == tol) {
// We've exhausted the open branches on <tree> and found all of them on <altTree>.
log('alternative has further open branches; removing');
return [true, false];
}
else if (offset == atol) {
// We've exhausted the open branches on <altTree> and found all of them on <tree>.
log('tree has further open branches; removing');
return [false, true];
}
offset++;
}
// If we're here, the open branches at index -<offset> don't match. We now
// check if one tree is a further developed extension of the other, in which
// case we only keep the one that's further developed.
tbranch = tree.openBranches[tol-offset];
atbranch = altTree.openBranches[atol-offset];
if ((offset == atol || offset == tol) &&
tbranch.string.startsWith(atbranch.string) &&
atbranch.todoList[0].nextRule != Prover.equalityReasoner) {
// Why these three checks? Start with the second. <tbranch> and
// <atbranch> are the first non-matching open branches, from the right.
// We check that <tbranch> extends <atbranch>, indicating that <tree>
// might be a further developed version of <altTree>. One complication
// is that we sometimes store alternative ways of expanding the same
// open branch, but this only happens if the stored alternative tries to
// close the tree by equalityReasoning. The third condition checks that
// we don't have this kind of case. The first condition replaces
// checking any further open branches. Note that any open branch to the
// left of <tbranch> on <tree> must also extend <atbranch>. For suppose
// there's some branch to left of <tbranch> on <tree>. Then <tbranch>
// ends with the result beta2 of a beta expansion. And then <atbranch>
// is at most the trunk of <tbranch> before that beta expansion, and so
// any branch to the left of <tbranch> also extends <atbranch>. But we
// can't yet infer that <tree> extends <altTree>, as there might be open
// branches to the left of <atbranch> on <altTree> that aren't extended
// by anything on <tree>. Suppose there is some open branch to the left
// of <atbranch>. Then <atbranch> ends with the result beta2 of a beta
// expansion. If <tree> extends <altTree> then it also contains that
// beta expansion, and it will only work on the beta2 branch after
// closing the beta1 branch. We know that <tree> extends the beta2
// branch. So if it extends <altTree> then there must be no open branch
// to the left of <tbranch>. Thus we check that *if* there is an open
// branch to the left of <atbranch> *then* there is none to the left of
// <tbranch>. Equivalently: either <atbranch> is the first open branch
// or <tbranch> is the first open branch. (Technically, while this is
// necessary for <tree> to extend <altTree>, it isn't sufficient. For
// example, suppose the relevant beta expansion on <altTree> expands
// pvq, and <tree> doesn't expand pvq but instead expands a conjunction
// p&q. Then <tbranch> ends in ...,p,q and <atbranch> in ...,q; if there
// are no other open branches on <tree>, we'll wrongly find that <tree>
// extends <altTree>. In practice, I don't think this situation can
// arise because alpha expansions are never alternatives to beta
// expansions.)
log('alternative is less developed than tree; removing');
return [true, false];
}
else if ((offset == tol || offset == atol) &&
atbranch.string.startsWith(tbranch.string) &&
tbranch.todoList[0].nextRule != Prover.equalityReasoner) {
log('tree is less developed than alternative; removing');
return [false, true];
}
return [true, true];
}
Prover.prototype.removeAlternative = function(index) {
/**
* remove alternative at position <index> from this.alternatives
*/
log("removing alternative "+index+" of "+this.alternatives.length);
this.alternatives[index].removed = true;
if (index == this.curAlternativeIndex) {
this.discardCurrentAlternative();
}
else {
this.alternatives.splice(index, 1);
if (index < this.curAlternativeIndex) {
this.curAlternativeIndex--;
}
}
}
Prover.prototype.discardCurrentAlternative = function() {
/**
* remove current tree from this.alternatives and move to next one
*/
this.alternatives.splice(this.curAlternativeIndex, 1);
if (this.curAlternativeIndex == this.alternatives.length) {
this.curAlternativeIndex = 0;
}
log("discarding current alternative; switching to alternative "+this.curAlternativeIndex);
if (this.alternatives.length) {
// don't make this.tree undefined if there are no more alternatives
this.tree = this.alternatives[this.curAlternativeIndex];
}
}
/**
* What follows are functions corresponding to individual tableau expansion steps.
*/
Prover.alpha = function(branch, nodeList) {
/**
* expand a conjunction-type node; <nodeList> is a list because some rules
* apply to more than one node. Not the alpha rule, so here <nodeList> has
* just one member.
*/
log('alpha '+nodeList[0]);
var node = nodeList[0];
var subnode1 = new Node(node.formula.sub1, Prover.alpha, nodeList);
var subnode2 = new Node(node.formula.sub2, Prover.alpha, nodeList);
branch.addNode(subnode1, 'addEvenIfDuplicate');
branch.addNode(subnode2, 'addEvenIfDuplicate');
branch.tryClose(subnode1);
if (!branch.closed) branch.tryClose(subnode2);
}
Prover.alpha.priority = 1;
Prover.alpha.toString = function() { return 'alpha' }
Prover.beta = function(branch, nodeList) {
/**
* expand a disjunction-type node
*/
log('beta '+nodeList[0]);
var node = nodeList[0];
var newbranch = branch.copy();
branch.tree.openBranches.unshift(newbranch);
// try to tackle simpler branch first:
var re = /[∧∨↔→∀∃□◇]/g;
var complexity1 = (node.formula.sub1.string.match(re) || []).length;
var complexity2 = (node.formula.sub2.string.match(re) || []).length;
if (complexity2 < complexity1) {
var subnode1 = new Node(node.formula.sub1, Prover.beta, nodeList);
var subnode2 = new Node(node.formula.sub2, Prover.beta, nodeList);
}
else {
var subnode2 = new Node(node.formula.sub1, Prover.beta, nodeList);
var subnode1 = new Node(node.formula.sub2, Prover.beta, nodeList);
}
branch.addNode(subnode1, 'addEvenIfDuplicate');
newbranch.addNode(subnode2, 'addEvenIfDuplicate');
branch.tryClose(subnode1, 'dontPruneAlternatives');
newbranch.tryClose(subnode2);
}
Prover.beta.priority = 9;
Prover.beta.toString = function() { return 'beta' }
Prover.gamma = function(branch, nodeList, matrix) {
/**
* expand an ∀ type node; <matrix> is set when this is called from modalGamma
* for S5 trees, see modalGamma() below.
*/
log('gamma '+nodeList[0]);
var fromModalGamma = (matrix != undefined);
var node = nodeList[0];
var newVariable = branch.newVariable(matrix);
var matrix = matrix || node.formula.matrix;
var newFormula = matrix.substitute(node.formula.variable, newVariable);
var newNode = new Node(newFormula, Prover.gamma, nodeList);
// NB: The last line sets fromRule to gamma even for s5 modalGamma nodes
newNode.instanceTerm = newVariable; // used in sentree
branch.addNode(newNode);
branch.tryClose(newNode);
// add application back onto todoList:
if (!fromModalGamma && newNode.type != 'gamma') {
// When expanding ∀x∀y∀zφ, we add ∀y∀zφ and ∀zφ to the branch, all of
// which can be expanded again and again, leading to lots of useless
// variations of φ. So we only add the outermost sentence back to the
// list, and only after the innermost was expanded, so that we first
// expand φ before expanding ∀x∀y∀zφ again.
var outer = node;
while (outer.fromRule == Prover.gamma) outer = outer.fromNodes[0];
var priority = 9;
// add to very end of todoList, so that we first expand all other nodes:
branch.todoList.push(Prover.makeTodoItem(Prover.gamma, [outer], priority));
}
}
Prover.gamma.priority = 8;
Prover.gamma.toString = function() { return 'gamma' }
Prover.modalGamma = function(branch, nodeList) {
/**
* expand a (translated) node of type □A
*
* □A and ¬◇A nodes are translated into ∀x(¬wRxvAx) and ∀x(¬wRx∨¬Ax). By the
* standard gamma rule, these would be expanded to ¬wRξ7 ∨ Aξ7 or ¬wRξ7 ∨
* ¬Aξ7. We don't want the resulting branches on the tree. See readme.org
*/
log('modalGamma '+nodeList[0]);
var node = nodeList[0];
if (branch.tree.prover.s5) {
// In S5, we still translate □A into ∀x(¬wRxvAx) rather than ∀xAx.
// That's because the latter doesn't tell us at which world the formula
// is evaluated ('w'), which makes it hard to translate back into
// textbook tableaux. (Think about the tableau for ◇□A→□A.) But when we
// expand the □A node, we ignore the accessibility clause. Instead, we
// expand ∀x(¬wRx∨Ax) to Aξ1 and use the free-variable machinery.
branch.todoList.push(Prover.makeTodoItem(Prover.modalGamma, nodeList));
return Prover.gamma(branch, nodeList, node.formula.matrix.sub2);
}
var wRx = node.formula.matrix.sub1.sub;
log('looking for '+wRx.predicate+wRx.terms[0]+'* nodes');
// find wR* node for □A expansion:
OUTERLOOP:
for (var i=0; i<branch.literals.length; i++) {
var wRy = branch.literals[i].formula;
if (wRy.predicate == wRx.predicate && wRy.terms[0] == wRx.terms[0]) {
log('found '+wRy);
// check if <node> has already been expanded with this wR* node:
for (var j=0; j<branch.nodes.length; j++) {
if (branch.nodes[j].fromRule == Prover.modalGamma &&
branch.nodes[j].fromNodes[0] == node &&
branch.nodes[j].fromNodes[1] == branch.literals[i]) {
log('already used');
continue OUTERLOOP;
}
}
// expand <node> with found wR*:
var modalMatrix = node.formula.matrix.sub2;
var v = wRy.terms[1];
log('expanding: '+node.formula.variable+' => '+v);
var newFormula = modalMatrix.substitute(node.formula.variable, v);
var newNode = new Node(newFormula, Prover.modalGamma, [node, branch.literals[i]]);
newNode.instanceTerm = v;
if (branch.addNode(newNode)) {
branch.tryClose(newNode);
// immediately expand with other eligible wR* nodes
branch.todoList.unshift(Prover.makeTodoItem(Prover.modalGamma, nodeList));
return;
}
}
}
// No wR* node found. Add to end of todolist:
branch.todoList.push(Prover.makeTodoItem(Prover.modalGamma, nodeList));
}
Prover.modalGamma.priority = 8;
Prover.modalGamma.toString = function() { return 'modalGamma' }
Prover.delta = function(branch, nodeList, matrix) {
/**
* expand an ∃ type node; <matrix> is set when this is called from modalDelta
* for S5 trees, see modalDelta() below.
*/
log('delta '+nodeList[0]);
var node = nodeList[0];
var fla = node.formula;
// find skolem term:
var funcSymbol = branch.tree.newFunctionSymbol(matrix);
// It suffices to skolemize on variables contained in this formula. This
// makes some proofs much faster by making some gamma applications
// redundant. However, translation into sentence tableau then becomes almost
// impossible, because here we need the missing gamma applications. Consider
// ∀x(Fx & ∃y¬Fy).
if (branch.freeVariables.length > 0) {
if (branch.tree.prover.s5) {
// branch.freeVariables contains world and individual variables
var skolemTerm = branch.freeVariables.filter(function(v) {
return v[0] == (matrix ? 'ζ' : 'ξ');
});
}
else {
var skolemTerm = branch.freeVariables.copy();
}
skolemTerm.unshift(funcSymbol);
}
else {
var skolemTerm = funcSymbol;
}
var matrix = matrix || node.formula.matrix;
var newFormula = matrix.substitute(node.formula.variable, skolemTerm);
var newNode = new Node(newFormula, Prover.delta, nodeList);
newNode.instanceTerm = skolemTerm;
branch.addNode(newNode);
branch.tryClose(newNode);
}
Prover.delta.priority = 2;
Prover.delta.toString = function() { return 'delta' }
Prover.modalDelta = function(branch, nodeList) {
/**
* expand a (translated) node of type ◇A
*/
log('modalDelta '+nodeList[0]);
var node = nodeList[0]; // a node of type ∃x(wRx∧Ax)
if (branch.tree.prover.s5) {
// In S5, we still translate ◇A into ∃x(wRx∧Ax) rather than ∃xAx. That's
// because the latter doesn't tell us at which world the formula is
// evaluated ('w'), which makes it hard to translate back into textbook
// tableaux. (Think about the tableau for ◇□A→□A.) But when we expand
// the ◇A node, we ignore the accessibility clause. Instead, we expand
// ∃x(wRx∧Ax) to Aφ, where φ is a suitable skolem term, just like for
// ordinary existential formulas.
return Prover.delta(branch, nodeList, node.formula.matrix.sub2);
}
var fla = node.formula;
// don't need skolem terms (see readme.org):
var newWorldName = branch.tree.newWorldName();
// The instance formula would be wRu∧Au. We immediately expand the
// conjunction to conform to textbooks modal rules:
var fla1 = node.formula.matrix.sub1.substitute(node.formula.variable, newWorldName);
var fla2 = node.formula.matrix.sub2.substitute(node.formula.variable, newWorldName);
var newNode1 = new Node(fla1, Prover.modalDelta, nodeList); // wRu
var newNode2 = new Node(fla2, Prover.modalDelta, nodeList); // Au
newNode2.instanceTerm = newWorldName;
branch.addNode(newNode1);
branch.addNode(newNode2);
branch.tryClose(newNode2);
}
Prover.modalDelta.priority = 2;
Prover.modalDelta.toString = function() { return 'modalDelta' }
Prover.literal = function(branch, nodeList) {
/**
* check if <branch> can be closed by applying a substitution, involving the
* newly added literal(s) <nodeList> as either the member of a complementary
* pair or as a self-complementary node ¬(Φ=Φ); also schedule the equality
* reasoner that tries to close the branch by calling LL* on any equality
* problem that newly arises on the branch by the addition of <nodeList>.
*
* There may be several options for closing the branch, with different
* substitutions. We need to try them all. It may also be better to not close
* the branch at all and instead continue expanding nodes. So we collect all
* possible unifiers, try the first and store alternative trees for later
* exploration with backtracking.
*
* (Sometimes a branch can be closed both by unifying a complementary pair
* and by turning a new node into a self-complementary node. For example,
* if the branch contains g(f(x1)) = x1 and the new node is ¬(x2=a), we can
* unify the two formulas with x1->a, x2->g(f(a)), but we can also close the
* branch by solving the trivial equality problem [] |- x2=a, whose solution
* is x2->a.)
*
* (Why have a special rule/step for this, rather than calling it from
* tryClose, in the same step in which the new node is added? Because we
* would then create the relevant EqualityProblems on non-active branches
* of a beta expansion. By the time we get to work on them, those
* branches might have changed by applying a substitution that closed
* the earlier active branch. As a result the created EqualityProblems
* could involve equations or terms that are no longer on the branch.)
*/
var tree = branch.tree;
var prover = tree.prover;
// collect unifiers that would allow closing the branch:
var unifiers = [];
for (var i=0; i<nodeList.length; i++) {
unifiers.extendNoDuplicates(branch.getClosingUnifiers(nodeList[i]));
}
// We apply each unifier to a different copy of the present tree. If one
// of them closes the entire tree, we're done. If not, and if there's a
// unifier that doesn't affect other branches, we use that one and
// discard the other tree copies. Otherwise we store the other copies
// for backtracking.
var altTrees = [];
var localTree = null;
for (var i=0; i<unifiers.length; i++) {
log("processing unifier on new tree: "+unifiers[i]);
var altTree = tree.copy();
altTree.applySubstitution(unifiers[i]);
altTree.closeCloseableBranches();
if (altTree.openBranches.length == 0) {
log('tree closes, stopping proof search');
prover.useTree(altTree);
return;
}
if (!localTree) {
if (!branch.unifierAffectsOtherBranches(unifiers[i])) {
log("That unifier didn't affect other branches.");
localTree = altTree;
}
else {
altTrees.push(altTree);
}
}
}
if (localTree) {
log("continuing with unifier that doesn't affect other branches");
prover.useTree(localTree);
prover.pruneAlternatives(localTree);
return;
}
// Instead of unifying (often as the only option), we could apply some other
// rule. In particular, we could try to close the branch with an equality
// rule.
if (tree.parser.hasEquality) {
log("checking if we could apply equality reasoning (on original tree)");
var eqProbs = branch.createEqualityProblems(nodeList);
if (eqProbs.length) {
log("scheduling equality problem(s) (on a new tree): "+eqProbs);
var altTree = tree.copy();
altTree.openBranches[0].todoList = eqProbs.map(function(p) {
return Prover.makeTodoItem(Prover.equalityReasoner, p);
});
// We have erased the other todoList items: if the branch can't be
// closed using equality reasoning, we'll switch to the alternative
// introduced below
altTrees.push(altTree);
}
}
// Alternatively, we could apply an ordinary rule. (This is an alternative
// to trying equality reasoning because the latter may only stop when
// depthLimit is reached, in which case we want to switch to the alternative
// of applying ordinary rules.)
if (!altTrees.length) {
// no alternatives found; simply continue with present tree
return;
}
if (branch.todoList.length) {
log("unifier applied on new tree; saving original tree as alternative");
altTrees.push(tree);
// Now altTrees contains trees with a unifier applied and/or trees with
// new EqualityProblems and, after these, the original ununified tree.
}
// We continue with the first altTree and store the others for backtracking.
var curTreeIndex = prover.curAlternativeIndex;
// Delete the active element #curTreeIndex:
prover.alternatives.splice(curTreeIndex, 1);
if (altTrees.length) {
do {
log("switching to first of the just saved alternatives (if not redundant)");
var newTree = altTrees.shift();
// replaces prover.alternatives[curTreeIndex] with newTree:
prover.useTree(newTree, curTreeIndex);
prover.pruneAlternatives(newTree);
} while (newTree.removed && altTrees.length);
if (altTrees.length) {
log("storing the others in prover.alternatives");
prover.storeAlternatives(altTrees);
}
}
}
Prover.literal.priority = 0;
Prover.literal.toString = function() { return 'literal' };
Prover.equalityReasoner = function(branch, equalityProblem) {
/**
* expand the <EqualityProblem> by one RBS rule (roughly, one appliation of LL)
*
* This method follows a similar logic to Prover.literal.
*/
log('tackling equality problem '+equalityProblem);
// equalityProblem.nextStep returns a list of EqualityProblems beginning
// with solved problems (if any) and continuing with problems that have
// extended the original problem by zero or one application of (LL*).
var newProblems = equalityProblem.nextStep();
log("equality reasoning returned "+newProblems);
if (newProblems.length == 0) {
log("equalityProblem exhausted; no further rules to schedule");
if (!branch.todoList[0]) {
// We've already saved an alternative on which we continue with the
// rest of the todoList (in literal()); so we can discard the
// present alternative.
branch.tree.prover.discardCurrentAlternative();
}
return;
}
// We apply each solution to a copy of the present tree. On all these copies,
// the current branch gets closed. If one of them closes all branches, we're
// done. Otherwise we continue with a solution that doesn't affect other
// branches, if any. If there's none, we add all possibilities as alternatives
// for backtracking. (Note that when we copy the tree, we change the
// identity of its Nodes, so the node references in newProblems break. We
// fix this in branch.closeWithEquality().)
var tree = branch.tree;
var prover = tree.prover;
var altTrees = [];
var localTree = null;
while (newProblems.length && !newProblems[0].nextStep) {
// newProblems[0] is a solved problem (that closes the current branch)
var solution = newProblems.shift();
var substitution = solution.getSubstitution();
// We could enforce a check that the solution makes use of newly added
// equalities, but in tests this doesn't speed things up.
log("applying solution "+solution);
var altTree = tree.copy();
altTree.openBranches[0].closeWithEquality(solution);
altTree.closeCloseableBranches();
if (altTree.openBranches.length == 0) {
log('that tree closes, stopping proof search');
prover.useTree(altTree);
return;
}
if (!localTree) {
if (!branch.unifierAffectsOtherBranches(substitution)) {
localTree = altTree;
}
else {
altTrees.push(altTree);
}
}
}
if (localTree) {
log("continuing with solution that doesn't affect other branches");
prover.useTree(localTree);
prover.pruneAlternatives(localTree);
return;
}
// We also consider the option of continuing with unsolved EqualityProblems
// in order to find another solution (using the current tree)
if (newProblems.length) {
log("scheduling revised non-solution problems");
var newTasks = newProblems.map(function(p) {
return Prover.makeTodoItem(Prover.equalityReasoner, p);
});
branch.todoList.extend(newTasks);
// Note that a substitution is only applied to this branch when the
// branch is closed. So we don't need to worry about the terms and
// equations of the new problems changing by the time we get to deal
// with them at the end of the todoList. However, we do need to worry
// about the fact that in-between rules might copy the present tree and
// store it as an alternative. A copied tree has new Node objects, and
// we need to make sure that the EqualityProblems on its todoLists point
// to these Nodes. We do this in tree.copy().
}
if (!altTrees.length) {
// no alternatives found; simply continue with present tree
return;
}
else if (branch.todoList.length) {
altTrees.push(tree);
}
// We continue with the first altTree and store the others for backtracking.
var curTreeIndex = prover.curAlternativeIndex;
prover.alternatives.splice(curTreeIndex, 1);
if (altTrees.length) {
do {
log("switching to first alternative");
var newTree = altTrees.shift();
prover.useTree(newTree, curTreeIndex);
prover.pruneAlternatives(newTree);
} while (newTree.removed && altTrees.length);
if (altTrees.length) {
prover.storeAlternatives(altTrees);
}
}
}
Prover.equalityReasoner.priority = 0;
Prover.equalityReasoner.toString = function() { return 'equality' }
Prover.reflexivity = function(branch, nodeList) {
/**
* apply the modal reflexivity rule (add a wRw node); nodeList is either
* empty or contains a node of form wRv where v might have been newly
* introduced
*/
log('applying reflexivity rule');
if (nodeList.length == 0) {
// rule applied to initial world w:
var worldName = branch.tree.parser.w;
}
else {
var worldName = nodeList[0].formula.terms[1];
}
var R = branch.tree.parser.R;
var formula = new AtomicFormula(R, [worldName, worldName]);
log('adding '+formula);
var newNode = new Node(formula, Prover.reflexivity, nodeList || []);
branch.addNode(newNode);
// No point calling branch.tryClose(newNode): ~Rwv won't be on the branch.
}
Prover.reflexivity.priority = 3;
Prover.reflexivity.needsPremise = false; // can only be applied if wRv is on the branch
Prover.reflexivity.premiseCanBeReflexive = false; // can be applied to wRw
Prover.reflexivity.toString = function() { return 'reflexivity' }
Prover.symmetry = function(branch, nodeList) {
/**
* apply the modal symmetry rule (add vRw if wRv is on branch);
* <nodeList> contains a node of form wRv
*/
log('applying symmetry rule');
var nodeFormula = nodeList[0].formula;
var R = branch.tree.parser.R;
var formula = new AtomicFormula(R, [nodeFormula.terms[1], nodeFormula.terms[0]]);
log('adding '+formula);
var newNode = new Node(formula, Prover.symmetry, nodeList);
branch.addNode(newNode);
}
Prover.symmetry.priority = 3;
Prover.symmetry.needsPremise = true; // can only be applied if wRv is on the branch
Prover.symmetry.premiseCanBeReflexive = false; // can be applied to wRw
Prover.symmetry.toString = function() { return 'symmetry' }
Prover.euclidity = function(branch, nodeList) {
/**
* apply the modal euclidity rule (add vRu if wRv and wRu are on branch);
* <nodeList> contains a newly added node of form wRv
*/
log('applying euclidity rule');
var node = nodeList[0];
var nodeFla = node.formula;
// When a wRv node has been added, euclidity always allows us to add vRv. In
// addition, for each earlier wRu node, we can add uRv as well as
// vRu. However, if we add all of these at once, they will be marked as
// having been added in the same step, so that if some of them are
// eventually used to derive a contradiction, senTree.removeUnusedNodes will
// keep them all (ex.: ◇□p→□◇p). So we have to add them one by one. (And
// they really are different applications of the euclidity rule.)
var flaStrings = branch.nodes.map(function(n) {
return n.formula.string;
});
var R = branch.tree.parser.R;
for (var i=0; i<branch.nodes.length; i++) {
var earlierFla = branch.nodes[i].formula;
if (earlierFla.predicate != R) continue;
if (earlierFla.terms[0] == nodeFla.terms[0]) {
// earlierFla is wRu, nodeFla wRv (or earlierFla == nodeFla); need
// to add uRv and vRu if not already there.
var newFla;
if (!flaStrings.includes(R + earlierFla.terms[1] + nodeFla.terms[1])) {
newFla = new AtomicFormula(R, [earlierFla.terms[1], nodeFla.terms[1]]);
}
else if (!flaStrings.includes(R + nodeFla.terms[1] + earlierFla.terms[1])) {
newFla = new AtomicFormula(R, [nodeFla.terms[1], earlierFla.terms[1]]);
}
if (newFla) {
log('adding '+newFla);
var newNode = new Node(newFla, Prover.euclidity, [branch.nodes[i], node]);
if (branch.addNode(newNode)) {
branch.todoList.unshift(Prover.makeTodoItem(Prover.euclidity, nodeList, 0));
return;
}
}
}
if (branch.nodes[i] == node) break;
}
}
Prover.euclidity.priority = 3;
Prover.euclidity.needsPremise = true; // can only be applied if wRv is on the branch
Prover.euclidity.premiseCanBeReflexive = true; // can be applied to wRw
Prover.euclidity.toString = function() { return 'euclidity' }
Prover.transitivity = function(branch, nodeList) {
/**
* apply the modal transitivity rule (add vRu if wRv and vRu are on branch);
* <nodeList> contains a newly added node of form wRv
*/
log('applying transitivity rule');
var R = branch.tree.parser.R;
var node = nodeList[0];
var nodeFla = node.formula;
// see if we can apply transitivity:
for (var i=0; i<branch.nodes.length; i++) {
var earlierFla = branch.nodes[i].formula;
if (earlierFla.predicate != R) continue;
if (earlierFla.terms[1] == nodeFla.terms[0]) {
// earlierFla uRw, nodeFla wRv
var newFla = new AtomicFormula(R, [earlierFla.terms[0], nodeFla.terms[1]]);
log('matches '+earlierFla+': adding '+newFla);
var newNode = new Node(newFla, Prover.transitivity, [branch.nodes[i], node]);
if (branch.addNode(newNode)) {
branch.todoList.unshift(Prover.makeTodoItem(Prover.transitivity, nodeList, 0));
return;
}
}
// not 'else if': if earlierFla is vRw we need to add both wRw and vRv.
// Hence the duplication in what follows.
if (earlierFla.terms[0] == nodeFla.terms[1]) {
// earlierFla vRu, nodeFla wRv
var newFla = new AtomicFormula(R, [nodeFla.terms[0], earlierFla.terms[1]]);
log('matches '+earlierFla+': adding '+newFla);
var newNode = new Node(newFla, Prover.transitivity, [branch.nodes[i], node]);
if (branch.addNode(newNode)) {
branch.todoList.unshift(Prover.makeTodoItem(Prover.transitivity, nodeList, 0));
return;
}
}
if (branch.nodes[i] == node) break;
}
}
Prover.transitivity.priority = 3;
Prover.transitivity.needsPremise = true; // can only be applied if wRv is on the branch
Prover.transitivity.premiseCanBeReflexive = false; // can be applied to wRw
Prover.transitivity.toString = function() { return 'transitivity' }
Prover.seriality = function(branch, nodeList) {
/**
* apply the modal seriality rule (add wRv); <nodeList> is either empty or
* contains a newly added node of form wRv
*/
log('applying seriality rule');
var R = branch.tree.parser.R;
if (nodeList.length == 0) {
// rule applied to initial world w.
var oldWorld = branch.tree.parser.w;
}
else {
var oldWorld = nodeList[0].formula.terms[1];
}
// check if oldWorld can already see a world:
for (var i=0; i<branch.nodes.length-1; i++) {
var earlierFla = branch.nodes[i].formula;
if (earlierFla.predicate == R && earlierFla.terms[0] == oldWorld) {
log(oldWorld+' can already see a world');
return;
}
}
var newWorld = branch.tree.newWorldName();
var newFla = new AtomicFormula(R, [oldWorld, newWorld]);
log('adding '+newFla);