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

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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.
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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
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.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
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 364: Introduction to Formal Semantics Lecture 4 January 24th.
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 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 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 16 10/23/03 Sandiway FONG.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.
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 388 Language and Computers Lecture 17 10/28/03 Sandiway FONG.
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.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
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 388: Language and Computers Sandiway Fong Lecture 6.
LING 388: Language and Computers Sandiway Fong Lecture 7 9/15.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
LING 388: Language and Computers Sandiway Fong 9/27 Lecture 10.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
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.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
Section 16.5, 16.6 plus other references
Tests, Backtracking, and Recursion
Programming Techniques
Presentation transcript:

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

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

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...

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

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).

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)

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)

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)

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

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)

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...

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).

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”).

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

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)