emacs-devel
[Top][All Lists]
Advanced

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

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


From: Garreau\, Alexandre
Subject: pcase ` meaning [Was: Re: Replace trivial pcase occurrences in the Emacs sources]
Date: Sun, 28 Oct 2018 03:44:21 +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-28 at 03:07, Garreau, Alexandre wrote:
> On 2018-10-28 at 02:21, Michael Heerdegen wrote:
>> Dmitry Gutov <address@hidden> writes:
>>
>>> We could also assume that the silent majority is okay with the way
>>> things are. For example, I like pcase, even if more complex cases
>>> might look cryptic.
>>
>> I also like it, and your assumption could be true.  Who knows.
>>
>> Pcase was a big improvement to what can be expressed with Elisp.  And I
>> think its syntax and semantics are quite straightforward.  I doubt we
>> will find something more simplistic that has the same power.
>>
>> Probably the main problem was that the documentation was originally
>> written in a quite terse academic style.  Also, the recursive nature of
>> ` that makes it possible to combine it with all other patter types is
>> hard to get, but also very natural and powerful.  It's just consistent
>> that when backquote is used to construct complicated lists, it is also
>> used to build complex patterns to match such lists.  Nonetheless it
>> seems that ` is one of the main reasons for the bad feelings some people
>> have about pcase.
>
> Isn’t ` needed in just the same cases you could as well use guard or
> predefined patterns?  when you need to eval things?  If there is some
> redundance or covering between both, maybe ` could be avoided so it
> confuses less?

Nevermind, I just discovered that normally rar ` is mandatory for the
main use case of pcase, pattern matching, which I find weird.  So its
normal syntax is what case should be, and its ` syntax is what pcase
should be.

Initially I expected this would have worked the same:

#+BEGIN_SRC emacs-lisp
  (defun evaluate (exp env)
    (pcase exp
      (('add x y)       (+ (evaluate x env) (evaluate y env)))
      (('call fun arg)  (funcall (evaluate fun env) (evaluate arg env)))
      (('fn arg body)   (lambda (val)
                            (evaluate body (cons (cons arg val) env))))
      ((pred numberp)     exp)
      ((pred symbolp)     (cdr (assq exp env)))
      (_                  (error "Unknown expression %S" exp))))
#+END_SRC

because intuitively, I thought what would apply to a raw top-level atom,
would apply to it as a component of a sequence, but it seem I’m wrong.

Which indeed is unreasonable, as it is incompatible with pcase
non-pattern-matching own mini-language, which strangely seems to be more
important to pcase than pattern matching.

So know I understand why advocating for replacing case with pcase: pcase
is not about pattern matching, it is just a correctly working case,
except it only works for atoms, with an optional prevailing
mini-language, and even more optional, additive and not straightforward
pattern matching.

Maybe that would make more sense if ` (and '?) were not part of pcase’s
own mini-language, redefined by pcase, but if pcase actually truly
eval’d its pattern, defining pred, guard, etc. as real functions,
defined with a cl-letf, using lexical scoping so to prevailably define
all these + user-defined patterns.

But even then, “,” I feel like would barely make sense… it’s used the
same way, but doesn’t mean the same thing.  It doesn’t mean “eval”, as
the thing after “,”, if not a function call, might be something not
bound to a value, so it only means “undo the ‘`’, do like I didn’t do
it”, which is inconsistent with general use of “`”.

When seeing “,var”, I’m thinking “the actual value of var, or an error”,
so as I don’t see var defined anywere, I feel like an error is coming,
and as I know none is coming, I feel something is wrong.  I don’t see
how to eval ,var without getting an error, except by redefining how to
eval, or rather how to get a symbol value.

It even more feel wrong that, this, doesn’t work, for no reason (it
can’t be incompatible with the minilanguage):

#+BEGIN_SRC emacs-lisp
  (pcase [1 2]
    ([a b] (+ a b)))
#+END_SRC

Yet that’s the most intuitive and straightforward way of using pattern
matching.



reply via email to

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