-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.py
executable file
·468 lines (358 loc) · 12.4 KB
/
logic.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
#!/usr/bin/env python
import prettytable
import re
import sys
from functools import reduce
# =============================================================================
# Parser
# =============================================================================
lexer_re = re.compile(r'[a-zA-Z]\w*|[~()]|[^~()\w\s]+')
var_re = re.compile(r'^[a-zA-Z]\w*$')
def tokenize(expr):
return lexer_re.findall(expr)
def isvar(token):
if token is None:
return False
if var_re.match(token):
return True
return False
def expected(expected, saw=None):
saw = '`%s`' % saw if saw else 'EOE'
raise SyntaxError('expected %s, saw %s' % (expected, saw))
def parse(expr):
if isinstance(expr, Expression):
return expr
if isinstance(expr, str):
expr = tokenize(expr)
return Parser(expr).parse()
class Parser(object):
def __init__(self, tokens):
self.tokens = tokens
self.terms = []
def read(self):
# consume first token
if self.tokens:
return self.tokens.pop(0)
return None
def parse(self, max_precedence=None):
op = None
while True:
toks = list(self.tokens)
term = self.next_term()
token = self.read()
if token is None:
if op is None:
return term
self.terms.append(term)
return op(*self.terms)
next_op = get_operation(token)
# token is not None, but no operation found either
if next_op is None:
expected('an operation or EOE', token)
# consumed beyond the maximum precedence (if specified
if max_precedence and next_op.precedence >= max_precedence:
self.tokens.insert(0, token) # put the token back
self.terms.append(term)
return op(*self.terms)
# already looped through and undergoing an operation
if op and op.precedence > next_op.precedence:
p = Parser(toks)
self.terms.append(p.parse(op.precedence))
self.tokens = p.tokens
if not self.tokens:
return op(*self.terms)
next_op = get_operation(self.read())
if op is not next_op:
self.terms = [op(*self.terms)]
else:
self.terms.append(term)
if op and op is not next_op:
self.terms = [op(*self.terms)]
op = next_op
def next_term(self):
token = self.read()
# unconditionals
if token == 'T':
return T
if token == 'F':
return F
# variable
if isvar(token):
return Var(token)
# Not character
if token in ('~', '!') or token == u'\u00ac':
return Not(self.next_term())
# open bracket
if token == '(':
toks, depth = [], 1
while self.tokens:
tok = self.read()
if tok == '(':
depth += 1
elif tok == ')':
if depth == 1:
break
depth -= 1
toks.append(tok)
else:
# depth did not return to 1, therefore not enough ')'
expected('an operation or `)`')
return parse(toks)
# no other valid characters left! (note, ')' will be consumed as above)
expected('a variable, unconditional, `~`, or `(`', token)
# =============================================================================
# Expression Classes
# =============================================================================
class Expression(object):
def __eq__(self, expr):
if not isinstance(expr, Expression):
return False
return self.equivalent(expr)
def __len__(self):
raise NotImplementedError
def __str__(self):
raise NotImplementedError
def get_names(self):
raise NotImplementedError
def equivalent(self, expr):
"""Returns bool as to whether the expression is equivalent to expr
If the logical biconditional of self and expr
is a tautology, then they are equivalent
E.g. p ^ (p v q) <-> p
"""
expr = parse(expr)
return Biconditional(self, expr).is_tautology()
def evaluate(self, variables):
"""Evaluates the expression
Note: variables is a dictonary in the
form of {'variable_name': True/False}
"""
raise NotImplementedError
def identical(self, expr):
"""Returns bool as to whether the expression is identical to expr
Note: checks structure, may not be the same instance!
"""
raise NotImplementedError
def is_contradiction(self):
return not any(TruthTable(self).values)
def is_tautology(self):
return all(TruthTable(self).values)
class Unconditional(Expression):
def __init__(self, symbol, value):
self.symbol = symbol
self.value = value
def __len__(self):
return 1
def __str__(self):
return self.symbol
def get_names(self):
return []
def evaluate(self, _=None):
return self.value
def identical(self, expr):
expr = parse(expr)
if not isinstance(expr, Unconditional):
return False
return expr.value == self.value
T = Unconditional('T', True)
F = Unconditional('F', False)
class Var(Expression):
def __init__(self, name):
self.name = name
def __len__(self):
return 1
def __str__(self):
return self.name
def get_names(self):
return [self.name]
def evaluate(self, variables):
return variables[self.name]
def identical(self, expr):
expr = parse(expr)
if not isinstance(expr, Var):
return False
return self.name == expr.name
def wrap(term, op):
if (# never put brackets around T/F or p
isinstance(term, (Unconditional, Var)) or
# wrap brackets around inner nots
(type(term) is Not and op is not Not) or
# operations with higher precedence
(issubclass(op, BinaryOperation) and op.precedence > type(term).precedence)):
return str(term)
return '(%s)' % term
class Operation(Expression):
pass
class Not(Operation):
def __init__(self, term):
term = parse(term)
self.term = term
def __len__(self):
return 1
def __str__(self):
return str(b'\xc2\xac', 'utf-8') + wrap(self.term, Not)
def get_names(self):
return self.term.get_names()
def evaluate(self, variables):
term = self.term.evaluate(variables)
return not term
def identical(self, expr):
expr = parse(expr)
if not isinstance(expr, Not):
return False
return self.term.identical(expr.term)
class BinaryOperation(Operation):
def __getitem__(self, index):
return self.terms[index]
def __iter__(self):
for term in self.terms:
yield term
def __len__(self):
return len(self.terms)
def append(self, term):
self.terms.append(term)
def get_names(self):
names = []
for term in self:
for name in term.get_names():
if name not in names:
names.append(name)
return sorted(names)
operations = {}
def get_operation(symbol):
symbol = symbol.upper()
if symbol not in operations:
return None
return operations[symbol]
def set_operation(symbol, operation):
symbol = symbol.upper()
operations[symbol] = operation
def operation(name, rule, unicode_symbol, *symbols, **kwargs):
two_args = kwargs.get('two_args', False)
precedence = kwargs.get('precedence', 1)
class BinaryOp(BinaryOperation):
def __init__(self, *terms):
self.terms = list(terms)
if len(terms) < 2:
raise TypeError(('binary operators take at least 2 ' +
'arguments (%d given)') % len(terms))
if two_args and len(terms) > 2:
raise TypeError(('the %s operator only takes 2 ' +
'arguments (%d given)') % (name, len(terms)))
def __str__(self):
wrap_ = lambda t: wrap(t, BinaryOp)
terms = map(wrap_, self.terms)
separator = ' %s ' % unicode_symbol
return separator.join(terms)
def evaluate(self, variables):
# evaluate all terms
values = map(lambda t: t.evaluate(variables), self.terms)
# apply rule to evaluated terms
return rule(*values)
def identical(self, expr):
expr = parse(expr)
if not isinstance(expr, BinaryOp) or \
len(self) != len(expr):
return False
for i, term in enumerate(self):
if not term.identical(expr[i]):
return False
return True
BinaryOp.__name__ = name
BinaryOp.two_args = two_args
BinaryOp.precedence = precedence
set_operation(unicode_symbol, BinaryOp)
for symbol in symbols:
set_operation(symbol, BinaryOp)
return BinaryOp
def and_(*values):
return reduce(lambda p, q: p and q, values)
def or_(*values):
return reduce(lambda p, q: p or q, values)
def xor(p, q):
return p != q
def nand(*values):
return not reduce(and_, values)
def nor(*values):
return not reduce(or_, values)
def conditional(p, q):
return not p or q
def biconditional(*values):
return reduce(lambda p, q: p == q, values)
And = operation('And', and_, u'\u2227', 'AND', '^', '&', '&&',)
Or = operation('Or', or_, u'\u2228', 'OR', 'v', '|', '||')
Xor = operation('Xor', xor, u'\u2295', 'XOR', two_args=True)
Nand = operation('Nand', nand, u'\u2191', 'NAND')
Nor = operation('Nor', nor, u'\u2193', 'NOR')
Conditional = operation('Conditional', conditional, u'\u2192',
'->', '-->', '=>', '==>', precedence=2,
two_args=True)
Biconditional = operation('Biconditional', biconditional, u'\u2194',
'<->', '<-->', '<=>', '<==>', '=', 'eq', 'XNOR',
precedence=3)
# =============================================================================
# Truth Tables
# =============================================================================
def bool_permutations(n):
"""Generate a list of each permutation of the list of n boolean values"""
if n == 1:
return [[True], [False]]
if n <= 0:
return [[]]
perms = []
sub_perms = bool_permutations(n-1)
for value in (True, False):
for perm in sub_perms:
perms.append([value] + perm)
return perms
class TruthTable(prettytable.Table):
def __init__(self, expr):
expr = parse(expr)
names = expr.get_names()
header = names + [str(expr)]
super(TruthTable, self).__init__(header)
self.expression = expr
self.values = []
for perm in bool_permutations(len(names)):
variables = dict(zip(names, perm))
value = expr.evaluate(variables)
self.append(perm + [value])
self.values.append(value)
class TooManyVariablesError(Exception):
pass
def truth_table(expr):
expr = parse(expr)
MAX_VARIABLES = 4
num_variables = len(expr.get_names())
if num_variables > MAX_VARIABLES:
raise TooManyVariablesError(
'%s variables in expression, maximum of %d allowed'
% (num_variables, MAX_VARIABLES))
return TruthTable(expr)
# =============================================================================
# Sample REPL
# =============================================================================
def repl(expr=None):
if expr is None:
expr = input('Enter an expression: ')
print()
try:
expr = parse(expr)
except Exception as e:
print('Error:', e)
else:
try:
tt = truth_table(expr)
except TooManyVariablesError as e:
print('Cannot generate truth table:', e)
print()
else:
print('Truth table:')
print(tt)
print('-' * 80)
if __name__ == '__main__':
if len(sys.argv) > 1:
for expr in sys.argv[1:]:
repl(expr)
while 1:
repl()