diff options
| author | Eli Zaretskii | 2016-01-23 12:21:07 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2016-01-23 12:21:07 +0200 |
| commit | 1d4887a3f35ffaf1f788cf5368e25d2cbf94ee94 (patch) | |
| tree | ec7a2c28b0065c37625b7fb4c010d45bf9c889d8 | |
| parent | f1e74e8672b1fbe81373b3dd1276b3f4b986e055 (diff) | |
| download | emacs-1d4887a3f35ffaf1f788cf5368e25d2cbf94ee94.tar.gz emacs-1d4887a3f35ffaf1f788cf5368e25d2cbf94ee94.zip | |
Improve documentation of 'pcase'
* doc/lispref/control.texi (Pattern matching case statement):
Reorganize, expand, and improve wording.
* etc/NEWS: Mention that 'pcase' changes are documented.
Co-authored-by: John Wiegley <johnw@gnu.org>
Co-authored-by: Michael Heerdegen <michael_heerdegen@web.de>
| -rw-r--r-- | doc/lispref/control.texi | 224 | ||||
| -rw-r--r-- | etc/NEWS | 2 |
2 files changed, 151 insertions, 75 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 008a991102b..df60347f839 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi | |||
| @@ -297,36 +297,147 @@ For example: | |||
| 297 | @cindex pcase | 297 | @cindex pcase |
| 298 | @cindex pattern matching | 298 | @cindex pattern matching |
| 299 | 299 | ||
| 300 | To compare a particular value against various possible cases, the macro | 300 | The @code{cond} form lets you choose between alternatives using |
| 301 | @code{pcase} can come handy. It takes the following form: | 301 | predicate conditions that compare values of expressions against |
| 302 | specific values known and written in advance. However, sometimes it | ||
| 303 | is useful to select alternatives based on more general conditions that | ||
| 304 | distinguish between broad classes of values. The @code{pcase} macro | ||
| 305 | allows to choose between alternatives based on matching the value of | ||
| 306 | an expression against a series of patterns. A pattern can be a | ||
| 307 | literal value (comparison to literal values is what @code{cond} does), | ||
| 308 | or it can be a more general description of the expected structure of | ||
| 309 | the expression's value. | ||
| 310 | |||
| 311 | @defmac pcase expression &rest clauses | ||
| 312 | Evaluate @var{expression} and choose among an arbitrary number of | ||
| 313 | alternatives based on the value of @var{expression}. The possible | ||
| 314 | alternatives are specified by @var{clauses}, each of which must be a | ||
| 315 | list of the form @code{(@var{pattern} @var{body-forms})}. | ||
| 316 | @code{pcase} tries to match the value of @var{expression} to the | ||
| 317 | @var{pattern} of each clause, in textual order. If the value matches, | ||
| 318 | the clause succeeds; @code{pcase} then evaluates its @var{body-forms}, | ||
| 319 | and returns the value of the last of @var{body-forms}. Any remaining | ||
| 320 | @var{clauses} are ignored. | ||
| 321 | |||
| 322 | The @var{pattern} part of a clause can be of one of two types: | ||
| 323 | @dfn{QPattern}, a pattern quoted with a backquote; or a | ||
| 324 | @dfn{UPattern}, which is not quoted. UPatterns are simpler, so we | ||
| 325 | describe them first. | ||
| 326 | |||
| 327 | Note: In the description of the patterns below, we use ``the value | ||
| 328 | being matched'' to refer to the value of the @var{expression} that is | ||
| 329 | the first argument of @code{pcase}. | ||
| 330 | |||
| 331 | A UPattern can have one of the following forms: | ||
| 302 | 332 | ||
| 303 | @example | 333 | @table @code |
| 304 | (pcase @var{exp} @var{branch}1 @var{branch}2 @var{branch}3 @dots{}) | ||
| 305 | @end example | ||
| 306 | 334 | ||
| 307 | where each @var{branch} takes the form @code{(@var{upattern} | 335 | @item '@var{val} |
| 308 | @var{body-forms}@dots{})}. | 336 | Matches if the value being matched is @code{equal} to @var{val}. |
| 337 | @item @var{atom} | ||
| 338 | Matches any @var{atom}, which can be a keyword, a number, or a string. | ||
| 339 | (These are self-quoting, so this kind of UPattern is actually a | ||
| 340 | shorthand for @code{'@var{atom}}.) | ||
| 341 | @item _ | ||
| 342 | Matches any value. This is known as @dfn{don't care} or @dfn{wildcard}. | ||
| 343 | @item @var{symbol} | ||
| 344 | Matches any value, and additionally let-binds @var{symbol} to the | ||
| 345 | value it matched, so that you can later refer to it, either in the | ||
| 346 | @var{body-forms} or also later in the pattern. | ||
| 347 | @item (pred @var{predfun}) | ||
| 348 | Matches if the predicate function @var{predfun} returns non-@code{nil} | ||
| 349 | when called with the value being matched as its argument. | ||
| 350 | @var{predfun} can be one of the possible forms described below. | ||
| 351 | @item (guard @var{boolean-expression}) | ||
| 352 | Matches if @var{boolean-expression} evaluates to non-@code{nil}. This | ||
| 353 | allows to include in a UPattern boolean conditions that refer to | ||
| 354 | symbols bound to values (including the value being matched) by | ||
| 355 | previous UPatterns. Typically used inside an @code{and} UPattern, see | ||
| 356 | below. For example, @w{@code{(and x (guard (< x 10)))}} is a pattern | ||
| 357 | which matches any number smaller than 10 and let-binds the variable | ||
| 358 | @code{x} to that number. | ||
| 359 | @item (let @var{upattern} @var{expression}) | ||
| 360 | Matches if the specified @var{expression} matches the specified | ||
| 361 | @var{upattern}. This allows to match a pattern against the value of | ||
| 362 | an @emph{arbitrary} expression, not just the expression that is the | ||
| 363 | first argument to @code{pcase}. (It is called @code{let} because | ||
| 364 | @var{upattern} can bind symbols to values using the @var{symbol} | ||
| 365 | UPattern.) | ||
| 366 | @item (app @var{function} @var{upattern}) | ||
| 367 | Matches if @var{function} applied to the value being matched returns a | ||
| 368 | value that matches @var{upattern}. This is like the @code{pred} | ||
| 369 | UPattern, except that it tests the result against @var{UPattern}, | ||
| 370 | rather than against a boolean truth value. The @var{function} call can | ||
| 371 | use one of the forms described below. | ||
| 372 | @item (or @var{upattern1} @var{upattern2}@dots{}) | ||
| 373 | Matches if one the argument UPatterns matches. As soon as the first | ||
| 374 | matching UPattern is found, the rest are not tested. For this reason, | ||
| 375 | if any of the UPatterns let-bind symbols to the matched value, they | ||
| 376 | should all bind the same symbols. | ||
| 377 | @item (and @var{upattern1} @var{upattern2}@dots{}) | ||
| 378 | Matches if all the argument UPatterns match. | ||
| 379 | @end table | ||
| 380 | |||
| 381 | The function calls used in the @code{pred} and @code{app} UPatterns | ||
| 382 | can have one of the following forms: | ||
| 383 | |||
| 384 | @table @asis | ||
| 385 | @item function symbol, like @code{integerp} | ||
| 386 | In this case, the named function is applied to the value being | ||
| 387 | matched. | ||
| 388 | @item lambda-function @code{(lambda (@var{arg}) @var{body})} | ||
| 389 | In this case, the lambda-function is called with one argument, the | ||
| 390 | value being matched. | ||
| 391 | @item @code{(@var{func} @var{args}@dots{})} | ||
| 392 | This is a function call with @var{n} specified arguments; the function | ||
| 393 | is called with these @var{n} arguments and an additional @var{n}+1-th | ||
| 394 | argument that is the value being matched. | ||
| 395 | @end table | ||
| 309 | 396 | ||
| 310 | It will first evaluate @var{exp} and then compare the value against each | 397 | Here's an illustrative example of using UPatterns: |
| 311 | @var{upattern} to see which @var{branch} to use, after which it will run the | ||
| 312 | corresponding @var{body-forms}. A common use case is to distinguish | ||
| 313 | between a few different constant values: | ||
| 314 | 398 | ||
| 399 | @c FIXME: This example should use every one of the UPatterns described | ||
| 400 | @c above at least once. | ||
| 315 | @example | 401 | @example |
| 316 | (pcase (get-return-code x) | 402 | (pcase (get-return-code x) |
| 317 | (`success (message "Done!")) | 403 | ('success (message "Done!")) |
| 318 | (`would-block (message "Sorry, can't do it now")) | 404 | ('would-block (message "Sorry, can't do it now")) |
| 319 | (`read-only (message "The shmliblick is read-only")) | 405 | ('read-only (message "The shmliblick is read-only")) |
| 320 | (`access-denied (message "You do not have the needed rights")) | 406 | ('access-denied (message "You do not have the needed rights")) |
| 321 | (code (message "Unknown return code %S" code))) | 407 | (code (message "Unknown return code %S" code))) |
| 322 | @end example | 408 | @end example |
| 323 | 409 | ||
| 324 | In the last clause, @code{code} is a variable that gets bound to the value that | 410 | The QPatterns are more powerful. They allow to match the value of the |
| 325 | was returned by @code{(get-return-code x)}. | 411 | @var{expression} that is the first argument of @code{pcase} against |
| 412 | specifications of its @emph{structure}. For example, you can specify | ||
| 413 | that the value must be a list of 2 elements whose first element is a | ||
| 414 | string and the second element is a number. QPatterns can have one of | ||
| 415 | the following forms: | ||
| 416 | |||
| 417 | @table @code | ||
| 418 | @item `(@var{qpattern1} . @var{qpattern2}) | ||
| 419 | Matches if the value being matched is a cons cell whose @code{car} | ||
| 420 | matches @var{qpattern1} and whose @code{cdr} matches @var{qpattern2}. | ||
| 421 | @item `[@var{qpattern1} @var{qpattern2} @dots{} @var{qpatternm}] | ||
| 422 | Matches if the value being matched is a vector of length @var{m} whose | ||
| 423 | @code{0}..@code{(@var{m}-1)}th elements match @var{qpattern1}, | ||
| 424 | @var{qpattern2} @dots{} @var{qpatternm}, respectively. | ||
| 425 | @item `(,@var{upattern1} ,@var{upattern2} @dots{}) | ||
| 426 | Matches if the value being matched is a list whose elements match the | ||
| 427 | corresponding @var{upattern1}, @var{upattern2}, etc. | ||
| 428 | @item @var{atom} | ||
| 429 | Matches if corresponding element of the value being matched is | ||
| 430 | @code{equal} to the specified @var{atom}. | ||
| 431 | @item ,@var{upattern} | ||
| 432 | Matches if the corresponding element of the value being matched | ||
| 433 | matches the specified @var{upattern}. | ||
| 434 | @end table | ||
| 435 | |||
| 436 | @end defmac | ||
| 326 | 437 | ||
| 327 | To give a more complex example, a simple interpreter for a little | 438 | Here is an example of using @code{pcase} to implement a simple |
| 328 | expression language could look like (note that this example requires | 439 | interpreter for a little expression language (note that this example |
| 329 | lexical binding): | 440 | requires lexical binding, @pxref{Lexical Binding}): |
| 330 | 441 | ||
| 331 | @example | 442 | @example |
| 332 | (defun evaluate (exp env) | 443 | (defun evaluate (exp env) |
| @@ -340,13 +451,19 @@ lexical binding): | |||
| 340 | (_ (error "Unknown expression %S" exp)))) | 451 | (_ (error "Unknown expression %S" exp)))) |
| 341 | @end example | 452 | @end example |
| 342 | 453 | ||
| 343 | Where @code{`(add ,x ,y)} is a pattern that checks that @code{exp} is a three | 454 | Here @code{`(add ,x ,y)} is a pattern that checks that @code{exp} is a |
| 344 | element list starting with the symbol @code{add}, then extracts the second and | 455 | three-element list starting with the literal symbol @code{add}, then |
| 345 | third elements and binds them to the variables @code{x} and @code{y}. | 456 | extracts the second and third elements and binds them to the variables |
| 346 | @code{(pred numberp)} is a pattern that simply checks that @code{exp} | 457 | @code{x} and @code{y}. Then it evaluates @code{x} and @code{y} and |
| 347 | is a number, and @code{_} is the catch-all pattern that matches anything. | 458 | adds the results. The @code{call} and @code{fn} patterns similarly |
| 459 | implement two flavors of function calls. @code{(pred numberp)} is a | ||
| 460 | pattern that simply checks that @code{exp} is a number and if so, | ||
| 461 | evaluates it. @code{(pred symbolp)} matches symbols, and returns | ||
| 462 | their association. Finally, @code{_} is the catch-all pattern that | ||
| 463 | matches anything, so it's suitable for reporting syntax errors. | ||
| 348 | 464 | ||
| 349 | Here are some sample programs including their evaluation results: | 465 | Here are some sample programs in this small language, including their |
| 466 | evaluation results: | ||
| 350 | 467 | ||
| 351 | @example | 468 | @example |
| 352 | (evaluate '(add 1 2) nil) ;=> 3 | 469 | (evaluate '(add 1 2) nil) ;=> 3 |
| @@ -355,56 +472,13 @@ Here are some sample programs including their evaluation results: | |||
| 355 | (evaluate '(sub 1 2) nil) ;=> error | 472 | (evaluate '(sub 1 2) nil) ;=> error |
| 356 | @end example | 473 | @end example |
| 357 | 474 | ||
| 358 | There are two kinds of patterns involved in @code{pcase}, called | 475 | Additional UPatterns can be defined using the @code{pcase-defmacro} |
| 359 | @emph{U-patterns} and @emph{Q-patterns}. The @var{upattern} mentioned above | 476 | macro. |
| 360 | are U-patterns and can take the following forms: | ||
| 361 | 477 | ||
| 362 | @table @code | 478 | @defmac pcase-defmacro name args &rest body |
| 363 | @item `@var{qpattern} | 479 | Define a new UPattern for @code{pcase}. The UPattern will have the |
| 364 | This is one of the most common form of patterns. The intention is to mimic the | 480 | form @code{(@var{name} @var{args})}. |
| 365 | backquote macro: this pattern matches those values that could have been built | 481 | @end defmac |
| 366 | by such a backquote expression. Since we're pattern matching rather than | ||
| 367 | building a value, the unquote does not indicate where to plug an expression, | ||
| 368 | but instead it lets one specify a U-pattern that should match the value at | ||
| 369 | that location. | ||
| 370 | |||
| 371 | More specifically, a Q-pattern can take the following forms: | ||
| 372 | @table @code | ||
| 373 | @item (@var{qpattern1} . @var{qpattern2}) | ||
| 374 | This pattern matches any cons cell whose @code{car} matches @var{qpattern1} and | ||
| 375 | whose @code{cdr} matches @var{qpattern2}. | ||
| 376 | @item [@var{qpattern1} @var{qpattern2} @dots{} @var{qpatternm}] | ||
| 377 | This pattern matches a vector of length @var{M} whose 0..(@var{M}-1)th | ||
| 378 | elements match @var{qpattern1}, @var{qpattern2} @dots{} @var{qpatternm}, | ||
| 379 | respectively. | ||
| 380 | @item @var{atom} | ||
| 381 | This pattern matches any atom @code{equal} to @var{atom}. | ||
| 382 | @item ,@var{upattern} | ||
| 383 | This pattern matches any object that matches the @var{upattern}. | ||
| 384 | @end table | ||
| 385 | |||
| 386 | @item @var{symbol} | ||
| 387 | A mere symbol in a U-pattern matches anything, and additionally let-binds this | ||
| 388 | symbol to the value that it matched, so that you can later refer to it, either | ||
| 389 | in the @var{body-forms} or also later in the pattern. | ||
| 390 | @item _ | ||
| 391 | This so-called @emph{don't care} pattern matches anything, like the previous | ||
| 392 | one, but unlike symbol patterns it does not bind any variable. | ||
| 393 | @item (pred @var{pred}) | ||
| 394 | This pattern matches if the function @var{pred} returns non-@code{nil} when | ||
| 395 | called with the object being matched. | ||
| 396 | @item (or @var{upattern1} @var{upattern2}@dots{}) | ||
| 397 | This pattern matches as soon as one of the argument patterns succeeds. | ||
| 398 | All argument patterns should let-bind the same variables. | ||
| 399 | @item (and @var{upattern1} @var{upattern2}@dots{}) | ||
| 400 | This pattern matches only if all the argument patterns succeed. | ||
| 401 | @item (guard @var{exp}) | ||
| 402 | This pattern ignores the object being examined and simply succeeds if @var{exp} | ||
| 403 | evaluates to non-@code{nil} and fails otherwise. It is typically used inside | ||
| 404 | an @code{and} pattern. For example, @code{(and x (guard (< x 10)))} | ||
| 405 | is a pattern which matches any number smaller than 10 and let-binds it to | ||
| 406 | the variable @code{x}. | ||
| 407 | @end table | ||
| 408 | 482 | ||
| 409 | @node Combining Conditions | 483 | @node Combining Conditions |
| 410 | @section Constructs for Combining Conditions | 484 | @section Constructs for Combining Conditions |
| @@ -1400,7 +1400,9 @@ that happen, `unhandled-file-name-directory' now defaults to calling | |||
| 1400 | * Lisp Changes in Emacs 25.1 | 1400 | * Lisp Changes in Emacs 25.1 |
| 1401 | 1401 | ||
| 1402 | ** pcase | 1402 | ** pcase |
| 1403 | +++ | ||
| 1403 | *** New UPatterns `quote', `app'. | 1404 | *** New UPatterns `quote', `app'. |
| 1405 | +++ | ||
| 1404 | *** New UPatterns can be defined with `pcase-defmacro'. | 1406 | *** New UPatterns can be defined with `pcase-defmacro'. |
| 1405 | +++ | 1407 | +++ |
| 1406 | *** New vector QPattern. | 1408 | *** New vector QPattern. |