Docsity

02A - Logic Notes - G, Study notes of Computer science

Toggle sidebar
bg1
Chapter 3
Propositional Formulas
It is amazing that people manage to cope with all the ambiguities in the English
language. Here are some sentences that illustrate the issue:
1. “You may have cake, or you may have ice cream.”
2. “If pigs can fly, then you can understand the Chebyshev bound.”
3. “If you can solve any problem we come up with, then you get an A for the
course.”
4. “Every American has a dream.”
What precisely do these sentences mean? Can you have both cake and ice cream
or must you choose just one dessert? If the second sentence is true, then is the
Chebyshev bound incomprehensible? If you can solve some problems we come
up with but not all, then do you get an A for the course? And can you still get an A
even if you can’t solve any of the problems? Does the last sentence imply that all
Americans have the same dream or might some of them have different dreams?
Some uncertainty is tolerable in normal conversation. But when we need to for-
mulate ideas precisely —as in mathematics and programming —the ambiguities
inherent in everyday language can be a real problem. We can’t hope to make an
exact argument if we’re not sure exactly what the statements mean. So before we
start into mathematics, we need to investigate the problem of how to talk about
mathematics.
To get around the ambiguity of English, mathematicians have devised a spe-
cial mini-language for talking about logical relationships. This language mostly
uses ordinary English words and phrases such as “or”, “implies”, and “for all”.
But mathematicians endow these words with definitions more precise than those
found in an ordinary dictionary. Without knowing these definitions, you might
sometimes get the gist of statements in this language, but you would regularly get
misled about what they really meant.
37
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download 02A - Logic Notes - G and more Study notes Computer science in PDF only on Docsity!

Chapter 3

Propositional Formulas

It is amazing that people manage to cope with all the ambiguities in the English language. Here are some sentences that illustrate the issue:

  1. “You may have cake, or you may have ice cream.”
  2. “If pigs can fly, then you can understand the Chebyshev bound.”
  3. “If you can solve any problem we come up with, then you get an A for the course.”
  4. “Every American has a dream.”

What precisely do these sentences mean? Can you have both cake and ice cream or must you choose just one dessert? If the second sentence is true, then is the Chebyshev bound incomprehensible? If you can solve some problems we come up with but not all, then do you get an A for the course? And can you still get an A even if you can’t solve any of the problems? Does the last sentence imply that all Americans have the same dream or might some of them have different dreams? Some uncertainty is tolerable in normal conversation. But when we need to for mulate ideas precisely —as in mathematics and programming —the ambiguities inherent in everyday language can be a real problem. We can’t hope to make an exact argument if we’re not sure exactly what the statements mean. So before we start into mathematics, we need to investigate the problem of how to talk about mathematics. To get around the ambiguity of English, mathematicians have devised a spe cial mini-language for talking about logical relationships. This language mostly uses ordinary English words and phrases such as “or”, “implies”, and “for all”. But mathematicians endow these words with definitions more precise than those found in an ordinary dictionary. Without knowing these definitions, you might sometimes get the gist of statements in this language, but you would regularly get misled about what they really meant.

37

38 CHAPTER 3. PROPOSITIONAL FORMULAS

Surprisingly, in the midst of learning the language of logic, we’ll come across the most important open problem in computer science —a problem whose solution could change the world.

3.1 Propositions from Propositions

In English, we can modify, combine, and relate propositions with words such as “not”, “and”, “or”, “implies”, and “if-then”. For example, we can combine three propositions into one like this:

If all humans are mortal and all Greeks are human, then all Greeks are mortal.

For the next while, we won’t be much concerned with the internals of propo sitions —whether they involve mathematics or Greek mortality —but rather with how propositions are combined and related. So we’ll frequently use variables such as P and Q in place of specific propositions such as “All humans are mortal” and “2 + 3 = 5”. The understanding is that these variables, like propositions, can take on only the values T (true) and F (false). Such true/false variables are sometimes called Boolean variables after their inventor, George —you guessed it —Boole.

3.1.1 “Not”, “And”, and “Or”

We can precisely define these special words using truth tables. For example, if P denotes an arbitrary proposition, then the truth of the proposition “ NOT P ” is defined by the following truth table:

P NOT P T F F T

