350 likes | 490 Views
CS1022 Computer Programming & Principles. Lecture 2 Logic and Proof. Plan of lecture. Predicate logic Relations between quantifiers Proof. Introduction. Propositional Logic focussed on the syntax and semantics of the logical connectives Looked at some equivalences
E N D
CS1022Computer Programming & Principles Lecture 2 Logic and Proof
Plan of lecture • Predicate logic • Relations between quantifiers • Proof CS1022
Introduction • Propositional Logic focussed on the syntax and semantics of the logical connectives • Looked at some equivalences • Propositional Logic is abstract and limited • Predicate Logic less abstract, more expressive, and more like natural language • After Predicate Logic, will discuss proof in Propositional Logic CS1022
Propositional Logic has Limitations • Examples: • P stands for “a car is red” • Q stands for “a book is red” • Every new red object would need a different proposition, e.g. “a cat is red” • There is no “formal connection” between propositions • Similarly, “a cat is fat” and “a cat is striped” • Similarly, “Bill loves Jill”, “Will loves Jill”, “Jill loves Phil” • What about representing quantifiers? “Someone loves Jill” and “Every cat is red” • Generally, articulating the logic to reason about more sorts of sentences CS1022
Introduce Elements of the Language • Introduce predicates (one and two place) • Introduce entities and variables • Introduce quantifiers • Predicates allow us to parameterise statements: • R(x) stands for “x bears/has property R” • In our example, if we assume R is “red” then • R(car) captures “the car is red” • R(book) captures “the book is red” • N.B.: truth of a predicate depends on its parameters • Parameters (variables) allows us to use quantifiers • Some objects are red • All objects are red CS1022
FOL Syntax • Predicate logic or First-order logic • Syntax: • Capital letters P, Q, R, S, ... are predicate symbols • Predicate symbols have a number of parameters (“arity”) • May use more “informative” symbols such as “Red” or “Blue” • An atomic formula is • A predicate symbol • Followed by a “(“ • Followed by 1 or more parameters separated by “,” • Followed by a “)” • Examples: P(x) Q(x, y) Father(andrew, bill) Likes(john,x) Attention - Number of parameters must match the "arity" of the predicate. CS1022
Parameters and Connectives • Notice that parameters can be • Variables x, y, z, ... or • Constants a, b, bill, john, 2, 34, and so on • Examples: Lives(elizabeth, x) Even(2) Divisible(10,2) Prime(8) • Connectives and, or, not, can be used too • Examples: • not(P(x) and Q(x, y)) not(P(a)) • Q(x,a) or (not(Q (x, b))) • Attention! Look at brackets, look at what is connected to what.... Constants are fixed inmeaning. CS1022
Truth and Parameters • When is a Predicate Logic statement true (false)? • Informally, when the predicate and its parameters correspond to what holds in the "real world" or in the "model" • Model: Bill loves Jill, The car is red. • Logical Formulation: Loves(bill, jill), Black(car) CS1022
Quantifiers • Quantifiers, formally (Attention to different xs) • xP(x)– “for all values of x, P holds” • x Q(x)– “for some values of x, Q holds” • We can add quantifiers to any formula • If is a predicate formula then x() is a formula • If is a predicate formula then x() is a formula • Examples: • x (P(x) and Q(x, x)) • x (Q(x) Q(y)) • x (y (R(x, y))) • x (R(bill, jill))) CS1022
Quantifiers Evaluated • Quantifierscalculation. When is a quantified expression true? • xP(x)– “for all values x can have wrt domain, P holds” • x Q(x)– “for some value x can have wrtdomain, Q holds” • Domain of Discourse (stuff we talk about): Phil, Bill, Will, Jill • Model: Loves(bill, jill), Loves(will, jill), and Loves(jill,phil) • Everyone loves Jill? xLoves(x,jill). For every thing in the domain, it loves Jill. • Jill loves someone? xLoves(jill, x). For some thing in the domain, Jill loves it. CS1022
Quantifier Interactions • Nesting of quantifiers is allowed • x (y (R(x, y))) • Not all variables may have quantifiers • x (Q(x) Q(y)) (y is not bound. Like a pronoun) • Scope of quantifier • Quantifier “binds” to nearest formula • “x (P(x)) andQ(x)”(x of Q(x) is not bound to the quantifier; it is free) • “Clash” of variable names • (xP(x)) (x Q(x)) (xof P(x) and xof Q(x) distinct) • Renaming clarifies dependency: (xP(x)) (y Q(y)) CS1022
Relation between Quantifiers • Quantifiers are dual of one another • Let be a formula; the following holds not(x()) x(not()) not(x()) x(not()) • Nested quantifiers can be handled similarly: not(x(y (P(x,y)))) x(not(y (P(x,y)))) x(not(y (P(x,y)))) x(y(not(P(x,y)))) CS1022
Reading (meaning of) Predicate Logic If Loves(x,y) stands for “x loves y” • The meaning of x (Loves(x,x))) is • For all x, Loves(x,x) holds • “Everyone loves him/herself” • The meaning of x (y (Loves(x,y))) is • For all x, there is a y for which Loves(x,y) holds • “Everyone loves someone” • The meaning of y(x(not (Loves(x,y))) is • There is a y such that for all x, Loves(x,y) does not hold • “There is someone whom no-one loves” ??NL and logic correlated?? CS1022
Writing (meaning of) Predicate Logic If Loves(x,y) stands for “x loves y” • Write a formula for “someone is loved by everyone” y(x(Loves(x,y)) • Write a formula for “no-one is loved by everyone” • It is not true that there is someone who is loved by everyone not(y(x(Loves(x,y)))) y(not(x(Loves(x,y)))) y(x(not(Loves(x,y)))) CS1022
Summary About Logic You should now know: • Why logic is important to computing • Propositional logic: syntax and meaning • How to write truth tables • Predicate logic: syntax and meaning • Attention: translating from natural language to logical formalisations is still an "art form", prone to error, and an active research area. CS1022
Proof Section • Motivation: why proof is important to computing • Methods of proof • Return to Propositional Logic for this. Complications working with Predicate Logic. CS1022
Why proof is important to Computing • Suppose you write a specification of the knowledge of some domain, e.g. who loves whom, who makes whom happy, rules about happiness, and rules about what follows from being happy. • We must demonstrate that our specification does not give rise to contradiction (someone loves and does not loves Jill) • We must demonstrate that our specification does not draw the wrong inferences • We must demonstrate that what we claim holds in the specification does hold. • The demonstration should be given as a proof, which is a systematic way to reason about the specification. • Not yet about programs (about actions), but getting there. CS1022
Basic idea • Deducing statements relative to assumptions and rules • Start with some logical formulas that you want to use in your proof (assumptions) • Identify what you want to prove (a conclusion) • Look at your rules • Use the templates for reasoning and the equivalences to transform formulas from your start formulas till you get what you want to prove. Logical steps • Skill in knowing the templates and equivalences • Skill in strategy (what templates and equivalences to use when) • Symbolic computing. Same idea as what you may have done with transformations of expressions with numbers or in geometry. CS1022
Simple example • Assume: iftoday is Friday thenI am wearing a green shirt • Assume: Today is Friday • Infer: I am wearing a green shirt • If P, then Q • P • Therefore, Q • P -> Q • P • Therefore, Q This is a reasoning template called: Modus Ponens Latin for "mode that affirms" CS1022
Other examples • Assume: Today is Friday andI am wearing a green shirt • Infer: I am wearing a green shirt • P and Q; Therefore, Q • Assume: Today is Friday orI am wearing a green shirt • Assume: I am not wearing a green shirt • Infer: Today is Friday • P or Q, Not Q; Therefore, P CS1022
A variety of reasoning templates • Double negative elimination • From ¬ ¬ φ, we infer φ • Conjunction introduction • From φand ψ, we infer ( φ∧ψ ). • Conjunction elimination • From ( φ∧ψ ), we infer φ and ψ • Disjunction introduction • From φ, we infer (φ∨ψ) and (ψ∨φ). Check your intuitions CS1022
A variety of reasoning templates • Disjunction elimination • From ¬ φ and (φ∨ψ), we infer ψ. • Modus ponens • From φand ( φψ ), we infer ψ. • Modus tollens (denies by denying) • From ¬ ψand ( φψ ), we infer ¬ φ. • Others CS1022
How to prove • Write down premises. • Apply natural deduction rule to one (or more) premise. • Write down the result of applying the rule to the premise(s). Make a note of what rule is applied and what premises are used. • Reapply 2 and 3 until the result in 3 is the intended conclusion. CS1022
Sample proof Problem: Prove that p implies p V q • p Premise • p V q Disjunction Introduction on 1 Conclusion Easy and obvious CS1022
Sample proof Problem: Prove that p and p V q s imply s • p Premise • p V q Disjunction Introduction on 1 • p V q s Premise • s Modus Ponens on 2 and 3 Conclusion Still pretty easy, but not so obvious Have to start to think about what rules to apply that will eventually yield your conclusion The longer the chains of reasoning, the trickier this can be CS1022
Sample proof (1) It is not sunny this afternoon (¬ p) and it is colder than yesterday (q). (2) We will go swimming (r) only if it is sunny (p). (3) If we do not go swimming (¬ r), then we will take a canoe trip (s). (4) If we take a canoe trip (s), then we will be home by sunset (t). Therefore, we will be home by sunset (t). (1) ¬ p ∧ q (2) r p (3) ¬ r s (4) s t Attention – tricky "only if". Why isn't (2) p r? "r only if p" says that r cannot be true when p is not true. The statement "r only if p" is false where r is true, but p is false. Where r is true, then so is p. CS1022
Sample proof • ¬ p ∧ q Premise • ¬ p Simplification using 1 • r p Premise • ¬ r Modus tollens using 2 and 3 • ¬ r s Premise • s Modus ponens using 4 and 5 • s t Premise • t Modus ponens using 6 and 7 Conclusion CS1022
Methods of proof (1) • Logical arguments as proofs of theorems • Common proof: establish the truth of (P Q) • If P is true then Q follows • Every situation in which P is true, Q is also true • There are standard methods of proof: • Direct argument • Contrapositive argument • Proof by contradiction CS1022
Direct Argument (1) • Assume P is true and show that Q is true • This rules out situations where P is true and Q is false • Remember truth table of (P Q) • “in all those situations where P is true, Q is also true” CS1022
Direct Argument (2) Example: show that if x and y are odd integers then xy is also an odd integer Solution • If x is an odd integer then it can be written as x = 2m + 1, for some integer m • Likewise, y = 2n + 1, for some integer n • Then xy = (2m + 1)(2n + 1) = 4mn + 2m + 2n + 1 = 2(2mn + m + n) + 1 = 2p + 1 • 2p + 1 is an odd integer CS1022
Contrapositive Argument (1) • Assume Q is false and show that P is false • It shows that ((notQ) (notP)) is true • This is the same as showing that (P Q) is true ((notQ) (notP)) (P Q) CS1022
Contrapositive Argument (2) Example: Let n be a positive integer. Using the contrapositive argument, prove that if n2 is odd, then n is odd. Solution • “n2 is odd” is P; “n is odd” is Q • notP(negation of “n2 is odd”) is “n2 is even” • not Q (negation of “n is odd”) is “n is even” • Prove “if not Q then notP”, that is, ((not Q) (notP)) or “if n is even then n2 is even” • Since n is even, then n = 2mfor some integer m • Then n2 = (2m)2 = 4m2 = 2(2m2) which is even CS1022
Proof by Contradiction (1) • Assume P is true and Q is false and derive a contradiction • This rules out the situation where P is true and Q is false which is the only case where (P Q) is false CS1022
Proof by Contradiction (2) Example: prove that if p2 is even (P), then p is even (Q) Solution • Assume P is true (p2 is even) and Q is false (p is not even) • If p is not even, then it is odd, that is, p = 2n + 1, and we have p2 = (2n + 1)2 = 4n2 + 4n + 1. • But 4n2 + 4n = 2 (2n2 + 2n) = 2m is even, so 4n2 + 4n + 1 is odd. • This means p2 is not even, that is, (not P) is true – a contradiction since we assumed P is true • Therefore our assumption that p is not even must be wrong, i.e. p is even CS1022
Further Reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 2) • Wikipedia article (http://en.wikipedia.org/wiki/Propositional_calculus) • Chapter of a book (http://www.cis.upenn.edu/~cis510/tcl/chap3.pdf) CS1022