Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Chapter 29 Declarative Programming

Syllabus sections covered: 4.3.1

Teaching resources

Topics Syllabus 40 min. Resources in Resources on this


section periods coursebook CD ROM
1 Prolog facts 4.3.1 2 Task 29.01 Exercise 29.01
Task 29.02
2 Prolog variables 4.3.1 2 Worked Example 29.01 Exercise 29.02
Exercise 29.03
3 Prolog rules 4.3.1 6 Worked Example 29.02 Exercise 29.04
Question 29.01
Task 29.03
Worked Example 29.03
Exam-style question 1
4 Instantiation and 4.3.1 4 Worked Example 29.04 Exercise 29.05
backtracking Exam-style question 2
5 Recursion 4.3.1 6 Task 29.04 Exercise 29.06
Worked Example 29.05 *Exercise 29.07
Task 29.05
6 Lists 4.3.1 4 Worked Example 29.06 *Exercise 29.08
**Task 29.06 **Exercise 29.09
Tasks and exercises marked * are for students who have completed the basic
exercises.
Tasks and exercises marked ** are stretch and challenge exercises.

Past exam paper questions

Paper Series Question


9691/31 June 2014 Q3
9691/32 June 2014 Q3
Excellent notes on Prolog can be found at https://en.wikibooks.org/wiki/Prolog

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:

© Cambridge University Press 2016


Figure 29.01
This is the error message that Prolog responds with:

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:

© Cambridge University Press 2016


Figure 29.03
Type a full stop (.) now, and you will get the response:

Figure 29.04
It is now waiting for a semicolon (;) to give more answers:

Figure 29.05

29.03 Topic 1 Prolog facts


Coursebook section 29.01 Imperative and declarative programming languages, 29.02 Prolog
basics and 29.03 Facts in Prolog

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:

© Cambridge University Press 2016


Figure 29.06
This is a good time to introduce the concept of Prolog variables.

29.02 Topic 2 Prolog variables


Coursebook section 29.04 Prolog variables and 29.05 The anonymous variable

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.

© Cambridge University Press 2016


Introduce the concept of the anonymous variable using the 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, _.)

29.03 Topic 3 Prolog rules


Coursebook section 29.06 Rules in Prolog

Teaching ideas

Discuss the first four clauses given in Worked Example 29.02. They represent the
following family tree:

Dave

Fred

Jack Alia Paul

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:

© Cambridge University Press 2016


If two persons (A and B) have the same parent, then A and B are siblings.
If a person (G) is a parent of a parent (P), then this person is a grandparent.
To write these rules as logic clauses we introduce variables (for example A, B, G, P).
Demonstrate Worked Example 29.02.
Students answer Question 29.01.
Students do Task 29.03.

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.

29.04 Topic 4 Instantiation and backtracking


Coursebook section 29.07 Instantiation and backtracking

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 program:


01 female(kate).
02 male(tom).
03 male(fred).
04 male(jack).
05 parent(kate,tom).
06 parent(fred,tom).
07 parent(kate,jack).
08 parent(fred,jack).
09 mother(X,Y) IF female(X) AND parent(X,Y).
10 father(X,Y) IF male(X) AND parent(X,Y).
11 brother(X,Y) IF male(X) AND parent(F,X) AND
parent(F,Y) AND NOT(X=Y).

Consider the following clauses and list the order in which the clauses are
used to produce the answer(s):

© Cambridge University Press 2016


parent(kate, Child). (clauses 05, 07)
mother(kate, Child). (clauses 09, 01, 05, 07)
brother(jack, Brother). (clauses 11, 4, 07, 05, 07, 08, 06, 08)

Check with the SWI-Prolog trace:

Figure 29.09

Figure 29.10

© Cambridge University Press 2016


Figure 29.11
Explain why Tom is output as Jack’s brother twice: Tom is Jack’s brother
because Kate is the parent to both Tom and Jack. Tom is again Jack’s
brother because Fred is the parent to both Tom and Jack.
How can this duplication be avoided?
We need to rewrite the rule for brother:
11 brother(X,Y) IF male(X) AND father(F,X) AND
father(F,Y) AND mother(M,X)
AND mother (M,Y) AND NOT(X=Y).

29.05 Topic 5 Recursion


Coursebook section 29.08 Recursion

Teaching ideas

Remind students about recursion in imperative languages (Chapter 25). Recursive


solutions have a base case and a general case. This is also true when using
declarative languages.
Explain how to construct a base case and a general case for ancestor in the family
program.
Students do Task 29.04.

© Cambridge University Press 2016


When students work through this task, remind them to check what is happening by
running a trace. Are they surprised how many calls are made before Dave is
returned as an ancestor? And how many calls are made before the program returns
the answer that there are no more ancestors?

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

Compare the factorial functions in Worked Example 25.01 (Chapter 25)


with the factorial function in Worked Example 29.03.
Discuss how a recursive definition can be recognised. Can students think of a
definition for recursion that is correct for imperative and declarative recursion?

Students do Task 29.05.

29.06 Topic 6 Lists


Coursebook section 29.09 Lists

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:

© Cambridge University Press 2016


Figure 29.13
Students can type the built-in predicates covered in Section 29.09 directly into 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).

Read and write predicates

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.

© Cambridge University Press 2016


Figure 29.15
Note that the input must start with a lower case letter and be followed by a full stop.

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

Get students to write an interface for the family program.


Here is an example:
parent(fred, jack).
parent(fred, alia).
parent(fred, paul).

© Cambridge University Press 2016


parent(dave, fred).
grandparent(A, B) :- parent(A, P), parent(P, B).
sibling(A, B) :- parent(P, A), parent(P, B),not(A=B).
ancestor(A, B) :- parent(A, B).
ancestor(A, B) :- parent(A, X), ancestor(X, B).

/* 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

© Cambridge University Press 2016

You might also like