prolog - radford universitymhtay/itec480/lecture_prolog/chapter_1_2_2010.pdf · 13 how prolog...
TRANSCRIPT
![Page 1: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/1.jpg)
1
Prolog Programming
Continued
Chapter 1: Introduction to Prolog
![Page 2: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/2.jpg)
2
Chapter 1: Introduction to Prolog
Defining relations by facts
Defining relations by rules
Recursive rules
How Prolog answers questions
Declarative and procedural meaning of programs
![Page 3: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/3.jpg)
3
Fig 1 Fig 2 Fig 3
![Page 4: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/4.jpg)
4
1.3 Recursive Rules
To add a new predecessor relation in our family program
For all X and Z,
X is a predecessor of Z if
X is a parent of Z.
In Prolog,
Rule 1 For Fig 1
predecessor(X,Z) :-
parent(X,Z).
![Page 5: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/5.jpg)
5
In our family program
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
So Tom is a direct predecessor of Liz and indirect
predecessor of Pat.
Rule 1 is just to find a direct predecessor
![Page 6: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/6.jpg)
6
Family program again
In Prolog,
Rule 2 For Fig 2
predecessor(X,Z) :-
parent(X,Y),
parent(Y,Z).
Rule 3 For Fig 3
predecessor(X,Z) :-
parent(X,Y1),
parent(Y1,Y2),
parent(Y2,Z).
![Page 7: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/7.jpg)
7
What about more depth for predecessor
predecessor(X,Z) :-
parent(X,Y1),
parent(Y1,Y2),
parent(Y2,Y3),
parent(Y3,Z).
The program will be lengthy and work to some extent.
![Page 8: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/8.jpg)
8
An Elegant and Correct Formation of Predecessor
For all X and Z,
X is a predecessor of Z if
there is a Y such that
(1) X is a parent of Y and
(2) Y is a predecessor of Z.
In Prolog,
predecessor(X,Z) :-
parent(X,Y),
predecessor(Y,Z).
![Page 9: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/9.jpg)
9
Recursive Rules
predecessor(X,Z) :-
parent(X,Z). % for direct predecessor
predecessor(X,Z) :-
parent(X,Y),
predecessor(Y,Z). % for indirect predecessor
![Page 10: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/10.jpg)
10
Questions to Prolog
Who are Pam’s successors?
?- predecessor(pam,X).
Answer:
X=bob
X=ann
X=pat
X=jim
![Page 11: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/11.jpg)
11
Chapter 1: Introduction to Prolog
Defining relations by facts
Defining relations by rules
Recursive rules
How Prolog answers questions
Declarative and procedural meaning of programs
![Page 12: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/12.jpg)
12
1.4 How Prolog answer questions
A question to Prolog is always a sequence of one or
more goals
To answer a question, Prolog tries to satisfy all the goals
To satisfy a goal means to demonstrate that the goal is
true
In other words, to satisfy a goal means to demonstrate
that the goal logically follows from the facts and rules in
the program
![Page 13: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/13.jpg)
13
How Prolog answers continue ---
If the question contains variables, Prolog also has to
find what are the particular objects ( in place of
variables) for which the goals are satisfy.
The particular instantiation of variables to these objects
is displayed to the user.
If prolog cannot demonstrate for some instantiation of
variables that the goals logically follow from the
program, then Prolog’s answer to the question will be
“no”.
![Page 14: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/14.jpg)
14
Mathematically,
Prolog accepts facts and rules as a set of axioms
the user’s question as a conjectured theorem
then prolog tries to prove this theorem
that is, to demonstrate that it can be logically
derived from the axioms
![Page 15: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/15.jpg)
15
Example
Axioms are :
All men are fallible.
Socrates is a man.
A theorem logically follows from these two axioms
Socrates is fallible.
![Page 16: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/16.jpg)
16
Example continue ---
For all X,
if X is a man then X is fallible.
In Prolog,
fallible(X) :- man(X). % All men are fallible
man(socrates). % Socrates is a man
?- fallible(socrates) % Is Socrates fallible?
yes
![Page 17: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/17.jpg)
17
our family program
parent(pam,bob). /* The following are rules */
parent(tom,bob). offspring(Y,X) :- parent(X,Y).
parent(tom,liz). grandparent(X,Y):-
parent(bob,ann). parent(X,Z),
parent(bob,pat). parent(Z,Y).
parent(pat,jim). sister(X,Y) :-
female(pam). parent(Z,X),
male(tom). parent(Z,Y),
male(bob). female(X).
female(liz). predecessor(X,Z):- %pr1
female(pat). parent(X,Z).
female(ann). predecessor(X,Z):- %pr2
male(jim). parent(X,Y),
predecessor(Y,Z).
![Page 18: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/18.jpg)
18
Example from our family program
Question
?- predecessor(tom,pat).
Need to check if it satisfies one of two rules pr1 or pr2
predecessor(X,Z):- %pr1 (direct)
parent(X,Z).
predecessor(X,Z):- %pr2 (indirect)
parent(X,Y),
predecessor(Y,Z).
![Page 19: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/19.jpg)
19
The complete execution trace to satisfy the goal predecessor(tom,pat)
by rule pr1 by rule pr2
Y=bob by fact parent(tom,bob)
by rule pr1
yes
no
parent(tom,pat)
parent(bob,pat)
predecessor(bob,pat)
parent(tom,Y)
predecessor(Y,pat)
predecessor(tom,pat)
![Page 20: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/20.jpg)
20
Important Points
Procedure
A set of clauses about the same relation is called a
procedure
for example, predecessor relation is a procedure since
it is defined by two clauses
Comments
/* This is a comment */
% This is also a comment
![Page 21: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/21.jpg)
21
Chapter 1: Introduction to Prolog
Defining relations by facts
Defining relations by rules
Recursive rules
How Prolog answers questions
Declarative and procedural meaning of programs
![Page 22: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/22.jpg)
22
1.5 Declarative and procedural meaning of programs
the declarative meaning
concerned with the relations defined in the program
what will be the output of the program
the procedural meaning
how this output is obtained, that is,
how the relations are actually evaluated by the
Prolog system
![Page 23: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/23.jpg)
23
Summary
Prolog programming consists of defining relations and querying about
relations
A program consists of clauses. ( facts, rules, questions)
A relation can be specified by facts or by stating rules about the relation
procedure is a set of clauses about the same relation
querying about relations, by means of questions
In prolog, to establish whether an object satisfies a query is often a
complicated process that involves logical inference, exploring among
alternatives and possibly backtracking. All this is done automatically by the
Prolog system and is , in principle, hidden from the user
The declarative view is advantageous from the programming point of view.
Nevertheless, the procedural details often have to be considered by the
programmer as well.
![Page 24: Prolog - Radford Universitymhtay/ITEC480/Lecture_Prolog/Chapter_1_2_2010.pdf · 13 How Prolog answers continue --- If the question contains variables, Prolog also has to find what](https://reader034.vdocument.in/reader034/viewer/2022051908/5ffc4d32933cf32e847fd1a4/html5/thumbnails/24.jpg)
24
End of Chapter 1