logic and proof. logic and reasoning: to reason is to use logic. are there other forms of decision...

49
Logic and Proof

Upload: griselda-ella-barber

Post on 26-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Logic and Proof

Logic and Reasoning: To reason is to use logic.

Are there other forms of decision making?

We first study propositional logic – the truth or falsehood of declarative statements.

We follow with an intro to predicate logic – a predicate is a statement that contains a variable.

We will examine various methods of proof – direct argument, contrapositive argument and by counterexample.

Our last topic proof by induction.

Propositions and Logic: A proposition is a statement to which it is possible to assign a

truth value – true or false.

Not all statements are propositions:

We use capital letters to represent propositions:

New propositions are constructed from earlier propositions by using the logical operators and, or and not.

the earth is flat29 is a prime number

Don't stand on the chair.

P: the earth is flatQ: 29 is a prime number

not P: the earth is not flat(P or Q): the earth is flat or 29 is a prime number(P and Q): the earth is flat and 29 is a prime number

Exercise: Express the following in symbolic form:

Logic is fun and today is FridayToday is not Friday, nor is logic funEither logic is fun or today is Friday

P: Logic is funQ: Today is Friday

Logic is fun and today is Friday P and Q

Today is not Friday, nor is logic fun not Q and not P

Either logic is fun or today is Friday P or Q

Truth Tables: It is not easy to determine the truth of complicated, compound

propositions if we know the truth of their simpler components.

We use symbolic Truth Tables to evaluated the truthfulness of complex propositions:

We call (P and Q) a conjunction and (P or Q) a disjunction.

Suppose we know that “logic is fun” and “today is Friday” are both true.What can we say about: “either logic is not fun or today is not Friday”

P not P P Q P and Q P or QT F T T T TF T T F F T

F T F TF F F F

Exercise: Determine the truth value of “either the moon is made of green

cheese or Henry VIII had six wives, or it is not true that the dodo is extinct”.

P: the moon is made of green cheeseQ: Henry VIII had six wivesR: the dodo is extinct

Either the moon is made of green cheese or Henry VIII had six wives, or it is not true that the dodo is extinct (P or Q or not R)

P Q R not R P or Q (P or Q) or not RT T T F T TT T F T T TT F T F T TT F F T T TF T T F T TF T F T T TF F T F F FF F F T F T

Logical Equivalence Two compound propositions built from the same simple

propositions are logically equivalent if they have the same truth value for every possible combination of truth values of their simple components.

If the above is true we say that the two compound propositions “have the same Truth Table”.

Exercise: Show (not (P and (not Q))) is logically equivalent to

((not P) or Q).

We need to build a Truth Table that contains the columns P, Q, not P, not Q, P and (not Q) and finally (not (P and (not Q))) and ((not P) or Q).

For simplicity's sake lets call (not (P and (not Q))), R and ((not P) or Q), S.

P Q not P not Q P and ( not Q) R ST TT FF TF F

F F F T TF T T F FT F F T TT T F T T

Conclusion Since the last two columns are identical in the previous Truth

Table the two compound propositions are logically equivalent.

Conditional Proposition: One final logical operator needs to be introduced – the

conditional operator (P --> Q).

An example:

The above is true regardless of whether today is actually Sunday or not!

Such propositions lead us to distinguish between factual truth and logical truth.

If today is Sunday then tomorrow is Monday

P --> Q if P then Q if P is true then Q is true P implies Q

Example: P: 1 == 5; Q: 3 == 7.

We know both these things to be factually false. However P --> Q is true.

In other words, assuming true something that is factually false can allow you to prove the truth of something else that is also factually false.

P: 1 == 5; Q: 4 == 4.

We can also show a false statement can be used to prove a true statement

So implications are to some extent independent of their components

If 1 == 5 then 1+2 = 5+2; in other words 3 == 7.

If 1 == 5 then 1-3 = 5-3; in other words -2 == 2. Now square each side(-2)2 == 4 == (2)2 == 4.

