Symbolic and Declarative Computing & AI, Programming in Prolog

Geography
September 8, 2020
Map the Supply Chain Paper
September 8, 2020

Symbolic and Declarative Computing & AI, Programming in Prolog

1 The Modalities of Your Task
The coursework task formulated on the pages below is your Prolog programming coursework. It comprises ten subtasks. The maximum number of marks
achievable by each task is 10. You are advised to follow the guidance given below. Try to work through the sheet in a systematic fashion (i.e. from beginning
to end with as much coverage as possible). Partial solutions will also attract
marks if the idea(s) involved show promise or substance.
Individual working and submission are required. Marks may be lost for late
submission. Staple your submission and number the pages in the style of this
document. Typewritten/word processed documents are a must in this case as
this is the evidence of your software working as required. In addition, you may
be asked to demonstrate and explain your software to me in person. For this
reason, the submission is not anonymous: write your name on each page and
staple the pages.
In your report, please reproduce all computer output using a typewriter font.
(Use the font Courier New if you use Microsoft WORD, and use the verbatim
environment if you use L ATEX.) The report should also include, as an appendix,
your source code in typewriter font. The source code should be presented pleasingly, with selfdocumenting names for the variables and predicates, and you
should use comments. The main body of the report should describe each predicate you have used and the reasons for defining them the way you have. For full
marks, not only the software should behave as specified but the report should
also be pleasing.
Hand out date: In Week 4, We. 10 February 2016, an electronic copy
(the pdf file Cw3_15-16.pdf) of this document will be made available on
Blackboard.
From: Dr. A. Csenki 1 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
Submission date: 29th February 2016, Monday, by 4.00 pm. (This day
is in Week 7.)
Submission to: Student Support Office, Horton Building.
Items to be submitted: Please submit two hardcopies of your full report
(incl. your source code) as specified by the regulations. DO NOT submit electronic solutions to anyone. Hardcopies (paper copies)
will be accepted only.
Furthermore, note that
The Linux version of SWI-Prolog is available in our School and you are
recommended to use this version to cut-and-paste your interactive session
into your document.
You should invoke SWI-Prolog from the command line terminal by simply
typing swipl.
You should use one single Prolog source file and the first line of this should
be the directive :- dynamic on/2, ..
To suppress the interactive display of the value of variables starting with
an underscore, you should issue the directive
:- set prolog flag(toplevel print anon, false).
Alternatively, you may issue the query
?- set prolog flag(toplevel print anon, false).
interactively. For example,
?- set prolog flag(toplevel print_anon, false).
true.
?- append([1,2,3], [4,5], L), append(_L, [6,7], Long).
Long = [1, 2, 3, 4, 5, 6, 7].
?- append([1,2,3], [4,5], L), append(L, [6,7], Long).
L = [1, 2, 3, 4, 5],
Long = [1, 2, 3, 4, 5, 6, 7].
You should embed the Prolog responses and your code into a narrative,
making your document a proper report.
Staple your submission and number the pages in the style if this
document.
From: Dr. A. Csenki 2 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
Some of the exercises need results from earlier ones. However, when solving such an exercise you may assume that the predicates from all previous exercises are available even if you have not defined the predicates concerned. In this case, you may test and demonstrate the workings of
your predicate by defining by facts the missing predicate.
The maximum achievable number of marks is 100; 10 marks for
each Task. marking advice
2 Your Tasks
A Prolog knowledge base uses the predicates desk/1, right_to/2 and on/2 to
describe the blocks world shown in Fig. 1.
on(b,c). on(d,e). on(e,g).
desk(a). desk(b). desk(d). desk(f).
right_to(a,b). right_to(b,d). right_to(d,f).
The predicates meaning are obvious:
desk/1 tells us which blocks rest on the desk.
on/2 tells us the names of the blocks resting on each other. Bear in mind
the order of the arguments.
right_to/1 tells us which of the base blocks are to the right of a neighbouring base block. Again, bear in mind the order of the arguments.
In the Appendix an interactive session is shown manipulating the database,
involving modelling of display, removal and addition of blocks. The steps shown
there exemplify the operation in the correct order of the predicates to be defined
by you. I have interspersed this dialogue with applications of listing/1 to
indicate the state of the Prolog database. This is for your assistance only.1
1Note that Prologs exact response will depend on what version of the SWI Prolog system
you are using. The response you will get on the School machine will look marginally different
from what I show you here as I have written this document on my computer at home using
an earlier version of SWI Prolog.
a b d f
c e
g
Figure 1: The blocks world.
From: Dr. A. Csenki 3 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
The predicates you are asked below to define should work not only for this
particular Prolog knowledge base but for all other similar knowledge bases.
(This is to prevent you from defining your predicates with reference to this
particular database only. That would be a pointless exercise as you hopefully
appreciate!)
You are asked below to write Prolog predicates carrying out these operations.
The predicates desk/1, on/2 and right_to/2 should be declared dynamic since
in the database their definitions will be changed. Therefore, the first line of your
file Cw3_15-16.pl (say) should be
:- dynamic on/2, desk/1, right_to/2.
Task 1
Define a predicate all_blocks/1 for finding out (in a list) all those blocks which
feature in the database. Use the built-in predicate setof/3.
Task 2
Define a predicate above/2 for finding out which blocks are situated above a
given block. It will on backtracking eventually return the name of all blocks
above a certain block which are in the same coloumn.
Task 3
Use setof/3 and the predicate from the previous exercise to get the list of all
blocks situated above a given one.
Task 4
Define a predicate rightmost/1 for finding the rightmost block resting on the
desk.
Task 5
Define a predicate place_on/2 for placing a new block on the desk or on top of
an existing block. (The assumption here is that if a new block is being placed
on the desk, then it will be placed to the right of all blocks already present.)
Fig 2 shows how the block are arranged after addition of the two new blocks h
and i.
Task 6
Define a predicate remove/1 for removing an existing block from the desk or
from the top of another block. Fig 3 shows how the blocks are arranged after
removal of the blocks g and f. Defining this predicate will involve some care as
there are several cases to be addressed. For example, what if you remove the last
From: Dr. A. Csenki 4 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
a b d f h
c e i
g
Figure 2: The blocks world after adding h and i.
a b d h
c e i
Figure 3: The blocks world after removal of g and f.
block from the table? Or, if you remove a block from the top of another block?
Or, if you remove one which rests on the table but to both sides of which there
are blocks etc. Obviously, careful consideration of all these cases is necessary.
(I have defined in total five clauses here.)
Now, there come four relatively easy exercises to bring this sheet to a conclusion.
Task 7
Define a predicate below/2 for finding out those blocks situated below a given
block. (This is a really easy exercise once we have above/2.)
Task 8
Define a predicate base/2 for finding out the base block of a given block, i.e.
the block at the base of the pile in which the given block is.
Notice that to make it more interesting in the last exercise, I also place here
block j on top of block i. Now the blocks are as shown in Fig 4.
From: Dr. A. Csenki 5 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
a b d h
j i
c e
Figure 4: The blocks world after addition of block j.
Task 9
Define a predicate right/2 for finding out for a base block which other base
blocks are to right of it.
Task 10
Define a predicate all_right_to/2 for finding all the blocks to the right of a
given block as a list.
From: Dr. A. Csenki 6 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
Appendix: Example Interactive Session
?- consult(Cw3 15-16.pl).
% Cw3 15-16.pl compiled 0.00 sec, 29 clauses
true.
?- listing([desk/1, right to/2, on/2]).
:- dynamic desk/1.
desk(a).
desk(b).
desk(d).
desk(f).
:- dynamic right to/2.
right_to(a, b).
right_to(b, d).
right_to(d, f).
:- dynamic on/2.
on(b, c).
on(d, e).
on(e, g).
true.
?- all blocks(All).
All = [a, b, c, d, e, f, g].
?- above(d,B).
B = e ;
B = g ;
false.
?- above(e,B).
B = g ;
false.
?- above(a,B).
false.
?- all above(d,Blocks).
Blocks = [e, g].
From: Dr. A. Csenki 7 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
?- rightmost(B).
B = f.
?- place on(desk,h).
true .
?- place on(h,i).
true.
?- listing([desk/1, right to/2, on/2]).
:- dynamic desk/1.
desk(a).
desk(b).
desk(d).
desk(f).
desk(h).
:- dynamic right to/2.
right_to(a, b).
right_to(b, d).
right_to(d, f).
right_to(f, h).
:- dynamic on/2.
on(b, c).
on(d, e).
on(e, g).
on(h, i).
true.
?- remove(g).
Block g removed.
true.
?- remove(f).
Block f removed.
true .
?- remove(d).
false.
?- below(e,B).
From: Dr. A. Csenki 8 of 9 Typeset with L ATEX
SDC/AI 3rd coursework 2015-2016 Programming in Prolog
B = d ;
false.
?- base(e,B).
B = d .
?- place on(i,j).
true.
?- base(j,B).
B = h .
?- right(c,R).
false.
?- right(b,R).
R = d ;
R = h ;
false.
?- all right to(c,R).
R = [d, e, h, i, j].
?-
From: Dr. A. Csenki 9 of 9 Typeset with L ATEX