Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Negation Chapter 5

2 Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate –\+/1this is a prefix operator n \+ is the preferred way to go n not is the traditional way to go

3 Non-Members n a is not a member of [q, w, e, r, t, y] ?- \+ member(a, [q,w,e,r,t,y]). Yes n Admission prices revised admission(Member, 2) :- club_member(Member). admission(NonMember, 5) :- \+ club_member(NonMember).

4 How Not Works n “Negation by failure” –not tries to prove its argument –if the argument fails, not succeeds n \+ club_member(mark). –tries to prove club_member(mark). –if club_member(mark) succeeds, \+ fails –if club_member(mark) fails, \+ succeeds

5 Definition of Not \+ G :- G, !, fail ;true. n Some dialects require \+ G :- call(G), !, fail ;true.

6 Not versus Cut n Not is generally less efficient than cut –club_member called twice for non-members n Not is more logical than cut –can rearrange clauses so not-clause comes first n Exercise: rewrite costToBuy/3 using \+ instead of !

7 Variables Under the Not n Handled by the predicate call ?- \+ parent(jim, X). X = _G301 Yes n Tried to find an X that jim was parent of –that failed –\+ succeeded –X remains unbound (OK – no child of jim)

8 Variables Under the Not n Failure results in no variable bindings ?- \+ parent(bob, X). No n Tried to find an X that bob was parent of –that succeeded (X = ann) –so \+ failed –answer was No, so no bindings printed

9 Not Never Binds Variables n No bindings printed for \+ questions –success means no values were found –failure means no bindings survive n Can’t use \+ or not to generate counter- examples –can’t say “find an X such that bob is not the parent of X”

10 Finding Bob’s Non-Children n Need to make X into someone first person(X) :- parent(X, _). person(X) :- parent(_, X). nonparent(X, Y) :- person(X), person(Y), \+ parent(X, Y). n Now can find nonparents ?- nonparent(bob, X). X = pam

11 Not Does Not Commute n \+’s variables should be as instantiated as necessary when called –otherwise it may prevent solutions being found ?- \+ parent(X, Y), X = jim, Y = bob. No ?- X = jim, Y = bob, \+ parent(X, Y). X = jim, Y = bob Yes

12 Exercise n Write clauses for likes/2 for Michele –Michele likes animals –except spiders and snakes –except she likes Sammy (who is a snake) n Use \+ instead of cut, fail and disjunction likes(michele, X) :- …

13 Using Negation to Block Cycles n Monkey example very “fragile” –change order of actions – get infinite recursion –put walking first, monkey just tries walking all over the place, never climbs on or pushes box n Want to avoid monkey going into a loop –getting into a state he was in before –not making progress toward the goal

14 Avoiding Cycles n Whenever monkey makes a move… n …he checks to see if he’s been in the new state before –if so, reject that move –if not, carry on looking for other moves n Need to keep track of previous states –an extra argument –add previous states as we go along

15 Keeping Track of Old States % canget(State, OldStates): % monkey can get banana from State % without repeating any of his OldStates canget(state( _, _, _, has), _). canget(State1, OldStates) :- move(State1, Move, State2), move(State1, Move, State2), \+ member(State2, OldStates), \+ member(State2, OldStates), canget(State2, [State1|OldStates]). canget(State2, [State1|OldStates]).

16 Passing Down vs Passing Up n Compare above with building list of moves canget(…, []). vs canget(…, _). and canget(…, [Move|Moves]) :- … canget(…, Moves). vs canget(Old, Olds) :- … canget(…, [Old|Olds]). Head passes up Body passes down

17 Combining Down & Up n Can do it all in Prolog canget(state(_,_,_,has), _, []). canget(Old, Olds, [Move|Moves]) :- move(Old, Move, New), \+ member(New, Olds), canget(New, [Old|Olds], Moves).

18 Hiding Arguments n Can have different number of arguments with same predicate name –hide arguments that always start out the same canget(StartState, Moves) :- canget(StartState, [], Moves). canget(Moves) :- canget(state(atdoor,onfloor,atwindow,hasnot),Moves).

19 Compound Nots n Suppose we want to define none_parents/1 –argument is a list with no parents in it n Could go down list & check each element –just like we did for all_male n But could just say: no member of this list is a parent –how do we say that?

20 Wrong Answers none_parents_1(List) :- member(P, List), \+ parent(P). none_parents_2(List) :- parent(P), \+ member(P, List). some member is not a parent [pam, bob, ann] some parent is not a member [pam, bob, liz] tom not there

21 Correct Answer n Not the case that a member is a parent none_parents(List) :- \+ (member(P, List), parent(P)). n Parentheses around the compound argument –\+ binds closer than, –use parentheses to change order n If there is a member who is a parent, fail –otherwise, you’re OK

22 Exercise n Write a predicate no_brothers/1 –no one in the argument is brother to anyone else in the argument % no_brothers( +ListOfPeople )

23 First Midterm Test Prolog

24 First Test n Next Thursday (February 7) n In class (HSH 206) n Topics –chapters 1 to 5 (in-class material only) n Closed book n Closed computer n More doing than saying

25 Doing n Writing Prolog code –facts –rules –queries n Reading & interpreting Prolog code –saying what Prolog’s response would be –“tracing execution”

26 Next Time n Input and Output –Chapter 6


Download ppt "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."

Similar presentations


Ads by Google