-
Notifications
You must be signed in to change notification settings - Fork 30
/
fol_formula.py
133 lines (91 loc) · 3.57 KB
/
fol_formula.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
class FormulaFOL:
def __init__(self):
pass
class Atom(FormulaFOL):
def __init__(self, name, args):
super().__init__()
self.name = name
self.args = args
def __str__(self):
printable_predicate = str(self.name) + "("
for i in range(len(self.args)):
if i == len(self.args) - 1:
printable_predicate = printable_predicate + str(self.args[i]) + ")"
else:
printable_predicate = printable_predicate + str(self.args[i]) + ', '
return printable_predicate
def __eq__(self, other):
is_equal = isinstance(other, Atom) and len(other.args) == len(self.args)
if not is_equal:
return False
for i in range(len(self.args)):
if self.args[i] != other.args[i]:
return False
return True
def __hash__(self):
return hash(tuple(self.args) + (self.name, 'atom'))
class Implies(FormulaFOL):
def __init__(self, left, right):
super().__init__()
self.left = left
self.right = right
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u27F6" + " " + self.right.__str__() + ")"
def __eq__(self, other):
return isinstance(other, Implies) and other.left == self.left and other.right == self.right
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'implies'))
class Not(FormulaFOL):
def __init__(self, inner):
super().__init__()
self.inner = inner
def __str__(self):
return "(" + u"\u00ac" + str(self.inner) + ")"
def __eq__(self, other):
return isinstance(other, Not) and other.inner == self.inner
def __hash__(self):
return hash((hash(self.inner), 'not'))
class And(FormulaFOL):
def __init__(self, left, right):
super().__init__()
self.left = left
self.right = right
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u2227" + " " + self.right.__str__() + ")"
def __eq__(self, other):
return isinstance(other, And) and other.left == self.left and other.right == self.right
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'and'))
class Or(FormulaFOL):
def __init__(self, left, right):
super().__init__()
self.left = left
self.right = right
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u2228" + " " + self.right.__str__() + ")"
def __eq__(self, other):
return isinstance(other, Or) and other.left == self.left and other.right == self.right
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'or'))
class ForAll(FormulaFOL):
def __init__(self, var, inner):
super().__init__()
self.inner = inner
self.var = var
def __str__(self):
return "(" + u"\u2200" + str(self.var) + str(self.inner) + ")"
def __eq__(self, other):
return isinstance(other, ForAll) and other.inner == self.inner and other.var == self.var
def __hash__(self):
return hash((hash(self.inner), 'all', hash(self.var)))
class Exists(FormulaFOL):
def __init__(self, var, inner):
super().__init__()
self.inner = inner
self.var = var
def __str__(self):
return "(" + u"\u2203" + str(self.var) + str(self.inner) + ")"
def __eq__(self, other):
return isinstance(other, Exists) and other.var == self.var and other.inner == self.inner
def __hash__(self):
return hash((hash(self.inner), 'exists', hash(self.var)))