prolog programmingmhtay/itec480/lecture_prolog/chapter_1_1_2010.pdf · easy to query the prolog...
TRANSCRIPT
![Page 1: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/1.jpg)
1/18/16
1
Prolog Programming Dr. Maung M. Htay Department of Information Technology
![Page 2: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/2.jpg)
2
Text Prolog Programming for Artificial Intelligence by Ivan Bratko Third Edition, Addison Wesley
![Page 3: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/3.jpg)
3
References u Artificial Intelligence by George F Luger, Fourth
Edition, Addison-Wesley
u Prolog Help/References http://www.geocities.com/saviranid/
u Roman Barták, 1997 page http://ktiml.mff.cuni.cz/~bartak/prolog.old/intro.html
![Page 4: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/4.jpg)
4
What is Prolog? u Programming in Logic
u Developed in early 1970, by Robert Kowalski, Maarteen van Emden, and David D.H. Warren at Edinburgh, U.K., Alain Colmerauer at Marseilles
u For symbolic, non-numeric computation
u Suited for solving problems that involve objects and relations between objects
![Page 5: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/5.jpg)
5
Language Design u centered around a small set of basic mechanisms
u including pattern matching, tree-based data structuring and automatic backtracking
![Page 6: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/6.jpg)
6
Chapter 1: Introduction to Prolog u Defining relations by facts
u Defining relations by rules
u Recursive rules
u How Prolog answers questions
u Declarative and procedural meaning of programs
![Page 7: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/7.jpg)
7
1.1 Defining relations by facts u The fact that “Tom is a parent of Bob” can be
written in Prolog as:
u parent(tom,bob).
u parent is the relation
u tom and bob are its arguments
![Page 8: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/8.jpg)
8
A Family Tree
![Page 9: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/9.jpg)
9
Prolog Program for the previous family tree parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,pat). parent(bob,ann). parent(pat,jim).
![Page 10: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/10.jpg)
10
Clauses u A clause declares one fact about a relation
For example, u parent(tom,bob) is a particular instance of the
parent relation u an instance is also called a relationship u a relation is defined as the set of all its instances
![Page 11: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/11.jpg)
11
Question to Prolog
For example, u Is Bob a parent of Pat?
In Prolog, u ?- parent(bob,pat).
Prolog will answer: u yes
![Page 12: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/12.jpg)
12
More questions A further query can be:
u ?- parent(liz,pat).
Prolog answers:
u no
![Page 13: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/13.jpg)
13
More questions continue ---
Who is Liz’s parent? u ?- parent(X,liz).
So the answer is: u X = tom
![Page 14: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/14.jpg)
14
More questions continue --- Who are Bob’s children? u ?- parent(bob,X).
The first answer is: u X = ann
The another answer follows: u X = pat
![Page 15: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/15.jpg)
15
Broader Questions -- Who is a parent of whom? In other words, u Find X and Y such that X is a parent of Y. In Prolog,
u ?- parent(X,Y).
![Page 16: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/16.jpg)
16
Broader Questions Continue --- The answers are output as: u X = pam u Y = bob;
u X = tom u Y = bob;
u X = tom u Y = liz; u ...
![Page 17: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/17.jpg)
17
Composed Query in Prolog u Who is a grandparent of Jim?
![Page 18: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/18.jpg)
18
Composed Query in Prolog Continue -- To find a grandparent, we need two steps:
u Who is a parent of jim? Assume that there is some Y.
u Who is a parent of Y? Assume that there is some X.
In Prolog, u ?- parent(Y, jim), parent(X,Y).
![Page 19: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/19.jpg)
19
Composed Query in Prolog Continue -- Who are Tom’s grandchildren? u ?- parent(tom,X), parent(X,Y).
Do Ann and Pat have a common parent? u ?- parent(X,ann), parent(X,pat).
![Page 20: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/20.jpg)
20
Important Points u Easy to define a relation, by stating the n-tuples
of objects that satisfy the relation such as parent u Easy to query the Prolog system about relations
defined in the program u A Prolog program consists of clauses. Each
clause terminates with a full stop u Arguments of relations can be concrete objects,
or constants (such as tom and ann), or general objects such as X and Y
![Page 21: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/21.jpg)
21
Important Points Continue --- u Concrete objects or constants are called atoms
and general objects are called variables u Questions to the system consist of one or more
goals that are to be satisfied in the program such as: ?- parent(X,ann), parent(X,pat).
u Answer can be positive ( if satisfiable) or negative (if unsatisfable)
u If several answers satisfy the question then Prolog will find as many of them as desired by the user
![Page 22: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/22.jpg)
22
1.2 Defining relations by rules More relations Unary relations Binary relations u female(pam). sex(pam,feminine). u male(tom). sex(tom,masculine). u male(bob). sex(bob,masculine). u female(liz). sex(liz,feminine). u … ... Unary relations are simple yes / no properties of objects.
![Page 23: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/23.jpg)
23
More Relations Example u to define a relation offspring as the inverse of the parent
relation as a fact u offspring(liz, tom) is inverse of parent(tom, liz) It is understood as Liz is an offspring of Tom if Tom is a parent of Liz. In general, we can say that Y is an offspring of X if X is a parent of Y.
![Page 24: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/24.jpg)
24
Relations are Defined Elegantly u to define offspring relation using already defined
parent relation For all X and Y, Y is an offspring of X if X is a parent of Y. In Prolog, u offspring(Y,X) :- parent(X,Y).
![Page 25: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/25.jpg)
25
What is Rules? For all X and Y, if X is a parent of Y then Y is an offspring of X In Prolog, u offspring(Y,X) :- parent(X,Y). is called a Rule.
![Page 26: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/26.jpg)
26
Difference between facts and rules u A fact like parent(tom,liz) is something always,
unconditionally true.
u On the other hand, rules specify things that are true if some condition is satisfied.
![Page 27: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/27.jpg)
27
Rules have u body, a condition part (the right-hand side of the rule) and
u head, a conclusion part (the left-hand side of the rule)
The format is
offspring(Y,X) :- parent(X,Y). ------------------ --------------- head body
![Page 28: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/28.jpg)
28
More Rules To define mother relation by rule
For all X and Y, X is the mother of Y if X is a parent of Y and X is a female.
In Prolog, u mother(X,Y) :- parent(X,Y), female(X).
![Page 29: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/29.jpg)
29
More Rules Continue --- To define grandparent relation by rule For all X and Y, X is a grandparent of Y if X is a parent of Z and Z is a parent of Y. In Prolog, u grandparent(X,Y) :- u parent(X,Z), u parent(Z,Y).
![Page 30: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/30.jpg)
30
More Rules Continue --- How do we define sister relation? For all X and Y, X is a sister of Y if both X and Y have the same parent, and X is a female. In Prolog, u sister(X,Y) :- u parent(Z,X), u parent(Z,Y), u female(X).
![Page 31: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/31.jpg)
31
Question to prolog Who is pat’s sister? In Prolog, u ?- sister(X,pat).
The answer to the previous program u X = ann u X = pat
u We need to modify the program since pat is a sister of herself
![Page 32: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/32.jpg)
32
Some Important Points, So Far u Prolog program can be added new clauses u Clauses are of three types: facts, rules, and questions u Facts declare things that are always, unconditionally true u Rules declare things that are true depending on a given
condition u Questions are to be asked by the user
![Page 33: Prolog Programmingmhtay/ITEC480/Lecture_Prolog/Chapter_1_1_2010.pdf · Easy to query the Prolog system about relations defined in the program ! A Prolog program consists of clauses](https://reader030.vdocuments.net/reader030/viewer/2022040807/5e49f3a06145da3958739177/html5/thumbnails/33.jpg)
33
More Important Points u Clauses consists of head and body u Body is a list of goals separated by commas u Facts have a head and the empty body u Questions only have the body u Rules have the head and the non-empty body u A variable can be substituted by another object, that
is called, variable is instantiated