54
Lists in Prolog Lists in Prolog Sections 3.1, 3.2 Sections 3.1, 3.2

Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Embed Size (px)

Citation preview

Page 1: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Lists in PrologLists in Prolog

Sections 3.1, 3.2Sections 3.1, 3.2

Page 2: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Testing a ListTesting a List

Suppose have defined female/1 and male/1Suppose have defined female/1 and male/1 Check whether a list is all maleCheck whether a list is all male

% all_male(?L)% all_male(?L)% -- male(X) for each element X of L% -- male(X) for each element X of L Identify the predicate & its argument(s)Identify the predicate & its argument(s)

– we’ll talk about the ? soonwe’ll talk about the ? soon State the relationship/property that holdsState the relationship/property that holds

Page 3: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Logic of List TestingLogic of List Testing

Every element is maleEvery element is male Going to use recursion (no loops!)Going to use recursion (no loops!) Base caseBase case

– ?????? Recursive caseRecursive case

– ??????

What’s the simplest list of all males?No, there’s one simpler than that!

What to do if it’s NOT the simplest case?

Page 4: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

All MalesAll Males

% all_male(?L)% all_male(?L)%% -- male(X) for all elements X of L-- male(X) for all elements X of L

all_male([]).all_male([]).all_male([H | T]) :-all_male([H | T]) :-

male(H),male(H),all_male(T).all_male(T).

Page 5: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

General List UseGeneral List Use

Common to want to do stuff with every Common to want to do stuff with every element of a listelement of a list– test it, make a copy of it, …test it, make a copy of it, …

Multi-clause predicate (usually 2)Multi-clause predicate (usually 2)– first clause is base case (usually 0 or 1 element)first clause is base case (usually 0 or 1 element)– last clause recursive case (extracts head, recurs last clause recursive case (extracts head, recurs

on tail)on tail)– extra cases as requiredextra cases as required

Page 6: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

On ?, + and – On ?, + and –

Documented argument with ? in front of itDocumented argument with ? in front of it– means that arg. can be variable or valuemeans that arg. can be variable or value

Alternatives are +Alternatives are +– shouldshould have a value have a value– unboundunbound variables not allowed/bad idea variables not allowed/bad idea

And –And –– should should notnot have a value have a value– unbound variables unbound variables onlyonly

“bound” = has a value other than just another variable

Used in system documentationsee help(member), help(is)

Page 7: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

ExerciseExercise

Write a predicate that takes a Write a predicate that takes a fully filled in fully filled in list of numberslist of numbers and creates (or tests) a and creates (or tests) a second list where each element is 1 more second list where each element is 1 more than in the first listthan in the first list– one_more([1,2,3], L) makes L=[2,3,4]one_more([1,2,3], L) makes L=[2,3,4]– one_more([1,2,4], [2,3,5]) succeedsone_more([1,2,4], [2,3,5]) succeeds– one_more([1,2,5], [2,3,7]) failsone_more([1,2,5], [2,3,7]) fails

Document itDocument it

Page 8: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Case AnalysisCase Analysis

Simplest list is the empty listSimplest list is the empty list If the first list is empty, what should the If the first list is empty, what should the

second list be?second list be?

If the first list is not empty, how is the head of If the first list is not empty, how is the head of the second list related to the head of the first?the second list related to the head of the first?

How are the tails related?How are the tails related?

Page 9: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

DocumentationDocumentation

How is the second list related to the first?How is the second list related to the first? What notations on the arguments?What notations on the arguments?

– ????– +?+?– ––??

What shall we call the arguments?What shall we call the arguments?

Page 10: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Possible SolutionPossible Solution

% one_more(+Less, ?More)% one_more(+Less, ?More)%% -- N is M+1 for each N in More-- N is M+1 for each N in More%% and the corresponding M in Less and the corresponding M in Less

one_more([], []).one_more([], []).one_more([M | Ms], [N | Ns]) :-one_more([M | Ms], [N | Ns]) :-

