Title: PROLOG 1
1PROLOG 1
Semester II Nov 2004
2Low and High Level Languages
- The evolution of computer languages is an
evolution away form low-level languages toward
high-level language. - What is the difference between low-level
languages and high-level languages? - In low-level language, the programmer specifies
how something is to be done. - In high-level language, the programmer specifies
simply what is to be done.
3Goal-Oriented Programming
- is a language in which the satisfaction of goals
is the basis of program execution. - or
- is a language in which the set of goals to be
satisfied is an essential part of a function or
procedure body. - Such languages include Prolog and other logic
programming languages.
4PROLOG
- The best way to learn about goal-oriented
programming is to read and write goal-oriented
programs in Prolog, for goal-oriented programming
is what Prolog is all about. - Prolog is a language that clearly breaks away
from the how-type languages, encouraging the
programmer to describe situations and problems,
not to the detailed means by which the problems
are to be solved.
5Defining Relations by Facts
- Prolog is a programming language for symbolic,
non-numeric computation. - It is specially well suited for solving problems
that involve objects and relations between
objects.
aishah
ali
khalid
naim
ahmed
zahid
noor
khairul
6Defining Relations by Facts
- The fact that Ali is a parent of Naim can be
written in Prolog as - parent(ali, naim).
- parent is the name of the relation ali and naim
are its arguments. - Names like ali and naim will be written with
initial lowercase letters. - The whole family tree is defined as follows
- parent(ali, naim).
- parent(aishah, naim).
- parent(ali, khalid).
- parent(naim, zahid).
- parent(naim, ahmed).
- parent(ahmed, noor).
7Defining Relations by Facts
- This program consists of 6 clauses. Each clause
terminates with a full stop. - Each clause declares 1 fact about the parent
relation. For example, parent(ali, naim) is a
particular instance of the parent relation. - A relation is the set of all its instances.
- The question Is Ali a parent of Naim? can be
communicated to Prolog by typing - ?- parent(ali, naim).
- Prolog will answer
- yes
- While
- ?- parent(ali, noor).
- will produce
- no
8Defining Relations by Facts
- More interesting question can also be asked, such
as Who is Khalids parent? - ?- parent(X, khalid).
- Prolog will answer
- X ali
- While the question Who are alis children?
which has more than one answer, can be
communicated to Prolog as - ?- parent(ali, X).
- will produce
- X naim
- press to list the other answers
- X khalid
- if you request more solutions, Prolog will
answer no.
9Defining Relations by Facts
- The question
- ?- parent(X, Y).
- means who is a parent of whom? or Find X
and Y such that X is a parent of Y. Prolog will
display all solutions one by one. - The question who is the grandparent of Noor? is
also possible but in an indirect way since the
relationship grandparent is not defined yet. - ?- parent(Y, noor), parent(X, Y).
- which means Find such X and Y that satisfy
the two requirements. If we change the order the
logical meaning remains the same - ?- parent(X, Y), parent(Y, noor).
- Exercise in the same way ask Who are Aishas
grandchildren?.
10Defining Relations by Facts
- The question Do Zahid and Ahmed have a common
parent? - ?- parent(X, zahid), parent(X, ahmed).
- The arguments of relations can (among other
things) be - Concrete objects or constants (known as atoms)
such as khalid and zahid. - General objects such as X and Y. (known as
variables). - Questions to the system consists of one or more
goals, such as - ?- parent(X, zahid), parent(X, ahmed).
11Exercises
12Defining Relations by Rules
- To add the information about the sex of the
people that occur in the parent relation - female(aishah).
- male(ali).
- male(naim).
- male(khalid).
- male(zahid).
- male(ahmed).
- female(noor).
- These relations are unary, hence the output is
either yes or no. - Same information can be conveyed using a binary
relation - sex(ali, masculine).
- sex(aishah, feminine).
13Defining Relations by Rules
- The following relation is the inverse of the
parent relation - Offspring(khalid, ali). ...
- or
- offspring(Y,X) - parent(X, Y).
- Which can be read as For all X and Y, if X is a
parent of Y then Y is an offspring of X. Such
Prolog clauses are called rules. - Facts like offspring(khalid, ali) is always true,
while rules specify things that are true if some
condition is satisfied. That is why rules have a
condition part (body) on the right and a
conclusion part (head) on the left.
14Defining Relations by Rules
- For all X and Y, X is the mother of Y if X is a
parent of Y and X is a female. - mother(X, Y) - parent(X, Y), female(X).
- It can also be written as
- mother(X, Y) - X is Ys mum
- parent(X, Y), / Comments /
- female(X).
- Relations can be illustrated by diagrams
- Nodes correspond to objects.
- Arcs between nodes correspond to binary
- relations.
- The arcs are oriented so as to point from the
- first argument of the relation to the
second. - Unary relations are indicated in the diagrams by
- simply marking the corresponding objects
with the - name of the relation.
X
parent
offspring
Y
15Defining Relations by Rules
- The relations that are being defined are
represented by dashed arcs. - The graph is read as follows
- If the relations shown by solid arcs hold, then
the relation shown by a dashed are also holds.
female
X
female
X
Z
parent
grandmother
Y
parent
mother
parent
parent
female
parent
Y
X
Y
Z
sister
16- For any X and Y, X is a sister of Y if (1) both X
and Y have the same parent and (2) X is a female. - sister(X,Y) - parent(Z,X), parent(Z,Y),
female(X). - Prolog will say that Noor is a sister to herself
? because Prolog assumes that X and Y can be the
same. To correct the rule we have to add that X
and Y must be different (Will be done later).
17Exercise
- Bratko, page 13
- 1.3
- 1.4
- 1.5
18Recursive Rules
- The predecessor relation
- It is defined in terms of parent relation.
- The definition is expressed in two rules
- The direct (immediate) predecessors.
- The indirect predecessor.
- Some X is an indirect predecessor of some Z if
there is a parent-ship chain of people between X
and Z. - The first rule
- For all X and Z
- X is a predecessor of Z if
- X is a parent of Z
- predecessor (X,Z) - parent(X, Z).
- The second rule can be written as a chain
- predecessor (X,Z) - parent(X, Y), parent(Y,
Z).
X
parent
predecessor
parent
parent
1
Z
19- But the program is lengthy and works with that a
certain depth only. - Predecessor can be defined in terms of itself
- For all X and Z,
- X is a predecessor of Z if
- (1) X is a parent of Y and
- (2) Y is a predecessor of Z.
- predecessor (X, Z) - parent (X, Y), predecessor
(Y, Z).
2
20- This is known as recursion, in which we define
something that is not yet been completely defined - Recursive programming is one of the fundamental
principles of programming in Prolog. It is not
possible to solve tasks of any significant
complexity in Prolog without the use of
recursion. - ?- predecessor (aishah, X).
- There will be 4 answers.
- The two clauses of predecessor are about the
predecessor relation, such as set of clauses is
called a procedure.
21Assignment
- Construct the family program by collecting all
clauses given previously in one program. - Bratko, exercise 1.6, page 18
22How Prolog Answers Questions?
- Prolog tries to satisfy all the goals.
- Example
- All men are fallible. Socrates is a man.
- We conclude that Socrates is fallible.
- For all X, if X is a man then X is fallible.
- fallible(X) - man(X).
- man(socrates).
- ?- fallible(socrates).
- yes
- ?- predecessor(ali, ahmed). is a derived fact
23Assignment
- Exercise
- Bratko, Exercise 1.7 page 22