Question: write a function that computes the truth table of a statement in propositional logic along with several functions that reason about those tables. Lets start

write a function that computes the truth table of a statement in propositional logic along with several functions that reason about those tables.

Lets start with the task of computing the truth table of a proposition, and describe it informally first. We want to be able to do something like:

>>> tt = truth_table(expression) 

where expression is a string that contains a valid Python logical expression, and the return value is a list where each element is a row in the table, e.g.:

>>> truth_table('a and b') 
[[True, True, True], [True, False, False], [False, True, False], [False, False, False]] 

the row [True, False, False] in the truth table corresponds to a=True, b=False and a result of False. The expressions can be more complicated than a and b, e.g. (a and b) or (c and not d).

You already know that in Python you can easily evaluate logical expressions, i.e. propositions:

>>> a = True >>> b = False >>> a and (not a or b) 

But, at this point you may ask how you can evaluate arbitrary expressions without even knowing the names of the variables ahead of time. Turns out its quite simple in Python using the exec and evalbuiltins:

>>> exec('a=True') >>> exec('b=True') >>> expression = 'a and (not a or b)' >>> eval(expression) 

We are providing you with template code that contains some black magic that enriches Pythons Boolean operators (and, or, not) with two additional operators: |iff| and |implies| that correspond to the bi-conditional (<>) and implication (>) operators, respectively. Using the template code youll be able to evaluate expressions such as:

>>> eval('a |implies| b') >>> eval('a |iff| b') 

the code:

 # NOTE: # You must use small single letters for your variable names, for eg. a, b, c # You may use parenthesis to group your expressions such as 'a and (b or c)'  # Implement the following functions: # truth_table  ######## Do not modify the following block of code ######## # ********************** BEGIN *******************************  from functools import partial import re class Infix(object): def __init__(self, func): self.func = func def __or__(self, other): return self.func(other) def __ror__(self, other): return Infix(partial(self.func, other)) def __call__(self, v1, v2): return self.func(v1, v2) @Infix def implies(p, q) : return not p or q @Infix def iff(p, q) : return (p |implies| q) and (q |implies| p) # You must use this function to extract variables # This function takes an expression as input and returns a sorted list of variables # Do NOT modify this function def extract_variables(expression): sorted_variable_set = sorted(set(re.findall(r'\b[a-z]\b', expression))) return sorted_variable_set # *********************** END ***************************  ############## IMPLEMENT THE FOLLOWING FUNCTIONS ############## ############## Do not modify function definitions ##############  # This function calculates a truth table for a given expression # input: expression # output: truth table as a list of lists # You must use extract_variables function to generate the list of variables from expression # return a list of lists for this function def truth_table(expression): pass 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!