Conditional Propositions (cont) We define the conditional proposition (P --> Q) to be false if P is

true and Q is false and to be true otherwise.

Its Truth Table is

Exercise: Complete the Truth Tables for the following:((not Q) --> (not P)), Q --> P and ((not P) --> (not Q)).

P Q P --> QT T TT F FF T TF F T

((not Q) --> (not P)) is called the contrapositive of P --> QQ --> P is called the converse of P --> Q((not P) --> (not Q)) is called the inverse of P --> Q

Answer

P Q not P not Q P --> Q not Q --> not P Q --> P not P --> not QT T F F T T T TT F F T F F T TF T T F T T F FF F T T T T T T

logically equivalent logically equivalent

Exercise: Show that P --> Q is logically equivalent to (not P) or Q.

Predicates and Quantifiers: A predicate is a sentence containing variables. The sentence is

either true or false depending on the values assigned to the variables in the sentence.

Example:

This sentence is true for x = 0 or 1 and false otherwise.

We can apply logical operations to predicates and create compound predicates just like we created compound propositions.

Predicates have additional operators called quantifiers -which, when applied to a predicate make a new predicate that is either true or false absolutely.

x is an integer satisfying x = x2A E

A

E

this symbol is read “for all”

this symbol is read “there exists”

Examples: All triangles have the sum of their angles equal to 180 degrees.

All cats have tails.

There exists an integer satisfying x2 = 2.

There is a prime number that is not odd.

x: x is a triangle and x has the sum of its angles equal to 180 degrees

A

x: x is a cat and x has a tail

A

x: x is an integer and x2 = 2.

E

x: x is an prime number and x is not odd

Etrue

false

NOTE 1: these statements are either absolutely true or absolutely false;

NOTE 2: and are not interchangeable

A E

x: x is an prime number and x is not odd

A

false

Exercise: Suppose P(x) is the predicate “x is an integer and x2 = 16”.

Express the predicate x: P(x) in words and determine its truth value.

Suppose P(x) is the predicate “x is a real number and x2 + 1 = 0”. Express the predicate x: P(x) in words and determine its truth value.

E

There is an x such that x is an integer and x2 = 16. x == -4 and x == 4 make this statement true.

E

There is an x such that x is a real number and x2 + 1 = 0. Since x2 >= 0, x2 + 1 >= 1 so it can not be 0 and thismakes this statement false.

Negations of Quantified Predicates. Suppose P(x) is the predicate “x is a real number and

x2 + 1 = 0”. The predicate x: P(x) in words is:

Hence its negation must be true:

In English this is ”there is no x, a real number such that x2 + 1 = 0”. In other words, “for all x, x a real number, x2 + 1 ≠ 0”. However we know how to express this symbolically

E

There is an x such that x is a real number and x2 + 1 = 0. Since x2 >= 0, x2 + 1 >= 1 so it can not be 0 and we know the statement is false.

not ( x: P(x))E

x: not P(x))A

General Equivalencies: The following are equivalent:

This last example shows up frequently in the database query language SQL:

is written

before being translated into SQL.

not ( x: P(x))E x: not (P(x))Aand

not ( x: P(x))A x: not (P(x))Eand

x: P(x)A not ( x: not P(x)))Eand

Find the suppliers who supply all red parts

Find the suppliers where there does not exist a red part that they do not supply

Exercise: Assume x and y are real numbers and P(x,y) denotes the

predicate x+ y = 0. Express the following in English and determine the truth value of each.

A

