Codelet 2: Evaluating Propositional Logic

The due date for this codelet is February 6, 11:59PM.

Introduction

The goals of this codelet are to solidify your knowledge of propositional logic and refresh your knowledge of recursive functions.

Outline

Your assignment

Your task is to:

  1. Download codelet2.zip from the course website and open it. You will find these instructions and codelet2.py which has scaffolding for you.
  2. Complete the core task which is implementing two functions, one which evaluates a formula of propositional logic given an assignment and the other determines whether a formula is a tautology.

Grading

When assessing your code, satisfactory achievement is demonstrated, in part, by:

Core Tasks

Your core task is to determine whether a formula of propositional logic is a tautology (or not). To scaffold this, you should first complete evaluate_formula which takes a formula and a dictionary of truth-value assignments for each atomic element of the formula and returns whether that formula is true (or false) given that assignment.

Format of Formula

The formula from propositional logic will conform to a pre-order format, represented as nested tuples. You may assume all your input is well-formed. Here are some examples to help you see how formula are formatted:

That is, formula are either a single string representing an atom or a tuple where we have an operator as the first element of the tuple, the left-hand side as the second element, and the right-hand side (if applicable) as the third element of the tuple. Elements may themselves be compound formula, written in the same format.

You should expect the following operators:

Contents of Assignments

Assignments are dictionaries mapping from atomic elements to their truth value. For example,

Hints for evaluate_formula

The most natural solution to evalute_formula uses a recursive function. Let’s think through some concrete examples.

Hints for is_tautology

You may use the method product from the library itertools. You may even find it helpful! See the code snippet below. Notice that given a set, \(S\), we can get all the elements in the set \(T := S \times S \times S\) using product(S, repeat=3).

>>> from itertools import product
>>> for s in product({'cat', 'dog', 'lizard'}, repeat=2):
...     print(s)
...
('dog', 'dog')
('dog', 'cat')
('dog', 'lizard')
('cat', 'dog')
('cat', 'cat')
('cat', 'lizard')
('lizard', 'dog')
('lizard', 'cat')
('lizard', 'lizard')

Other Notes

Submitting

Submit your file codelet2.py (with that EXACT name) to gradescope. There is an autograder which will tests your code. You may re-submit as many times as you would like prior to the deadline.