The first row of the table indicates that when proposition P is true, the proposition “ NOT P ” is false. The second line indicates that when P is false, “ NOT P ” is true. This is probably what you would expect. In general, a truth table indicates the true/false value of a proposition for each possible setting of the variables. For example, the truth table for the proposition “P AND Q” has four lines, since the two variables can be set in four different ways:

P Q T T T T F F F T F F F F

P AND Q

According to this table, the proposition “P AND Q” is true only when P and Q are both true. This is probably the way you think about the word “and.”

40 CHAPTER 3. PROPOSITIONAL FORMULAS

Don’t take this as an insult; we just need to figure out whether this proposition is true or false. Curiously, the answer has nothing to do with whether or not you can understand the Chebyshev bound. Pigs do not fly, so we’re on either line (ft) or line (ff) of the truth table. In both cases, the proposition is true! In contrast, here’s an example of a false implication:

“If the moon shines white, then the moon is made of white cheddar.”

Yes, the moon shines white. But, no, the moon is not made of white cheddar cheese. So we’re on line (tf) of the truth table, and the proposition is false. The truth table for implications can be summarized in words as follows:

An implication is true exactly when the if-part is false or the then-part is true.

This sentence is worth remembering; a large fraction of all mathematical state ments are of the if-then form!

3.1.3 “If and Only If”

Mathematicians commonly join propositions in one additional way that doesn’t arise in ordinary speech. The proposition “P if and only if Q” asserts that P and Q are logically equivalent; that is, either both are true or both are false.

P Q T T T T F F F T F F F T

P IFF Q

The following if-and-only-if statement is true for every real number x:

x^2 − 4 ≥ 0 iff | | ≥x 2

For some values of x, both inequalities are true. For other values of x, neither in equality is true. In every case, however, the proposition as a whole is true.

3.1.4 Problems

Class Problems

Problem 3.1. When the mathematician says to his student, “If a function is not continuous, then it is not differentiable,” then letting D stand for “differentiable” and C for contin uous, the only proper translation of the mathematician’s statement would be

NOT(C) IMPLIES NOT(D),

or equivalently, D IMPLIES C.

3.2. PROPOSITIONAL LOGIC IN COMPUTER PROGRAMS 41

But when a mother says to her son, “If you don’t do your homework, then you can’t watch TV,” then letting T stand for “watch TV” and H for “do your homework,” a reasonable translation of the mother’s statement would be

NOT(H) IFF NOT(T ),

or equivalently, H IFF T. Explain why it is reasonable to translate these two IF-THEN statements in dif ferent ways into propositional formulas.

Problem 3.2. Prove by truth table that OR distributes over AND :

[P OR (Q AND R)] is equivalent to [(P OR Q) AND (P OR R)] (3.1)

Homework Problems

Problem 3.3. Describe a simple recursive procedure which, given a positive integer argument, n, produces a truth table whose rows are all the assignments of truth values to n propositional variables. For example, for n = 2, the table might look like:

T T T F F T F F Your description can be in English, or a simple program in some familiar lan guage (say Scheme or Java), but if you do write a program, be sure to include some sample output.

3.2 Propositional Logic in Computer Programs

Propositions and logical connectives arise all the time in computer programs. For example, consider the following snippet, which could be either C, C++, or Java:

if ( x > 0 || (x <= 0 && y > 100) ) .. . (further instructions)

The symbol || denotes “or”, and the symbol && denotes “and”. The further in structions are carried out only if the proposition following the word if is true. On closer inspection, this big expression is built from two simpler propositions. Let A

3.2. PROPOSITIONAL LOGIC IN COMPUTER PROGRAMS 43

English Cryptic Notation not P ¬P (alternatively, P ) P and Q P ∧ Q P or Q P ∨ Q P implies Q P −→ Q if P then Q P −→ Q P iff Q P ←→Q

For example, using this notation, “If P and not Q, then R” would be written:

(P ∧ Q) −→ R

This symbolic language is helpful for writing complicated logical expressions compactly. But words such as “ OR ” and “ IMPLIES ,” generally serve just as well as the cryptic symbols ∨ and −→, and their meaning is easy to remember. So we’ll use the cryptic notation sparingly, and we advise you to do the same.

3.2.2 Logically Equivalent Implications

