© patrick blackburn, johan bos & kristina striegnitz lecture 10 exercises –solutions to...
Post on 19-Dec-2015
214 views
TRANSCRIPT
![Page 1: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/1.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Lecture 10
• Exercises– Solutions to Exercises of LPN chapter 9
• Theory– Explain how to control Prolog`s
backtracking behaviour with the help of the cut predicate
– Introduce negation – Explain how cut can be packaged into a
more structured form, namely negation as failure
![Page 2: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/2.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Solution to Exercise 9.3
Write a two-place predicate termtype(+Term,?Type) that takes a term and gives back the type(s) of that term (atom, number, constant, variable etc.). The types should be given back in the order of their generality.
termtype(Term,Type) :- atom(Term), Type = atom; number(Term), Type = number; atomic(Term), Type = constant; atomic(Term), Type = simple_term; var(Term), Type = variable; var(Term), Type = simple_term; not(atomic(Term)), nonvar(Term), Type = complex_term; Type = term.
![Page 3: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/3.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Solution to Exercise 9.5
Which of the following is a well-formed term? What is the main operator? Give the bracketing.
:- op(300, xfx, [are, is_a]).:- op(300, fx, likes).:- op(200, xfy, and).:- op(100, fy, famous).
?- X is_a witch.is_a(X,witch).
?- harry and ron and hermione are friends.are(and(harry,and(ron,hermione)),friends).
?- harry is_a wizard and likes quidditch.ERROR: Syntax error: Operator priority clash (is_a and likes have same precedence)
?- dumbledore is_a famous famous wizard.is_a(dumbledore, famous(famous(wizard)))
![Page 4: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/4.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Lecture 10: Cuts and Negation
• Theory– Explain how to control Prolog`s
backtracking behaviour with the help of the cut predicate
– Introduce negation – Explain how cut can be packaged into a
more structured form, namely negation as failure
![Page 5: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/5.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
The Cut
• Backtracking is a characteristic feature of Prolog
• But backtracking can lead to inefficiency:– Prolog can waste time exploring
possibilities that lead nowhere– It would be nice to have some control
• The cut predicate !/0 offers a way to control backtracking
![Page 6: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/6.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example of cut
• The cut is a Prolog predicate, so we can add it to rules in the body:– Example:
p(X):- b(X), c(X), !, d(X), e(X).
• Cut is a goal that always succeeds• Commits Prolog to the choices that
were made since the parent goal was called
![Page 7: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/7.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Explaining the cut
• In order to explain the cut, we will– Look at a piece of cut-free Prolog code
and see what it does in terms of backtracking
– Add cuts to this Prolog code– Examine the same piece of code with
added cuts and look how the cuts affect backtracking
![Page 8: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/8.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
![Page 9: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/9.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
?- p(X).
![Page 10: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/10.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
?- p(X).
![Page 11: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/11.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
?- p(X).
?- a(X).
![Page 12: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/12.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1
?- p(X).
?- a(X).
X=1
![Page 13: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/13.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),d(X),e(X).
X=1
![Page 14: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/14.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),d(X),e(X).
X=1
![Page 15: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/15.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- c(1),d(1),e(1).
?- b(X),c(X),d(X),e(X).
X=1 X=1
![Page 16: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/16.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- b(X),c(X),d(X),e(X).
X=1 X=1
![Page 17: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/17.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- b(X),c(X),d(X),e(X).
X=1 X=1
†
![Page 18: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/18.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(1), e(1).
?- c(1),d(1),e(1).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 19: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/19.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 20: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/20.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(2), e(2).?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 21: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/21.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
?- p(X).
?- a(X).
?- d(2), e(2).
?- e(2).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 22: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/22.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
X=2
?- p(X).
?- a(X).
?- d(2), e(2).
?- e(2).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 23: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/23.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
X=2;
?- p(X).
?- a(X). ?- f(X).
?- d(2), e(2).
?- e(2).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
![Page 24: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/24.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
X=2;
X=3
?- p(X).
?- a(X). ?- f(X).
?- d(2), e(2).
?- e(2).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
X=3
![Page 25: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/25.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut-free code
p(X):- a(X). p(X):- b(X), c(X), d(X), e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
X=1;
X=2;
X=3;
no
?- p(X).
?- a(X). ?- f(X).
?- d(2), e(2).
?- e(2).
?- d(1), e(1).
?- c(1),d(1),e(1). ?- c(2),d(2),e(2).
?- b(X),c(X),d(X),e(X).
X=1 X=1X=2
†
X=3
![Page 26: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/26.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Adding a cut
• Suppose we insert a cut in the second clause:
• If we now pose the same query we will get the following response:
p(X):- b(X), c(X), !, d(X), e(X).
?- p(X).
X=1;
no
![Page 27: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/27.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
![Page 28: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/28.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- p(X).
?- p(X).
![Page 29: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/29.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
?- p(X).
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
![Page 30: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/30.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1
?- p(X).
?- a(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
![Page 31: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/31.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
![Page 32: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/32.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
![Page 33: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/33.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- c(1),!,d(1),e(1).
X=1
![Page 34: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/34.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- c(1), !, d(1), e(1).
X=1
?- !, d(1), e(1).
![Page 35: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/35.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- c(1), !, d(1), e(1).
X=1
?- !, d(1), e(1).
?- d(1), e(1).
X
X
![Page 36: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/36.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Example: cut
?- p(X).
X=1;
no
?- p(X).
?- a(X). ?- b(X),c(X),!,d(X),e(X).
X=1
p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X).p(X):- f(X). a(1).b(1). b(2).c(1). c(2).d(2).e(2).f(3).
?- c(1), !, d(1), e(1).
X=1
?- !, d(1), e(1).
?- d(1), e(1).
†
![Page 37: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/37.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
What the cut does
• The cut only commits us to choices made since the parent goal was unified with the left-hand side of the clause containing the cut
• For example, in a rule of the form
q:- p1, … , pn, !, r1, … , rn.
when we reach the cut it commits us:– to this particular clause of q
– to the choices made by p1, … , pn
– NOT to choices made by r1, … , rn
![Page 38: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/38.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Using Cut
• Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two
max(X,Y,Y):- X =< Y.max(X,Y,X):- X>Y.
![Page 39: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/39.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Using Cut
• Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two
max(X,Y,Y):- X =< Y.max(X,Y,X):- X>Y.
?- max(2,3,3).
yes
?- max(7,3,7).
yes
![Page 40: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/40.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Using Cut
• Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two
max(X,Y,Y):- X =< Y.max(X,Y,X):- X>Y.
?- max(2,3,2).
no
?- max(2,3,5).
no
![Page 41: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/41.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
The max/3 predicate
• What is the problem?
• There is a potential inefficiency– Suppose it is called with ?- max(3,4,Y).– It will correctly unify Y with 4– But when asked for more solutions, it will
try to satisfy the second clause. This is completely pointless!
max(X,Y,Y):- X =< Y.max(X,Y,X):- X>Y.
![Page 42: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/42.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
max/3 with cut
• With the help of cut this is easy to fix
• Note how this works:– If the X =< Y succeeds, the cut commits us
to this choice, and the second clause of max/3 is not considered
– If the X =< Y fails, Prolog goes on to the second clause
max(X,Y,Y):- X =< Y, !.max(X,Y,X):- X>Y.
![Page 43: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/43.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Green Cuts
• Cuts that do not change the meaning of a predicate are called green cuts
• The cut in max/3 is an example of a green cut: – the new code gives exactly the same
answers as the old version, – but it is more efficient
![Page 44: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/44.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another max/3 with cut
• Why not remove the body of the second clause? After all, it is redundant.
• How good is it?
max(X,Y,Y):- X =< Y, !.max(X,Y,X).
![Page 45: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/45.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another max/3 with cut
• Why not remove the body of the second clause? After all, it is redundant.
• How good is it?– okay
max(X,Y,Y):- X =< Y, !.max(X,Y,X).
?- max(200,300,X).
X=300
yes
![Page 46: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/46.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another max/3 with cut
• Why not remove the body of the second clause? After all, it is redundant.
• How good is it?– okay
max(X,Y,Y):- X =< Y, !.max(X,Y,X).
?- max(400,300,X).
X=400
yes
![Page 47: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/47.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another max/3 with cut
• Why not remove the body of the second clause? After all, it is redundant.
• How good is it?– oops….
max(X,Y,Y):- X =< Y, !.max(X,Y,X).
?- max(200,300,200).
yes
![Page 48: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/48.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Revised max/3 with cut
• Unification after crossing the cut
• This does work
max(X,Y,Z):- X =< Y, !, Y=Z.max(X,Y,X).
?- max(200,300,200).
no
![Page 49: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/49.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Red Cuts
• Cuts that change the meaning of a predicate are called red cuts
• The cut in the revised max/3 is an example of a red cut: – If we take out the cut, we don’t get an
equivalent program
• Programs containing red cuts– Are not fully declarative– Can be hard to read– Can lead to subtle programming mistakes
![Page 50: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/50.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another built-in predicate: fail/0
• As the name suggests, this is a goal that will immediately fail when Prolog tries to prove it
• That may not sound too useful
• But remember: when Prolog fails, it tries to backtrack
![Page 51: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/51.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers
• The cut fail combination allows to code exceptions
enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
![Page 52: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/52.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers
• The cut fail combination allows to code exceptions
enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
?- enjoys(vincent,a).
yes
![Page 53: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/53.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers
• The cut fail combination allows to code exceptions
enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
?- enjoys(vincent,b).
no
![Page 54: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/54.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers
• The cut fail combination allows to code exceptions
enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
?- enjoys(vincent,c).
yes
![Page 55: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/55.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers
• The cut fail combination allows to code exceptions
enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
?- enjoys(vincent,d).
yes
![Page 56: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/56.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Negation as Failure
• The cut-fail combination seems to be offering us some form of negation
• It is called negation as failure, and defined as follows:
neg(Goal):- Goal, !, fail. neg(Goal).
![Page 57: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/57.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers revisited
enjoys(vincent,X):- burger(X), neg(bigKahunaBurger(X)).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
![Page 58: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/58.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Vincent and burgers revisited
enjoys(vincent,X):- burger(X), neg(bigKahunaBurger(X)).
burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).
bigMac(a). bigKahunaBurger(b).bigMac(c).whopper(d).
?- enjoys(vincent,X).
X=a
X=c
X=d
![Page 59: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/59.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Another built-in predicate: \+
• Because negation as failure is so often used, there is no need to define it
• In standard Prolog the prefix operator \+ means negation as failure
• So we could define Vincent`s preferences as follows:
enjoys(vincent,X):- burger(X), \+ bigKahunaBurger(X).
?- enjoys(vincent,X).
X=a
X=c
X=d
![Page 60: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/60.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Negation as failure and logic
• Negation as failure is not logical negation
• Changing the order of the goals in the vincent and burgers program gives a different behaviour:
enjoys(vincent,X):- \+ bigKahunaBurger(X), burger(X).
?- enjoys(vincent,X).
no
![Page 61: © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s](https://reader038.vdocuments.us/reader038/viewer/2022103123/56649d375503460f94a0fd6b/html5/thumbnails/61.jpg)
© P
atr
ick B
lackb
urn
, Jo
han
Bos &
Kri
sti
na S
trie
gn
itz
Next lecture
• Exercises– 10.1, 10.2
• Theory– Database Manipulation and Collecting
Solutions• Discuss data manipulation in Prolog• Introduce built-in predicates that let us collect
all solutions into a single list