x ( y (P(x,y))

E

E

x ( y (P(x,y))

A

Predicate Logic and SQL In SQL we are often confronted with answering a query which

contains the word “all” in the condition.

Check out this link:

The query that interests us is:

Expressed in predicate logic this is:

http://www.cs.newpaltz.edu/~pletcha/SupplierPart.pdf

Find the Suppliers who supply all Red Parts.

S(x): x is a supplierP(y): y is a red partR(x,y): x supplies y

Find x such that S(x) and P(y) and y R(x,y)

Find x such that S(x) and P(y) and not y not R(x,y)

A

E

SQL

Select sname from Supplier s where not exists (select * from Part p where p.color = 'Red' and s.sno not in (select sno from Supplies ss where ss.pnp = p.pno))

Methods of Proof for (P --> Q): Standard Methods of Proof:

Direct Argument:

Contrapositive Argument:

Proof by Contradiction (Counter-example):

Assume P is true and try to prove Q is true.

Assume Q is not true and try to prove P is not true.

Assume P is true and Q is false and try to derive a contradiction. This is the only case where P--> Q is false.

Direct Argument: Use Direct Argument ot prove that if x and y are odd integers

then xy is an odd integer.

If x is odd, x = 2n + 1. If y is odd, y = 2m + 1.xy = (2n + 1)(2m + 1) = 4nm + 2n + 2m + 1 = 2(2nm + n + m) + 1and this latter number is odd. So xy is odd.

Prove by Direct Argument. The sum of two even integers is even as well.

Contrapositive: If n is a positive integer, prove by contraposition that if n2 is odd

then n is odd.

If n is a positive integer, prove by contraposition that if n2 is even then n is even.

Assume n is even (not odd) and prove n2 is even (not odd).

If n is even then n = 2a. Then n2 = (2a)(2a) = 4a2 and this numberis even.

Assume n is odd (not even) and prove n2 is odd (not even).

The previous example shows that the product of any twoodd integers is odd. In particular, if x = y = n is odd thenxy = n2 is odd.

Exercise: These two statements are {inverse/converse} to one another?

Prove the Following by Contraposition: If x and y are integers and x + y is even then either both x and y

are even or they are both odd.

Contradiction: Use contradiction to show that if x2 = 2 then x is not a fraction.

Suppose there exist a and b, integers such that x = a/b, a and b arereduced to lowest terms (is, gcd(a,b) = 1) and x2 = 2.

Then (a/b)2 = 2 so a2/b2 = 2 so a2 = 2b2.

This proves that a2 is even so by the previous example a is even.

Hence a = 2c and so a2 = (2c)(2c) = 4c2 and this we know to beequal to 2b2.

Since 4c2 = 2b2 we can conclude that 4c2/2 = 2b2/2, ie 2c2 = b2.

Using the same reasoning we used above we can conclude that b is even.Hence gcd(a,b) != 1 since 2 divides them both.

This is a contradiction of our original assumption so we can concludethat no such representation of x exists.

This is the proof of existence of irrational numbers.

Example: Prove by contradiction that there are no rational (fraction)

solutions for the equation

x3 + x + 1 = 0

Observation: A common knowledge in CS is that sometimes, when a problem

is very difficult to solve using known programming languages, the easiest thing to do is invent a new language and solve the problem that way.

The previous example shows that the Greeks used the same technique. The Greeks were confounded by the the length of the hypotenuse of the following triangle.

It was obviously real but they couldn't figure out what fraction it was (a/b). Someone eventually came along and in the language if algebra, proved it wasn't a fraction after all.

1

1

?

Mathematical Induction: “Proving” something by Mathematical Induction (MI) is a

misnomer if by proof you mean something like direct proof we saw earlier.

We do not prove something by MI; rather we do the following two things:

We prove the problem at hand has a certain structure. We declare the problem to be solved and the statement of

the problem to be true.

This is very similar to the separation of content from display structure that one sees when using style sheets (.css files).

When Can I Use Math Induction? Math Induction can be used to solve a problem if:

1)The problem has a size parameter. So there are different expressions of the problem, each expression a different size. Typically, problem of size 1, problem of size 2, ...

2)The initial or base problem (typically, the problem of size 1) is trivial to prove directly.

