-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfol.js
More file actions
576 lines (520 loc) · 14.4 KB
/
fol.js
File metadata and controls
576 lines (520 loc) · 14.4 KB
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
// FOL Evaluator
//==============
//
// Copyright (c) 2012-2017 Michael Rieppel
//
// These are the global variables that store information about the model (domain and
// extensions for various lexical items)
var DOMAIN = [];
var CONSTANTS = {};
var SENTENCES = {};
var PREDICATES1 = {};
var PREDICATES2 = {};
// Denotation Constructors
//========================
// Below are constructor functions for the various different kinds of denotations. These
// constructors generally take three arguments (s,f,v): s is the symbol to which the
// denotation is assigned, f the formula the symbol is an immediate constituent of, and
// v a list of the predicate abstractors that the symbol is in the scope of.
// The constructor then returns the appropriate denotation; this is a function which,
// inter alia, also sometimes stores the just-mentioned information via closures.
//
// Broadly, the semantic background theory is as follows. Letting e be the type of
// individuals, t the type of truth values, and g the type of assignment functions, the
// various lexical items denote functions of the following types:
//
// Variables: (g,e)
// Sentences: (g,t)
// 1-place predicates: ((g,e),(g,t))
// 2-place predicates: ((g,e),((g,e),(g,t)))
// Unary connective: ((g,t),(g,t))
// Binary connective: ((g,t),((g,t),(g,t)))
// Predicate abstractor: ((g,t),((g,e),(g,t))) i.e. takes sentence returns predicate
// Quantifier: (((g,e),(g,t)),(g,t)) i.e. takes predicate returns sentence
//
// However, there is a complication: those functions which are supposed to return a
// boolean in actuality return an array with the relevant boolean value at the 0th
// index of the array, and other information in the rest of the array. This returned
// array is needed so that the program can compute not only the truth value of the
// sentence being evaluated, but also the entire history of that evaluation.
//
// Specifically: the 1th index contains the formula being evaluated, the 2th index
// contains the assignment function (a JavaScript object) relative to which the formula
// is being evaluated, and the 3rd index contain arrays corresponding to evaluations
// further down the evaluation process.
//
// For the record, I here include some commented-out denotation constructors showing what
// they would look like without these complications:
//
// function mk1prDen(s) { // for 1-place predicates
// var ext = getExt(s);
// return function (x) {
// return function (g) {
// return isin(x(g),ext);
// }
// }
// }
//
// function mkCnjDen() { // for the conjunction operator
// return function (x) {
// return function (y) {
// return function (g) {
// return x(g)&&y(g);
// }
// }
// }
// }
//
// function mkUqDen() { // for the universal quantifier
// return function(p) {
// return function (g) {
// var tv = true;
// for(var i=0;i<DOMAIN.length;i++) {
// if(!((p(function (h) {return DOMAIN[i];}))(g))){tv = false;}
// }
// return tv.
// }
// }
// }
// (g,e)
function mkVarDen(s) {
return function (g) {return g[s];}
}
// ((g,e),(g,t))
function mk1prDen(s,f,v) {
var ext = get1Ext(s);
return function (x) {
return function (g) {
var out = isin(x(g),ext);
return [out,f,getRealG(g,v)];
}
}
}
// ((g,e),((g,e),(g,t)))
function mk2prDen(s,f,v) {
var ext = get2Ext(s);
return function (x) {
return function (y) {
return function (g) {
var out = isIn([x(g),y(g)],ext);
return [out,f,getRealG(g,v)];
}
}
}
}
// ((g,e),((g,e),(g,t)))
function mkIdDen(s,f,v) {
return function (x) {
return function (y) {
return function (g) {
var out = x(g)===y(g);
return [out,f,getRealG(g,v)];
}
}
}
}
// (g,t)
function mkSenDen(s,f,v) {
var tv = getSenExt(s);
return function(g) {
return [tv,f,getRealG(g,v)];
}
}
// ((g,t),(g,t))
function mkNegDen(s,f,v) {
return function (x) {
return function (g) {
var xg = x(g);
return [!xg[0],f,getRealG(g,v),xg];
}
}
}
// ((g,t),((g,t),(g,t)))
function mkCnjDen(s,f,v) {
return function (x) {
return function (y) {
return function (g) {
var xg = x(g);
var yg = y(g);
return [xg[0]&&yg[0],f,getRealG(g,v),xg,yg];
}
}
}
}
// ((g,t),((g,t),(g,t)))
function mkDsjDen(s,f,v) {
return function (x) {
return function (y) {
return function (g) {
var xg = x(g);
var yg = y(g);
return [xg[0]||yg[0],f,getRealG(g,v),xg,yg];
}
}
}
}
// ((g,t),((g,t),(g,t)))
function mkCndDen(s,f,v) {
return function (x) {
return function (y) {
return function (g) {
var xg = x(g);
var yg = y(g);
return [(!xg[0])||yg[0],f,getRealG(g,v),xg,yg];
}
}
}
}
// ((g,t),((g,t),(g,t)))
function mkBicDen(s,f,v) {
return function (x) {
return function (y) {
return function (g) {
var xg = x(g);
var yg = y(g);
return [xg[0]===yg[0],f,getRealG(g,v),xg,yg];
}
}
}
}
// (((g,e),(g,t)),(g,t))
function mkUqDen(s,f,v) {
return function(p) {
return function (g) {
var tr = [];
var tv = true;
for(var i=0;i<DOMAIN.length;i++) {
var foo = p(function (h) {return DOMAIN[i];})(g);
if(!foo[0]) {tv = false;}
tr = tr.concat([foo]);
}
return [tv,f,getRealG(g,v),tr];
}
}
}
// (((g,e),(g,t)),(g,t))
function mkEqDen(s,f,v) {
return function(p) {
return function (g) {
var tr = [];
var tv = false;
for(var i=0;i<DOMAIN.length;i++) {
var foo = p(function (h) {return DOMAIN[i];})(g);
if(foo[0]) {tv = true;}
tr = tr.concat([foo]);
}
return [tv,f,getRealG(g,v),tr];
}
}
}
// ((g,t),((g,e),(g,t)))
function mkAbsDen(s) {
return function (t) {
return function (x) {
return function (g) {
g[s]=x(g);
return t(g);
}
}
}
}
function mkBinDen(c,f,v) {
switch (c) {
case '&' : return mkCnjDen(c,f,v);
case 'v' : return mkDsjDen(c,f,v);
case '>' : return mkCndDen(c,f,v);
case '<>' : return mkBicDen(c,f,v);
}
}
function get1Ext(p) {
return PREDICATES1[p];
}
function get2Ext(p) {
return PREDICATES2[p];
}
function getSenExt(p) {
if(p==='#') {
return false;
} else {
return SENTENCES[p];
}
}
// Assignment, [Char] -> Assignment
// Takes an assignment function (a JavaScript object), and an array of variables, and
// returns a "pruned" assignment that only stores information about assigned values
// for the variables in the provided array.
function getRealG(g,v) {
var out = {};
for(var i=0;i<v.length;i++) {
out[v[i]] = g[v[i]];
}
return out;
}
// Some Array Comparison Functions
// ===============================
// checks if o is in array arr, where o is an int or array of ints
function isIn(o,arr) {
for(var i=0;i<arr.length;i++) {
if(arrEq(arr[i],o)) {
return true;
}
}
return false;
}
// checks ints, and Arrays of ints of any depth, for equality
function arrEq(ar1,ar2) {
var out = true;
if((Array.isArray(ar1) && Array.isArray(ar2)) && ar1.length==ar2.length) {
for(var i=0;i<ar1.length;i++) {
out = arrEq(ar1[i],ar2[i]) && out;
}
return out;
}
if(!(ar1 instanceof Array) && !(ar2 instanceof Array)) {
return ar1==ar2;
}
return false;
}
// simpler membership function where o is not an array
function isin(o,ar) {
return ar.indexOf(o)>=0;
}
// determines if array x is a subset (initial segment) of array y
function subset(x,y) {
var out = false;
for(var i=0;i<x.length;i++) {
for(var j=0;j<y.length;j++) {
if(x[i]===y[j]) {out = true;}
}
if(out) {out = false;} else {return false;}
}
return true;
}
// Interpretation
//===============
// Takes a tree as output by insertDen(), does functional application on the various
// denotations in the tree, and returns the output of those functional applications.
// This output is a function that then takes a variable assignment and returns
// an array representation of the evaluation history on that assignment.
function interpret(t) {
if(t[0]=='Q') {
return t[2](t[3](interpret(t[4])));
}
if(t[0]=='U') {
return t[2](interpret(t[3]));
}
if(t[0]=='B') {
return t[3](interpret(t[2]))(interpret(t[4]));
}
if(t[0]=='0p') {
return t[2];
}
if(t[0]=='1p') {
return t[2](t[3]);
}
if(t[0]=='2p' || t[0]=='=') {
return t[2](t[3])(t[4]);
}
}
// Building a Tree Containing Denotations
//=======================================
// Tree, [Char] -> Tree
// Takes a parse tree as output by the parse() function, and an array tracking which
// variable binders have been encountered in the tree so far, and then substitutes
// the appropriate denotations into the tree. (Each denotation then has access to
// information about which variable binders it occurs in the scope of.)
function insertDen(t,v) {
if(t[0]=='Q' && t[2]=='E') {
return [t[0],t[1],mkEqDen('E',t[1],v),mkAbsDen(t[3]),insertDen(t[4],v.concat(t[3]))];
}
if(t[0]=='Q' && t[2]=='A') {
return [t[0],t[1],mkUqDen('A',t[1],v),mkAbsDen(t[3]),insertDen(t[4],v.concat(t[3]))];
}
if(t[0]=='U') {
return [t[0],t[1],mkNegDen('~',t[1],v),insertDen(t[3],v)];
}
if(t[0]=='B') {
return [t[0],t[1],insertDen(t[2],v),mkBinDen(t[3],t[1],v),insertDen(t[4],v)];
}
if(t[0]=='0p') {
if(SENTENCES[t[2]]===undefined) {
throw 'ERROR: cannot evaluate. The loaded model contains no extension for the sentence letter '+t[2]+'.';
} else {return [t[0],t[1],mkSenDen(t[2],t[1],v)];}
}
if(t[0]=='1p') {
if(PREDICATES1[t[2]]===undefined) {
throw 'ERROR: cannot evaluate. The loaded model contains no extension for the 1-place predicate '+t[2]+'.';
}
var ck = defined(v,[t[3]]);
if(!ck[0]) {
throw 'ERROR: cannot evaluate. The symbol '+ck[1]+' occurs in the formula you entered as either a constant that has no extension in the loaded model, or as an unbound variable.';
} else {return [t[0],t[1],mk1prDen(t[2],t[1],v),mkVarDen(t[3])];}
}
if(t[0]=='2p') {
if(PREDICATES2[t[2]]===undefined) {
throw 'ERROR: cannot evaluate. The loaded model contains no extension for the 2-place predicate '+t[2]+'.';
}
var ck = defined(v,[t[3],t[4]]);
if(!ck[0]) {
throw 'ERROR: cannot evaluate. The symbol '+ck[1]+' occurs in the formula you entered as either a constant that has no extension in the loaded model, or as an unbound variable.';
} else {return [t[0],t[1],mk2prDen(t[2],t[1],v),mkVarDen(t[3]),mkVarDen(t[4])];}
}
if(t[0]=='=') {
var ck = defined(v,[t[3],t[4]]);
if(!ck[0]) {
throw 'ERROR: cannot evaluate. The symbol '+ck[1]+' occurs in the formula you entered as either a constant that has no extension in the loaded model, or as an unbound variable.';
} else {return [t[0],t[1],mkIdDen(t[2],t[1],v),mkVarDen(t[3]),mkVarDen(t[4])];}
} else throw "ERROR: can't identify tree in insertDen(). t[0] is:"+t[0];
function defined(v,ar) {
for(var i=0;i<ar.length;i++) {
if(v.indexOf(ar[i])<0 && CONSTANTS[ar[i]]===undefined) {
return [false,ar[i]];
}
}
return [true,[]];
}
}
// FORMULA PARSING CODE
//====================
/* THE GRAMMAR
S ::= Q S | U S | '(' S B S ')' | A
Q ::= '(A' V ')' | '(E' V ')'
U ::= '~'
B ::= '&' | 'v' | '>' | '<>'
A ::= '#' | T=T | P | P T | P T T | P T T T ...
P ::= 'A' | 'B' | 'C' | 'D' | ...
T ::= C | V
C :: = 'a' | 'b' | 'c' | 'd'
V :: = 'w' | 'x' | 'y' | 'z'
*/
// String -> Tree
// Takes a string and if it's a wff, returns a parse tree of the string, otherwise
// returns an empty array. The first element of any parse tree is always an identifier
// of the type of wff (Q = quantifier wff, B = binary connective wff, P = predicational
// wff etc.) the second element is the wff that's being parsed, and the rest
// is the actual parsing. E.g. "Ax(~Fx&Gx)" parses to:
// [Q,Ax(~Fx&Gx),A,x,[B,(~Fx&Gx),[U,~Fx,~,[P,Fx,F,x]],&,[P,Gx,G,x]]]
function parse(s) {
s = s.replace(/ /g,'');
if(s=='') {return [];}
var s1 = [];
var s2 = [];
if(isQ(s)) {
s1 = parse(s.substring(2));
return s1.length ? ['Q',s,s.charAt(0),s.charAt(1),s1] : [];
}
if(isU(s[0])) {
s1 = parse(s.substring(1));
return s1.length ? ['U',s,s[0],s1] : [];
}
if(s[0] =='(' && s[s.length-1]==')') {
var a = gSub(s);
if(a.indexOf(undefined)>=0 || a.indexOf('')>=0) {
return [];
} else {
s1 = parse(a[0]);
s2 = parse(a[2]);
if(s1.length && s2.length) {
return ['B',s,s1,a[1],s2];
} else {return [];}
}
}
if(isAt(s)) {
if(s.length==1) {
return ['0p',s,s];
}
if(s.length==2) {
return ['1p',s,s[0],s[1]];
}
if(s.length==3 && s[1]=='=') {
return ['=',s,s[1],s[0],s[2]];
}
if(s.length==3) {
return ['2p',s,s[0],s[1],s[2]];
} else {return [];} // not allowing n-place predicates for n>2
} else {return [];}
}
// String -> Bool
// Determines if s is an atomic wff
function isAt(s) {
var pr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if(s.length==1 && s==='#') {return true;}
if(s.length==3 && isT(s[0]) && s[1]=='=' && isT(s[2])) {
return true;
}
if(pr.indexOf(s[0])>=0) {
if(s.length==1) {
return true;
} else {
for(var i=1;i<s.length;i++) {
if(!isT(s[i])) {
return false;
}
}
return true;
}
} else {return false;}
}
// String -> Bool
// Determines if s begins with a quantifier
function isQ(s) {
var q = ['E','A'];
if(s.length>2 && q.indexOf(s[0])>=0 && isV(s[1]) && !isB(s[2]) && !(isV(s[2]) && s[3]!=='=')) {
return true;
} else {return false;}
}
// String -> Bool
// Determines if s begins with a unary connective
function isU(s) {
var u = ['~'];
for(var i=0;i<u.length;i++) {
if(s.indexOf(u[i])==0) {return true;}
}
return false;
}
// String -> [String]
// takes a string beginning with '(' and ending with ')', and determines if there is a
// binary connective enclosed only by the outermost parentheses. If so, returns an array
// with the string to the left and the string to the right of the binary connective;
// otherwise returns an array of three undefined's.
function gSub(s) {
var stk = [];
var l = 0;
for(var i=0;i<s.length;i++) {
if(s[i]=='(') {
stk.push('(');
} else if(s[i]==')' && stk.length>0) {
stk.pop();
} else if(stk.length==1 && (l = isB(s.substring(i)))>0) {
return [s.substring(1,i),s.substring(i,i+l),s.substring(i+l,s.length-1)];
}
}
return [undefined,undefined,undefined];
}
// String -> Int
// takes a string and determines if it begins with a binary connective. If so, returns
// the length of the connective, otherwise returns 0.
function isB(s) {
var bc = ['&','v','>','<>'];
for(var i=0;i<bc.length;i++) {
if(s.indexOf(bc[i]) == 0) {
return bc[i].length;
}
}
return 0;
}
// Char -> Bool
// Determines if c is a term
function isT(c) {
return (isC(c) || isV(c));
}
// Char -> Bool
// Determines if c is a variable.
function isV(c) {
return 'abcdefghijklmnopqrstuwxyz'.indexOf(c)>=0;
}
// Char -> Bool
// Determines if c is a constant
function isC(c) {
return 'abcdefghijklmnopqrstuwxyz'.indexOf(c)>=0;
}