Do these two sentences say the same thing?

If I am hungry, then I am grumpy. If I am not grumpy, then I am not hungry.

We can settle the issue by recasting both sentences in terms of propositional logic. Let P be the proposition “I am hungry”, and let Q be “I am grumpy”. The first sentence says “P implies Q” and the second says “(not Q) implies (not P )”. We can compare these two statements in a truth table:

P

T T T T

T F F F

F T T T

F F T T

Q P IMPLIES Q Q IMPLIES P

Sure enough, the columns of truth values under these two statements are the same, which precisely means they are equivalent. In general, “( NOT Q) IMPLIES (NOT P )” is called the contrapositive of the implication “P IMPLIES Q.” And, as we’ve just shown, the two are just different ways of saying the same thing. In contrast, the converse of “P IMPLIES Q” is the statement “Q IMPLIES P ”. In terms of our example, the converse is:

If I am grumpy, then I am hungry.

44 CHAPTER 3. PROPOSITIONAL FORMULAS

This sounds like a rather different contention, and a truth table confirms this sus picion: P T T F F

Q

T T T

F F T

T T F

F T T

P IMPLIES Q Q IMPLIES P

Thus, an implication is logically equivalent to its contrapositive but is not equiva lent to its converse. One final relationship: an implication and its converse together are equivalent to an iff statement, specifically, to these two statements together. For example,

If I am grumpy, then I am hungry. If I am hungry, then I am grumpy.

are equivalent to the single statement:

I am grumpy iff I am hungry.

Once again, we can verify this with a truth table:

P Q (P IMPLIES Q) AND (Q IMPLIES P ) T T T T T T F F F T F T T F F F F T T T

Q IFF P

T

F

F

T

The underlined operators have the same column of truth values, proving that the corresponding formulas are equivalent.

46 CHAPTER 3. PROPOSITIONAL FORMULAS

3.2.3 Problems

Class Problems

Problem 3.4. This problem^1 examines whether the following specifications are satisfiable:

  1. If the file system is not locked, then

(a) new messages will be queued. (b) new messages will be sent to the messages buffer. (c) the system is functioning normally, and conversely, if the system is func tioning normally, then the file system is not locked.

  1. If new messages are not queued, then they will be sent to the messages buffer.
  2. New messages will not be sent to the message buffer.

(a) Begin by translating the five specifications into propositional formulas using four propositional variables:

L ::= file system locked, Q ::= new messages are queued, B ::= new messages are sent to the message buffer, N ::= system functioning normally.

(b) Demonstrate that this set of specifications is satisfiable by describing a single truth assignment for the variables L, Q, B, N and verifying that under this assign ment, all the specifications are true.

(c) Argue that the assignment determined in part (b) is the only one that does the job.

Problem 3.5. Propositional logic comes up in digital circuit design using the convention that T corresponds to 1 and F to 0. A simple example is a 2-bit half-adder circuit. This circuit has 3 binary inputs, a 1 , a 0 and b, and 3 binary outputs, c, o 1 , o 0. The 2-bit word a 1 a 0 gives the binary representation of an integer, k, between 0 and 3. The 3-bit word cs 1 s 0 gives the binary representation of k + b. The third output bit, c, is called the final carry bit. So if k and b were both 1, then the value of a 1 a 0 would be 01 and the value of the output cs 1 s 0 would 010 , namely, the 3-bit binary representation of 1 + 1.

(^1) From Rosen, 5th edition, Exercise 1.1.

3.2. PROPOSITIONAL LOGIC IN COMPUTER PROGRAMS 47

In fact, the final carry bit equals 1 only when all three binary inputs are 1, that is, when k = 3 and b = 1. In that case, the value of cs 1 s 0 is 100 , namely, the binary representation of 3 + 1. This 2-bit half-adder could be described by the following formulas:

c 0 = b s 0 = a 0 XOR c 0 c 1 = a 0 AND c 0 the carry into column 1 s 1 = a 1 XOR c 1 c 2 = a 1 AND c 1 the carry into column 2 c = c 2.

(a) Generalize the above construction of a 2-bit half-adder to an n + 1 bit half- adder with inputs an,... , a 1 , a 0 and b for arbitrary n ≥ 0. That is, give simple formulas for si and ci for 0 ≤ i ≤ n + 1, where ci is the carry into column i and c = cn+1.

