Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.

Similar presentations


Presentation on theme: "LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31."— Presentation transcript:

1 LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31

2 Administrivia Homework 1 –given out in the lab class last Thursday –reminder: due today (midnight in my mailbox)

3 Last Time Prolog lists: –comma-separated notation [1,2,3,4,5] –head-tail notation [1|[2,3]] Recursion: –defining something in terms of itself –example: last/2 finding the last element of a list Today: –more on recursion...

4 Prolog Lists Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) Notes: –There are two cases –We use the head/tail notation in the recursive case –we list the base case first and the recursive case last because Prolog accesses its database in first-to-last order Captures intuition –Base Case: –i.e. simplest case last element of a list containing just one element –Recursive Case: we define last element of a nontrivial list in terms of the last element of its tail –captures idea that last [1,2,3] = last tail [1,2,3] tail [1,2,3] = [2,3] last [2,3] = last tail [2,3] tail [2,3] = [3] last [3] is 3 (by the case case) so last [1,2,3] is 3

5 Prolog Computation Summary Simple Procedure –Given a query, e.g. ?- q(A1,A2). –match query against the database in first-to-last order Fact –a fact matches the query q(B1,B2). –only if the predicate names match (here: q ) the number of arguments (arity) match (here: 2) –and each corresponding argument matches (here: A1 must match B1, and A2 must match B2 ) Rule –a rule matches the query if the head of the rule matches the query q(C1,C2) :- p(C1), r(C2). –if the head of the rule matches the query (like Fact-matching) ?- q(A1,A2). q(C1,C2) :- p(C1), r(C2). –the body of the rule creates new sub-queries that must all be true in Prolog’s world for the entire rule to be true ?- p(C1). ?- r(C2).

6 Prolog Lists Query ?- last([1,2,3],Z).Z=3 Computation procedure –?- last([1,2,3],Z). last([X],X). NO –match fact last([X],X). NO last([X|L],Y) :- last(L,Y). YES –match head of rule last([X|L],Y) :- last(L,Y). YES match names: last match arity: 2 match corresponding 1st arguments: [1,2,3] = [X|L] –X=1 L=[2,3] match corresponding 2nd arguments: –Y=Z create new sub-query: ?- last([2,3],Y). last([X],X). (base case) last([X|L],Y) :- last(L,Y). (recursive case)

7 Prolog Lists Sub-Query ?- last([2,3],Y). Computation procedure (2nd time around) –?- last([2,3],Y). last([X],X). NO –match fact last([X],X). NO last([X’|L’],Y’) :- last(L’,Y’). YES –match head of rule last([X’|L’],Y’) :- last(L’,Y’). YES match names: last match arity: 2 match corresponding 1st arguments: [2,3] = [X’|L’] –X’=2 L’=[3] match corresponding 2nd arguments: –Y=Y’ create new sub-query: ?- last([3],Y’). last([X],X). (base case) last([X|L],Y) :- last(L,Y). (recursive case)

8 Prolog Lists Sub-Query ?- last([3],Y’). Computation procedure (3rd time around) –?- last([3],Y’). last([X”],X”). YES –match fact last([X”],X”). YES match names: last match arity: 2 match corresponding 1st arguments: [3] = [X”] –X”=3 match corresponding 2nd arguments: –X”=Y’ no new sub-query We’re done! –X”=3, X”=Y’, Y=Y’, Y=Z Original query was ?- last([1,2,3],Z).Z=3 YES last([X],X). (base case) last([X|L],Y) :- last(L,Y). (recursive case)

9 Prolog tracing commands –?- trace.switch tracing on –[trace] ?-prompt –?- notrace.switch tracing off in trace mode –[return] creep (one step forward) trace mode display –Call about to evaluate –Exit succeeded –Redo see if query can be satisfied another way –Fail can’t be matched Let’s run through our example using the SWI-Prolog trace mechanism

10 Prolog Lists Query ?- last([],Z).No Computation tree –?- last([],Z).(Neither case matches!) No last([X],X). (base case) last([X|L],Y) :- last(L,Y). (recursive case)

11 Prolog Lists What happens to the computation tree for this query? –?- last(W,3).W=[3] (base case) W=[X] X=3 –; ?- last(W,3). (recursive case) W=[X|L] Y=3 –?- last(L,3). (base case) L=[3] »W = [X,3] –; –?- last(L,3). (recursive case) L=[X’|L’] Y’=3 –?- last(L’,3). (base case) L’=[3] »W = [X, X’,3] –; and so on… last([X],X). (base case) last([X|L],Y) :- last(L,Y). (recursive case) all lists with last element = 3 [3] [ _,3] [ _, _,3] [ _, _, _,3] and so on...

12 Computation tree Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) Query –?- last([mary,likes,john],Y). last([mary,likes,john],Y). [X] ≠ [mary,likes,john][X|L] = [mary,likes,john] View of recursive matching procedure as a computation tree last([likes,john],Y). [X’] ≠ [likes,john][X’|L’] = [likes,john] last([john],Y’). [X”] = [john] X” = john X” = Y’ Y’ = john Y = Y’ Y = john last([X”],X”). last([X’],X’). last([X],X).

13 Computation tree Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) last([mary,likes,john],Y). [X] ≠ [mary,likes,john][X|L] = [mary,likes,john] Answer: Y = john did we completely explore the search space?NO Ask for more answers (;) what happens? last([likes,john],Y). [X’] ≠ [likes,john][X’|L’] = [likes,john] last([john],Y’). [X”] = [john] X” = john last([X”],X”). last([X’],X’). last([X],X). [X”|L”] = [john] last([],Y”’). no match last([X”|L”],Y”).

14 Length of a List Usage –?- len(L,N). –L a list, N the length of L Definition –len([],0). (base case) –len([X|L],N) :- len(L,M), N is M+1. (recursive case) Notes –recursive definition (similar in style to last/2) –Prolog builtin predicate is/2 evaluates arithmetic expressions on the right- hand-side (RHS) Examples –?- len([john,[saw,mary]],X).X=2 –?- len([john,saw,mary], 4).No

15 Length of a List Usage –?- len(L,N). –L a list, N the length of L Definition –len([],0). (base case) –len([X|L],N) :- len(L,M), N is M+1. (recursive case) Example –?- len(L,3).what happens? First answer: [ _, _, _ ] what happens after you hit semicolon ? (try it for yourself in Prolog and see)


Download ppt "LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31."

Similar presentations


Ads by Google