-
Notifications
You must be signed in to change notification settings - Fork 14
/
logic.js
1864 lines (1507 loc) · 61.1 KB
/
logic.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
"use strict";
// polyfill the Array includes method (which is not supported in IE). This code moved from index.html
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, "includes", {
enumerable: false,
value: function(obj) {
var newArr = this.filter(function(el) {
return el == obj;
});
return newArr.length > 0;
}
});
}
/// Global variables
var sentences = []; // sentences[name] is a sentence/term attached to a name string; easier to populate this dynamically than to create a general string to sentence/term parser.
var unlockedLaws = [];
var allLaws = []; // list of all Laws
var lawsByShortName = {}; // laws indexed by shortname
// convert a list of sentences, boxes, or contexts to a string
function listToString(list) {
if (list.length == 0) return "";
var i;
var str = "";
for (i = 0; i < list.length; i++) {
str += toContext(list[i]).name + ", ";
}
str = str.substring(0, str.length - 2);
return str;
}
// create a deduction string
function deductionString(prefix, list, conclusion)
{
if (list.length == 0) {
if (conclusion.type == "environment")
return "Form environment " + conclusion.name + ".";
else
return "Deduce " + conclusion.name + ".";
}
else {
if (conclusion.type == "environment")
return prefix + " " + listToString(list) + ": form environment " + conclusion.name + ".";
else
return prefix + " " + listToString(list) + ": deduce " + conclusion.name + ".";
}
}
//// FreeVariable object
function FreeVariable(name)
{
this.type = "free variable";
this.subtype = name;
this.name = "<I>"+name+"</I>";
this.longName = this.name;
}
// the name of a free variable associated to a non-negative integer i. Note: this is the name without the italics; should be matched to the "subtype" of a variable rather than its name.
function FreeVariableName(i) {
var str = "xyz"[i%3];
var j;
for (j=0; j < (i-2)/3; j++) str += "'";
return str;
}
//// BoundVariable object. In this text, it is assumed that free and bound variables have disjoint namespaces; we'll use x,y,z,x',y', etc. for free variables and X,Y,Z,X',Y',etc. for bound variables. Note: this is the name without the italics; should be matched to the "subtype" of a variable rather than its name.
function BoundVariable(name)
{
this.type = "bound variable";
this.subtype = name;
this.name = "<I>"+name+"</I>";
this.longName = this.name;
}
/// the name of a bound variable associated to a non-negative integer i
function BoundVariableName(i) {
var str="XYZ"[i%3];
var j;
for (j=0; j < (i-2)/3; j++) str += "'";
return str;
}
/// Operator object. relationStyle is for 2-ary operators placed in between the arguments, e.g. x+y, as opposed to f(x,y).
function Operator( name, arity, relationStyle )
{
this.type = "operator";
this.subtype = name;
this.name = "<I>" + name + "</I>";
this.arity = arity;
this.relationStyle = relationStyle;
switch(this.arity)
{
case 0: this.longName = "Constant " + this.name; break;
case 1: this.longName = "Unary operator " + this.name + "()"; break;
case 2:
if (this.relationStyle) this.longName = "Binary operator " + this.name;
else this.longName = "Binary operator " + this.name + "(,)";
}
}
/// Term object. The only way to make a term is to use a free or bound variable or primitive term, or to apply an operator with a list of terms.
function Term()
{
this.type = "term";
this.subtype = ""; // will be "free variable", "bound variable", "primitive", or "operator evaluation"
this.name = "";
this.longName = "";
this.operator = ""; // the operator used (only needed for operator subtype)
this.argList = []; // list of args, or just the variable
}
// turn a free variable into a term
function freeVariableTerm(free)
{
var term = new Term();
term.subtype = "free variable";
term.name = free.name;
term.longName = free.longName;
term.argList = [free];
return term;
}
// turn a bound variable into a term
function boundVariableTerm(bound)
{
var term = new Term();
term.subtype = "bound variable";
term.name = bound.name;
term.longName = bound.longName;
term.argList = [bound];
return term;
}
// turn an operator and some arguments into a term
function operatorTerm(operator, argList)
{
var term = new Term();
term.subtype = "operator evaluation";
var i;
term.operator = operator;
for (i=0; i < term.operator.arity; i++)
term.argList[i] = toTerm(argList[i]);
if (term.operator.relationStyle) {
term.name = term.argList[0].longName + " " + term.operator.name + " " + term.argList[1].longName;
term.longName = "(" + term.name + ")";
} else if (term.operator.arity == 0) {
term.name = term.operator.name;
term.longName = term.name;
} else {
var str = term.operator.name + "(";
for (i = 0; i < term.operator.arity; i++ )
str += term.argList[i].name + ", ";
str = str.substring(0, str.length - 2);
term.name = str + ")";
term.longName = term.name;
}
return term;
}
function primitiveTerm(name)
{
var term = new Term();
term.subtype = "primitive";
term.name = name;
term.longName = name;
return term;
}
// turn an object into a term if possible
function toTerm(obj)
{
if (typeof(obj) == "string") return primitiveTerm(obj);
if (obj.type == "term") return obj;
if (obj.type == "free variable") return freeVariableTerm(obj);
if (obj.type == "bound variable") return boundVariableTerm(obj);
error("Unrecognised type for conversion to term.");
}
/// Predicate object. relationStyle is for 2-ary predicates placed in between the arguments, e.g. x=y, as opposed to P(x,y).
function Predicate( name, arity, relationStyle )
{
this.type = "predicate";
this.subtype = name;
this.name = "<I>" + name + "</I>";
this.arity = arity;
this.relationStyle = relationStyle;
switch(this.arity)
{
case 0: this.longName = "Proposition " + this.name; break;
case 1: this.longName = "Predicate " + this.name + "()"; break;
case 2:
if (this.relationStyle) this.longName = "Relation " + this.name;
else this.longName = "Predicate " + this.name + "(,)";
}
}
// we have one special predicate: the equality relation
var equality = new Predicate("=", 2, true);
// the sentence x=y
function equals(x,y) {
return predicateSentence( equality, [x,y] );
}
//// Connective object
function Connective(name, arity) {
this.name = name; // "AND", "OR", "NOT", "IMPLIES", "IFF", "TRUE", "FALSE"
this.arity = arity; // 0, 1, or 2
}
var ANDConnective = new Connective("AND", 2);
var ORConnective = new Connective("OR", 2);
var NOTConnective = new Connective("NOT", 1);
var IMPLIESConnective = new Connective("IMPLIES", 2);
var IFFConnective = new Connective("IFF", 2);
var TRUEConnective = new Connective("TRUE", 0);
var FALSEConnective = new Connective("FALSE", 0);
//// Sentence object
function Sentence() {
this.type = ""; // "primitive", "connective", "quantifier"
this.subtype = ""; // "AND", "OR", "NOT", "IMPLIES", "IFF" for connectives; "atomic" or "predicate" for primitives; "for all" or "there exists" for quantifiers
this.name = ""; // name of sentence when used standalone
this.longName = ""; // name of sentence when combined with other sentences
this.argList = []; // the list of arguments in this sentence (usually of length 0, 1, or 2)
this.predicate = ""; // the predicate used (only relevant for predicate subtype)
this.connective = ""; // the connective used (only relevant for connective subtype)
}
// create an atomic sentence
function atomicSentence(name) {
var sentence = new Sentence();
sentence.type = "primitive";
sentence.subtype = "atomic";
sentence.name = "<em>"+name+"</em>";
sentence.longName = sentence.name;
return sentence;
}
// create a sentence from predicate and args
function predicateSentence(predicate, argList) {
var sentence = new Sentence;
sentence.type = "primitive";
sentence.subtype = "predicate";
sentence.predicate = predicate;
var i;
for (i=0; i < argList.length; i++)
sentence.argList[i] = toTerm(argList[i]);
if (sentence.predicate.relationStyle) {
sentence.name = argList[0].longName + " " + sentence.predicate.name + " " + argList[1].longName;
sentence.longName = "(" + sentence.name + ")";
} else if (sentence.predicate.arity == 0) {
sentence.name = sentence.predicate.name;
sentence.longName = sentence.name;
} else {
var str = sentence.predicate.name + "(";
for (i = 0; i < sentence.predicate.arity; i++ )
str += toTerm(argList[i]).name + ", ";
str = str.substring(0, str.length - 2);
sentence.name = str + ")";
sentence.longName = sentence.name;
}
sentence.name = sentence.name;
return sentence;
}
// from a sentence from a connective and list of arguments
function connectiveSentence(connective, argList) {
var sentence = new Sentence();
sentence.type = "connective";
sentence.subtype = connective.name;
sentence.argList = argList;
sentence.connective = connective;
switch(connective.arity) {
case 0:
sentence.name = connective.name;
sentence.longName = sentence.name;
break;
case 1:
sentence.name = connective.name + " " + argList[0].longName;
sentence.longName = "(" + sentence.name + ")";
break;
case 2:
sentence.name = argList[0].longName + " " + connective.name + " " + argList[1].longName;
sentence.longName = "(" + sentence.name + ")";
break;
}
return sentence;
}
// form the conjunction of two sentences
function AND( sentence1, sentence2) {
return connectiveSentence(ANDConnective, [sentence1, sentence2]);
}
// form the disjunction of two sentences
function OR( sentence1, sentence2) {
return connectiveSentence(ORConnective, [sentence1, sentence2]);
}
// form the implication of two sentences
function IMPLIES( sentence1, sentence2) {
return connectiveSentence(IMPLIESConnective, [sentence1, sentence2]);
}
// form the iff of two sentences
function IFF( sentence1, sentence2) {
return connectiveSentence(IFFConnective, [sentence1, sentence2]);
}
// form the negation of a sentence
function NOT( sentence1) {
return connectiveSentence(NOTConnective, [sentence1]);
}
// form the TRUE sentence
function TRUE() {
return connectiveSentence(TRUEConnective, []);
}
// form the FALSE sentence
function FALSE() {
return connectiveSentence(FALSEConnective, []);
}
// a sentence of the form "for all <bound>: <predicate>"
function forAll(predicate, bound) {
var sentence = new Sentence();
sentence.type = "quantifier";
sentence.subtype = "for all";
sentence.name = "FOR ALL " + bound.name + ": " + predicate.longName;
sentence.longName = "(" + sentence.name + ")";
sentence.argList = [predicate, toTerm(bound)];
sentence.name = sentence.name;
return sentence;
}
// a sentence of the form "there exists <bound>: <predicate>"
function thereExists(predicate, bound) {
var sentence = new Sentence();
sentence.type = "quantifier";
sentence.subtype = "there exists";
sentence.name = "THERE EXISTS " + bound.name + ": " + predicate.longName;
sentence.longName = "(" + sentence.name + ")";
sentence.argList = [predicate, toTerm(bound)];
sentence.name = sentence.name;
return sentence;
}
// tries to convert an obj to sentence if it can
function toSentence(obj) {
if (typeof(obj) == 'string') return atomicSentence(obj);
if (obj instanceof Sentence ) return obj;
if (obj instanceof Context ) return obj.sentence;
if (obj instanceof Law) return obj.conclusion.sentence;
if (obj instanceof Exercise) return obj.law.conclusion.sentence;
if (obj instanceof Assumption) return obj.sentence;
if (obj.type == "sentenceBox") return obj.sentence;
if (obj.type == "formulaBox") return obj.sentence;
error("Unable to convert object to sentence.");
}
//some atomic sentences. Moved from index.html
var A = atomicSentence("A");
var B = atomicSentence("B");
var C = atomicSentence("C");
var D = atomicSentence("D");
// Law object
function Law(shortName, name, givens, conclusion) {
this.shortName = shortName;
this.name = name; // name of law, e.g. "EXERCISE 1.1"
// givens is an array of given hypotheses (can be empty). I allow sentences as givens, so these need to be converted to contexts.
var givenslist = [];
givens.forEach( function(given) {
givenslist.push(toContext(given));
});
this.givens = givenslist;
this.conclusion = toContext(conclusion); // given conclusion
this.unlocked = false; // by default the law is not unlocked
this.string = deductionString("Given", givens, this.conclusion);
this.index = allLaws.length; // the order of the law in the text (used to determine circularity) - the allLaws.length is a placeholder, will be overwritten
this.clone = ""; // points to the clone of the law with additional root environment, if needed
// for most laws, the matching givens and conclusions template is the same as what is displayed to the user.
this.givensTemplate = this.givens;
this.conclusionTemplate = this.conclusion;
allLaws.push(this);
lawsByShortName[shortName] = this;
if (allFormulas(this.givens)) {
if (this.conclusion.type == 'sentence in environment' || this.conclusion.type == 'environment') {
var givensClone = this.givens.slice(0);
givensClone.push(rootEnvironmentContext());
this.clone = new Law(this.shortName + "Clone", this.name, givensClone, this.conclusion);
}
}
}
// add (name of) expr to list if not already there
function record(list, expr) {
if (!list.includes(expr.name)) {
list.push(expr.name);
sentences[expr.name] = expr; // remember the variable object associated to name
}
}
function makeOptions(getPrimitives, getFreeVars, getBoundVars, getPrimTerms, getPredicates, getOperators, getAtomic) {
var options = [];
options.getPrimitives = getPrimitives;
options.getFreeVars = getFreeVars;
options.getBoundVars = getBoundVars;
options.getPrimTerms = getPrimTerms;
options.getPredicates = getPredicates;
options.getOperators = getOperators;
options.getAtomic = getAtomic;
return options;
}
// list the (names of) atomic primitives, free variables, bound variables, primitive terms, predicates, operators occurring in a law
function listPrimitives(law, getPrimitives, getFreeVars, getBoundVars, getPrimTerms, getPredicates, getOperators, getAtomic, useTemplate) {
var list = [];
var options = makeOptions(getPrimitives, getFreeVars, getBoundVars, getPrimTerms, getPredicates, getOperators, getAtomic);
var givens;
var conclusion;
if (useTemplate) {
givens = law.givensTemplate;
conclusion = law.conclusionTemplate;
} else {
givens = law.givens;
conclusion = law.conclusion;
}
givens.forEach( function(item) {
pushPrimitivesFromContext(list, toContext(item), options);
});
// usually the line below is redundant, as any primitives in conclusion should have already appeared in one of the givens, but there are some exceptions, e.g. universal introduction without specifying the bound variable
pushPrimitivesFromContext(list, conclusion, options);
return list;
}
// push all the primitives from context onto list (removing duplicates)
function pushPrimitivesFromContext(list, context, options) {
if (context.type == "formula" || context.type == "sentence in environment") {
pushPrimitivesFromSentence(list, context.sentence, options);
}
if (context.type == "environment" || context.type == "sentence in environment")
{
context.environment.forEach( function(item) {
if (item.type == "assuming" || item.type == "setting") {
pushPrimitivesFromSentence(list, item.sentence, options);
}
if (item.type == "setting" || item.type == "letting") {
if (options.getFreeVars) {
record(list, item.variable);
}
}
});
}
if (context.type == "term context") {
pushPrimitivesFromSentence(list, context.term, options);
}
}
// push all the primitives from sentence/term onto list (removing duplicates)
function pushPrimitivesFromSentence(list, sentence, options)
{
switch(sentence.type) {
case "primitive":
if (options.getPrimitives) record(list, sentence);
switch(sentence.subtype) {
case "atomic":
if (options.getAtomic) record(list, sentence);
break;
case "predicate":
if (options.getPredicates) record(list, sentence.predicate);
sentence.argList.forEach( function(arg) { pushPrimitivesFromSentence(list,arg, options);} );
break;
}
break;
case "quantifier":
if (options.getBoundVars) record(list, sentence.argList[1]);
pushPrimitivesFromSentence(list, sentence.argList[0], options);
break;
case "connective":
sentence.argList.forEach( function(arg) { pushPrimitivesFromSentence(list,arg, options);} );
break;
case "term":
switch(sentence.subtype) {
case "primitive":
if (options.getPrimTerms) record(list,sentence);
return;
case "operator evaluation":
if (options.getOperators) record(list, sentence.operator);
sentence.argList.forEach( function(arg) { pushPrimitivesFromSentence(list,arg, options);} );
return;
case "free variable":
if (options.getFreeVars) record(list, sentence.argList[0]);
return;
case "bound variable":
if (options.getBoundVars) record(list, sentence.argList[0]);
return;
}
}
}
// returns true if all terms in givens are formulas or terms (i.e., no environment is involved)
function allFormulas(givens) {
var i;
for (i = 0; i < givens.length; i++) {
if (givens[i].type != "formula" && givens[i].type != "term context") return false;
}
return true;
}
// a tricky routine: tries to match arglist to the givens of a law and see what the primitives are, returning this data in an output object. Note: for predicate logic, some of the matches need to be discarded because the conclusion does not obey scoping laws (e.g. repeated free variables, etc.). Also there may be situations where there are multiple possible substitutions (creating existential quantifier)
function matchWithGivens( arglist, law, primitives ) {
// for matching, we use the template, rather than the givens and conclusion displayed to user (but these are usually the same)
var givens = law.givensTemplate;
var conclusion = law.conclusionTemplate;
var output = new Object();
output.matches = true; // so far, no reason to doubt a match.
output.illegal = false; // sometimes there is technically a match but something is ill-formed
output.multivalued = false; // do we need multiple conclusions, or just one?
output.env = []; // by default, the output environment will be the root one.
primitives.forEach( function(primitive) {
output[primitive] = "";
});
// technically one needs to ensure that primitives and free variables avoid reserved words such as "matches". This is unlikely to come up in practice.
if (arglist.length != givens.length) {
output.matches = false;
return output;
}
// convert everything to contexts if not already done so (this step may be redundant)
var i;
for (i = 0; i < givens.length; i++) {
arglist[i] = toContext(arglist[i]);
givens[i] = toContext(givens[i]);
}
// check if all the givens are formulas
if (!allFormulas(givens))
{
var proposedYet = false;
var proposedEnv = [];
for (i = 0; i < givens.length; i++) {
if (givens[i].type == "sentence in environment" || givens[i].type == "environment") {
if (arglist[i].environment.length < givens[i].environment.length) {
// can't match if the template has more deeply nested assumptions than the arglist!
output.matches = false;
return output;
}
var candidateEnv = arglist[i].environment.slice( 0, arglist[i].environment.length - givens[i].environment.length);
if (proposedYet == false) {
proposedYet = true;
proposedEnv = candidateEnv;
}
else if (assumptionListToString(proposedEnv) != assumptionListToString(candidateEnv)) { // need to convert to string here as a proxy for passing by value rather than by reference
output.matches = false;
return output;
}
}
}
output.env = proposedEnv;
}
switch(law.shortName) { // a number of laws are too complex to be matched by the standard algorithm and have to be treated separately
case "UniversalIntroduction":
case "UniversalIntroduction2":
matchUniversalIntroduction(arglist, output, law);
break;
case "UniversalSpecification":
case "UniversalSpecification2":
matchUniversalSpecification(arglist, output, law);
break;
case "ExistentialInstantiation":
case "ExistentialInstantiation2":
matchExistentialInstantiation(arglist, output, law);
break;
case "ExistentialIntroduction":
case "ExistentialIntroduction2":
matchExistentialIntroduction(arglist, output, law);
break;
case "Indiscernability":
matchIndiscernability(arglist, output, law);
break;
case "UniversalRenamingBoundVar":
case "ExistentialRenamingBoundVar":
matchRenamingBoundVar(arglist, output, law);
break;
case "BarbaraSingular":
matchBarbaraSingular(arglist, output);
break;
default:
var i;
for (i = 0; i < givens.length; i++) {
matchWithGiven( arglist[i], givens[i], output);
if (!output.matches) return output;
}
for (i=0; i < primitives.length; i++) {
if (output[primitives[i]] == "") { // somehow failed to match one of the primitives
output.matches = false;
return output;
}
}
output.conclusion = subs(conclusion, output);
}
return output;
}
// match arglist against one of the two laws of universal introduction and report the conclusions in output
function matchUniversalIntroduction(arglist, output, law) {
if (!output.matches) return;
// arglist[0] needs to be of the form "A, [letting x be arbitrary]" after the output.env
if (arglist[0].type != "sentence in environment") {
output.matches = false;
return;
}
var assumption = arglist[0].environment[output.env.length];
if (assumption.type != "letting") {
output.matches = false;
return;
}
var freeVariable = assumption.variable;
var statement = arglist[0].sentence;
var boundVariable;
switch (law.shortName) {
case "UniversalIntroduction":
if (arglist[1].type != "term context") {
output.matches = false;
return;
}
if (arglist[1].term.subtype != "bound variable") {
output.matches = false;
return;
}
boundVariable = arglist[1].term.argList[0];
break;
case "UniversalIntroduction2":
if (arglist[1].type != "environment") {
output.matches = false;
return;
}
// choose the next available bound Variable
boundVariable = nextAvailableBoundVariable(statement);
break;
}
var newSentence = forAll( searchReplace( statement, freeVariable, boundVariable), boundVariable);
output.conclusion = sentenceContext( newSentence, output.env );
}
// return the next availalbe bound variable not already in a statement
function nextAvailableBoundVariable(statement) {
var boundVars = [];
pushPrimitivesFromSentence(boundVars, statement, makeOptions(false, false, true,false, false, false, false));
var num=0;
var match;
var str, longStr;
do {
str = BoundVariableName(num);
longStr = "<I>"+str+"</I>";
match = boundVars.includes(longStr);
num++;
} while (match);
return new BoundVariable(str);
}
// match arglist against the law of universal specification and report the conclusions in output
function matchUniversalSpecification(arglist, output, law) {
if (!output.matches) return;
// arglist[0] needs to be of the form "FOR ALL X: P(X)" after the output.env
if (arglist[0].type != "sentence in environment") {
output.matches = false;
return;
}
if (arglist[0].sentence.type != "quantifier" || arglist[0].sentence.subtype != "for all") {
output.matches = false;
return;
}
var sentence = arglist[0].sentence.argList[0];
var boundVar = toTerm(arglist[0].sentence.argList[1]).argList[0];
if (arglist[1].type != "term context") {
output.matches = false;
return;
}
var term = toTerm(arglist[1].term);
var newSentence = searchReplace( sentence, boundVar, term );
if (law.shortName == "UniversalSpecification") {
if (hasBoundOrUnknownFree(term, output.env)) {
output.illegal = true; // will keep matching but will display in silver
}
output.conclusion = sentenceContext( newSentence, output.env );
} else if (law.shortName == "UniversalSpecification2") {
if (term.subtype != "free variable") {
output.matches = false;
return;
}
var env = output.env.slice(0);
env.push(toAssumption(term.argList[0]));
output.conclusion = sentenceContext( newSentence, env );
}
}
// returns true if term contains a bound variable or a free variable not already in environment
function hasBoundOrUnknownFree(term,env) {
switch(term.subtype) {
case "primitive":
return false;
case "free variable":
var i;
for (i=0; i < env.length; i++)
if (env[i].type == 'letting' || env[i].type == 'setting')
if (env[i].variable.name == term.argList[0].name)
return false;
return true;
case "bound variable":
return true;
case "operator":
var i;
for (i=0; i < term.argList.length; i++)
if (hasBoundOrUnknownFree(term.argList[i],env)) return true;
return false;
}
}
// replace all occurrences of term "search" with term "replace"
function searchReplace(statement, search, replace) {
var newArgList = [];
var i;
if (statement.type == "free variable" || statement.type == "bound variable") return statement;
if (statement.name == search.name) return toTerm(replace);
for (i=0; i < statement.argList.length; i++)
newArgList[i] = searchReplace(statement.argList[i], search, replace);
switch (statement.type) {
case "term":
if (statement.subtype == "free variable") return statement;
if (statement.subtype == "bound variable") return statement;
if (statement.subtype == "primitive") return statement;
if (statement.subtype == "operator evaluation") return operatorTerm(statement.operator, newArgList);
return;
case "primitive":
if (statement.subtype == "atomic") return statement;
if (statement.subtype == "predicate") return predicateSentence(statement.predicate, newArgList);
return;
case "connective":
return connectiveSentence(statement.connective, newArgList);
case "quantifier":
if (statement.subtype == "for all") return forAll( newArgList[0], newArgList[1]);
if (statement.subtype == "there exists") return thereExists( newArgList[0], newArgList[1]);
return;
}
}
// match arglist against the law of existential instantiation and report the conclusions in output
function matchExistentialInstantiation(arglist, output, law) {
if (!output.matches) return;
// arglist[0] needs to be of the form "THERE EXISTS X: P(X)" after the output.env
if (arglist[0].type != "sentence in environment") {
output.matches = false;
return;
}
if (arglist[0].sentence.type != "quantifier" || arglist[0].sentence.subtype != "there exists") {
output.matches = false;
return;
}
var sentence = arglist[0].sentence.argList[0];
var boundVar = toTerm(arglist[0].sentence.argList[1]).argList[0];
var freeVariable;
if (law.shortName == "ExistentialInstantiation") {
if (arglist[1].type != "term context") {
output.matches = false;
return;
}
if (arglist[1].term.subtype != "free variable") {
output.matches = false;
return;
}
freeVariable = arglist[1].term.argList[0];
} else if (law.shortName == "ExistentialInstantiation2") {
// choose the next available free Variable
var freeVars = [];
pushPrimitivesFromContext(freeVars, sentenceContext(sentence,output.env), makeOptions(false, true, false, false, false, false, false));
var num=0;
var match;
var str, longStr;
do {
str = FreeVariableName(num);
longStr = "<I>"+str+"</I>";
match = freeVars.includes(longStr);
num++;
} while (match);
freeVariable = new FreeVariable(str);
}
var newSentence = searchReplace( sentence, boundVar, freeVariable );
var env = output.env.slice(0);
env.push( settingAssumption(newSentence, freeVariable) );
output.conclusion = sentenceContext( newSentence, env );
}
// match arglist against one of the two laws of existential introduction and report the conclusions in output
function matchExistentialIntroduction(arglist, output, law) {
if (!output.matches) return;
// arglist[0] needs to be of the form "P(X)" after the output.env
if (arglist[0].type != "sentence in environment") {
output.matches = false;
return;
}
var sentence = arglist[0].sentence;
// arglist[1] needs to be a term context
if (arglist[1].type != "term context") {
output.matches = false;
return;
}
var term = arglist[1].term;
if (hasBoundOrUnknownFree(term, output.env)) {
output.illegal = true; // will keep matching but will display in silver
}
var boundVariable;
switch (law.shortName) {
case "ExistentialIntroduction2":
if (arglist[2].type != "term context") {
output.matches = false;
return;
}
if (arglist[2].term.subtype != "bound variable") {
output.matches = false;
return;
}
boundVariable = arglist[2].term.argList[0];
break;
case "ExistentialIntroduction":
// choose the next available bound Variable
boundVariable = nextAvailableBoundVariable(sentence);
break;
}
output.multivalued = true;
var translations = allSearchReplace(sentence, term, boundVariable);
output.conclusions = [];
var i;
for (i=0; i < translations.length; i++)
output.conclusions.push(sentenceContext( thereExists(translations[i],boundVariable), output.env ));
}
// match arglist against the law of indiscernability and report the conclusions in output
function matchIndiscernability(arglist, output, law) {
if (!output.matches) return;
// arglist[0], arglist[1] have to be sentences
if (arglist[0].type != "sentence in environment" || arglist[1].type != "sentence in environment") {
output.matches = false;
return;
}
var sentence = arglist[0].sentence;
var sentence2 = arglist[1].sentence;
// sentence2 needs to be of the form alpha=beta
if (sentence2.subtype != "predicate" || sentence2.predicate != equality) {
output.matches = false;
return;