#!/usr/bin/env python """ Test various syllogisms with truth tables. """ def implies(a,b): """ implies(a,b) returns (a => b) >>> implies(True, False) False """ return not a or b syllogisms = [ 'implies(a,b)', 'not a or b', 'a and b', 'not (not a or not b)', 'not not a', 'a', 'implies(a,b)', 'implies(not b, not a)', 'a and (b or c)', '(a and b) or (a and c)', 'a or (b and c)', '(a or b) and (a or c)', 'a and b and c', 'a or b or c', 'implies(a,b) and implies(b,c)', 'implies(a,c)', 'implies(implies(a,b) and implies(b,c), implies(a,c))', 'True', ] def print_truth_table(left, right): """Print the boolean value of 'left' and 'right' when (a,b,c) are set to all combinations of True and False. """ label = ('%-6s '*5) % ('a', 'b', 'c', left, right) print print ' ' + label print ' ' + '-'*len(label) looks_ok = True for a in (True, False): for b in (True, False): for c in (True, False): print (' ' + '%-6s '*3 + '%-*s '*2) \ % (a, b, c, len(left), eval(left), len(right), eval(right)) if eval(left) != eval(right): looks_ok = False if looks_ok: print " ... YES; left and right columns are the same." else: print " ... NO; left and right columns are not the same." def main(): while syllogisms: left = syllogisms.pop(0) right = syllogisms.pop(0) print_truth_table(left, right) main()