SlideShare a Scribd company logo
Fall 2008 Prolog: Negation
The current topic: Prolog
! Introduction
! Object-oriented programming: Python
! Functional programming: Scheme
! Python GUI programming (Tkinter)
! Types and values
• Logic programming: Prolog
! Introduction
! Rules, unification, resolution, backtracking, lists.
! More lists, math, structures.
! More structures, trees, cut.
– Next up: Negation.
• Syntax and semantics
• Exceptions
1 Fall 2008 Prolog: Negation
Announcements
• Term Test 2 has been marked.
– Handed back at the end of class today.
– The deadline for submitting a re-mark request is the end of class, Friday
November 28th. Make sure you understand the posted solutions before
submitting a re-mark request.
– Average: 68.4%
2
Fall 2008 Prolog: Negation
Using not instead of cut to avoid wrong answers
• Prolog has a not operator, but its behaviour is more subtle than in other
languages.
• Example: Replacing a cut with not:
– With cut:
A :- B, !, C.
A :- D.
– With not:
A :- B, C.
A :- not(B), D.
• Observe that not can be more cumbersome than cut.
– repetitive if-then-else
3 Fall 2008 Prolog: Negation
Using not for inequality
• Example:
crispy(snap).
crispy(crackle).
crispy(pop).
breakfast(A,B,C) :- crispy(A), crispy(B), crispy(C).
?- breakfast(A,B,C).
A = snap
B = snap
C = snap ;
A = snap
B = snap
C = crackle ;
...
• But we really want A, B, and C to be different from each other.
4
Fall 2008 Prolog: Negation
Using not for inequality
crispy(snap).
crispy(crackle).
crispy(pop).
breakfast(A,B,C) :- crispy(A), crispy(B), crispy(C), not(A=B),
not(A=C), not(B=C).
?- breakfast(A,B,C).
A = snap
B = crackle
C = pop ;
A = snap
B = pop
C = crackle ;
...
5 Fall 2008 Prolog: Negation
Negation in Prolog
• not(B) can also be written as + B.
– And not(X=Y) can also be written as X = Y.
• The goal + X succeeds iff X fails.
• Examples:
?- + member(b, [a,b,c]).
No
?- + member(x, [a,b,c]).
Yes
?- + member(X, [a,b,c]).
No
6
Fall 2008 Prolog: Negation
Negation in Prolog
?- + member(X, [a,b,c]).
No
• It might look like this query is asking "Does there exist an X for which
member(X, [a,b,c]) does not succeed?".
– We know there are lots of values of X for which member(X, [a,b,c]) does not
succeed.
– But that's not what negation means in Prolog.
– There exists X for which member(X, [a,b,c]) succeeds.
– So then + member(X, [a,b,c]) fails.
7 Fall 2008 Prolog: Negation
Negation as failure
• Prolog assumes that if it can't prove an assertion, then the assertion is
false.
– And Prolog assumes that if it can prove an assertion, then the assertion is true.
• This is the "closed world assumption": in the universe of facts Prolog
knows about, failure to prove is proof of failure.
– But if we know something Prolog doesn't, this can lead to surprises: things that
Prolog thinks are false when we know they're true, and the opposite.
– Example:
university(uoft).
?- university(york).
No
?- + university(york).
Yes
8
Fall 2008 Prolog: Negation
?- sad(michael).
No
?- sad(jim).
Yes
?- sad(Someone).
No
• Isn't anyone sad?
• No, that just means that it's
not true we can't find anyone
happy.
– In other words, there exists
someone who is happy.
Be careful with negation
sad(X) :- + happy(X).
happy(X) :- beautiful(X), rich(X).
rich(bill).
beautiful(michael).
rich(michael).
beautiful(cinderella).
?- sad(bill).
Yes
?- sad(cinderella).
Yes
9 Fall 2008 Prolog: Negation
Tracing negation
• Let's look at how Prolog answers sad(Someone).:
?- sad(Someone).
Call: (7) sad(_G283) ? creep
Call: (8) happy(_G283) ? creep
Call: (9) beautiful(_G283) ? creep
Exit: (9) beautiful(michael) ? creep
Call: (9) rich(michael) ? creep
Exit: (9) rich(michael) ? creep
Exit: (8) happy(michael) ? creep
Fail: (7) sad(_G283) ? creep
No
10
Fall 2008 Prolog: Negation
Set overlap
• Write a predicate overlap(S1, S2) that succeeds if lists S1 and S2
have a common element. Then write a predicate disjoint(S1, S2)
that succeeds if S1 and S2 have no common element.
overlap(S1, S2) :- member(X, S1), member(X, S2).
disjoint(S1, S2) :- + overlap(S1, S2).
?- overlap([a,b,c], [c,d,e]).
Yes
?- disjoint([a,b,c], [c,d,e]).
No
?- overlap([a,b,c], [d,e,f]).
No
?- disjoint([a,b,c], [d,e,f]).
Yes
?- disjoint([a,b,d], S).
No
11 Fall 2008 Prolog: Negation
What does that mean?
?- disjoint([a,b,d], S).
No
• The query should mean "can you find a list S that is disjoint from the list
[a,b,d] (and if so what is it)?".
• Obviously there are many such sets, so why "No"?
• Answer: because Prolog succeeded in finding a set that did overlap with
S, so it announced failure of the original query.
?- overlap([a,b,d], S).
S = [a|_G226] ;
S = [_G225, a|_G229] ;
S = [_G225, _G228, a|_G232] ;
...
12
Fall 2008 Prolog: Negation
Safe use of negation
• The goal not(G) is safe if either:
– G is fully instantiated when not(G) is processed, or
– G has uninstantiated variables, but they don't appear anywhere else in the clause.
• Safe example:
childlessMan(X) :- male(X), + parent(X,Y).
– X is instantiated in male(X), and Y isn't used elsewhere.
• Unsafe example:
childlessMan(X) :- + parent(X,Y), male(X).
– X is not instantiated before the negation, and is used elsewhere.
• If necessary, add a precondition to warn the programmer.
– recall that +Var means that Var must be instantiated.
% disjoint(+S1, +S2) succeeds if...
disjoint(S1, S2) :- + overlap(S1, S2).
– When the precondition is satisfied, this negation is safe.
13 Fall 2008 Prolog: Negation
Double-negation doesn't "cancel out"
• In other languages, not(not(<expression>)) is equivalent to
<expression>.
– But not in Prolog.
?- member(X,[a,b,c]).
X = a ;
X = b ;
X = c ;
No
?- not(not(member(X,[a,b,c]))).
X = _G166 ;
No
14
Fall 2008 Prolog: Negation
Double-negation doesn't "cancel out"
?- not(not(member(X,[a,b,c]))).
X = _G166 ;
No
• Why is X uninstantiated in this example?
– Since member(X, [a,b,c]) succeeds (by instantiating X to, say, a),
not(member(X, [a,b,c])) fails.
– When a goal fails, the variables it instantiated get uninstantiated. So X gets
uninstantiated.
– But since not(member(X, [a,b,c])) fails, not(not(member(X,
[a,b,c]))) succeeds.
15 Fall 2008 Prolog: Negation
fail
• The fail predicate fails immediately. Example:
p(X) :- fail.
?- p(csc326).
No
• We can use fail to state that something is false.
16
Fall 2008 Prolog: Negation
fail
• Example: We want to represent "Colbert does not like bears (regardless
of whatever else he likes)."
– One solution: Add "not(bear(X))" to every rule describing what Colbert likes.
For example:
likes(colbert, X) :- animal(X), not(bear(X)).
likes(colbert, X) :- toy(X), not(bear(X)).
likes(colbert, X) :- livesInArctic(X), not(bear(X)).
...
– Let's try to use fail instead.
– First attempt:
bear(yogi).
animal(yogi).
likes(colbert, X) :- bear(X), fail.
likes(colbert, X) :- animal(X).
?- likes(colbert, yogi).
Yes
17 Fall 2008 Prolog: Negation
fail
• We need to add a cut to prevent other rules from being tried after the
first rule reaches fail.
– Second attempt:
bear(yogi).
cat(tom).
animal(yogi).
animal(tom).
likes(colbert, X) :- bear(X), !, fail.
likes(colbert, X) :- animal(X).
?- likes(colbert, yogi).
No
?- likes(colbert, tom).
Yes
?- likes(colbert, X).
No
– Downside: This solution only works when X is instantiated.
18
Fall 2008 Prolog: Negation
fail
• Another example: Define a predicate different(X, Y) that succeeds
if X and Y don't unify.
different(X, Y) :- X=Y, !, fail.
different(_, _).
?- different(a, b).
Yes
?- different(a, a).
No
• Notice that the above definition is equivalent to:
different(X, Y) :- not(X=Y).
19 Fall 2008 Prolog: Negation
Defining "not" using cut and fail
• We can define the not predicate as follows:
not(X) :- X, !, fail.
not(_).
• (To test this out, use a name other than "not", since Prolog won't let
you redefine the built-in "not").
20
Fall 2008 Prolog: Negation
fail
• Recall the original version of bstmem(Tree, X):
bstmem(node(X, _, _), X).
bstmem(node(K, L, _), X) :- X < K, bstmem(L, X).
bstmem(node(K, _, R), X) :- X > K, bstmem(R, X).
• Recall that this version was inefficient.
21 Fall 2008 Prolog: Negation
Inefficiency in bstmem
[trace] ?- bstmem(node(5, node(3,empty,empty), emtpy), 1).
Call: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ?
creep
^ Call: (9) 1<5 ? creep
^ Exit: (9) 1<5 ? creep
Call: (9) bstmem(node(3, empty, empty), 1) ? creep
^ Call: (10) 1<3 ? creep
^ Exit: (10) 1<3 ? creep
Call: (10) bstmem(empty, 1) ? creep
Fail: (10) bstmem(empty, 1) ? creep
Redo: (9) bstmem(node(3, empty, empty), 1) ? creep
^ Call: (10) 1>3 ? creep
^ Fail: (10) 1>3 ? creep
Redo: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ?
creep
^ Call: (9) 1>5 ? creep
^ Fail: (9) 1>5 ? creep
No
22
Fall 2008 Prolog: Negation
fail
• We solved the inefficiency illustrated on the previous slide as follows:
bstmem(node(X, _, _), X).
bstmem(node(K, L, _), X) :- X < K, !, bstmem(L, X).
bstmem(node(K, _, R), X) :- X > K, bstmem(R, X).
• What if we try to instead solve this inefficiency by using fail:
bstmem(empty,_) :- !, fail.
bstmem(node(X, _, _), X).
bstmem(node(K, L, _), X) :- X < K, bstmem(L, X).
bstmem(node(K, _, R), X) :- X > K, bstmem(R, X).
23 Fall 2008 Prolog: Negation
Tracing the new bstmem
[trace] ?- bstmem(node(5, node(3,empty,empty), emtpy), 1).
Call: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep
^ Call: (9) 1<5 ? creep
^ Exit: (9) 1<5 ? creep
Call: (9) bstmem(node(3, empty, empty), 1) ? creep
^ Call: (10) 1<3 ? creep
^ Exit: (10) 1<3 ? creep
Call: (10) bstmem(empty, 1) ? creep
Call: (11) fail ? creep
Fail: (11) fail ? creep
Fail: (10) bstmem(empty, 1) ? creep
Redo: (9) bstmem(node(3, empty, empty), 1) ? creep
^ Call: (10) 1>3 ? creep
^ Fail: (10) 1>3 ? creep
Redo: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep
^ Call: (9) 1>5 ? creep
^ Fail: (9) 1>5 ? creep
No
24
Fall 2008 Prolog: Negation
fail
• What went wrong?
– fail only affects the present goal (bstmem(empty, 1) in the example).
– It does not directly cause the failure of a previous goal (so, in the example, Prolog
still looks for other rules for the goal bstmem(node(3,empty,empty), 1)).
25 Fall 2008 Prolog: Negation
Advice on writing Prolog
To minimize bugs, especially with cut and not:
• Use cut and not as necessary to avoid wrong answers.
• Follow the rules for safe use of not.
• Follow the rules for doing arithmetic.
• Always use ";" when testing to check all possible answers.
– It's easy to get first answer right and rest wrong if "else" misused.
• Test with variables in every combination of positions.
• Use preconditions to state where variables are disallowed.
• Use cut to avoid duplicate answers.
• Use cut where possible for efficiency.
• Use _ where possible for efficiency.
26
Fall 2008 Prolog: Negation
Summary: logic programming and Prolog
• Logic programming:
– Unification, resolution, backtracking.
– Specify kind of result wanted (what you want), not how to get it.
• Prolog:
– The major logic programming language.
– Efficiency can be a worry:
• cut
• ordering the predicates
27 Fall 2008 Prolog: Negation
Bubble sort
• Write a predicate bsort(+Before, ?After) that succeeds if After is
a sorted version of Before. bsort should use bubble sort to sort the list.
bsort(Before, After) :- bsortaux(Before, [], After).
• Helper predicate bsortaux(+Prelower, +Preupper, ?Sorted)
succeeds if Sorted is a list that consists of a sorted version of
Prelower followed by (an unchanged) Preupper.
bsortaux([], Preupper, Preupper) :- !.
bsortaux(Prelower, Preupper, Sorted) :-
bubble(Prelower, Preupper, Postlower, Postupper),
bsortaux(Postlower, Postupper, Sorted).
28
Fall 2008 Prolog: Negation
Bubble sort
• Helper predicate bubble(+Prelower, +Preupper, ?Postlower, ?Postupper)
succeeds if performing one round of bubble sort on unsorted portion
Prelower and sorted portion Preupper results in unsorted portion
Postlower and sorted portion Postupper.
bubble([X, Y | Rest], Preupper, [X | Bubbled], Postupper) :-
X =< Y, % No swap needed.
!,
bubble([Y | Rest], Preupper, Bubbled, Postupper).
bubble([X, Y | Rest], Preupper, [Y | Bubbled], Postupper) :-
bubble([X | Rest], Preupper, Bubbled, Postupper).
bubble([X], Preupper, [], [X|Preupper]) :- !.
bubble([], Preupper, [], Preupper). % not needed, we hope
29 Fall 2008 Prolog: Negation
Tracing bsort
[trace] ?- bsort([2,1], S).
Call: (7) bsort([2, 1], _G290) ? creep
Call: (8) bsortaux([2, 1], [], _G290) ? creep
Call: (9) bubble([2, 1], [], _L206, _L207) ? creep
^ Call: (10) 2=<1 ? creep
^ Fail: (10) 2=<1 ? creep
Redo: (9) bubble([2, 1], [], _L206, _L207) ? creep
Call: (10) bubble([2], [], _G346, _L207) ? creep
Exit: (10) bubble([2], [], [], [2]) ? creep
Exit: (9) bubble([2, 1], [], [1], [2]) ? creep
Call: (9) bsortaux([1], [2], _G290) ? creep
Call: (10) bubble([1], [2], _L245, _L246) ? creep
Exit: (10) bubble([1], [2], [], [1, 2]) ? creep
Call: (10) bsortaux([], [1, 2], _G290) ? creep
Exit: (10) bsortaux([], [1, 2], [1, 2]) ? creep
Exit: (9) bsortaux([1], [2], [1, 2]) ? creep
Exit: (8) bsortaux([2, 1], [], [1, 2]) ? creep
Exit: (7) bsort([2, 1], [1, 2]) ? creep
S = [1, 2]
30
Fall 2008 Prolog: Negation
Tracing bsort
[trace] ?- bsort([3,2,1], S).
Call: (7) bsort([3, 2, 1], _G293) ? creep
Call: (8) bsortaux([3, 2, 1], [], _G293) ? creep
Call: (9) bubble([3, 2, 1], [], _L206, _L207) ? creep
^ Call: (10) 3=<2 ? creep
^ Fail: (10) 3=<2 ? creep
Redo: (9) bubble([3, 2, 1], [], _L206, _L207) ? creep
Call: (10) bubble([3, 1], [], _G352, _L207) ? creep
^ Call: (11) 3=<1 ? creep
^ Fail: (11) 3=<1 ? creep
Redo: (10) bubble([3, 1], [], _G352, _L207) ? creep
Call: (11) bubble([3], [], _G358, _L207) ? creep
Exit: (11) bubble([3], [], [], [3]) ? creep
Exit: (10) bubble([3, 1], [], [1], [3]) ? creep
Exit: (9) bubble([3, 2, 1], [], [2, 1], [3]) ? creep
31 Fall 2008 Prolog: Negation
Tracing bsort
Call: (9) bsortaux([2, 1], [3], _G293) ? creep
Call: (10) bubble([2, 1], [3], _L266, _L267) ? creep
^ Call: (11) 2=<1 ? creep
^ Fail: (11) 2=<1 ? creep
Redo: (10) bubble([2, 1], [3], _L266, _L267) ? creep
Call: (11) bubble([2], [3], _G367, _L267) ? creep
Exit: (11) bubble([2], [3], [], [2, 3]) ? creep
Exit: (10) bubble([2, 1], [3], [1], [2, 3]) ? creep
Call: (10) bsortaux([1], [2, 3], _G293) ? creep
Call: (11) bubble([1], [2, 3], _L305, _L306) ? creep
Exit: (11) bubble([1], [2, 3], [], [1, 2, 3]) ? creep
Call: (11) bsortaux([], [1, 2, 3], _G293) ? creep
Exit: (11) bsortaux([], [1, 2, 3], [1, 2, 3]) ? creep
Exit: (10) bsortaux([1], [2, 3], [1, 2, 3]) ? creep
Exit: (9) bsortaux([2, 1], [3], [1, 2, 3]) ? creep
Exit: (8) bsortaux([3, 2, 1], [], [1, 2, 3]) ? creep
Exit: (7) bsort([3, 2, 1], [1, 2, 3]) ? creep
S = [1, 2, 3]
32
Fall 2008 Prolog: Negation
Exercises
• Fix the sibling predicate (that we previously defined) so that it doesn't
consider a person to be there own sibling. Then make sure that this fix
has eliminated any unusual behaviour in the aunt, uncle, nephew, and
niece predicates that you defined in a previous set of exercises.
• Trace bsort on more interesting (and larger) examples. For example,
trace the call:
bsort([1,5,2,6,3,4], S).
• Challenge: Recall that the efficiency of bubble sort can be improved by
halting after the first iteration during which no swaps are performed (we
can halt at that point since if no swaps are performed, the list must be
already sorted). Modify bsort by adding this improvement.
33

More Related Content

Viewers also liked

Backtracking
BacktrackingBacktracking
Backtracking
Sulman Bin Khurshid
 
Monkey tails
Monkey tailsMonkey tails
Monkey tails
mo nkey
 
Monkey tails real version
Monkey tails real versionMonkey tails real version
Monkey tails real version
mo nkey
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of ada
Sahil Kumar
 
Monkey & banana problem in AI
Monkey & banana problem in AIMonkey & banana problem in AI
Monkey & banana problem in AI
Manjeet Kamboj
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In Prolog
DataminingTools Inc
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
DataminingTools Inc
 
Backtracking
BacktrackingBacktracking
Backtracking
subhradeep mitra
 

Viewers also liked (9)

Backtracking
BacktrackingBacktracking
Backtracking
 
Monkey tails
Monkey tailsMonkey tails
Monkey tails
 
Monkey tails real version
Monkey tails real versionMonkey tails real version
Monkey tails real version
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of ada
 
Monkey & banana problem in AI
Monkey & banana problem in AIMonkey & banana problem in AI
Monkey & banana problem in AI
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In Prolog
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
 
Backtracking
BacktrackingBacktracking
Backtracking
 

Similar to 20 prolog5

09 logic programming
09 logic programming09 logic programming
09 logic programming
saru40
 
Declarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere MortalsDeclarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere Mortals
Bertram Ludäscher
 
Cut and Goal on prolog
Cut and Goal on prologCut and Goal on prolog
Cut and Goal on prolog
chauhankapil
 
21CSC206T_UNIT 5.pptx artificial intelligence
21CSC206T_UNIT 5.pptx artificial intelligence21CSC206T_UNIT 5.pptx artificial intelligence
21CSC206T_UNIT 5.pptx artificial intelligence
ANTOARA2211003040050
 
Prolog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In PrologProlog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In Prolog
PROLOG CONTENT
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
Aarsh Ps
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach us
Daniel Sobral
 
Midterm sols
Midterm solsMidterm sols
Midterm sols
Robert Edwards
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
saru40
 
Query Rewriting and Optimization for Ontological Databases
Query Rewriting and Optimization for Ontological DatabasesQuery Rewriting and Optimization for Ontological Databases
Query Rewriting and Optimization for Ontological Databases
Giorgio Orsi
 
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof..."Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
Paris Women in Machine Learning and Data Science
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
Tendayi Mawushe
 
Signals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptxSignals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptx
Matlab Assignment Experts
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework Help
Excel Homework Help
 
Midterm
MidtermMidterm
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
kyleburton
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
Aarsh Ps
 
Prolog2 (1)
Prolog2 (1)Prolog2 (1)
Structured regression for efficient object detection
Structured regression for efficient object detectionStructured regression for efficient object detection
Structured regression for efficient object detection
zukun
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
Pierre de Lacaze
 

Similar to 20 prolog5 (20)

09 logic programming
09 logic programming09 logic programming
09 logic programming
 
Declarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere MortalsDeclarative Datalog Debugging for Mere Mortals
Declarative Datalog Debugging for Mere Mortals
 
Cut and Goal on prolog
Cut and Goal on prologCut and Goal on prolog
Cut and Goal on prolog
 
21CSC206T_UNIT 5.pptx artificial intelligence
21CSC206T_UNIT 5.pptx artificial intelligence21CSC206T_UNIT 5.pptx artificial intelligence
21CSC206T_UNIT 5.pptx artificial intelligence
 
Prolog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In PrologProlog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In Prolog
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach us
 
Midterm sols
Midterm solsMidterm sols
Midterm sols
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
 
Query Rewriting and Optimization for Ontological Databases
Query Rewriting and Optimization for Ontological DatabasesQuery Rewriting and Optimization for Ontological Databases
Query Rewriting and Optimization for Ontological Databases
 
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof..."Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
"Let us talk about output features! by Florence d’Alché-Buc, LTCI & Full Prof...
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Signals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptxSignals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptx
 
