Solution

Logic Programming SS17
Solution - Exercise Sheet 1
Jera Hensel
Prof.aa
Dr. Jürgen Giesl
Exercise 1 (Simple Prolog):
(1.5 + 2 + 1.5 = 5 points)
Consider the following Prolog program.
evolvedFrom(cat,miacis).
evolvedFrom(hyena,miacis).
evolvedFrom(weasel,miacis).
evolvedFrom(cynodictis,miacis).
evolvedFrom(raccoon,cynodictis).
evolvedFrom(bear,cynodictis).
evolvedFrom(tomarctus,cynodictis).
evolvedFrom(fox,tomarctus).
evolvedFrom(wolf,tomarctus).
evolvedFrom(dog,tomarctus).
a) Implement a predicate evolvedFromSameCreature(A,B) in Prolog which is true if both A and B
evolved from the same creature according to the predicate evolvedFrom above. For example,
the query ?- evolvedFromSameCreature(fox,wolf) should yield the answer true, whereas
?- evolvedFromSameCreature(cat,dog) should yield false.
b) Implement a predicate descendsFrom(A,C) in Prolog which is true if A is a descendant of C, i.e., A either
directly evolved from C or A evolved from a descendant B of C.
Make sure that the evaluation of all queries ?- descendsFrom(...,...) terminates.
c) List all answers that Prolog gives for the following queries, in the order that Prolog gives them. Try to
solve this part of the exercise without the help of a computer.
1. ?- evolvedFrom(X,tomarctus).
2. ?- evolvedFromSameCreature(raccoon,X).
3. ?- descendsFrom(wolf,X).
Solution:
a) evolvedFromSameCreature(A,B) :- evolvedFrom(A,C), evolvedFrom(B,C).
b) descendsFrom(A,C) :- evolvedFrom(A,C).
descendsFrom(A,C) :- evolvedFrom(A,B), descendsFrom(B,C).
c) 1. X = fox ;
X = wolf ;
X = dog.
2. X = raccoon ;
X = bear ;
X = tomarctus.
3. X = tomarctus ;
X = cynodictis ;
X = miacis ;
false.
1
Logic Programming SS17
Solution - Exercise Sheet 1
Exercise 2 (Syntax):
(2 + 1 = 3 points)
Consider the following Prolog program.
eats(rabbit,grass).
eats(grasshopper,grass).
eats(mouse,grass).
eats(mouse,corn).
eats(mouse,grasshopper).
eats(fox,rabbit).
eats(fox,mouse).
plant(grass).
plant(corn).
animal(rabbit).
animal(grasshopper).
animal(mouse).
animal(fox).
has_enemy(X) :- animal(X), eats(Y,X).
competitors(X,Y) :- animal(X), animal(Y), eats(X,Z), eats(Y,Z).
a) Construct the corresponding sets of formulas, predicate symbols, function symbols, and variables based
on the program.
b) Give Prolog queries corresponding to the following questions:
• “Which plants does the mouse eat?”
• “Which competitors of the grasshopper do eat grasshoppers?”
Solution:
a) Φ = {
eats(rabbit,grass),
eats(grasshopper,grass),
eats(mouse,grass),
eats(mouse,corn),
eats(mouse,grasshopper),
eats(fox,rabbit),
eats(fox,mouse),
plant(grass),
plant(corn),
animal(rabbit),
animal(grasshopper),
animal(mouse),
animal(fox),
∀X, Y
∀X, Y, Z
animal(X) ∧ eats(Y ,X) → has_enemy(X),
animal(X) ∧ animal(Y ) ∧ eats(X,Z) ∧ eats(Y ,Z) → competitors(X,Y )
} over Σ = Σ0 = {grass, corn, rabbit, grasshopper, mouse, fox}, ∆1 = {plant, animal, has_enemy},
∆2 = {eats, competitors}, ∆ = ∆1 ∪ ∆2 , and V = {X, Y, Z}.
2
Logic Programming SS17
Solution - Exercise Sheet 1
b)
• ?- eats(mouse,X), plant(X).
• ?- competitors(grasshopper,X), eats(X,grasshopper).
Exercise 3 (Induction):
(3 points)
Let t be an arbitrary term. Then the size |t| of t is defined as follows. |X| = 1 if X is a variable. Otherwise we
have for n ≥ 0 that |f (t1 , . . . , tn )| = 1 + Σni=1 |ti |.
Show by structural induction that for every term t and every variable renaming σ we have |t| = |σ(t)|.
Solution:
Let t be an arbitrary term and σ be an arbitrary variable renaming. In the induction base, we consider the
case that t is a variable. Then we have |t| = 1. According to the definition of variable renamings, we have
σ(t) ∈ V with |σ(t)| = 1, so |t| = 1 = |σ(t)| holds.
In the induction step, we consider the case t = f (t1 , . . . , tn ) with n ≥ 0. As indution hypothesis, we can
assume that we have |ti | = |σ(ti )| for all i ∈ {1, . . . , n}. We have σ(t) = f (σ(t1 ), . . . , σ(tn )). We also know that
|t| = 1 + Σni=1 |ti |. Using the induction hypothesis, we then have |t| = 1 + Σni=1 |σ(ti )| = |σ(t)|.
Exercise 4 (Semantics):
(3 + 3 + 3 = 9 points)
Let (Σ, ∆) be a signature with Σ = Σ0 = {2, 6}, ∆ = ∆1 ∪ ∆3 , ∆1 = {even}, and ∆3 = {plus}.
Moreover, let
• Φ = {even(2), ∀X, Y, Z
• ϕ = ∀Y
even(X) ∧ even(Y ) ∧ plus(X, Y, Z) → even(Z)},
plus(2, Y, 6) ∧ even(6) → even(Y ),
• S = (N, α) with
– α2 = 2, α6 = 6,
– αplus = {(x, y, z) ∈ N3 | x + y = z},
– αeven = {2 ∗ i | i ∈ N}.
Prove or disprove the following statements.
You may use that addition on natural numbers is commutative.
a) S |= ϕ
b) |= ϕ
c) Φ |= ϕ
Solution:
3
Logic Programming SS17
Solution - Exercise Sheet 1
a) Let I = (N, α, β) for some variable assignment β. Since ϕ contains no free variables, we have:
S |= ϕ ⇔ I |= ϕ
⇔ I |= ∀Y plus(2, Y, 6) ∧ even(6) → even(Y )
⇔
I[[Y /y]] |= plus(2, Y, 6) ∧ even(6) → even(Y ) for all y ∈ N
⇔
I[[Y /y]] |= even(Y ) if I[[Y /y]] |= plus(2, Y, 6) ∧ even(6) for all y ∈ N
⇔
I[[Y /y]] |= even(Y ) if (I[[Y /y]] |= plus(2, Y, 6) and I[[Y /y]] |= even(6)) for all y ∈ N
⇔
y ∈ αeven if ((α2 , y, α6 ) ∈ αplus and α6 ∈ αeven ) for all y ∈ N
⇔
y ∈ {2 ∗ i | i ∈ N} if
((2, y, 6) ∈ {(x, y, z) ∈ N3 | x + y = z} and 6 ∈ {2 ∗ i | i ∈ N}) for all y ∈ N
⇔
y ∈ {2 ∗ i | i ∈ N} if (y = 4 and 6 ∈ {2 ∗ i | i ∈ N}) for all y ∈ N
⇔ y ∈ {2 ∗ i | i ∈ N} if y = 4
This implication is obviously true since 4 is divisible by two.
b) We choose the following interpretation I 0 = ({2, 3, 6}, α0 , β) with some variable assignment β and α20 =
0
0
= {2, 6}. Then we have:
2, α60 = 6, αplus
= {(2, 3, 6)}, αeven
I 0 |= ϕ ⇔ I 0 |= ∀Y plus(2, Y, 6) ∧ even(6) → even(Y )
⇔
I 0 [[Y /y]] |= plus(2, Y, 6) ∧ even(6) → even(Y ) for all y ∈ {2, 3, 6}
⇒ I 0 [[Y /3]] |= plus(2, Y, 6) ∧ even(6) → even(Y )
⇔
if I 0 [[Y /3]] |= plus(2, Y, 6) and I 0 [[Y /3]] |= even(6), then I 0 [[Y /3]] |= even(Y )
⇔
0
0
0
if (α20 , 3, α60 ) ∈ αplus
and α60 ∈ αeven
, then 3 ∈ αeven
⇔
if (2, 3, 6) ∈ {(2, 3, 6)} and 6 ∈ {2, 6}, then 3 ∈ {2, 6}
This is obviously wrong. Thus, the statement is disproved as I 0 is not a model of ϕ.
c) We choose the interpretation I 0 from the last exercise part again. Now we have:
I 0 |= even(2)
0
⇔ α20 ∈ αeven
⇔ 2 ∈ {2, 6}
This is obviously true.
Furthermore, we have:
⇔
⇔
I 0 |= ∀X, Y, Z even(X) ∧ even(Y ) ∧ plus(X, Y, Z) → even(Z)
I 0 [[X/x, Y /y, Z/z]] |= even(X) ∧ even(Y ) ∧ plus(X, Y, Z) → even(Z) for all x, y, z ∈ {2, 3, 6}
I 0 [[X/x, Y /y, Z/z]] |= even(Z) if I 0 [[X/x, Y /y, Z/z]] |= even(X) ∧ even(Y ) ∧ plus(X, Y, Z)
for all x, y, z ∈ {2, 3, 6}
⇔
⇔
⇔
I 0 [[X/x, Y /y, Z/z]] |= even(Z) if I 0 [[X/x, Y /y, Z/z]] |= even(X) and I 0 [[X/x, Y /y, Z/z]] |= even(Y )
and I 0 [[X/x, Y /y, Z/z]] |= plus(X, Y, Z) for all x, y, z ∈ {2, 3, 6}
0
0
0
0
z ∈ αeven
if x ∈ αeven
and y ∈ αeven
and (x, y, z) ∈ αplus
for all x, y, z ∈ {2, 3, 6}
z ∈ {2, 6} if x ∈ {2, 6} and y ∈ {2, 6} and (x, y, z) ∈ {(2, 3, 6)} for all x, y, z ∈ {2, 3, 6}
4
Logic Programming SS17
Solution - Exercise Sheet 1
There is no y ∈ {2, 6} such that (x, y, z) ∈ {(2, 3, 6)}. Therefore, the implication is true.
Hence, we have I 0 |= Φ. However, from part b) we know that I 0 2 ϕ. Thus, the statement is disproved.
5