You are on page 1of 2

Logic Programming SuSe 22

Exercise Sheet 8 (due Wednesday, June 29, 2022, 16:00 pm)


Prof.aa
Dr. Jürgen Giesl Nils Lommen and Fabian Meyer

Notes:
• To solve the programming exercises you can use the Prolog interpreter SWI-Prolog, available for free
on this website. For Debian and Ubuntu it suffices to install the swi-prolog package. You can use the
command “swipl” to start it and use “[foo].” to load the clauses from the file foo.pl in the current
directory.
• Please solve these exercises in groups of four!
• Please upload a PDF with your solutions in a ZIP-archive via RWTHmoodle before the exercise course on
Wednesday, June 29, 2022, 16:00 pm. Please name your archive Sheet_i_Mat1_Mat2_Mat3_Mat4.zip,
where i is the number of the sheet and Mat_1. . . Mat_4 are the immatriculation numbers of the group
members in ascending order. Please make sure that only one of the group members uploads your
solution.
• Make sure that your solutions for the programming exercises are accepted by SWI-Prolog. Files which
are not accepted by SWI-Prolog will not be marked.

• The exercise course will take place on Wednesday, June 29, 2022, 16:30 pm in the lecture hall AH 4.

Exercise 1 (Cut): (3 points)


Consider the following Prolog program:
ite ( Cond , Then , _ ) : - Cond , ! , Then .
ite (_ ,_ , Else ) : - Else .

xor (X , Y ) : - ite (X , ite (Y , false , true ) , Y ).


Please give a graphical representation of the complete SLD tree for the query ?- xor(true,false). Here,
true/0 and false/0 are predefined predicates, where the proof of true immediately succeeds and the proof
of false fails. For every part of the tree that is cut off by evaluating !, please indicate the cut (as shown in
the example SLD tree below). For the cut-off parts only indicate the first cut-off goal, but do not evaluate
further (i.e., do not continue below b or d in the example SLD tree below).

//
a q, !, r b

... c
//
!, r d

...
...

Exercise 2 (Negation and Meta Variables): (5 points)


In the lecture the binary predicate or (;) was presented which makes use of meta variables. In this exercise
we want to extend this idea to the predicates exactlyOne, atLeast, and atMost.

• The formula exactlyOne(a1 , . . . am ) is true iff ai is true for exactly one 1 ≤ i ≤ m and for all 1 ≤ j ≤ m
with j ̸= i, aj is false.

1
Logic Programming SuSe 22
Exercise Sheet 8 (due Wednesday, June 29, 2022, 16:00 pm)

• The formula atLeastn (a1 , . . . , am ) is true iff ai is true for at least n indices 1 ≤ i ≤ m. For n > m,
atLeastn (a1 , . . . am ) is false.
• The formula atMostn (a1 , . . . am ) is true iff ai is true for at most n indices 1 ≤ i ≤ m. For n ≥ m,
atMostn (a1 , . . . am ) is true.

Please implement the predicates exactlyOne(XS), atLeast(N,XS), and atMost(N,XS) in Prolog where N is a
natural number (using the predefined Prolog numbers) and XS is a list (using the predefined data structure for
lists in Prolog). In addition to predefined arithmetic predicates and functions, you may also use cuts (!) and
negation (\+).

Exercise 3 (Input and Output): (12 points)


Please write a predicate rc/2 that reads a graph structure from a file, computes all nodes that are unreachable
from the start node of the first edge specified in the input file and writes back the result to another file. The
first argument of rc should be the input file, the second argument is the output file.
As an example, if the input file contains

edge(a,b).
edge(b,c).
edge(c,d).
edge(d,a).

edge(e,f).
edge(f,e).
then the output file must consist of the following facts (the order is irrelevant) to describe all nodes that are
not reachable from the start node a of the first listed edge from a to b:
unreachable(e).
unreachable(f).
You may use cuts (!) and negation (\+) as well as predefined predicates and predefined functions.
Hints:
• First collect all edges from the input file in a list.
• Create a list containing all start and end nodes of all edges. Remove all duplicates from this list.

• Then, define a predicate that tests whether there is a path from one node to another node in the given
graph that does not exceed a specified length (similar to Exercise 1 of Exercise Sheet 6).
• Finally, for all nodes n check whether there is a path from the first specified node to n. Write unreachable(n)
to the output file if this is not the case. Note that whenever there is a path from node a to node b, there
is also a path from a to b whose length does not exceed the number of nodes in the given graph.

You might also like