(b) Write similar definitions for the digits and carries in the sum of two n + 1-bit binary numbers an... a 1 a 0 and bn... b 1 b 0.

Visualized as digital circuits, the above adders consist of a sequence of single- digit half-adders or adders strung together in series. These circuits mimic ordinary pencil-and-paper addition, where a carry into a column is calculated directly from the carry into the previous column, and the carries have to ripple across all the columns before the carry into the final column is determined. Circuits with this design are called ripple-carry adders. Ripple-carry adders are easy to understand and remember and require a nearly minimal number of operations. But the higher- order output bits and the final carry take time proportional to n to reach their final values.

(c) How many of each of the propositional operations does your adder from part (b) use to calculate the sum?

Problem 3.6. (a) A propositional formula is valid iff it is equivalent to T. Verify by truth table that (P IMPLIES Q) OR (Q IMPLIES P )

is valid.

(b) Let P and Q be propositional formulas. Describe a single propositional for mula, R, involving P and Q such that R is valid iff P and Q are equivalent.

(c) A propositional formula is satisfiable iff there is an assignment of truth values to its variables —an environment —which makes it true. Explain why

P is valid iff NOT(P ) is not satisfiable.

3.2. PROPOSITIONAL LOGIC IN COMPUTER PROGRAMS 49

a

a

a

n

a

2n+

a

n+

a

n+

c

c

(n+1)

‐bit

add

module

(n+1)

‐bit

add d l

module

module

r

r

rn

c

2(n+2)

‐bit

add

module

p

n

p

p

p

2n+

p

n+

p

n+

n

n+

n+

Figure 3.1: Structure of a Double-size Add1 Module.

50 CHAPTER 3. PROPOSITIONAL FORMULAS

Chapter 5

First-Order Logic

5.1 Quantifiers

There are a couple of assertions commonly made about a predicate: that it is some times true and that it is always true. For example, the predicate

“x^2 ≥ 0 ”

is always true when x is a real number. On the other hand, the predicate

“ 5 x^2 − 7 = 0”

is only sometimes true; specifically, when x = ± 7 / 5. There are several ways to express the notions of “always true” and “sometimes true” in English. The table below gives some general formats on the left and spe cific examples using those formats on the right. You can expect to see such phrases hundreds of times in mathematical writing!

Always True For all n, P (n) is true. For all x ∈ R, x^2 ≥ 0. P (n) is true for every n. x^2 ≥ 0 for every x ∈ R.

Sometimes True There exists an n such that P (n) is true. There exists an x ∈ R such that 5 x^2 − 7 = 0. P (n) is true for some n. 5 x^2 − 7 = 0 for some x ∈ R. P (n) is true for at least one n. 5 x^2 − 7 = 0 for at least one x ∈ R.

All these sentences quantify how often the predicate is true. Specifically, an assertion that a predicate is always true is called a universal quantification, and an assertion that a predicate is sometimes true is an existential quantification. Some times the English sentences are unclear with respect to quantification:

73

74 CHAPTER 5. FIRST-ORDER LOGIC

“If you can solve any problem we come up with, then you get an A for the course.”

The phrase “you can solve any problem we can come up with” could reasonably be interpreted as either a universal or existential quantification:

“you can solve every problem we come up with,”

or maybe

“you can solve at least one problem we come up with.”

In any case, notice that this quantified phrase appears inside a larger if-then state ment. This is quite normal; quantified statements are themselves propositions and can be combined with and, or, implies, etc., just like any other proposition.

5.1.1 More Cryptic Notation

There are symbols to represent universal and existential quantification, just as there are symbols for “and” (∧), “implies” (−→), and so forth. In particular, to say that a predicate, P , is true for all values of x in some set, D, one writes:

∀x ∈ D. P (x)

The symbol ∀ is read “for all”, so this whole expression is read “for all x in D, P (x) is true”. To say that a predicate P (x) is true for at least one value of x in D, one writes:

∃x ∈ D. P (x)

