CSE 311 Lecture 03: Equivalence and Proofs

Topics

Equivalence and circuits
A brief review of Lecture 02.
Checking equivalence
Applications and a basic brute-force algorithm.
Logical proofs
A method for establishing equivalence that extends to richer logics.

Equivalence and circuits

A brief review of Lecture 02.

Logical equivalence

$A \equiv B$ is an assertion that two propositions $A$ and $B$ have the same truth values in all possible cases.

$A \equiv B$ and $\underbrace{(A \leftrightarrow B) \equiv \T}_\mathsf{tautology}$ have the same meaning.

$p$ $q$ $p \wedge q$ $q \wedge p$ $p \wedge q \leftrightarrow q \wedge p$
$\F$ $\F$ $\T$
$\F$ $\F$ $\T$
$\F$ $\F$ $\T$
$\T$ $\T$ $\T$

When $p=\T$ and $q=\F$, $p \wedge q$ is false but $p \vee q$ is true!

Important equivalences

DeMorgan’s laws
$\neg(p \wedge q) \equiv \neg p \vee \neg q$
$\neg(p \vee q) \equiv \neg p \wedge \neg q$
Law of implication
$p \rightarrow q \equiv \neg p \vee q$
Contrapositive
$p \rightarrow q \equiv \neg q \rightarrow \neg p $
Biconditional
$p \leftrightarrow q \equiv (p \rightarrow q) \wedge (q \rightarrow p)$
Double negation
$p \equiv \neg \neg p$
Identity
$p \wedge \mathsf{T} \equiv p$
$p \vee \mathsf{F} \equiv p$
Domination
$p \wedge \mathsf{F} \equiv \mathsf{F}$
$p \vee \mathsf{T} \equiv \mathsf{T}$
Idempotence
$p \wedge p \equiv p$
$p \vee p \equiv p$
Commutativity
$p \wedge q \equiv q \wedge p$
$p \vee q \equiv q \vee p$
Associativity
$(p \wedge q) \wedge r \equiv p \wedge (q \wedge r)$
$(p \vee q) \vee r \equiv p \vee (q \vee r)$
Distributivity
$p \wedge (q \vee r) \equiv (p \wedge q) \vee (p \wedge r)$
$p \vee (q \wedge r) \equiv (p \vee q) \wedge (p \vee r)$
Absorption
$p \wedge (p \vee q) \equiv p$
$p \vee (p \wedge q) \equiv p$
Negation
$p \wedge \neg p \equiv \mathsf{F}$
$p \vee \neg p \equiv \mathsf{T}$

We will always give you this list!

Digital circuits

Digital circuits implement propositional logic:

  • $\T$ corresponds to 1 or high voltage.
  • $\F$ corresponds to 0 or low voltage.

Digital gates are functions that

  • take values 0/1 as inputs and produce 0/1 as output;
  • correspond to logical connectives (many of them).

AND, OR, and NOT gates

$p$ $q$ $\mathsf{out}$
0 0 0
0 1 0
1 0 0
1 1 1
$p$ $q$ $p \wedge q$
$\F$
$\F$
$\F$
$\T$
$p$ $q$ $\mathsf{out}$
0 0 0
0 1 1
1 0 1
1 1 1
$p$ $q$ $p \vee q$
$\F$
$\T$
$\T$
$\T$
$p$ $\mathsf{out}$
0 1
1 0
$p$ $\neg p$
$\T$
$\F$

Combinational logic circuits: wiring up gates

Values get sent along wires connecting gates.

$\neg p \wedge (\neg q \wedge (r \vee s))$

Combinational logic circuits: wiring up gates

Wires can send one value to multiple gates.

$(p \wedge \neg q) \vee (\neg q \wedge r)$

Other useful gates

NAND gate

$\neg (p \wedge q)$

$p$ $q$ $\mathsf{out}$
0 0 1
0 1 1
1 0 1
1 1 0

NOR gate

$\neg (p \vee q)$

$p$ $q$ $\mathsf{out}$
0 0 1
0 1 0
1 0 0
1 1 0

XOR gate

$p \oplus q$

