Skip to content

Instantly share code, notes, and snippets.

@marshareb
Last active February 8, 2017 23:37
Show Gist options
  • Save marshareb/b40dd9d855a91c47b68f61fbbc52060f to your computer and use it in GitHub Desktop.
Save marshareb/b40dd9d855a91c47b68f61fbbc52060f to your computer and use it in GitHub Desktop.
Generates truth tables for boolean expressions.
#Generates the truth table for n variables
def truthtable (n):
if n < 1:
return [[]]
subtable = truthtable(n-1)
return [ row + [v] for row in subtable for v in [0,1] ]
#Prints the first truth table in a nice manner.
def print_truth_table(lis, num_of_variables):
stri = "| "
for i in range(num_of_variables):
stri += str(i)
stri += " | "
print(stri)
stri = ""
for i in range(num_of_variables):
stri+="____"
print(stri)
for i in lis:
stri = "| "
for j in i:
stri += str(j)
stri += " | "
print(stri)
#From the truth table, generate a list of just the truth values for a variable.
def list_for_variable(truthtable, variable):
list1 =[]
for i in truthtable:
list1.append(i[variable])
return list1
#Checks if a statement is a tautology
def tautology(list):
taut = True
for i in list:
if i != 1:
taut = False
if taut == True:
print("There is a tautology")
else:
print("There is not a tautology")
#If then -- the only case where this is false is if the first variable is true and the second is false.
def if_then(lis1, lis2, var1 = "p", var2 = "q"):
list2 = []
for i in range(len(lis1)):
if lis1[i] == 1 and lis2[i] ==1:
list2.append(1)
elif lis1[i] == 1 and lis2[i] == 0:
list2.append(0)
elif lis1[i] == 0 and lis2[i] == 1:
list2.append(1)
else:
list2.append(1)
print('|' + str(var1) + '->' + str(var2) + '|')
for i in list2:
print("| " + str(i) + " |")
return list2
#If and only if -- both variables must have the same logical value
def if_and_only_if(lis, lis2, var1= "p", var2 = "q"):
list2 = []
for i in range(len(lis)):
if lis[i] == lis2[i]:
list2.append(1)
else:
list2.append(0)
print('|' + str(var1) + '<->' + str(var2) + '|')
for i in list2:
print("| " + str(i) + " |")
return list2
#Logical and -- if both variables are true, the statement is true
def And(lis1, lis2, var1="p", var2="q"):
list2 =[]
for i in range(len(lis1)):
if lis1[i] == 1 and lis2[i]==1:
list2.append(1)
else:
list2.append(0)
print('|' + str(var1) + '^' + str(var2) + '|')
for i in list2:
print("| " + str(i) + " |")
return list2
#Logical or -- if at least 1 is true, the statement is true
def Or(lis1, lis2, var1="p", var2="q"):
list2 = []
for i in range(len(lis1)):
if lis1[i] == 1 or lis2[i] == 1:
list2.append(1)
else:
list2.append(0)
print('|' + str(var1) + 'v' + str(var2) + '|')
for i in list2:
print("| " + str(i) + " |")
return list2
#Logical not -- if the variable is true, then the statement is false and vice versa
def Not(lis1, var1="p"):
list2 = []
for i in range(len(lis1)):
if lis1[i] == 1:
list2.append(0)
else:
list2.append(1)
print('|!' +str(var1) + ' |')
for i in list2:
print("| " + str(i) + " |")
return list2
#If all the values are false, we have a contradiction.
def Contradiction(list):
contra = True
for i in list:
if i == 1:
contra = False
if contra == True:
print("There is a contradiction")
else:
print("There is no contradiction")
#Check DeMorgan's Laws
tb = truthtable(2)
a = list_for_variable(tb, 0)
b = list_for_variable(tb, 1)
# !(p ^ q) = !p v !q
tautology(if_and_only_if(Not(And(a, b), "p^q"), Or(Not(a, "p"), Not(b, "q"), "p", "q")))
#!(pvq) = !p ^ !q
tautology(if_and_only_if(Not(Or(a,b), "p^q"), And(Not(a, "p"), Not(b, "q"), "p", "q")))
@marshareb
Copy link
Author

marshareb commented Feb 8, 2017

Example:

tb = truthtable(3)
p = list_for_variable(tb, 0)
q = list_for_variable(tb, 1)
r = list_for_variable(tb, 2)
print_truth_table(tb, 3)
#p -> r
list1 = if_then(p, r, "p", "r")
# q -> r
list2 = if_then(q, r, "q", "r")
#(p -> r) v (q -> r)
list3 = Or(list1, list2, "p->r", "q->r")

#(p ^ q)
list4 = And(p, q, "p", "q")
#(p ^ q) -> r
list5 = if_then(list4, r, "p^q", "r")

list6 = if_and_only_if(list3, list5, "first", "second")
tautology(list6)

# p ^ q
list7 = And(p, q, "p", "q")
# (p ^ q) -> p
list8 = if_then(list7, p, "p^q", "p")

tautology(list8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment