Professional Documents
Culture Documents
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.
//
a q, !, r b
... c
//
!, r d
...
...
• 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 (\+).
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.