Alok Mehta - Programming in Lisp - Macros 1 66-2210-01 Programming in Lisp Arrays, Macros, Expert Systems, Misc.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
A problem with functions: arguments must always have values that can be worked out Whenever we call a function we give it arguments. Lisp then works out.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
>(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?
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
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)
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CSCI Programming in Lisp; Instructor: Alok Mehta1 CSCI 2210: Programming in Lisp Introduction to Lisp.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
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.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
CSCI 2210: Programming in Lisp
PRACTICAL COMMON LISP Peter Seibel 1.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
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.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
CSCI 2210: Programming in Lisp
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
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.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
User-Written Functions
Section 15.4, 15.6 plus other materials
Defining Macros in Lisp
Example of formula (defun roots (a b c) (list
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
The Metacircular Evaluator
J.E. Spragg Mitthögskolan 1997
slides created by Marty Stepp
topics mutable data structures
Defining Macros in Lisp
Announcements Quiz 5 HW6 due October 23
Common Lisp II.
Defining Macros in Scheme
The general format of case is the following: (case <key form>
Presentation transcript:

Alok Mehta - Programming in Lisp - Macros Programming in Lisp Arrays, Macros, Expert Systems, Misc.

Alok Mehta - Programming in Lisp - Macros 2 Print, Read  Print  Example > (print '(A B))  Evaluates a single argument –Can be a constant (ex. 100), a variable (x), or any single expression  Prints its value on a new line  Returns the value printed  Read - reads a single expression  Example: > (read)20 23 (A B) C 20 ;; Note: Only the 20 is read; rest is ignored > (progn (print 'Enter-temperature) (read)) Enter-temperature 20 20

Alok Mehta - Programming in Lisp - Macros 3 Format  Format  Allows more elegant printing > (progn (print "Enter temperature: ") (read)) "Enter temperature: " > (progn (format t "~%Enter temperature: ") (read)) Enter temperature: –The second parameter (t) is the output buffer (T=stdout) –The character “~” signifies that a control character follows –The character “%” signifies a newline (Lisp: “~%” C: “\n”) –The characters “~a” tells Lisp to substitute the next value printf ("The value is ( %d, %d )", x, y); /* A C stmt */ > (format t "The value is ( ~a, ~a )" x y) ;; Lisp way > (format t "The value is ( ~10a, ~a )" x y) ; Can get fancy

Alok Mehta - Programming in Lisp - Macros 4 Streams  Write output to a file (e.g. knowledge base)  Prototype of with-open-file (with-open-file ( :direction ) …)  Example > (setf fact-database '((It is raining) (It is pouring) (The old man is snoring))) > (with-open-file (my-file ”myfile.lsp” :direction :output) (print fact-database my-file))

Alok Mehta - Programming in Lisp - Macros 5 My-Trace-Load  Redefine the built-in Lisp “Load” function  Example of Built In function > (load "a.lsp")  Additional requirements –Want to print each expression that is read in. –Want to print the value returned by the expression  Definition (defun my-trace-load (filename &aux next-expr next-result) (with-open-file (f filename :direction :input) (do ((next-expr (read f nil) (read f nil))) ((not next-expr)) (format t "~%~%Evaluating '~a'" next-expr) (setf next-result (eval next-expr)) (format t "~% Returned: '~a'" next-result))))

Alok Mehta - Programming in Lisp - Macros 6 Read-Line, Read-Char  Read-Line  Reads an individual line (terminated by a carraige return)  Returns it as a character string > (read-line)Hello World “Hello World”  Read-Char  Reads an individual character  Returns it as a character > (read-char)x #\x ;; This is Lisp notation for the character ‘x’

Alok Mehta - Programming in Lisp - Macros 7 Good Programming Style  Make liberal use of comments  Give procedures, variables, and methods meaningful names  Procedures should be short –Not too many arguments –Well defined goal –Appropriate level of abstraction and modularity  Presume as little as possible about the situation in effect –Don’t make use of too many global variables –Check to ensure that parameters are of the type expected –Define assertions, and validate them in procedures  Use of good programming style –Makes programs easy to write, read, and understand –Reduces the number of bugs, and the difficulty in finding bugs –Chapter 10 gives tips on debugging

Alok Mehta - Programming in Lisp - Macros 8 Trace  Trace  Format (trace )  Example > (defun factorial (x) (if (<= x 1) 1 (factorial (- x 1)))) > (trace factorial)  Causes entry, exit, parameter, and return values to be printed –For EACH procedure being traced > (factorial 3) ; 1> FACTORIAL called with arg: 3 ; | 2> FACTORIAL called with arg: | 2 ; | | 3> FACTORIAL called with arg: | | 1 ; | |3< FACTORIAL returns value: | | 1 ; | 2< FACTORIAL returns value: | 2 ; 1< FACTORIAL returns value: 6 6  Untrace stops tracing a procedure: (untrace )

Alok Mehta - Programming in Lisp - Macros 9 Arrays  Array  Data type; elements are numerically indexed  Method 1: Use a list > (setf a '(a b c d e f)) > (nth 3 a) D  Disadvantages of using a list to implement an array –Update is messy. For example, to set element 3 of a to ‘Z’ (to obtain the list ‘(A B C Z E F)’): > (append (butlast a 2) '(Z) (nthcdr 4 a)) => (append (A B C) (Z) (E F)) => (A B C Z E F) –Multi-dimensional arrays are messier

Alok Mehta - Programming in Lisp - Macros 10 Arrays (cont.)  Lisp has a built in Array data type  Creating arrays –Create a one-dimensional array, initial size = 4 > (setf a (make-array 4)) – Create a two dimensional 8x8 array > (setf board (make-array (3 3))) –Create a two dimensional array, with initial contents > (setf board2 (make-array (3 3) :initial-contents '((X O NIL) (O X NIL) (NIL X O)) ))  Accessing/updating elements: aref > (aref a 2) ; Get element at index 2 > (aref board 2 1) ; Gets element at index (2,1) > (setf (aref a 2) 87) ; Sets element at index 2 to “87”

Alok Mehta - Programming in Lisp - Macros 11 Macros  Typical procedure call (defun)  Evaluate arguments  Call procedure  Bind arguments to variables inside the procedure  Macro procedure (defmacro)  Macros do not evaluate their arguments  When a macro is evaluated, an intermediate form is produced  The intermediate form is evaluated, producing a value

Alok Mehta - Programming in Lisp - Macros 12 Example: Manually Define Pop  Review of the built in function “Pop”:  Implements Stack data structure “Pop” operation > (setf a '( )) > (pop a) 1 > a ( )  How would you emulate this using other functions? –Attempt 1: Remove the element “1” from A > (setf a (rest a)) ( );; A is set correctly to ( ), but we want “1” to be returned –Attempt 2: Remove first element AND return it > (prog1 (first a) (setf a (rest a))) –Attempt 3: Write a Lisp expression that generates above expression > (list 'prog1 (list 'first a) (list 'setf a (list 'rest a)))

Alok Mehta - Programming in Lisp - Macros 13 Our-Pop, using Macro  Convert Lisp expression into a macro (Our-Pop) > (defmacro our-pop (stack) (list 'prog1 (list 'first stack) (list 'setf stack (list 'rest stack)))) –Note similarity to Defun  Example Call > (OUR-POP a)  Notes –The parameter “A” is NOT evaluated –“A” is substituted for stack wherever the variable stack appears (list 'prog1 (list 'first a) (list 'setf a (list 'rest a))) –Intermediate form is generated (prog1 (first a) (setf a (rest a))) –Intermediate form is evaluated A is set to (rest A); the first element is returned

Alok Mehta - Programming in Lisp - Macros 14 Our-Pop using Defun  Why doesn’t this (defun) work the same way? > (defun our-pop (stack) (prog1 (first stack) (setf stack (rest stack)))) > (setf a '( )) > (our-pop a) 1 > a ( )  Reason: Lisp passes parameters “by-value” –The value of A is COPIED into the variable “stack” –Any changes to the variable “stack” are done to the COPY, and NOT the original variable A –When the function returns, the original value of A is unchanged

Alok Mehta - Programming in Lisp - Macros 15 Significance of Eval Steps  Macro evaluation has several steps (as noted) –The parameter “A” is NOT evaluated –“A” is substituted for stack wherever the variable stack appears –Intermediate form is generated –Intermediate form is evaluated  Note that A is evaluated at step 4 above (not step 1)  Why does this matter? –Answer: For the same reason that it matters in C/C++ macros –You may not want arguments evaluated at all –Or, you may want them evaluated multiple times –Macros give this flexibility

Alok Mehta - Programming in Lisp - Macros 16 Backquotes  Significance of Evaluation Steps (cont)  Consider > (defmacro our-if-macro (conditional then-part else-part) (list 'if conditional then-part else-part)) > (defun our-if-fun (conditional then-part else-part) (if conditional then-part else-part)) > (if (= 1 2) (print "Equal") (print "Not Equal")) –Lisp evaluates all parameters of OUR-IF-FUN before function is called  Backquote Mechanism  Forward quotes: Entire next expression is not evaluated > (defun temp () (setf a '(a b c d e)))  Backquote: Next expression is not evaluated (with exceptions) > (defun temp () (setf a `(a b c d e))) > (defun temp (x) (setf a `(a b c d e,x)) –The “,x” expression is evaluated; the value of X is used.

Alok Mehta - Programming in Lisp - Macros 17 Backquotes (cont.)  Exceptions - Backquote evaluates the following ,variable - Evaluates the value of the variable > (setf x '(h i j)) > (setf a `(a b c,x e f)) (A B C (H I J) E F) - Splices the elements of a list > (setf a `(a b e f)) (A B C H I J E F)  Backquotes simplify macro development > (defmacro our-if-macro (conditional then-part else-part) (list 'if conditional then-part else-part)) ;; old way > (defmacro our-if-macro (conditional then-part else-part) `(if,conditional,then-part,else-part))

Alok Mehta - Programming in Lisp - Macros 18 Backquotes simplify macros  Original version of our-pop > (defmacro our-pop (stack) (list 'prog1 (list 'first stack) (list 'setf stack (list 'rest stack))))  Our-pop redefined using backquotes > (defmacro our-pop (stack) `(prog1 (first,stack) (setf,stack (rest,stack)))) –Syntax is much closer to the intermediate form  Macros can be defined with following parameters  Optional (&optional)  Rest (&rest)  Key (&key)

Alok Mehta - Programming in Lisp - Macros 19 Case Study: Expert Systems  Overview of using Lisp for  Symbolic Pattern Matching (Chapter 24)  Rule Based Expert Systems and Forward Chaining (Chapter 26)  Backward Chaining and PROLOG (Chapter 27)  Motivational example  Animal guessing application was very simplistic –Want a more sophisticated application  Given: –A set of Facts –A set of Rules  Desired result –Answer complex questions and queries

Alok Mehta - Programming in Lisp - Macros 20 Smarter Animal Guessing  Facts about an animal named “Joey” –F1. (Joey’s mother has feathers) –F2. (Joey does not fly) –F3. (Joey swims) –F4. (Joey is black and white) –F5. (Joey lives in Antarctica)  Rules about animals in general –R1. If (animal X has feathers) THEN (animal X is a bird) –R2. If (animal X is a bird) and (animal X swims) and (animal X does not fly) and (animal X is black and white) THEN (animal is a penguin) –R3. If (animal X’s mother Z) THEN (animal X Z) Example: if (animal X’s mother has feathers) then (animal X has feathers) –R4. If (animal X Z) THEN (animal’s mother Z)  Notes –By combining the facts and rules, we can deduce that Joey is a penguin, and that the Joey’s mother is a penguin.

Alok Mehta - Programming in Lisp - Macros 21 Symbolic Pattern Matching  Symbolic pattern matching example  Match F1 with the IF part of R1 –F1. (Joey’s mother has feathers) –R1. If (animal X has feathers) THEN (animal X is a bird)  The expression(Joey’s mother has feathers) matches the pattern (animal X has feathers).  The association (animal X = Joey’s mother) is implied  In general  Symbolic pattern matching –matching an ordinary expression (e.g. fact) to a pattern expression  Unification: more advanced version of pattern matching –match two pattern expressions to see if they can be made identical –Find all substitutions that lead to this

Alok Mehta - Programming in Lisp - Macros 22 Rule Based Expert System  Rule Based Expert Systems  Once the pattern matching step is done, then we know that Rule R1 can be combined with fact F1 –F1. (Joey’s mother has feathers) –R1. If (animal X has feathers) THEN (animal X is a bird)  The association (animal X = Joey’s mother), along with the second part of the rule (animal X is a bird) leads to a derived fact: –(Joey’s mother is a bird)

Alok Mehta - Programming in Lisp - Macros 23 Forward Chaining  Basic philosophy:  Given a set of rules R and a set of facts F, what new facts (DF) can be derived? –DF1: Joey has feathers (R3,F1) –DF2: Joey’s mother is a bird (R1, F1) –DF3: Joey is a bird (R1,DF1) [or, (R3,DF2)] –DF4: Joey’s mother does not fly (R4, F2) –DF5: Joey’s mother swims (R4, F3) –DF6: Joey’s mother is black and white (R4, F4) –DF7: Joey’s mother lives in Antarctica (R4, F5) –DF8: Joey is a penguin (R2, DF3, F2, F3, F4) –DF9: Joey’s mother is a penguin (R4, DF8) or (R2, DF2, DF5, DF4, DF6)

Alok Mehta - Programming in Lisp - Macros 24 Backward Chaining  Basic philosophy  Can a statement (e.g. Joey is a penguin) be proven given the current set of facts and rules?  Work backwards, to determine what facts, if true, can prove that Joey is a penguin (or prove that Joey is not a penguin). –B1. R2: (Joey is a penguin) IF (a) Joey is a bird; (b) Joey swims; (c) Joey does not fly; and (d) Joey is black and white –B2. R1: (Joey is a bird) IF (Joey has feathers) –B4. R3: (Joey has feathers) IF (Joey’s mother has feathers) –DF1. (Joey has feathers), since we know (Joey’s mother has feathers (F1) –DF2. (Joey is a bird), since we know (Joey has feathers) (DF1) –DF3. (Joey is a penguin), since (a), (b), (c), and (d) are known to be true (DF2, F3, F2, F4 respectively)  The fact (Joey is a penguin) can be derived

Alok Mehta - Programming in Lisp - Macros 25 Does this apply in the real world?  Given a clinical specimen, need to know what tests to perform? –Example facts about a specific patient specimen (to test whether a person has Syphilis): F1. Automated Reagin Test result is Reactive, Titer=8 F2. Microheme Agglutination Test is Non-Reactive F3. Specimen is from a pregnant woman F4. Prior history indicates a result of Reactive, Titer=2 F5. Prior test was performed 12 months ago –Example rules R1. IF (Automated Reagin Test is Reactive, Titer >= 4) AND (Microheme Agglutination Test is Non-Reactive) THEN (Rapid Plasma Reagin test must be performed) R2. IF (Specimen is from a pregnant woman) THEN (Microheme Agglutination Test must be performed) R3. IF (Specimen is from a pregnant woman) THEN (Automated Reagin Test must be performed twice) –Sample questions: What tests need to be performed? (Forward chaining) Should we do the RPR test? (Backward chaining) Is the specimen considered abnormal? (Backward chaining)

Alok Mehta - Programming in Lisp - Macros 26 Lisp  Lisp is a good language for implementing expert systems.  Concise programs  Flexible processing of lists  Basic implementations are shown in chapters  Other applications of expert systems  Mathematics: Calculus, geometry  Computer configuration, electronic circuits  Evaluate geological formations, planned investments  Diagnosis of infections

Alok Mehta - Programming in Lisp - Macros 27 Building an Expert System  Knowledge representation  how to represent facts, patterns, rules  how to represent sets of these  Build a pattern matcher  Build the inference engine  Forward Chaining  and/or Backward Chaining

Alok Mehta - Programming in Lisp - Macros 28 Implementing Pattern Matching  Want a procedure to match patterns  Input –Animal X has feathers (pattern) ((? X) has feathers) ; Uses (? Var) for pattern variable –Joey’s mother has feathers (regular expression or Fact) ((mother Joey) has feathers)  Returns –Mapping between pattern variables ( (X (mother Joey)) )  Example call > (match '((? X) has feathers) '((mother Joey) has feathers) ) ((X (mother Joey))) > (match '((? X) has (? Z)) '(Joey has feathers))) ((X Joey) (Z feathers))

Alok Mehta - Programming in Lisp - Macros 29 Return values of Match  Return value is a set of variable bindings –Example ((X Joey) (Z feathers)) –General Form ( (var1 value1) (var2 value2) … (varN valueN) )  What if the two patterns don’t match? –First attempt: On failure, return NIL > (match '((? X) has feathers)) '(Joey does not fly)) NIL  But consider... > (match '(Joey has feathers) '(Joey has feathers))) –This matches, but there is no need to bind any variables. –So, need to return SUCCESS with a list of zero variable bindings: ( ) NIL<-- NIL = ( )  Need to differentiate between failed match and match with no variable bindings

Alok Mehta - Programming in Lisp - Macros 30 Return values of Match (cont.)  Approach taken by Winston/Horn  Note: This is NOT the only way to do it –NIL = Success, empty list of pattern variables –FAIL = Symbol returned when the pattern and datum don’t match  Examples > (match '((? X) has feathers) '((mother Joey) has feathers)) ((X (mother Joey))) > (match '((? X) has (? Z)) '(Joey has feathers))) ((X Joey) (Z feathers)) > (match '((? X) has feathers)) '(Joey does not fly)) FAIL > (match '(Joey has feathers) '(Joey has feathers))) NIL; Treated as an empty list

Alok Mehta - Programming in Lisp - Macros 31 Match Function Definition  Function definition for MATCH: 4 basic branches ;; Calculates and returns bindings (if successful) ;; Or, returns 'FAIL (defun match (p d &optional bindings) ;; 1. If P and D are both atoms, ;; If they’re equal, it’s a match, otherwise FAIL ;; e.g. (match 'FEATHERS 'FEATHERS) ;; 2. If P is a pattern variable ;; Assign the value of D to the pattern variable in P ;; e.g. (match '(? X) 'JOEY) ;; should assign the value JOEY to the variable X ;; 3. If P and D are both Lists ;; Recursively solve for matches ;; e.g. (match '(A B (? X) (D E)) '(A B C (D E))) ;; should recursively call match on (A vs. A) ;; (B vs. B) ((? X) vs. C) ((D E) vs. (D E)) ;; 4. Any other case (e.g. atom vs. list, etc.) ;; FAIL )

Alok Mehta - Programming in Lisp - Macros 32 Match: Case 1, Case 4  Part 1:  Build up Cond infrastructure  Implement case 1 (P and D are both atoms)  Implement case 4 (Everything Else) (defun match (p d &optional bindings) (cond ((and (atom p) (atom d)) ;; Case 1: Both p and d are atoms ;; If P and D are equal: Match. Return bindings ;; Otherwise, return FAIL (if (eql p d) bindings 'FAIL)) (…Case 2…) (…Case 3…) (t ;; Case 4: Any other case. Return FAIL 'FAIL) )

Alok Mehta - Programming in Lisp - Macros 33 Match: Case 3  Case 3 (P and D are lists and need to be solved recursively) –Algorithm ;; A) Match the first pair, to get new bindings ;; B) If first pair failed, ;; C) return fail ;; D) Otherwise, using the bindings returned ;; by step (A), match remaining pairs –Code (defun match (p d &optional bindings) (cond (…Case 1…) (…Case 2…) ((and (listp p) (listp d)) ; P and D are both Lists ;; Recursively solve for matches (let ((result (match (first p) (first d) bindings))) ;(A) (if (eq 'fail result) ;(B) 'FAIL ;(C) (match (rest p) (rest d) result)))) ;(D) (…Case 4…) ))

Alok Mehta - Programming in Lisp - Macros 34 Match, Case 2  Case 2: P is a variable, D is a piece of data  Example: P=(? X); D=Joey  Make the binding (X Joey)  Add it to the bindings already defined –Old Bindings: ( (A apple) (B banana) ) –After adding: ( (X Joey) (A apple) (B banana) )  Code for adding a new binding to a list of bindings (defun add-binding (p d bindings) (cons (list (second p) d) bindings))

Alok Mehta - Programming in Lisp - Macros 35 Match, Case 2 (continued)  Note: –Before adding a binding, need to check if the binding already exists –If binding exists, it should match previous binding  Example 1 > (match '((? X) (? Y) implies (? X) (? Z)) (Joey smokes implies Joey (will get cancer))) ( (X Joey) (Y smokes) (Z (will get cancer))) NOT ((X Joey) (Y smokes) (X Joey) (Z (will get cancer))) –When the algorithm finds (X Joey), no new binding should be created  Example 2 > (match '((? X) (? Y) implies (? X) (? Z)) (Joey smokes implies Mary (will get cancer))) FAIL –This should fail because X cannot be bound to both Joey and Mary –When the algorithm finds (X Joey) while trying to bind (X Mary), the routine should return FAIL

Alok Mehta - Programming in Lisp - Macros 36 Match, Case 2 (continued) –Example 1 (match '(? X) 'Joey '((X Joey) (Y smokes) (Z (will get cancer))) –Example 2 (match '(? X) 'Mary '((X Joey) (Y smokes) (Z (will get cancer))) –Example 3 (match '(? X) 'Joey '((Y smokes) (Z (will get cancer))) –Algorithm ;; 1. Check if variable is already bound ;; 2. If bound, try to match the value of the variable ;; to the datum ;; 3. If bound and the binding matches, return binding ;; (nothing new needs to be done). (Example 1) ;; 4. If bound and the binding doesn’t match, return FAIL ;; (Example 2) ;; 5. If not bound, add a binding (Example 3)

Alok Mehta - Programming in Lisp - Macros 37 Match, Case 2 (continued) –Find-binding: Uses Assoc > (find-binding '(? X) '((X Joey) (Y smokes) (Z (will get…))) (X Joey) –Match (defun match (p d &optional bindings) (cond (…Case 1…) ((and (listp p) (eq (second p) '?)) ; Is p=~ (? X) (let (binding (find-binding p bindings)) ; (1) (if binding ; (2) (match (second binding) d bindings) ; (3,4) (add-binding p d bindings) ; (5) ))) (…Case 3…) (…Case 4…) ))