$p$ $q$ $\mathsf{out}$
0 0 0
0 1 1
1 0 1
1 1 0

XNOR gate

$p \leftrightarrow q$

$p$ $q$ $\mathsf{out}$
0 0 1
0 1 0
1 0 0
1 1 1

Checking equivalence

Applications and a basic brute-force algorithm.

Why do we care about checking equivalence?

Many practical problems are solved by logical equivalence checking!
Hardware verification, program verification, query optimization and caching, compiler optimization, …
Example: verifying compiler optimizations
Given a sequence of instructions $S$ and an optimized sequence $P$, we can construct logical formulas $s$ and $p$ that encode their meaning. To verify that $P$ behaves exactly like $S$, we check that $p \leftrightarrow s \equiv \T$.
Demo: verifying compiler peephole optimizations with Alive
Is this optimization correct?
; Original program (S)
%a = xor %y, %x ; a = y ^ x
%b = and %y, %x ; b = y & x
%c = ashr %b, 1 ; c = b >> 1
%d = add %c, %a ; d = c + a
  =>
; Optimized program (P)
%d = add %x, %y ; d = x + y

No! The right shift (ashr) should be replaced with a left shift (shl).

Checking logical (and circuit) equivalence

Can we write an algorithm to decide if two propositions are equivalent?
Yes! Generate the truth tables for both propositions and check if they are the same for every entry.
What is the run time of the algorithm?
Every propositional variable has two possibilities ($\T$, $\F$). If there are $n$ variables, there are $2^n$ rows in the truth table. So the running time is exponential in the number of variables.
In theory, the news are bad …
We know of no algorithm that performs better in general. If you found one, or proved that it doesn’t exist, you’d solve a famous open problem in computer science and win $1 million.
But in practice, the news are pretty good …
Provers like Z3 can solve equivalence checking problems with millions of variables and formulas. And that’s enough for many real applications!

Logical proofs

A method for establishing equivalence that extends to richer logics.

Proof: use known equivalences to derive new ones

To show that $A$ is equivalent to $B$
Apply a series of logical equivalences to subexpressions to convert $A$ to $B$.
To show that $A$ is a tautology
Apply a series of logical equivalences to subexpressions to convert $A$ to $\T$.

Example: show that $A$ is equivalent to $B$

Let $A$ be $p \vee (p \wedge p)$, and let $B$ be $p$.

$p \vee (p \wedge p)$ $\equiv$ $p \vee p $ Idempotence
$\equiv$ $p $ Idempotence
DeMorgan’s laws
$\neg(p \wedge q) \equiv \neg p \vee \neg q$
$\neg(p \vee q) \equiv \neg p \wedge \neg q$
Law of implication
$p \rightarrow q \equiv \neg p \vee q$
Contrapositive
$p \rightarrow q \equiv \neg q \rightarrow \neg p $
Biconditional
$p \leftrightarrow q \equiv (p \rightarrow q) \wedge (q \rightarrow p)$
Double negation
$p \equiv \neg \neg p$
Identity
$p \wedge \mathsf{T} \equiv p$
$p \vee \mathsf{F} \equiv p$
Domination
$p \wedge \mathsf{F} \equiv \mathsf{F}$
$p \vee \mathsf{T} \equiv \mathsf{T}$
Idempotence
$p \wedge p \equiv p$
$p \vee p \equiv p$
Commutativity
$p \wedge q \equiv q \wedge p$
$p \vee q \equiv q \vee p$
Associativity
$(p \wedge q) \wedge r \equiv p \wedge (q \wedge r)$
$(p \vee q) \vee r \equiv p \vee (q \vee r)$
Distributivity
$p \wedge (q \vee r) \equiv (p \wedge q) \vee (p \wedge r)$
$p \vee (q \wedge r) \equiv (p \vee q) \wedge (p \vee r)$
Absorption
$p \wedge (p \vee q) \equiv p$
$p \vee (p \wedge q) \equiv p$
Negation
$p \wedge \neg p \equiv \mathsf{F}$
$p \vee \neg p \equiv \mathsf{T}$

Example: show that $A$ is a tautology

Let $A$ be $\neg p \vee (p \vee p)$.

