from IPython.display import *
import itertools
from tabulate import tabulate
# a boolean class for beautiful printing
class Boolean:
def __init__(self, val):
self.val = val
def __and__(self, other):
return Boolean(self.val and other.val)
def __or__(self, other):
return Boolean(self.val or other.val)
def __invert__(self):
return Boolean(not self.val)
def __repr__(self):
if self.val:
return 'T'
else:
return 'F'
# boolean equivalent of if-then
def if_then(a, b):
return (~a) | b
# boolean equivalent of if-only-if
def iff(a, b):
return if_then(a, b) & if_then(b, a)
def mex(s):
return r'\begin{align*}%s\end{align*}'%s
true = Boolean(True)
false = Boolean(False)
coll = (true, false)
table = []
table.append(list(map(mex, ['x', 'y', 'z', r'\neg y', r'(\neg y \land z)', r'\neg z',
r'((\neg z) \rightarrow x)', r'\neg((\neg z) \rightarrow x)', 'p'])))
for x, y, z in itertools.product((true, false), repeat=3):
row = [x, y, z, ~y, (~y)&z, ~z, if_then(~z, x), ~if_then(~z, x), iff((~y)&z, ~if_then(~z, x))]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', 'z', r'(x \lor y)', r'(x \lor y) \rightarrow z',
r'(x \rightarrow z)', r'(y \rightarrow z)',
r'(x \rightarrow z) \land (y \rightarrow z)'])))
for x, y, z in itertools.product((true, false), repeat=3):
row = [x, y, z, x | y, if_then(x | y, z), if_then(x, z), if_then(y, z), if_then(x, z) & if_then(y, z)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', r'(x \rightarrow \mathrm{False})', r'\neg x',
r'(x \rightarrow \mathrm{False}) \rightarrow \neg x'])))
for x, in itertools.product((true, false), repeat=1):
row = [x, if_then(x, false), ~x, if_then(if_then(x, false), ~x)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'(x \rightarrow y)', r'\neg y', r'(x \rightarrow \neg y)',
r'\left( (x \rightarrow y) \land (x \rightarrow \neg y) \right)',
r'\neg x'])))
table[0].append('Expression')
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, if_then(x, y), ~y, if_then(x, ~y), if_then(x, y) & if_then(x, ~y), ~x,
if_then(if_then(x, y) & if_then(x, ~y), ~x)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'(x \rightarrow y)', r'\neg y',
r'x \land ((x \rightarrow y))'])))
table[0].append('Expression')
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, if_then(x, y), ~y, x & if_then(x, y),
x & if_then(x, y) & (~y)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'(x \rightarrow y)', r'\neg x',
r'(\neg x \rightarrow y)', r'\neg y',
r'(x \rightarrow y) \land ((\neg x) \rightarrow y)'])))
table[0].append('Expression')
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, if_then(x, y), ~x, if_then(~x, y), ~y,
if_then(x, y) & if_then(~x, y), if_then(x, y) & if_then(~x, y) & (~y)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
# definition of the new operand
def nand(x, y):
return ~(x & y)
table = []
table.append(list(map(mex, ['x', 'y', r'(x \land y)', r'x \uparrow y'])))
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, x & y, nand(x, y)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'x \uparrow y', r'y \uparrow x'])))
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, nand(x, y), nand(y, x)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', 'z', r'x \uparrow y', r'(x \uparrow y) \uparrow z',
r'y \uparrow z', r'x \uparrow (y \uparrow z)'])))
for x, y, z in itertools.product((true, false), repeat=3):
row = [x, y, z, nand(x, y), nand(nand(x, y), z), nand(y, z), nand(x, nand(y, z))]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'x \uparrow y', r'(x \land y)',
r'\neg(x \uparrow y)'])))
for x,y in itertools.product((true, false), repeat=2):
row = [x, y, nand(x, y), x & y, ~nand(x, y)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', r'\neg x', r'x \uparrow x'])))
for x, in itertools.product((true, false), repeat=1):
row = [x, ~x, nand(x, x)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'\neg x', r'\neg y', r'(\neg x) \land (\neg y) = u',
r'\neg u', r'(x \lor y)'])))
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, ~x, ~y, (~x)&(~y), ~((~x)&(~y)), x | y]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))
table = []
table.append(list(map(mex, ['x', 'y', r'\neg x', r'(\neg x) \lor y', r'x \rightarrow y'])))
for x, y in itertools.product((true, false), repeat=2):
row = [x, y, ~x, (~x) | y, if_then(x, y)]
row = list(map(repr, row))
table.append(row)
Markdown(tabulate(table, tablefmt='html', headers='firstrow', colalign=['center'] * len(table[0])))