department of computer science - wordpress.com · – ∩conjunction (and) – ∪disjunction (or)...

45
Logic Programming Budditha Hettige Department of Computer Science

Upload: nguyenkhanh

Post on 08-Jun-2019

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Logic Programming

Budditha Hettige

Department of Computer Science

Page 2: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Introduction to Logic

Page 3: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

3

Logic

• A formal system for describing knowledge

• Logic is a language

• Logic deals with

– Syntax

– Semantics

– Inference mechanism

Page 4: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 5: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

7/11/2015 Budditha Hettige ([email protected]) 5

Page 6: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 7: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 8: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Propositional logic

Page 9: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 10: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 11: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 12: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 13: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 14: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 15: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 16: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 17: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 18: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 19: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 20: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 21: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 22: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 23: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Logical Equivalences

7/11/2015 Budditha Hettige ([email protected]) 23

Page 24: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

24

Logical Equivalences contd.

• (P) P

• PQ QP

• P Q PQ

• (PQ) PQ

• (PQ) PQ

• P(QR) (PQ) (PR)

• P Q Q P

Page 25: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Logical Equivalences contd.

7/11/2015 Budditha Hettige ([email protected]) 25

Conditional Biconditional

Page 26: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 27: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 28: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 29: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 30: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 31: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 32: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 33: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

Simplification

• Example: Simplify the formula

(P ∧ Q) ∨ ¬ (¬ P ∨ Q).

• Solution

7/11/2015 Budditha Hettige ([email protected]) 33

Page 34: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 35: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 36: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 37: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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.

Page 38: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 39: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 40: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 41: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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)

Page 42: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 43: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

43

Example …R P R

P SP

S S

{ }

Therefore, we conclude that R is true

Page 44: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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

Page 45: Department of Computer Science - WordPress.com · – ∩conjunction (AND) – ∪disjunction (OR) – ¬not – →implies – ↔equivalence or if and only if. Example • P means

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