negation chapter 5. stating negative conditions n sometimes you want to say that some condition does...

26
Negation Negation Chapter 5 Chapter 5

Upload: reginald-ferguson

Post on 21-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

NegationNegation

Chapter 5Chapter 5

Page 2: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Stating Negative ConditionsStating Negative Conditions

Sometimes you want to say that some Sometimes you want to say that some condition does notcondition does not holdhold

Prolog allows thisProlog allows this– not/1not/1 this is a predicatethis is a predicate– \+/1\+/1 this is a prefix operatorthis is a prefix operator

\+ is the preferred way to go\+ is the preferred way to go not is the traditional way to gonot is the traditional way to go

Page 3: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Non-MembersNon-Members

a is not a member of [q, w, e, r, t, y]a is not a member of [q, w, e, r, t, y]?- ?- \+ member(a, [q,w,e,r,t,y]).\+ member(a, [q,w,e,r,t,y]).YesYes Admission prices revisedAdmission prices revisedadmission(Member, 2) :-admission(Member, 2) :-

club_member(Member).club_member(Member).admission(NonMember, 5) :-admission(NonMember, 5) :-

\+ club_member(NonMember).\+ club_member(NonMember).

Page 4: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

How Not WorksHow Not Works

““Negation by failure”Negation by failure”– not tries to prove its argumentnot tries to prove its argument– if the argument fails, not succeedsif the argument fails, not succeeds

\+ club_member(mark).\+ club_member(mark).– tries to prove club_member(mark).tries to prove club_member(mark).– if club_member(mark) succeeds, \+ failsif club_member(mark) succeeds, \+ fails– if club_member(mark) fails, \+ succeedsif club_member(mark) fails, \+ succeeds

Page 5: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Definition of NotDefinition of Not

\+ G :-\+ G :-G, !, failG, !, fail;;true.true.

Some dialects require Some dialects require \+ G :-\+ G :-

call(G), !, failcall(G), !, fail;;true.true.

Page 6: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Not versus CutNot versus Cut

Not is generally less efficient than cutNot is generally less efficient than cut– club_member called twice for non-membersclub_member called twice for non-members

Not is more logical than cutNot is more logical than cut– can rearrange clauses so not-clause comes firstcan rearrange clauses so not-clause comes first

Exercise: rewrite costToBuy/3 using \+ Exercise: rewrite costToBuy/3 using \+ instead of !instead of !

Page 7: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Variables Under the NotVariables Under the Not

Handled by the predicate callHandled by the predicate call?- \+ parent(jim, X).?- \+ parent(jim, X).X = _G301X = _G301YesYes Tried to find an X that jim was parent ofTried to find an X that jim was parent of

– that failedthat failed– \+ succeeded\+ succeeded– X remains unbound (OK – no child of jim)X remains unbound (OK – no child of jim)

Page 8: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Variables Under the NotVariables Under the Not

Failure results in no variable bindings Failure results in no variable bindings

?- \+ parent(bob, X).?- \+ parent(bob, X).

NoNo Tried to find an X that bob was parent ofTried to find an X that bob was parent of

– that succeeded (X = ann)that succeeded (X = ann)– so \+ failedso \+ failed– answer was No, so no bindings printedanswer was No, so no bindings printed

Page 9: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Not Not NeverNever Binds Variables Binds Variables

No bindings printed for \+ questionsNo bindings printed for \+ questions– success means no values were foundsuccess means no values were found– failure means no bindings survivefailure means no bindings survive

Can’t use \+ or not to generate counter-Can’t use \+ or not to generate counter-examplesexamples– can’t say “find an X such that bob is not the can’t say “find an X such that bob is not the

parent of X”parent of X”

Page 10: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Finding Bob’s Non-ChildrenFinding Bob’s Non-Children

Need to make X into someone firstNeed to make X into someone firstperson(X) :- parent(X, _).person(X) :- parent(X, _).person(X) :- parent(_, X).person(X) :- parent(_, X).nonparent(X, Y) :-nonparent(X, Y) :-

person(X), person(Y), \+ parent(X, Y).person(X), person(Y), \+ parent(X, Y). Now can find nonparentsNow can find nonparents?- ?- nonparent(bob, X).nonparent(bob, X).X = pamX = pam

Page 11: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Not Does Not CommuteNot Does Not Commute

\+’s variables should be as instantiated as \+’s variables should be as instantiated as necessary when callednecessary when called– otherwise it may prevent solutions being foundotherwise it may prevent solutions being found?- ?- \+ parent(X, Y), X = jim, Y = bob.\+ parent(X, Y), X = jim, Y = bob.NoNo?- ?- X = jim, Y = bob, \+ parent(X, Y).X = jim, Y = bob, \+ parent(X, Y).X = jim, Y = bobX = jim, Y = bobYesYes

Page 12: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

ExerciseExercise

Write clauses for likes/2 for MicheleWrite clauses for likes/2 for Michele– Michele likes animalsMichele likes animals– except spiders and snakesexcept spiders and snakes– except she likes Sammy (who is a snake)except she likes Sammy (who is a snake)

Use \+ instead of cut, fail and disjunctionUse \+ instead of cut, fail and disjunctionlikes(michele, X) :-likes(michele, X) :-

……

Page 13: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Using Negation to Block CyclesUsing Negation to Block Cycles

