Prolog Day 1
Prolog Day 1
Prolog Day 1
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.
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.
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)
|
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.
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
Based on this knowledge base, formulate the rules defining the following
predicates:
Hint
sits_left_of(X,Y) :- sits_right_of(Y,X).
Solution
next_to_each_other(X,Y) :-
sits_right_of(X,Y).
next_to_each_other(X,Y) :-
sits_left_of(X,Y).
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
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,
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).