cs206 lecture 08 logic progammingcs206/lecs/lec08.pdfa logic program for plus is one that somehow...

24

Upload: others

Post on 06-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 1 of 100Go Back

Full ScreenCloseQuit

CS206 Lecture 08Logic Progamming

G. SivakumarComputer Science Department

IIT [email protected]

http://www.cse.iitb.ac.in/∼sivaThu, Jan 16, 2003

Plan for Lecture 8• First-Order Logic Examples• Introduction to Logic Programming• Prolog Vocabulary Syntax

Page 2: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 2 of 100Go Back

Full ScreenCloseQuit

Predicate Logic SemanticsFormal Treatment later in the course. First we will do the following.• Informal Treatment

� Translating from English to Logic� Constructing models for Formulae

• Two Applications of Logic� Logic Programming (2-3 lectures)� Equational Reasoning (2-3 lectures)

Page 3: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 3 of 100Go Back

Full ScreenCloseQuit

From English to LogicConsider the following statements.• Every student has a faculty advisor.• Every Indian Grandmaster has beaten some European Grandmaster.• There exist in�nitely many Pythagorean triples.• No two students got the same marks.

We have to choose• Suitable domains (numbers, people, strings, ...)• Suitable functions and predicates• Use correct connectives• Use correct quanti�ers

Page 4: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 4 of 100Go Back

Full ScreenCloseQuit

Possible Solutions• Every student has a faculty advisor.

∀x∃y(student(x) ∧ faculty(y) ∧ advisor(x, y))

• Every Indian Grandmaster has beaten some European Grandmaster.∀x∃y(gm(x) ∧ gm(y) ∧ indian(x)...)

• There exist in�nitely many Pythagorean triples.∀n∃i∃j∃k(i > n ∧ i2 = j2 + k2)

• No two students got the same marks.

Page 5: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 5 of 100Go Back

Full ScreenCloseQuit

From Logic Formula to

MeaningConsider the following formulae.• ∀x∃y(P (x) → Q(x, y))

• (∀x∃yP (x, y)) → (∀y∃xP (x, y))

Can we make them mean true (or false) by choosing suitable Interpreta-tions?Let us try domain of natural numbers.

Page 6: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 6 of 100Go Back

Full ScreenCloseQuit

Possible Interpretations∀x∃y(P (x) → Q(y))

can mean• trueDomain of x, y is Natural Numbers� P (x) means x is even� Q(x, y) means y > x and y is odd

• falseDomain of x, y is Natural Numbers� P (x) means x is prime� Q(x, y) means y divides x and 2 ≤ y ≤ (x− 1)

Page 7: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 7 of 100Go Back

Full ScreenCloseQuit

Logic Programming ParadigmConsider a simple function like addition (plus)Functional ViewBinary function� Two inputs and one output.Example:plus(2,3) �> 5,

plus(1,4) �> 5

Directionality is associated.Given the two inputs, a program can compute the output.

Page 8: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 8 of 100Go Back

Full ScreenCloseQuit

From Functions to RelationsSuppose we treat plus as a relation taking 3 arguments.That is, we say

plus(2,3,5) is true because 2 + 3 = 5.

plus(2,3,4) is false because 2 + 3 6= 4.and so on . . . .A Logic Program for plus is one that somehow expresses for any threearguments X, Y, Z the truth value of plus(X,Y,Z).Can be done using two clauses (facts and rules) as we will see later.

Page 9: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 9 of 100Go Back

Full ScreenCloseQuit

Querying a Logic ProgramNow we can ask the Logic program interpreter queries like:?- plus(2,3,6).

?- plus(1,4,3).

?- plus(1,1,2).

and it will correctly answer yes or no.Such queries which have no variables are called ground goals.They do not seem to achieve much as it can only verify answers.

Page 10: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 10 of 100Go Back

Full ScreenCloseQuit

Queries with VariablesQueries can also be of the form:?- plus(2,3,X).

?- plus(2,4,Y).

where uppercase letters denote variables.Now the interpreter will answerX = 5

for �rst query andY = 6

for second query.This seems to be doing some computing. (Variables in queries are exis-tentially quanti�ed and we are solving for suitable values.)But nothing new from imperative or functional programming yet.

Page 11: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 11 of 100Go Back

Full ScreenCloseQuit

No Directionality!But, what about goals like?- plus(X,2,6).

?- plus(2,Y,7).

or even?- plus(X,Y,5).

The interpreter will generate: X = 4 for �rst query, and Y = 5 for secondquery, and many answers for the third query including

<X = 1, Y = 4>,

<X = 2, Y = 3>,

...

Question: What about a goal like plus(X,Y,Z)?

Page 12: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 12 of 100Go Back

Full ScreenCloseQuit

History of Logic ProgrammingAbout 25 years old.In 1970s in Marseilles, France the �rst Prolog Interpreter was developed.Robert Kowalski's famous slogan

Algorithm = Logic + ControlFor a subset of First-order Logic (Horn clauses) which was powerful enoughto express most relations, the control to solve goals could be automatedand implemented e�ciently!