Stochastic Processes Homework Help
Stochastic Processes Homework HelpStochastic Processes Homework Help
Stochastic Processes Homework Help
 
Midterm
MidtermMidterm
Midterm
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Prolog2 (1)
Prolog2 (1)Prolog2 (1)
Prolog2 (1)
 
Structured regression for efficient object detection
Structured regression for efficient object detectionStructured regression for efficient object detection
Structured regression for efficient object detection
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 

Recently uploaded

Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
Tarandeep Singh
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
APNIC
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
thezot
 
cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
GNAMBIKARAO
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
3a0sd7z3
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
APNIC
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
3a0sd7z3
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
rtunex8r
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
Infosec train
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
Donato Onofri
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
dtagbe
 

Recently uploaded (11)

Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
 
cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
 

20 prolog5

  • 1. Fall 2008 Prolog: Negation The current topic: Prolog ! Introduction ! Object-oriented programming: Python ! Functional programming: Scheme ! Python GUI programming (Tkinter) ! Types and values • Logic programming: Prolog ! Introduction ! Rules, unification, resolution, backtracking, lists. ! More lists, math, structures. ! More structures, trees, cut. – Next up: Negation. • Syntax and semantics • Exceptions 1 Fall 2008 Prolog: Negation Announcements • Term Test 2 has been marked. – Handed back at the end of class today. – The deadline for submitting a re-mark request is the end of class, Friday November 28th. Make sure you understand the posted solutions before submitting a re-mark request. – Average: 68.4% 2 Fall 2008 Prolog: Negation Using not instead of cut to avoid wrong answers • Prolog has a not operator, but its behaviour is more subtle than in other languages. • Example: Replacing a cut with not: – With cut: A :- B, !, C. A :- D. – With not: A :- B, C. A :- not(B), D. • Observe that not can be more cumbersome than cut. – repetitive if-then-else 3 Fall 2008 Prolog: Negation Using not for inequality • Example: crispy(snap). crispy(crackle). crispy(pop). breakfast(A,B,C) :- crispy(A), crispy(B), crispy(C). ?- breakfast(A,B,C). A = snap B = snap C = snap ; A = snap B = snap C = crackle ; ... • But we really want A, B, and C to be different from each other. 4
  • 2. Fall 2008 Prolog: Negation Using not for inequality crispy(snap). crispy(crackle). crispy(pop). breakfast(A,B,C) :- crispy(A), crispy(B), crispy(C), not(A=B), not(A=C), not(B=C). ?- breakfast(A,B,C). A = snap B = crackle C = pop ; A = snap B = pop C = crackle ; ... 5 Fall 2008 Prolog: Negation Negation in Prolog • not(B) can also be written as + B. – And not(X=Y) can also be written as X = Y. • The goal + X succeeds iff X fails. • Examples: ?- + member(b, [a,b,c]). No ?- + member(x, [a,b,c]). Yes ?- + member(X, [a,b,c]). No 6 Fall 2008 Prolog: Negation Negation in Prolog ?- + member(X, [a,b,c]). No • It might look like this query is asking "Does there exist an X for which member(X, [a,b,c]) does not succeed?". – We know there are lots of values of X for which member(X, [a,b,c]) does not succeed. – But that's not what negation means in Prolog. – There exists X for which member(X, [a,b,c]) succeeds. – So then + member(X, [a,b,c]) fails. 7 Fall 2008 Prolog: Negation Negation as failure • Prolog assumes that if it can't prove an assertion, then the assertion is false. – And Prolog assumes that if it can prove an assertion, then the assertion is true. • This is the "closed world assumption": in the universe of facts Prolog knows about, failure to prove is proof of failure. – But if we know something Prolog doesn't, this can lead to surprises: things that Prolog thinks are false when we know they're true, and the opposite. – Example: university(uoft). ?- university(york). No ?- + university(york). Yes 8
  • 3. Fall 2008 Prolog: Negation ?- sad(michael). No ?- sad(jim). Yes ?- sad(Someone). No • Isn't anyone sad? • No, that just means that it's not true we can't find anyone happy. – In other words, there exists someone who is happy. Be careful with negation sad(X) :- + happy(X). happy(X) :- beautiful(X), rich(X). rich(bill). beautiful(michael). rich(michael). beautiful(cinderella). ?- sad(bill). Yes ?- sad(cinderella). Yes 9 Fall 2008 Prolog: Negation Tracing negation • Let's look at how Prolog answers sad(Someone).: ?- sad(Someone). Call: (7) sad(_G283) ? creep Call: (8) happy(_G283) ? creep Call: (9) beautiful(_G283) ? creep Exit: (9) beautiful(michael) ? creep Call: (9) rich(michael) ? creep Exit: (9) rich(michael) ? creep Exit: (8) happy(michael) ? creep Fail: (7) sad(_G283) ? creep No 10 Fall 2008 Prolog: Negation Set overlap • Write a predicate overlap(S1, S2) that succeeds if lists S1 and S2 have a common element. Then write a predicate disjoint(S1, S2) that succeeds if S1 and S2 have no common element. overlap(S1, S2) :- member(X, S1), member(X, S2). disjoint(S1, S2) :- + overlap(S1, S2). ?- overlap([a,b,c], [c,d,e]). Yes ?- disjoint([a,b,c], [c,d,e]). No ?- overlap([a,b,c], [d,e,f]). No ?- disjoint([a,b,c], [d,e,f]). Yes ?- disjoint([a,b,d], S). No 11 Fall 2008 Prolog: Negation What does that mean? ?- disjoint([a,b,d], S). No • The query should mean "can you find a list S that is disjoint from the list [a,b,d] (and if so what is it)?". • Obviously there are many such sets, so why "No"? • Answer: because Prolog succeeded in finding a set that did overlap with S, so it announced failure of the original query. ?- overlap([a,b,d], S). S = [a|_G226] ; S = [_G225, a|_G229] ; S = [_G225, _G228, a|_G232] ; ... 12
  • 4. Fall 2008 Prolog: Negation Safe use of negation • The goal not(G) is safe if either: – G is fully instantiated when not(G) is processed, or – G has uninstantiated variables, but they don't appear anywhere else in the clause. • Safe example: childlessMan(X) :- male(X), + parent(X,Y). – X is instantiated in male(X), and Y isn't used elsewhere. • Unsafe example: childlessMan(X) :- + parent(X,Y), male(X). – X is not instantiated before the negation, and is used elsewhere. • If necessary, add a precondition to warn the programmer. – recall that +Var means that Var must be instantiated. % disjoint(+S1, +S2) succeeds if... disjoint(S1, S2) :- + overlap(S1, S2). – When the precondition is satisfied, this negation is safe. 13 Fall 2008 Prolog: Negation Double-negation doesn't "cancel out" • In other languages, not(not(<expression>)) is equivalent to <expression>. – But not in Prolog. ?- member(X,[a,b,c]). X = a ; X = b ; X = c ; No ?- not(not(member(X,[a,b,c]))). X = _G166 ; No 14 Fall 2008 Prolog: Negation Double-negation doesn't "cancel out" ?- not(not(member(X,[a,b,c]))). X = _G166 ; No • Why is X uninstantiated in this example? – Since member(X, [a,b,c]) succeeds (by instantiating X to, say, a), not(member(X, [a,b,c])) fails. – When a goal fails, the variables it instantiated get uninstantiated. So X gets uninstantiated. – But since not(member(X, [a,b,c])) fails, not(not(member(X, [a,b,c]))) succeeds. 15 Fall 2008 Prolog: Negation fail • The fail predicate fails immediately. Example: p(X) :- fail. ?- p(csc326). No • We can use fail to state that something is false. 16
  • 5. Fall 2008 Prolog: Negation fail • Example: We want to represent "Colbert does not like bears (regardless of whatever else he likes)." – One solution: Add "not(bear(X))" to every rule describing what Colbert likes. For example: likes(colbert, X) :- animal(X), not(bear(X)). likes(colbert, X) :- toy(X), not(bear(X)). likes(colbert, X) :- livesInArctic(X), not(bear(X)). ... – Let's try to use fail instead. – First attempt: bear(yogi). animal(yogi). likes(colbert, X) :- bear(X), fail. likes(colbert, X) :- animal(X). ?- likes(colbert, yogi). Yes 17 Fall 2008 Prolog: Negation fail • We need to add a cut to prevent other rules from being tried after the first rule reaches fail. – Second attempt: bear(yogi). cat(tom). animal(yogi). animal(tom). likes(colbert, X) :- bear(X), !, fail. likes(colbert, X) :- animal(X). ?- likes(colbert, yogi). No ?- likes(colbert, tom). Yes ?- likes(colbert, X). No – Downside: This solution only works when X is instantiated. 18 Fall 2008 Prolog: Negation fail • Another example: Define a predicate different(X, Y) that succeeds if X and Y don't unify. different(X, Y) :- X=Y, !, fail. different(_, _). ?- different(a, b). Yes ?- different(a, a). No • Notice that the above definition is equivalent to: different(X, Y) :- not(X=Y). 19 Fall 2008 Prolog: Negation Defining "not" using cut and fail • We can define the not predicate as follows: not(X) :- X, !, fail. not(_). • (To test this out, use a name other than "not", since Prolog won't let you redefine the built-in "not"). 20
  • 6. Fall 2008 Prolog: Negation fail • Recall the original version of bstmem(Tree, X): bstmem(node(X, _, _), X). bstmem(node(K, L, _), X) :- X < K, bstmem(L, X). bstmem(node(K, _, R), X) :- X > K, bstmem(R, X). • Recall that this version was inefficient. 21 Fall 2008 Prolog: Negation Inefficiency in bstmem [trace] ?- bstmem(node(5, node(3,empty,empty), emtpy), 1). Call: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep ^ Call: (9) 1<5 ? creep ^ Exit: (9) 1<5 ? creep Call: (9) bstmem(node(3, empty, empty), 1) ? creep ^ Call: (10) 1<3 ? creep ^ Exit: (10) 1<3 ? creep Call: (10) bstmem(empty, 1) ? creep Fail: (10) bstmem(empty, 1) ? creep Redo: (9) bstmem(node(3, empty, empty), 1) ? creep ^ Call: (10) 1>3 ? creep ^ Fail: (10) 1>3 ? creep Redo: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep ^ Call: (9) 1>5 ? creep ^ Fail: (9) 1>5 ? creep No 22 Fall 2008 Prolog: Negation fail • We solved the inefficiency illustrated on the previous slide as follows: bstmem(node(X, _, _), X). bstmem(node(K, L, _), X) :- X < K, !, bstmem(L, X). bstmem(node(K, _, R), X) :- X > K, bstmem(R, X). • What if we try to instead solve this inefficiency by using fail: bstmem(empty,_) :- !, fail. bstmem(node(X, _, _), X). bstmem(node(K, L, _), X) :- X < K, bstmem(L, X). bstmem(node(K, _, R), X) :- X > K, bstmem(R, X). 23 Fall 2008 Prolog: Negation Tracing the new bstmem [trace] ?- bstmem(node(5, node(3,empty,empty), emtpy), 1). Call: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep ^ Call: (9) 1<5 ? creep ^ Exit: (9) 1<5 ? creep Call: (9) bstmem(node(3, empty, empty), 1) ? creep ^ Call: (10) 1<3 ? creep ^ Exit: (10) 1<3 ? creep Call: (10) bstmem(empty, 1) ? creep Call: (11) fail ? creep Fail: (11) fail ? creep Fail: (10) bstmem(empty, 1) ? creep Redo: (9) bstmem(node(3, empty, empty), 1) ? creep ^ Call: (10) 1>3 ? creep ^ Fail: (10) 1>3 ? creep Redo: (8) bstmem(node(5, node(3, empty, empty), emtpy), 1) ? creep ^ Call: (9) 1>5 ? creep ^ Fail: (9) 1>5 ? creep No 24
  • 7. Fall 2008 Prolog: Negation fail • What went wrong? – fail only affects the present goal (bstmem(empty, 1) in the example). – It does not directly cause the failure of a previous goal (so, in the example, Prolog still looks for other rules for the goal bstmem(node(3,empty,empty), 1)). 25 Fall 2008 Prolog: Negation Advice on writing Prolog To minimize bugs, especially with cut and not: • Use cut and not as necessary to avoid wrong answers. • Follow the rules for safe use of not. • Follow the rules for doing arithmetic. • Always use ";" when testing to check all possible answers. – It's easy to get first answer right and rest wrong if "else" misused. • Test with variables in every combination of positions. • Use preconditions to state where variables are disallowed. • Use cut to avoid duplicate answers. • Use cut where possible for efficiency. • Use _ where possible for efficiency. 26 Fall 2008 Prolog: Negation Summary: logic programming and Prolog • Logic programming: – Unification, resolution, backtracking. – Specify kind of result wanted (what you want), not how to get it. • Prolog: – The major logic programming language. – Efficiency can be a worry: • cut • ordering the predicates 27 Fall 2008 Prolog: Negation Bubble sort • Write a predicate bsort(+Before, ?After) that succeeds if After is a sorted version of Before. bsort should use bubble sort to sort the list. bsort(Before, After) :- bsortaux(Before, [], After). • Helper predicate bsortaux(+Prelower, +Preupper, ?Sorted) succeeds if Sorted is a list that consists of a sorted version of Prelower followed by (an unchanged) Preupper. bsortaux([], Preupper, Preupper) :- !. bsortaux(Prelower, Preupper, Sorted) :- bubble(Prelower, Preupper, Postlower, Postupper), bsortaux(Postlower, Postupper, Sorted). 28
  • 8. Fall 2008 Prolog: Negation Bubble sort • Helper predicate bubble(+Prelower, +Preupper, ?Postlower, ?Postupper) succeeds if performing one round of bubble sort on unsorted portion Prelower and sorted portion Preupper results in unsorted portion Postlower and sorted portion Postupper. bubble([X, Y | Rest], Preupper, [X | Bubbled], Postupper) :- X =< Y, % No swap needed. !, bubble([Y | Rest], Preupper, Bubbled, Postupper). bubble([X, Y | Rest], Preupper, [Y | Bubbled], Postupper) :- bubble([X | Rest], Preupper, Bubbled, Postupper). bubble([X], Preupper, [], [X|Preupper]) :- !. bubble([], Preupper, [], Preupper). % not needed, we hope 29 Fall 2008 Prolog: Negation Tracing bsort [trace] ?- bsort([2,1], S). Call: (7) bsort([2, 1], _G290) ? creep Call: (8) bsortaux([2, 1], [], _G290) ? creep Call: (9) bubble([2, 1], [], _L206, _L207) ? creep ^ Call: (10) 2=<1 ? creep ^ Fail: (10) 2=<1 ? creep Redo: (9) bubble([2, 1], [], _L206, _L207) ? creep Call: (10) bubble([2], [], _G346, _L207) ? creep Exit: (10) bubble([2], [], [], [2]) ? creep Exit: (9) bubble([2, 1], [], [1], [2]) ? creep Call: (9) bsortaux([1], [2], _G290) ? creep Call: (10) bubble([1], [2], _L245, _L246) ? creep Exit: (10) bubble([1], [2], [], [1, 2]) ? creep Call: (10) bsortaux([], [1, 2], _G290) ? creep Exit: (10) bsortaux([], [1, 2], [1, 2]) ? creep Exit: (9) bsortaux([1], [2], [1, 2]) ? creep Exit: (8) bsortaux([2, 1], [], [1, 2]) ? creep Exit: (7) bsort([2, 1], [1, 2]) ? creep S = [1, 2] 30 Fall 2008 Prolog: Negation Tracing bsort [trace] ?- bsort([3,2,1], S). Call: (7) bsort([3, 2, 1], _G293) ? creep Call: (8) bsortaux([3, 2, 1], [], _G293) ? creep Call: (9) bubble([3, 2, 1], [], _L206, _L207) ? creep ^ Call: (10) 3=<2 ? creep ^ Fail: (10) 3=<2 ? creep Redo: (9) bubble([3, 2, 1], [], _L206, _L207) ? creep Call: (10) bubble([3, 1], [], _G352, _L207) ? creep ^ Call: (11) 3=<1 ? creep ^ Fail: (11) 3=<1 ? creep Redo: (10) bubble([3, 1], [], _G352, _L207) ? creep Call: (11) bubble([3], [], _G358, _L207) ? creep Exit: (11) bubble([3], [], [], [3]) ? creep Exit: (10) bubble([3, 1], [], [1], [3]) ? creep Exit: (9) bubble([3, 2, 1], [], [2, 1], [3]) ? creep 31 Fall 2008 Prolog: Negation Tracing bsort Call: (9) bsortaux([2, 1], [3], _G293) ? creep Call: (10) bubble([2, 1], [3], _L266, _L267) ? creep ^ Call: (11) 2=<1 ? creep ^ Fail: (11) 2=<1 ? creep Redo: (10) bubble([2, 1], [3], _L266, _L267) ? creep Call: (11) bubble([2], [3], _G367, _L267) ? creep Exit: (11) bubble([2], [3], [], [2, 3]) ? creep Exit: (10) bubble([2, 1], [3], [1], [2, 3]) ? creep Call: (10) bsortaux([1], [2, 3], _G293) ? creep Call: (11) bubble([1], [2, 3], _L305, _L306) ? creep Exit: (11) bubble([1], [2, 3], [], [1, 2, 3]) ? creep Call: (11) bsortaux([], [1, 2, 3], _G293) ? creep Exit: (11) bsortaux([], [1, 2, 3], [1, 2, 3]) ? creep Exit: (10) bsortaux([1], [2, 3], [1, 2, 3]) ? creep Exit: (9) bsortaux([2, 1], [3], [1, 2, 3]) ? creep Exit: (8) bsortaux([3, 2, 1], [], [1, 2, 3]) ? creep Exit: (7) bsort([3, 2, 1], [1, 2, 3]) ? creep S = [1, 2, 3] 32
  • 9. Fall 2008 Prolog: Negation Exercises • Fix the sibling predicate (that we previously defined) so that it doesn't consider a person to be there own sibling. Then make sure that this fix has eliminated any unusual behaviour in the aunt, uncle, nephew, and niece predicates that you defined in a previous set of exercises. • Trace bsort on more interesting (and larger) examples. For example, trace the call: bsort([1,5,2,6,3,4], S). • Challenge: Recall that the efficiency of bubble sort can be improved by halting after the first iteration during which no swaps are performed (we can halt at that point since if no swaps are performed, the list must be already sorted). Modify bsort by adding this improvement. 33