Download presentation
Presentation is loading. Please wait.
1
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29
2
Administrivia Homework 1 –given out in the lab class last Thursday –reminder: due this Thursday –I’ve gotten about 10 homeworks submitted so far Mailing list –ling388@listserv.arizona. eduling388@listserv.arizona. edu –you should have received an email from the listserv –you can use it for homework discussion etc.
3
Today’s Topics Prolog lists Recursion: –defining something in terms of itself
4
Prolog Lists Definition: –an ordered set –i.e. the order of list elements matters Prolog has two ways for writing a list –both can be used –“comma-separated” notation –head/tail notation Comma-separated list notation Examples: –[1,2,3,4,5] a list of the numbers from 1 to 5 in ascending order –[5,4,3,2,1] a different list from [1,2,3,4,5] –[the,cat,sat,on,the,mat] a list of the 6 words in the sentence the cat sat on the mat in left to right order –[] the empty list - a list with no members
5
Prolog Lists Database Fact oneToFive([1,2,3,4,5]). Database Query ?- oneToFive([1,2,3,4,5]).Yes ?- oneToFive([5,4,3,2,1]).No Database Query with Variables ?- oneToFive([1,2,X,4,Y]).X=3 Y=5 ?- oneToFive(X).X=[1,2,3,4,5]
6
Prolog Lists Prolog lists can contains lists as members Example: –[[1,2],[3,4],5] –is a list with three members –a list [1,2] followed by –another list [3,4] followed by –a single number 5 Example –represent bracketed structures –[john,[saw,mary]] –is a list containing two items symbol john list [saw,mary] –different from –[john,saw,mary] john saw mary
7
Prolog Lists Head and Tail –[1,2,3,4,5] and [5,4,3,2,1] are different order matters! –How are they represented? –Mathematically, we can think of lists as being composed of a head plus a tail –Prolog has a special notation for this nesting for lists [ head | tail ] | is the vertical bar character separating the head from the tail Example: [1,2,3] 1 [2,3] [1,2,3] 2 [3] 3 [] head tail
8
Prolog Lists Head and Tail –[1,2,3,4,5] and [5,4,3,2,1] can also be written in Prolog using head/tail notation as: –[1|[2|[3|[4|[5|[]]]]]] –[5|[4|[3|[2|[1|[]]]]]] respectively –Note: [] is the empty list and has to be explicitly represented in the head/tail notation We can even mix the two representations in the same list –[1|[2,3,4,5]] –[1|[2|[3,4,5]]] –[1,2,3|[4,5]] –[1,2,3,4|[5|[]]] –[1,2,3,4|[5]]] –[1,2|[3|[4|[5]]]]
9
Prolog Lists Lists with Variables in Rules –Write a recursive definition that returns the last item in a list –We define a predicate last that takes two arguments: 1.the list we’re interested in, and 2.the last item –To handle sample queries like: ?- last([1,2,3,4,5],5).Yes ?- last([1,2,3,4,5],2).No ?- last([1,2,3],X).X=3 ?- last([],X).No
10
Prolog Lists Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) Notes: –Two cases –We use the head/tail notation in the recursive case
11
Prolog Lists Query ?- last([1,2,3],Z).Z=3 Computation tree –?- last([1,2,3],Z).X=1 L=[2,3]Y=Z ?- last([2,3],Y).X’=2 L’=[3]Y=Y’ –?- last([3],Y’). »Y’= 3 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)
12
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)
13
Last/2 Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) How to read this definition –case 1: base case last([X], X). first argument 2nd argument LISTLAST ELEMENT if the first argument matches [X] then the last element is X [X] stands for a list with a single member denoted by the variable X e.g. like [1] or [mary] or [[p]]then X = 1 or X = mary or X = [p] ? - last([mary],X).X=mary
14
Last/2 Definition –last([X],X). (base case) –last([X|L],Y) :- last(L,Y). (recursive case) How to read this definition –case 2: recursive case last([X|L], Y) :- last(L,Y). first argument 2nd argumentif LISTLAST ELEMENT if the first argument matches [X|L] then the last element is Y provided last(L,Y) is true [X|L] stands for a list with head denoted by the variable X and tail denoted by L e.g. like [1,2] or [mary,likes,john] or [[p]] then X = 1 or X = mary or X = [p] and L = [2] or L = [likes,john] or L = [] ?- last([mary,likes,john],Y).true if last([likes,john],Y) is true ?- last([likes,john],Y).true if last([john],Y) is true ?- last([john],Y).true if Y = john(case 1: base case)
15
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] Prolog’s computational rule tries to match database starting with 1st rule 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).
16
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”).
17
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
18
Prolog tracing SWI-Prolog –trace mechanism gives a linear rendition of the computation tree –sometimes a bit difficult to read, i.e. see what’s going on need some practice –Prolog keeps track of what it has tried and what it hasn’t this allows Prolog to return multiple answers Example: ?- trace. Yes [trace] ?- last([mary,likes,john],Y). Call: (7) last([mary, likes, john], _G322) ? creep Call: (8) last([likes, john], _G322) ? creep Call: (9) last([john], _G322) ? creep Exit: (9) last([john], john) ? creep Exit: (8) last([likes, john], john) ? creep Exit: (7) last([mary, likes, john], john) ? creep Y = john ; Redo: (9) last([john], _G322) ? creep Call: (10) last([], _G322) ? creep Fail: (10) last([], _G322) ? creep Fail: (8) last([likes, john], _G322) ? creep Fail: (7) last([mary, likes, john], _G322) ? creep No
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.