CS1022 Computer Programming & Principles Lecture 2 Logic and Proof
Jan 01, 2016
CS1022 Computer Programming &
Principles
Lecture 2Logic and Proof
Plan of lecture• Predicate logic• Relations between quantifiers• Proof
2CS1022
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
3CS1022
• 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
Propositional Logic has Limitations
4CS1022
• 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
Introduce Elements of the Language
5CS1022
• 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)
FOL Syntax
6CS1022
Attention - Number of parameters must match the "arity" of the predicate.
• 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....
Parameters and Connectives
7CS1022
Constants are fixed in meaning.
• 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)
Truth and Parameters
8CS1022
informal model talk, not natural language.
• Quantifiers, formally (Attention to different xs)– x P(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)))
Quantifiers
9CS1022
• Quantifiers calculation. When is a quantified expression true?– x P(x) – “for all values x can have wrt domain, P holds”– x Q(x) – “for some value x can have wrt domain, Q holds”
• Domain of Discourse (things we talk about): Phil, Bill, Will, Jill
• Model: Bill loves Jill, Will loves Jill, Jill loves Phil• Everyone loves Jill? x Loves(x,jill). For every thing in
the domain, it loves Jill.• Jill loves someone? x Loves(jill, x). For some thing in
the domain, Jill loves it.
Quantifiers Evaluated
10CS1022
• 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)) and Q(x)” (x of Q(x) is not bound to the
quantifier; it is free)• “Clash” of variable names– (x P(x)) (x Q(x)) (x of P(x) and x of Q(x) distinct) – Renaming clarifies dependency: (x P(x)) (y Q(y))
Quantifier Interactions
11CS1022
• Quantifiers are duals of one another• Let be a formula. The following holds:
not(x()) x(not())not(x ()) x(not())
• Proof using a model:Domain = Bill, JillModel = Bill is happy.It is not the case that everyone is happy ?=?
Someone is not happyIt is not the case that someone is happy ?=?
Everyone is not happy
Relation between Quantifiers
12CS1022
• 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))))
Relation between Quantifiers
13CS1022
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”
Reading (meaning of) Predicate Logic
14CS1022
??NL and logic correlated??
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))))
Writing (meaning of) Predicate Logic
15CS1022
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 an active research area. There are very powerful automated tools doing this lately:
http://gmb.let.rug.nl/explorer/explore.php
Summary About Logic
16CS1022
Proof Section• Motivation: why proof is important to computing• Methods of proof• Return to Propositional Logic for this.
Complications working with Predicate Logic.
17
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.18CS1022
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.
19CS1022
Simple example• Assume: if today is Friday then I 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
20CS1022
This is a reasoning template called:Modus PonensLatin for "mode that affirms"
Other examples• Assume: Today is Friday and I am wearing a green shirt• Infer: I am wearing a green shirt
• P and Q; Therefore, Q
• Assume: Today is Friday or I am wearing a green shirt• Assume: I am not wearing a green shirt• Infer: Today is Friday
• P or Q, Not Q; Therefore, P
21CS1022
A variety of reasoning templates
22CS1022
• 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
A variety of reasoning templates
23CS1022
• Disjunction elimination– From ¬ φ and (φ ψ), we infer ψ.∨
• Modus ponens– From φ and ( φ ψ ), we infer ψ.
• Modus tollens (denies by denying)– From ¬ ψ and ( φ ψ ), we infer ¬ φ.
• Others
How to prove
24CS1022
1. Write down premises.2. Apply natural deduction rule to one (or more)
premise.3. 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.
4. Reapply 2 and 3 until the result in 3 is the intended conclusion.
Sample proof
25CS1022
Problem: Prove that p implies p V q1. p Premise2. p V q Disjunction Introduction on 1
Conclusion
Easy and obvious
Sample proof
26CS1022
Problem: Prove that p and p V q s imply s1. p Premise2. p V q Disjunction Introduction on 13. p V q s Premise4. s Modus Ponens on 2 and 3
Conclusion
Still pretty easy, but not so obviousHave to start to think about what rules to apply that will eventually yield your conclusionThe longer the chains of reasoning, the trickier this can be
Sample proof
27CS1022
(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.
Sample proof
28CS1022
1. ¬ p q∧ Premise2. ¬ p Simplification using 13. r p Premise4. ¬ r Modus tollens using 2 and 35. ¬ r s Premise6. s Modus ponens using 4 and 57. s t Premise8. t Modus ponens using 6 and 7
Conclusion
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
29CS1022
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”
30CS1022
P Q (P Q)T T TT F FF T TF F T
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
Direct Argument (2)
31CS1022
Contrapositive Argument (1)• Assume Q is false and show that P is false– It shows that ((not Q) (not P)) is true– This is the same as showing that (P Q) is true
((not Q) (not P)) (P Q)
32CS1022
P Q not P not Q (not Q) (not P) P QT T F F T TT F F T F FF T T F T TF F T T T T
Example: Let n be a positive integer. Using the contrapositive argument, prove that if n2 is odd, then n is odd, e.g. 32 is odd, then 3 is odd.
Solution• “n2 is odd” is P; “n is odd” is Q– not P (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 not P”, that is, ((not Q) (not P)) or “if n is even then n2 is even”
• Since n is even, then n = 2m for some integer m• Then n2 = (2m)2 = 4m2 = 2(2m2) which is even
Contrapositive Argument (2)
33CS1022
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
34CS1022
P Q (P Q)T T TT F FF T TF F T
Example: prove that if p2 is even (Q), then p is even (S)Solution– Assume Q is true (p2 is even) and S 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
Proof by Contradiction (2)
35CS1022
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)
36CS1022