department of computer science - wordpress.com · – ∩conjunction (and) – ∪disjunction (or)...
TRANSCRIPT
Logic Programming
Budditha Hettige
Department of Computer Science
Introduction to Logic
3
Logic
• A formal system for describing knowledge
• Logic is a language
• Logic deals with
– Syntax
– Semantics
– Inference mechanism
Syllogisms
• The notation of syllogisms was introduced by
Aristotle
• Syllogisms are basic for defining classical logic
– All men are mortal.
– Socrates is a man.
– Then we conclude that, Socrates is mortal
7/11/2015 Budditha Hettige ([email protected]) 4
7/11/2015 Budditha Hettige ([email protected]) 5
History of Logic
• Philosophical Logic (500 BC to 19th Century)– Logic dealt with arguments in the natural
language used by humans.
– Example
• All men are mortal.
• Socrates is a man
• Therefore, Socrates is mortal.
• Symbolic Logic (Mid to late 19th Century)
• Mathematical Logic (Late 19th to mid 20th Century)
• Logic in Computer Science
Budditha Hettige ([email protected]) 6
7
Ontology/Epistemology
Language Ontology Epistemology
Propositional logic Facts T/F/Unknown
Predicate logic Facts, objects,
relations
T/F/unknown
Temporal Logic Facts, objects,
relations, time
T/F/unknown
Fuzzy logic Degree of truth Degree of belief
0….1
Propositional logic
9
Propositional logic
• Proposition is a statement (sentence) which is either true or false
• Propositional symbols such as P, Q are used to represent sentences
• The following logical connectives are used to combine propositions– ∩ conjunction (AND)
– ∪ disjunction (OR)
– ¬ not
– → implies
– ↔ equivalence or if and only if
Example
• P means “It is hot.”
• Q means “It is humid.”
• R means “It is raining.”
• (P Q) R
“If it is hot and humid, then it is raining”
• Q P
“If it is humid, then it is hot”
Budditha Hettige ([email protected]) 10
Example
• Examples of propositions:
– The Moon is made of green cheese.
– Trenton is the capital of New Jersey.
– Toronto is the capital of Canada.
– 1 + 0 = 1
– 0 + 0 = 2
• Examples that are not propositions.
– What time is it?
– x + 1 = 2
Source: (Richard Mayr University of Edinburgh, UK)
7/11/2015 Budditha Hettige ([email protected]) 11
Syntax
• Sequence of element of the vocabulary yields a formula
• The formula which abide the rules of propositional logic is called well formed formula (WFF)
– A symbol is a sentence
– If S is a sentence, then S is a sentence
– If S is a sentence, then (S) is a sentence
– If S and T are sentences, then
(S T), (S T), (S T), and (S ↔ T) are sentences
Budditha Hettige ([email protected]) 12
Logical connectives
• Disjunction
The disjunction of propositions p and q is denoted
by p ∨ q and has this truth table:
7/11/2015 Budditha Hettige ([email protected]) 13
logical connectives
• The Conjunction of propositions p and q is denoted
by p ∧ q and has this truth table:
7/11/2015 Budditha Hettige ([email protected]) 14
logical connectives
• Implication
– If p and q are propositions, then p → q is a
conditional statement or implication which is read
as “if p, then q” and has this truth table:
7/11/2015 Budditha Hettige ([email protected]) 15
Implication
• In p → q, p is the hypothesis (antecedent or
premise) and q is the conclusion (or consequence).
• Implication can be expressed by disjunction and
negation: p → q ≡ ¬p ∨ q
7/11/2015 Budditha Hettige ([email protected]) 16
Different Ways of Expressing p → q
• if p, then q
• p implies q
• if p, q
• p only if q
• q unless ¬p
• q when p
• q if p
• q whenever p
• p is sufficient for q
• q follows from p
• q is necessary for p a
necessary condition for p
is q
• a sufficient condition for
q is p
7/11/2015 Budditha Hettige ([email protected]) 17
Biconditional
• If p and q are propositions, then the biconditionalproposition p ↔ q has this truth table
• p ↔ q also reads as
– p if and only if q
– p iff q.
– p is necessary and sufficient for q
– if p then q, and conversely
– p implies q, and vice-versa
7/11/2015 Budditha Hettige ([email protected]) 18
Precedence of Logical Operators
1. ¬
2. ∧
3. ∨
4. →
5. ↔
• Thus p ∨ q → ¬r is equivalent to (p ∨ q) → ¬r.
• If the intended meaning is p ∨ (q → ¬r) then
parentheses must be used.
7/11/2015 Budditha Hettige ([email protected]) 19
Logical Equivalence
• Definition
– Two compound propositions p and q are logically equivalent if the columns in a truth table giving their truth values agree.
– This is written as p ≡ q.
• It is easy to show:
Factp ≡ q if and only if p ↔ q is a tautology.
7/11/2015 Budditha Hettige ([email protected]) 20
21
Propositional logic …
• Assignment: each raw of a truth table
• Interpretation: what each assignment yields
• Model: an assignment that makes a true interpretation
• Counter example: an assignment that makes a false interpretation
• Tautology an expression whose interpretation is always true Example: p ∨ ¬p.
• Contradiction: an expression whose interpretation is always false Example: p ∧ ¬p.
• Equivalence - Two expressions with same interpretations
De Morgan’s Laws
• ¬(p ∧ q) ≡ ¬p ∨ ¬q
¬(p ∨ q) ≡ ¬p ∧ ¬q
• Truth table proving De Morgan’s second law.
7/11/2015 Budditha Hettige ([email protected]) 22
Logical Equivalences
7/11/2015 Budditha Hettige ([email protected]) 23
24
Logical Equivalences contd.
• (P) P
• PQ QP
• P Q PQ
• (PQ) PQ
• (PQ) PQ
• P(QR) (PQ) (PR)
• P Q Q P
Logical Equivalences contd.
7/11/2015 Budditha Hettige ([email protected]) 25
Conditional Biconditional
A Proof in Propositional Logic
• To prove: ¬(p ∨ (¬p ∧ q)) ≡ ¬p ∧ ¬q
¬(p ∨ (¬p ∧ q))
≡ ¬p ∧ ¬(¬p ∧ q) by De Morgan’s 2nd law
≡ ¬p ∧ (¬(¬p) ∨ ¬q) by De Morgan’s first law
≡ ¬p ∧ (p ∨ ¬q) by the double negation law
≡ (¬p ∧ p) ∨ (¬p ∧ ¬q) by the 2nd distributive law
≡ F ∨ (¬p ∧ ¬q) because ¬p ∧ p ≡ F
≡ (¬p ∧ ¬q) ∨ F by commutativity of disj.
≡ ¬p ∧ ¬q by the identity law for F
7/11/2015 Budditha Hettige ([email protected]) 26
Conjunctive and Disjunctive
Normal Form
• A literal is either a propositional variable, or the negation of one.
– Examples: p, ¬p.
• A clause is a disjunction of literals.
– Example: p ∨ ¬q ∨ r.
• A formula in conjunctive normal form (CNF) is a conjunction of clauses.
– Example: (p ∨ ¬q ∨ r) ∧ (¬p ∨ ¬r)
• Disjunctive normal form (DNF) by swapping the words ‘conjunction’ and ‘disjunction’ in the definitions above.
– Example: (¬p ∧ q ∧ r) ∨ (¬q ∧ ¬r) ∨ (p ∧ r).
7/11/2015 Budditha Hettige ([email protected]) 27
Transformation into Conjunctive
Normal Form• Every propositional formula one can construct an equivalent
one in conjunctive normal form.
• Steps
1. Express all other operators by conjunction, disjunction
and negation.
2. Push negations inward by De Morgan’s laws and the
double negation law until negations appear only in
literals.
3. Use the commutative, associative and distributive laws
to obtain the correct form.
4. Simplify with domination, identity, idempotent, and
negation laws.
7/11/2015 Budditha Hettige ([email protected]) 28
Transformation into CNF
Transform the following formula into CNF.¬(p → q) ∨ (r → p)
Express implication by disjunction and negation.¬(¬p ∨ q) ∨ (¬r ∨ p)
Push negation inwards by De Morgan’s laws and double negation.
(p ∧ ¬q) ∨ (¬r ∨ p)Convert to CNF by associative and distributive laws.
(p ∨ ¬r ∨ p) ∧ (¬q ∨ ¬r ∨ p)
Optionally simplify by commutative and idempotent laws.(p ∨ ¬r) ∧ (¬q ∨ ¬r ∨ p)
and by commutative and absorbtion laws(p ∨ ¬r)
7/11/2015 Budditha Hettige ([email protected]) 29
Translating English Into Logic
The word “but” in English is often translated as ∧.
– Example: Today is is hot but it is not sunny.
– Because the second part of the sentence is a surprise, “but” is used instead of “and”.
– Example: Write each sentence in symbols, assigning propositional variables to statements as follows:
• P: It is hot.
• Q: It is sunny.
– It is not hot but it is sunny.
7/11/2015 Budditha Hettige ([email protected]) 30
Translating English Into Logic
• Example:
You can use the microlab only if you are a cs major or not a
freshman.
• P: You can use the microlab.
• Q: You are a cs major.
• R: You are a freshman.
– Rewrite the statement using logical connectives.
• Example:If it snows or rains today, I will not go for a walk.
– Rewrite this proposition using logical connectives and propositional variables
7/11/2015 Budditha Hettige ([email protected]) 31
Deriving the Semantic Values
• If Peter and Susan leave, I will be upset
– KEY: p = Peter leaves; q = Susan leaves; r = I will
be upset.
7/11/2015 Budditha Hettige ([email protected]) 32
Simplification
• Example: Simplify the formula
(P ∧ Q) ∨ ¬ (¬ P ∨ Q).
• Solution
7/11/2015 Budditha Hettige ([email protected]) 33
34
Propositional Logic..
• An argument consists of a set of propositions called premises and another proposition called conclusion as in the following statement
P1P2P3……Pn C
• If the above statement is a tautology, then the argument is called valid
Modus tollens
• The modus tollens rule can be stated formally as
• The argument has two premises.
– The first premise is a conditional or "if-then" statement, for example that if P then Q.
– The second premise is that it is not the case that Q .
– From these two premises, it can be logically concluded that it is not the case that P.
• Example
– If I am the god, then I can use super powers
– I cannot use super powers
– Therefore, I am not the god.
7/11/2015 Budditha Hettige ([email protected]) 35
36
Reasoning – Example
((P Q)QP R) R
LHS – ((P Q)QP R) (using modus tolens) P(P R) (P R)P (P R)P (using modus ponens) R (i.e. RHS)
• Note that this can also be shown by using truth tables
• Better way would be use CNF and method of contradiction
Method of contradiction
• is to assume that a statement is not true
• and then to show that that assumption leads to a
contradiction
• Example
– In the case of trying to prove P → Q
– this is equivalent to assuming that P ∩ ┐Q.
– That is, to assume that P is true and Q is false
• A good example of this is by proving that 2 is
irrational
7/11/2015 Budditha Hettige ([email protected]) 37
In the case of trying to prove this is equivalent to assuming that That is, to assume that is true and is false.
Example
7/11/2015 Budditha Hettige ([email protected]) 38
A good example of this is by proving that is irrationalThen , where and are relatively prime integers
39
Conjunctive normal forms
• We can express propositional logic formulae as conjunct of disjunction (CNF)
• Procedure– Eliminate , replacing PQ with
(P Q)(Q P)
– Eliminate , replacing P Q with PQ
– Push negation in using• (P) P, (PQ) PQ, (PQ) PQ
– Distribute over (break at sign)
• With the use of CNF proof can be done by using the method of contradiction
40
Example
• Consider the paragraph:If Sarath works hard and or lucky he can pass the exam. Sarath is curious but he is not lucky. If sarath is curious then he works hard.
• From this paragraph, check whether sarath can pass the exam.
• Let P- sarath works hard,
Q- Luck
S- sarath is curious
R- pass exam
41
Example…Then the paragraph can be written in propositional logic as follows
(PQ) R ----(a)
SQ ----(b)
S P ----(c)Convert these into CNF
(a) Eliminate , we get (PQ)R
remove , then get (PQ)R
R(PQ)
(R P) (RQ)
break at sign
(R P) ----(a1)
(RQ) ----(a2)
(b) Only the last step apply
S ----(b1)
Q ----(b2)
( c) elimination
SP ----(c1)
42
Example …..
Thus CNF are as follows(R P) ----(a1)
(RQ) ----(a2)
S ----(b1)
Q ----(b2)
SP ----(c1)
Suppose we want to show sarath can pass the exam.
For this purpose, assume its negation is true add it as a new CNF to
the system, i.e. R.
R ----(d1)
Then resolve the above and try to get a contradiction as follows
43
Example …R P R
P SP
S S
{ }
Therefore, we conclude that R is true
Limitations of Propositional logic
• Inability to generalize
• Inability to represent internal information
• Example: Suppose we have:
• “All men are mortal.”
• “Socrates is a man”.
• Does it follow that “Socrates is mortal” ?
– This cannot be expressed in propositional logic.
– We need a language to talk about objects, their properties and their relations.
7/11/2015 Budditha Hettige ([email protected]) 44
Questions
• With suitable example, define the following terms
– Tautology
– Contradiction
– Counter example
– Model assignment
• Using truth tables, determine whether the following
are Tautologies, Contradiction or neither.
7/11/2015 Budditha Hettige ([email protected]) 45