Subject: bug#72328: [PATCH] Nested backquote in pcase

From: Michael Heerdegen <[email-masked]>
Date: Mon, 29 Jul 2024 18:03:59 +0200

Thuna <[email-masked]> writes:

> > This would be a backward-incompatible change, breaking existing code.
>
> Yes, I forgot to mention this in my original report, but given how
> unintuitive (IMO) the current behavior is, I expect that very few people
> will be using patterns which would be effected by this change.

We have _dozens_ of such patterns in the Emacs Elisp sources that would
break.  Your expectation is wrong.

> > And this change would break the symmetry between `backquote' the macro
> > and backquote patterns in `pcase'.  This is an important design idea.
>
> I am not quite sure why you think that this breaks symmetry with the
> backquote macro;

We want that something like this works as expected:

#+begin_src emacs-lisp
(let ((a 1) (b 2))
  (pcase `(69 foo (97 ,a ((,b))))
    (`(69 foo (97 ,a ((,b))))
    (list a b)))) ==> (1 2)
#+end_src

I think it's obvious what I mean with symmetry between backquote and
pcase backquote without giving a formal definition.

> the purpose of this patch was to establish a symmetry
> in the first place.  Would you consider the examples provided as
> unnatural?

I'm more concerned about what we would loose.

> AFAICT right now the only way to match an unevaluated (or quoted) `,foo
> is with `(,'\` (,'\, foo)) which is quite unideal.

If `foo` is the literal symbol you can simply match using a quote pattern
like in

  (pcase '`,foo
    ('`,foo t))
 ==> t

> It is not often that you need this but having this not be
> representable in this way is not particularly useful.

It indeed gets ugly when one wants to match a non-constant
backquote expression using a backquote pcase pattern with partial
unquotes (the "mixed case").

But making pcase backquote patterns less expressive just to make this
special case simpler doesn't make sense to me.  OTOH I agree that having
a convenient solution for this kind of problem would be nice.

Michael.