aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/control.texi
diff options
context:
space:
mode:
authorGlenn Morris2018-11-04 09:37:03 -0800
committerGlenn Morris2018-11-04 09:37:03 -0800
commit410e65e4ce6f871fd1b8a2ef4b227cbeeb17c1dd (patch)
tree7b7272b5ddfc5b51992b590e1ec63136a3f20381 /doc/lispref/control.texi
parent4fbdccedd58ffe4cd5f7ed7b744123cc25084bc4 (diff)
parent6937c35d3260fe3fc32249313c7e9b6231cbd3dd (diff)
downloademacs-410e65e4ce6f871fd1b8a2ef4b227cbeeb17c1dd.tar.gz
emacs-410e65e4ce6f871fd1b8a2ef4b227cbeeb17c1dd.zip
Merge from origin/emacs-26
6937c35 (origin/emacs-26) Improve recent changes in documentation of ... c04b48c Rewrite documentation of buffer display 7cadb32 ; * doc/lispref/control.texi (pcase Macro): Fix another typo. 963f1d9 ; * doc/lispref/control.texi (pcase Macro): Fix a typo. e824c91 Improve documentation of destructuring-binding macros
Diffstat (limited to 'doc/lispref/control.texi')
-rw-r--r--doc/lispref/control.texi228
1 files changed, 120 insertions, 108 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index e5041ee627b..5cc43c428ad 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -419,65 +419,68 @@ This is not completely equivalent because it can evaluate @var{arg1} or
419@node Pattern-Matching Conditional 419@node Pattern-Matching Conditional
420@section Pattern-Matching Conditional 420@section Pattern-Matching Conditional
421@cindex pcase 421@cindex pcase
422@cindex pattern matching 422@cindex pattern matching, programming style
423 423
424Aside from the four basic conditional forms, Emacs Lisp also 424Aside from the four basic conditional forms, Emacs Lisp also
425has a pattern-matching conditional form, the @code{pcase} macro, 425has a pattern-matching conditional form, the @code{pcase} macro,
426a hybrid of @code{cond} and @code{cl-case} 426a hybrid of @code{cond} and @code{cl-case}
427(@pxref{Conditionals,,,cl,Common Lisp Extensions}) 427(@pxref{Conditionals,,,cl,Common Lisp Extensions})
428that overcomes their limitations and introduces 428that overcomes their limitations and introduces
429the @dfn{pattern matching} programming style. 429the @dfn{pattern matching programming style}.
430First, the limitations: 430The limitations that @code{pcase} overcomes are:
431 431
432@itemize 432@itemize
433@item The @code{cond} form chooses among alternatives 433@item
434by evaluating the predicate @var{condition} of each 434The @code{cond} form chooses among alternatives by evaluating the
435of its clauses (@pxref{Conditionals}). 435predicate @var{condition} of each of its clauses
436The primary limitation is that variables let-bound in @var{condition} 436(@pxref{Conditionals}). The primary limitation is that variables
437are not available to the clause's @var{body-forms}. 437let-bound in @var{condition} are not available to the clause's
438@var{body-forms}.
438 439
439Another annoyance (more an inconvenience than a limitation) 440Another annoyance (more an inconvenience than a limitation)
440is that when a series of @var{condition} predicates implement 441is that when a series of @var{condition} predicates implement
441equality tests, there is a lot of repeated code. 442equality tests, there is a lot of repeated code. (@code{cl-case}
442For that, why not use @code{cl-case}? 443solves this inconvenience.)
443 444
444@item 445@item
445The @code{cl-case} macro chooses among alternatives by evaluating 446The @code{cl-case} macro chooses among alternatives by evaluating
446the equality of its first argument against a set of specific 447the equality of its first argument against a set of specific
447values. 448values.
448The limitations are two-fold: 449
450Its limitations are two-fold:
449 451
450@enumerate 452@enumerate
451@item The equality tests use @code{eql}. 453@item
452@item The values must be known and written in advance. 454The equality tests use @code{eql}.
455@item
456The values must be known and written in advance.
453@end enumerate 457@end enumerate
454 458
455@noindent 459@noindent
456These render @code{cl-case} unsuitable for strings or compound 460These render @code{cl-case} unsuitable for strings or compound
457data structures (e.g., lists or vectors). 461data structures (e.g., lists or vectors). (@code{cond} doesn't have
458For that, why not use @code{cond}? 462these limitations, but it has others, see above.)
459(And here we end up in a circle.)
460@end itemize 463@end itemize
461 464
462@noindent 465@noindent
463Conceptually, the @code{pcase} macro borrows the first-arg focus 466Conceptually, the @code{pcase} macro borrows the first-arg focus
464of @code{cl-case} and the clause-processing flow of @code{cond}, 467of @code{cl-case} and the clause-processing flow of @code{cond},
465replacing @var{condition} with a generalization of 468replacing @var{condition} with a generalization of
466the equality test called @dfn{matching}, 469the equality test which is a variant of @dfn{pattern matching},
467and adding facilities so that you can concisely express a 470and adding facilities so that you can concisely express a
468clause's predicate, and arrange to share let-bindings between 471clause's predicate, and arrange to share let-bindings between
469a clause's predicate and @var{body-forms}. 472a clause's predicate and @var{body-forms}.
470 473
471The concise expression of a predicate is known as a @dfn{pattern}. 474The concise expression of a predicate is known as a @dfn{pattern}.
472When the predicate, called on the value of the first arg, 475When the predicate, called on the value of the first arg, returns
473returns non-@code{nil}, the pattern matches the value 476non-@code{nil}, we say that ``the pattern matches the value'' (or
474(or sometimes ``the value matches the pattern''). 477sometimes ``the value matches the pattern'').
475 478
476@menu 479@menu
477* The @code{pcase} macro: pcase Macro. Plus examples and caveats. 480* The @code{pcase} macro: pcase Macro. Includes examples and caveats.
478* Extending @code{pcase}: Extending pcase. Define new kinds of patterns. 481* Extending @code{pcase}: Extending pcase. Define new kinds of patterns.
479* Backquote-Style Patterns: Backquote Patterns. Structural matching. 482* Backquote-Style Patterns: Backquote Patterns. Structural patterns matching.
480* Destructuring patterns:: Using pcase patterns to extract subfields. 483* Destructuring with pcase Patterns:: Using pcase patterns to extract subfields.
481@end menu 484@end menu
482 485
483@node pcase Macro 486@node pcase Macro
@@ -498,30 +501,30 @@ of the last of @var{body-forms} in the successful clause.
498Otherwise, @code{pcase} evaluates to @code{nil}. 501Otherwise, @code{pcase} evaluates to @code{nil}.
499@end defmac 502@end defmac
500 503
501Each @var{pattern} has to be a @dfn{pcase pattern}, which can either 504@cindex pcase pattern
502use one of the core patterns defined below, or use one of the patterns 505Each @var{pattern} has to be a @dfn{pcase pattern}, which can use
503defined via @code{pcase-defmacro}. 506either one of the core patterns defined below, or one of the patterns
507defined via @code{pcase-defmacro} (@pxref{Extending pcase}).
504 508
505The rest of this subsection 509The rest of this subsection describes different forms of core
506describes different forms of core patterns, 510patterns, presents some examples, and concludes with important caveats
507presents some examples, 511on using the let-binding facility provided by some pattern forms. A
508and concludes with important caveats on using the 512core pattern can have the following forms:
509let-binding facility provided by some pattern forms.
510A core pattern can have the following forms:
511 513
512@table @code 514@table @code
513 515
514@item _ 516@item _
515Matches any @var{expval}. 517Matches any @var{expval}.
516This is known as @dfn{don't care} or @dfn{wildcard}. 518This is also known as @dfn{don't care} or @dfn{wildcard}.
517 519
518@item '@var{val} 520@item '@var{val}
519Matches if @var{expval} is @code{equal} to @var{val}. 521Matches if @var{expval} equals @var{val}. The comparison is done as
522if by @code{equal} (@pxref{Equality Predicates}).
520 523
521@item @var{keyword} 524@item @var{keyword}
522@itemx @var{integer} 525@itemx @var{integer}
523@itemx @var{string} 526@itemx @var{string}
524Matches if @var{expval} is @code{equal} to the literal object. 527Matches if @var{expval} equals the literal object.
525This is a special case of @code{'@var{val}}, above, 528This is a special case of @code{'@var{val}}, above,
526possible because literal objects of these types are self-quoting. 529possible because literal objects of these types are self-quoting.
527 530
@@ -533,17 +536,17 @@ Matches any @var{expval}, and additionally let-binds @var{symbol} to
533If @var{symbol} is part of a sequencing pattern @var{seqpat} 536If @var{symbol} is part of a sequencing pattern @var{seqpat}
534(e.g., by using @code{and}, below), the binding is also available to 537(e.g., by using @code{and}, below), the binding is also available to
535the portion of @var{seqpat} following the appearance of @var{symbol}. 538the portion of @var{seqpat} following the appearance of @var{symbol}.
536This usage has some caveats (@pxref{pcase-symbol-caveats,,caveats}). 539This usage has some caveats, see @ref{pcase-symbol-caveats,,caveats}.
537 540
538Two symbols to avoid are @code{t}, which behaves like @code{_} 541Two symbols to avoid are @code{t}, which behaves like @code{_}
539(above) and is deprecated, and @code{nil}, which signals error. 542(above) and is deprecated, and @code{nil}, which signals an error.
540Likewise, it makes no sense to bind keyword symbols 543Likewise, it makes no sense to bind keyword symbols
541(@pxref{Constant Variables}). 544(@pxref{Constant Variables}).
542 545
543@item (pred @var{function}) 546@item (pred @var{function})
544Matches if the predicate @var{function} returns non-@code{nil} 547Matches if the predicate @var{function} returns non-@code{nil}
545when called on @var{expval}. 548when called on @var{expval}.
546@var{function} can have one of the possible forms: 549the predicate @var{function} can have one of the following forms:
547 550
548@table @asis 551@table @asis
549@item function name (a symbol) 552@item function name (a symbol)
@@ -570,20 +573,17 @@ the actual function call becomes: @w{@code{(= 42 @var{expval})}}.
570@item (app @var{function} @var{pattern}) 573@item (app @var{function} @var{pattern})
571Matches if @var{function} called on @var{expval} returns a 574Matches if @var{function} called on @var{expval} returns a
572value that matches @var{pattern}. 575value that matches @var{pattern}.
573@var{function} can take one of the 576@var{function} can take one of the forms described for @code{pred},
574forms described for @code{pred}, above. 577above. Unlike @code{pred}, however, @code{app} tests the result
575Unlike @code{pred}, however, 578against @var{pattern}, rather than against a boolean truth value.
576@code{app} tests the result against @var{pattern},
577rather than against a boolean truth value.
578 579
579@item (guard @var{boolean-expression}) 580@item (guard @var{boolean-expression})
580Matches if @var{boolean-expression} evaluates to non-@code{nil}. 581Matches if @var{boolean-expression} evaluates to non-@code{nil}.
581 582
582@item (let @var{pattern} @var{expr}) 583@item (let @var{pattern} @var{expr})
583Evaluates @var{expr} to get @var{exprval} 584Evaluates @var{expr} to get @var{exprval} and matches if @var{exprval}
584and matches if @var{exprval} matches @var{pattern}. 585matches @var{pattern}. (It is called @code{let} because @var{pattern}
585(It is called @code{let} because 586can bind symbols to values using @var{symbol}.)
586@var{pattern} can bind symbols to values using @var{symbol}.)
587@end table 587@end table
588 588
589@cindex sequencing pattern 589@cindex sequencing pattern
@@ -596,18 +596,16 @@ but instead of processing values, they process sub-patterns.
596 596
597@table @code 597@table @code
598@item (and @var{pattern1}@dots{}) 598@item (and @var{pattern1}@dots{})
599Attempts to match @var{pattern1}@dots{}, in order, 599Attempts to match @var{pattern1}@dots{}, in order, until one of them
600until one of them fails to match. 600fails to match. In that case, @code{and} likewise fails to match, and
601In that case, @code{and} likewise fails to match, 601the rest of the sub-patterns are not tested. If all sub-patterns
602and the rest of the sub-patterns are not tested. 602match, @code{and} matches.
603If all sub-patterns match, @code{and} matches.
604 603
605@item (or @var{pattern1} @var{pattern2}@dots{}) 604@item (or @var{pattern1} @var{pattern2}@dots{})
606Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order, 605Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order,
607until one of them succeeds. 606until one of them succeeds. In that case, @code{or} likewise matches,
608In that case, @code{or} likewise matches, 607and the rest of the sub-patterns are not tested. (Note that there
609and the rest of the sub-patterns are not tested. 608must be at least two sub-patterns.
610(Note that there must be at least two sub-patterns.
611Simply @w{@code{(or @var{pattern1})}} signals error.) 609Simply @w{@code{(or @var{pattern1})}} signals error.)
612@c Issue: Is this correct and intended? 610@c Issue: Is this correct and intended?
613@c Are there exceptions, qualifications? 611@c Are there exceptions, qualifications?
@@ -1042,12 +1040,11 @@ Both use a single backquote construct (@pxref{Backquote}).
1042 1040
1043This subsection describes @dfn{backquote-style patterns}, 1041This subsection describes @dfn{backquote-style patterns},
1044a set of builtin patterns that eases structural matching. 1042a set of builtin patterns that eases structural matching.
1045For background, @xref{Pattern-Matching Conditional}. 1043For background, @pxref{Pattern-Matching Conditional}.
1046 1044
1047@dfn{Backquote-style patterns} are a powerful set of 1045Backquote-style patterns are a powerful set of @code{pcase} pattern
1048@code{pcase} pattern extensions (created using @code{pcase-defmacro}) 1046extensions (created using @code{pcase-defmacro}) that make it easy to
1049that make it easy to match @var{expval} against 1047match @var{expval} against specifications of its @emph{structure}.
1050specifications of its @emph{structure}.
1051 1048
1052For example, to match @var{expval} that must be a list of two 1049For example, to match @var{expval} that must be a list of two
1053elements whose first element is a specific string and the second 1050elements whose first element is a specific string and the second
@@ -1171,87 +1168,102 @@ evaluation results:
1171(evaluate '(sub 1 2) nil) @result{} error 1168(evaluate '(sub 1 2) nil) @result{} error
1172@end example 1169@end example
1173 1170
1174@node Destructuring patterns 1171@node Destructuring with pcase Patterns
1175@subsection Destructuring Patterns 1172@subsection Destructuring with @code{pcase} Patterns
1176@cindex destructuring patterns 1173@cindex destructuring with pcase patterns
1177 1174
1178Pcase patterns not only express a condition on the form of the objects 1175Pcase patterns not only express a condition on the form of the objects
1179they can match but they can also extract sub-fields of those objects. 1176they can match, but they can also extract sub-fields of those objects.
1180Say we have a list and want to extract 2 elements from it with the 1177For example we can extract 2 elements from a list that is the value of
1181following code: 1178the variable @code{my-list} with the following code:
1182 1179
1183@example 1180@example
1184 (pcase l 1181 (pcase my-list
1185 (`(add ,x ,y) (message "Contains %S and %S" x y))) 1182 (`(add ,x ,y) (message "Contains %S and %S" x y)))
1186@end example 1183@end example
1187 1184
1188This will not only extract @code{x} and @code{y} but will additionally 1185This will not only extract @code{x} and @code{y} but will additionally
1189test that @code{l} is a list containing exactly 3 elements and whose 1186test that @code{my-list} is a list containing exactly 3 elements and
1190first element is the symbol @code{add}. If any of those tests fail, 1187whose first element is the symbol @code{add}. If any of those tests
1191@code{pcase} will directly return @code{nil} without calling 1188fail, @code{pcase} will immediately return @code{nil} without calling
1192@code{message}. 1189@code{message}.
1193 1190
1194@dfn{Destructuring} of an object is an operation that extracts 1191Extraction of multiple values stored in an object is known as
1195multiple values stored in the object, e.g., the 2nd and the 3rd 1192@dfn{destructuring}. Using @code{pcase} patterns allows to perform
1196element of a list or a vector. @dfn{Destructuring binding} is 1193@dfn{destructuring binding}, which is similar to a local binding
1197similar to a local binding (@pxref{Local Variables}), but it gives 1194(@pxref{Local Variables}), but gives values to multiple elements of
1198values to multiple elements of a variable by extracting those values 1195a variable by extracting those values from an object of compatible
1199from an object of compatible structure. 1196structure.
1200 1197
1201The macros described in this section use @dfn{destructuring 1198The macros described in this section use @code{pcase} patterns to
1202patterns}, which are normal Pcase patterns used in a context where we 1199perform destructuring binding. The condition of the object to be of
1203presume that the object does match the pattern, and we only want 1200compatible structure means that the object must match the pattern,
1204to extract some subfields. For example: 1201because only then the object's subfields can be extracted. For
1202example:
1205 1203
1206@example 1204@example
1207 (pcase-let ((`(add ,x ,y) l)) 1205 (pcase-let ((`(add ,x ,y) my-list))
1208 (message "Contains %S and %S" x y)) 1206 (message "Contains %S and %S" x y))
1209@end example 1207@end example
1210 1208
1211@noindent 1209@noindent
1212does the same as the previous example, except that it directly tries 1210does the same as the previous example, except that it directly tries
1213to extract @code{x} and @code{y} from @code{l} without first verifying 1211to extract @code{x} and @code{y} from @code{my-list} without first
1214if @code{l} is a list which has the right number of elements and has 1212verifying if @code{my-list} is a list which has the right number of
1215@code{add} as its first element. 1213elements and has @code{add} as its first element. The precise
1216The precise behavior when the object does not actually match the 1214behavior when the object does not actually match the pattern is
1217pattern is undefined, although the body will not be silently skipped: 1215undefined, although the body will not be silently skipped: either an
1218either an error is signaled or the body is run with some of the 1216error is signaled or the body is run with some of the variables
1219variables potentially bound to arbitrary values like @code{nil}. 1217potentially bound to arbitrary values like @code{nil}.
1218
1219The pcase patterns that are useful for destructuring bindings are
1220generally those described in @ref{Backquote Patterns}, since they
1221express a specification of the structure of objects that will match.
1222
1223For an alternative facility for destructuring binding, see
1224@ref{seq-let}.
1220 1225
1221@defmac pcase-let bindings body@dots{} 1226@defmac pcase-let bindings body@dots{}
1222Bind variables according to @var{bindings} and then eval @var{body}. 1227Perform destructuring binding of variables according to
1228@var{bindings}, and then evaluate @var{body}.
1223 1229
1224@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern} 1230@var{bindings} is a list of bindings of the form @w{@code{(@var{pattern}
1225@var{exp})}}, where @var{exp} is an expression to evaluate and 1231@var{exp})}}, where @var{exp} is an expression to evaluate and
1226@var{pattern} is a destructuring pattern. 1232@var{pattern} is a @code{pcase} pattern.
1227 1233
1228All @var{exp}s are evaluated first after which they are matched 1234All @var{exp}s are evaluated first, after which they are matched
1229against their respective @var{pattern}, introducing new variable 1235against their respective @var{pattern}, introducing new variable
1230bindings which can then be used inside @var{body}. 1236bindings that can then be used inside @var{body}. The variable
1237bindings are produced by destructuring binding of elements of
1238@var{pattern} to the values of the corresponding elements of the
1239evaluated @var{exp}.
1231@end defmac 1240@end defmac
1232 1241
1233@defmac pcase-let* bindings body@dots{} 1242@defmac pcase-let* bindings body@dots{}
1234Bind variables according to @var{bindings} and then eval @var{body}. 1243Perform destructuring binding of variables according to
1244@var{bindings}, and then evaluate @var{body}.
1235 1245
1236@var{bindings} is a list of bindings of the form @code{(@var{pattern} 1246@var{bindings} is a list of bindings of the form @code{(@var{pattern}
1237@var{exp})}, where @var{exp} is an expression to evaluate and 1247@var{exp})}, where @var{exp} is an expression to evaluate and
1238@var{pattern} is a destructuring pattern. 1248@var{pattern} is a @code{pcase} pattern. The variable bindings are
1239 1249produced by destructuring binding of elements of @var{pattern} to the
1240Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is 1250values of the corresponding elements of the evaluated @var{exp}.
1241matched against its corresponding @var{pattern} before passing to the 1251
1242next element of @var{bindings}, so the variables introduced in each 1252Unlike @code{pcase-let}, but similarly to @code{let*}, each @var{exp}
1243binding are available in the @var{exp}s that follow it, additionally 1253is matched against its corresponding @var{pattern} before processing
1244to being available in @var{body}. 1254the next element of @var{bindings}, so the variable bindings
1255introduced in each one of the @var{bindings} are available in the
1256@var{exp}s of the @var{bindings} that follow it, additionally to
1257being available in @var{body}.
1245@end defmac 1258@end defmac
1246 1259
1247@findex dolist
1248@defmac pcase-dolist (pattern list) body@dots{} 1260@defmac pcase-dolist (pattern list) body@dots{}
1249This construct executes @var{body} once for each element of 1261Execute @var{body} once for each element of @var{list}, on each
1250@var{list}, in a context where the variables appearing in the the 1262iteration performing a destructuring binding of variables in
1251destructuring pattern @var{pattern} are bound to the corresponding 1263@var{pattern} to the values of the corresponding subfields of the
1252values found in the element. 1264element of @var{list}. The bindings are performed as if by
1253When @var{pattern} is a simple variable, this ends up being equivalent 1265@code{pcase-let}. When @var{pattern} is a simple variable, this ends
1254to @code{dolist}. 1266up being equivalent to @code{dolist} (@pxref{Iteration}).
1255@end defmac 1267@end defmac
1256 1268
1257 1269