LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Computer Science CPSC 322 Lecture 25 Top Down Proof Procedure (Ch 5.2.2)
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
LING 364: Introduction to Formal Semantics Lecture 24 April 13th.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 13: 10/9.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
1 Programming Languages and Paradigms Lisp Programming.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/27.
LING 388: Language and Computers Sandiway Fong 9/29 Lecture 11.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
LING 388: Language and Computers Sandiway Fong Lecture 12: 10/5.
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
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 9: 9/21.
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.
1 Lecture 5 Topics –Closure Properties for REC Proofs –2 examples Applications.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
LING 388 Language and Computers Lecture 7 9/23/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1.
LING 388: Language and Computers Sandiway Fong Lecture 7: 9/12.
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 Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
Formal Models of Computation Part II The Logic Model
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
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.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
LING 388: Language and Computers Sandiway Fong 9/27 Lecture 10.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Lisp.
PROLOG.
Presentation transcript:

LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5

2 Administrivia Homework 1 –reviewed today This Thursday –Computer Lab class –meet in Social Sciences 224 –Homework 2

3 Homework 1 Review Use database bird(tweety). “tweety is a bird” bird(woody). “woody is a bird” (2pts) What do the following queries return as an answer? The queries ?- \+ \+ bird(tweety). ?- \+ bird(X). (6pts) Give the logical reasoning for each answer ?- \+ \+ bird(tweety). Yes logic ?- trace. Yes [trace] ?- \+ \+ bird(tweety). Call: (9) bird(tweety) ? creep Exit: (9) bird(tweety) ? creep Yes (... doesn’t really give enough information) \+ \+ bird(tweety) is true if \+ bird(tweety) is false if bird(tweety) is true but bird(tweety) is true since it’s in the database so \+ bird(tweety) is false and so \+ \+ bird(tweety) is true that’s why Prolog says Yes

4 Homework 1 Review Use database bird(tweety). “tweety is a bird” bird(woody). “woody is a bird” (2pts) What do the following queries return as an answer? The queries ?- \+ \+ bird(tweety). ?- \+ bird(X). (6pts) Give the logical reasoning for each answer ?- \+ bird(X). No logic [trace] ?- \+ bird(X). Call: (8) bird(_G312) ? creep Exit: (8) bird(tweety) ? creep No (... shows us X gets instantiated as tweety ) \+ bird(X) is true if bird(X) is false but bird(X) is true when X=tweety so \+ bird(X) is false that’s why Prolog says No

5 Today’s Topics More on recursion Last time –we defined last/2 and len/2 to pick the last element of a list and compute the length of a list, respectively. last([X],X). (base case) last([X|L],Y) :- last(L,Y).(recursive case) len([],0). (base case) len([X|L],N) :- len(L,M), N is M+1. (recursive case)

6 Today’s Topics More on recursion –Look at taking apart and putting lists together –And symbols (atoms)

7 Building and Taking Atoms Apart Built-in predicate atom_chars/2 has two modes of usage –atom_chars(symbol,list) 1.takes names apart ?- atom_chars(will,X). – X is the list of characters representing will 2.builds names from a list of characters ?- atom_chars(X,[ ’ J’,o,h,n]). –use quotes around capitalized J to avoid J being interpreted as a variable ?- atom_chars(X,[w,o,n,’’’’,t]). – ’’’’ denotes the single quote (or ’\’’ )

8 Building and Taking Atoms Apart more example queries ?- atom_chars(has,[h,a,s]). –Yes ?- atom_chars(will,[w,X,l,l]). –X = i more example queries ?- atom_chars(X,Y). what happens here? ?- atom_chars(X,[J,o,h,n]).

9 Building and Taking Lists Apart append/3 is a built-in predicate in SWI-Prolog defined as follows: –append(L1,L2,L3) is true –if list L3 is the linear concatenation of lists L1 and L2 [1,2] append [3,4] = [1,2,3,4] Example –?- append([1,2],[3,4],[1,2,3,4 ]). Note –append/3 has multiple modes of usage –i.e. it can be used to take apart as well as concatenate lists let’s run example queries –?- append([1],[2,3],X). –?- append(X,Y,[1,2]). –?- append(_,[X],[1,2,3]). –?- append(X,Y,Z). Note: –the underscore character ‘_’ is a variable with no name –it’s a variable but we don’t care about its final value –no binding will be reported by the interpreter for underscores

10 Building and Taking Lists Apart append/3 can be defined recursively as follows: –app([],L,L). Base case –app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case we use the predicate name app/3 here to avoid a naming clash append/3 is already defined (built-in)

11 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: –if we concatenate [] with an arbitrary list L, we get L Recursive case says: –if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 –the answer is going to be a list headed by X –the tail is some other list L3 –where L3 is defined as –the concatenation of L1 and L2 Example ?- app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?- app([2,3],[4,5],L3).

12 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: –if we concatenate [] with an arbitrary list L, we get L Recursive case says: –if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 –the answer is going to be a list headed by X –the tail is some other list L3 –where L3 is defined as –the concatenation of L1 and L2 Example ?- app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?- app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?- app([3],[4,5],L3’).

13 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: –if we concatenate [] with an arbitrary list L, we get L Recursive case says: –if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 –the answer is going to be a list headed by X –the tail is some other list L3 –where L3 is defined as –the concatenation of L1 and L2 Example ?- app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?- app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?- app([3],[4,5],L3’). L3’ = [3|L3”] where L3” is given by ?- app([],[4,5],L3”).

14 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: –if we concatenate [] with an arbitrary list L, we get L Recursive case says: –if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 –the answer is going to be a list headed by X –the tail is some other list L3 –where L3 is defined as –the concatenation of L1 and L2 Example ?- app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?- app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?- app([3],[4,5],L3’). L3’ = [3|L3”] where L3” is given by ?- app([],[4,5],L3”). L3” = [4,5] ( Base case)

15 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] 1st answer

16 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]).

17 Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case 2nd answer L1 = [1] L2 = [2]

18 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] Example contd. ?- app(L1’,L2’,[2]). matches head of recursive case app([X|L1”],L2”,[X|L3”]) L1’ = [2|L1”] L2’ = L2” [2] = [X|L3”] L3” =[] ?- app(L1”,L2”,L3”). ?- app(L1”,L2”,[]).

19 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] Example contd. ?- app(L1’,L2’,[2]). matches head of recursive case app([X|L1”],L2”,[X|L3”]) L1’ = [2|L1”] L2’ = L2” [2] = [X|L3”] L3” =[] ?- app(L1”,L2”,L3”). ?- app(L1”,L2”,[]). matches app([],L,L). Base case L1” = [] L2”=L=[] 3rd answer L1 = [1,2] L2 = []

20 Building and Taking Lists Apart app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Example ?- app(L1,L2,[1,2]). has 3 answers L1 = [], L2 = [1,2] L1 = [1], L2 = [2] L1 = [1,2], L2 = [] in each case it is true that L1 concatenated with L2 is [1,2]