N is M + 1,N is M + 1,one_more(Ms, Ns).one_more(Ms, Ns).

Page 11: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Logical AdditionLogical Addition

%% one_more_1(?Less, ?More)one_more_1(?Less, ?More)%% -- N is M+1 for each N in More -- N is M+1 for each N in More%% and the corresponding M in Less and the corresponding M in Less%% -- Note: N or M must be instantiated -- Note: N or M must be instantiatedone_more_1([], []).one_more_1([], []).one_more_1([M | Ms], [N | Ns]) :-one_more_1([M | Ms], [N | Ns]) :-

plus(M, 1, N),plus(M, 1, N),one_more(Ms, Ns).one_more(Ms, Ns).

% M + 1 is equal to N

Page 12: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Miscellaneous Math StuffMiscellaneous Math Stuff

Logical additionLogical addition ((will fill in any one variablewill fill in any one variable))– plus(X, Y, Z)plus(X, Y, Z) X + Y is equal to Z X + Y is equal to Z– succ(M, N)succ(M, N) M + 1 is equal to N M + 1 is equal to N

Comparisons Comparisons ((variables bound to expressionsvariables bound to expressions))– M < N, N > PM < N, N > P M < N & N > P M < N & N > P– M =< N, N >= PM =< N, N >= P M M N & N N & N P P– N =:= PN =:= P N = PN = P– N =\= PN =\= P N N P P

Page 13: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

ExerciseExercise

Write a predicate all_leq/2Write a predicate all_leq/2– each element of the first list is less than or each element of the first list is less than or

equal to the corresponding element of the equal to the corresponding element of the secondsecond

– (also, lists are the same length)(also, lists are the same length)?- ?- all_leq([1, 2, 3, 4], [1, 2, 3, 5]).all_leq([1, 2, 3, 4], [1, 2, 3, 5]).YesYes?- ?- all_leq([1, 2, 3, 4, 5], [7, 6, 5, 4, 3]).all_leq([1, 2, 3, 4, 5], [7, 6, 5, 4, 3]).NoNo

Page 14: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

By Cases in PrologBy Cases in Prolog

Suppose we want to write larger/3Suppose we want to write larger/3– 33rdrd argument is larger of 1 argument is larger of 1stst two two

Two cases:Two cases:– first argument is larger than secondfirst argument is larger than second– first argument is less than or equal to secondfirst argument is less than or equal to second

%% larger(+First, +Second, ?Larger)%% larger(+First, +Second, ?Larger)

larger(First, Second, First) :- First > Second.larger(First, Second, First) :- First > Second.

larger(First, Second, Second) :- First =< Second.larger(First, Second, Second) :- First =< Second.

Page 15: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Splitting a ListSplitting a List

Suppose we want to split a list by size:Suppose we want to split a list by size:?- split_by_size(10, [3, 56, 18, 7, 8, 10, 4], L, G).?- split_by_size(10, [3, 56, 18, 7, 8, 10, 4], L, G).L = [3, 7, 8, 10, 4]L = [3, 7, 8, 10, 4]G = [56, 18]G = [56, 18]

33rdrd is elements of 2 is elements of 2ndnd less than or equal to 1 less than or equal to 1stst

44thth is elements of 2 is elements of 2ndnd greater than 1 greater than 1stst

How do we do that?How do we do that?

Page 16: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Specifying the SplittingSpecifying the Splitting

Each element of 2Each element of 2ndnd in either 3 in either 3rdrd or 4 or 4thth

split_by_size(N, [H | T2], [H | T3], T4)split_by_size(N, [H | T2], [H | T3], T4)oror

split_by_size(N, [H | T2], T3, [H | T4])split_by_size(N, [H | T2], T3, [H | T4])– retains the order of elementsretains the order of elements– not necessary, but not ruled out (not necessary, but not ruled out (document itdocument it))

