On the laboratories PCs it is installed the SWI Prolog version 5.6.20. The Prolog interpreter can be started with the command plwin.exe
, more informations can be found in the SWI Prolog documentation or FAQ. Basically the main commands and features are:
| ?-
, you can ask queries or issue commands. Every line should be terminated by a full stop.end_of_file
.?- [test].
user
as program file. The program should be terminated by end_of_file
. or (Ctl-D).?- [user].
|: isEven(0).
|: isEven(s(s(X))) :- isEven(X).
|: % user://1 compiled 0.00 sec, 408 bytes
Yes
listing/0
shows the stored program. E.g.,?- listing.
% Foreign: rl_add_history/1
isEven(0).
isEven(s(s(A))) :-
isEven(A).
% Foreign: rl_read_init_file/1
Yes
Suppose we are working with the following knowledge base:
hasWand(harry).
quidditchPlayer(harry).
wizard(ron).
wizard(X) :- hasBroom(X),hasWand(X).
hasBroom(X) :- quidditchPlayer(X).
How does Prolog respond to the following queries? (use the SWI-Prolog to verify the answers)
wizard(ron).
wizard(hermione).
wizard(harry).
wizard(Y).
Find all the colour assignment for the map of Australia using the following Prolog code.
colourable([WA, SA, NT, Q, NSW, V, T]) :- rgb(WA), rgb(SA), rgb(NT), rgb(Q), rgb(NSW), rgb(V), rgb(T), WA \= NT, NT \= Q, Q \= NSW, NSW \= V, SA \= WA, SA \= NT, SA \= Q, SA \= NSW, SA \= V. rgb(red). rgb(green). rgb(blue).
Use the CLP(FD) solver of SWI-Prolog to solve the following problems. You can use the example shown during the lecture as a skeleton to write the solution. Examples, together with a description of the predicates available in the CLP(FD) library, can be found here.
Remember to include the line
use_module(library('clp/bounds')).
Find the solution to the cryptarithmetic puzzle TWO + TWO = FOUR
by encoding the problem using the CLP(FD) solver of the SWI-Prolog.
A scheduling problem consists in a given set of tasks with associated duration; in addition, tasks may have precedence constraints among them (e.g. debugging must be performed after the code has been written). The purpose of this exercise is to show how to use a Constraint Logic Program system to devise a scheduling.
In this lab we use the following encoding of the problem into CSP over the integer domain:
Consider the following scheduling problem with 7 different tasks:
Task | Duration |
t1 | 16 |
t2 | 6 |
t3 | 13 |
t4 | 7 |
t5 | 5 |
t6 | 18 |
t7 | 4 |
and the following precedence relation: