<<

CSE 1400 Applied Discrete Boolean Department of Sciences College of Engineering Florida Tech Fall 2011

Control Structures 1 Boolean Logic 3 Normal Forms 4 The Conditional Operator 5 Complex 6 Truth Tables 8 10 Satisfiable and Valid Propositions 11 Constructing Boolean Functions 12 Satisfiability 13 Counting Boolean Expressions 14 Consistency and Completeness of 14 Problems on Boolean Logic 15

Abstract Logic controls the action of a computer.

Control Structures

The Böhm-Jacopini shows that only three funda- mental control structures are necessary to implement any Turing algorithm.

1. Execute instructions sequentially one after another as in figure 1

2. Select one of two instruction to execute according to the value of a Boolean variable as in figure 2

3. Iterate through a sequence of instructions until a Boolean variable changes value as in figure 3 cse 1400 applied discrete mathematics boolean logic 2

Figure 1: Sequential execution of statements.

Statement

Next Statement

Figure 2: Conditional execution of statements.

Boolean Condition

Then Else Statement(s) Statement(s)

Figure 3: Loop (iterative) execution of statements.

Boolean Condition

Loop Statement(s) cse 1400 applied discrete mathematics boolean logic 3

Boolean Logic

Boolean logic provides the basis to control the execution of algorithms. studies the behav- ior of formulas constructed using Boolean variables. The domain Boolean variables are typically named of these variables is the of truth values B = {False, True}.A p, q, r, s,.... Boolean variable is also called a and is often inter- preted as the name for a declarative natural language sentence: A proposition is a sentence that can only be True or False. A formal Example propositions: describes what propositions are and how they can be ma- • All humans are mortal. (True) nipulated. • Socrates was human. (True) • Socrates was mortal. (True) 1. False, also called 0, is a proposition. • George Washington was President of the United States of America from 2. True, also called 1, is a proposition. 1789 to 1797. (True) 3. Boolean variables over the set of bits are propositions. If p is a • May 1, 1911 was a Wednesday. (False) Boolean variable, then p ∈ {0, 1}. •2 + 3 = 7 (False) 4. If p and q are propositions, then • π = 3.14 (False) • All relations are functions. (False) ¬ (a)N ot p, denoted p is a proposition p and ¬p are called literals. One of (b) p and q, denoted p ∧ q is a proposition them is False, the other is True. Haskell uses && to express and. (c) p or q, denoted p ∨ q is a proposition Haskell uses k to express or. 5. Nothing else is a proposition.

To begin we need to define the Boolean operations described above. The not operator ¬ is defined by 1 and repre- sented by the in figure 4.

The not Operator Table 1: The truth table for the not Input Output operator. p ¬p 0 1 1 0

p ¬p Figure 4: Logic gate for the not opera- tor.

The and operator ∧ is defined by truth table 2 and represented by the logic gate in figure 5.

p p ∧ q Figure 5: Logic gate for the and opera- q tor. cse 1400 applied discrete mathematics boolean logic 4

The and Operator Table 2: The truth table for the and Input Output operator. p q p ∧ q 0 0 0 0 1 0 1 0 0 1 1 1 The or Operator Table 3: The truth table for the or Input Output operator. p q p ∨ q 0 0 0 0 1 1 1 0 1 1 1 1

The or operator ∨ is defined by truth table 3 and represented by the logic gate in figure 6. From a mathematical point of view, the meaning of proposition p is immaterial. Collections of Boolean variables are manipulated using the above rules, that is all. From a computing point of view, the meaning of a proposition gives reasons for manipulating it.

Normal Forms

A form is a syntactic structure. A form is normal if every expression can be transformed into its structure. Two normal forms for Boolean logic are conjunctive and disjunctive normal forms.

•A literal is a Boolean variable p or its denial ¬p.

•A clause is a disjunction of literals, for example

p ∨ ¬q ∨ ¬r

is a clause.

• An implicant is a conjunction of literals, for example

p ∧ ¬q ∧ ¬r

is a implicant.

p ∨ Figure 6: Logic gate for the or operator. q p q cse 1400 applied discrete mathematics boolean logic 5

A Boolean expression B is in if B is the conjunction of clauses ck (each ck is the disjunction of literals.)

n−1 ^ B = ck k=0 For instance, (p ∨ r) ∧ (¬q ∨ r)

is in conjunctive normal form. A Boolean expression B is in if B is

the disjunction of implicants dk (each dk is the conjunction of liter- als.) n−1 _ B = dk k=0 For instance, (p ∧ r) ∨ (¬q ∧ r)

is in disjunctive normal form.

The Conditional Operator

Conditional statements bridge from True statement to True statement. In logic, the conditional is written

if p then q

where p and q are Boolean variables. Symbolically, the conditional is written p → q

The value of a conditional ‘if p then q’ is defined as follows. The logical conditional is sim- ilar to the Haskell conditional 1. When p is True the value of the conditional has the value of q. if p then q else true.

2. When p is False the value of the conditional is True.

That is,  q if p = True if p then q = True if p = False These rules are summarized in the truth table below, which also shows that the conditional p → q is equivalent to ¬p ∨ q. The value (True or False) of a conditional can be difficult to understand, especially when the premise is False. To gain some understanding, consider these statements.

• “John scores above 90 on the final.” Call this p. cse 1400 applied discrete mathematics boolean logic 6

Table 4: The conditional operator.

Input Output p q p → q ¬p ∨ q 0 0 1 1 0 1 1 1 1 0 0 0 1 1 1 1

p p → q q

• “John earns an A in the course.” Call this q. Pretend I make the statement • “If John scores above 90 on the final, then John earns an A in the course.” That is p → q. Ask yourself, when my statement is True and when it is False? There are four cases. Two cases where p is True: 1. If John scores above 90 on the final and John earns an A in the course, do you agree my statement p → q is True?

2. If John scores above 90 on the final and John does not earn an A in the course, do you agree my statement p → q is False? And two cases where p is False. 2. If John scores 90 or below on the final and John’s grade is B or be- low, do you agree my statement p → q is not False and therefore must be True?

3. If John scores 90 or below on the final and John grade is A, do you agree my statement p → q is not False and therefore must be True? Notice the statement p → q tells you nothing about the value of q when p = False. The conditional operator is at the heart of establishing a proof. p → q is called an . p is called the premise. q is called the conclusion. Complex Propositions

Complex propositions can be made from by combining sim- ple propositions using Boolean not, and and or opera- tions. For values of n = 0, 1, 2, . . ., it is useful to list the expressions 2n that can be created using n Boolean variables p0 through pn−1. There are 2 Boolean functions on n Boolean variables. cse 1400 applied discrete mathematics boolean logic 7

1. When n = 0 there are no propositions, but there are two Boolean functions, the constant True and the constant False.

2. When n = 1 there is one proposition p and four Boolean functions: True, False, p, and ¬p. Notice that True can be written as an expression in p: True = p ∨ ¬p. Also, 3. When n = 2 there is 2 propositions p and q, and sixteen Boolean False can be written as an expression in functions. p: False = p ∧ ¬p. See the section on primitive type Bool.

Name Name Symbol

False 0 not or (nor) ¬(∨) and ∧ equivalence ≡ not conditional ¬( → ) not q ¬q bufferp p converse ← not converse ¬(← ) not p ¬p bufferq q conditional → ⊕ not and (nand) ¬(∧) or ∨ True 1 I use the binary value in the table as Input BooleanFunctions a check-sum to know that each of the 2 22 = 16 Boolean functions are listed. p q 0 p ∧ q ¬(p → q) p ¬(q → p) q p ⊕ q p ∨ q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 1 0 1 Binary 0 1 2 3 4 5 6 7

Input Boolean Functions p q ¬(p ∨ q) ≡ ¬q q → p ¬p p → q ¬(p ∧ q) 1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 1 0 1 Binary 8 9 10 11 12 13 14 15 Each of the three variables can assume one of two values giving eight combi- 23 4. When n = 3 there are two-hundred-and-fifty-six, that is 2 = nations (0, 0, 0) through (1, 1, 1).A 28 = 256, different Boolean functions can be constructed using 3 maps each combination to one of two values. Therefore there are 28 Boolean variables p, q, and r. different functions. n 5. In general, 22 different Boolean functions can be defined on a domain of n Boolean variables p0,..., pn−1. cse 1400 applied discrete mathematics boolean logic 8

