LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Declarative Programming Lists in PROLOG Autumn 2014.
CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
LING 364: Introduction to Formal Semantics Lecture 24 April 13th.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 13: 10/9.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/27.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.
LING 364: Introduction to Formal Semantics Lecture 8 February 7th.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/22.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
LING 364: Introduction to Formal Semantics Lecture 10 February 14th.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 9 February 9th.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.
LING 388 Language and Computers Lecture 14 10/16/03 Sandiway FONG.
LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/13.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
LING 388 Language and Computers Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 12: 10/5.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
LING 388 Language and Computers Lecture 18 10/30/03 Sandiway FONG.
LING 388 Language and Computers Lecture 7 9/23/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 7: 9/12.
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
LING 388 Language and Computers Lecture 15 10/21/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
LING 388: Language and Computers Sandiway Fong Lecture 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/30.
LING 388: Language and Computers Sandiway Fong Lecture 3.
LING 388: Language and Computers Sandiway Fong Lecture 6.
LING 388: Language and Computers Sandiway Fong Lecture 7 9/15.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Programming Paradigms Backus Naur Form and Syntax Diagrams.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Logic Programming Lecture 2: Unification and proof search.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
Presentation transcript:

LING 438/538 Computational 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 midnight – submission to me –acceptable formats:.doc (word),.txt (plain text),.pdf

3 Administrivia Class Mailing List –is active –you can use it for (homework) discussion etc. between yourselves –you should have received an 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 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]

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 [] [] [1,2,3] [2,3] [3] [1,2,3] = [1|[2,3]][1,2,3] = [1|[2|[3]]][1,2,3] = [1|[2|[3|[]]]] (powerpoint animation)

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). (base case) last([X|L],Y) :- last(L,Y). (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). (base case) last([X|L],Y) :- last(L,Y). (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) L= L= [1,2,3,1,2,3,1,2 3,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 1list 2concatenation 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 CALL s) –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).