Exercises · SymPy · Topics: propositions, NOT / AND / OR, Implies, Equivalent, truth tables, tautology, contradiction, logic gates
Pr. Zouhair el Hadiq
Use sympy.symbols to declare boolean variables $p$ and $q$. Build the compound expression $p \land (q \lor \neg p)$ and evaluate it for all four combinations of truth values.
.subs({p: True, q: False}) to evaluate.p=..., q=... → ....Using SymPy's Not, And, Or, compute $\neg p$, $p \land q$, and $p \lor q$ for all four assignments of $(p, q)$.
Build the expression $p \to q$ using sympy.Implies(p, q) and print its complete truth table using sympy.logic.boolalg.truth_table.
Build $p \leftrightarrow q$ using sympy.Equivalent(p, q) and print its full truth table.
Evaluate two different parenthesisations of $\neg p \land q \lor r$ for the assignment $p = \text{True},\; q = \text{False},\; r = \text{True}$:
Show that A and B give different results and explain the standard precedence order: $\neg > \land > \lor > {\to} > {\leftrightarrow}$.
Given the implication $p \to q$, define and simplify (using simplify_logic) each of the following:
| Name | Formula |
|---|---|
| Original | $p \to q$ |
| Converse | $q \to p$ |
| Contrapositive | $\neg q \to \neg p$ |
| Inverse | $\neg p \to \neg q$ |
Write a helper is_tautology(expr) -> bool that returns True when an expression is a tautology (True for every assignment).
not satisfiable(Not(expr)).Apply it to each formula below and report the result:
Write is_contradiction(expr) -> bool that returns True when a formula is False for every assignment.
not satisfiable(expr).Test it on:
Use SymPy's Xor(p, q) and verify the equivalence:
$$p \oplus q \;\equiv\; (p \lor q) \land \neg(p \land q)$$
is_tautology(Equivalent(xor_direct, xor_expanded)) to confirm.Define Python functions NAND(a, b) and NOR(a, b) using SymPy's Not, And, Or.
Then show that NAND is functionally complete by expressing NOT, AND, and OR using only NAND:
Verify each equivalence using is_tautology(Equivalent(...)).
Consider the formula representing Hypothetical Syllogism:
$$\bigl((p \to q) \land (q \to r)\bigr) \to (p \to r)$$
is_tautology.