Prolog Tutorial
Prolog Tutorial
Lecture Overview
Prolog
1. Introduction
• Interaction
• Terms
2. Clauses and predicates
• Clauses
• Predicates
• Variables
3. Satisfying goals
2
Prolog
A standard free Prolog can be downloaded from
http://www.swi-prolog.org
?- write(‘Hello, world!’).
Hello, world!
true
?-
animal(X):-dog(X).
This program has three clauses. The first two are facts and
the last is a rule. The facts represent the statements “fido
5
?- animal(fido).
true
?- animal(felix).
false
?- cat(X).
X = felix.
cat(felix).
7
cat(mittens).
cat(tom).
cat(phidippides).
?- listing(cat).
cat(felix).
cat(mittens).
cat(tom).
cat(phidippides).
true.
Comments
Comments are written enclosed in /* … */ delimiters.
Predicates
Unfortunately, the same name may be used as the functor
for compound terms of different arities. The name may
also be used as an atom. For instance, the following is
legal:
parent(victoria, albert).
parent(john).
animal(parent).
All the clauses for which the head has a given combination
of functor and arity comprise a definition of a predicate.
The clauses do not have to appear as consecutive lines of
the program but it makes the program easier to read if
they do.
Recursion
Recursion is frequently used when defining predicates.
Recursion can be direct or indirect. Here’s an example of
direct recursion:
likes(john, X):-likes(X,Y),dog(Y)
Predicates
Predicates are relations between a number of values (its
arguments) which can be either true or false. This
contrasts with functions, which can evaluate to any type of
object (e.g. a number, a string, a point, etc.).
Loading clauses
As mentioned earlier, the BIP consult/1 causes Prolog to
read the clauses in the file given as consult’s argument. If
the same file is consulted again during a Prolog session,
then all the clauses from the first consultation are
removed from the database before the new
consultation’s clauses are loaded.
Square brackets are an abbreviation for consult/1:
?-[‘testfile1.pl].
means the same as
?-consult(‘testfile1.pl’).
Loading clauses from more than one file can have
interesting effects, particularly if both files try to define
the same predicate. (Only the last version of the predicate
will be kept.) So beware!
18
Variables
Variables can be used in the head or body of a clause, and
in goals entered at the system prompt. Their
interpretation depends on where they are used.
Variables in goals.
Variables in goals can be interpreted as “find values of the
variables that make the goal satisfied”. For example, the
goal
?-animal(A)
means to find values of A such that animal(A) is satisfied.
Quantification.
If a variable appears in the head of a rule or a fact it is
taken to indicate that the rule or fact applies for all
possible values of the variable. Such a variable is said to
be universally quantified. For example,
animal(X):-dog(X).
means that for any value that X can take on, animal(X) is
true if dog(X) is true.
On the other hand, if a variable appears in the body of a
rule it is part of a goal that is satisfied if there exists a
value of the variable that satisfies the goal. Such a
variable is said to be existentially quantified. For
example,
dogowner(X):-dog(Y),owns(X,Y)
means that for any value X can take on, X is a dogowner if
there exists a Y such that Y is a dog and X owns Y.
Satisfying goals
We look more closely at how Prolog satisifies goals. The
process starts when the user types a query at the
command prompt.
The Prolog system attempts to satisfy each goal in the
query in turn, working from left to right. When a goal
involves variables, this generally involves binding them to
values. If all the goals succeed in turn, the whole query
succeeds, and the system will output the values of the
variables used in the query.
A call term is an atom or a compound term. It cannot be a
number, variable, or list. Every goal must be a call term.
Goals relating to BIPs are evaluated in a way predefined by
the Prolog system (consider write/1 and nl/0). Goals
relating to user-defined predicates are evaluated by
examining the database of rules and facts loaded by the
user.
Prolog attempts to satisfy a goal by matching it with the
heads of clauses in the database, working from top to
bottom.
For example, the goal
22
?-dog(X).
might be matched with the fact
dog(fido).
Unification
Prolog uses a very general form of matching known as
unfication. (We’ve seen it before in type unification.)
Unification generally involves one or more variables being
given values in order to make two call terms identical; this
is known as binding the variables to the values. For
example, the terms dog(X) and dog(fido) can be unified by
binding variable X to the atom fido.
23
person(john, Y, 23)
person(X, smith, 27)
pred1(X, X, man)
pred1(london, dog, A)
pred2(X, X, man)
pred2(london, london, A)
Evaluating goals
Suppose that we have a database with the clauses:
capital(london, england).
european(england).
pred(X, ‘european capital’) :-
capital(X, Y), european(Y), write(X), nl.
The goal in the query will unify with the head of the rule in
the database, with X bound to london and A bound to
‘european capital’. Prolog now tries to evaluate the rule by
evaluating the terms in the body of the rule, in turn.
• First it tries to evaluate capital(X, Y), which due to X
being bound to london is interpreted as
capital(london, Y). To evaluate this, it looks for
matching terms in the database, and finds
27
Declarative programming
It should be clear that the order in which the clauses
defining a predicate occur in the database and the order of
goals in the body of a rule are of vital importance when
evaluating a user’s query.
It is part of the philosophy of logic programming that
programs should be written to minimize the effect of
these two factors as much as possible. Programs that do
so are called fully or partly declarative.