In 2In 2ndnd if H if H N, in 3 N, in 3rdrd if H > N if H > N– in either case, split_by_size(N, T2, T3, T4)in either case, split_by_size(N, T2, T3, T4)

Page 17: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Recursive CasesRecursive Cases

split_by_size(N, [H | T2], [H | T3], T4) :-split_by_size(N, [H | T2], [H | T3], T4) :-

H =< N,H =< N,

split_by_size(N, T2, T3, T4).split_by_size(N, T2, T3, T4).

split_by_size(N, [H | T2], T3, [H | T4]) :-split_by_size(N, [H | T2], T3, [H | T4]) :-

H > N,H > N,

split_by_size(N, T2, T3, T4).split_by_size(N, T2, T3, T4).

Page 18: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Base CaseBase Case

BeforeBefore the recursive cases the recursive cases 22ndnd argument = empty list argument = empty list

– thus 3thus 3rdrd argument = empty list argument = empty list– and 4and 4thth argument = empty list argument = empty list

split_by_size(_N, [], [], []).split_by_size(_N, [], [], []).

Page 19: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

DocumentationDocumentation

N compared to HN compared to H– so both must be knownso both must be known

N not bound to any other valueN not bound to any other value– must be given (+N)must be given (+N)

H bound to first element of two listsH bound to first element of two lists– one one or the otheror the other must be given (?L1, ?L2, ?L3) must be given (?L1, ?L2, ?L3)– note normal usage (split or join or test join)note normal usage (split or join or test join)

Page 20: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Split By Size DocumentationSplit By Size Documentation

%% split_by_size(+N, ?L, ?S, ?B)%% split_by_size(+N, ?L, ?S, ?B)%% normal use (+N,+L,-S,-B) or (+N,?L,+S,+B)normal use (+N,+L,-S,-B) or (+N,?L,+S,+B)%% elts. of S are elts. of L less than/equal to Nelts. of S are elts. of L less than/equal to N%% elts. of B are elts. of L greater than Nelts. of B are elts. of L greater than N%% elements in S and B in same order as in Lelements in S and B in same order as in L%% sample call:sample call:%% ?- split_by_size(10, [3, 21, 10, 12], S, B).?- split_by_size(10, [3, 21, 10, 12], S, B).%% S = [3, 10]S = [3, 10]%% B = [21, 12]B = [21, 12] (no other solutions)(no other solutions)

Page 21: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Getting Elements by PositionGetting Elements by Position

last/2 queries last element of a listlast/2 queries last element of a list nth1/3 queries nth element of a listnth1/3 queries nth element of a list

– starts counting at 1starts counting at 1 nth0/3 queries nth element of a listnth0/3 queries nth element of a list

– starts counting at 0starts counting at 0

Page 22: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

ExamplesExamples

?- ?- last(L, [a, x, e]).last(L, [a, x, e]).L = eL = e?- ?- nth0(2, [a, x, e], Elt).nth0(2, [a, x, e], Elt).Elt = eElt = e?- ?- nth1(N, [a, x, e], e).nth1(N, [a, x, e], e).N = 3N = 3

?- ?- last(_Last, [a, x, e]), nth0(1, [a, x, e], _Second), last(_Last, [a, x, e]), nth0(1, [a, x, e], _Second), nth1(2, L, _Second), nth1(1, L, _Last).nth1(2, L, _Second), nth1(1, L, _Last).

L = [e, x | _G1]L = [e, x | _G1]

Page 23: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Insertion and DeletionInsertion and Deletion

Insertion to & deletion from listsInsertion to & deletion from lists– just oppositesjust opposites– three way relationshipthree way relationship– shorter list, extra element, longer listshorter list, extra element, longer list– use same predicate for bothuse same predicate for both– select/3 in SWI-Prologselect/3 in SWI-Prolog?- ?- select(ListWithX, X, ListWithoutX).select(ListWithX, X, ListWithoutX).

Page 24: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Using select/3Using select/3

To delete an elementTo delete an element?- ?- select([g, o, n, e], n, L).select([g, o, n, e], n, L).L = [g, o, e]L = [g, o, e]

To insert an elementTo insert an element?- ?- select(L, n, [g, o, e]).select(L, n, [g, o, e]).L = [n, g, o, e] L = [n, g, o, e] ;;L = [g, n, o, e] L = [g, n, o, e] ;;L = [g, o, n, e] L = [g, o, n, e] ;;

Page 25: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Using select/3Using select/3

Deleting any elementDeleting any element?- ?- select([a, b, c], X, Rest).select([a, b, c], X, Rest).X = a, Rest = [b, c] X = a, Rest = [b, c] ;;X = b, Rest = [a, c] X = b, Rest = [a, c] ;;X = c, Rest = [a, b] X = c, Rest = [a, b] ;;NoNo

Page 26: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Variable Naming SuggestionVariable Naming Suggestion

Make names like picturesMake names like pictures

– X removed from LiXst leaves ListX removed from LiXst leaves List Good for appendGood for append

insert(X, List, LiXst) :-insert(X, List, LiXst) :-select(LiXst, X, List).select(LiXst, X, List).

delete(X, LiXst, List) :-delete(X, LiXst, List) :-select(LiXst, X, List).select(LiXst, X, List).

double_double(X, RXXSXXT) :-double_double(X, RXXSXXT) :-append(R, [X,X|SXXT], RXXSXXT),append(R, [X,X|SXXT], RXXSXXT),append(S, [X,X|T], SXXT).append(S, [X,X|T], SXXT).

Page 27: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

ExerciseExercise

Use select/3 to write one_moved/2, a Use select/3 to write one_moved/2, a predicate that says that its two (list) predicate that says that its two (list) arguments are different by only one element arguments are different by only one element being moved. For example:being moved. For example:?- ?- one_moved([t,r,a,i,l], [t,r,i,a,l]).one_moved([t,r,a,i,l], [t,r,i,a,l]).YesYes?- ?- one_moved([f,r,e,e,d], [f,r,e,d]).one_moved([f,r,e,e,d], [f,r,e,d]).NoNo

Page 28: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Start, Moves, GoalStart, Moves, Goal

Many problems can be formulated as Many problems can be formulated as getting from a start state to a goal state thru getting from a start state to a goal state thru a sequence of movesa sequence of moves– monkey program (section 2.5)monkey program (section 2.5)– travel program (section 4.4)travel program (section 4.4)– NDFAs (section 4.3)NDFAs (section 4.3)

Page 29: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Follow That MonkeyFollow That Monkey

Monkey example (section 2.5)Monkey example (section 2.5) States – four questionsStates – four questions

– which part of the room is the monkey in?which part of the room is the monkey in?– is the monkey on the box or on the floor?is the monkey on the box or on the floor?– where is the box?where is the box?– does the monkey have the banana?does the monkey have the banana?

Actions – grasp, climb, push, walkActions – grasp, climb, push, walk– last two go from place to placelast two go from place to place

Page 30: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

The Room (Initial State)The Room (Initial State)

doorbox

window

banana

monkey

atwindow middle atdoor

Page 31: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Start and Goal StatesStart and Goal States

Start state:Start state:– monkey is at the door (atdoor)monkey is at the door (atdoor)– monkey is on the floor (onfloor)monkey is on the floor (onfloor)– box is at the window (atwindow)box is at the window (atwindow)– monkey does not have the banana (hasnot)monkey does not have the banana (hasnot)

Goal state:Goal state:– monkey has the banana (has)monkey has the banana (has)– (don’t care about the others)(don’t care about the others)

Page 32: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Monkey GraspsMonkey Grasps

