Introduction To Prolog Practical Session 2
Introduction To Prolog Practical Session 2
INTRODUCTION TO PROLOG
Practical Session 2
KB1
man(malik).
man(javed).
man(osama).
playsAirGuitar(javed).
?-. This symbol (or something like it, depending on the implementation of
Prolog you are using) is the prompt symbol that the Prolog interpreter
displays when it is waiting to evaluate a query. We just type in the actual
query
?- man(malik) followed by . (a full stop).
KB2
listensToMusic(malik).
happy(osama).
playsAirGuitar(malik) :- listensToMusic(malik).
playsAirGuitar(osama) :- listensToMusic(osama).
listensToMusic(osama):- happy(osama).
KB2 contains two facts, and last three items are rules.
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.
Let’s consider an example. We will ask Prolog whether Malik plays air
guitar:
?- playsAirGuitar(malik).
Page 1 of 6
BS-III Artificial Intelligence
playsAirGuitar(malik) :- listensToMusic(malik).
Our next example shows that Prolog can chain together uses of modus
ponens. Suppose we ask:
?- playsAirGuitar(osama).
Prolog would respond “yes”. Why? Well, using the fact happy(osama)
and the rule
listensToMusic(osama):- happy(osama)
Prolog can deduce the new fact listensToMusic(osama). This new
fact is not explicitly recorded in the knowledge base — it is only implicitly
present (it is inferred knowledge). Nonetheless, Prolog can then use it just
like an explicitly recorded fact.
Thus, together with the rule
playsAirGuitar(osama) :- listensToMusic(osama)
it can deduce that playsAirGuitar(osama), which is what we asked
it.
The facts and rules contained in a knowledge base are called clauses.
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.
To make things crystal clear, let’s first get clear about the basic characters
(or symbols) at our disposal. The upper-case letters are A, B, ..., Z; the
lower-case letters are a, b, ..., z; the digits are 1, 2, ..., 9; and the special
characters are +, -, *, /, <, >, =, :, ., &, ~, and _. The _ character is
called underscore. The blank space is also a character, but a rather unusual
one, being invisible. A string is an unbroken sequence of characters.
Numbers
Real numbers aren’t particularly important in typical Prolog applications. So
although most Prolog implementations do support floating point numbers or
floats (that is, representations of real numbers such as 1657.3087) we are not
going to discuss them in this course.
But integers (that is: ... -2, -1, 0, 1, 2, 3 ...) are useful for such tasks as
counting the elements of a list, and we’ll discuss how to manipulate them in
Page 2 of 6
BS-III Artificial Intelligence
a later lecture. Their Prolog syntax is the obvious one: 23, 1001, 0, -365,
and so on.
Atoms
An atom is either:
1. 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:
batch, big_tasty_burger, and m_mon2.
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, and we discuss it in a later lecture.
Complex terms
Constants, numbers, and variables are the building blocks: now we need to
know how to fit them together to make complex terms. Recall that complex
terms are often called structures.
Complex terms are build out of a functor followed by a sequence of
arguments. The arguments are put in ordinary brackets, separated by
commas, and placed after the functor. The functor must be an atom. That
is, variables cannot be used as functors.
On the other hand, arguments can be any kind of term. Now, we’ve already
seen lots of examples of complex terms when we looked at KB1
For example, playsAirGuitar(javed) is a complex term: its functor
is playsAirGuitar and its argument is javed.
Listing Command
To see this, type in the command listing, followed by a full stop, and hit
return. That is, type
?- listing.
yes
Page 3 of 6
BS-III Artificial Intelligence
Exercise 1.2 How many facts, rules, clauses, and predicates are there in
the following knowledge base? What are the heads of the rules, and what
are the goals they contain?
woman(vincent).
woman(mia).
man(jules).
person(X) :- man(X); woman(X).(rules)
loves(X,Y) :- knows(Y,X).
father(Y,Z) :- man(Y), son(Z,Y).
father(Y,Z) :- man(Y), daughter(Z,Y).
Page 4 of 6
BS-III Artificial Intelligence
1. vINCENT (atom)
2. Footmassage (variable)
3. variable23 (atom)
4. Variable2000 (variable)
5. big_kahuna_burger (atom)
6. ’big kahuna burger’ (atom)
7. big kahuna burger (neither)
8. ’Jules’ (atom)
9. _Jules (variable)
10. ’_Jules’ (atom)
Exercise 1.2 How many facts, rules, clauses, and predicates are there in
the following knowledge base? What are the heads of the rules, and what
are the goals they contain?
woman(vincent). (fact)
woman(mia). (fact)
man(jules). (fact)
person(X) :- man(X); woman(X). (rules)
loves(X,Y) :- knows(Y,X). ). (rules)
Page 5 of 6
BS-III Artificial Intelligence
Exercise 1
Here’s a simple example. The following two line knowledge base defines two predicates, namely
vertical/2 and horizontal/2, which specify what it means for a line to be vertical or
horizontal respectively.
vertical(line(point(X,Y),point(X,Z))).
horizontal(line(point(X,Y),point(Z,Y))).
1. vertical(line(point(1,1),point(1,3))). = yes
2. vertical(line(point(1,1),point(3,2))). = no
3. horizontal(line(point(1,1),point(2,Y))).=> y=1 =yes
4. horizontal(line(point(2,3),P)). = yes
Exercise 2
Which of the following pairs of terms match? Where relevant, give the variable
instantiations that lead to successful matching.
1. bread = bread = yes
2. ’Bread’ = bread =N
3. ’bread’ = bread = Y
4. Bread = bread = > Bread=bread = yes
5. bread = sausage = N
6. food(bread) = bread = N
7. food(bread) = X = > X=food(bread) =Y
8. food(X) = food(bread) = > X=bread = Y
9. food(bread,X) = food(Y,sausage)
X = sausage
Y = bread
Yes
10. food(bread,X,softdrink) = food(Y,sausage,X) = N
11. food(bread,X,juice) = food(Y,kahuna_burger) =N
12. food(X) = X
X = food(**)
Yes
13. meal(food(bread),drink(juice)) = meal(X,Y)
X = food(bread)
Y = drink(juice)
YES
14. meal(food(bread),X) = meal(X,drink(juice))
N
Exercise 3
Here is a tiny lexicon and mini grammar with only one rule which defines a sentence as
consisting of five words: an article, a noun, a verb, and again an
article and a noun.
word(article,a).
word(article,every).
word(noun,criminal).
word(noun,’big kahuna burger’).
word(verb,eats).
word(verb,likes).
sentence(Word1,Word2,Word3,Word4,Word5)
:-word(article,Word1),word(noun,Word2),word(verb,Word3),word(article,Word4),word(
noun,Word5).
What query do you have to pose in order to find out which sentences the grammar can
generate? List all sentences that this grammar can generate in the order Prolog will
generate them. Make sure that you understand why Prolog generates them in this order.
sentence(Word1,Word2,Word3,Word4,Word5)
Page 6 of 6