forked from asaparov/prontoqa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fol.py
628 lines (538 loc) · 17.5 KB
/
fol.py
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
class FOLFormula(object):
def __init__(self):
return
class FOLAnd(FOLFormula):
def __init__(self, conjuncts):
self.operands = conjuncts
def __eq__(self, other):
if type(other) != FOLAnd:
return False
if len(self.operands) != len(other.operands):
return False
for i in range(len(self.operands)):
if self.operands[i] != other.operands[i]:
return False
return True
def __ne__(self, other):
if type(other) != FOLAnd:
return True
if len(self.operands) != len(other.operands):
return True
for i in range(len(self.operands)):
if self.operands[i] != other.operands[i]:
return True
return False
def __hash__(self):
value = 0
for operand in self.operands:
value ^= hash(operand)
return hash((FOLAnd, value))
def apply(self, func):
operands = []
for operand in self.operands:
operands.append(func(operand))
return FOLAnd(operands)
def visit(self, visit):
for operand in self.operands:
visit(operand)
class FOLOr(FOLFormula):
def __init__(self, disjuncts):
self.operands = disjuncts
def __eq__(self, other):
if type(other) != FOLOr:
return False
if len(self.operands) != len(other.operands):
return False
for i in range(len(self.operands)):
if self.operands[i] != other.operands[i]:
return False
return True
def __ne__(self, other):
if type(other) != FOLOr:
return True
if len(self.operands) != len(other.operands):
return True
for i in range(len(self.operands)):
if self.operands[i] != other.operands[i]:
return True
return False
def __hash__(self):
value = 0
for operand in self.operands:
value ^= hash(operand)
return hash((FOLOr, value))
def apply(self, func):
operands = []
for operand in self.operands:
operands.append(func(operand))
return FOLOr(operands)
def visit(self, visit):
for operand in self.operands:
visit(operand)
class FOLNot(FOLFormula):
def __init__(self, operand):
self.operand = operand
def __eq__(self, other):
if type(other) != FOLNot:
return False
return self.operand == other.operand
def __ne__(self, other):
if type(other) != FOLNot:
return True
return self.operand != other.operand
def __hash__(self):
return hash((FOLNot, self.operand))
def apply(self, func):
return FOLNot(func(self.operand))
def visit(self, visit):
visit(self.operand)
class FOLIfThen(FOLFormula):
def __init__(self, antecedent, consequent):
self.antecedent = antecedent
self.consequent = consequent
def __eq__(self, other):
if type(other) != FOLIfThen:
return False
return self.antecedent == other.antecedent and self.consequent == other.consequent
def __ne__(self, other):
if type(other) != FOLIfThen:
return True
return self.antecedent != other.antecedent or self.consequent != other.consequent
def __hash__(self):
return hash((FOLIfThen, self.antecedent, self.consequent))
def apply(self, func):
return FOLIfThen(func(self.antecedent), func(self.consequent))
def visit(self, visit):
visit(self.antecedent)
visit(self.consequent)
class FOLIff(FOLFormula):
def __init__(self, antecedent, consequent):
self.antecedent = antecedent
self.consequent = consequent
def __eq__(self, other):
if type(other) != FOLIff:
return False
return self.antecedent == other.antecedent and self.consequent == other.consequent
def __ne__(self, other):
if type(other) != FOLIff:
return True
return self.antecedent != other.antecedent or self.consequent != other.consequent
def __hash__(self):
return hash((FOLIff, self.antecedent, self.consequent))
def apply(self, func):
return FOLIff(func(self.antecedent), func(self.consequent))
def visit(self, visit):
visit(self.antecedent)
visit(self.consequent)
class FOLEquals(FOLFormula):
def __init__(self, left, right):
self.left = left
self.right = right
def __eq__(self, other):
if type(other) != FOLEquals:
return False
return self.left == other.left and self.right == other.right
def __ne__(self, other):
if type(other) != FOLEquals:
return True
return self.left != other.left or self.right != other.right
def __hash__(self):
return hash((FOLEquals, self.left, self.right))
def apply(self, func):
return FOLEquals(func(self.left), func(self.right))
def visit(self, visit):
visit(self.left)
visit(self.right)
class FOLForAll(FOLFormula):
def __init__(self, variable, operand):
self.variable = variable
self.operand = operand
def __eq__(self, other):
if type(other) != FOLForAll:
return False
return self.variable == other.variable and self.operand == other.operand
def __ne__(self, other):
if type(other) != FOLForAll:
return True
return self.variable != other.variable or self.operand != other.operand
def __hash__(self):
return hash((FOLForAll, self.variable, self.operand))
def apply(self, func):
return FOLForAll(self.variable, func(self.operand))
def visit(self, visit):
visit(self.operand)
class FOLExists(FOLFormula):
def __init__(self, variable, operand):
self.variable = variable
self.operand = operand
def __eq__(self, other):
if type(other) != FOLExists:
return False
return self.variable == other.variable and self.operand == other.operand
def __ne__(self, other):
if type(other) != FOLExists:
return True
return self.variable != other.variable or self.operand != other.operand
def __hash__(self):
return hash((FOLExists, self.variable, self.operand))
def apply(self, func):
return FOLExists(self.variable, func(self.operand))
def visit(self, visit):
visit(self.operand)
class FOLFuncApplication(FOLFormula):
def __init__(self, function, args):
self.function = function
self.args = args
def __eq__(self, other):
if type(other) != FOLFuncApplication:
return False
if len(self.args) != len(other.args):
return False
if self.function != other.function:
return False
for i in range(len(self.args)):
if self.args[i] != other.args[i]:
return False
return True
def __ne__(self, other):
if type(other) != FOLFuncApplication:
return True
if len(self.args) != len(other.args):
return True
if self.function != other.function:
return True
for i in range(len(self.args)):
if self.args[i] != other.args[i]:
return True
return False
def __hash__(self):
value = 0
for operand in self.args:
value ^= hash(operand)
return hash((FOLFuncApplication, self.function, value))
def apply(self, func):
return FOLFuncApplication(self.function, [func(arg) for arg in self.args])
def visit(self, visit):
for arg in self.args:
visit(arg)
class FOLTerm(object):
def __init__(self):
return
class FOLVariable(FOLTerm):
def __init__(self, variable):
self.variable = variable
def __eq__(self, other):
if type(other) != FOLVariable:
return False
return self.variable == other.variable
def __ne__(self, other):
if type(other) != FOLVariable:
return True
return self.variable != other.variable
def __hash__(self):
return hash((FOLVariable, self.variable))
def apply(self, func):
return FOLVariable(self.variable)
def visit(self, visit):
return
class FOLConstant(FOLTerm):
def __init__(self, constant):
self.constant = constant
def __eq__(self, other):
if type(other) != FOLConstant:
return False
return self.constant == other.constant
def __ne__(self, other):
if type(other) != FOLConstant:
return True
return self.constant != other.constant
def __hash__(self):
return hash((FOLConstant, self.constant))
def apply(self, func):
return FOLConstant(self.constant)
def visit(self, visit):
return
class FOLNumber(FOLTerm):
def __init__(self, number):
self.number = number
def __eq__(self, other):
if type(other) != FOLNumber:
return False
return self.number == other.number
def __ne__(self, other):
if type(other) != FOLNumber:
return True
return self.number != other.number
def __hash__(self):
return hash((FOLNumber, self.number))
def apply(self, func):
return FOLNumber(self.number)
def visit(self, visit):
return
def substitute(formula, src, dst):
def apply_substitute(f):
if f == src:
return dst
elif type(src) == FOLVariable and type(f) in [FOLForAll, FOLExists] and src.variable == f.variable:
return f
else:
return f.apply(apply_substitute)
return apply_substitute(formula)
def copy(formula):
def apply_identity(f):
return f.apply(apply_identity)
return apply_identity(formula)
def max_variable(formula):
max_var = 0
def max_variable_visit(f):
nonlocal max_var
if type(f) == FOLVariable or type(f) == FOLForAll or type(f) == FOLExists:
max_var = max(max_var, f.variable)
f.visit(max_variable_visit)
max_variable_visit(formula)
return max_var
def free_variables(formula):
free_variables = []
bound_variables = []
def free_variables_visit(f):
nonlocal bound_variables, free_variables
if type(f) == FOLVariable and f.variable not in bound_variables and f.variable not in free_variables:
free_variables.append(f.variable)
if type(f) == FOLForAll or type(f) == FOLExists:
bound_variables.append(f.variable)
f.visit(free_variables_visit)
if type(f) == FOLForAll or type(f) == FOLExists:
bound_variables.pop()
free_variables_visit(formula)
return free_variables
def bound_variables(formula):
bound_variables = []
def bound_variables_visit(f):
nonlocal bound_variables
if (type(f) == FOLForAll or type(f) == FOLExists) and f.variable not in bound_variables:
bound_variables.append(f.variable)
f.visit(bound_variables_visit)
bound_variables_visit(formula)
return bound_variables
def predicates(formula):
predicate_list = []
def predicates_visit(f):
nonlocal predicate_list
if type(f) == FOLFuncApplication:
predicate_list.append(f.function)
f.visit(predicates_visit)
predicates_visit(formula)
return predicate_list
def contains(formula, subtree):
found = False
def visit(f):
nonlocal found
if found:
return
elif f == subtree:
found = True
else:
f.visit(visit)
formula.visit(visit)
return found
def parse_fol_term_from_tptp(string, pos, var_map):
end = pos
if string[end] == '"':
end += 1
while True:
if string[end] == '"' and string[end - 1] != '\\':
break
end += 1
end += 1
return FOLConstant(string[pos:end]), end
must_be_var = False
if string[end] == '$':
must_be_var = True
end += 1
while string[end].isdigit() or string[end].isalpha() or string[end] == '≥' or string[end] == '.' or string[end] == '_':
end += 1
if string[pos:end] in var_map:
term = FOLVariable(var_map[string[pos:end]])
else:
if must_be_var:
raise Exception(f"parse_fol_term_from_tptp ERROR at {pos+1}: Variable undeclared.")
term = FOLConstant(string[pos:end])
if string[end] == '(':
end += 1
args = []
while True:
arg, end = parse_fol_term_from_tptp(string, end, var_map)
args.append(arg)
if string[end] == ',':
end += 1
continue
elif string[end] == ')':
end += 1
break
else:
raise Exception(f"parse_fol_term_from_tptp ERROR at {end+1}: Expected a closing parenthesis or comma in argument array.")
if type(term) == FOLConstant:
term = FOLFuncApplication(term.constant, args)
else:
term = FOLFuncApplication(term, args)
return term, end
def parse_fol_quantifier_from_tptp(string, pos, var_map):
index = string.find(']:', pos)
var_names = string[pos:index].split(',')
variables = []
for var_name in var_names:
if var_name in var_map:
raise Exception(f"parse_fol_from_tptp ERROR at {pos+1}: Variable '{var_name}' already declared in this scope.")
variable = len(var_map) + 1
var_map[var_name] = variable
variables.append(variable)
(formula, pos) = parse_fol_literal_from_tptp(string, index + len(']:'), var_map)
for var_name in var_names:
del var_map[var_name]
return variables, formula, pos
def parse_fol_literal_from_tptp(string, pos, var_map):
if string.startswith('?[', pos):
(variables, formula, pos) = parse_fol_quantifier_from_tptp(string, pos + len('?['), var_map)
for variable in reversed(variables):
formula = FOLExists(variable, formula)
elif string.startswith('![', pos):
(variables, formula, pos) = parse_fol_quantifier_from_tptp(string, pos + len('!['), var_map)
for variable in reversed(variables):
formula = FOLForAll(variable, formula)
elif string.startswith('~', pos):
(operand, pos) = parse_fol_literal_from_tptp(string, pos + 1, var_map)
formula = FOLNot(operand)
elif string.startswith('(', pos):
(formula, pos) = parse_fol_from_tptp(string, pos + 1, var_map)
if string[pos] != ')':
raise Exception(f"parse_fol_from_tptp ERROR at {pos+1}: Expected a closing parenthesis.")
pos += 1
else:
formula, pos = parse_fol_term_from_tptp(string, pos, var_map)
if pos < len(string) and string[pos] == '=':
other, pos = parse_fol_literal_from_tptp(string, pos + 1, var_map)
formula = FOLEquals(formula, other)
return formula, pos
def parse_fol_from_tptp(string, pos, var_map):
while string[pos].isspace():
pos += 1
if string.startswith('(', pos):
(formula, pos) = parse_fol_from_tptp(string, pos + 1, var_map)
if string[pos] != ')':
raise Exception(f"parse_fol_from_tptp ERROR at {pos+1}: Expected a closing parenthesis.")
return formula, pos + 1
formula, pos = parse_fol_literal_from_tptp(string, pos, var_map)
while pos < len(string) and string[pos].isspace():
pos += 1
if pos < len(string) and (string[pos] == '&' or string[pos] == '|'):
operands = [formula]
operator = string[pos]
pos += 1
while True:
while string[pos].isspace():
pos += 1
other, pos = parse_fol_literal_from_tptp(string, pos, var_map)
operands.append(other)
if pos == len(string):
break
while string[pos].isspace():
pos += 1
if string[pos] == operator:
pos += 1
else:
break
if operator == '&':
formula = FOLAnd(operands)
else:
formula = FOLOr(operands)
if string.startswith('=>', pos):
pos += len('=>')
while string[pos].isspace():
pos += 1
other, pos = parse_fol_from_tptp(string, pos, var_map)
return FOLIfThen(formula, other), pos
else:
return formula, pos
def do_parse_fol_from_tptp(string):
var_map = {}
formula, pos = parse_fol_from_tptp(string, 0, var_map)
return formula
def fol_to_tptp(formula):
if type(formula) == FOLVariable:
return f'X{formula.variable}'
elif type(formula) == FOLConstant:
return formula.constant
elif type(formula) == FOLNumber:
return formula.number
elif type(formula) == FOLFuncApplication:
return formula.function + '(' + ','.join([fol_to_tptp(arg) for arg in formula.args]) + ')'
elif type(formula) == FOLAnd:
return '(' + ' & '.join([fol_to_tptp(operand) for operand in formula.operands]) + ')'
elif type(formula) == FOLOr:
return '(' + ' | '.join([fol_to_tptp(operand) for operand in formula.operands]) + ')'
elif type(formula) == FOLNot:
return '~' + fol_to_tptp(formula.operand)
elif type(formula) == FOLIfThen:
return '(' + fol_to_tptp(formula.antecedent) + ' => ' + fol_to_tptp(formula.consequent) + ')'
elif type(formula) == FOLIff:
return '(' + fol_to_tptp(formula.antecedent) + ' <=> ' + fol_to_tptp(formula.consequent) + ')'
elif type(formula) == FOLEquals:
return '(' + fol_to_tptp(formula.left) + '=' + fol_to_tptp(formula.right) + ')'
elif type(formula) == FOLForAll:
return f'![X{formula.variable}]:(' + fol_to_tptp(formula.operand) + ')'
elif type(formula) == FOLExists:
return f'?[X{formula.variable}]:(' + fol_to_tptp(formula.operand) + ')'
elif type(formula) == FOLFuncApplication:
return formula.function + '(' + ','.join([fol_to_tptp(arg) for arg in formula.args]) + ')'
else:
raise Exception(f"fol_to_tptp ERROR: Unrecognized formula type {type(formula)}.")
def unify_term(first, second, first_var_map, second_var_map):
if type(first) == FOLVariable:
if not isinstance(second, FOLTerm):
return False
if first.variable in first_var_map:
return first_var_map[first.variable] == second
else:
first_var_map[first.variable] = second
return True
elif type(second) == FOLVariable:
if not isinstance(first, FOLTerm):
return False
if second.variable in second_var_map:
return second_var_map[second.variable] == first
else:
second_var_map[second.variable] = first
return True
else:
return first == second
def unify(first, second, first_var_map, second_var_map):
if type(first) != type(second):
return False
elif type(first) == FOLAnd or type(first) == FOLOr:
if len(first.operands) != len(second.operands):
return False
for i in range(len(first.operands)):
if not unify(first.operands[i], second.operands[i], first_var_map, second_var_map):
return False
elif type(first) == FOLNot:
return unify(first.operand, second.operand, first_var_map, second_var_map)
elif type(first) == FOLIfThen or type(first) == FOLIff:
return unify(first.antecedent, second.antecedent, first_var_map, second_var_map) and unify(first.consequent, second.consequent, first_var_map, second_var_map)
elif type(first) == FOLEquals:
return unify_term(first.left, second.left, first_var_map, second_var_map) and unify_term(first.right, second.right, first_var_map, second_var_map)
elif type(first) == FOLForAll or type(first) == FOLExists:
first_var_map[first.variable] = FOLVariable(second.variable)
second_var_map[second.variable] = FOLVariable(first.variable)
if not unify(first.operand, second.operand, first_var_map, second_var_map):
return False
del first_var_map[first.variable]
del second_var_map[second.variable]
elif type(first) == FOLFuncApplication:
if first.function != second.function or len(first.args) != len(second.args):
return False
for i in range(len(first.args)):
if not unify_term(first.args[i], second.args[i], first_var_map, second_var_map):
return False
else:
raise Exception(f"unify ERROR: Unrecognized formula type {type(first)}.")
return True