Number of variables Number of truth assignments Number of Boolean functions

0 0 1 2 = 22 1 1 2 4 = 22 2 2 4 16 = 22 3 3 8 256 = 22 4 4 16 65536 = 22 n n 2n 22 The number of Boolean functions grow rapidly as the number of n propositional variables increases There are 22 different Boolean functions in n variables.

Truth Tables

Given a Boolean function in n variables

B(p0,..., pn−1),

how do you compute its output for given input values for

its variables p0,..., pn−1? For small values of n, a truth table is a reasonable method to express the computation. For instance, pretend you want to know the input/output behavior of the function

B(p, q) = ¬(p ∨ q)

Using the basic operations described above you can create a truth table for B(p, q). The four combinations of input values are listed in the two left columns, and the outputs for these inputs are listed in the ¬ column colored crimson.

Input Output p q ¬ (p ∨ q) 0 010 0 101 1 001 1 101

When the output column is always True, the Boolean expression is said to be a . The expression ((p) ∧ (p → q)) → q, called cse 1400 applied discrete mathematics boolean logic 9 modus ponens, is a basic rule of .

Input Output p q (p ∧ (p → q)) → q 0 0 0 0 110 0 1 0 0 111 1 0 1 0 010 1 1 1 1 111

This xkcd cartoon used under a Creative Commons license. See http://xkcd.com/license.html A tautology is a theorem of Boolean logic. There are several useful Boolean logic tautologies.

Modus Ponens p ∧ (p → q) → q

Modus Tollens ¬q ∧ (p → q) → ¬p

Reductio ad Absurdum

(¬p → False) → p

Resolution (q → q) ∧ (¬p → r) → q ∨ r

Simplification (p ∧ q) → p

Hypothetical

[(p → q) ∧ (q → r)] → (p → r)

The of a tautology is called a contradiction. Consider the expression Apply DeMorgan’s laws and convert disjunctive forms ¬a ∨ b to conditional ((p ∧ ¬q) ∨ (¬p ∧ r)) ∧ (¬q ∧ ¬r) a → b notation. cse 1400 applied discrete mathematics boolean logic 10 which is the negation the resolution

(p → q) ∧ (¬p → r)) → (q ∨ r)

Input Output p q r ((p ∧ ¬q) ∨ (¬p ∧ r)) ∧ (¬q ∧ ¬r) 0 0 0 0 0 001 0 0 1 0 1 100 0 1 0 0 0 000 0 1 1 0 1 100 1 0 0 1 0 001 1 0 1 1 0 000 1 1 0 0 0 000 1 1 1 0 0 000

A contingency is a Boolean expression that is sometimes True and sometimes False. Satisfiability (SAT for short) is a classic hard : Given a Boolean expression B(p0, p1,..., pn−1) Logical Equivalence in n Boolean variables, is there an assignment of True or False values to each of the variables that make B( ) True You can convey the same meaning (or value) in many ways. p0, p1,..., pn−1 ? Consider the three statements.

1. If it is raining and sunny there will be a rainbow.

2. If there is no rainbow, then it is not raining or not sunny.

3. It is not raining or it is not sunny or there is a rainbow.

Although it may not be obvious at first glance, these three propo- sitions are equivalent. To understand this, let p, q, and r be the atomic propositions “it is raining”, “it is sunny”, “there is a rain- bow” Using these names, and logical operations, the three complex Logicians would identify p, q, and propositions 1, 2, 3 can be represented symbolically. r as temporal propositions. Their truth is not absolute, but depends on ( ∧ ) → time. Likewise, their truth depends 1. p q r on location as well. Spacio-temporal reasoning is an advanced field with 2. (¬r) → ((¬p) ∨ (¬q)) many applications.