The backward-E, ∃, is read “there exists”. So this expression would be read, “There exists an x in D such that P (x) is true.” The symbols ∀ and ∃ are always followed by a variable —usually with an indication of the set the variable ranges over —and then a predicate, as in the two examples above. As an example, let Probs be the set of problems we come up with, Solves(x) be the predicate “You can solve problem x”, and G be the proposition, “You get an A for the course.” Then the two different interpretations of

“If you can solve any problem we come up with, then you get an A for the course.”

can be written as follows:

(∀x ∈ Probs. Solves(x)) IMPLIES G,

or maybe

(∃x ∈ Probs. Solves(x)) IMPLIES G.

76 CHAPTER 5. FIRST-ORDER LOGIC

Variables Over One Domain

When all the variables in a formula are understood to take values from the same nonempty set, D, it’s conventional to omit mention of D. For example, instead of ∀x ∈ D ∃y ∈ D. Q(x, y) we’d write ∀x∃y. Q(x, y). The unnamed nonempty set that x and y range over is called the domain of discourse, or just plain domain, of the formula. It’s easy to arrange for all the variables to range over one domain. For exam ple, Goldbach’s Conjecture could be expressed with all variables ranging over the domain N as

∀n. n ∈ Evens IMPLIES (∃ p∃ q. p ∈ Primes ∧ q ∈ Primes ∧ n = p + q).

5.1.4 Negating Quantifiers

There is a simple relationship between the two kinds of quantifiers. The following two sentences mean the same thing:

It is not the case that everyone likes to snowboard. There exists someone who does not like to snowboard.

In terms of logic notation, this follows from a general property of predicate formu las: NOT ∀x. P (x) is equivalent to ∃x. NOT P (x).

Similarly, these sentences mean the same thing:

There does not exist anyone who likes skiing over magma. Everyone dislikes skiing over magma.

We can express the equivalence in logic notation this way:

(NOT ∃x. P (x)) IFF ∀x. NOT P (x). (5.1)

The general principle is that moving a “not” across a quantifier changes the kind of quantifier.

5.1.5 Validity

A propositional formula is called valid when it evaluates to T no matter what truth values are assigned to the individual propositional variables. For example, the propositional version of the Distributive Law is that P AND (Q OR R) is equivalent to (P AND Q) OR (P AND R). This is the same as saying that

[P AND (Q OR R)] IFF [(P AND Q) OR (P AND R)]

is valid.

5.1. QUANTIFIERS 77

The same idea extends to predicate formulas, but to be valid, a formula now must evaluate to true no matter what values its variables may take over any un specified domain, and no matter what interpretation a predicate variable may be given. For example, we already observed that the rule for negating a quantifier is captured by the valid assertion (5.1). Another useful example of a valid assertion is

∃x∀y. P (x, y) IMPLIES ∀y∃x. P (x, y). (5.2)

Here’s an explanation why this is valid:

Let D be the domain for the variables and P 0 be some binary predicate^1 on D. We need to show that if

∃x ∈ D ∀y ∈ D. P 0 (x, y)

holds under this interpretation, then so does

∀y ∈ D ∃x ∈ D. P 0 (x, y). (5.4)

So suppose (5.3) is true. Then by definition of ∃, this means that some element d 0 ∈ D has the property that

∀y ∈ D. P 0 (d 0 , y).

By definition of ∀, this means that

P 0 (d 0 , d)

is true for all d ∈ D. So given any d ∈ D, there is an element in D, namely, d 0 , such that P 0 (d 0 , d) is true. But that’s exactly what (5.4) means, so we’ve proved that (5.4) holds under this interpretation, as required.

We hope this is helpful as an explanation, but we don’t really want to call it a “proof.” The problem is that with something as basic as (5.2), it’s hard to see what more elementary axioms are ok to use in proving it. What the explanation above did was translate the logical formula (5.2) into English and then appeal to the meaning, in English, of “for all” and “there exists” as justification. So this wasn’t a proof, just an explanation that once you understand what (5.2) means, it becomes obvious. In contrast to (5.2), the formula

∀y∃x. P (x, y) IMPLIES ∃x∀y. P (x, y). (5.5)

is not valid. We can prove this just by describing an interpretation where the hy pothesis, ∀y∃x. P (x, y), is true but the conclusion, ∃x∀y. P (x, y), is not true. For

(^1) That is, a predicate that depends on two variables.