if the monkey is on the box in the middle of if the monkey is on the box in the middle of the room, thenthe room, then grasp grasping gets him the bananaing gets him the bananamove(state(middle, onbox, middle, hasnot),move(state(middle, onbox, middle, hasnot),

grasp,grasp,state(middle, onbox, middle, has)).state(middle, onbox, middle, has)).

((box must also be in the middle; monkey can’t box must also be in the middle; monkey can’t already have the banana; nothing else changesalready have the banana; nothing else changes))

Page 33: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Monkey ClimbsMonkey Climbs

if the monkey is on the floor next to the if the monkey is on the floor next to the box, then box, then climbclimbing gets him on the boxing gets him on the boxmove(state(Where, onfloor, Where, Has),move(state(Where, onfloor, Where, Has),

climb,climb,state(Where, onbox, Where, Has)).state(Where, onbox, Where, Has)).

((monkey & box stay in the same place; doesn’t monkey & box stay in the same place; doesn’t change whether the monkey has the banana or change whether the monkey has the banana or notnot))

Page 34: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Monkey Pushes the BoxMonkey Pushes the Box

if the monkey is on the floor next to the if the monkey is on the floor next to the box, then box, then pushpushing gets both to a new placeing gets both to a new placemove(state(OldLoc, onfloor, OldLoc, Has),move(state(OldLoc, onfloor, OldLoc, Has),

push(OldLoc, NewLoc),push(OldLoc, NewLoc),state(NewLoc, onfloor, NewLoc, state(NewLoc, onfloor, NewLoc,

Has)).Has)).((monkey & box both move to new place; monkey monkey & box both move to new place; monkey

stays on floor; doesn’t change whether the stays on floor; doesn’t change whether the monkey has the banana or notmonkey has the banana or not))

Page 35: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Monkey Walks SomewhereMonkey Walks Somewhere

if the monkey is on the floor, if the monkey is on the floor, walkwalking gets ing gets him to a new positionhim to a new positionmove(state(OldLoc, onfloor, Where, Has),move(state(OldLoc, onfloor, Where, Has),

walk(OldLoc, NewLoc),walk(OldLoc, NewLoc),state(NewLoc, onfloor, Where, Has)).state(NewLoc, onfloor, Where, Has)).

((monkey stays on floor; box doesn’t move; monkey stays on floor; box doesn’t move; doesn’t change whether the monkey has the doesn’t change whether the monkey has the banana or notbanana or not))

Page 36: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

To Get the BananaTo Get the Banana

canget/1 gets the monkey to the bananacanget/1 gets the monkey to the banana– query = initial statequery = initial state– base case = goal statebase case = goal state– recursive case: make any move & try againrecursive case: make any move & try again

Shows monkey Shows monkey cancan get the banana… get the banana… ……but doesn’t say howbut doesn’t say how Add a list to canget/1 to make canget/2Add a list to canget/1 to make canget/2

– list shows the moves the monkey makeslist shows the moves the monkey makes

Page 37: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

canget/2canget/2

%% canget(+State, ?Moves)%% canget(+State, ?Moves)%% monkey can get banana from State by monkey can get banana from State by

MovesMoves

% nothing to do if monkey % nothing to do if monkey has has bananabananacanget(state(_,_,_,has), []).canget(state(_,_,_,has), []).

% else make a move & continue% else make a move & continuecanget(S1, [Move | Moves]) :-canget(S1, [Move | Moves]) :-

move(S1, Move, S2),move(S1, Move, S2),canget(S2, Moves).canget(S2, Moves).

List is built as moves get madeWill change if backtracking occurs

Page 38: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

?- ?- canget(state(atdoor, onfloor, atwindow, hasnot), canget(state(atdoor, onfloor, atwindow, hasnot), Moves).Moves).

((doesn’t have it yetdoesn’t have it yet))

((can’t grasp itcan’t grasp it))

((can’t climb boxcan’t climb box))

((can’t push boxcan’t push box))

((walk somewhere (NewLoc)walk somewhere (NewLoc)))