$\neg p \vee (p \vee p)$ $\equiv$ $\neg p \vee p $ Idempotence
$\equiv$ $p \vee \neg p $ Commutativity
$\equiv$ $\T $ Negation
DeMorgan’s laws
$\neg(p \wedge q) \equiv \neg p \vee \neg q$
$\neg(p \vee q) \equiv \neg p \wedge \neg q$
Law of implication
$p \rightarrow q \equiv \neg p \vee q$
Contrapositive
$p \rightarrow q \equiv \neg q \rightarrow \neg p $
Biconditional
$p \leftrightarrow q \equiv (p \rightarrow q) \wedge (q \rightarrow p)$
Double negation
$p \equiv \neg \neg p$
Identity
$p \wedge \mathsf{T} \equiv p$
$p \vee \mathsf{F} \equiv p$
Domination
$p \wedge \mathsf{F} \equiv \mathsf{F}$
$p \vee \mathsf{T} \equiv \mathsf{T}$
Idempotence
$p \wedge p \equiv p$
$p \vee p \equiv p$
Commutativity
$p \wedge q \equiv q \wedge p$
$p \vee q \equiv q \vee p$
Associativity
$(p \wedge q) \wedge r \equiv p \wedge (q \wedge r)$
$(p \vee q) \vee r \equiv p \vee (q \vee r)$
Distributivity
$p \wedge (q \vee r) \equiv (p \wedge q) \vee (p \wedge r)$
$p \vee (q \wedge r) \equiv (p \vee q) \wedge (p \vee r)$
Absorption
$p \wedge (p \vee q) \equiv p$
$p \vee (p \wedge q) \equiv p$
Negation
$p \wedge \neg p \equiv \mathsf{F}$
$p \vee \neg p \equiv \mathsf{T}$

Example: show equivalence with a truth table and proof

$p \wedge (p \rightarrow q) \equiv p \wedge q$

A truth table for $p \wedge (p \rightarrow q) \leftrightarrow p \wedge q \equiv \T$.

$p$ $q$ $p \rightarrow q$ $p\wedge (p \rightarrow q)$ $p \wedge q$ $p \wedge (p \rightarrow q) \leftrightarrow p \wedge q$
$\F$ $\F$ $\T$ $\F$ $\F$ $\T$
$\F$ $\T$ $\T$ $\F$ $\F$ $\T$
$\T$ $\F$ $\F$ $\F$ $\F$ $\T$
$\T$ $\T$ $\T$ $\T$ $\T$ $\T$

Example: show equivalence with a truth table and proof

$p \wedge (p \rightarrow q) \equiv p \wedge q$

$p \wedge (p \rightarrow q)$ $\equiv$ $p \wedge (\neg p \vee q) $ Law of implication
$\equiv$ $(p \wedge \neg p) \vee (p \wedge q) $ Distributivity
$\equiv$ $\F \vee (p \wedge q) $ Negation
$\equiv$ $(p \wedge q) \vee \F $ Commutativity
$\equiv$ $p \wedge q $ Identity
DeMorgan’s laws
$\neg(p \wedge q) \equiv \neg p \vee \neg q$
$\neg(p \vee q) \equiv \neg p \wedge \neg q$
Law of implication
$p \rightarrow q \equiv \neg p \vee q$
Contrapositive
$p \rightarrow q \equiv \neg q \rightarrow \neg p $
Biconditional
$p \leftrightarrow q \equiv (p \rightarrow q) \wedge (q \rightarrow p)$
Double negation
$p \equiv \neg \neg p$
Identity
$p \wedge \mathsf{T} \equiv p$
$p \vee \mathsf{F} \equiv p$
Domination
$p \wedge \mathsf{F} \equiv \mathsf{F}$
$p \vee \mathsf{T} \equiv \mathsf{T}$
Idempotence
$p \wedge p \equiv p$
$p \vee p \equiv p$
Commutativity
$p \wedge q \equiv q \wedge p$
$p \vee q \equiv q \vee p$
Associativity
$(p \wedge q) \wedge r \equiv p \wedge (q \wedge r)$
$(p \vee q) \vee r \equiv p \vee (q \vee r)$
Distributivity
$p \wedge (q \vee r) \equiv (p \wedge q) \vee (p \wedge r)$
$p \vee (q \wedge r) \equiv (p \vee q) \wedge (p \vee r)$
Absorption
$p \wedge (p \vee q) \equiv p$
$p \vee (p \wedge q) \equiv p$
Negation
$p \wedge \neg p \equiv \mathsf{F}$
$p \vee \neg p \equiv \mathsf{T}$

