-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclause_form.py
129 lines (117 loc) · 4.91 KB
/
clause_form.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
from fol import *
def remove_equivalences(statement):
if isinstance(statement, Predicate):
pass
elif isinstance(statement, Equivalence):
statement = remove_equivalences(statement.get_implications())
elif isinstance(statement, Quantifier):
statement.statement = remove_equivalences(statement.statement)
elif hasattr(statement, "get_children"):
statement.set_children([remove_equivalences(s) for s in statement.get_children()])
return statement
def remove_implications(statement):
if isinstance(statement, Predicate):
pass
elif isinstance(statement, Implication):
statement = remove_implications(statement.get_or())
elif isinstance(statement, Quantifier):
statement.statement = remove_implications(statement.statement)
elif hasattr(statement, "get_children"):
statement.set_children([remove_implications(c) for c in statement.get_children()])
return statement
def get_new_variable(used_names):
for i in range(1, 5):
for char in range(ord("z"), ord("k"), -1):
char = chr(char)*i
if char not in used_names:
return char
def get_new_constant(used_names):
for i in range(1, 5):
for char in range(ord("a"), ord("k")):
char = chr(char)*i
if char not in used_names:
return char
def standardize_apart(statement, scope={}, used_names=[]):
if isinstance(statement, Variable):
if statement.name in scope:
statement.name = scope[statement.name]
else:
raise Exception()
elif isinstance(statement, Quantifier):
if statement.variable_name not in used_names:
scope[statement.variable_name] = statement.variable_name
used_names.append(statement.variable_name)
else:
sub = get_new_variable(used_names)
scope[statement.variable_name] = sub
statement.variable_name = sub
used_names.append(sub)
statement.statement = standardize_apart(statement.statement, scope, used_names)
elif hasattr(statement, "get_children"):
statement.set_children([standardize_apart(s, scope, used_names) for s in statement.get_children()])
return statement
def get_functions(statement):
if isinstance(statement, Function):
return [statement.name]
if hasattr(statement, "get_children"):
return [f for s in statement.get_children() for f in get_functions(s)]
else:
return []
def skolemize(statement, to_skolemize={}, quantified_variables=[], used_names=None):
if used_names is None:
used_names = get_functions(statement)
if isinstance(statement, Variable):
if statement.name in to_skolemize:
statement = Function(to_skolemize[statement.name], *[Variable(v) for v in quantified_variables])
elif isinstance(statement, ThereExists):
new_constant = get_new_constant(used_names)
to_skolemize[statement.variable_name] = new_constant
used_names.append(new_constant)
statement = skolemize(statement.statement, to_skolemize, quantified_variables, used_names)
elif isinstance(statement, ForAll):
quantified_variables.append(statement.variable_name)
statement.statement = skolemize(statement.statement, to_skolemize, quantified_variables, used_names)
elif hasattr(statement, "get_children"):
statement.set_children([skolemize(s, to_skolemize, quantified_variables, used_names) for s in statement.get_children()])
return statement
def discard_forall(statement):
if isinstance(statement, ForAll):
statement = statement.statement
if hasattr(statement, "get_children"):
statement.set_children([discard_forall(s) for s in statement.get_children()])
return statement
def cnf(statement):
print(statement)
print("\nremove equivalences")
statement = remove_equivalences(statement)
print(statement)
print("\nremove implications")
statement = remove_implications(statement)
print(statement)
print("\npush negation")
statement = statement.push_negation()
print(statement)
print('\nStandardize Apart')
statement = standardize_apart(statement)
print(statement)
print('\nSkolemize')
statement = skolemize(statement)
print(statement)
print('\nRemoving For All quatifiers')
statement = discard_forall(statement)
print(statement)
if __name__ == "__main__":
p1 = Predicate('P', Variable('x'))
p2 = Predicate('Q', Variable('x'))
p3 = Predicate('Q', Variable('y'))
f1 = Predicate('R', Variable('y'), Variable('x'))
expression = ForAll('x', Equivalence(p1, And([p2, ThereExists('y',And([p3,f1 ]) )]) ) )
cnf(expression)
# test expressions for standardize apart
# expression = ForAll('x', ThereExists('y', ThereExists('y', Predicate('p',Variable('x')))))
# expression = And([ForAll('x',Predicate('P', Variable('x'))), ThereExists('x',Predicate('P', Variable('x'))),ThereExists('x',Predicate('P', Variable('x')))])
# expression = ForAll('x', And([ ThereExists('y', Predicate('p',Variable('y'))), ThereExists('y', Predicate('p',Variable('y')))]))
# print(expression)
# print('\n\n')
# expression = remove_implications(expression)
# print(standardize_apart(expression))