 # 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 \mathsf{T}}_\mathsf{tautology}$ have the same meaning.

$p$ $q$ $p \wedge q$ $q \wedge p$ $p \wedge q \leftrightarrow q \wedge p$
$\mathsf{F}$ $\mathsf{F}$ $\mathsf{F}$ $\mathsf{F}$ $\mathsf{T}$
$\mathsf{F}$ $\mathsf{T}$ $\mathsf{F}$ $\mathsf{F}$ $\mathsf{T}$
$\mathsf{T}$ $\mathsf{F}$ $\mathsf{F}$ $\mathsf{F}$ $\mathsf{T}$
$\mathsf{T}$ $\mathsf{T}$ $\mathsf{T}$ $\mathsf{T}$ $\mathsf{T}$

When $p=\mathsf{T}$ and $q=\mathsf{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:

• $\mathsf{T}$ corresponds to 1 or high voltage.
• $\mathsf{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$
$\mathsf{F}$ $\mathsf{F}$ $\mathsf{F}$
$\mathsf{F}$ $\mathsf{T}$ $\mathsf{F}$
$\mathsf{T}$ $\mathsf{F}$ $\mathsf{F}$
$\mathsf{T}$ $\mathsf{T}$ $\mathsf{T}$
$p$ $q$ $\mathsf{out}$
0 0 0
0 1 1
1 0 1
1 1 1
$p$ $q$ $p \vee q$
$\mathsf{F}$ $\mathsf{F}$ $\mathsf{F}$
$\mathsf{F}$ $\mathsf{T}$ $\mathsf{T}$
$\mathsf{T}$ $\mathsf{F}$ $\mathsf{T}$
$\mathsf{T}$ $\mathsf{T}$ $\mathsf{T}$
$p$ $\mathsf{out}$
0 1
1 0
$p$ $\neg p$
$\mathsf{F}$ $\mathsf{T}$
$\mathsf{T}$ $\mathsf{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 \mathsf{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 = x & 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 ($\mathsf{T}$, $\mathsf{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 …

### 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.