CSE (c) S. Tanimoto, 2002 AI Techniques

Slides:



Advertisements
Similar presentations
1 Programming Languages and Paradigms Lisp Programming.
Advertisements

Common Lisp! John Paxton Montana State University Summer 2003.
Lesson 4: Formatting Input Data for Arithmetic
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
CSE (c) S. Tanimoto, 2007 Production Sys. & Pattern Matching 1 CSE 415 Production Systems Many AI programs are “data driven.” The program examines.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
Common lisp A functional programming language. Useful URL:
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
CSE S. Tanimoto Pattern Matching 1 Pattern Matching Many Lisp programs are “data driven”. The program exams the data and then decides how to process.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
C++ Memory Management – Homework Exercises
Parsing 2 of 4: Scanner and Parsing
Using the Console.
Defining Macros in Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
Data Structures in Lisp
CS314 – Section 5 Recitation 1
6.001: Structure and Interpretation of Computer Programs
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Env. Model Implementation
LISP A brief overview.
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Chapter 8: Advanced Pattern Matching
First Lecture on Introductory Lisp
The Metacircular Evaluator
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Functions, Patterns and Datatypes
Modern Programming Languages Lecture 20 Fakhar Lodhi
Functions, Patterns and Datatypes
CSE S. Tanimoto Explicit Function Application
CSE (c) S. Tanimoto, 2004 AI Techniques
Functional Programming Concepts
Lisp: Using Functions as Data
LISP A brief overview.
Functions, Patterns and Datatypes
Functional Programming Concepts
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Functions, Patterns and Datatypes
6.001 SICP Interpretation Parts of an interpreter
Lisp: Using Functions as Data
Bindings, Scope, and Extent
Data Structures in Lisp
Data Structures in Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
Functional Programming Concepts
Bindings, Scope, and Extent
Common Lisp II.
Programming Languages
Functions, Patterns and Datatypes
Presentation transcript:

CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques Pattern Matching Many AI programs are “data driven”. The program exams the data and then decides how to process it. Rule-based programming: the program consists of a collection of rules and a means for applying them. CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Production System Form A rule of the form if [condition] then [action] is sometimes called a production rule. Lisp’s COND form provides a means to implement rule testing and application. ; Example structure: (COND (condition1 action1) (condition2 action2) ... (conditionN actionN) ) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

A Lisp Production System for Translation into Roman Numerals (defun to-roman () (let ((n nil))(loop (cond ((null n) (setq n (read))) ((equal n 0) (terpri) (setq n nil)) ((and (numberp n)(> n 0)(< n 4)) (princ #\I)(decf n)) ((and (numberp n)(= n 4)) (format t "IV")(decf n 4)) ((and (numberp n)(< n 9)) (princ #\V)(decf n 5)) ((and (numberp n)(> n 9)(< n 40)) (princ #\X)(decf n 10)) (t (format t "cannot handle ~S~%" n)(setq n nil)) ) ) ) ) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques Focus on the Condition ((equal n 0)) (terpri) (setq n nil)) A test to see whether the data possesses some particular property. We generally use predicates -- functions that return boolean values. Many conditions can be expressed as a comparison between the input data and a “pattern”. A trivial case is: (equal n 0); n = data, 0 = pattern. CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques The MATCH Function It’s not built into Common Lisp but defined in the text. It supports... 1. exact matching of 1-level lists (match '(hello there) my-input) 2. wild-card construct, e.g., (match '(my name is (? X)) '(my name is john) ) 3. wild-card with restrictive predicate, e.g., (match '(age (numberp age) '(age 21))) 4. wild-sequence construct, e.g., (* LST) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran)) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

What MATCH Returns MATCH returns an association list. (match '(hello there) '(hello)) NIL (match '(hello there) '(hello there)) ((YES . YES)) (match '(my name is (? X)) '(my name is john)) ((X . JOHN)(YES . YES)) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

What MATCH Returns (Cont.) (match '(age (numberp age) '(age 21) )) ((AGE. 21)(YES . YES)) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran) ) ((X JIM AND JAN) (Y MANUEL AND TRAN) (YES . YES)) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Accessing Associations Returned by MATCH (match '(hello there) '(hello there)) ((YES . YES)) ;;; VAL is defined in MATCH.CL (val 'x '((x . 5)(y . 10)) 5 (val 'x (match '(hi (? x))'(hi there))) THERE CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Accessing Associations (Cont.) (match '((* STUFF) is (? NAME)) '(my name is john)) ((STUFF MY NAME)(NAME . JOHN)(YES . YES)) (setq result *) (val 'stuff result) (MY NAME) (val ’name result) JOHN (val 'y result) NIL CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

The MATCH Function (continued) MATCH is defined in the file MATCH.CL You must load this file explicitly to use it. (load "MATCH.CL") or, if you have renamed the file to match.lsp, use (load "match.lsp") Make sure there is a copy of the file in your current directory. CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques Getting User Input (READ) returns the next form from standard input. (READ-LINE) returns a string containing the next line of text from standard input. To get a line of text as a list, we can use: (defun get-input-as-list (prompt-string) (format t "~A~%" prompt-string) (read-line) ; Swallow & ignore current line. (read-from-string ;Get a form from a string. (concatenate ;Combine some sequences, 'string ;result to be a string. "(" ;begin a list (read-line) ;words in the list ")" ;end of the list ) ) ) ) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Using GET-INPUT-AS-LIST (get-input-as-list "How are you?") How are you? I am fine (I AM FINE) 11 READ-FROM-STRING returns 2 values -- the form read and the number of characters used from the string. Therefore GET-INPUT-AS-LIST also returns 2 values. CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Using MATCH to Parse Input (load "MATCH.CL") (defun get-favorite-color () (let* ((ans (get-input-as-list "What is your favorite color?") ) (result (or (match '((? c)) ans) (match '(its (? c)) ans) (match '((? c) is my (* x)) ans) (match '(my (* x) is (? c)) ans) ) ) ) (if result (val 'c result) 'unknown) ) ) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Parsing Input with MATCH (Cont.) >(get-favorite-color) What is your favorite color? Its red RED Green is my favorite color GREEN black BLACK My very favorite is chartreuse CHARTREUSE CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Reading Math Formulas as English (load "MATCH.CL") (defun get-sum () (let* ((exp (get-input-as-list "Describe a particular sum") ) (result (or (match '((? a) plus (? b)) exp) (match '(the sum of (? a) and (? b)) exp) ) ) ) (if result (list '+ (val 'a result)(val 'b result)) ) ) ) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques

Reading Math Formulas (Cont.) >(get-sum) Describe a particular sum 5 plus 10 (+ 5 10) the sum of area and perimeter (+ AREA PERIMETER) CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques