Tutorial SHEET-1 Introduction To SWI Prolog
Tutorial SHEET-1 Introduction To SWI Prolog
1. Introduction
1.1. Syntax
All the facts, rules and queries in prolog are built of terms.
There are four kinds of terms in Prolog: atoms, numbers, variables, and
complex terms (or structures). Atoms and numbers are lumped together under
the heading constants, and constants and variables together make up the
simple terms of Prolog.
1. Atoms
An atom is either:
● A string of characters made up of upper-case letters, lower-case
letters, digits, and the underscore character, that begins with a
lower-case letter. For example: butch, big_kahuna_burger,
and m_monroe2.
1
● An arbitrary sequence of characters enclosed in single quotes. For
example ’Vincent’, ’The Gimp’, ’Five_Dollar_Shake’,
’&^%&#@$ &*’, and ’ ’. The character between the single
quotes is called the atom name. Note that we are allowed to use
spaces in such atoms — in fact, a common reason for using single
quotes is so we can do precisely that.
2. Numbers
Prolog supports floating point numbers as well as integers. Their Prolog
syntax is the obvious one: 23, 1001, 0, -365, and so on.
3. Variables
A variable is a string of upper-case letters, lower-case letters, digits and
underscore characters that starts either with an upper-case letter or with
underscore. For example, X, Y, Variable, _tag, X_526, and
List, List24, _head, Tail, _input and Output are all Prolog
variables.
The variable _ (that is, a single underscore character) is rather special. It’s
called the anonymous variable.
4. Complex Terms
Constants, numbers, and variables are the building blocks that fit together
to make complex terms.
2
For example, playsAirGuitar(jody) is a complex term: its functor is
playsAirGuitar and its argument is jody. Other examples are
loves(vincent,mia) and jealous(marcellus,W). Here both the
complex terms have two arguments and the latter has the second
argument as a variable.
The number of arguments that a complex term has is called its arity. For
instance, woman(mia) is a complex term with arity 1, while
loves(vincent,mia) is a complex term with arity 2.
1.2.1. Example 1
Knowledge Base can be created in a Prolog file, which can be created using any
text editor.
The facts and rules contained in a knowledge base are called clauses. Facts are
used to state things that are unconditionally true of the domain of interest.
For example, we can state that Mia, Jody, and Yolanda are women, and that
Jody plays air guitar, using the following four facts:
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
3
This collection of facts is a knowledge base. Let it be named KB1. It is an
example of a Prolog program. Note that the names mia, jody, and yolanda,
and the properties woman and playsAirGuitar, have been written so that the
first letter is in lower-case.
Save the file with the .pl extension in the working directory of Prolog as kb1.pl
For executing queries, open SWI Prolog console. Prolog will display its prompt,
something like
?-
which indicates that it is ready to accept a query.
Type in the command listing, followed by a full stop, and hit return. That is, type
?- listing.
and press the return key.
Now, the listing command is a special in-built Prolog predicate that instructs
Prolog to display the contents of the current knowledge base. But we haven’t yet
told Prolog about any knowledge bases, so it will just say
yes
This is a correct answer: as yet Prolog knows nothing — so it correctly displays
all this nothing and says yes. You may get a little more (for example, the names
of libraries that have been loaded).
4
Sometimes, Prolog may give an error message if the file is not found. You can
change the working directory to where the file is stored by typing:
?- working_directory(CWD,'NewPath').
Try the listing command again. Prolog will now also display the contents of the
loaded file.
Similarly, we can ask whether Jody plays air guitar by posing the following query:
?- playsAirGuitar(jody).
Prolog will again answer “yes”, because this is one of the facts in KB1. However,
suppose we asK whether Mia plays air guitar:
?- playsAirGuitar(mia).
We will get the answer
no
This is because it is not a fact in KB1. Moreover, KB1 is extremely simple, and
contains no other information (such as the rules) which might help Prolog
try to infer (that is, deduce whether Mia plays air guitar. So Prolog correctly
concludes that playsAirGuitar(mia) does not follow from KB1.
5
Not only can we use Prolog to deduce or infer whether a fact is true or false, but
we can also use Prolog to find out which
1.2.2. Example 2
Now let us make a new knowledge base KB2 with
listensToMusic(mia).
happy(yolanda).
playsAirGuitar(mia) :- listensToMusic(mia).
playsAirGuitar(yolanda) :- listensToMusic(yolanda).
listensToMusic(yolanda):- happy(yolanda).
Rules state information that is conditionally true of the domain of interest. For
example, the first rule says that Mia plays air guitar if she listens to music, and
the last rule says that Yolanda listens to music if she is happy. More generally,
the :- should be read as “if”, or “is implied by”. The part on the left hand side of
the :- is called the head of the rule, the part on the right hand side is called the
body.
So in general rules say: if the body of the rule is true, then the head of the rule
is true too. And now for the key point: if a knowledge base contains a rule head
:- body, and Prolog knows that body follows from the information in the
knowledge base, then Prolog can infer head.
We can view a fact as a rule with an empty body.
Let’s consider an example. We will ask Prolog whether Mia plays air guitar:
?- playsAirGuitar(mia).
Prolog will respond “yes”. Why? Well, although playsAirGuitar(mia) is not a
fact explicitly recorded in KB2, KB2 does contain the rule
playsAirGuitar(mia) :- listensToMusic(mia).
Moreover, KB2 also contains the fact listensToMusic(mia). Hence Prolog
can use modus ponens to deduce that playsAirGuitar(mia) is true.
6
Suppose we ask:
?- playsAirGuitar(yolanda).
Prolog would respond “yes”. It uses the fact happy(yolanda) and the rule
listensToMusic(yolanda):- happy(yolanda),
Prolog can deduce the new fact listensToMusic(yolanda). This new fact is
not explicitly recorded in the knowledge base — it is only implicitly present (it is
inferred knowledge). Thus Prolog can chain together uses of modus ponens.
KB2 consists of three predicates (or procedures). The three predicates are:
listensToMusic
happy
playsAirGuitar
The happy predicate is defined using a single clause (a fact). The
listensToMusic and playsAirGuitar predicates are each defined using
two clauses (in both cases, two rules). In essence, the predicates are the
concepts we find important, and the various clauses we write down concerning
them are our attempts to pin down what they mean and how they are
interrelated.
Prolog allows us to define two predicates with the same functor but with a
different number of arguments. For example, we are free to define a knowledge
base that defines a two place predicate love (this might contain such facts as
love(vincent,mia)), and also a three place love predicate (which might
contain such facts as love(vincent,marcellus,mia)).
However, if we did this, Prolog would treat the two place love and the three
place love as completely different predicates.
7
happy/1
playsAirGuitar/1
1.2.3. Example 3
Let KB3 be another knowledge base:
happy(vincent).
listensToMusic(butch).
playsAirGuitar(vincent):-
listensToMusic(vincent),
happy(vincent).
playsAirGuitar(butch):-
happy(butch).
playsAirGuitar(butch):-
listensToMusic(butch).
8
So KB3 only fulfils one of the two preconditions needed to establish
playsAirGuitar(vincent), and our query fails.
Next, note that KB3 contains two rules with exactly the same head, namely:
playsAirGuitar(butch):-
happy(butch).
playsAirGuitar(butch):-
listensToMusic(butch).
This is a way of stating that Butch plays air guitar if either he listens to music, or if
he is happy.
That is, listing multiple rules with the same head is a way of expressing logical
disjunction (that is, it is a way of saying or). So if we posed the query
?- playsAirGuitar(butch).
Prolog would answer “yes” even though the first of these rules will not help (KB3
does not allow Prolog to conclude that happy(butch)), KB3 does contain
listensToMusic(butch) and this means Prolog can apply modus ponens
using the rule
playsAirGuitar(butch):-
listensToMusic(butch).
to conclude that playsAirGuitar(butch).
There is another way of expressing disjunction in Prolog. We could replace the
pair of rules given above by the single rule
playsAirGuitar(butch):-
happy(butch);
listensToMusic(butch).
The semicolon ; is the Prolog symbol for OR. But Prolog programmers usually
write multiple rules, as extensive use of semicolon can make Prolog code hard to
read.
1.2.4 Example 4
KB4 is as follows:
woman(mia).
woman(jody).
9
woman(yolanda).
loves(vincent,mia).
loves(marcellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
jealous(X,Y) :- loves(X,Z),loves(Y,Z).
KB4 contains three facts about woman, four facts about the loves relation and
one rule. It also contains three variables: X, Y and Z.
There is information about other women in the knowledge base. We can access
this information by typing the following simple query
?- ;
So it responds:
X = jody
If we press ; a second time, Prolog returns the answer
X = yolanda
10
Since no further responses are possible, Prolog replies with ‘no’ if ; is pressed a
third time.
Another query,
?- loves(marcellus,X),woman(X).
gives X = mia as the only answer.
2. Exercises
wizard(ron).
hasWand(harry).
quidditchPlayer(harry).
wizard(X) :- hasBroom(X),hasWand(X).
hasBroom(X) :- quidditchPlayer(X).
How does Prolog respond to the following queries?
1) wizard(ron).
2) witch(ron).
3) wizard(hermione).
11
4) witch(hermione).
5) wizard(harry).
6) wizard(Y).
7) witch(Y).
12