<<

: and Simplification

CS 270: Mathematical Foundations of Computer Science Jeremy Johnson Propositional Calculus

Topics Motivation: Simplifying Conditional Expressions Rules of Boolean Algebra Proofs Using Truth Tables Simplification and Equational Reasoning Logic Circuits Functional Completeness of nand Tautologies, Satisfiability and Automatic Verification Programming Example  Boolean expressions arise in conditional statements. It is possible to abstract the relations with boolean variables (propositions that are either true or false). Using this abstraction one can reason and simplify conditional statements.  if ((a < b) || ((a >= b) && (c == d))) then { … } else { … }  Let p denote the relation (a

3 Programming Example (cont)

 The previous expression is equivalent (two expressions are equivalent if they are true for the same values of the variables occurring in the expressions) to a simpler expression

 (p || !p && q) ≡ p || q

 We can see this since if p is true both expressions are true, and if p is false, then !p is true and (!p && q) is true exactly when q is true.

4 Boolean Algebra

 The Boolean operators ∨ and ∧ are analogous to addition and multiplication with true and false playing the roles of 1 and 0. Complement is used for .  This provides a compact notation and suggests appropriate algebraic simplification  Similar properties hold such as the associative, commutative, and distributive identities. Disjunctive Normal Form  A Boolean expression is a  Any Boolean function can be written as a Boolean

expression s x0 x1 f 0 0 0 0  Write a Boolean expression that evaluates to true for 0 0 1 0 each row in the that is true and false for other rows. 0 1 0 1  The Boolean expression for a given row is the 0 1 1 1 conjunction of the variables that are true and the 1 0 0 0 negation of variables that are false. 1 0 1 1  Take the disjunction of all such rows. 1 1 0 0  E.G. (multiplexor function) 1 1 1 1

(¬s ∧ x0 ∧ ¬x1 ) ∨ (¬s ∧ x0 ∧ x1 ) ∨ (s ∧ ¬x0 ∧ x1 ) ∨ (s ∧ x0 ∧ x1 ) 6 Sums of Products  Disjunctive normal form, using the notation of Boolean Algebra, corresponds to a sum of products s x0 x1 f  E.G. (multiplexor function) 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1

7 Properties of Boolean Algebra  Boolean expressions can be simplified using rules of Boolean algebra  Identity law: A + 0 = A and A ● 1 = A.  Zero and One laws (annhilator): A + 1 = 1 and A ● 0 = 0  Inverse laws (complementation):  Idempotent laws: A + A = A = A ● A  Commutative laws: A + B = B + A and A ● B = B ● A.  Associative laws: A + (B + C) = (A + B) + C and A ● (B ● C) = (A ● B) ● C.  Distributive laws: A ● (B + C) = (A ● B) + (A ● C) and A + (B ● C) = (A + B) ● (A + C)  Absorption laws: A●(A+B) = A = A + A●B  Double Negation: =  DeMorgan’s laws: 𝐴𝐴̅ 𝐴𝐴 Proof using Truth Tables

E.G. DeMorgan’s Law

A B (A ∨ B) ¬(A ∨ B) ¬A ∧ ¬B 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 Completeness

 Any identity from Boolean Algebra can be proved using the properties listed

 A sufficient  Associativity, commutativity, absorption, one of the distributive laws and the two complement laws Proof of Double Negation

= + 0 = + 𝑥𝑥̅ = (𝑥𝑥̅ + )( + ) = 𝑥𝑥̅ +𝑥𝑥𝑥𝑥̅ 1 = (𝑥𝑥̅ +𝑥𝑥 )(𝑥𝑥̅ +𝑥𝑥̅ ) = (𝑥𝑥̅ + 𝑥𝑥)(� + ) = 𝑥𝑥+̅ 𝑥𝑥 𝑥𝑥 𝑥𝑥̅ = 𝑥𝑥+ 0𝑥𝑥̅ 𝑥𝑥 𝑥𝑥̅ = 𝑥𝑥 𝑥𝑥̅𝑥𝑥̅ 𝑥𝑥 𝑥𝑥 Simplification of Boolean Expressions  Simplifying multiplexor expression using Boolean algebra

 Equational reasoning: replace subexpressions by equivalent expressions  Reflexive, Symmetric, Transitive  Verify that the boolean function corresponding to this expression has the same truth table as the original function. 12 Polynomial Algebra

Boolean operations via polynomial algebra Assume x2 = x, y2 = y,… Arithmetic performed mod 2 1⋅0 = 0⋅1 = 0⋅0 = 0, 1⋅1 = 1 [behaves like and] 0+0 = 1+1 = 0, 0+1 = 1+0 = 1 [behaves like xor]

And. x ∧ y via multiplication xy Xor. x ⊕ y via addition x + y Not. ¬x via 1+x Or. x ∨ y via x+y+xy

13 Polynomial Identities

Inverse Laws: (x ∧ ¬x) = F, (x ∨ ¬x) = T x(1+x) = x + x2 = 2x = 0 x + (1+x) + x(1+x) = 1 Distributive Law: x ∧ (y ∨ z) x(y + z + yz) = xy + xz + xyz = xy + xz + x2yz = xy + xz + xyxz DeMorgan’ Laws: ¬x ∧ ¬y = ¬(x ∨ y) and ¬x ∨ ¬y = ¬(x ∧ y) (1+x)(1+y) = 1 + x + y + xy (1+x)+(1+y) + (1+x)(1+y) = (1+x) + (1+y) + (1 + x + y + xy) = 1+xy 14 Logic Circuits  A single line labeled x is a logic circuit. One end is the input and the other is the output. If A and B are logic circuits so are:

 and gate A B

A  or gate B

 inverter (not) A

15 XOR

“One or the other, but x y x ⊕ y not both” 0 0 0 0 1 1 1 0 1 Notation for circuits: 1 1 0

x x y

y Multi-Input Gates  Cascaded and gate  A ∧ B ∧ C ∧ D ≡ ((A ∧ B) ∧ C) ∧ D ≡ (A ∧ B) ∧ (C ∧ D) ≡ A ∧ (B ∧ (C ∧ D))

A A B B

C C D D

A B C D

17 Logic Circuits Given a boolean expression it is easy to write down the corresponding logic circuit Here is the circuit for the original multiplexor expression

x0

x1

s

18 Logic Circuits

Here is the circuit for the simplified multiplexor expression

x0

x1

s

19 Nand

Nand – negation of the conjunction x y x | y operation: 0 0 1 0 1 1 1 0 1 1 1 0

A is an inverted and gate:

20 Implementing Logic Gates with Transistors

+V

+V A NAND B A output gate B ground ground

A Transistor NOT Gate A Transistor NAND Gate

21 Nand is functionally complete

Using DeMorgan’s Law, ¬ and ∧ are enough to generate all Boolean functions All boolean functions can be implemented using nand gates (and, or and not can be implemented using nand) not: x y x | y 0 0 1  and: 0 1 1 1 0 1 or: 1 1 0 Arithmetic via Logic Full Adder Used to add to binary numbers stored as an array of bits using carry ripple addition

CarryIn

 a Three binary inputs Sum a, b and CarryIn b Two binary outputs CarryOut Sum and CarryOut such that a + b + CarryIn = 2*CarryOut + Sum Carry 110 A 101 B 111 A+B = 1100 23 Logic Circuit Lab Exercise Derive a truth table for the output bits (Sum and CarryOut) of a full adder. Using the truth table derive a sum of products expression for Sum and CarryOut. Draw a circuit for these expressions. Using properties of Boolean algebra simplify your expressions. Draw the simplified circuits.

CarryIn

a

Sum

b

CarryOut

24 Tautologies  A tautology is a boolean expression that is always true, independent of the values of the variables occurring in the expression. The properties of Boolean Algebra are examples of tautologies.  Tautologies can be verified using truth tables. The truth table below shows that x → y ≡ ¬ x ∨ y

x y x → y ¬ x ∨ y 0 0 1 1 0 1 1 1 1 0 0 0 1 1 1 1

25 Exercise

Derive the tautology x → y ≡ ¬ x ∨ y from the sum of products expression obtained from the truth table for x → y. You will need to use properties of Boolean algebra to simplify the sum of products expression to obtain the desired equivalence.

26 Solution x y x → y 0 0 1 Derive the tautology 0 1 1 1 0 0 x → y ≡ ¬ x ∨ y 1 1 1

(¬ ¬ ) ¬ (¬ ¬ ) (¬ ) ¬ 𝑥𝑥 → 𝑦𝑦 ≡ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ¬ (¬ ) ¬ ≡ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ¬ # (# ) ≡ 𝑥𝑥 ∧ 𝑦𝑦 ∨ 𝑦𝑦 ∨ 𝑥𝑥 ∨ 𝑥𝑥 ∧ 𝑦𝑦 ¬ ≡ 𝑥𝑥 ∧ 𝑡𝑡 ∨ 𝑡𝑡 ∧ 𝑦𝑦 ≡ 𝑥𝑥 ∨ 𝑦𝑦 27 Tautology Checker  A program can be written to check to see if a Boolean expression is a tautology.

 Simply generate all possible truth assignments for the variables occurring in the expression and evaluate the expression with its variables to each of these assignments. If the evaluated expressions are always true, then the given Boolean expression is a tautology.

 Two Boolean expressions E1 and E2 are equivalent if E1 ≡ E2 is a tautology.

28 Proof using Truth Tables

E.G. DeMorgan’s Law

A B (A ∨ B) ¬(A ∨ B) ¬A ∧ ¬B 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 Satisfiability

 A formula is satisfiable if there is an assignment to the variables that make it true  Checking to see if a formula f is satisfiable can be done by searching a truth table for a true entry  Exponential in the number of variables  Does not appear to be a polynomial time algorithm (satisfiability is NP-complete)  There are efficient satisfiability checkers that work well on many practical problems  f is a tautology ¬ f is not satisfiable. Arguments

 An argument is a list of premises followed by a conclusion

 ϕ1,…,ϕn ∴ ψ  An argument is valid if any assignment that makes the premises true also makes the conclusion true  An argument is invalid if there is some assignment, counter example, that makes the premises true but does not make the conclusion true

 Equivalent to ϕ1 ∧ … ∧ ϕn → ψ Lab Exercise Derive a truth table for the output bits (Sum and CarryOut) of a full adder. Using the truth table derive a sum of products expression for Sum and CarryOut. Draw a circuit for these expressions. Using properties of Boolean algebra simplify your expressions. Draw the simplified circuits.

CarryIn

a

Sum

b

CarryOut

32