This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
propositions.py
196 lines (140 loc) · 5.37 KB
/
propositions.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
from continuation import EndContinuation, PropContinuation, \
EXContinuation, EUContinuation, EGContinuation
from pypy.rlib import jit
class Proposition(object):
__slots__ = ('cached_label', )
_immutable_ = True
def __init__(self):
self.cached_label = None
def evaluate(self, state, *args):
raise NotImplementedError('abstract base class')
def _label(self):
raise NotImplementedError('abstract base class')
def __eq__(self, other):
return self.__class__ == other.__class__
def __neq__(self, other):
return not self == other
@jit.purefunction
def label(self):
if not self.cached_label:
self.cached_label = self._label()
return self.cached_label
__str__ = label
__repr__ = label
class LessProposition(Proposition):
__slots__ = ('left', 'right')
_immutable_ = True
def __init__(self, left, right):
Proposition.__init__(self)
self.left = left
self.right = right
def evaluate(self, state, s, f):
if self.left.eval(state) < self.right.eval(state):
return s, f, state
return f, s, state
@jit.purefunction
def _label(self):
return "(%s < %s)" % (self.left.label(), self.right.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.left == other.left and self.right == other.right
class EqualsProposition(Proposition):
_immutable_ = True
def __init__(self, left, right):
Proposition.__init__(self)
self.left = left
self.right = right
def evaluate(self, state, s, f):
if self.left.eval(state) == self.right.eval(state):
return s, f, state
return f, s, state
@jit.purefunction
def _label(self):
return "(%s = %s)" % (self.left.label(), self.right.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.left == other.left and self.right == other.right
class NonCachingProposition(Proposition):
pass
class TrueProposition(NonCachingProposition):
_immutable_ = True
def evaluate(self, state, s, f):
return s, f, state
@jit.purefunction
def _label(self):
return "true"
class FalseProposition(NonCachingProposition):
_immutable_ = True
def evaluate(self, state, s, f):
return f, s, state
@jit.purefunction
def _label(self):
return "false"
class NegationProposition(Proposition):
_immutable_ = True
def __init__(self, proposition):
Proposition.__init__(self)
self.proposition = proposition
def evaluate(self, state, s, f):
return PropContinuation(self.proposition, f, s), s, state
@jit.purefunction
def _label(self):
return "not(%s)" % self.proposition.label()
def __eq__(self, other):
return self.__class__ == other.__class__ and self.proposition == other.proposition
class AndProposition(Proposition):
_immutable_ = True
def __init__(self, left, right):
Proposition.__init__(self)
self.left = left
self.right = right
def evaluate(self, state, s, f):
return PropContinuation(self.left, PropContinuation(self.right, s, f), f), EndContinuation(False), state
@jit.purefunction
def _label(self):
return "and(%s, %s)" % (self.left.label(), self.right.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.left == other.left and self.right == other.right
def OrProposition(left, right):
return NegationProposition(AndProposition(NegationProposition(left), NegationProposition(right)))
class EUProposition(Proposition):
__slots__ = ('first', 'second')
_immutable_ = True
def __init__(self, first, second):
Proposition.__init__(self)
self.first = first
self.second = second
@jit.purefunction
def _label(self):
return 'E(%s U %s)' % (self.first.label(), self.second.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.first == other.first and self.second == other.second
def evaluate(self, state, s, f):
ff = EUContinuation(self, s, f)
fail = PropContinuation(self.first, ff, f)
return PropContinuation(self.second, s, fail), f, state
class EGProposition(Proposition):
__slots__ = 'proposition'
_immutable_ = True
def __init__(self, proposition):
Proposition.__init__(self)
self.proposition = proposition
@jit.purefunction
def _label(self):
return 'EG(%s)' % (self.proposition.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.proposition == other.proposition
def evaluate(self, state, s, f):
p = EGContinuation(self, s, f)
return PropContinuation(self.proposition, p, f, False), f, state
class EXProposition(Proposition):
__slots__ = 'proposition'
_immutable_ = True
def __init__(self, proposition):
Proposition.__init__(self)
self.proposition = proposition
@jit.purefunction
def _label(self):
return 'EX(%s)' % (self.proposition.label())
def __eq__(self, other):
return self.__class__ == other.__class__ and self.proposition == other.proposition
def evaluate(self, state, s, f):
return EXContinuation(self.proposition, state, s, f), f, state