-
Notifications
You must be signed in to change notification settings - Fork 30
/
formula.py
144 lines (107 loc) · 3.39 KB
/
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
134
135
136
137
138
139
140
141
142
143
144
"""This module is designed to define formulas in propositional logic.
For example, the following piece of code creates an object representing (p v s).
formula1 = Or(Atom('p'), Atom('s'))
As another example, the piece of code below creates an object that represents (p → (p v s)).
formula2 = Implies(Atom('p'), Or(Atom('p'), Atom('s')))
"""
# from typeguard import typechecked
class Formula:
def __init__(self):
pass
# end def
# end class Formula
class Atom(Formula):
"""
This class represents propositional logic variables.
"""
def __init__(self, name: str):
super().__init__()
self.name = name
# end def
def __str__(self):
return str(self.name)
# end def
def __eq__(self, other: Formula):
return isinstance(other, Atom) and other.name == self.name
# end def
def __hash__(self):
return hash((self.name, 'atom'))
# end def
# end class Atom
class Implies(Formula):
def __init__(self, left: Formula, right: Formula):
super().__init__()
self.left = left
self.right = right
# end def
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u2192" + " " + self.right.__str__() + ")"
# end def
def __eq__(self, other: Formula):
return isinstance(other, Implies) and other.left == self.left and other.right == self.right
# end def
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'implies'))
# end def
# end class Implies
class Not(Formula):
def __init__(self, inner: Formula):
super().__init__()
self.inner = inner
# end def
def __str__(self):
return "(" + u"\u00ac" + str(self.inner) + ")"
# end def
def __eq__(self, other: Formula):
return isinstance(other, Not) and other.inner == self.inner
# end def
def __hash__(self):
return hash((hash(self.inner), 'not'))
# end def
# end class Not
class And(Formula):
def __init__(self, left: Formula, right: Formula):
super().__init__()
self.left = left
self.right = right
# end def
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u2227" + " " + self.right.__str__() + ")"
# end def
def __eq__(self, other: Formula):
return isinstance(other, And) and other.left == self.left and other.right == self.right
# end def
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'and'))
# end def
# end class And
class Or(Formula):
def __init__(self, left: Formula, right: Formula):
super().__init__()
self.left = left
self.right = right
# end def
def __str__(self):
return "(" + self.left.__str__() + " " + u"\u2228" + " " + self.right.__str__() + ")"
# end def
def __eq__(self, other: Formula):
return isinstance(other, Or) and other.left == self.left and other.right == self.right
# end def
def __hash__(self):
return hash((hash(self.left), hash(self.right), 'or'))
# end def
# end class Or
class Iff:
"""
Describes the 'if and only if' logical connective (<->) from propositional logic.
Unicode value for <-> is 2194.
"""
pass
# end class Iff
class Xor:
"""
Describes the xor (exclusive or) logical connective from propositional logic.
Unicode value for xor is 2295.
"""
pass
# end class Xor