Page 13: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 13 of 100Go Back

Full ScreenCloseQuit

Successful Applications of

PrologArti�cial Intelligence: Rule-based Systems:

• Expert System Shells• British Nationality Act (used by UK immigration)Natural-Language Processing

Compilers: Prototyping and testing of compiler toolsDatabases: Deductive Data-bases (HornLog)Japanese Fifth Generation Project.

Page 14: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 14 of 100Go Back

Full ScreenCloseQuit

Drawbacks of Prolog• Speed• Programming in the Large (Modules)• Type systems and Type Checking• Debugging Tools• Need for Extra-Logical Features

More on these later in the course.

Page 15: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 15 of 100Go Back

Full ScreenCloseQuit

Prolog Programming ModelThe model for prolog programming is the following:

Program

|

+--V----------+

| Prolog |

Query -->| Interpreter |--> Answers

+-------------+

The Program is a set of logic formulae (axioms or de�nitions).The Query is a theorem to prove (with existentially quanti�ed variables).The Interpreter is a proof procedure that is sound and complete.

Page 16: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 16 of 100Go Back

Full ScreenCloseQuit

A Simple Prolog ProgramConsider the following domain:• States of India• Capital cities of each state• Rivers of India• States each river �ows through• Languages spoken in each state• Chief minister of each state

Page 17: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 17 of 100Go Back

Full ScreenCloseQuit

Representing simple

information (Facts)In Prolog we can write Facts (unit clauses) as follows.Suppose we wish to say that Jayalalitha is the chief minister of Tamil-

nadu.

We choose a predicate, say cm_of(X,Y) which means that �X is the chiefminister of Y� and write

cm_of(tn,jaya)

Similarly:cm_of(kerala,karunakaran)

cm_of(bengal,basu)

cm_of(bihar,laloo)

...

Page 18: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 18 of 100Go Back

Full ScreenCloseQuit

Example (Cont'd)Note that each fact that we have introduced has a meaning true. And wewrite down only true facts.Similarly we can introduce other predicates and build up our facts as below.

spoken_in(tn,tamil) flows_thru(tn,kaveri)

spoken_in(maha,marati) flows(bihar,ganga)

.... ....

capital_of(tn,madras)

capital_of(ap,hyderabad)

....

Page 19: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 19 of 100Go Back

Full ScreenCloseQuit

What can we do with only

Facts?Assume many such facts are already added to our Prolog Program.We can now ask the interpreter queries like-cm_of(bihar,siva)

Answer --> no.

flows_through(X,ganga)

Answer --> yes,

X = bihar ;

X = up ;

X = bengal

Note that for the second query, Prolog will generate di�erent values for X(all correct!) one by one when prompted.

Page 20: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 20 of 100Go Back

Full ScreenCloseQuit

RulesSeems trivial so far. Queries can be answered by just looking up facts.What about asking questions like:• what languages are spoken in the states through which Ganga runs?• what are the capital cities of the states through which Kaveri �ows?

From the given facts these can be answered provided Prolog is given thecorrect Rules to make these inferences.

Page 21: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 21 of 100Go Back

Full ScreenCloseQuit

How to write Rules in PrologA rule in Prolog consists of two parts:• A head (a unit clause)• The body (unit clauses separated by �,� and ended with �.�) connectedby �:-�.

For example:spoken_near(River, Lang) :-

flows_through(River, State),

spoken_in(State, Lang).

Note that River, Lang and State are variables here and not speci�cnames.One way to read this rule in English is as follows:

IF River �ows_through StateAND Lang is spoken in StateTHEN Lang is spoken near River.

Page 22: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 22 of 100Go Back

Full ScreenCloseQuit

Answering Queries using RulesConsider the query:spoken_near(ganga,Lang)

There is no fact that can answer this directly.We check for a rule with spoken_near as the head.There is one. It requires us to solve the subgoals:

flows_through(State,ganga),

spoken_in(State,Lang).

Page 23: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 23 of 100Go Back

Full ScreenCloseQuit

Solving SubgoalsWe try the �rst subgoal.

flows_through(State,ganga)

Answer --> State = bihar;

State = bengal;

...

For each answer we try the second subgoal:spoken_in(bihar, Lang)

Answer--> Lang = hindi;

...

spoken_in(bengal, Lang)

Answer--> Lang = bengali;

...

and generate answers for the original query.We will see later in what order goals are solved and answers generated.

Page 24: CS206 Lecture 08 Logic Progammingcs206/lecs/lec08.pdfA Logic Program for plus is one that somehow expresses for any three arguments X, Y, Z the truth value of plus(X,Y,Z) . Can be

Home PageTitle PageContents

JJ II

J I

Page 24 of 100Go Back

Full ScreenCloseQuit

Exercises• What are the capitals of the states through which a river runs?• What is the capital city of the state of some chief minister?• What languages are spoken in the state of some chief minister?

Also, what will happen if the two clauses in the body are interchanged?spoken_near(River, Lang) :-

spoken_in(State, Lang),

flows_through(River, State).