LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
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 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 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 364: Introduction to Formal Semantics Lecture 3 January 19th.
LING 388 Language and Computers Lecture 18 10/30/03 Sandiway FONG.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
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 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 12 10/9/03 Sandiway FONG.
LING 388 Language and Computers Lecture 6 9/18/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.
Formal Models of Computation Part II The Logic Model
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
Introduction to Python
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 3.
LING 388: Language and Computers Sandiway Fong Lecture 6.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Getting Started with Visual Prolog
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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.
LING/C SC/PSYC 438/538 Lecture 16 Sandiway Fong. SWI Prolog Grammar rules are translated when the program is loaded into Prolog rules. Solves the mystery.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
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 Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Section 16.5, 16.6 plus other references
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
ARTIFICIAL INTELLIGENCE LAB UCEL601 ( Second Term) Presented by Nirmala 1.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong.
Presentation transcript:

LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG

Administrivia Today Today  Computer Lab SBS 224  3 Exercises  Each exercise will end with one or more homework questions  Due date Next TuesdayNext Tuesday Submit answers to Charles answers to Charles

Administrivia From last class From last class  Install SWI-Prolog on your PC   Problems?  See Charles Lin Lecture2 slides Lecture2 slides  Download permission problem fixed  Slides now available in both PDF and Powerpoint formats

Last Class: Prolog Introduced… Introduced…  Basic Data Structures  Constants Names (begin with lower case letter)Names (begin with lower case letter) NumbersNumbers  Variables (begin with an upper case letter)  Complex Data Structures  Lists (delimited by square brackets)  Structures - functor/argument(s)

Last Class: Prolog Also introduced… Also introduced…  Clauses  Facts - Structures + ‘.’  Rules - head :- body + ‘.’  Disjunction (;)  Unification (=) - matching mechanism  Negation (\+)  Queries  Facts prefixed by ?-

Using SWI Prolog Start it up from the Program Menu Start it up from the Program Menu  Interpreter accepts queries (?-) Two methods of entering program clauses Two methods of entering program clauses u At the interpreter  ?- assert(modal(should)).  Note: asserta (add to the beginning),asserta (add to the beginning), assertz (add to the end)assertz (add to the end)

Using SWI Prolog Two methods of entering program clauses… Two methods of entering program clauses…  Consulting a file from the working directory  ?- consult(file).  Alternative notation: ?- [file]. - list notation?- [file]. - list notation Note: file should be the filename without the usual.pl extension Note: the file must be reloaded if the contents have been changed  How to find out what the working directory is ?- working_directory(X,Y).?- working_directory(X,Y). X - current working directoryX - current working directory Y - new working directoryY - new working directory

