pcase.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. ;;; pcase.el --- ML-style pattern-matching macro for Elisp -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords:
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; ML-style pattern matching.
  18. ;; The entry points are autoloaded.
  19. ;; Todo:
  20. ;; - (pcase e (`(,x . ,x) foo)) signals an "x unused" warning if `foo' doesn't
  21. ;; use x, because x is bound separately for the equality constraint
  22. ;; (as well as any pred/guard) and for the body, so uses at one place don't
  23. ;; count for the other.
  24. ;; - provide ways to extend the set of primitives, with some kind of
  25. ;; define-pcase-matcher. We could easily make it so that (guard BOOLEXP)
  26. ;; could be defined this way, as a shorthand for (pred (lambda (_) BOOLEXP)).
  27. ;; But better would be if we could define new ways to match by having the
  28. ;; extension provide its own `pcase--split-<foo>' thingy.
  29. ;; - along these lines, provide patterns to match CL structs.
  30. ;; - provide something like (setq VAR) so a var can be set rather than
  31. ;; let-bound.
  32. ;; - provide a way to fallthrough to subsequent cases (not sure what I meant by
  33. ;; this :-()
  34. ;; - try and be more clever to reduce the size of the decision tree, and
  35. ;; to reduce the number of leaves that need to be turned into function:
  36. ;; - first, do the tests shared by all remaining branches (it will have
  37. ;; to be performed anyway, so better do it first so it's shared).
  38. ;; - then choose the test that discriminates more (?).
  39. ;; - provide Agda's `with' (along with its `...' companion).
  40. ;; - implement (not PAT). This might require a significant redesign.
  41. ;; - ideally we'd want (pcase s ((re RE1) E1) ((re RE2) E2)) to be able to
  42. ;; generate a lex-style DFA to decide whether to run E1 or E2.
  43. ;;; Code:
  44. (require 'macroexp)
  45. ;; Macro-expansion of pcase is reasonably fast, so it's not a problem
  46. ;; when byte-compiling a file, but when interpreting the code, if the pcase
  47. ;; is in a loop, the repeated macro-expansion becomes terribly costly, so we
  48. ;; memoize previous macro expansions to try and avoid recomputing them
  49. ;; over and over again.
  50. ;; FIXME: Now that macroexpansion is also performed when loading an interpreted
  51. ;; file, this is not a real problem any more.
  52. (defconst pcase--memoize (make-hash-table :weakness 'key :test 'eq))
  53. ;; (defconst pcase--memoize-1 (make-hash-table :test 'eq))
  54. ;; (defconst pcase--memoize-2 (make-hash-table :weakness 'key :test 'equal))
  55. (defconst pcase--dontcare-upats '(t _ pcase--dontcare))
  56. (defvar pcase--dontwarn-upats '(pcase--dontcare))
  57. (def-edebug-spec
  58. pcase-PAT
  59. (&or symbolp
  60. ("or" &rest pcase-PAT)
  61. ("and" &rest pcase-PAT)
  62. ("guard" form)
  63. ("let" pcase-PAT form)
  64. ("pred" pcase-FUN)
  65. ("app" pcase-FUN pcase-PAT)
  66. pcase-MACRO
  67. sexp))
  68. (def-edebug-spec
  69. pcase-FUN
  70. (&or lambda-expr
  71. ;; Punt on macros/special forms.
  72. (functionp &rest form)
  73. sexp))
  74. ;; See bug#24717
  75. (put 'pcase-MACRO 'edebug-form-spec 'pcase--edebug-match-macro)
  76. ;; Only called from edebug.
  77. (declare-function get-edebug-spec "edebug" (symbol))
  78. (declare-function edebug-match "edebug" (cursor specs))
  79. (defun pcase--edebug-match-macro (cursor)
  80. (let (specs)
  81. (mapatoms
  82. (lambda (s)
  83. (let ((m (get s 'pcase-macroexpander)))
  84. (when (and m (get-edebug-spec m))
  85. (push (cons (symbol-name s) (get-edebug-spec m))
  86. specs)))))
  87. (edebug-match cursor (cons '&or specs))))
  88. ;;;###autoload
  89. (defmacro pcase (exp &rest cases)
  90. "Evaluate EXP and attempt to match it against structural patterns.
  91. CASES is a list of elements of the form (PATTERN CODE...).
  92. A structural PATTERN describes a template that identifies a class
  93. of values. For example, the pattern \\=`(,foo ,bar) matches any
  94. two element list, binding its elements to symbols named `foo' and
  95. `bar' -- in much the same way that `cl-destructuring-bind' would.
  96. A significant difference from `cl-destructuring-bind' is that, if
  97. a pattern match fails, the next case is tried until either a
  98. successful match is found or there are no more cases.
  99. Another difference is that pattern elements may be quoted,
  100. meaning they must match exactly: The pattern \\='(foo bar)
  101. matches only against two element lists containing the symbols
  102. `foo' and `bar' in that order. (As a short-hand, atoms always
  103. match themselves, such as numbers or strings, and need not be
  104. quoted.)
  105. Lastly, a pattern can be logical, such as (pred numberp), that
  106. matches any number-like element; or the symbol `_', that matches
  107. anything. Also, when patterns are backquoted, a comma may be
  108. used to introduce logical patterns inside backquoted patterns.
  109. The complete list of standard patterns is as follows:
  110. _ matches anything.
  111. SYMBOL matches anything and binds it to SYMBOL.
  112. If a SYMBOL is used twice in the same pattern
  113. the second occurrence becomes an `eq'uality test.
  114. (or PAT...) matches if any of the patterns matches.
  115. (and PAT...) matches if all the patterns match.
  116. \\='VAL matches if the object is `equal' to VAL.
  117. ATOM is a shorthand for \\='ATOM.
  118. ATOM can be a keyword, an integer, or a string.
  119. (pred FUN) matches if FUN applied to the object returns non-nil.
  120. (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil.
  121. (let PAT EXP) matches if EXP matches PAT.
  122. (app FUN PAT) matches if FUN applied to the object matches PAT.
  123. Additional patterns can be defined using `pcase-defmacro'.
  124. The FUN argument in the `app' pattern may have the following forms:
  125. SYMBOL or (lambda ARGS BODY) in which case it's called with one argument.
  126. (F ARG1 .. ARGn) in which case F gets called with an n+1'th argument
  127. which is the value being matched.
  128. So a FUN of the form SYMBOL is equivalent to (FUN).
  129. FUN can refer to variables bound earlier in the pattern.
  130. See Info node `(elisp) Pattern matching case statement' in the
  131. Emacs Lisp manual for more information and examples."
  132. (declare (indent 1) (debug (form &rest (pcase-PAT body))))
  133. ;; We want to use a weak hash table as a cache, but the key will unavoidably
  134. ;; be based on `exp' and `cases', yet `cases' is a fresh new list each time
  135. ;; we're called so it'll be immediately GC'd. So we use (car cases) as key
  136. ;; which does come straight from the source code and should hence not be GC'd
  137. ;; so easily.
  138. (let ((data (gethash (car cases) pcase--memoize)))
  139. ;; data = (EXP CASES . EXPANSION)
  140. (if (and (equal exp (car data)) (equal cases (cadr data)))
  141. ;; We have the right expansion.
  142. (cddr data)
  143. ;; (when (gethash (car cases) pcase--memoize-1)
  144. ;; (message "pcase-memoize failed because of weak key!!"))
  145. ;; (when (gethash (car cases) pcase--memoize-2)
  146. ;; (message "pcase-memoize failed because of eq test on %S"
  147. ;; (car cases)))
  148. ;; (when data
  149. ;; (message "pcase-memoize: equal first branch, yet different"))
  150. (let ((expansion (pcase--expand exp cases)))
  151. (puthash (car cases) `(,exp ,cases ,@expansion) pcase--memoize)
  152. ;; (puthash (car cases) `(,exp ,cases ,@expansion) pcase--memoize-1)
  153. ;; (puthash (car cases) `(,exp ,cases ,@expansion) pcase--memoize-2)
  154. expansion))))
  155. (declare-function help-fns--signature "help-fns"
  156. (function doc real-def real-function buffer))
  157. ;; FIXME: Obviously, this will collide with nadvice's use of
  158. ;; function-documentation if we happen to advise `pcase'.
  159. (put 'pcase 'function-documentation '(pcase--make-docstring))
  160. (defun pcase--make-docstring ()
  161. (let* ((main (documentation (symbol-function 'pcase) 'raw))
  162. (ud (help-split-fundoc main 'pcase)))
  163. ;; So that eg emacs -Q -l cl-lib --eval "(documentation 'pcase)" works,
  164. ;; where cl-lib is anything using pcase-defmacro.
  165. (require 'help-fns)
  166. (with-temp-buffer
  167. (insert (or (cdr ud) main))
  168. (mapatoms
  169. (lambda (symbol)
  170. (let ((me (get symbol 'pcase-macroexpander)))
  171. (when me
  172. (insert "\n\n-- ")
  173. (let* ((doc (documentation me 'raw)))
  174. (setq doc (help-fns--signature symbol doc me
  175. (indirect-function me) nil))
  176. (insert "\n" (or doc "Not documented.")))))))
  177. (let ((combined-doc (buffer-string)))
  178. (if ud (help-add-fundoc-usage combined-doc (car ud)) combined-doc)))))
  179. ;;;###autoload
  180. (defmacro pcase-exhaustive (exp &rest cases)
  181. "The exhaustive version of `pcase' (which see)."
  182. (declare (indent 1) (debug pcase))
  183. (let* ((x (make-symbol "x"))
  184. (pcase--dontwarn-upats (cons x pcase--dontwarn-upats)))
  185. (pcase--expand
  186. ;; FIXME: Could we add the FILE:LINE data in the error message?
  187. exp (append cases `((,x (error "No clause matching `%S'" ,x)))))))
  188. ;;;###autoload
  189. (defmacro pcase-lambda (lambda-list &rest body)
  190. "Like `lambda' but allow each argument to be a pattern.
  191. I.e. accepts the usual &optional and &rest keywords, but every
  192. formal argument can be any pattern accepted by `pcase' (a mere
  193. variable name being but a special case of it)."
  194. (declare (doc-string 2) (indent defun)
  195. (debug ((&rest pcase-PAT) body)))
  196. (let* ((bindings ())
  197. (parsed-body (macroexp-parse-body body))
  198. (args (mapcar (lambda (pat)
  199. (if (symbolp pat)
  200. ;; Simple vars and &rest/&optional are just passed
  201. ;; through unchanged.
  202. pat
  203. (let ((arg (make-symbol
  204. (format "arg%s" (length bindings)))))
  205. (push `(,pat ,arg) bindings)
  206. arg)))
  207. lambda-list)))
  208. `(lambda ,args ,@(car parsed-body)
  209. (pcase-let* ,(nreverse bindings) ,@(cdr parsed-body)))))
  210. (defun pcase--let* (bindings body)
  211. (cond
  212. ((null bindings) (macroexp-progn body))
  213. ((pcase--trivial-upat-p (caar bindings))
  214. (macroexp-let* `(,(car bindings)) (pcase--let* (cdr bindings) body)))
  215. (t
  216. (let ((binding (pop bindings)))
  217. (pcase--expand
  218. (cadr binding)
  219. `((,(car binding) ,(pcase--let* bindings body))
  220. ;; We can either signal an error here, or just use `pcase--dontcare'
  221. ;; which generates more efficient code. In practice, if we use
  222. ;; `pcase--dontcare' we will still often get an error and the few
  223. ;; cases where we don't do not matter that much, so
  224. ;; it's a better choice.
  225. (pcase--dontcare nil)))))))
  226. ;;;###autoload
  227. (defmacro pcase-let* (bindings &rest body)
  228. "Like `let*' but where you can use `pcase' patterns for bindings.
  229. BODY should be an expression, and BINDINGS should be a list of bindings
  230. of the form (PAT EXP)."
  231. (declare (indent 1)
  232. (debug ((&rest (pcase-PAT &optional form)) body)))
  233. (let ((cached (gethash bindings pcase--memoize)))
  234. ;; cached = (BODY . EXPANSION)
  235. (if (equal (car cached) body)
  236. (cdr cached)
  237. (let ((expansion (pcase--let* bindings body)))
  238. (puthash bindings (cons body expansion) pcase--memoize)
  239. expansion))))
  240. ;;;###autoload
  241. (defmacro pcase-let (bindings &rest body)
  242. "Like `let' but where you can use `pcase' patterns for bindings.
  243. BODY should be a list of expressions, and BINDINGS should be a list of bindings
  244. of the form (PAT EXP).
  245. The macro is expanded and optimized under the assumption that those
  246. patterns *will* match, so a mismatch may go undetected or may cause
  247. any kind of error."
  248. (declare (indent 1) (debug pcase-let*))
  249. (if (null (cdr bindings))
  250. `(pcase-let* ,bindings ,@body)
  251. (let ((matches '()))
  252. (dolist (binding (prog1 bindings (setq bindings nil)))
  253. (cond
  254. ((memq (car binding) pcase--dontcare-upats)
  255. (push (cons (make-symbol "_") (cdr binding)) bindings))
  256. ((pcase--trivial-upat-p (car binding)) (push binding bindings))
  257. (t
  258. (let ((tmpvar (make-symbol (format "x%d" (length bindings)))))
  259. (push (cons tmpvar (cdr binding)) bindings)
  260. (push (list (car binding) tmpvar) matches)))))
  261. `(let ,(nreverse bindings) (pcase-let* ,matches ,@body)))))
  262. ;;;###autoload
  263. (defmacro pcase-dolist (spec &rest body)
  264. "Like `dolist' but where the binding can be a `pcase' pattern.
  265. \n(fn (PATTERN LIST) BODY...)"
  266. (declare (indent 1) (debug ((pcase-PAT form) body)))
  267. (if (pcase--trivial-upat-p (car spec))
  268. `(dolist ,spec ,@body)
  269. (let ((tmpvar (make-symbol "x")))
  270. `(dolist (,tmpvar ,@(cdr spec))
  271. (pcase-let* ((,(car spec) ,tmpvar))
  272. ,@body)))))
  273. (defun pcase--trivial-upat-p (upat)
  274. (and (symbolp upat) (not (memq upat pcase--dontcare-upats))))
  275. (defun pcase--expand (exp cases)
  276. ;; (message "pid=%S (pcase--expand %S ...hash=%S)"
  277. ;; (emacs-pid) exp (sxhash cases))
  278. (macroexp-let2 macroexp-copyable-p val exp
  279. (let* ((defs ())
  280. (seen '())
  281. (codegen
  282. (lambda (code vars)
  283. (let ((prev (assq code seen)))
  284. (if (not prev)
  285. (let ((res (pcase-codegen code vars)))
  286. (push (list code vars res) seen)
  287. res)
  288. ;; Since we use a tree-based pattern matching
  289. ;; technique, the leaves (the places that contain the
  290. ;; code to run once a pattern is matched) can get
  291. ;; copied a very large number of times, so to avoid
  292. ;; code explosion, we need to keep track of how many
  293. ;; times we've used each leaf and move it
  294. ;; to a separate function if that number is too high.
  295. ;;
  296. ;; We've already used this branch. So it is shared.
  297. (let* ((code (car prev)) (cdrprev (cdr prev))
  298. (prevvars (car cdrprev)) (cddrprev (cdr cdrprev))
  299. (res (car cddrprev)))
  300. (unless (symbolp res)
  301. ;; This is the first repeat, so we have to move
  302. ;; the branch to a separate function.
  303. (let ((bsym
  304. (make-symbol (format "pcase-%d" (length defs)))))
  305. (push `(,bsym (lambda ,(mapcar #'car prevvars) ,@code))
  306. defs)
  307. (setcar res 'funcall)
  308. (setcdr res (cons bsym (mapcar #'cdr prevvars)))
  309. (setcar (cddr prev) bsym)
  310. (setq res bsym)))
  311. (setq vars (copy-sequence vars))
  312. (let ((args (mapcar (lambda (pa)
  313. (let ((v (assq (car pa) vars)))
  314. (setq vars (delq v vars))
  315. (cdr v)))
  316. prevvars)))
  317. ;; If some of `vars' were not found in `prevvars', that's
  318. ;; OK it just means those vars aren't present in all
  319. ;; branches, so they can be used within the pattern
  320. ;; (e.g. by a `guard/let/pred') but not in the branch.
  321. ;; FIXME: But if some of `prevvars' are not in `vars' we
  322. ;; should remove them from `prevvars'!
  323. `(funcall ,res ,@args)))))))
  324. (used-cases ())
  325. (main
  326. (pcase--u
  327. (mapcar (lambda (case)
  328. `(,(pcase--match val (pcase--macroexpand (car case)))
  329. ,(lambda (vars)
  330. (unless (memq case used-cases)
  331. ;; Keep track of the cases that are used.
  332. (push case used-cases))
  333. (funcall
  334. (if (pcase--small-branch-p (cdr case))
  335. ;; Don't bother sharing multiple
  336. ;; occurrences of this leaf since it's small.
  337. #'pcase-codegen codegen)
  338. (cdr case)
  339. vars))))
  340. cases))))
  341. (dolist (case cases)
  342. (unless (or (memq case used-cases)
  343. (memq (car case) pcase--dontwarn-upats))
  344. (message "Redundant pcase pattern: %S" (car case))))
  345. (macroexp-let* defs main))))
  346. (defun pcase--macroexpand (pat)
  347. "Expands all macro-patterns in PAT."
  348. (let ((head (car-safe pat)))
  349. (cond
  350. ((null head)
  351. (if (pcase--self-quoting-p pat) `',pat pat))
  352. ((memq head '(pred guard quote)) pat)
  353. ((memq head '(or and)) `(,head ,@(mapcar #'pcase--macroexpand (cdr pat))))
  354. ((eq head 'let) `(let ,(pcase--macroexpand (cadr pat)) ,@(cddr pat)))
  355. ((eq head 'app) `(app ,(nth 1 pat) ,(pcase--macroexpand (nth 2 pat))))
  356. (t
  357. (let* ((expander (get head 'pcase-macroexpander))
  358. (npat (if expander (apply expander (cdr pat)))))
  359. (if (null npat)
  360. (error (if expander
  361. "Unexpandable %s pattern: %S"
  362. "Unknown %s pattern: %S")
  363. head pat)
  364. (pcase--macroexpand npat)))))))
  365. ;;;###autoload
  366. (defmacro pcase-defmacro (name args &rest body)
  367. "Define a new kind of pcase PATTERN, by macro expansion.
  368. Patterns of the form (NAME ...) will be expanded according
  369. to this macro."
  370. (declare (indent 2) (debug defun) (doc-string 3))
  371. ;; Add the function via `fsym', so that an autoload cookie placed
  372. ;; on a pcase-defmacro will cause the macro to be loaded on demand.
  373. (let ((fsym (intern (format "%s--pcase-macroexpander" name)))
  374. (decl (assq 'declare body)))
  375. (when decl (setq body (remove decl body)))
  376. `(progn
  377. (defun ,fsym ,args ,@body)
  378. (define-symbol-prop ',fsym 'edebug-form-spec ',(cadr (assq 'debug decl)))
  379. (define-symbol-prop ',name 'pcase-macroexpander #',fsym))))
  380. (defun pcase--match (val upat)
  381. "Build a MATCH structure, hoisting all `or's and `and's outside."
  382. (cond
  383. ;; Hoist or/and patterns into or/and matches.
  384. ((memq (car-safe upat) '(or and))
  385. `(,(car upat)
  386. ,@(mapcar (lambda (upat)
  387. (pcase--match val upat))
  388. (cdr upat))))
  389. (t
  390. `(match ,val . ,upat))))
  391. (defun pcase-codegen (code vars)
  392. ;; Don't use let*, otherwise macroexp-let* may merge it with some surrounding
  393. ;; let* which might prevent the setcar/setcdr in pcase--expand's fancy
  394. ;; codegen from later metamorphosing this let into a funcall.
  395. (if vars
  396. `(let ,(mapcar (lambda (b) (list (car b) (cdr b))) vars)
  397. ,@code)
  398. `(progn ,@code)))
  399. (defun pcase--small-branch-p (code)
  400. (and (= 1 (length code))
  401. (or (not (consp (car code)))
  402. (let ((small t))
  403. (dolist (e (car code))
  404. (if (consp e) (setq small nil)))
  405. small))))
  406. ;; Try to use `cond' rather than a sequence of `if's, so as to reduce
  407. ;; the depth of the generated tree.
  408. (defun pcase--if (test then else)
  409. (cond
  410. ((eq else :pcase--dontcare) then)
  411. ((eq then :pcase--dontcare) (debug) else) ;Can/should this ever happen?
  412. (t (macroexp-if test then else))))
  413. ;; Note about MATCH:
  414. ;; When we have patterns like `(PAT1 . PAT2), after performing the `consp'
  415. ;; check, we want to turn all the similar patterns into ones of the form
  416. ;; (and (match car PAT1) (match cdr PAT2)), so you naturally need conjunction.
  417. ;; Earlier code hence used branches of the form (MATCHES . CODE) where
  418. ;; MATCHES was a list (implicitly a conjunction) of (SYM . PAT).
  419. ;; But if we have a pattern of the form (or `(PAT1 . PAT2) PAT3), there is
  420. ;; no easy way to eliminate the `consp' check in such a representation.
  421. ;; So we replaced the MATCHES by the MATCH below which can be made up
  422. ;; of conjunctions and disjunctions, so if we know `foo' is a cons, we can
  423. ;; turn (match foo . (or `(PAT1 . PAT2) PAT3)) into
  424. ;; (or (and (match car . `PAT1) (match cdr . `PAT2)) (match foo . PAT3)).
  425. ;; The downside is that we now have `or' and `and' both in MATCH and
  426. ;; in PAT, so there are different equivalent representations and we
  427. ;; need to handle them all. We do not try to systematically
  428. ;; canonicalize them to one form over another, but we do occasionally
  429. ;; turn one into the other.
  430. (defun pcase--u (branches)
  431. "Expand matcher for rules BRANCHES.
  432. Each BRANCH has the form (MATCH CODE . VARS) where
  433. CODE is the code generator for that branch.
  434. VARS is the set of vars already bound by earlier matches.
  435. MATCH is the pattern that needs to be matched, of the form:
  436. (match VAR . PAT)
  437. (and MATCH ...)
  438. (or MATCH ...)"
  439. (when (setq branches (delq nil branches))
  440. (let* ((carbranch (car branches))
  441. (match (car carbranch)) (cdarbranch (cdr carbranch))
  442. (code (car cdarbranch))
  443. (vars (cdr cdarbranch)))
  444. (pcase--u1 (list match) code vars (cdr branches)))))
  445. (defun pcase--and (match matches)
  446. (if matches `(and ,match ,@matches) match))
  447. (defconst pcase-mutually-exclusive-predicates
  448. '((symbolp . integerp)
  449. (symbolp . numberp)
  450. (symbolp . consp)
  451. (symbolp . arrayp)
  452. (symbolp . vectorp)
  453. (symbolp . stringp)
  454. (symbolp . byte-code-function-p)
  455. (symbolp . recordp)
  456. (integerp . consp)
  457. (integerp . arrayp)
  458. (integerp . vectorp)
  459. (integerp . stringp)
  460. (integerp . byte-code-function-p)
  461. (integerp . recordp)
  462. (numberp . consp)
  463. (numberp . arrayp)
  464. (numberp . vectorp)
  465. (numberp . stringp)
  466. (numberp . byte-code-function-p)
  467. (numberp . recordp)
  468. (consp . arrayp)
  469. (consp . atom)
  470. (consp . vectorp)
  471. (consp . stringp)
  472. (consp . byte-code-function-p)
  473. (consp . recordp)
  474. (arrayp . byte-code-function-p)
  475. (vectorp . byte-code-function-p)
  476. (vectorp . recordp)
  477. (stringp . vectorp)
  478. (stringp . recordp)
  479. (stringp . byte-code-function-p)))
  480. (defun pcase--mutually-exclusive-p (pred1 pred2)
  481. (or (member (cons pred1 pred2)
  482. pcase-mutually-exclusive-predicates)
  483. (member (cons pred2 pred1)
  484. pcase-mutually-exclusive-predicates)))
  485. (defun pcase--split-match (sym splitter match)
  486. (cond
  487. ((eq (car-safe match) 'match)
  488. (if (not (eq sym (cadr match)))
  489. (cons match match)
  490. (let ((res (funcall splitter (cddr match))))
  491. (cons (or (car res) match) (or (cdr res) match)))))
  492. ((memq (car-safe match) '(or and))
  493. (let ((then-alts '())
  494. (else-alts '())
  495. (neutral-elem (if (eq 'or (car match))
  496. :pcase--fail :pcase--succeed))
  497. (zero-elem (if (eq 'or (car match)) :pcase--succeed :pcase--fail)))
  498. (dolist (alt (cdr match))
  499. (let ((split (pcase--split-match sym splitter alt)))
  500. (unless (eq (car split) neutral-elem)
  501. (push (car split) then-alts))
  502. (unless (eq (cdr split) neutral-elem)
  503. (push (cdr split) else-alts))))
  504. (cons (cond ((memq zero-elem then-alts) zero-elem)
  505. ((null then-alts) neutral-elem)
  506. ((null (cdr then-alts)) (car then-alts))
  507. (t (cons (car match) (nreverse then-alts))))
  508. (cond ((memq zero-elem else-alts) zero-elem)
  509. ((null else-alts) neutral-elem)
  510. ((null (cdr else-alts)) (car else-alts))
  511. (t (cons (car match) (nreverse else-alts)))))))
  512. ((memq match '(:pcase--succeed :pcase--fail)) (cons match match))
  513. (t (error "Uknown MATCH %s" match))))
  514. (defun pcase--split-rest (sym splitter rest)
  515. (let ((then-rest '())
  516. (else-rest '()))
  517. (dolist (branch rest)
  518. (let* ((match (car branch))
  519. (code&vars (cdr branch))
  520. (split
  521. (pcase--split-match sym splitter match)))
  522. (unless (eq (car split) :pcase--fail)
  523. (push (cons (car split) code&vars) then-rest))
  524. (unless (eq (cdr split) :pcase--fail)
  525. (push (cons (cdr split) code&vars) else-rest))))
  526. (cons (nreverse then-rest) (nreverse else-rest))))
  527. (defun pcase--split-equal (elem pat)
  528. (cond
  529. ;; The same match will give the same result.
  530. ((and (eq (car-safe pat) 'quote) (equal (cadr pat) elem))
  531. '(:pcase--succeed . :pcase--fail))
  532. ;; A different match will fail if this one succeeds.
  533. ((and (eq (car-safe pat) 'quote)
  534. ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
  535. ;; (consp (cadr pat)))
  536. )
  537. '(:pcase--fail . nil))
  538. ((and (eq (car-safe pat) 'pred)
  539. (symbolp (cadr pat))
  540. (get (cadr pat) 'side-effect-free))
  541. (ignore-errors
  542. (if (funcall (cadr pat) elem)
  543. '(:pcase--succeed . nil)
  544. '(:pcase--fail . nil))))))
  545. (defun pcase--split-member (elems pat)
  546. ;; FIXME: The new pred-based member code doesn't do these optimizations!
  547. ;; Based on pcase--split-equal.
  548. (cond
  549. ;; The same match (or a match of membership in a superset) will
  550. ;; give the same result, but we don't know how to check it.
  551. ;; (???
  552. ;; '(:pcase--succeed . nil))
  553. ;; A match for one of the elements may succeed or fail.
  554. ((and (eq (car-safe pat) 'quote) (member (cadr pat) elems))
  555. nil)
  556. ;; A different match will fail if this one succeeds.
  557. ((and (eq (car-safe pat) 'quote)
  558. ;; (or (integerp (cadr pat)) (symbolp (cadr pat))
  559. ;; (consp (cadr pat)))
  560. )
  561. '(:pcase--fail . nil))
  562. ((and (eq (car-safe pat) 'pred)
  563. (symbolp (cadr pat))
  564. (get (cadr pat) 'side-effect-free)
  565. (ignore-errors
  566. (let ((p (cadr pat)) (all t))
  567. (dolist (elem elems)
  568. (unless (funcall p elem) (setq all nil)))
  569. all)))
  570. '(:pcase--succeed . nil))))
  571. (defun pcase--split-pred (vars upat pat)
  572. (let (test)
  573. (cond
  574. ((and (equal upat pat)
  575. ;; For predicates like (pred (> a)), two such predicates may
  576. ;; actually refer to different variables `a'.
  577. (or (and (eq 'pred (car upat)) (symbolp (cadr upat)))
  578. ;; FIXME: `vars' gives us the environment in which `upat' will
  579. ;; run, but we don't have the environment in which `pat' will
  580. ;; run, so we can't do a reliable verification. But let's try
  581. ;; and catch at least the easy cases such as (bug#14773).
  582. (not (pcase--fgrep (mapcar #'car vars) (cadr upat)))))
  583. '(:pcase--succeed . :pcase--fail))
  584. ((and (eq 'pred (car upat))
  585. (let ((otherpred
  586. (cond ((eq 'pred (car-safe pat)) (cadr pat))
  587. ((not (eq 'quote (car-safe pat))) nil)
  588. ((consp (cadr pat)) #'consp)
  589. ((stringp (cadr pat)) #'stringp)
  590. ((vectorp (cadr pat)) #'vectorp)
  591. ((byte-code-function-p (cadr pat))
  592. #'byte-code-function-p))))
  593. (pcase--mutually-exclusive-p (cadr upat) otherpred)))
  594. '(:pcase--fail . nil))
  595. ((and (eq 'pred (car upat))
  596. (eq 'quote (car-safe pat))
  597. (symbolp (cadr upat))
  598. (or (symbolp (cadr pat)) (stringp (cadr pat)) (numberp (cadr pat)))
  599. (get (cadr upat) 'side-effect-free)
  600. (ignore-errors
  601. (setq test (list (funcall (cadr upat) (cadr pat))))))
  602. (if (car test)
  603. '(nil . :pcase--fail)
  604. '(:pcase--fail . nil))))))
  605. (defun pcase--fgrep (vars sexp)
  606. "Check which of the symbols VARS appear in SEXP."
  607. (let ((res '()))
  608. (while (consp sexp)
  609. (dolist (var (pcase--fgrep vars (pop sexp)))
  610. (unless (memq var res) (push var res))))
  611. (and (memq sexp vars) (not (memq sexp res)) (push sexp res))
  612. res))
  613. (defun pcase--self-quoting-p (upat)
  614. (or (keywordp upat) (integerp upat) (stringp upat)))
  615. (defun pcase--app-subst-match (match sym fun nsym)
  616. (cond
  617. ((eq (car-safe match) 'match)
  618. (if (and (eq sym (cadr match))
  619. (eq 'app (car-safe (cddr match)))
  620. (equal fun (nth 1 (cddr match))))
  621. (pcase--match nsym (nth 2 (cddr match)))
  622. match))
  623. ((memq (car-safe match) '(or and))
  624. `(,(car match)
  625. ,@(mapcar (lambda (match)
  626. (pcase--app-subst-match match sym fun nsym))
  627. (cdr match))))
  628. ((memq match '(:pcase--succeed :pcase--fail)) match)
  629. (t (error "Uknown MATCH %s" match))))
  630. (defun pcase--app-subst-rest (rest sym fun nsym)
  631. (mapcar (lambda (branch)
  632. `(,(pcase--app-subst-match (car branch) sym fun nsym)
  633. ,@(cdr branch)))
  634. rest))
  635. (defsubst pcase--mark-used (sym)
  636. ;; Exceptionally, `sym' may be a constant expression rather than a symbol.
  637. (if (symbolp sym) (put sym 'pcase-used t)))
  638. (defmacro pcase--flip (fun arg1 arg2)
  639. "Helper function, used internally to avoid (funcall (lambda ...) ...)."
  640. (declare (debug (sexp body)))
  641. `(,fun ,arg2 ,arg1))
  642. (defun pcase--funcall (fun arg vars)
  643. "Build a function call to FUN with arg ARG."
  644. (if (symbolp fun)
  645. `(,fun ,arg)
  646. (let* (;; `vs' is an upper bound on the vars we need.
  647. (vs (pcase--fgrep (mapcar #'car vars) fun))
  648. (env (mapcar (lambda (var)
  649. (list var (cdr (assq var vars))))
  650. vs))
  651. (call (progn
  652. (when (memq arg vs)
  653. ;; `arg' is shadowed by `env'.
  654. (let ((newsym (make-symbol "x")))
  655. (push (list newsym arg) env)
  656. (setq arg newsym)))
  657. (if (functionp fun)
  658. `(funcall #',fun ,arg)
  659. `(,@fun ,arg)))))
  660. (if (null vs)
  661. call
  662. ;; Let's not replace `vars' in `fun' since it's
  663. ;; too difficult to do it right, instead just
  664. ;; let-bind `vars' around `fun'.
  665. `(let* ,env ,call)))))
  666. (defun pcase--eval (exp vars)
  667. "Build an expression that will evaluate EXP."
  668. (let* ((found (assq exp vars)))
  669. (if found (cdr found)
  670. (let* ((vs (pcase--fgrep (mapcar #'car vars) exp))
  671. (env (mapcar (lambda (v) (list v (cdr (assq v vars))))
  672. vs)))
  673. (if env (macroexp-let* env exp) exp)))))
  674. ;; It's very tempting to use `pcase' below, tho obviously, it'd create
  675. ;; bootstrapping problems.
  676. (defun pcase--u1 (matches code vars rest)
  677. "Return code that runs CODE (with VARS) if MATCHES match.
  678. Otherwise, it defers to REST which is a list of branches of the form
  679. \(ELSE-MATCH ELSE-CODE . ELSE-VARS)."
  680. ;; Depending on the order in which we choose to check each of the MATCHES,
  681. ;; the resulting tree may be smaller or bigger. So in general, we'd want
  682. ;; to be careful to chose the "optimal" order. But predicate
  683. ;; patterns make this harder because they create dependencies
  684. ;; between matches. So we don't bother trying to reorder anything.
  685. (cond
  686. ((null matches) (funcall code vars))
  687. ((eq :pcase--fail (car matches)) (pcase--u rest))
  688. ((eq :pcase--succeed (car matches))
  689. (pcase--u1 (cdr matches) code vars rest))
  690. ((eq 'and (caar matches))
  691. (pcase--u1 (append (cdar matches) (cdr matches)) code vars rest))
  692. ((eq 'or (caar matches))
  693. (let* ((alts (cdar matches))
  694. (var (if (eq (caar alts) 'match) (cadr (car alts))))
  695. (simples '()) (others '()) (memq-ok t))
  696. (when var
  697. (dolist (alt alts)
  698. (if (and (eq (car alt) 'match) (eq var (cadr alt))
  699. (let ((upat (cddr alt)))
  700. (eq (car-safe upat) 'quote)))
  701. (let ((val (cadr (cddr alt))))
  702. (unless (or (integerp val) (symbolp val))
  703. (setq memq-ok nil))
  704. (push (cadr (cddr alt)) simples))
  705. (push alt others))))
  706. (cond
  707. ((null alts) (error "Please avoid it") (pcase--u rest))
  708. ;; Yes, we can use `memq' (or `member')!
  709. ((> (length simples) 1)
  710. (pcase--u1 (cons `(match ,var
  711. . (pred (pcase--flip
  712. ,(if memq-ok #'memq #'member)
  713. ',simples)))
  714. (cdr matches))
  715. code vars
  716. (if (null others) rest
  717. (cons (cons
  718. (pcase--and (if (cdr others)
  719. (cons 'or (nreverse others))
  720. (car others))
  721. (cdr matches))
  722. (cons code vars))
  723. rest))))
  724. (t
  725. (pcase--u1 (cons (pop alts) (cdr matches)) code vars
  726. (if (null alts) (progn (error "Please avoid it") rest)
  727. (cons (cons
  728. (pcase--and (if (cdr alts)
  729. (cons 'or alts) (car alts))
  730. (cdr matches))
  731. (cons code vars))
  732. rest)))))))
  733. ((eq 'match (caar matches))
  734. (let* ((popmatches (pop matches))
  735. (_op (car popmatches)) (cdrpopmatches (cdr popmatches))
  736. (sym (car cdrpopmatches))
  737. (upat (cdr cdrpopmatches)))
  738. (cond
  739. ((memq upat '(t _))
  740. (let ((code (pcase--u1 matches code vars rest)))
  741. (if (eq upat '_) code
  742. (macroexp--warn-and-return
  743. "Pattern t is deprecated. Use `_' instead"
  744. code))))
  745. ((eq upat 'pcase--dontcare) :pcase--dontcare)
  746. ((memq (car-safe upat) '(guard pred))
  747. (if (eq (car upat) 'pred) (pcase--mark-used sym))
  748. (let* ((splitrest
  749. (pcase--split-rest
  750. sym (lambda (pat) (pcase--split-pred vars upat pat)) rest))
  751. (then-rest (car splitrest))
  752. (else-rest (cdr splitrest)))
  753. (pcase--if (if (eq (car upat) 'pred)
  754. (pcase--funcall (cadr upat) sym vars)
  755. (pcase--eval (cadr upat) vars))
  756. (pcase--u1 matches code vars then-rest)
  757. (pcase--u else-rest))))
  758. ((and (symbolp upat) upat)
  759. (pcase--mark-used sym)
  760. (if (not (assq upat vars))
  761. (pcase--u1 matches code (cons (cons upat sym) vars) rest)
  762. ;; Non-linear pattern. Turn it into an `eq' test.
  763. (pcase--u1 (cons `(match ,sym . (pred (eq ,(cdr (assq upat vars)))))
  764. matches)
  765. code vars rest)))
  766. ((eq (car-safe upat) 'let)
  767. ;; A upat of the form (let VAR EXP).
  768. ;; (pcase--u1 matches code
  769. ;; (cons (cons (nth 1 upat) (nth 2 upat)) vars) rest)
  770. (macroexp-let2
  771. macroexp-copyable-p sym
  772. (pcase--eval (nth 2 upat) vars)
  773. (pcase--u1 (cons (pcase--match sym (nth 1 upat)) matches)
  774. code vars rest)))
  775. ((eq (car-safe upat) 'app)
  776. ;; A upat of the form (app FUN PAT)
  777. (pcase--mark-used sym)
  778. (let* ((fun (nth 1 upat))
  779. (nsym (make-symbol "x"))
  780. (body
  781. ;; We don't change `matches' to reuse the newly computed value,
  782. ;; because we assume there shouldn't be such redundancy in there.
  783. (pcase--u1 (cons (pcase--match nsym (nth 2 upat)) matches)
  784. code vars
  785. (pcase--app-subst-rest rest sym fun nsym))))
  786. (if (not (get nsym 'pcase-used))
  787. body
  788. (macroexp-let*
  789. `((,nsym ,(pcase--funcall fun sym vars)))
  790. body))))
  791. ((eq (car-safe upat) 'quote)
  792. (pcase--mark-used sym)
  793. (let* ((val (cadr upat))
  794. (splitrest (pcase--split-rest
  795. sym (lambda (pat) (pcase--split-equal val pat)) rest))
  796. (then-rest (car splitrest))
  797. (else-rest (cdr splitrest)))
  798. (pcase--if (cond
  799. ((null val) `(null ,sym))
  800. ((or (integerp val) (symbolp val))
  801. (if (pcase--self-quoting-p val)
  802. `(eq ,sym ,val)
  803. `(eq ,sym ',val)))
  804. (t `(equal ,sym ',val)))
  805. (pcase--u1 matches code vars then-rest)
  806. (pcase--u else-rest))))
  807. ((eq (car-safe upat) 'not)
  808. ;; FIXME: The implementation below is naive and results in
  809. ;; inefficient code.
  810. ;; To make it work right, we would need to turn pcase--u1's
  811. ;; `code' and `vars' into a single argument of the same form as
  812. ;; `rest'. We would also need to split this new `then-rest' argument
  813. ;; for every test (currently we don't bother to do it since
  814. ;; it's only useful for odd patterns like (and `(PAT1 . PAT2)
  815. ;; `(PAT3 . PAT4)) which the programmer can easily rewrite
  816. ;; to the more efficient `(,(and PAT1 PAT3) . ,(and PAT2 PAT4))).
  817. (pcase--u1 `((match ,sym . ,(cadr upat)))
  818. ;; FIXME: This codegen is not careful to share its
  819. ;; code if used several times: code blow up is likely.
  820. (lambda (_vars)
  821. ;; `vars' will likely contain bindings which are
  822. ;; not always available in other paths to
  823. ;; `rest', so there' no point trying to pass
  824. ;; them down.
  825. (pcase--u rest))
  826. vars
  827. (list `((and . ,matches) ,code . ,vars))))
  828. (t (error "Unknown pattern `%S'" upat)))))
  829. (t (error "Incorrect MATCH %S" (car matches)))))
  830. (def-edebug-spec
  831. pcase-QPAT
  832. ;; Cf. edebug spec for `backquote-form' in edebug.el.
  833. (&or ("," pcase-PAT)
  834. (pcase-QPAT [&rest [&not ","] pcase-QPAT]
  835. . [&or nil pcase-QPAT])
  836. (vector &rest pcase-QPAT)
  837. sexp))
  838. (pcase-defmacro \` (qpat)
  839. "Backquote-style pcase patterns.
  840. QPAT can take the following forms:
  841. (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr.
  842. [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match
  843. its 0..(n-1)th elements, respectively.
  844. ,PAT matches if the pcase pattern PAT matches.
  845. ATOM matches if the object is `equal' to ATOM.
  846. ATOM can be a symbol, an integer, or a string."
  847. (declare (debug (pcase-QPAT)))
  848. (cond
  849. ((eq (car-safe qpat) '\,) (cadr qpat))
  850. ((vectorp qpat)
  851. `(and (pred vectorp)
  852. (app length ,(length qpat))
  853. ,@(let ((upats nil))
  854. (dotimes (i (length qpat))
  855. (push `(app (pcase--flip aref ,i) ,(list '\` (aref qpat i)))
  856. upats))
  857. (nreverse upats))))
  858. ((consp qpat)
  859. `(and (pred consp)
  860. (app car ,(list '\` (car qpat)))
  861. (app cdr ,(list '\` (cdr qpat)))))
  862. ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
  863. (t (error "Unknown QPAT: %S" qpat))))
  864. (provide 'pcase)
  865. ;;; pcase.el ends here