Spelling fixes.
[emacs.git] / lisp / emacs-lisp / pcase.el
blob7709b59d137249b5c5f14f2dd497120e846854e1
1 ;;; pcase.el --- ML-style pattern-matching macro for Elisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords:
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; ML-style pattern matching.
26 ;; The entry points are autoloaded.
28 ;; Todo:
30 ;; - (pcase e (`(,x . ,x) foo)) signals an "x unused" warning if `foo' doesn't
31 ;; use x, because x is bound separately for the equality constraint
32 ;; (as well as any pred/guard) and for the body, so uses at one place don't
33 ;; count for the other.
34 ;; - provide ways to extend the set of primitives, with some kind of
35 ;; define-pcase-matcher. We could easily make it so that (guard BOOLEXP)
36 ;; could be defined this way, as a shorthand for (pred (lambda (_) BOOLEXP)).
37 ;; But better would be if we could define new ways to match by having the
38 ;; extension provide its own `pcase--split-<foo>' thingy.
39 ;; - along these lines, provide patterns to match CL structs.
40 ;; - provide something like (setq VAR) so a var can be set rather than
41 ;; let-bound.
42 ;; - provide a way to fallthrough to subsequent cases.
43 ;; - try and be more clever to reduce the size of the decision tree, and
44 ;; to reduce the number of leaves that need to be turned into function:
45 ;; - first, do the tests shared by all remaining branches (it will have
46 ;; to be performed anyway, so better so it first so it's shared).
47 ;; - then choose the test that discriminates more (?).
48 ;; - ideally we'd want (pcase s ((re RE1) E1) ((re RE2) E2)) to be able to
49 ;; generate a lex-style DFA to decide whether to run E1 or E2.
51 ;;; Code:
53 ;; Macro-expansion of pcase is reasonably fast, so it's not a problem
54 ;; when byte-compiling a file, but when interpreting the code, if the pcase
55 ;; is in a loop, the repeated macro-expansion becomes terribly costly, so we
56 ;; memoize previous macro expansions to try and avoid recomputing them
57 ;; over and over again.
58 (defconst pcase--memoize (make-hash-table :weakness 'key :test 'eq))
60 (defconst pcase--dontcare-upats '(t _ dontcare))
62 ;;;###autoload
63 (defmacro pcase (exp &rest cases)
64 "Perform ML-style pattern matching on EXP.
65 CASES is a list of elements of the form (UPATTERN CODE...).
67 UPatterns can take the following forms:
68 _ matches anything.
69 SYMBOL matches anything and binds it to SYMBOL.
70 (or UPAT...) matches if any of the patterns matches.
71 (and UPAT...) matches if all the patterns match.
72 `QPAT matches if the QPattern QPAT matches.
73 (pred PRED) matches if PRED applied to the object returns non-nil.
74 (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil.
75 (let UPAT EXP) matches if EXP matches UPAT.
76 If a SYMBOL is used twice in the same pattern (i.e. the pattern is
77 \"non-linear\"), then the second occurrence is turned into an `eq'uality test.
79 QPatterns can take the following forms:
80 (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr.
81 ,UPAT matches if the UPattern UPAT matches.
82 STRING matches if the object is `equal' to STRING.
83 ATOM matches if the object is `eq' to ATOM.
84 QPatterns for vectors are not implemented yet.
86 PRED can take the form
87 FUNCTION in which case it gets called with one argument.
88 (FUN ARG1 .. ARGN) in which case it gets called with N+1 arguments.
89 A PRED of the form FUNCTION is equivalent to one of the form (FUNCTION).
90 PRED patterns can refer to variables bound earlier in the pattern.
91 E.g. you can match pairs where the cdr is larger than the car with a pattern
92 like `(,a . ,(pred (< a))) or, with more checks:
93 `(,(and a (pred numberp)) . ,(and (pred numberp) (pred (< a))))"
94 (declare (indent 1) (debug case)) ;FIXME: edebug `guard' and vars.
95 ;; We want to use a weak hash table as a cache, but the key will unavoidably
96 ;; be based on `exp' and `cases', yet `cases' is a fresh new list each time
97 ;; we're called so it'll be immediately GC'd. So we use (car cases) as key
98 ;; which does come straight from the source code and should hence not be GC'd
99 ;; so easily.
100 (let ((data (gethash (car cases) pcase--memoize)))
101 ;; data = (EXP CASES . EXPANSION)
102 (if (and (equal exp (car data)) (equal cases (cadr data)))
103 ;; We have the right expansion.
104 (cddr data)
105 (when data
106 (message "pcase-memoize: equal first branch, yet different"))
107 (let ((expansion (pcase--expand exp cases)))
108 (puthash (car cases) (cons exp (cons cases expansion)) pcase--memoize)
109 expansion))))
111 ;;;###autoload
112 (defmacro pcase-let* (bindings &rest body)
113 "Like `let*' but where you can use `pcase' patterns for bindings.
114 BODY should be an expression, and BINDINGS should be a list of bindings
115 of the form (UPAT EXP)."
116 (declare (indent 1) (debug let))
117 (cond
118 ((null bindings) (if (> (length body) 1) `(progn ,@body) (car body)))
119 ((pcase--trivial-upat-p (caar bindings))
120 `(let (,(car bindings)) (pcase-let* ,(cdr bindings) ,@body)))
122 `(pcase ,(cadr (car bindings))
123 (,(caar bindings) (pcase-let* ,(cdr bindings) ,@body))
124 ;; We can either signal an error here, or just use `dontcare' which
125 ;; generates more efficient code. In practice, if we use `dontcare' we
126 ;; will still often get an error and the few cases where we don't do not
127 ;; matter that much, so it's a better choice.
128 (dontcare nil)))))
130 ;;;###autoload
131 (defmacro pcase-let (bindings &rest body)
132 "Like `let' but where you can use `pcase' patterns for bindings.
133 BODY should be a list of expressions, and BINDINGS should be a list of bindings
134 of the form (UPAT EXP)."
135 (declare (indent 1) (debug let))
136 (if (null (cdr bindings))
137 `(pcase-let* ,bindings ,@body)
138 (let ((matches '()))
139 (dolist (binding (prog1 bindings (setq bindings nil)))
140 (cond
141 ((memq (car binding) pcase--dontcare-upats)
142 (push (cons (make-symbol "_") (cdr binding)) bindings))
143 ((pcase--trivial-upat-p (car binding)) (push binding bindings))
145 (let ((tmpvar (make-symbol (format "x%d" (length bindings)))))
146 (push (cons tmpvar (cdr binding)) bindings)
147 (push (list (car binding) tmpvar) matches)))))
148 `(let ,(nreverse bindings) (pcase-let* ,matches ,@body)))))
150 (defmacro pcase-dolist (spec &rest body)
151 (if (pcase--trivial-upat-p (car spec))
152 `(dolist ,spec ,@body)
153 (let ((tmpvar (make-symbol "x")))
154 `(dolist (,tmpvar ,@(cdr spec))
155 (pcase-let* ((,(car spec) ,tmpvar))
156 ,@body)))))
159 (defun pcase--trivial-upat-p (upat)
160 (and (symbolp upat) (not (memq upat pcase--dontcare-upats))))
162 (defun pcase--expand (exp cases)
163 ;; (message "pid=%S (pcase--expand %S ...hash=%S)"
164 ;; (emacs-pid) exp (sxhash cases))
165 (let* ((defs (if (symbolp exp) '()
166 (let ((sym (make-symbol "x")))
167 (prog1 `((,sym ,exp)) (setq exp sym)))))
168 (seen '())
169 (codegen
170 (lambda (code vars)
171 (let ((prev (assq code seen)))
172 (if (not prev)
173 (let ((res (pcase-codegen code vars)))
174 (push (list code vars res) seen)
175 res)
176 ;; Since we use a tree-based pattern matching
177 ;; technique, the leaves (the places that contain the
178 ;; code to run once a pattern is matched) can get
179 ;; copied a very large number of times, so to avoid
180 ;; code explosion, we need to keep track of how many
181 ;; times we've used each leaf and move it
182 ;; to a separate function if that number is too high.
184 ;; We've already used this branch. So it is shared.
185 (let* ((code (car prev)) (cdrprev (cdr prev))
186 (prevvars (car cdrprev)) (cddrprev (cdr cdrprev))
187 (res (car cddrprev)))
188 (unless (symbolp res)
189 ;; This is the first repeat, so we have to move
190 ;; the branch to a separate function.
191 (let ((bsym
192 (make-symbol (format "pcase-%d" (length defs)))))
193 (push `(,bsym (lambda ,(mapcar #'car prevvars) ,@code)) defs)
194 (setcar res 'funcall)
195 (setcdr res (cons bsym (mapcar #'cdr prevvars)))
196 (setcar (cddr prev) bsym)
197 (setq res bsym)))
198 (setq vars (copy-sequence vars))
199 (let ((args (mapcar (lambda (pa)
200 (let ((v (assq (car pa) vars)))
201 (setq vars (delq v vars))
202 (cdr v)))
203 prevvars)))
204 (when vars ;New additional vars.
205 (error "The vars %s are only bound in some paths"
206 (mapcar #'car vars)))
207 `(funcall ,res ,@args)))))))
208 (main
209 (pcase--u
210 (mapcar (lambda (case)
211 `((match ,exp . ,(car case))
212 ,(apply-partially
213 (if (pcase--small-branch-p (cdr case))
214 ;; Don't bother sharing multiple
215 ;; occurrences of this leaf since it's small.
216 #'pcase-codegen codegen)
217 (cdr case))))
218 cases))))
219 (if (null defs) main
220 `(let ,defs ,main))))
222 (defun pcase-codegen (code vars)
223 `(let ,(mapcar (lambda (b) (list (car b) (cdr b))) vars)
224 ,@code))
226 (defun pcase--small-branch-p (code)
227 (and (= 1 (length code))
228 (or (not (consp (car code)))
229 (let ((small t))
230 (dolist (e (car code))
231 (if (consp e) (setq small nil)))
232 small))))
234 ;; Try to use `cond' rather than a sequence of `if's, so as to reduce
235 ;; the depth of the generated tree.
236 (defun pcase--if (test then else)
237 (cond
238 ((eq else :pcase--dontcare) then)
239 ((eq then :pcase--dontcare) (debug) else) ;Can/should this ever happen?
240 ((eq (car-safe else) 'if)
241 (if (equal test (nth 1 else))
242 ;; Doing a test a second time: get rid of the redundancy.
243 ;; FIXME: ideally, this should never happen because the pcase--split-*
244 ;; funs should have eliminated such things, but pcase--split-member
245 ;; is imprecise, so in practice it can happen occasionally.
246 `(if ,test ,then ,@(nthcdr 3 else))
247 `(cond (,test ,then)
248 (,(nth 1 else) ,(nth 2 else))
249 (t ,@(nthcdr 3 else)))))
250 ((eq (car-safe else) 'cond)
251 `(cond (,test ,then)
252 ;; Doing a test a second time: get rid of the redundancy, as above.
253 ,@(remove (assoc test else) (cdr else))))
254 ;; Invert the test if that lets us reduce the depth of the tree.
255 ((memq (car-safe then) '(if cond)) (pcase--if `(not ,test) else then))
256 (t `(if ,test ,then ,else))))
258 (defun pcase--upat (qpattern)
259 (cond
260 ((eq (car-safe qpattern) '\,) (cadr qpattern))
261 (t (list '\` qpattern))))
263 ;; Note about MATCH:
264 ;; When we have patterns like `(PAT1 . PAT2), after performing the `consp'
265 ;; check, we want to turn all the similar patterns into ones of the form
266 ;; (and (match car PAT1) (match cdr PAT2)), so you naturally need conjunction.
267 ;; Earlier code hence used branches of the form (MATCHES . CODE) where
268 ;; MATCHES was a list (implicitly a conjunction) of (SYM . PAT).
269 ;; But if we have a pattern of the form (or `(PAT1 . PAT2) PAT3), there is
270 ;; no easy way to eliminate the `consp' check in such a representation.
271 ;; So we replaced the MATCHES by the MATCH below which can be made up
272 ;; of conjunctions and disjunctions, so if we know `foo' is a cons, we can
273 ;; turn (match foo . (or `(PAT1 . PAT2) PAT3)) into
274 ;; (or (and (match car . `PAT1) (match cdr . `PAT2)) (match foo . PAT3)).
275 ;; The downside is that we now have `or' and `and' both in MATCH and
276 ;; in PAT, so there are different equivalent representations and we
277 ;; need to handle them all. We do not try to systematically
278 ;; canonicalize them to one form over another, but we do occasionally
279 ;; turn one into the other.
281 (defun pcase--u (branches)
282 "Expand matcher for rules BRANCHES.
283 Each BRANCH has the form (MATCH CODE . VARS) where
284 CODE is the code generator for that branch.
285 VARS is the set of vars already bound by earlier matches.
286 MATCH is the pattern that needs to be matched, of the form:
287 (match VAR . UPAT)
288 (and MATCH ...)
289 (or MATCH ...)"
290 (when (setq branches (delq nil branches))
291 (let* ((carbranch (car branches))
292 (match (car carbranch)) (cdarbranch (cdr carbranch))
293 (code (car cdarbranch))
294 (vars (cdr cdarbranch)))
295 (pcase--u1 (list match) code vars (cdr branches)))))
297 (defun pcase--and (match matches)
298 (if matches `(and ,match ,@matches) match))
300 (defconst pcase-mutually-exclusive-predicates
301 '((symbolp . integerp)
302 (symbolp . numberp)
303 (symbolp . consp)
304 (symbolp . arrayp)
305 (symbolp . stringp)
306 (symbolp . byte-code-function-p)
307 (integerp . consp)
308 (integerp . arrayp)
309 (integerp . stringp)
310 (integerp . byte-code-function-p)
311 (numberp . consp)
312 (numberp . arrayp)
313 (numberp . stringp)
314 (numberp . byte-code-function-p)
315 (consp . arrayp)
316 (consp . stringp)
317 (consp . byte-code-function-p)
318 (arrayp . stringp)
319 (arrayp . byte-code-function-p)
320 (stringp . byte-code-function-p)))
322 (defun pcase--split-match (sym splitter match)
323 (cond
324 ((eq (car match) 'match)
325 (if (not (eq sym (cadr match)))
326 (cons match match)
327 (let ((pat (cddr match)))
328 (cond
329 ;; Hoist `or' and `and' patterns to `or' and `and' matches.
330 ((memq (car-safe pat) '(or and))
331 (pcase--split-match sym splitter
332 (cons (car pat)
333 (mapcar (lambda (alt)
334 `(match ,sym . ,alt))
335 (cdr pat)))))
336 (t (let ((res (funcall splitter (cddr match))))
337 (cons (or (car res) match) (or (cdr res) match))))))))
338 ((memq (car match) '(or and))
339 (let ((then-alts '())
340 (else-alts '())
341 (neutral-elem (if (eq 'or (car match))
342 :pcase--fail :pcase--succeed))
343 (zero-elem (if (eq 'or (car match)) :pcase--succeed :pcase--fail)))
344 (dolist (alt (cdr match))
345 (let ((split (pcase--split-match sym splitter alt)))
346 (unless (eq (car split) neutral-elem)
347 (push (car split) then-alts))
348 (unless (eq (cdr split) neutral-elem)
349 (push (cdr split) else-alts))))
350 (cons (cond ((memq zero-elem then-alts) zero-elem)
351 ((null then-alts) neutral-elem)
352 ((null (cdr then-alts)) (car then-alts))
353 (t (cons (car match) (nreverse then-alts))))
354 (cond ((memq zero-elem else-alts) zero-elem)
355 ((null else-alts) neutral-elem)
356 ((null (cdr else-alts)) (car else-alts))
357 (t (cons (car match) (nreverse else-alts)))))))
358 (t (error "Uknown MATCH %s" match))))
360 (defun pcase--split-rest (sym splitter rest)
361 (let ((then-rest '())
362 (else-rest '()))
363 (dolist (branch rest)
364 (let* ((match (car branch))
365 (code&vars (cdr branch))
366 (split
367 (pcase--split-match sym splitter match)))
368 (unless (eq (car split) :pcase--fail)
369 (push (cons (car split) code&vars) then-rest))
370 (unless (eq (cdr split) :pcase--fail)
371 (push (cons (cdr split) code&vars) else-rest))))
372 (cons (nreverse then-rest) (nreverse else-rest))))
374 (defun pcase--split-consp (syma symd pat)
375 (cond
376 ;; A QPattern for a cons, can only go the `then' side.
377 ((and (eq (car-safe pat) '\`) (consp (cadr pat)))
378 (let ((qpat (cadr pat)))
379 (cons `(and (match ,syma . ,(pcase--upat (car qpat)))
380 (match ,symd . ,(pcase--upat (cdr qpat))))
381 :pcase--fail)))
382 ;; A QPattern but not for a cons, can only go to the `else' side.
383 ((eq (car-safe pat) '\`) (cons :pcase--fail nil))
384 ((and (eq (car-safe pat) 'pred)
385 (or (member (cons 'consp (cadr pat))
386 pcase-mutually-exclusive-predicates)
387 (member (cons (cadr pat) 'consp)
388 pcase-mutually-exclusive-predicates)))
389 (cons :pcase--fail nil))))
391 (defun pcase--split-equal (elem pat)
392 (cond
393 ;; The same match will give the same result.
394 ((and (eq (car-safe pat) '\`) (equal (cadr pat) elem))
395 (cons :pcase--succeed :pcase--fail))
396 ;; A different match will fail if this one succeeds.
397 ((and (eq (car-safe pat) '\`)
398 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
399 ;; (consp (cadr pat)))
401 (cons :pcase--fail nil))
402 ((and (eq (car-safe pat) 'pred)
403 (symbolp (cadr pat))
404 (get (cadr pat) 'side-effect-free)
405 (funcall (cadr pat) elem))
406 (cons :pcase--succeed nil))))
408 (defun pcase--split-member (elems pat)
409 ;; Based on pcase--split-equal.
410 (cond
411 ;; The same match (or a match of membership in a superset) will
412 ;; give the same result, but we don't know how to check it.
413 ;; (???
414 ;; (cons :pcase--succeed nil))
415 ;; A match for one of the elements may succeed or fail.
416 ((and (eq (car-safe pat) '\`) (member (cadr pat) elems))
417 nil)
418 ;; A different match will fail if this one succeeds.
419 ((and (eq (car-safe pat) '\`)
420 ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
421 ;; (consp (cadr pat)))
423 (cons :pcase--fail nil))
424 ((and (eq (car-safe pat) 'pred)
425 (symbolp (cadr pat))
426 (get (cadr pat) 'side-effect-free)
427 (let ((p (cadr pat)) (all t))
428 (dolist (elem elems)
429 (unless (funcall p elem) (setq all nil)))
430 all))
431 (cons :pcase--succeed nil))))
433 (defun pcase--split-pred (upat pat)
434 ;; FIXME: For predicates like (pred (> a)), two such predicates may
435 ;; actually refer to different variables `a'.
436 (cond
437 ((equal upat pat) (cons :pcase--succeed :pcase--fail))
438 ((and (eq 'pred (car upat))
439 (eq 'pred (car-safe pat))
440 (or (member (cons (cadr upat) (cadr pat))
441 pcase-mutually-exclusive-predicates)
442 (member (cons (cadr pat) (cadr upat))
443 pcase-mutually-exclusive-predicates)))
444 (cons :pcase--fail nil))
445 ;; ((and (eq 'pred (car upat))
446 ;; (eq '\` (car-safe pat))
447 ;; (symbolp (cadr upat))
448 ;; (or (symbolp (cadr pat)) (stringp (cadr pat)) (numberp (cadr pat)))
449 ;; (get (cadr upat) 'side-effect-free)
450 ;; (progn (message "Trying predicate %S" (cadr upat))
451 ;; (ignore-errors
452 ;; (funcall (cadr upat) (cadr pat)))))
453 ;; (message "Simplify pred %S against %S" upat pat)
454 ;; (cons nil :pcase--fail))
457 (defun pcase--fgrep (vars sexp)
458 "Check which of the symbols VARS appear in SEXP."
459 (let ((res '()))
460 (while (consp sexp)
461 (dolist (var (pcase--fgrep vars (pop sexp)))
462 (unless (memq var res) (push var res))))
463 (and (memq sexp vars) (not (memq sexp res)) (push sexp res))
464 res))
466 ;; It's very tempting to use `pcase' below, tho obviously, it'd create
467 ;; bootstrapping problems.
468 (defun pcase--u1 (matches code vars rest)
469 "Return code that runs CODE (with VARS) if MATCHES match.
470 Otherwise, it defers to REST which is a list of branches of the form
471 \(ELSE-MATCH ELSE-CODE . ELSE-VARS)."
472 ;; Depending on the order in which we choose to check each of the MATCHES,
473 ;; the resulting tree may be smaller or bigger. So in general, we'd want
474 ;; to be careful to chose the "optimal" order. But predicate
475 ;; patterns make this harder because they create dependencies
476 ;; between matches. So we don't bother trying to reorder anything.
477 (cond
478 ((null matches) (funcall code vars))
479 ((eq :pcase--fail (car matches)) (pcase--u rest))
480 ((eq :pcase--succeed (car matches))
481 (pcase--u1 (cdr matches) code vars rest))
482 ((eq 'and (caar matches))
483 (pcase--u1 (append (cdar matches) (cdr matches)) code vars rest))
484 ((eq 'or (caar matches))
485 (let* ((alts (cdar matches))
486 (var (if (eq (caar alts) 'match) (cadr (car alts))))
487 (simples '()) (others '()))
488 (when var
489 (dolist (alt alts)
490 (if (and (eq (car alt) 'match) (eq var (cadr alt))
491 (let ((upat (cddr alt)))
492 (and (eq (car-safe upat) '\`)
493 (or (integerp (cadr upat)) (symbolp (cadr upat))
494 (stringp (cadr upat))))))
495 (push (cddr alt) simples)
496 (push alt others))))
497 (cond
498 ((null alts) (error "Please avoid it") (pcase--u rest))
499 ((> (length simples) 1)
500 ;; De-hoist the `or' MATCH into an `or' pattern that will be
501 ;; turned into a `memq' below.
502 (pcase--u1 (cons `(match ,var or . ,(nreverse simples)) (cdr matches))
503 code vars
504 (if (null others) rest
505 (cons (cons
506 (pcase--and (if (cdr others)
507 (cons 'or (nreverse others))
508 (car others))
509 (cdr matches))
510 (cons code vars))
511 rest))))
513 (pcase--u1 (cons (pop alts) (cdr matches)) code vars
514 (if (null alts) (progn (error "Please avoid it") rest)
515 (cons (cons
516 (pcase--and (if (cdr alts)
517 (cons 'or alts) (car alts))
518 (cdr matches))
519 (cons code vars))
520 rest)))))))
521 ((eq 'match (caar matches))
522 (let* ((popmatches (pop matches))
523 (_op (car popmatches)) (cdrpopmatches (cdr popmatches))
524 (sym (car cdrpopmatches))
525 (upat (cdr cdrpopmatches)))
526 (cond
527 ((memq upat '(t _)) (pcase--u1 matches code vars rest))
528 ((eq upat 'dontcare) :pcase--dontcare)
529 ((memq (car-safe upat) '(guard pred))
530 (if (eq (car upat) 'pred) (put sym 'pcase-used t))
531 (let* ((splitrest
532 (pcase--split-rest
533 sym (apply-partially #'pcase--split-pred upat) rest))
534 (then-rest (car splitrest))
535 (else-rest (cdr splitrest)))
536 (pcase--if (if (and (eq (car upat) 'pred) (symbolp (cadr upat)))
537 `(,(cadr upat) ,sym)
538 (let* ((exp (cadr upat))
539 ;; `vs' is an upper bound on the vars we need.
540 (vs (pcase--fgrep (mapcar #'car vars) exp))
541 (env (mapcar (lambda (var)
542 (list var (cdr (assq var vars))))
543 vs))
544 (call (if (eq 'guard (car upat))
546 (when (memq sym vs)
547 ;; `sym' is shadowed by `env'.
548 (let ((newsym (make-symbol "x")))
549 (push (list newsym sym) env)
550 (setq sym newsym)))
551 (if (functionp exp) `(,exp ,sym)
552 `(,@exp ,sym)))))
553 (if (null vs)
554 call
555 ;; Let's not replace `vars' in `exp' since it's
556 ;; too difficult to do it right, instead just
557 ;; let-bind `vars' around `exp'.
558 `(let* ,env ,call))))
559 (pcase--u1 matches code vars then-rest)
560 (pcase--u else-rest))))
561 ((symbolp upat)
562 (put sym 'pcase-used t)
563 (if (not (assq upat vars))
564 (pcase--u1 matches code (cons (cons upat sym) vars) rest)
565 ;; Non-linear pattern. Turn it into an `eq' test.
566 (pcase--u1 (cons `(match ,sym . (pred (eq ,(cdr (assq upat vars)))))
567 matches)
568 code vars rest)))
569 ((eq (car-safe upat) 'let)
570 ;; A upat of the form (let VAR EXP).
571 ;; (pcase--u1 matches code
572 ;; (cons (cons (nth 1 upat) (nth 2 upat)) vars) rest)
573 (let* ((exp
574 (let* ((exp (nth 2 upat))
575 (found (assq exp vars)))
576 (if found (cdr found)
577 (let* ((vs (pcase--fgrep (mapcar #'car vars) exp))
578 (env (mapcar (lambda (v) (list v (cdr (assq v vars))))
579 vs)))
580 (if env `(let* ,env ,exp) exp)))))
581 (sym (if (symbolp exp) exp (make-symbol "x")))
582 (body
583 (pcase--u1 (cons `(match ,sym . ,(nth 1 upat)) matches)
584 code vars rest)))
585 (if (eq sym exp)
586 body
587 `(let* ((,sym ,exp)) ,body))))
588 ((eq (car-safe upat) '\`)
589 (put sym 'pcase-used t)
590 (pcase--q1 sym (cadr upat) matches code vars rest))
591 ((eq (car-safe upat) 'or)
592 (let ((all (> (length (cdr upat)) 1))
593 (memq-fine t))
594 (when all
595 (dolist (alt (cdr upat))
596 (unless (and (eq (car-safe alt) '\`)
597 (or (symbolp (cadr alt)) (integerp (cadr alt))
598 (setq memq-fine nil)
599 (stringp (cadr alt))))
600 (setq all nil))))
601 (if all
602 ;; Use memq for (or `a `b `c `d) rather than a big tree.
603 (let* ((elems (mapcar 'cadr (cdr upat)))
604 (splitrest
605 (pcase--split-rest
606 sym (apply-partially #'pcase--split-member elems) rest))
607 (then-rest (car splitrest))
608 (else-rest (cdr splitrest)))
609 (pcase--if `(,(if memq-fine #'memq #'member) ,sym ',elems)
610 (pcase--u1 matches code vars then-rest)
611 (pcase--u else-rest)))
612 (pcase--u1 (cons `(match ,sym ,@(cadr upat)) matches) code vars
613 (append (mapcar (lambda (upat)
614 `((and (match ,sym . ,upat) ,@matches)
615 ,code ,@vars))
616 (cddr upat))
617 rest)))))
618 ((eq (car-safe upat) 'and)
619 (pcase--u1 (append (mapcar (lambda (upat) `(match ,sym ,@upat))
620 (cdr upat))
621 matches)
622 code vars rest))
623 ((eq (car-safe upat) 'not)
624 ;; FIXME: The implementation below is naive and results in
625 ;; inefficient code.
626 ;; To make it work right, we would need to turn pcase--u1's
627 ;; `code' and `vars' into a single argument of the same form as
628 ;; `rest'. We would also need to split this new `then-rest' argument
629 ;; for every test (currently we don't bother to do it since
630 ;; it's only useful for odd patterns like (and `(PAT1 . PAT2)
631 ;; `(PAT3 . PAT4)) which the programmer can easily rewrite
632 ;; to the more efficient `(,(and PAT1 PAT3) . ,(and PAT2 PAT4))).
633 (pcase--u1 `((match ,sym . ,(cadr upat)))
634 ;; FIXME: This codegen is not careful to share its
635 ;; code if used several times: code blow up is likely.
636 (lambda (_vars)
637 ;; `vars' will likely contain bindings which are
638 ;; not always available in other paths to
639 ;; `rest', so there' no point trying to pass
640 ;; them down.
641 (pcase--u rest))
642 vars
643 (list `((and . ,matches) ,code . ,vars))))
644 (t (error "Unknown upattern `%s'" upat)))))
645 (t (error "Incorrect MATCH %s" (car matches)))))
647 (defun pcase--q1 (sym qpat matches code vars rest)
648 "Return code that runs CODE if SYM matches QPAT and if MATCHES match.
649 Otherwise, it defers to REST which is a list of branches of the form
650 \(OTHER_MATCH OTHER-CODE . OTHER-VARS)."
651 (cond
652 ((eq (car-safe qpat) '\,) (error "Can't use `,UPATTERN"))
653 ((floatp qpat) (error "Floating point patterns not supported"))
654 ((vectorp qpat)
655 ;; FIXME.
656 (error "Vector QPatterns not implemented yet"))
657 ((consp qpat)
658 (let* ((syma (make-symbol "xcar"))
659 (symd (make-symbol "xcdr"))
660 (splitrest (pcase--split-rest
662 (apply-partially #'pcase--split-consp syma symd)
663 rest))
664 (then-rest (car splitrest))
665 (else-rest (cdr splitrest))
666 (then-body (pcase--u1 `((match ,syma . ,(pcase--upat (car qpat)))
667 (match ,symd . ,(pcase--upat (cdr qpat)))
668 ,@matches)
669 code vars then-rest)))
670 (pcase--if
671 `(consp ,sym)
672 ;; We want to be careful to only add bindings that are used.
673 ;; The byte-compiler could do that for us, but it would have to pay
674 ;; attention to the `consp' test in order to figure out that car/cdr
675 ;; can't signal errors and our byte-compiler is not that clever.
676 `(let (,@(if (get syma 'pcase-used) `((,syma (car ,sym))))
677 ,@(if (get symd 'pcase-used) `((,symd (cdr ,sym)))))
678 ,then-body)
679 (pcase--u else-rest))))
680 ((or (integerp qpat) (symbolp qpat) (stringp qpat))
681 (let* ((splitrest (pcase--split-rest
682 sym (apply-partially 'pcase--split-equal qpat) rest))
683 (then-rest (car splitrest))
684 (else-rest (cdr splitrest)))
685 (pcase--if `(,(if (stringp qpat) #'equal #'eq) ,sym ',qpat)
686 (pcase--u1 matches code vars then-rest)
687 (pcase--u else-rest))))
688 (t (error "Unknown QPattern %s" qpat))))
691 (provide 'pcase)
692 ;;; pcase.el ends here