Truth Tables of CIS-375 Homework 1

In [1]:
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)

Question 1

In [15]:
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])))
Out[15]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}z\end{align*} \begin{align*}\neg y\end{align*} \begin{align*}(\neg y \land z)\end{align*} \begin{align*}\neg z\end{align*} \begin{align*}((\neg z) \rightarrow x)\end{align*} \begin{align*}\neg((\neg z) \rightarrow x)\end{align*} \begin{align*}p\end{align*}
T T T F F F T F T
T T F F F T T F T
T F T T T F T F F
T F F T F T T F T
F T T F F F T F T
F T F F F T F T F
F F T T T F T F F
F F F T F T F T F

Question 2

In [3]:
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])))
Out[3]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}z\end{align*} \begin{align*}(x \lor y)\end{align*} \begin{align*}(x \lor y) \rightarrow z\end{align*} \begin{align*}(x \rightarrow z)\end{align*} \begin{align*}(y \rightarrow z)\end{align*} \begin{align*}(x \rightarrow z) \land (y \rightarrow z)\end{align*}
T T T T T T T T
T T F T F F F F
T F T T T T T T
T F F T F F T F
F T T T T T T T
F T F T F T F F
F F T F T T T T
F F F F T T T T

Question 3 (i)

In [14]:
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])))
Out[14]:
\begin{align*}x\end{align*} \begin{align*}(x \rightarrow \mathrm{False})\end{align*} \begin{align*}\neg x\end{align*} \begin{align*}(x \rightarrow \mathrm{False}) \rightarrow \neg x\end{align*}
T F F T
F T T T

Question 3 (ii)

In [5]:
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])))
Out[5]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}(x \rightarrow y)\end{align*} \begin{align*}\neg y\end{align*} \begin{align*}(x \rightarrow \neg y)\end{align*} \begin{align*}\left( (x \rightarrow y) \land (x \rightarrow \neg y) \right)\end{align*} \begin{align*}\neg x\end{align*} Expression
T T T F F F F T
T F F T T F F T
F T T F T T T T
F F T T T T T T

Question 4 (i)

In [6]:
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])))
Out[6]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}(x \rightarrow y)\end{align*} \begin{align*}\neg y\end{align*} \begin{align*}x \land ((x \rightarrow y))\end{align*} Expression
T T T F T F
T F F T F F
F T T F F F
F F T T F F

Question 4 (ii)

In [13]:
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])))
Out[13]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}(x \rightarrow y)\end{align*} \begin{align*}\neg x\end{align*} \begin{align*}(\neg x \rightarrow y)\end{align*} \begin{align*}\neg y\end{align*} \begin{align*}(x \rightarrow y) \land ((\neg x) \rightarrow y)\end{align*} Expression
T T T F T F T F
T F F F T T F F
F T T T T F T F
F F T T F T F F

Question 5 (a)

In [8]:
# 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])))
Out[8]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}(x \land y)\end{align*} \begin{align*}x \uparrow y\end{align*}
T T T F
T F F T
F T F T
F F F T

Question 5 (b)

In [9]:
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])))
Out[9]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}x \uparrow y\end{align*} \begin{align*}y \uparrow x\end{align*}
T T F F
T F T T
F T T T
F F T T
In [10]:
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])))
Out[10]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}z\end{align*} \begin{align*}x \uparrow y\end{align*} \begin{align*}(x \uparrow y) \uparrow z\end{align*} \begin{align*}y \uparrow z\end{align*} \begin{align*}x \uparrow (y \uparrow z)\end{align*}
T T T F T F T
T T F F T T F
T F T T F T F
T F F T T T F
F T T T F F T
F T F T T T T
F F T T F T T
F F F T T T T

Question 5 (c)

In [16]:
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])))
Out[16]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}x \uparrow y\end{align*} \begin{align*}(x \land y)\end{align*} \begin{align*}\neg(x \uparrow y)\end{align*}
T T F T T
T F T F F
F T T F F
F F T F F
In [18]:
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])))
Out[18]:
\begin{align*}x\end{align*} \begin{align*}\neg x\end{align*} \begin{align*}x \uparrow x\end{align*}
T F F
F T T

Question 5 (d)

In [11]:
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])))
Out[11]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}\neg x\end{align*} \begin{align*}\neg y\end{align*} \begin{align*}(\neg x) \land (\neg y) = u\end{align*} \begin{align*}\neg u\end{align*} \begin{align*}(x \lor y)\end{align*}
T T F F F T T
T F F T F T T
F T T F F T T
F F T T T F F
In [12]:
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])))
Out[12]:
\begin{align*}x\end{align*} \begin{align*}y\end{align*} \begin{align*}\neg x\end{align*} \begin{align*}(\neg x) \lor y\end{align*} \begin{align*}x \rightarrow y\end{align*}
T T F T T
T F F F F
F T T T T
F F T T T
In [ ]: