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$.
; 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 $