-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
executable file
·581 lines (445 loc) · 16.6 KB
/
data.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict
# To model modal logic, we have here a Kripke class
class Kripke:
"""
Models a propositional Kripke model
"""
def __init__(self):
self.V = defaultdict(set) # dict { var -> set(worlds) }
self.W = set() # set of world (strings)
self.R = defaultdict(set) # dict { world -> set(worlds) }
self._cached_blind_worlds = None
def entails(self, expression):
"Checks whether our model 𝓜 entails the expression"
return sorted(self.W) == sorted(expression.calc(self))
def blind_worlds(self):
if self._cached_blind_worlds != None:
return self._cached_blind_worlds
self._cached_blind_worlds = set(w for w in self.W if not self.R[w])
return self._cached_blind_worlds
def add_vals(self, var, ws):
"Adds valuations to the worlds (aka V(p) = {w1, w2, w3})"
self.V[var].update(ws)
return self
def add_val(self, var, w):
"Adds world w to V(var) set"
self.V[var].add(w)
return self
def add_worlds(self, ws):
"Adds worlds ws"
self.W.update(ws)
return self
def add_world(self, w):
"Adds a world w"
self.W.add(w)
return self
def add_transes(self, ts):
"Adds transistions between worlds, ts is a sequence of tuples"
for (a,b) in ts:
self.R[a].add(b)
return self
def add_trans(self, t):
"Adds transition between worlds, t is a tuple of worlds"
self.R[t[0]].add(t[1])
return self
def __repr__(self):
return "Kripke(W=%s, R=%s, V=%s)" % (str(self.W), str(self.R), str(self.V))
def __str__(self):
"Stringifies the object"
return "W = {%s}\nR = {%s}\n%s" % (
", ".join(self.W),
", ".join('(%s, %s)' % (v,w) for v in self.R for w in self.R[v]),
"\n".join("V(%s) = {%s}" % (var, ", ".join(self.V[var])) for var in self.V)
)
# Data components for logic expressions
# Should be easily extensible and adaptable
#
# These data components are supposed to be immutable.
#
# Currently implemented are the Objects
# And, Or, Implies, Not, Var, Constant (true or false)
class LogicExpression:
"""
Logic Expression is a parent-class, a sort of interface all Logical
Expressions have to uphold
"""
class_name = None # Text representing class
symbols = [] # A data container for all operators for expr
out_symbol = None # The symbol used to output
def __init__(self):
"""
Logic expressions are initialised by calling create_expression, rather
than their own constructors, this avoids having 2 (a -> a) objects in
for instance sub expressions
"""
raise NotImplementedError()
def __repr__(self):
"""
repr is the way for the logic expression to show itself as an object structure.
for instance: And(Or(Var(a), Var(b)), Constant(False))
"""
raise NotImplementedError()
def __str__(self):
"""
str is the way for the logic expression to be human readable
for instance: (a ∨ b) ∧ 0
"""
raise NotImplementedError()
def calc(self, kripke):
"""
calc evaluates the expression in the context of a kripke model.
It returns
kripke : Kripke model
returns a set of worlds in which the expression holds
"""
raise NotImplementedError()
def stack_calc(self, kripke, spacing=""):
"""
A variant of calc which gives a stack trace like structure back
return (set of worlds, stack trace (string))
"""
raise NotImplementedError()
def game_calc(self, kripke, world, spacing=""):
"""
A variant of calc which generates game trees
returns (true/false, stack_trace)
"""
raise NotImplementedError()
def expressions(self):
"returns a set of sub expressions"
raise NotImplementedError()
def depth(self):
"returns the amount of levels of expressions"
raise NotImplementedError()
def children(self):
"returns generator that loops through direct sub expressions"
raise NotImplementedError()
def same_type(self, other):
"basically an isinstance of"
return self.class_name == other.class_name
class And(LogicExpression):
class_name = 'and'
symbols = ('and', '&&', '&', '^', '/\\', '∧', '*')
out_symbol = '∧'
def __init__(self, l, r):
self._left, self._right = l, r
def __str__(self):
l = str(self._left)
r = str(self._right)
return "(%s %s %s)" % (l, And.out_symbol, r)
def __repr__(self):
return "And(%s,%s)" % (self._left.__repr__(), self._right.__repr__())
def calc(self, kripke):
return set.intersection(self._left.calc(kripke), self._right.calc(kripke))
def stack_calc(self, kripke, spacing=""):
lhs, lstack = self._left.stack_calc(kripke, spacing+" ")
rhs, rstack = self._right.stack_calc(kripke, spacing+" ")
res = set.intersection(lhs, rhs)
lines = [
"%s%s returns {%s}" % (spacing, And.out_symbol, ", ".join(res)),
'%s- left expression %s returns {%s}' % (spacing, self._left, ", ".join(lhs)),
lstack,
'%s- right expression %s returns {%s}' % (spacing, self._right, ", ".join(rhs)),
rstack
]
return res, "\n".join(lines)
def expressions(self):
e = self._left.expressions()
e.add(self)
e.update(self._right.expressions())
return e
def depth(self):
return max(self._left.depth(), self._right.depth()) + 1
def variables(self):
return self._left.variables().union(self._right.variables())
def children(self):
yield self._left
yield self._right
class Or(LogicExpression):
class_name = 'or'
symbols = ('or', '||', '|', 'v', '\\/', '∨', '+')
out_symbol = '∨'
def __init__(self, l, r):
self._left, self._right = l, r
def __str__(self):
l = str(self._left)
r = str(self._right)
return "(%s %s %s)" % (l, Or.out_symbol, r)
def __repr__(self):
return "Or(%s, %s)" % (self._left.__repr__(), self._right.__repr__())
def calc(self, kripke):
return set.union(self._left.calc(kripke), self._right.calc(kripke))
def stack_calc(self, kripke, spacing=""):
lhs, lstack = self._left.stack_calc(kripke, spacing+" ")
rhs, rstack = self._right.stack_calc(kripke, spacing+" ")
res = set.union(lhs, rhs)
lines = [
"%s%s returns {%s}" % (spacing, Or.out_symbol, ", ".join(res)),
'%s- left expression %s gives back {%s}' % (spacing, self._left, ", ".join(lhs)),
lstack,
'%s- right expression %s gives back {%s}' % (spacing, self._right, ", ".join(rhs)),
rstack
]
return res, "\n".join(lines)
def expressions(self):
e = self._left.expressions()
e.add(self)
e.update(self._right.expressions())
return e
def depth(self):
return max(self._left.depth(), self._right.depth()) + 1
def variables(self):
return self._left.variables().union(self._right.variables())
def children(self):
yield self._left
yield self._right
class Implies(LogicExpression):
class_name = 'implies'
symbols = ('implies', '->', '=>', '→')
out_symbol = '→'
def __init__(self, l, r):
self._left, self._right = l, r
def __str__(self):
l = str(self._left)
r = str(self._right)
return "(%s %s %s)" % (l, Implies.out_symbol, r)
def __repr__(self):
return "Implies(%s, %s)" % (self._left.__repr__(), self._right.__repr__())
def calc(self, kripke):
lhs = self._left.calc(kripke)
res = kripke.W.difference(lhs)
if not lhs:
return res
res.update(lhs.intersection(self._right.calc(kripke)))
return res
def stack_calc(self, kripke, spacing=""):
lhs, lstack = self._left.stack_calc(kripke, spacing+" ")
premise_doesnt_hold = kripke.W.difference(lhs)
rhs, rstack = self._right.stack_calc(kripke, spacing+" ")
res = set.union(premise_doesnt_hold, lhs.intersection(rhs))
lines = [
"%s%s, returns {%s}" % (spacing, Implies.out_symbol, ", ".join(res)),
"%s- Premise doesn't hold for {%s}" % (spacing, ", ".join(premise_doesnt_hold)),
'%s- left expression %s gives back {%s}' % (spacing, self._left, ", ".join(lhs)),
lstack,
'%s- right expression %s gives back {%s}' % (spacing, self._right, ", ".join(rhs)),
rstack
]
return res, "\n".join(lines)
def expressions(self):
e = self._left.expressions()
e.add(self)
e.update(self._right.expressions())
return e
def depth(self):
return max(self._left.depth(), self._right.depth()) + 1
def variables(self):
return self._left.variables().union(self._right.variables())
def children(self):
yield self._left
yield self._right
class Not(LogicExpression):
class_name = 'not'
symbols = ('not', '~', '¬', '!')
out_symbol = '¬'
def __init__(self, e):
self._expr = e
def __repr__(self):
return "Not(%s)" % self._expr.__repr__()
def __str__(self):
return "~%s" % str(self._expr)
def calc(self, kripke):
return kripke.W.difference(self._expr.calc(kripke))
def stack_calc(self, kripke, spacing=""):
expr_res, expr_stack = self._expr.stack_calc(kripke, spacing+" ")
res = kripke.W.difference(expr_res)
return res, "%s returned {%s}\n%s" % (Not.out_symbol, ", ".join(res), expr_stack)
def expressions(self):
expr = self._expr.expressions()
expr.add(self)
return expr
def depth(self):
return self._expr.depth() + 1
def variables(self):
return self._expr.variables()
def children(self):
yield self._expr
class Box(LogicExpression):
class_name = 'box'
symbols = ('☐', 'box')
out_symbol = '☐'
def __init__(self, e):
self._expr = e
def __repr__(self):
return "☐%s" % self._expr.__repr__()
def __str__(self):
return "Box(%s)" % str(self._expr)
def calc(self, kripke):
internal = self._expr.calc(kripke)
actual = set(w for w in kripke.W if all(v in internal for v in kripke.R[w]))
actual.update(kripke.blind_worlds())
return actual
def stack_calc(self, kripke, spacing=""):
inter, inter_stack = self._expr.stack_calc(kripke, spacing+" ")
actual = set(w for w in kripke.W if all(v in inter for v in kripke.R[w]))
out = actual.union(kripke.blind_worlds())
stack = [
"%s%s returned {%s}" % (spacing, Box.out_symbol, ", ".join(out)),
"%s- blind worlds, for which any box holds {%s}" % (spacing, ", ".join(actual)),
"%s- worlds with successors in which condition holds {%s}" % (spacing, ", ".join(actual)),
inter_stack
]
return out, "\n".join(stack)
def expressions(self):
expr = self._expr.expressions()
expr.add(self)
return expr
def depth(self):
return self._expr.depth() + 1
def variables(self):
return self._expr.variables()
def children(self):
yield self._expr
class Diamond(LogicExpression):
class_name = 'box'
symbols = ('◇', 'diamond')
out_symbol = '◇'
def __init__(self, e):
self._expr = e
def __repr__(self):
return "Diamond(%s)" % self._expr.__repr__()
def __str__(self):
return "◇%s" % str(self._expr)
def calc(self, kripke):
internal = self._expr.calc(kripke)
return set(w for w in kripke.W if any(v in internal for v in kripke.R[w]))
def stack_calc(self, kripke, spacing=""):
inter, inter_stack = self._expr.stack_calc(kripke, spacing+" ")
out = set(w for w in kripke.W if any(v in inter for v in kripke.R[w]))
stack = "%s%s returned {%s}\n%s" % (spacing, self.out_symbol, ", ".join(out), inter_stack)
return out, stack
def expressions(self):
expr = self._expr.expressions()
expr.add(self)
return expr
def depth(self):
return self._expr.depth() + 1
def variables(self):
return self._expr.variables()
def children(self):
yield self._expr
class Var(LogicExpression):
class_name = 'var'
def __init__(self, n):
self.name = n
def __str__(self):
return "%s" % self.name
def __repr__(self):
return "Var(%s)" % self.name
def calc(self, kripke):
return kripke.V[self.name]
def stack_calc(self, kripke, spacing=""):
holds_in = kripke.V[self.name]
return holds_in, "%s%s holds in {%s}" % (spacing, self.name, ", ".join(holds_in))
def expressions(self):
return {self}
def depth(self):
return 0
def variables(self):
return {self.name}
def children(self):
return
yield
class Constant(LogicExpression):
true_symbols = ('True', 'true', '1', '⊤')
false_symbols = ('False', 'false', '0', '⊥')
symbols = ('true', '1', 'false', '0')
out_symbols = ('⊥', '⊤')
class_name = 'const'
def __init__(self, value):
self.value = value
def __repr__(self):
return "{True}" if self.value else "{False}"
def __str__(self):
return Constant.out_symbols[self.value]
def calc(self, kripke):
return kripke.W if self.value else set()
def stack_calc(self, kripke, spacing=""):
out = kripke.W if self.value else set()
stack = "%s%s holds for {%s}" % (spacing, self.out_symbols[self.value], ", ".join(out))
return out, stack
def expressions(self):
return {self}
def depth(self):
return 0
def variables(self):
return set()
def children(self):
return
yield
# The infix classes, prefix classes etc.
infix_classes = [And, Or, Implies]
prefix_classes = [Not, Box, Diamond]
# These are some meta data structures, to quickly lookup:
# - what class belongs to what symbol
# - what operators we have
# - what instances already exist
operator_class = {} # maps a symbol to its class '&' -> class And
infix_operators = [] # a list of all symbols used for infix operators e.g. '&'
prefix_operators = [] # a list of all symbols used for prefix operators e.g. '~'
instances = {} # maps a tuple (class, args) to instance, since no duplicates are allowed
# initialise the meta structures
for c in infix_classes:
operator_class.update((s, c) for s in c.symbols)
infix_operators.extend(c.symbols)
for c in prefix_classes:
operator_class.update((s, c) for s in c.symbols)
prefix_operators.extend(c.symbols)
def create_expression(operator, *kwargs):
"Creates an expression from tokens, or if operator is 'var', 'const', 'varconst'"
if operator in ('var', 'const', 'varconst'):
return create_var_const(*kwargs)
constructor = operator_class[operator]
expression = instances.get((constructor, kwargs))
if expression is None:
expression = instances[(constructor, kwargs)] = constructor(*kwargs)
return expression
def create_var_const(name):
"Creates vars and constants (attempt to avoid complicated BNF)"
if name.lower() in Constant.symbols:
constructor = Constant
args = (name.lower() in Constant.true_symbols)
else:
constructor = Var
args = name
expr = instances.get((constructor, args))
if expr == None:
expr = instances[(constructor, args)] = constructor(args)
return expr
if __name__ == '__main__':
# Testing
# Can vars and constants be properly made
for i in ('Abc', 'Note', 'a', 't', 'True', 'False'):
var_const = create_var_const(i)
expr = create_expression('varconst', i)
print(repr(expr))
print(var_const == expr)
print('-' * 20)
a = create_var_const('a')
b = create_var_const('b')
t = create_var_const('t')
at = create_expression('&', a, t)
print(a)
for i in a.children():
print(i)
create_expression('->', a, b)
create_expression('->', a, b)
print(instances)
for i in infix_operators:
print('input (a & t) %s b' % i)
expr = create_expression(i, at, b)
expr2 = create_expression(i, at, b)
print(expr)
print(expr == expr2)