Monkey example very “fragile”Monkey example very “fragile”– change order of actions – get infinite recursionchange order of actions – get infinite recursion– put walking first, monkey just tries walking all put walking first, monkey just tries walking all

over the place, never climbs on or pushes boxover the place, never climbs on or pushes box Want to avoid monkey going into a loopWant to avoid monkey going into a loop

– getting into a state he was in beforegetting into a state he was in before– not making progress toward the goalnot making progress toward the goal

Page 14: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Avoiding CyclesAvoiding Cycles

Whenever monkey makes a move…Whenever monkey makes a move… ……he checks to see if he’s been in the new he checks to see if he’s been in the new

state beforestate before– if so, reject that moveif so, reject that move– if not, carry on looking for other movesif not, carry on looking for other moves

Need to keep track of previous statesNeed to keep track of previous states– an extra argumentan extra argument– add previous states as we go alongadd previous states as we go along

Page 15: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Keeping Track of Old StatesKeeping Track of Old States

% canget(State, OldStates):% canget(State, OldStates):% monkey can get banana from State% monkey can get banana from State% without repeating any of his OldStates% without repeating any of his OldStates

canget(state( _, _, _, has)canget(state( _, _, _, has), _, _).).canget(State1canget(State1, OldStates, OldStates) :-) :- move(State1, Move, State2),move(State1, Move, State2), \+ member(State2, OldStates),\+ member(State2, OldStates), canget(State2canget(State2, [State1|OldStates], [State1|OldStates]).).

Page 16: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Passing Down Passing Down vsvs Passing Up Passing Up

Compare above with building list of movesCompare above with building list of movescanget(…, []).canget(…, []).vsvscanget(…, _).canget(…, _).

andand

canget(…, [Move|Moves]) :- … canget(…, Moves).canget(…, [Move|Moves]) :- … canget(…, Moves).vsvscanget(Old, Olds) :- … canget(…, [Old|Olds]).canget(Old, Olds) :- … canget(…, [Old|Olds]).

Head passes upBody passes down

Page 17: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Combining Down & UpCombining Down & Up

Can do it all in PrologCan do it all in Prologcanget(state(_,_,_,has), _, []).canget(state(_,_,_,has), _, []).

canget(Old, Olds, [Move|Moves]) :-canget(Old, Olds, [Move|Moves]) :-move(Old, Move, New),move(Old, Move, New),\+ member(New, Olds),\+ member(New, Olds),canget(New, [Old|Olds], Moves).canget(New, [Old|Olds], Moves).

Page 18: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Hiding ArgumentsHiding Arguments

Can have different number of arguments Can have different number of arguments with same predicate namewith same predicate name– hide arguments that always start out the samehide arguments that always start out the same

canget(StartState, Moves) :-canget(StartState, Moves) :-canget(StartState, [], Moves).canget(StartState, [], Moves).

canget(Moves) :-canget(Moves) :-canget(state(atdoor,onfloor,atwindow,hasnot),canget(state(atdoor,onfloor,atwindow,hasnot),

Moves).Moves).

Page 19: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Compound NotsCompound Nots

Suppose we want to define none_parents/1Suppose we want to define none_parents/1– argument is a list with no parents in itargument is a list with no parents in it

Could go down list & check each elementCould go down list & check each element– just like we did for all_malejust like we did for all_male

But could just say: no member of this list is But could just say: no member of this list is a parenta parent– how do we say that?how do we say that?

Page 20: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Wrong AnswersWrong Answers

none_parents_1(List) :-none_parents_1(List) :-

member(P, List),member(P, List),

\+ parent(P).\+ parent(P).

none_parents_2(List) :-none_parents_2(List) :-

parent(P),parent(P),

\+ member(P, List).\+ member(P, List).

some member is not a parent[pam, bob, ann]

some parent is not a member[pam, bob, liz]tom not there

Page 21: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Correct AnswerCorrect Answer

Not the case that a member is a parentNot the case that a member is a parentnone_parents(List) :-none_parents(List) :-

\+ \+ ((member(P, List), parent(P)member(P, List), parent(P))).. Parentheses around the compound argumentParentheses around the compound argument

– \+ binds closer than ,\+ binds closer than ,– use parentheses to change orderuse parentheses to change order

If there is a member who is a parent, failIf there is a member who is a parent, fail– otherwise, you’re OKotherwise, you’re OK

Page 22: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

ExerciseExercise

Write a predicate no_brothers/1Write a predicate no_brothers/1– no one in the argument is brother to anyone else no one in the argument is brother to anyone else

in the argumentin the argument%% no_brothers( +ListOfPeople )%% no_brothers( +ListOfPeople )

Page 23: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

First Midterm TestFirst Midterm Test

PrologProlog

Page 24: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

First TestFirst Test

Next Thursday (February 7)Next Thursday (February 7) In class (HSH 206)In class (HSH 206) TopicsTopics

– chapters 1 to 5 (in-class material only)chapters 1 to 5 (in-class material only) Closed bookClosed book Closed computerClosed computer More doing than sayingMore doing than saying

Page 25: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

DoingDoing

Writing Prolog codeWriting Prolog code– factsfacts– rulesrules– queriesqueries

Reading & interpreting Prolog codeReading & interpreting Prolog code– saying what Prolog’s response would besaying what Prolog’s response would be– ““tracing execution”tracing execution”

Page 26: Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate

Next TimeNext Time

Input and OutputInput and Output– Chapter 6Chapter 6