A logic toolkit
The problem
Build a small program that reads a logical formula and tells you everything about it: its truth table, whether it is a tautology, a contradiction or a contingency, and whether it is equivalent to a second formula.
Requirements
- Read a formula written in Python syntax with up to four letters
- Print the complete truth table, one row per line
- Classify the formula as a tautology, a contradiction or a contingency
- Compare two formulas and print a row that separates them when they are not equivalent
Technologies
- Python 3
- itertools.product
- eval with a dictionary of letter values
- all() and any()
Step by step
- Write a function that lists every row of values for the letters in a formula.
- Evaluate the formula on each row and print the table.
- Use all() and any() on the column of results to classify the formula.
- Add the equivalence check, then test it on De Morgan's laws and on the converse of an implication.
Expected result
A command-line tool that prints truth tables, classifies formulas and proves or refutes equivalences, tested on the standard laws of logic.
How it is evaluated
- The table has 2 to the power n rows in a fixed order
- De Morgan's laws and the contrapositive are reported as equivalent
- The converse of an implication is reported as different with a separating row
- A tautology, a contradiction and a contingency are each classified correctly
Ways to improve it
- Accept the symbols ¬, ∧, ∨ and → instead of Python words
- Simplify a formula with the laws of logic and show each step
- Find a satisfying assignment for a formula in clause form