Professional Documents
Culture Documents
Chapter 29 Declarative Programming: Teaching Resources
Chapter 29 Declarative Programming: Teaching Resources
Teaching resources
Supporting notes
Students will understand better how to write declarative programs if they have some
practical experience. The declarative programming environment used in the
coursebook is SWI-Prolog (see www.swi-prolog.org for a free download). It is
strongly recommended that all worked examples are tested using a declarative
programming environment and that all tasks are tried.
Here is a link to an excellent tutorial site:
http://www.learnprolognow.org/lpnpage.php?pageid=online
The most common error with writing Prolog programs is to forget the full stop (.) at
the end of a clause.
For example, the following file is to be queried:
Figure 29.02
This is not a very helpful message. So you need to check source programs very
carefully. The fact that Prolog reports that it has six clauses whereas the source
program has seven gives the clue that only one clause is incorrectly written.
‘ex1a.pl:1:19’ means look at line 1, character position 19. This is the character after
‘)’.
Having consulted an error-free program, you might type in a query and not get an
answer:
Figure 29.04
It is now waiting for a semicolon (;) to give more answers:
Figure 29.05
Teaching ideas
Start with explaining the difference between imperative and declarative
programming. Give students a list of terms associated with logic programming (fact,
rule, query, goal, clause, head, body, term, atom).
Explain that the simplest logic programs are just collections of facts (the knowledge
base).
Students do Task 29.01 and Task 29.02.
Exercise 29.01
Students launch Prolog and consult the file created in Task 29.01 (in
SWI-Prolog, choose the File menu and consult …).
Type queries such as:
capitalCity(paris).
capitalCity(london).
capitalCity(Paris).
You will need to explain the surprising result from the last query:
Teaching ideas
Students need to understand that data items need to start with a lower-case letter
as all strings starting with an upper-case letter are interpreted as variables.
Exercise 29.02
Students should expand their capitalCity example with the clauses given
in Section 29.04.
Type queries such as:
capitalCity(AllCapitalCities).
cityInCountry(paris, ParisInCountry).
cityInCountry(CapitalCityOfFrance, france).
Explain that it helps understanding to choose variable names that convey
what the query is about.
Figure 29.07
Demonstrate Worked Example 29.01.
Exercise 29.03
Students should save the program used in Worked Example 29.01 and
run some of their own queries. For example: what contains potato?
ingredient(Dish, potato, _.)
Teaching ideas
Discuss the first four clauses given in Worked Example 29.02. They represent the
following family tree:
Dave
Fred
Figure 29.08
If we want to state all relationships, such as the fact that Dave is the grandfather of
Jack, Alia and Paul, then we need a lot more facts:
grandfather(dave, jack).
grandfather(dave, alia).
grandfather(dave, paul).
If we also want to know that Jack, Alia and Paul are siblings (brother and sister) then
we need these facts:
sibling(jack, alia).
sibling(jack, paul).
sibling(alia, jack).
sibling(alia, paul).
sibling(paul, alia).
sibling(paul, jack).
This is very tedious and error prone.
Introduce the need for rules that allow Prolog to deduce further facts from given
facts. We cannot explicitly write every fact into the knowledge base.
We know these rules:
Exercise 29.04
Ask students to draw their own family tree. Then they write the facts into
a knowledge base. Let them add the rules for grandparent, sibling and
father. Now they need to test that the knowledge base returns the
correct answers. Get them to query their knowledge base and check the
answers against their family tree.
Ask them to write more rules into their knowledge base to find: mother,
sister, grandfather, grandmother, brother, aunt, uncle.
Make sure they test their rules.
Teaching ideas
This is quite a difficult concept. Demonstrate Worked Example 29.04, both using the
graphical debugger and the trace as shown in Figure 29.08 of the coursebook.
Exercise 29.05
Consider the following clauses and list the order in which the clauses are
used to produce the answer(s):
Figure 29.09
Figure 29.10
Teaching ideas
Exercise 29.06
Students should add the ancestor rules to the knowledge base of their
own family tree and check that it gives the correct results.
Demonstrate Worked Example 29.05.
Exercise 29.07
Teaching ideas
The concept of a list is fairly simple: square brackets contain items separated by
commas. For example [a,b,c] is a list.
It might be more difficult for students to understand the effect of operations on lists.
Explain to students that a list has a head: the first item in the list. The remainder of
the list is known as the tail. So the head of [a,b,c] is a. The tail of [a,b,c] is [b,c].
The tail of a list is always a list. For example, the tail of [c] is the empty list [].
Trying to get the head or the tail of the empty list results in an error.
Students should save the following clauses shown in Section 29.09 in a text file:
Figure 29.12
And then they should query them in the Prolog shell:
Figure 29.14
The use of append to find a partial list is interesting and you might need to explain
this in more detail. Run further queries to aid understanding:
append([a], PartList, [a,b,c,d,e]).
append([apple, banana], [cherry, pear], MyFavouriteFruit).
Explain that a Prolog program can be written with a user interface. Show how the
following program works:
go:-
write('What is your name? '),
read(Yourname),
write('Hello, '),
write(Yourname), nl.
Exercise 29.08
Here is another example of recursion. Let students test this and explain
what happens.
list_cars:-car(Car), write(Car), nl, fail.
list_cars.
car(peugeot).
car(renault).
car(citroen).
car(ford).
car(triumph).
Figure 29.16
Demonstrate Worked Example 29.06. Students should enter this program and test it
themselves.
Exercise 29.09
/* interface */
familyTree:-
write('What is your name? '),
read(YourName), nl,
ancestor(Ancestor, YourName),
write(YourName), write(' has the following
Ancestors:'),write(Ancestor).
Figure 29.17