Prolog Syntax: Care With Spacing Simple guidelines Simple guidelines  Omit spaces between predicate/functor name and “(“,  Example: * pred (X)  Omit spaces before period at the end of a clause,  Example: * verb(run).  Spaces usually ok in other situations,  Example: headTail( [ H | T ], H, T ).  Need a space following end period if another clause follows on the same line,  Example: * a(1).a(2).

Exercise 1: Prolog Queries Enter program database: Enter program database:  modal(should). modal(could). modal(shall). modal(may). Run queries: Run queries:  ?- modal(X). Run negative queries: Run negative queries:  ?- \+ modal(be).  ?- \+ modal(should). Note: Note:  type ; (disjunction) to get Prolog interpreter to look for additional answers

Exercise 1: Prolog Queries Enter program facts: Enter program facts:  aux(am). aux(are). aux(is). aux(was). aux(were).  aux(do). aux(does). aux(did). Enter program rules for predicate hasCNeg/1: Enter program rules for predicate hasCNeg/1: “has contracted negated form” e.g. could - couldn’t, does - doesn’t  hasCNeg(X) :- modal(X).  hasCNeg(X) :- aux(X). Run queries: Run queries:  ?- hasCNeg(X).  ?- findall(X,hasCNeg(X),L).  ?- hasCNeg(sleep).

Exercise 1: Prolog Queries Homework Questions Homework Questions  Question (A)  What does the query ?- \+ modal(X) return?  Explain why it doesn’t return X = be cf. queries ?- modal(be).?- \+ modal(be).cf. queries ?- modal(be).?- \+ modal(be).  Question (B)  Modify predicate hasCNeg/1 to block ?- hasCNeg(shall).*shalln’t?- hasCNeg(shall).*shalln’t ?- hasCNeg(may).*mayn’t?- hasCNeg(may).*mayn’t ?- hasCNeg(am). *amn’t?- hasCNeg(am). *amn’t from succeeding  Hint: see previous lecture

Exercise 2: Building Names Built-in bidirectional predicate: atom_chars/2 Built-in bidirectional predicate: atom_chars/2  takes names apart  ?- atom_chars(will,X).  builds names from characters  ?- atom_chars(X,[‘J’,o,h,n]). Note: use quotes around capitalized J to avoid intepretation as a variableNote: use quotes around capitalized J to avoid intepretation as a variable  ?- atom_chars(X,[w,o,n,’’’’,t]). Note: ’’’’ denotes the single quoteNote: ’’’’ denotes the single quote

Exercise 2: Building Names Run queries: Run queries:  ?- atom_chars(has,[h,a,s]).  ?- atom_chars(will,[w,X,l,l]).  ?- atom_chars(X,Y).  ?- atom_chars(X,[J,o,h,n]). Note: Note:  atom_chars/2 is an example of a built-in predicate that has multiple modes of usage - very versatile  in other programming languages, you’d need a variety of functions  e.g. atom_to_chars, chars_to_atom, check_atom_with_chars and more…

Exercise 2: Building Names Another versatile built-in predicate (in SWI- Prolog) with multiple modes of usage: append/1 Another versatile built-in predicate (in SWI- Prolog) with multiple modes of usage: append/1  append(L1,L2,L3) concatenates lists L1 and L2 to form list L3  Run 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 the anonymous variable, no binding will be reported by the interpreter

Exercise 2: Building Names Homework question Homework question  Use both built-ins atom_chars/2 and append/3 to write a general rule  addNT/2 “add n’t” defined as follows:  addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart Y  Examples: could couldn’t, is isn’t  Make sure it (A) rejects may mayn’t(A) rejects may mayn’t (B) handles irregular forms can can`t, shall shan`t, will won`t(B) handles irregular forms can can`t, shall shan`t, will won`t

Exercise 3: Lists Recall alternative notation (| list separator) Recall alternative notation (| list separator)  [1|[2|[3]]] = [1,2,3]  or [1|[2,3]] - mixed form  Generally, in [H|T]H=head of list, T=tail of list Enter program fact: Enter program fact:  headTail([H|T],H,T). Run queries: Run queries:  ?- headTail([1,2,3],X,Y).  ?- headTail([],X,Y).  ?- headTail(X,1,[2]).  ?- headTail(X,[1],[2,3]).

Exercise 3: Lists How can built-in append/3 be defined? How can built-in append/3 be defined? Enter program clauses: Enter program clauses:  app([],L,L).  app([H|T],L,[H|U]) :- app(T,L,U).  Note: append/3 is taken (built-in), so we use app/3 Re-run queries: Re-run queries:  ?- app([1],[2,3],X).  ?- app(X,Y,[1,2]).  ?- app(_,[X],[1,2,3]).  ?- app(X,Y,Z).

Exercise 3: Lists Prolog Debugger Prolog Debugger  Use debugger to run program step-by-step  ?- debug. turn on debuggerturn on debugger  ?- trace. turn on tracingturn on tracing  ?- notrace. turns off tracing but stays in the debuggerturns off tracing but stays in the debugger  ?- nodebug. turns off debuggerturns off debugger

Exercise 3: Lists Homework Question Homework Question  (A) How many inference steps does it take to run the following query:  ?- app([1,2,3],[4],L).  (B) How many inference steps does it take to run the following query:  ?- app([1],[2,3,4],L).  (C) Explain why the number of steps differ despite the fact both queries return the same result.