Example: show tautology with a truth table and proof

$(p \wedge q) \rightarrow (q \vee p)$

A truth table for $(p \wedge q) \rightarrow (q \vee p) \equiv \T$.

$p$ $q$ $p \wedge q$ $q \vee p$ $(p \wedge q) \rightarrow (q \vee p)$
$\F$ $\F$ $\F$ $\F$ $\T$
$\F$ $\T$ $\F$ $\T$ $\T$
$\T$ $\F$ $\F$ $\T$ $\T$
$\T$ $\T$ $\T$ $\T$ $\T$

Example: show tautology with a truth table and proof

$(p \wedge q) \rightarrow (q \vee p)$

$(p \wedge q) \rightarrow (q \vee p)$ $\equiv$ $\neg (p \wedge q) \vee (q \vee p) $ Law of implication
$\equiv$ $(\neg p \vee \neg q) \vee (q \vee p) $ DeMorgan
$\equiv$ $\neg p \vee (\neg q \vee (q \vee p)) $ Associativity
$\equiv$ $\neg p \vee ((\neg q \vee q) \vee p) $ Associativity
$\equiv$ $\neg p \vee (p \vee (\neg q \vee q)) $ Commutativity
$\equiv$ $(\neg p \vee p) \vee (\neg q \vee q) $ Associativity
$\equiv$ $(p \vee \neg p) \vee (q \vee \neg q) $ Commutativity (twice)
$\equiv$ $\T \vee \T $ Negation (twice)
$\equiv$ $\T $ Idempotence
DeMorgan’s laws
$\neg(p \wedge q) \equiv \neg p \vee \neg q$
$\neg(p \vee q) \equiv \neg p \wedge \neg q$
Law of implication
$p \rightarrow q \equiv \neg p \vee q$
Contrapositive
$p \rightarrow q \equiv \neg q \rightarrow \neg p $
Biconditional
$p \leftrightarrow q \equiv (p \rightarrow q) \wedge (q \rightarrow p)$
Double negation
$p \equiv \neg \neg p$
Identity
$p \wedge \mathsf{T} \equiv p$
$p \vee \mathsf{F} \equiv p$
Domination
$p \wedge \mathsf{F} \equiv \mathsf{F}$
$p \vee \mathsf{T} \equiv \mathsf{T}$
Idempotence
$p \wedge p \equiv p$
$p \vee p \equiv p$
Commutativity
$p \wedge q \equiv q \wedge p$
$p \vee q \equiv q \vee p$
Associativity
$(p \wedge q) \wedge r \equiv p \wedge (q \wedge r)$
$(p \vee q) \vee r \equiv p \vee (q \vee r)$
Distributivity
$p \wedge (q \vee r) \equiv (p \wedge q) \vee (p \wedge r)$
$p \vee (q \wedge r) \equiv (p \vee q) \wedge (p \vee r)$
Absorption
$p \wedge (p \vee q) \equiv p$
$p \vee (p \wedge q) \equiv p$
Negation
$p \wedge \neg p \equiv \mathsf{F}$
$p \vee \neg p \equiv \mathsf{T}$

Truth tables versus proofs

Proofs are not smaller than truth tables where there are a few propositional variables.

But proofs are usually much smaller when there are many variables.

We can extend the proof method to reason about richer logics for which truth tables don’t apply.

Theorem provers use a combination of search (truth tables) and deduction (proofs) to automate equivalence checking.

Summary

Checking equivalence has many real-world applications.
Verification, optimization, and more!
There are two ways to check equivalence of propositional formulas.
Brute-force: compare their truth tables.
Proof-based: apply equivalences to transform one into the other.