– Assuming the problem of size n-1 is true we can prove that the problem of size n is also true. This is for an arbitrary (not specified exactly) value of n.

Symbolically we show

A problem with this structure is said to be inductive.

If P is the problem we show P(1) is true P(n-1) --> P(n) is true for arbitrary n.

Exercise: Please read the following website.

Is Math Induction applicable to this problem? How? How not?

http://en.wikipedia.org/wiki/Turtles_all_the_way_down

Program Correctness: Testing a program does not prove it is correct. It only proves it

is correct with the data used to test it. It may fail with other data.

Program Proof of Correctness uses formal logic to prove program correctness. Specifically it proves that for known input the program will necessarily produce known (correct) output.

Math Induction is used in a limited sense to prove the correctness of algorithms that contain loops.

Example: Consider the following algorithm which is supposed to find the

maximum element in a list of n positive integers - a

1, a

2, a

3, ... , a

n.

begin i := 0; M := 0; while (i < n) do begin i := i + 1; M = max(M,a

i);

endend

Solution: Show the problem has a parameterized problem statement.

Prove P(1). This states that the above algorithm finds the maximum element of all lists of positive integers of size 1.

P: The previous algorithm finds the maximum element inany list of positive integers of size n, n = 1, 2, 3, ...

begin i := 0; M := 0; while (i < 1) do begin i := i + 1; M = max(M,a

i);

endend

In this case there is onlyone iteration of the loop.Since M == 0 initially and a

1 > 0, max(0,a

1) = a

1 and

so after one iteration M == a1.

Thus M is the maximum valuefound in the list so P(1) is true.

Solution (2): Prove that if P(n-1) is true we can show P(n) is true.

Suppose a1, a

2, a

3, ... , a

n is a list of n positive integers. After n-

1 iterations of the loop the algorithm will have processed a

1, a

2, a

3, ... , a

n-1 . This is a list of n-1 elements so we can

assume that at this point the value of M,say Mn-1

, is the largest value in this shortened list.

During the next (and last) iteration of the loop, the new value of M is M = max(M

n-1,a

n). If a

n is the largest element in the entire

list then the new value of M is an; otherwise M already has the

value of the largest element and its value won't change.

Hence applied to a list of size n, the algorithm assigns to M the value of the largest element in the list.

Conclusion: We have shown that the problem as stated is “inductive” and so

this algorithm assigns to M the value of the largest element in the list for any list of finite length.

Example 1: A convex polygon has 180(number_of_sides-2) degrees in its

interior angles.

P(n): A convex polygon with n sides has 180(n-2) degrees in its interior angles for n >= 3.

P(3): It is know that a convex polygon with 3 sides is a triangle and that a triangle has 180 = 180(3-2) degrees in its interior angles.

Suppose it is true that for any polygon with n-1 sides the number of degrees in its interior angles is 180((n-1)-2) = 180(n-3) degrees.

Let the following polygon have n sides.

Example 1 (cont):

Join v1 and v

3 by a straight line. This produces two new

polygons:

P(3) with vertices {v1, v

2, v

3} and its interior angles = 180.

P(n-1) with vertices {v1, v

3, ..., v

n} and its interior angle =

180(n-3)

v1

v2

v3

v4

v5

vn

P(n) is a ploygon with vertices {v

1, v

2, v

3, ... , v

n}, n > 3.

Example 1 (cont):

We now make the following observation

The sum of the interior angles of the large polygon P(n) is the sum of the angles of P(3) and p(n-1).

Hence assuming P(n-1) is true we have shown P(n) is true so the problem is inductive and true for all n.

v1

v2

v3

v4

v5

vn

P(n) is a ploygon with vertices {v

1, v

2, v

3, ... , v

n}, n > 3.

P(n) = P(3) + P(n-1) = 180 + 180(n-3) = 180(1 + n – 3) = 180(n - 2)

Example: Prove, by induction, that 7n -1 is divisible by 6 for n >= 1.

