Extended Pattern Matching
The pattern matcher described above is useful, but too limited for serious applications, such as the Lisp Critic. The Lisp Critic uses a similar but somewhat more complicated pattern matcher.
(extend-match:pat-match pattern item)
PAT-MATCH returns a list of binding lists. Each
binding list is one possible way the pattern can match the input
item. NIL means there were no matchs. Bindings are
dotted pairs, i.e.,
-
(X . A)means the variableXmatched the atomA -
(X A)means the variableXmatched the list(A)
PAT-MATCH is extensible. Custom pattern forms can
be added without modifying the source code. Some pattern forms already
defined include:
| Pattern | Matches |
|---|---|
(... (?*) ...) |
matches any list where ?* matches
zero or more intermediate elements; note that
(?*) does not itself bind to anything
|
(?and pat1 pat2 ...) |
matches anything that matches all of the patterns |
(?or pat1 pat2 ...) |
matches anything that matches any of the patterns |
(?not pat) |
matches anything that does not match the pattern |
The extensible matcher uses a question mark to mark pattern matching
operators, such as ?and and
?*.
New operators can be defined by users as described
here.
This however raises a potential issue with how to interpret
?FOO. Is it a variable or is it an extension like
?AND? If ?FOO is not an extension when we wrote
some code with a
pattern, but later we defines ?FOO to be an extension,
the pattern that
used to work will now behave differently, with no warning.
To avoid this ambiguity, we drop the convenient simple
?X syntax for pattern variables. Instead we
write (? X), or, in general,
(? variable-name). Now there
is no ambiguity.
-
?FOOis a pattern extension. -
(? FOO)is a pattern variable.
This approach also lets us generalize ? and ?* to take an optional
variable name. That means
-
we can use
(?)as an anonymous variable that matches any one element -
we can use
(?* variable)to collect the list of zero or more elements that?*matches.
Implementing Extensible Patterns
Applications that use pattern-directed inference typically requires many specialized pattern matching forms. For example, an application working with geographic data will probably want patterns that match objects based on nearness.
One way to do this would be add tests to a long if-then-else conditional, e.g.,
(defun match-p (pat form &optional (blists '(nil)))
(cond ((eql pat form) blists)
((or (atom pat) (atom form)) nil)
((eql (first pat) '?) (match-variable pat form blists))
((eql (first pat) '?is) (match-predicate pat form blists))
((eql (first pat) '?and) (match-and pat form blists))
(t match car and cdr)))
This has several problems. First, it requires users to modify the source code for the matcher. Second, it leads to a very long conditional.
A better way to do this is to use data-driven code where pattern extensions are defined by associating pattern symbol with matching functions defined outside the matcher. This is how that looks in the extensible match-p function.
(defun match-p (x y &optional (lsts '(nil)))
(cond ((var-p x) (update-bindings x y lsts))
((atom x) (and (eql x y) lsts))
((?-function-p (car x))
(funcall (car x) (cdr x) y lsts))
((atom y) nil)
(t (match-p (cdr x) (cdr y)
(match-p (car x) (car y) lsts)))))
Data-driven programming is typically implemented by making a
table of some form -- hash table, list, array -- where functions, e.g.,
lambda forms, are stored under symbols. This works
in JavaScript, Java, and so on. In Lisp, an even simpler approach
is to store the match code
on the symbol-function of the symbol. If we do that,
then ?-function-p is just:
(defun ?-function-p (x)
(and (var-p x) (fboundp x)))
(defun var-p (x)
(and (symbolp x)
(eql (char (symbol-name x) 0) #\?)))
To define a pattern form, we just define a function
whose names begins with a question mark. When the
matcher sees a list whose CAR is
such a function, it calls the function
with the CDR of the list,
the object to be matched, and the
current binding lists. The function should
return a list of binding lists, possibly
NIL if the match fails.
For example, one pre-defined extension is the
pattern function ??. Here are some
example uses:
| Pattern | Matches |
|---|---|
| (?? numberp) | any number |
| (?? oddp) | any odd number |
| (?? > 5) | any number greater than 5 |
Here is how match-p would call the
function ?? for two examples:
| Code | ?? call |
|---|---|
| (match-p '(?? numberp) 6) | (?? '(number) 6) |
| (match-p '(?? > 5) 6) | (?? '(> 5) 6) |
Here is how ?? in turn applies the function
it is passed to the object being matched and any
additional arguments in the pattern.
| Code | function call |
|---|---|
| (match-p '(?? numberp) 6) | (apply 'numberp 6 nil) |
| (match-p '(?? > 5) 6) | (apply ''> 6 '(5)) |
APPLY then calls the function with the given
arguments. If it returns true, ?? returns the
list of binding lists. If it returns false, ??
returns NIL.
The pre-defined extension
(?AND pat1 pat2 pat3...) returns the
binding lists that allow all the patterns to match the input,
if any. So the pattern
(?and (?? numberp) (?? > 5) (?? < 10) ?x)
would match any number that is greater than 5 and less than 10,
and the variable to that number.
?AND is more typical of pattern extensions
in that is recursively calls match-p to
do most of the matching.