0% found this document useful (0 votes)
136 views6 pages

Introduction To Prolog Practical Session 2

The document discusses Prolog programs and knowledge bases. It provides examples of Prolog code representing facts and rules. It also explains how Prolog can use chaining of rules and modus ponens to infer facts not explicitly stated by reasoning over the knowledge base. Key Prolog concepts introduced include clauses, predicates, variables, atoms, and complex terms. Exercises are provided to test understanding of these concepts.

Uploaded by

Sesa Singha Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views6 pages

Introduction To Prolog Practical Session 2

The document discusses Prolog programs and knowledge bases. It provides examples of Prolog code representing facts and rules. It also explains how Prolog can use chaining of rules and modus ponens to infer facts not explicitly stated by reasoning over the knowledge base. Key Prolog concepts introduced include clauses, predicates, variables, atoms, and complex terms. Exercises are provided to test understanding of these concepts.

Uploaded by

Sesa Singha Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

BS-III Artificial Intelligence

INTRODUCTION TO PROLOG
Practical Session 2

Prolog programs simply are knowledgebases, collections of facts and rules


which describe some collection of relationships that we find interesting.

To execute a query in Prolog load the Knowledge Base (program) in the


buffer.
To create a KB go to FILE NEW and text editor will be open and you can
type your KB as given below and save it with .pl extension.
For bringing the program into the buffer go to FILE and than CONSULT
and locate the file. Now you can run queries and observe how your program
replies.

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.

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.

This fundamental deduction step is what logicians call modus ponens.

Let’s consider an example. We will ask Prolog whether Malik plays air
guitar:
?- playsAirGuitar(malik).

Prolog will respond “yes”. Why? Well, although


playsAirGuitar(malik) is not a fact explicitly recorded in KB2,
KB2 does contain the rule

Page 1 of 6
BS-III Artificial Intelligence

playsAirGuitar(malik) :- listensToMusic(malik).

Moreover, KB2 also contains the fact listensToMusic(malik).


Hence Prolog can use modus ponens to deduce that
playsAirGuitar(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.

Summing up: any fact produced by an application of modus ponens can be


used as input to further rules. By chaining together applications of modus
ponens in this way, Prolog is able to retrieve information that logically
follows from the rules and facts recorded in the knowledge base.

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.

2. An arbitrary sequence of character enclosed in single quotes. For example


’Waheed’, ’The Planet’, ’Five_Rupee_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.

3. A string of special characters. For example: @= and ====> and ; and


:- are all atoms. As we have seen, some of these atoms, such as ; and :-
have a pre-defined meaning.

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.

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

Page 3 of 6
BS-III Artificial Intelligence

This is a correct answer: as yet Prolog knows nothing — so it correctly


displays all this nothing and says yes.

Incidentally, listing can be used in other ways. For example, typing


?- listing(playsAirGuitar).
simply lists all the information in the knowledge base about the
playsAirGuitar predicate.

Exercise 1.1 Which of the following sequences of characters are atoms,


which are variables, and which are neither?
1. vINCENT
2. Footmassage
3. variable23
4. Variable2000
5. big_kahuna_burger
6. ’big kahuna burger’
7. big kahuna burger
8. ’Jules’
9. _Jules
10. ’_Jules’

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).

Exercise 1.3 Represent the following in Prolog:


1. Butch is a killer.killer(butch).
2. Mia and Marcellus are married.married(mia,marcellus).
3. Zed is dead.dead(Zed).
4. Mia likes everyone who is a good debator.likes(mia,Debator).
5. Mia eats anything that is nutritious or tasty.eat(mia,X):-
nutritious(X);tasty(X).

Exercise 1.1 Which of the following sequences of characters are atoms,


which are variables, and which are neither?

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)

father(Y,Z) :- man(Y), son(Z,Y). ). (rules)

father(Y,Z) :- man(Y), daughter(Z,Y). ).(rules)

Exercise 1.3 Represent the following in Prolog:


1. Butch is a killer. killer(butch).
2. Mia and Marcellus are married. married(mia,marcellus).
3. Zed is dead. dead(Zed).
4. Mia likes everyone who is a good debator .likes(mia,Debator).
5. Mia eats anything that is nutritious or tasty. eat(mia,X):-
nutritious(X);tasty(X).

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))).

How prolog will respond to the following queries:

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

You might also like