3. (¬p) ∨ (¬q) ∨ r

Using these symbolic representations, a truth table can be con- structed. Notice that the output is identical for each combination cse 1400 applied discrete mathematics boolean logic 11 of input signals.

Input Output p q r (p ∧ q) → r (¬r → (¬p) ∨ (¬q))(¬p) ∨ (¬q) ∨ r 0 0 0 1 1 1 0 0 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1

In the truth table, the output values can reasoned as follows.

1. For statement 1, the output is1 for p = 0 or q = 0, which fills in the first 6 rows. The output of seventh row is 0 because the premise p ∧ q is True and the conclusion r is False. And, the output in row eight is 1 because the premise is True and the con- clusion is True.

2. For statement 2, the output is1 whenever r = 1, which fills every other row with 1 starting from the second. The output is also 1 whenever p or q is false. For the seventh row, the output is 0 because the premise ¬r is True and the conclusion ¬p ∨ ¬q is False.

3. Verify the output for statement 3 for exercise.

Boolean expressions are equivalent when they have identical input-output behavior. This equivalence partitions Boolean expres- • Reflexive: B(p, q,...) ≡ B(p, q,...) sions into equivalence classes. Notice this instance of equivalence is a for all Boolean expressions ( ) relation that is reflexive, symmetric, and transitive. B p, q,... . • Symmetric: If B(p, q,...) ≡ C(p, q,...), then C(p, q,...) ≡ Satisfiable and Valid Propositions B(p, q,...), for all Boolean expres- sions B(p, q,...) and C(p, q,...). Definition 1 (Truth Assignment). Let B = B(p0, p1,..., pn−1) be a • Transitive: If A(p, q,...) ≡ Boolean function in the atomic (True or False) variables B(p, q,...) and B(p, q,...) ≡ C(p, q,...), then A(p, q,...) ≡ C(p, q,...), for all Boolean expres- p0, p1,..., pn−1. sions A(p, q,...), B(p, q,...), and C(p, q,...). An truth assignment T maps values True or False to each of the (p0, p1, p2) = (True, False, True) is a variables p0, p1,..., pn−1. truth assignment of the variables in the Boolean function Definition 2 (Satisfiable Propositions). A Boolean expression B is B = B(p0, p1, p2) satisfiable if B has a truth assignment that makes B True. cse 1400 applied discrete mathematics boolean logic 12

Definition 3 (Valid Propositions). A Boolean expression B is valid if B is True for all truth assignments.

Definition 4 (Unsatisfiable Propositions). If B is valid, then ¬B is un- satisfiable. That is, ¬B is unsatisfiable if no truth assignment that makes ¬B True, or equivalently, ¬B is False for every truth assignment.

Constructing Boolean Functions

How can you build a circuit that has given input-output behavior? Pretend you needed to construct a Boolean function that behaves as indicated in the table below. Input Output p q r 0 0 1 0 1 0 1 0 0 1 1 1

To build the Boolean function as a disjunction of conjunctions write a conjunctive clause for each row where the output is True. The clause is the AND of literals in the row. The variable used if it is True and the negation of the variable is used otherwise. For instance in the truth table above there are two rows with output 1. Using the rule, the first row produces the conjunctive clause (¬p ∧ ¬q), and the fourth row produces the conjunctive clause (p ∧ q). Next, form the disjunction (OR) of all the conjunctive clauses.

r(p, q) = (¬p ∧ ¬q) ∨ (p ∧ q)

To build a Boolean function as a conjunction of disjunctions write a disjunctive clause for each row where the output is False. The clause is the OR of literals in the row. The variable used if it is False and the negation of the variable is used otherwise. Using the truth table above there are two rows with output 0. Using the rule, the second row produces the disjunctive clause. (p ∨ ¬q), and the third row produces the disjunctive clause. (¬p ∧ q). Form the AND of all the disjunctive clauses.

r(p, q) = (p ∨ ¬q) ∧ (¬p ∨ q)

A more complex instance is given in the truth table below that describes a full adder: The Boolean function that adds two bits a and b and a carry-in bit cin to produce a sum bit s and a carry-out bit cout cse 1400 applied discrete mathematics boolean logic 13

