Prolog: Tutorial 5: Barbara Morawska
Prolog: Tutorial 5: Barbara Morawska
Barbara Morawska
1. p(a, b) assumption
2. p(a, c) assumption
3. p(b, d) assumption
4. p(c, e) assumption
5 g (X , Y ) : −p(X , Z ), p(Z , Y ). assumption
6. ?- g(a, W). goal
7. ?- p(a, Z), p(Z,W). 5, 6, θ1 : X /a, Y /W
8. ?- p(b,W). 1, 7, θ2 : Z /b
9. [] 3, 8, θ3 : W /d
The answer is: θ3 θ2 θ1 = X /a, W /d
SLD-refutation - exercises
I Draw a tree of all possible refutations of P ∪ {G }. Mark each
answer in the leaf.
I Check this tree using tracing and backtracking.
I Let natural numbers be constructed only from 0 and s-a successor
predicate: 0, s(0), s(s(0)), . . . .
I Define predicate add(X , Y , Z ) which is true if Z is the sum of X , Y .
I For example add(0, s(0), s(0)) should be true.
I Example of a goal ?- add(s(s(0)), s(0), A)
I Construct an SLD-refutation of this goal.
I Define predicate mult(X , Y , Z )
Cut predicate
Cut predicate: !
It is a nullary predicate that stops backtracking.
It has similar function as negation. Compare the following programs:
I With negation:
p(a).
p(b).
q(a).
r(b).
s(X) :- p(X), q(X).
s(X) :- \+p(X), r(X).
I With cut:
p(a).
p(b).
q(a).
r(b).
s(X) :- p(X),!, q(X).
s(X) :- r(X).
Cut predicate
a :- b, c.
a :- d, f.
b :- d, !, e.
b :- f.
c.
d :- g, h.
d.
e :- i, j.
e :- k.
f.