emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase ` meaning [Was: Re: Replace trivial pcase occurrences in the E


From: Garreau\, Alexandre
Subject: Re: pcase ` meaning [Was: Re: Replace trivial pcase occurrences in the Emacs sources]
Date: Mon, 29 Oct 2018 11:22:57 +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

Le 29/10/2018 à 00h57, Michael Heerdegen a écrit :
> "Garreau, Alexandre" <address@hidden> writes:
>
>> “(var name)”, as does racket and some common-lisp match/unify
>> implementations.  And so to integrate with “`”: “,(var name)” doesn’t
>> disturb me, it is okay: will bind “name” to something, pretty
>> straightforward.
>
> That would make totally sense to me.  I often found it confusing that
> using a symbol which is already bound (outside of pcase) isn't turned
> into an equality test.  Having an explicit `var' or `bind' for binding
> variables would be nice.

Mmmh, that’s what I thought initially, and why I regretted cl-case
didn’t eval its arguments, but in reality, neither pcase does, though it
gives this impression, since it requires quoting symbols for equality.

“var” is to make explicit bind variable bindings where it could not
being done so otherwise, so to me ,(var name) looks less confusing, and,
if lists and arrays out of the box matched lists and arrays, so that
“(pcase (list 1 2 3) ((1 2 3) t))” was true, it would allow to bind a
list first element when it’s also the name of a predefined pattern, like
“(pcase (list 1 2 3) (((var and) 2 3) and))” would give “1” (it looks
silly to use a such name for a variable, but we never know, and at least
it makes pcase not unable to match some patterns (without ` I mean),
like guile’s match).

Also, as to speak about common pattern matching facilities, and how they
make patterns look related to their match: all of them make normal,
unbound identifier, lead to binding, instead of evaluation and
replacement by their bound value.  I (strangely, I realize no), see no
facilities in pattern matching in non-lisp languages to match the value
of an already bound variable… or am I wrong? do they allow that?

Even in pcase I don’t see how to do that except manually using (and var
(guard (equal var val))), which exactely looks like a cond, but more
complex, embed in a, now uselessly complex pcase, which, then, defeat
its purpose.

And yet that’d been the behavior I’d imagine for ,var, in `, that’s why
it’s confusing.  However, I cannot suggest to change it to lead ,var to
be evaluated to its value bound outside of pcase, because the way pcase
bind ` seems to already be used by several other pattern matching
facilities, so that’d break compatibility and bring confusion, too,
sadly (maybe a definable pattern could change the default ` behavior, so
people could experiment this behavior at least? yet their code could
lead to confusion then).

>> Beside lisp, all pattern matching language make their feature serve
>> one purpose: the pattern look like what the data should be. adding “`”
>> and “,” destroy this feature, because the pattern no longer looks like
>> what is matched.
>
> But with, for example, el-search, that suddenly looks very natural if
> you can, for example, transpose the first two arguments of all `foo'
> calls in your code with the rule.
>
>    `(foo ,a ,b . ,rest) -> `(foo ,b ,a . ,rest)

No it doesn’t look natural: the transformation does, the reading is
straightforward, the result is, but making the first pattern isn’t,
because a and b aren’t bound to anything, so the “`” meaning is truly
different (at least partially) than normal.

> The pattern exactly looks like the matched data!  Just with a different
> point of view.  Using ``' is not what most people would naively use to
> implement destructuring, but I don't find it unnatural or not intuitive,
> no.

Afaik, the matched data, here, is “(foo <something> <something>
. <something>)”, like “(foo 1 2 . 3)” (unquoted), or, when quoted “('foo
1 2 . 3)” (or “'(foo 1 2 . 3)” if you want, but if you ever changed to
“`” here, you’d have to admit it doesn’t mean the same as in pcase, and,
obviously: the fact you maybe used it in the data you want to match,
doesn’t mean you need to use it in the pattern (because the reader
removes it) so the purpose is defeated), that’s not what I call “look
exactely like it”: “(foo 1 2 . 3)” is “exactely”, “('foo a b . rest)”
would be enough.  I find “`(foo ,(var a) ,(var b) . ,(var rest))” is
still understandable and not that confusing, but it *doesn’t* look like
the matched data, like it does in other languages, such as
ocaml/haskell/rust/etc.

> The above pattern would look different if we would have to write it as
>
>   `(foo ,(var a) ,(var b) . ,(var rest))
>
> so the implicit binding feature of symbols also makes patterns much more
> readable in more complex cases, which is a big win.

It is a big win only in terms of avoiding confusion (with “`”), and
allowing otherwise impossible stuff (without “`”), but I wasn’t
proposing it to make more readable, only more powerful and less
confusing.  What would make more readable are directly matching them
when they don’t correspound an already defined pattern (such as (1 2 3),
since no pattern is defined after a number, or absolutely all occurences
of [], since no pattern is defined after an array), like guile’s and
racket’s matches, and some common lisp matches, do, and, otherwise,
pattern named after type constructor such as `vector' or `list', as you
suggested in the other thread, to correct me:

#+begin_src emacs-lisp
(pcase-defmacro list (&rest args)
  `(,'\`  ,(mapcar (lambda (thing) `(,'\, ,thing)) args)))
#+end_src

I tried to adapt for arrays:

#+begin_src emacs-lisp
(pcase-defmacro array (&rest args)
  `[,'\` ,(mapcar (lambda (thing) `(,'\, ,thing)) args)])
#+end_src

But it doesn’t work x), that, plus my initial non-working
“(pcase-defmacro list (&rest args) ``(,@args))”, I must find “`”, is,
indeed, quite confusing (how is “,'” needed? how isn’t “,'\`” equivalent
to “\`”?), even in real life (or maybe here it is because of pcase it is
that complex?), when it get complex, like used directly recursively.



reply via email to

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