LING 438/538 Computational Linguistics Sandiway Fong Lecture 21: 11/7.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
-
date post
18-Dec-2015 -
Category
Documents
-
view
213 -
download
1
Transcript of LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
LING 438/538Computational Linguistics
Sandiway Fong
Lecture 3: 8/29
2
Administrivia
• Prolog– assume you’ve installed
SWI-Prolog– if you’ve not had
programming experience, have looked at LING 388 Lecture 2 exercises (and are ok with them)
– if you’ve had programming experience, have consulted the on-line tutorials
• Homework 1– some simple Prolog
exercises– out today– due in one week @
midnight– email submission to me– acceptable formats:
• .doc (word), • .txt (plain text), • .pdf
3
Administrivia
• Class Mailing List– [email protected]– is active– you can use it for (homework) discussion
etc. between yourselves– you should have received an email from
the listserv
4
Today’s Topics
• ... continue our introduction to Prolog
• Last time– Looked at how to state database facts about gold, silver and
bronze medals and how to define the rule of transitivity– Explored the computation tree to understand how Prolog
comes up with answers in a step-by-step fashion– Looked at one instance (left recursion) of how Prolog’s
computation rule may produce infinite loops
5
Today’s Topics
• Continue our introduction to Prolog
• Today– Prolog lists: comma-separated and head-tail notations
6
Prolog Lists
• Lists are one of Prolog’s main data structure types
• There are two ways of writing lists – “comma-separated” list notation– “head-tail” list notation
7
Prolog Lists“comma-separated” list notation
• Definition– an ordered set
• 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
8
Lists and Database Queries
• Database FactoneToFive([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]
9
Prolog Lists
• Representation– [1,2,3] and [3,2,1]
• contain the same elements but represent different lists (order matters)
• Decomposition– (recursive)– [1,2,3]
• can be thought of as the list consisting of• 1 followed by [2,3]
– [2,3]• can be thought of as the list consisting of• 2 followed by [3]
– [3]• can be thought of as the list consisting of• 3 followed by []
1 [2,3]
[1,2,3]
2 [3]
3 []
head tail
10
Prolog Lists
• Head-Tail Notation– [head|tail ]
• | is the vertical bar character separating the head from the tail
• comma-separated and head-tail notations can be mixed
• Examples– [1,2,3]= [1|[2,3]]
• i.e. list consisting of head 1 followed by tail [2,3]– [2,3]= [2|[3]]
• list consisting of head 2 followed by tail [3]– [3]= [3|[]]
• list consisting of head 3 followed by tail []
12
3 []
[1,2,3][2,3]
[3]
[1,2,3] = [1|[2,3]][1,2,3] = [1|[2|[3]]][1,2,3] = [1|[2|[3|[]]]] (powerpointanimation)
11
Prolog Lists
• Head and Tail– Note:
• []is the empty list• the empty list terminates a head/tail list• the empty list has no head• the empty list has no tail
• 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]]]]
12
Prolog Lists
• Lists with Variables in Rules– write a Prolog program that returns the last item in a list– define a predicate last(L,X) – that takes two arguments:
1. L: the list we’re interested in, and
2. X: 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
13
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– for more detailed slides on last/2,
• see LING 388 lecture 3 slides
14
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).last([X],X). (base case)(base case)
last([X|L],Y) :- last(L,Y).last([X|L],Y) :- last(L,Y).(recursive case)(recursive case)
15
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).last([X],X). (base case)(base case)
last([X|L],Y) :- last(L,Y).last([X|L],Y) :- last(L,Y).(recursive case)(recursive case)
16
Prolog Lists
• Prolog lists can contains lists as members
• Example– can represent bracketed
structures– [john,[saw,mary]]– is a list containing two items
• symbol john• list [saw,mary]
– different from– [john,saw,mary]
– ?- last([john,[saw,mary]],X).– ?- last([john,saw,mary], X).
john
saw mary
[saw,mary]
[john,[saw,mary]]
17
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
18
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?
19
An interesting list
• an infinite list L– L = [1,2,3|L]
• Notes– using both list notations– using a variable to stand for the
tail of a list– equating the tail to the list itself
• what happens for queries?– ?- last(L,X).– ?- len(L,X).
12
3
L=
L= [1,2,3,1,2,3,1,23,1,2,3,1,2,3|...]
20
List Concatenation
• Task– given two lists L1 and L2– concatenate L1 and L2 to form list L3
• Examples– L1 = [the,cat], L2 = [3,4,5]L3=[the,cat,3,4,5]
– L1 = [], L2=[john,mary] L3 = [john,mary]– L1 = [john,mary], L2=[] L3 = [john,mary]
• SWI-Prolog built-in– append(L1,L2,L3)
21
List Concatenation
• Definition• append/3 is already defined (built-in), so we use a new predicate name app/3
to avoid a naming clash– app([],L2,L2). (base case)– app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). (recursive
case)
• Base Case– app(L1, L2, L3)– list 1 list 2 concatenation of L1 and L2– if L1 is the empty list– L3 must be L2– by substitution, we have app([],L2,L2).
• Recursive Case– what if L1 is not empty?
22
List Concatenation
• Definition– app([],L2,L2). (base case)– app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). (recursive case)
• Recursive Case– what if L1 is not empty?
– e.g. L1=[1] L2=[2,3]– use head/tail notation
• factor off 1st element, see if that reduce the problem
– split L1=[1,2] into head 1 and tail []– we can already solve the tail case
• it’s the base case: L1’= [] L2’ = [2,3], L3’ = [2,3]
– answer is the head 1 • with tail = the answer to concatenation for the tail case• [1|L3’]
• = [1|[2,3]] = [1,2,3]
23
List Concatenation
• Definition– app([],L2,L2). (base case)– app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). (recursive case)
• Computation tree (3 steps)– app([1,2],[3,4],L).
• doesn’t match base case• matches recursive case provided • X=1, L1=[2], L2=[3,4], L=[1|L3]
– app([2],[3,4],L3).• doesn’t match base case• matches recursive case provided • X’=2, L1=[], L2’=[3,4], L3 = [2|L3’]
– app([],[3,4],L3’).• matches base case• L2” = [3,4], L3’ = L2”
– by substitution L is [1|[2|[3,4]]]• L = [1,2,3,4]
24
Question 1
• [438/538] Homework Question (3pts)– (A) How many inference steps does it take to run the following
query: • ?- app([1,2,3,4],[5],L).
– (B) How many inference steps does it take to run the following query:• ?- app([1],[2,3,4,5],L).
– (C) Explain why the number of steps differ despite the fact both queries return the same result.
– (for inference steps: count the number of CALLs)– you can do it manually or use the Prolog step-by-step debugger (?-
trace.) – see manual
25
List Concatenation
• Definition– app([],L2,L2). (base case)– app([X|L1],L2,[X|L3]) :- app(L1,L2,L3).(recursive case)
• Other Queries– non-concatenative uses
– ?- app([1,2],L,[1,2,3,4]). L=[3,4]
– ?- app(L,[3,4],[1,2,3,4]). L=[1,2]
26
Question 2
• [438/538] Homework Question (6 pts)– give the answer and– draw the complete computation tree (see lecture 2) for the
following queries?- app(X,Y,[1,2,3]).?- app(_,[X],[1,2]).
• [438/538] Homework Question (2 pts)– what does the general form of the 2nd query compute for some
arbitrary list L??- app(_,[X],L).
27
Question 3
• [optional 438/mandatory 538]• Homework Question (4 pts)
– explain what happens for the following query?
?- app(X,Y,Z).