emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase ` meaning


From: Garreau\, Alexandre
Subject: Re: pcase ` meaning
Date: Tue, 30 Oct 2018 01:27:42 +0100
User-agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-09-15, modified by Debian

On 2018-10-29 at 13:26, Clément Pit-Claudel wrote:
> On 28/10/2018 19.09, Garreau, Alexandre wrote:
>> the pattern look like what the data should be. adding “`” and “,”
>> destroy this feature, because the pattern no longer looks like what
>> is matched.
>
> Maybe it's a matter of perspective? Adding "`" and "," is precisely
> what makes the pattern look like the data: the invariant is that if
> you have a match, repeating the pattern produces the original data.

Oh, you mean “repeating *in the body*”?  Didn’t notice that.  Indeed,
this is something I didn’t notice: that’s making me way more comfortable
with “`” (as it is used this way in all schemes too, so its meaning
couldn’t and won’t be changed).

But normally, even if, as a side effect, pattern matching match in
appearance what it’d be if you wanted to repeat the pattern, repeated,
like here:

#+BEGIN_SRC emacs-lisp
  (pcase `(1 2 3) (`(,a ,b ,c) `(,a ,b ,c)))
#+END_SRC

(or may appear also as '(1 2 3), or (list 1 2 3))

#+BEGIN_SRC ocaml
  match [1;2;3] with [a;b;c] -> [a;b;c]
#+END_SRC

But this is a non-primordial side effect.  Notice how in the later case
the matched data *always* look the same as the pattern (beside replacing
a number per an identifier): *that’s* the purpose of pattern matching.
While in the lisp case you got those extra “,”.

Of course, these might not cause problem if, incidentally, you were to
use them in matched data too (but it’d be odd):

#+BEGIN_SRC emacs-lisp
  (pcase `(,x ,y ,z) (`(,a ,b ,c) `(,a ,b ,c)))
#+END_SRC

But that will rarely happen, as it is somewhat redundant.

> Here's a concrete example: (pcase '(1 2 3 4) (`(,x ,y . ,z) `(,x ,y
> . ,z))) is a no-op.  In other words, q-patterns are equations with
> multiple unknown variables (holes).  When you write a q-pattern,
> you're asking "what values should the variables in this pattern take
> so that evaluating the pattern would return the matched data?"

Normally, the pattern isn’t made related to the reuse of its var, but
related to the matched data, to look like it.  Only then, by
implication, you got it to look like what you would do to reconstruct it
in the body.

But that is a really interesting perspective I missed.  Thank you for
sharing it.  This is the kind of implicit “logical” intuitive ideas that
make designs meaningful that are kept unwritten, while they should, I
believe, be advertised so to explain why a specific design is handy or
natural (if ever newcomers found it unnatural).



reply via email to

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