[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pcase and the unpopular backquote pattern

From: Michael Heerdegen
Subject: pcase and the unpopular backquote pattern
Date: Wed, 20 Mar 2019 21:57:09 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)


I'm thinking about whether we should install something like this:

From eb8d1f86c744c119ede54cde07a36c1da061c766 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <address@hidden>
Date: Wed, 20 Mar 2019 21:21:03 +0100
Subject: [PATCH] * lisp/emacs-lisp/pcase.el: Add patterns list and list*

 lisp/emacs-lisp/pcase.el | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index c0a55f3a41..ae45959ff0 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -230,9 +230,10 @@ pcase--make-docstring
                       (when me
                         (push (cons symbol me)
-        ;; Ensure backquote is first.
-        (let ((x (assq '\` more)))
-          (setq more (cons x (delq x more))))
+        ;; Ensure list stuff is first.
+        (mapc (lambda (m) (let ((x (assq m more)))
+                            (setq more (cons x (delq x more)))))
+              (reverse '(list list* \`)))
         ;; Do the output.
         (while more
           (let* ((pair (pop more))
@@ -1009,5 +1010,24 @@ pcase--u1
    ;; compounded values that are not `consp'
    (t (error "Unknown QPAT: %S" qpat))))

+(pcase-defmacro list (&rest patterns)
+  "Match lists.
+\(list P0 ... Pn-1) matches any list with N elements that are
+matched by patterns P0...Pn-1 in order."
+  `(,'\` ,(mapcar (lambda (pat) `(,'\, ,pat)) patterns)))
+(pcase-defmacro list* (&rest patterns)
+  "Match lists with rest.
+\(list* P0 ... Pn-1 R) matches any possibly dotted list with at
+least N elements where the elements with index 0 to N-1 are
+matched by patterns P0...Pn-1 in order, and the list of the
+remaining elements (i.e. the Nth `cdr' of the matched list) is
+matched by pattern R."
+  (unless (cdr patterns)
+    (error "Pattern list* used with less than two arguments"))
+  `(,'\` ,(let* ((l (mapcar (lambda (pat) `(,'\, ,pat)) patterns)))
+            (setcdr (last l 2) (car (last l)))
+            l)))
 (provide 'pcase)
 ;;; pcase.el ends here

``' is disliked by some people because its semantics are, while elegant
and consistent, not easy to grasp.  I've got the impression that lots
of people dislike pcase mainly for the backquote patterns.

Given that backquote patterns are used mainly for lists, the suggested
list and list* patterns, analogue to functions list and cl-list*, are
not much less powerful, while their syntax and semantics can be
explained without using recursion.  One disadvantage is that when you
want to destructure nested lists you need to use nested "list" patterns.
I guess for some people that would still be easier to read.  Another
disadvantage would be that we would add completely redundant patterns -
but I don't really see a problem here when it would improve readability
for people.  So, like using the backquote macro only for construction of
complicated nested lists, we would use the backquote pcase pattern only
to construct the complicated patterns needing to perform deeper

A second, minor, reason why I find list and list* patterns useful apart
from that is that it helps to avoid the nested backquote mess you get
when you want to define a pcase macro that expands to a backquote

If you for example (just for demonstration, I know this could also be
implemented using 'app') you want to define a pattern 'car' that matches
any cons whose car is matched by a pattern you specify, so for example:

(pcase (list "Hallo" 1 2 3)
  ((car (pred stringp)) t))
==> t.

With list* the definition would look like

(pcase-defmacro car (pat)
  `(list* ,pat _))

while without you would need to write it as

(pcase-defmacro car (pat)
  `(,'\` ((,'\, ,pat) . (,'\, _))))

Not something one needs to do often, but when you need to do that, this
,'\, salad is a pain.

list and list* patterns are themselves expanding to backquote patterns
btw, so they do that work for you in the above case.

Ok, if people want something like that, I could replace at least the
simple (one-level) backquote pcase patterns with equivalent list and
list* forms in the Emacs sources.  What do you think?


P.S. BTW, if you wonder why we need two patterns list and list*: that's
because an argument list is not allowed to be a real dotted list, like
in (list 1 2 . 3), which is an invalid expression.  For the same reason
the dot syntax can't be used in pattern "list".  Arguments are allowed
to be dotted lists OTOH, so we could alternatively make it so that the
`list' pattern accepts only one argument, a list of patterns, which is
then allowed to be dotted.  That would be something less intuitive OTOH
and I would expect that it should work recursively as well... and then
it's only one more step and we are back to ``'.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]