state(NewLoc, onfloor, atwindow, hasnot)state(NewLoc, onfloor, atwindow, hasnot)

Page 39: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(NewLoc, onfloor, atwindow, hasnot), canget(state(NewLoc, onfloor, atwindow, hasnot), Moves).Moves).

((doesn’t have it yetdoesn’t have it yet))

((can’t grasp itcan’t grasp it))

((climb box (if he walked to the window)climb box (if he walked to the window)))

state(atwindow, onbox, atwindow, hasnot)state(atwindow, onbox, atwindow, hasnot)

Page 40: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(atwindow, onbox, atwindow, hasnot), canget(state(atwindow, onbox, atwindow, hasnot), Moves).Moves).

((doesn’t have it yetdoesn’t have it yet))

((can’t grasp itcan’t grasp it))

((can’t climb boxcan’t climb box))

((can’t push boxcan’t push box))

((can’t walk anywherecan’t walk anywhere))

((dead end – back up to walking somewheredead end – back up to walking somewhere))

Page 41: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(NewLoc, onfloor, atwindow, hasnot), canget(state(NewLoc, onfloor, atwindow, hasnot), Moves).Moves).

((didn’t have it yetdidn’t have it yet))

((couldn’t grasp itcouldn’t grasp it))

((climbing box didn’t workclimbing box didn’t work))

((push box somwhere (if he walked to the window)push box somwhere (if he walked to the window)))

state(NewLoc, onfloor, NewLoc, hasnot)state(NewLoc, onfloor, NewLoc, hasnot)

Page 42: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(NewLoc, onfloor, NewLoc, hasnot), canget(state(NewLoc, onfloor, NewLoc, hasnot), Moves).Moves).

((doesn’t have it yetdoesn’t have it yet))

((can’t grasp itcan’t grasp it))

((climb boxclimb box))

state(NewLoc, onbox, NewLoc, hasnot)state(NewLoc, onbox, NewLoc, hasnot)

Page 43: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(NewLoc, onbox, NewLoc, hasnot), canget(state(NewLoc, onbox, NewLoc, hasnot), Moves).Moves).

((doesn’t have it yetdoesn’t have it yet))

((grasp it – if he pushed the box to the middlegrasp it – if he pushed the box to the middle))

state(middle, onbox, middle, has)state(middle, onbox, middle, has)

Page 44: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

canget(state(middle, onbox, middle, has),canget(state(middle, onbox, middle, has), Moves).Moves).

((has it – nothing left to dohas it – nothing left to do))

Page 45: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

How to Get the BananaHow to Get the Banana

?- ?- canget(state(atdoor, onfloor, atwindow, hasnot), canget(state(atdoor, onfloor, atwindow, hasnot), Moves).Moves).

Moves = [walk(atdoor, atwindow),Moves = [walk(atdoor, atwindow),

push(atwindow, middle),push(atwindow, middle),

climb,climb,

grasp]grasp]

Page 46: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

NDFAsNDFAs

Simulating NDFAs works same waySimulating NDFAs works same way– list of symbols gets us from start to endlist of symbols gets us from start to end

May have multiple goal statesMay have multiple goal states– represented by final/1represented by final/1

Usually give list & see if it gets us to a goalUsually give list & see if it gets us to a goal– instead of giving initial state & finding list to instead of giving initial state & finding list to

get us to goalget us to goal

Page 47: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

NDFA MovesNDFA Moves

Like monkey moves, but simplerLike monkey moves, but simplertrans(s1, a, s1).trans(s1, a, s1).– getting/doing getting/doing aa in state in state s1s1 moves you to state moves you to state s1s1– cfcf. move(OldState, Move, NewState).. move(OldState, Move, NewState).

““Silent” moves can just happenSilent” moves can just happensilent(s2, s4).silent(s2, s4).– doing nothing in doing nothing in s2s2 can move you to can move you to s4s4

Page 48: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

