https://github.com/m2ym/optima.git
git clone 'https://github.com/m2ym/optima.git'
(ql:quickload :optima)
optima is a fast pattern matching library which uses optimizing techniques widely used in the functional programming world. See the following references for more details:
A pattern specifier, or a pattern for short ambiguous, is an expression that describes how a value matches some specification. Pattern specifiers are defined as follows:
pattern-specifier ::= constant-pattern
| variable-pattern
| place-pattern
| guard-pattern
| not-pattern
| or-pattern
| and-pattern
| constructor-pattern
| derived-pattern
constant-pattern ::= t | nil
| keyword
| atom-except-symbol
| (quote VALUE)
variable-pattern ::= SYMBOL | (variable SYMBOL)
place-pattern ::= (place SYMBOL)
guard-pattern ::= (guard PATTERN TEST-FORM)
not-pattern ::= (not PATTERN)
or-pattern ::= (or PATTERN*)
and-pattern ::= (and PATTERN*)
constructor-pattern ::= (NAME ARG*)
derived-pattern ::= (NAME PATTERN*)
A constant-pattern matches the constant itself.
Examples:
(match 1 (1 2)) => 2
(match "foo" ("foo" "bar")) => "bar"
(match '(1) ('(1) 2)) => 2
A variable-pattern matches any value and binds the value to the variable. “_” and “otherwise” are special variable-patterns (a.k.a wildcard-pattern) which match any value but don't bind.
Examples:
(match 1 (x x)) => 1
(match 1 (_ 2)) => 2
(match 1
(2 2)
(otherwise 'otherwise))
=> OTHERWISE
A place-pattern matches any value, in the same way as variable-patterns do, but binds the value by using SYMBOL-MACROLET.
Examples:
(defvar c (cons 1 2))
(match c ((cons (place x) y) (incf x) (incf y)))
c
=> (2 . 2)
A guard-pattern is a special pattern that tests whether TEST-FORM is satisfied in the current matching context.
Examples:
(match 1 ((guard x (eql x 2)) t))
=> NIL
(match 1 ((guard x (eql x 1)) t))
=> T
A not-pattern matches a value that is not matched with sub-PATTERN.
Examples:
(match 1 ((not 2) 3)) => 3
(match 1 ((not (not 1)) 1)) => 1
An or-pattern matches a value that is matched with one of sub-PATTERNs.
Examples:
(match '(2 . 1) ((or (cons 1 x) (cons 2 x)) x))
=> 1
An and-pattern matches a value that is matched with all of its sub-PATTERNs. The most common use case is to match a value and bind the value to a variable.
Examples:
(match 1 ((and 1 x) x))
=> 1
A constructor-pattern matches the sub-components of a value based on its structure. The following constructors are available:
Syntax:
cons-constructor-pattern ::= (cons CAR-PATTERN CDR-PATTERN)
Examples:
(match '(1 . 2)
((cons a b) (+ a b)))
=> 3
Syntax:
assoc-constructor-pattern ::= (assoc ITEM PATTERN &key key test)
Examples:
(match '((1 . :one))
((assoc 1 x) x))
=> :ONE
(match '((1 . :one) (2 . :two))
((assoc 2 x) x))
=> :TWO
(match '(1 (2 . 3))
((assoc 2 x) x))
=> 3
(match '(("a" . 123))
((assoc "A" 123 :test #'string-equal) t))
=> T
Syntax:
property-constructor-pattern ::= (property KEY PATTERN)
Examples:
(match '(:a 1)
((property :a x) x))
=> 1
(match '(:a 1 :b 2)
((property :b x) x))
=> 2
(match '(:a 1 2)
((property :a x) x))
=> 1
Syntax:
vector-constructor-pattern ::= (vector PATTERN*)
Examples:
(match #(1 2)
((vector a b) (+ a b)))
=> 3
Syntax:
simple-vector-constructor-pattern ::= (simple-vector PATTERN*)
Examples:
(match #(1 2)
((simple-vector a b) (+ a b)))
=> 3
Matches an instance of a given subclass of standard-class, as well as the instance's slots.
Syntax:
class-constructor-pattern ::= (class NAME slot*)
| (NAME slot*)
slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)
CLASS can be omitted. If slot is a symbol, then it will be regarded as (slot slot). If more than one PATTERN is given, then they will be wrapped by and-pattern like (and PATTERN*).
Examples:
(defclass point ()
((x :initarg :x)
(y :initarg :y)))
(defvar p (make-instance 'point :x 1 :y 2))
(match p
((point x y) (list x y)))
=> (1 2)
(match p
((point (x 1 x)) x))
=> 1
(defstruct person (name age))
(defvar foo (make-person :name "foo" :age 30))
(match foo
((person name age) (list name age)))
=> ("foo" 30)
You can also use MAKE-INSTANCE style pattern syntax like:
(match foo
((person :name name :age age) (list name age)))
=> ("foo" 30)
This is equal to the example above except this implicitly resolves the slot names using the Metaobject Protocol. In this case, you have to make sure the slot names can be determined uniquely during the compilation. Otherwise, you will get a compiler error.
Matches any structure value, and its slot values.
Syntax:
structure-constructor-pattern ::= (structure CONC-NAME slot*)
| (CONC-NAME slot*)
slot ::= SLOT-NAME
| (SLOT-NAME PATTERN*)
As in the CLASS constructor-pattern, STRUCTURE can be omitted. CONC-NAME is a prefix string of a predicate (CONC-NAME + “p”) and accessors (CONC-NAME + SLOT-NAME). For example, if we have the following defstruct,
(defstruct person name age)
the structure constructor-pattern (person- name age) is valid because PERSON-P, PERSON-NAME and PERSON-AGE are available here. Technically, we don't need an actual structure definition. If we have the following code, for instance,
(defun point-p (p) (consp p))
(defun point-x (p) (car p))
(defun point-y (p) (cdr p))
the pattern matching below is valid.
(match (cons 1 2)
((point- x y) (list x y)))
=> (1 2)
Examples:
(defstruct (person (:conc-name :p-)
(:predicate p-p))
name age)
(match (make-person :name "foo" :age 30)
((p- name age) (list name age)))
=> ("foo" 30)
As in the class constructor-pattern, you can also use MAKE-INSTANCE style pattern syntax like:
(match (cons 1 2)
((point- :x x :y y) (list x y)))
=> (1 2)
A derived-pattern is a pattern that is defined with DEFPATTERN. There are some builtin derived patterns as below:
Expansion of LIST derived patterns:
(list a b c) => (cons a (cons b (cons c nil)))
Expansion of LIST* derived patterns:
(list* a b c) => (cons a (cons b c))
Expansion of SATISFIES derived patterns:
(satisfies f) => (guard it (f it))
Expansion of EQ, EQL, EQUAL, EQUALP derived patterns:
(eq 'foo) => (guard it (eq it 'foo))
(eql 123) => (guard it (eql it 123))
(equal '(1 2)) => (guard it (equal it '(1 2)))
(equalp "foo") => (guard it (equalp it "foo"))
Expansion of TYPE derived patterns:
(TYPE type) => (guard it (typep it 'type))
You may want to use a quasiquote in a pattern specifier like:
(match '(1 2 3 4)
(`(1 ,x ,@y) (list x y)))
To do so, you need to use a specific quasiquote reader, for example fare-quasiquote , loading fare-quasiquote-optima system, because there is no standard expanded form for quasiquote expressions.
You can define your own constructor patterns by using the
OPTIMA.CORE
package. First, define a data structure for the
constructor pattern.
(defstruct (my-cons-pattern (:include constructor-pattern)
(:constructor make-cons-pattern (car-pattern cdr-pattern
&aux (subpatterns (list car-pattern
cdr-pattern))))))
Note that you must keep SUBPATTERNS
of the constructor pattern in
sync so that optima can take care of them.
Second, specify a condition for when the destructor of two constructor patterns can be shared. This makes it possible to perform some optimizations.
(defmethod constructor-pattern-destructor-sharable-p ((x my-cons-pattern) (y my-cons-pattern))
t)
Third, define a destructor generator for the constructor pattern. The
destructor generator will make a destructor that specifies how to
check the the data (PREDICATE-FORM
) and how to access the
data (ACCESSOR-FORMS
).
(defmethod constructor-pattern-make-destructor ((pattern my-cons-pattern) var)
(make-destructor :predicate-form `(consp ,var)
:accessor-forms (list `(car ,var) `(cdr ,var))))
Finally, define a parser and an unparser for the constructor pattern.
(defmethod parse-constructor-pattern ((name (eql 'my-cons)) &rest args)
(apply #'make-my-cons-pattern (mapcar #'parse-pattern args)))
(defmethod unparse-pattern ((pattern my-cons-pattern))
`(cons ,(unparse-pattern (my-cons-pattern-car-pattern pattern))
,(unparse-pattern (my-cons-pattern-cdr-pattern pattern))))
See the source code for more detail.
%equal a b
Equality function for comparing pattern constants.
%equals var value
Equality macro for comparing pattern constants. This specializes the comparison form to some specific form as follows:
(equals x nil) => (null x)
(equals x 'foo) => (eq x 'foo)
(equals x 123) => (eql x 123)
(equals x '(a b)) => (%equals x '(a b))
%svref simple-vector index
Safe SVREF.
%assoc item alist &key (test #'eql)
Safe ASSOC.
%get-property item plist
Safe GETF.
destructor
destructor-accessor-forms instance
make-destructor &key ((bindings bindings) nil) ((predicate-form predicate-form)
nil) ((accessor-forms
accessor-forms)
nil)
variable-pattern
variable-pattern-name instance
make-variable-pattern &optional name
place-pattern
place-pattern-name instance
make-place-pattern name
constant-pattern
constant-pattern-value instance
make-constant-pattern value
complex-pattern
complex-pattern-subpatterns instance
guard-pattern
guard-pattern-test-form instance
guard-pattern-subpattern pattern
make-guard-pattern subpattern test-form &aux (subpatterns (list subpattern))
not-pattern
not-pattern-subpattern pattern
make-not-pattern subpattern &aux (subpatterns (list subpattern))
or-pattern
or-pattern-subpatterns instance
make-or-pattern &rest subpatterns
and-pattern
and-pattern-subpatterns instance
make-and-pattern &rest subpatterns
constructor-pattern
constructor-pattern-subpatterns instance
constructor-pattern-arity pattern
constructor-pattern-destructor-sharable-p x y
constructor-pattern-make-destructor pattern var
cons-pattern
cons-pattern-car-pattern pattern
cons-pattern-cdr-pattern pattern
make-cons-pattern car-pattern cdr-pattern &aux (subpatterns
(list car-pattern cdr-pattern))
assoc-pattern
assoc-pattern-item instance
assoc-pattern-key instance
assoc-pattern-test instance
assoc-pattern-value-pattern pattern
make-assoc-pattern item value-pattern &key (key nil) (test nil) &aux (subpatterns
(list
value-pattern))
property-pattern
property-pattern-item instance
property-pattern-value-pattern pattern
make-property-pattern item value-pattern &aux (subpatterns (list value-pattern))
vector-pattern
vector-pattern-subpatterns instance
make-vector-pattern &rest subpatterns
simple-vector-pattern
simple-vector-pattern-subpatterns instance
make-simple-vector-pattern &rest subpatterns
class-pattern
class-pattern-subpatterns instance
class-pattern-class-name instance
class-pattern-slot-names instance
make-class-pattern class-name &rest slot-specs
structure-pattern
structure-pattern-subpatterns instance
structure-pattern-conc-name instance
structure-pattern-slot-names instance
make-structure-pattern conc-name &rest slot-specs
pattern-variables pattern
Returns the set of variables in PATTERN. If PATTERN is not linear, an error will be raised.
place-pattern-included-p pattern
check-patterns patterns
Check if PATTERNS are valid. Otherwise, an error will be raised.
lift-guard-patterns pattern
pattern-expand-function name
pattern-expand-1 pattern
pattern-expand pattern
pattern-expand-all pattern
parse-pattern pattern
parse-constructor-pattern name &rest args
unparse-pattern pattern
match arg &body clauses
Matches ARG with CLAUSES. CLAUSES is a list of the form of (PATTERN
. BODY) where PATTERN is a pattern specifier and BODY is an implicit
progn. If ARG matches some PATTERN, match
then evaluates
the corresponding BODY and returns the evaluated value. If no pattern matches,
then returns NIL.
Evaluating a form (FAIL) in the clause body causes the latest pattern matching to fail. For example,
(match 1
(x (if (eql x 1)
(fail)
x))
(_ 'ok))
returns OK, because the form (FAIL) in the first clause is evaluated.
If BODY starts with the symbols WHEN or UNLESS, then the next form will be used to introduce (FAIL). That is,
(match list ((list x) when (oddp x) x))
(match list ((list x) unless (evenp x) x))
will be translated to
(match list ((list x) (if (oddp x) x (fail))))
(match list ((list x) (if (evenp x) (fail) x)))
Examples:
(match 1 (1 1))
=> 1
(match 1 (2 2))
=> 2
(match 1 (x x))
=> 1
(match (list 1 2 3)
(list x y z) (+ x y z))
=> 6
multiple-value-match values-form &body clauses
Matches the multiple values of VALUES-FORM with CLAUSES. Unlike MATCH, CLAUSES have to have the form of (PATTERNS . BODY), where PATTERNS is a list of patterns. The number of values that will be used to match is determined by the maximum arity of PATTERNS among CLAUSES.
Examples:
(multiple-value-match (values 1 2)
((2) 1)
((1 y) y))
=> 2
ematch arg &body clauses
Same as MATCH, except MATCH-ERROR will be raised if not matched.
multiple-value-ematch values-form &body clauses
Same as MULTIPLE-VALUE-MATCH, except MATCH-ERROR will be raised if not matched.
cmatch arg &body clauses
Same as MATCH, except a continuable MATCH-ERROR will be raised if none of the clauses match.
multiple-value-cmatch values-form &body clauses
Same as MULTIPLE-VALUE-MATCH, except a continuable MATCH-ERROR will be raised if none of the clauses match.
fail
Causes the latest pattern matching to fail. After this failure, matching continues at the next pattern.
match-error
match-error-values condition
match-error-patterns condition
defpattern name lambda-list &body body
Defines a derived pattern specifier named NAME. This is analogous to DEFTYPE.
Examples:
;; Defines a LIST pattern.
(defpattern list (&rest args)
(when args
`(cons ,(car args) (list ,@(cdr args)))))
Syntax:
(alist (KEY . PATTERN)*)
Expansion:
(alist (k . p)*) => (and (assoc k p)*)
Examples:
(match '((1 . :one) (2 . :two) (3 . :three))
((alist (1 . x) (3 . y)) (list x y)))
=> (:ONE :THREE)
Syntax:
(plist {KEY PATTERN}*)
Expansion:
(plist {k p}*) => (and (property k p)*)
Examples:
(match '(:name "John" :age 23)
((plist :name "John" :age age) age))
=> 23
if-match pattern arg &body (then &optional else)
Equivalent to (match ARG (PATTERN THEN) (otherwise ELSE)).
when-match pattern arg &body body
Equivalent to (match ARG (PATTERN BODY…)).
unless-match pattern arg &body body
Equivalent to (match ARG (PATTERN) (otherwise BODY…)).
let-match bindings &body body
Similar to LET, except not only a variable but also a pattern can be used in BINDINGS.
let-match* bindings &body body
Similar to LET-MATCH but matches sequentially.
let-match1 pattern arg &body body
Equivalent to (let-match ((PATTERN ARG)) BODY…).
lambda-match &body clauses
Equivalent to (lambda (arg) (match arg CLAUSES…)).
lambda-ematch &body clauses
Equivalent to (lambda (arg) (ematch arg CLAUSES…)).
lambda-cmatch &body clauses
Equivalent to (lambda (arg) (cmatch arg CLAUSES…)).
lambda-match1 pattern &body body
Equivalent to (lambda-match (PATTERN BODY…)).
lambda-ematch1 pattern &body body
Equivalent to (lambda-ematch (PATTERN BODY…)).
lambda-cmatch1 pattern &body body
Equivalent to (lambda-cmatch (PATTERN BODY…)).
LLGPL
Syntax:
(ppcre REGEXP PATTERN*)
Matches REGEXP against the target string. Sub-PATTERNs will be used to match the matched groups, if the REGEXP matched.
Examples:
(match "2012-11-04"
((ppcre "^(\\d{4})-(\\d{2})-(\\d{2})$" year month day)
(list year month day)))
=> ("2012" "11" "04")
LLGPL