Input Output

a b cin s cout 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1

The sum bit s can be represented by the expression s = (¬a ∧ ¬b ∧ cin) ∨ (¬a ∧ b ∧ ¬cin) ∨ (a ∧ ¬b ∧ ¬cin) ∨ (a ∧ b ∧ cin) which can be factored as a ⊕ b ⊕ cin where ⊕ is the xoroperation. The carry-out bit cout can be represented by the expression cout = (¬a ∧ b ∧ cin) ∨ (a ∧ ¬b ∧ cin) ∨ (a ∧ b ∧ ¬cin) ∨ (a ∧ b ∧ cin) which can be factored as

(a ∧ b) ∨ (cin ∧ (a ⊕ b))

Two expressions that compute output s are

(¬p ∧ ¬q ∧ ¬r) ∨ (¬p ∧ q ∧ r) ∨ (p ∧ ¬q ∧ ¬r) ∨ (p ∧ q ∧ r) and

(p ∨ q ∨ ¬r) ∧ (p ∨ ¬q ∨ ¬r) ∧ (¬p ∨ ¬q ∨ r) ∧ (¬p ∨ ¬q ∨ ¬r)

Satisfiability

The satisfiability problem is: SAT is how one refers to the satisfiabil- ity problem. Given a Boolean function, is there some assignment of truth values that maps to True?

Satisfiability is known to be a hard problem. To find a truth assign- ment for a Boolean function B(p0,..., pn−1) one may have to test all n 2 possible truth assignments of the n Boolean variables p0,..., pn−1. cse 1400 applied discrete mathematics boolean logic 14

Counting Boolean Expressions

Many propositions can be constructed using Booleanop - erations over a set of propositions. Pretend you are given a set of n propositions. Think about it. A Boolean expression con- Call the n propositions p0, p1,..., pn−1. structed using the formal syntax rules above is a function from the domain B × B × · · · × B = Bn of the propositions p0, p1,..., pn−1 to the range B = {0, 1}. The cardinality of the domain is 2n since each proposition pk, k = 0, 1, . . . , n − 1 can be in one of two states: True or False. The cardinality of the range is 2. To define a function, each of the 2n input values in the domain is mapped to one, and only one, of the 2 values in the range. That is, there are 2 choices 2n n times or 22 different choices. Each one represents a different Boolean function.

Consistency and Completeness of Boolean Algebra

Boolean algebra is based on underlying and the that can be derived from from the axioms using rules of inference. A direct proof of a proposition q is obtained by applying the rule of inference

If p is True and p → q is True, then q is True.

Boolean logic is consistent if it is not possible to construct a Boolean proposition p such that both p and ¬p are True. Stated dif- ferently, Boolean logic is inconsistent if it is possible to construct a Boolean proposition p such that both p and ¬p are True. Boolean logic is complete if it there is a proof of every True Boolean proposition s. Stated differently, Boolean logic is incom- plete if there is a True proposition s that no proof within Boolean logic. Within the system of Boolean logic It turns out that Boolean logic is both consistent and complete. The it is possible to prove every True proposition (complete) and not possible next few ideas outline why this is True. to prove any False proposition. Consider the Boolean expression

s = p → (¬p → q) cse 1400 applied discrete mathematics boolean logic 15

Construct a truth table for r and determine that it is a tautology.

Input Output p q p → (¬p → q) 0 0 0 1 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1

Tautologies can be used in proof, and it is convenient to name them. Let’s call s = p → (¬p → q) the consistency/complete rule. Now, let’s pretend that Boolean logic is inconsistent. That is, let’s pretend there is a Boolean proposition p such that both p and 6= p are theorems. Then, since is

s = p → (¬p → q) is a theorem and p is True, it must be that ¬p → q is True. And, since ¬p is True, it must be that q is True. That is,

If Boolean logic is inconsistent, then every proposition q is a theorem. Stated in the contra-positive, if there is proposition q that is not a theorem, then Boolean logic is consistent.

To complete the argument, the proposition q = r ∨ s is an instance of a proposition that is not always True. Therefore, Boolean logic is consistent.

Problems on Boolean Logic

1. Match the term with its symbol. (a) AND (a) ∧ (b) Equivalent (b) ⊕ (c) Exclusive or (c) ≡ (d) True (d) ¬ (e) False (e)0 (f) Implies (if . . . then . . . ) (f) ∨ (g) NOT (g) → (h) OR (h)1 (i) Conditional (if . . . then . . . ) 2. Given that p = True, q = False and r = False, compute the value of the following propositions. cse 1400 applied discrete mathematics boolean logic 16

(a) ¬p ∨ r (e) p ∧ ¬r (b) ¬p → r (f) (p ∧ q) → r (c) ¬(p ∧ q) ∨ ¬r (g) ¬(p ∨ ¬q) ∧ ¬r (d) (p → r) → q (h) p → (r → q)

3. Express the conditional operator  1 if p = 0 or (p = 1 and q = 1) p → q = 0 if p = 1 and q = 0

using the not ¬ and or ∨ operations. 4. Write the Haskell expression

if p then q else r

as a Boolean expression using variables p, q and r and operators ¬, ∧ and →. 5. When p is False, describe the output values of the conditional from problem 4. 6. When p is True, describe the output values of the conditional from problem 4. 7. Show that the conditional p → q is equivalent to ¬p ∨ q. 8. Construct truth tables to prove DeMorgan’s laws (a) ¬(p ∧ q) ≡ (¬p) ∨ (¬q) (b) ¬(p ∨ q) ≡ (¬p) ∧ (¬q) 9. Show how to distribute NOT into a conditional.

¬(p → q) = ( )

10. Show how to factoring NOT out of a conditional.

¬p → ¬q ≡ ¬( )

11. Construct a truth table to prove a conditional is equivalent to its contra-positive, that is,

p → q ≡ ¬q → ¬p

12. Construct a truth table to prove AND distributes over OR, that is,

p ∧ (r ∨ r) ≡ (p ∧ r) ∨ (p ∧ r)

13. Construct a truth table to prove OR distributes over AND, that is,

p ∨ (r ∧ r) ≡ (p ∨ r) ∧ (p ∨ r)

Construct a truth table for the Boolean expression

((p → q) ∧ (q → r)) ∧ ¬(p → r) cse 1400 applied discrete mathematics boolean logic 17

14. Construct a truth table for the Boolean expression

((p ∧ q) → r) ≡ (p → (q → r))

Is the equivalence True or False? Explain your answer. 15. Construct a truth table for the Boolean expressions

(p ← q) ⊕ (¬q → p)

16. Identify each statement as a tautology, a contradiction, or a contingency. (a) A ∩ ∅ = U (b) A ∩ (B ∪ C) = (A ∪ B) ∪ (A ∩ C) (c) {(0, 1)} ⊆ {(0, 0), (0, 1), (1, 0), (1, 1)} (d) (0, 1) ⊆ {(0, 0), (0, 1), (1, 0), (1, 1)} (e) (0, 1) ∈ {(0, 0), (0, 1), (1, 0), (1, 1)} (f) (p → (q ∧ ¬q)) → ¬p 17. Complex computer circuits can be build from three basic gate types, called And, Or, and Not, and drawn as illustrated below. p ∧ q p q

And Gate p ∨ q p q

Or Gate p ¬p

Not Gate

The inputs p and q are high (1) or low (0) voltages. Fill in the chart below.

p q ¬p p ∧ q p ∨ q 0 0 0 1 0 0 1

18. Let Bn be the set of all strings of high or low voltages of length n. For instance there are 4 strings of length 2. B2 = {00, 01, 10, 11} What is the cardinality of Bn? 19. How many functions can be constructed from Bn to B = {0, 1} using And, Or, and Not operations? 20. How many functions can be constructed from Bn to Bm = {0m, . . . , 1m} using the gate operations And, Or, and Not? 21. It is estimated that there are 1080 atoms in the universe. Find the number of propositions n such that there are more Boolean functions in n variables than there are atoms in the universe.