NDFA AcceptanceNDFA Acceptance

Just like getting list of moves for monkeyJust like getting list of moves for monkeyaccept(State, []) :-accept(State, []) :-

final(State).final(State).((likelike canget(state(_,_,_,has), [])) canget(state(_,_,_,has), []))accept(State, [Move|Moves]) :-accept(State, [Move|Moves]) :-

trans(State, Move, NewState),trans(State, Move, NewState),accept(NewState, Moves).accept(NewState, Moves).

((likelike canget(FromState, ByMoves) canget(FromState, ByMoves) rulerule))

Page 49: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Planning for a TripPlanning for a Trip

At a given place, need to be somewhere elseAt a given place, need to be somewhere else Have certain moves availableHave certain moves available Want a list of moves to makeWant a list of moves to make Just like the monkeyJust like the monkey General goal search method appliesGeneral goal search method applies

– some specialization may helpsome specialization may help

Page 50: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Trip PredicatesTrip Predicates

canreach(FromCity, ToCity, Flights)canreach(FromCity, ToCity, Flights)– like canget/acceptlike canget/accept

flight(Number, From, To, Dep, Arr)flight(Number, From, To, Dep, Arr)– like move/translike move/trans

?- ?- canreach(halifax, detroit, Flights).canreach(halifax, detroit, Flights).

Flights = [fly(ac1023, halifax, toronto, 10:00, 12:05),Flights = [fly(ac1023, halifax, toronto, 10:00, 12:05),

fly(nw235, toronto, detroit, 14:30, fly(nw235, toronto, detroit, 14:30, 15:45)]15:45)]

Page 51: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

canreach/3canreach/3

canreach(City, City, []).canreach(City, City, []).canreach(From, To, [fly(N,From,Stop,D,A)|Flights]) :-canreach(From, To, [fly(N,From,Stop,D,A)|Flights]) :-

flight(N, From, Stop, D, A),flight(N, From, Stop, D, A),canreach(Stop, To, Flights).canreach(Stop, To, Flights).

flight(ac1023, halifax, toronto, 10:00, 12:05).flight(ac1023, halifax, toronto, 10:00, 12:05).flight(nw235, toronto, detroit, 14:30, 15:45).flight(nw235, toronto, detroit, 14:30, 15:45).flight(ac1035, halifax, moncton, 11:30, 12:15).flight(ac1035, halifax, moncton, 11:30, 12:15).

Page 52: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

More Elaborate PlanningMore Elaborate Planning

Travel planning (section 4.4)Travel planning (section 4.4)– not all flights go every daynot all flights go every day– whole trip on one daywhole trip on one day– allow 40 minutes for transfersallow 40 minutes for transfers

Otherwise just a lot of fluffOtherwise just a lot of fluff– P1-P3 : Fnum : Deptime P1-P3 : Fnum : Deptime is same as is same as

fly(Fnum, P1, P3, Deptime, _)fly(Fnum, P1, P3, Deptime, _)

Page 53: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Listing FlightsListing Flights

Flight database, p. 101 (p. 112, 2Flight database, p. 101 (p. 112, 2ndnde)e)– timetable/3 would be better as flight/6timetable/3 would be better as flight/6

flight(edinburgh, london, ba4733, 9:40, 10:50, alldays).flight(edinburgh, london, ba4733, 9:40, 10:50, alldays). Get cities just as fastGet cities just as fast Don’t have to break up structures to get at dataDon’t have to break up structures to get at data

– structures should be used to structures should be used to lumplump info together info together– each lump can be used as a unit (see family, 4.1,2)each lump can be used as a unit (see family, 4.1,2)

Page 54: Lists in Prolog Sections 3.1, 3.2. Testing a List n Suppose have defined female/1 and male/1 n Check whether a list is all male % all_male(?L) % -- male(X)

Next TimeNext Time

Controlling backtrackingControlling backtracking– Chapter 5Chapter 5