Pattern matching and unification ובקיצור - עליזה יעל נצר בינה מלאכותית מכללת אשקלון.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Using Rules Chapter 6. 2 Logic Programming (Definite) logic program: A  B 1, B 2, …, B m program clause (Horn) head body  A 1, A 2, …, A n goal clause.
1 Chapter 3 Knowledge Representation AI & ESChapter 3 2 Knowledge Representation Semantic Network Network Representation Conceptual Graph Frame.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
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.
1 CSC 550: Introduction to Artificial Intelligence Spring 2004 AI programs  Eliza  knowledge representation  pattern-matching  Predicate Calculus 
Functional Programming 02 Lists
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Natural deduction of logical proofs Kalish & Montegue: a set of heuristics for doing logical proofs Introduction rules –not introduction, and introduction,
Production Systems A production system is –a set of rules (if-then or condition-action statements) –working memory the current state of the problem solving,
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
1 Human-Computer Interaction Web Interface & Natural Language.
CS 100Lecture 281 CS100J Lecture 27 n Previous Lecture –Interfaces –Comparable –Reflection –super –Re-Reading: n Lewis & Loftus, Section 5.5 n Savitch,
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Control in LISP More on Predicates & Conditionals.
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.
Basic Introduction to Lisp
1 Pertemuan 8 Representing Knowledge Using Rules Matakuliah: T0264/Inteligensia Semu Tahun: Juli 2006 Versi: 2/1.
Chapter 6 Representing Knowledge Using Rules Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์
1 Simple Lisp Programs References:
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
CS 550 Programming Languages Jeremy Johnson
ML: a quasi-functional language with strong typing
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
Env. Model Implementation
The Metacircular Evaluator
Data abstraction, revisited
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
topics mutable data structures
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
Abstraction and Repetition
A User study on Conversational Software
Common Lisp II.
Lisp.
The general format of case is the following: (case <key form>
Presentation transcript:

Pattern matching and unification ובקיצור - עליזה יעל נצר בינה מלאכותית מכללת אשקלון

Men Are all alike. IN WHAT WAY. They're always bugging us about something or another. CAN YOU BE MORE SPECIFIC. Well, my boyfriend made me come here. YOUR BOYFRIEND MADE YOU COME HERE. He says I'm depressed much of the time. I AM SORRY TO HEAR YOU ARE DEPRESSED. It's true. I'm unhappy. DO YOU THINK COMING HERE WILL HELP YOU NOT TO BE UNHAPPY. I need some help, that much is certain. WHAT WOULD IT MEAN TO YOU IF YOU GOT SOME HELP. Perhaps I could learn to get along with my mother. TELL ME MORE ABOUT YOUR FAMILY.

על עליזה פותחה על ידי Joseph Weizenbaum – 1966 חיקוי של שיחה עם פסיכולוג רוג ' ריאני. הרוג ' ריאנים דוגלים בשיטה ה -nondirective, כלומר הבלתי - מנחה – מאפשרים למטופל להוביל ולגלות את עצמו. המטרה של וויצנבאום היתה ללמוד pattern- matching ( התאמת תבניות ) – אבל התגובה לתוכנית היתה אחרת. ects/intro.to.ai/lecture2slide3.html

אז איך זה עובד ? Specification: 1. Read input. 2. Find a pattern matching input. 3. Transform input into a response. 4. Print response. Back to 1

Pattern matching Pattern: (i need a X) Response: (what would it mean to you if you got a X ?) Match symbols with same symbols and variables with any symbol. Generalization of equal: (defun pat-match (pattern input) "Does pattern match input? Any variable can match anything." (if (variable-p pattern) t (if (or (atom pattern) (atom input)) (eql pattern input) (and (pat-match (first pattern) (first input)) (pat-match (rest pattern) (rest input))))))

Representing variables use naming convention on symbols. Any symbol with name starting with '?'. (defun variable-p (x) "Is x a variable (a symbol beginning with '?')?" (and (symbolp x) (equal (char (symbol-name x) 0) #\?)))

Extracting information from the match Want to get out the bindings of variables when successful. How to represent bindings? [difficult issue to optimize]. Take advantage of the SUBLIS primitive. (sublis '((?X. vacation)) '(what would it mean to you if you got a ?X ?)) (WHAT WOULD IT MEAN TO YOU IF YOU GOT A VACATION ?)

Pattern matching with variables (defun pat-match (pattern input) "Does pattern match input? BUGGY VERSION." (if (variable-p pattern) (list (cons pattern input)) (if (or (atom pattern) (atom input)) (eql pattern input) (append (pat-match (first pattern) (first input)) (pat-match (rest pattern) (rest input)))))) 4 bugs: a. (eql pattern input) can return T and append will die. b. (eql pattern input) can return NIL meaning failure, and be interpreted as an empty binding (semi-predicate problem). c. We want the bindings of variables to agree: (?X ?X) (1 2) fails. d. Inefficient to check first and rest when we know that first fails.

Bindings Management Need to be more careful about bindings: a. pass them as parameter to pat-match (pat-match pat input bindings) b. distinguish between failure and no-bindings. New abstraction: (defconstant fail nil) (defconstant no-bindings '((t. t))) (defun get-binding (var bindings) "Find a (var. value) pair in a binding list." (assoc var bindings)) (defun binding-val (binding) "Get the value part of a binding." (cdr binding)) (defun lookup (var bindings) "Get the value part (for var) from a binding list." (binding-val (get-binding var bindings))) (defun extend-bindings (var val bindings) "Add a (var. value) pair to a binding list." (cons (cons var val) bindings))

A (first) correct version of pat- match with binding management (defun pat-match (pattern input &optional (bindings no- bindings)) "Match pattern against input in the context of the bindings." (cond ((eq bindings fail) fail) ((variable-p pattern) (match-variable pattern input bindings)) ((eql pattern input) bindings) ((and (consp pattern) (consp input)) (pat-match (rest pattern) (rest input) (pat-match (first pattern) (first input) bindings))) (t fail)))

(defun match-variable (var input bindings) "Does VAR match input? Uses (or updates) and returns bindings." (let ((binding (get-binding var bindings))) (cond ((not binding) (extend-bindings var input bindings)) ((equal input (binding-val binding)) bindings) (t fail)))) > (pat-match '(i need a ?X) '(i need a vacation)) ((?X. vacation) (t. t))

Make extend-bindings smarter (defun extend-bindings (var val bindings) "Add a (var. val) pair to a binding list." (cons (var val) (if (eq bindings no-bindings) nil bindings))) > (pat-match '(1 2 3) '(1 2 3)) ((t. t)) > (pat-match '(?X is ?X) '((2 + 2) is 4)) nil > (pat-match '(?X is ?X) '((2 + 2) is (2 + 2))) ((?X 2 + 2))

Segment pattern matching - Similar to kleene-star notation in regular expressions: want to match any number of elements in a list. - Choose a syntax to write segment variables: (?* ?var) (defun segment-pattern-p (pattern) (and (consp pattern) (starts-with (first pattern) '?*))) > (pat-match '((?* ?x) is (?* ?p) ?) '(1 + 2 * 3 is ?)) ((?x * 3) (?p 5 + 2)) (defun pat-match (pattern input &optional (bindings no-bindings)) "Match pattern against input in the context of the bindings." (cond ((eq bindings fail) fail) ((variable-p pattern) (match-variable pattern input bindings)) ((eql pattern input) bindings) ((segment-pattern-p pattern) ;; *** (segment-pattern-match pattern input bindings)) ;; *** ((and (consp pattern) (consp input)) (pat-match (rest pattern) (rest input) (pat-match (first pattern) (first input) bindings))) (t fail)))

Segment Pattern Matching and backtracking How far should segment pattern match? Assume segment pattern is followed by a non variable C (pattern starts with ((?* ?p) C p2...)) Look for first occurrence of C in input: index pos. (... C i2...) If not found, fail. Else match up to pos and check that (rest input) matches (rest pattern) b2 = (pat-match (p2...) (r2...)) If b2 succeeds -> succeed and extend with binding ?p. Else? Maybe need to bind ?p to a longer segment. > (pat-match '((?* ?p) is a (?* ?x)) '(what he is is a fool)) ((?p what he is) (?x fool))

(defun segment-match (pattern input bindings &optional (start 0)) "Match segment pattern ((?* var). pat) against input." (let ((var (second (first pattern))) (pat (rest pattern))) (if (null pat) (match-variable var input bindings) ;; assume pat starts with a constant (let ((pos (position (first pat) input :start start :test #'equal))) (if (null pos)fail (let ((b2 (pat-match pat (subseq input pos) bindings))) ;; If this one failed, try a longer segment ;; Else check that the variables match (if (eq b2 fail) (segment-match pattern input bindings (+ pos 1)) (match-variable var (subseq input 0 pos) b2))))))))

A bug in segment-match! > (pat-match '((?* ?x) a b (?* ?x)) '(1 2 a b a b 1 2 a b)) --> NIL! (defun segment-match (pattern input bindings &optional (start 0)) "Match segment pattern ((?* var). pat) against input." (let ((var (second (first pattern))) (pat (rest pattern))) (if (null pat) (match-variable var input bindings) ;; assume pat starts with a constant (let ((pos (position (first pat) input :start start :test #'equal))) (if (null pos) fail (let ((b2 (pat-match pat (subseq input pos) (match-variable var (subseq input 0 pos) bindings)))) ;; If this one failed, try a longer segment (if (eq b2 fail) (segment-match pattern input bindings (+ pos 1)) b2))))))) > (pat-match '((?* ?x) a b (?* ?x)) '(1 2 a b a b 1 2 a b)) ((?X 1 2 A B))

ELIZA: A Rule-based translator (defun rule-pattern (rule) (first rule)) (defun rule-responses (rule) (rest rule)) Rule example: ((?* ?x) I want (?* ?y)) (What would it mean if you got ?y) (Why do you want ?y) (Suppose you got ?y soon)) If pattern matches, select one of the answers randomly.

How to handle a set of rules? Several rules match, what to do? - Option 1: accept the first matching rule. - Option 2: choose one rule randomly among the matching rules. - Option 3: combine the responses from several matching rules. Use option 1.

(defparameter *eliza-rules* '((((?* ?x) hello (?* ?y)) (How do you do. Please state you problem.)) (((?* ?x) I want (?* ?y)) (What would it mean if you got ?y) (Why do you want ?y) (Suppose you got ?y soon)) (((?* ?x) if (?* ?y)) (Do you really think it is likely that ?y) (Do you wish that ?y) (What do you think about ?y) (Really -- if ?y))))

A rule-based interpreter (defun eliza () "Respond to user input using pattern matching rules." (loop (print 'eliza>) (write (flatten (use-eliza-rules (read))) :pretty t))) (defun use-eliza-rules (input) "Find some rul with which to transform the input." (some #'(lambda (rule) (let ((result (pat-match (rule-pattern rule) input))) (if (not (eq result fail)) (sublis (switch-viewpoint result) (random-elt (rule-responses rule)))))) *eliza-rules*))

(defun switch-viewpoint (words) "Change I to you and vice-versa, and so on." (sublis '((I. you) (you. I) (me. you) (am. are)) words)) ;; aux functions (defun flatten (the-list) "Append together elements or lists in the-list" (mappend #'mklist the-list)) (defun mklist (x) "Return x if it is a list, otherwise (x)" (if (listp x) x (list x))) (defun random-elt (choices) (elt choices (random (length choices))))

What Eliza Can't Do In a word: anything. ELIZA defines the major areas of AI in terms of what she can't do and doesn't know. She doesn't know anything: Knowledge Representation (KR) She can't reason: Reasoning She can't plan: Planning She can't learn: Machine Learning She doesn't understand natural language: Natural Language Processing (NLP)