P(1): 71 – 1 = 6 is divisible by 6. This is true.

Assume P(n-1) is true – that 7n-1 -1 is divisible by 6

Consider 7n -1. If we prove this is divisible by 6 we have shown P(n) so we have shown the problem to be inductive.

But 7n – 1 = 7(7n-1) - 1 = 7(7n-1) – 7 + 7 - 1 = 7(7n-1 -1 ) + 7 – 1 = 7(7n-1 -1 ) + 6.

This last expression is divisible by 6 since both of its components; namely 6 and 7n-1 -1 are both divisible by 6.

Therefore P(n-1) true --> P(n) is true so by induction P(n) is true for all n >= 1.

Homework: Do all the exercises at the end of Chapter 2 and be prepared for

a quiz.

Algorithm Correctness (Reprise): To prove an algorithm is correct we must prove that what we

say about the algorithm is true before, during and after execution of the algorithm.

To prove it by induction we must be able to express what we claim in terms of a problem size. This is particularly easy if the algorithm involved is a loop.

If A is the algorithm, P is a precondition (what we know or think to be true just before the algorithm is used and Q is the post-condition (what we claim will be true just after the algorithm completes execution) then the statement

means that “if the execution of algorithm A starts with predicate P true, then it will terminate with predicate Q true”.

P is called a precondition and Q is called a post-condition.

{P} A {Q}

Example:

{n is a positive integer}begin sq := 0; for i:= 1 to n do sq := sq + 2*i -1;end{sq = n2}

P(n) is the predicate “sq = n2 after n executions of the loop”

Step 1: Show P(1) is true. P(1) states that sq = 1 after one iteration of the loop. After one iteration sq = 0 + 2*1 – 1 = 0 + 2 – 1 = 1 So P(1) is true.

Step 2: Assume P(k) is true and show P(k+1) is true for arbitrary k. Assume P(k), so that sq = k2 after k iterations of the loop. After the k+1st iteration the value of sq will be sq = k2 + 2*(k+1) -1 = k2 + 2k + 2 – 1 = k2 + 2k – 1 = (k + 1)2 So we have shown P(k) --> P(k+1) to be true.

Conclusion: P(n) is true for all positive integer values of n.

Exercise: Prove that BubbleSort actually sorts any list of n sortrable

items.procedure bubbleSort( A : list of sortable items ) defined as: n := length( A ) do swapped := false n := n - 1 for each i in 0 to n - 1 inclusive do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) swapped := true end if end for while swappedend procedure

P(n): BubbleSort sorts a list of n sortable items

Show P(1) is true: If the list contains only 1 item then we notice that the list is

already in sorted order so we only want to be sure that the algorithm does not modify the list in some way or does not crash.

If n = 1 then the for-loop goes from 0 to -1. We understand this to mean the for-loop does not execute. Since swapped is set to false and does not change; the while-loop only executes one iteration and in that iteration no change is made to the list so the list remains in sorted order.

Hence P(1) is true.

Assume P(k) is true and show P(k+1) is true. Suppose A is a list of k+1 sortable elements. The algorithm can

be rewritten as:

n = length(A)n = n – 1for each i in = to n – 1 inclusive do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) /////////////////////////n := length( A ) -1do swapped := false n := n - 1 for each i in 0 to n - 1 inclusive do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) swapped := true end if end forwhile swapped

Assume P(k) is true and show P(k+1) is true. The algorithm now has two parts; the second part is clearly P(k)

which we can assume is true.

The first part is a single iteration – the first – of the main while loop.

My claim – something that would need to be proved by a further induction – is that this first iteration leaves the original list modified in having, upon completion, its largest element in the highest index location.

So my claim, together with the inductive step allowing us to declare that the rest of the algorithm sorts the rest of the list lets us say that the entire list of k+1 items is sorted.

Exercise: Show the first iteration of the BubbleSort while-loop puts thelargest element of the list in the highest index location.