You are on page 1of 9

Starting Prolog

 Start a terminal. (The little icon of a computer screen.)


 Type pl at the prompt.
 You should then see something like this on your screen:
 Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13)
 Copyright (c) 1990-2003 University of Amsterdam.
 SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
 and you are welcome to redistribute it under certain conditions.
 Please visit http://www.swi-prolog.org for details.

 For help, use ?- help(Topic). or ?- apropos(Word).

 ?-

 You can now send commands and queries to the Prolog interpreter by
typing them in after the ?- prompt.
 For example, type listing. (don't forget the full stop at the end) and
press 'return'.
 You should get something like:
 % Foreign: rl_read_init_file/1

 % Foreign: rl_add_history/1

 Yes

listing is a command that makes Prolog show you the facts and rules
that are currently loaded. It seems that something has been loaded
(Foreign: rl_read_init_file/1 ... ), but you ignore it and except for
that the knowledge base is empty. And it should be, since you have not
actually loaded anything, yet.

Loading and querying knowledge


bases
 Download the knowledge bases kb1.pl, kb2.pl, and kb3.pl (which are
the knowledge bases that we just talked about). To download, right
click on the following links and choose 'save link to disk'.
kb1.pl
kb2.pl
kb3.pl

 Now type consult('kb1.pl'). at the ?- prompt. This loads knowledge


base kb1.pl. Prolog should respond with
 % kb1.pl compiled 0.00 sec, 0 bytes

 Yes

 Type listing. to see which facts and clauses Prolog now knows about.
 If you now send queries to Prolog, they will be answered based on this
knowledge. Try this. Here are some queries that you could send.

Is ron a wizard? wizard(ron).


Is ron a muggle? muggle(ron).
Who is a muggle? muggle(X).
Who does crookshanks chase? chases(crookshanks,X).
Who chases whom? chases(X,Y).

 Don't forget to put a full stop at the end of each query before pressing
return. If you do forget to put a full stop, Prolog will react by just doing
nothing. This is what you will see on the screen:
 ?- wizard(harry)
 |

To fix it just type a full stop and press return again:


?- wizard(harry)
| .

Yes

 If something else goes wrong and Prolog start writing crazy things on
the screen without getting back to showing you the ?- prompt,
type Ctrl-c. This should show you the following line:
 Action (h for help) ?

Type a (for abort) and press return. This should get you back to
the ?- prompt.
 Load and query the other two knowledge bases (kb2.pl and kb3.pl), as
well. It is probably a good idea to clean Prologs internal knowledge
base before loading a new one. The easiest way to do this, is to actually
leave Prolog by typing halt. and restart it (by typing pl).
 Play a bit with the knowledge bases kb1.pl, kb2.pl, and kb3.pl, before
going on to the next section, which will tell you how to write your own
knowledge bases.

Writing knowledge bases


 Start emacs (or another editor of your choice), open a new file
called Name.pl (where Name is a name of your choice), write your
knowledge base, save the file. You can then load and query this
knowledge base in the same way as you loaded and queried the
knowledge bases kb1.pl, kb2.pl, and kb3.pl. When making changes
to Name.pl, you have to save the file again and you have to load it into
Prolog again.
 If you don't know emacs, go here for a quick introduction.
 As a first exercise, write a Prolog knowledge base specifying the
following facts and rules:
1. Harry is a wizard.
2. Hagrid scares Dudley.
3. All wizards are magical.
4. Uncle Vernon hates anyone who is magical.
5. Aunt Petunia hates anyone who is magical or scares Dudley.

Hints

 Load this database into Prolog. Prolog should give you a short answer
saying that it compiled your knowledge base. If it gives you a different
answer, there might be a problem with your knowledge base. Skip
ahead to the next section to see which are the things that Prolog may
complain about.
 Ask the following queries:
1. Does Aunt Petunia hate Hagrid? (The answer should be yes.)
2. Who does Uncle Vernon hate? (The answer should be Harry.)
3. Who does Aunt Petunia hate? (The answer should be Harry and
Hagrid. Type semicolon to get the second answer.)
Interpreting the error messages and
warnings that Prolog may give you
 Prolog will tell you if the things that you are writing are syntactically
incorrect.
Download the knowledge base kb.syntax_error.pl and load it into
Prolog. You will get the following answer.
 ?- consult('kb.syntax_error.pl').
 ERROR: /home/kris/lehre/esslli04/esslli04prolog/kbs/kb.syntax_error.pl:5:
Syntax error: Operator expected
 ERROR: /home/kris/lehre/esslli04/esslli04prolog/kbs/kb.syntax_error.pl:11:
Syntax error: Operator expected
 ERROR: /home/kris/lehre/esslli04/esslli04prolog/kbs/kb.syntax_error.pl:14:
Syntax error: Unexpected end of clause
 ERROR: /home/kris/lehre/esslli04/esslli04prolog/kbs/kb.syntax_error.pl:17:
Syntax error: Unexpected end of file
 % kb.syntax_error.pl compiled 0.00 sec, 1,348 bytes

Yes

This tells you that there are four syntax errors. It also tells you that the
Prolog interpreter thinks that the errors are in lines 6, 12, 15, and 18.
And it gives you some indications as to what might cause the problems
(operator expected, unexpected end of clause/file).
Which are the mistakes?
Solutions

 Another thing that you might get complaints about are singleton
variables; i.e., variables which are used only once in a clause. Download
the knowledge base kb.singleton_vars.pl and load it into Prolog. You will
get the following:
 ?- consult('kb.singleton_vars.pl').
 Warning:
(/home/kris/lehre/esslli04/esslli04prolog/kbs/kb.singleton_vars.pl:8):
 Singleton variables: [X, Y]
 % kb.singleton_vars.pl compiled 0.00 sec, 1,520 bytes

Yes

Prolog tells you that X and Y are singleton variables in the clause


starting at line 8. Prolog warns you when it detects singleton variables,
because singleton variables are often due to a typo (as in our example).
Sometimes you do want to use variables which are mentioned only once
in a clause. To avoid getting the singleton variable warning, you should
use the anonymous variable _ instead of a named variable in this case.
 Finally, Prolog complains when different clause belonging to the
definition of the same predicate are not together in the file. Here is an
example: kb.clauses_not_together.pl. When you load this knowledge
base Prolog answers:
 ?- consult('kb.clauses_not_together.pl').
 Warning:
(/home/kris/lehre/esslli04/esslli04prolog/kbs/kb.clauses_not_together.pl:14
):
 Clauses of hate/2 are not together in the source-file
 % kb.clauses_not_together.pl compiled 0.00 sec, 0 bytes

Yes

The first clause of the definition of the predicate hate/2 is separated


from the other two clauses by a rule with the head magical(X).

The Gryffindor table


The following picture shows who is sitting at the Gryffindor table. Define the
functor sits_right_of/2 to represent who is sitting right of
whom. sits_right_of(X,Y) should be true if X is to the right of Y.
Solution

Based on this knowledge base, formulate the rules defining the following
predicates:

 sits_left_of/2: sits_left_of(X,Y) should be true if X is to the left of Y.


Hint

Hint
sits_left_of(X,Y) :- sits_right_of(Y,X).

 are_neighbors_of/3: are_neighbors_of(X,Y,Z) should be true if X is to the


left of Z and Y is to the right of Z.
Solution
Hint
are_neighbors_of(X,Y,Z) :-
sits_left_of(X,Z),
sits_right_of(Y,Z).

 next_to_each_other/2: next_to_each_other(X,Y) should be true if X is next


to Y.
Hint

Solution
next_to_each_other(X,Y) :-
sits_right_of(X,Y).
next_to_each_other(X,Y) :-
sits_left_of(X,Y).

Test your implementation by asking queries. For example:

 Is Lavender to the right of Parvati?


 Is Lavender to the right of Neville?
 Who is to the right of Hermione?
 Who is sitting at the table?
 Who is sitting two seats to the right of Hermione?
 Who is sitting between Neville and Fred?

Family relationships
Use the predicates male/1, female/1, and parent_of/2 to represent the following
family tree as a Prolog knowledge base.
Solution

Now, formulate rules to capture the following relationships:

 father_of(Father,Child) and mother_of(Mother,Child)
 grandfather_of(Grandfather,Child) and grandmother_of(Grandmother,Child)
 sister_of(Sister,Person)
 aunt_of(Aunt,Person) and uncle_of(Uncle,Person)

Hint

To test your knowledge base ask all kinds of queries. For example,

 Does Harry have an aunt? Who?


 Who are the grandparents of Harry?
 Who are the grandchildren of Paul and Helen?
 Does James have a sister?
 ...

Hint
There are four cases in which the predicate menu(Status,X,Y,Z) should be true.
For each of them you have to specify a rule. The four cases are

 Status = hungry, X is
some starter, Y is some main dish, Z is some desert
 Status = not_so_hungry, X is some starter, Y is some main
dish, Z = nothing
 Status = not_so_hungry, X = nothing, Y is some main dish, Z is some
desert
 Status = on_diet, X is some starter, Y is some main dish, Z is some
desert
Hint
menu(hungry,X,Y,Z) :- starter(X),
main(Y),
desert(Z).
menu(not_so_hungry,X,Y,nothing) :- starter(X),
main(Y).
menu(not_so_hungry,nothing,Y,Z) :- main(Y),
desert(Z).
menu(on_diet,X,nothing,nothing) :- starter(X).

You might also like