all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Emacs development discussions." <emacs-devel@gnu.org>
To: emacs-devel@gnu.org
Subject: Re: Code for cond*
Date: Sat, 27 Jan 2024 23:16:28 -0500	[thread overview]
Message-ID: <jwv34uignrp.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: E1rTDrE-0006Qc-Id@fencepost.gnu.org

Richard Stallman [2024-01-25 23:30:24] wrote:

> [[[ To any NSA and FBI agents reading my email: please consider    ]]]
> [[[ whether defending the US Constitution against all enemies,     ]]]
> [[[ foreign or domestic, requires you to follow Snowden's example. ]]]
>
>   > Finally, I see a fairly extensive but hardcoded pattern language, which
>   > seems like a regression compared to Pcase where the pattern language is
>   > built from a few primitives only (with the rest defined on top via
>   > `pcase-defmacro`).
>
> cond* also has a macro-like way to extend the patterns.

Is this what you're referring to?

        ((get (car subpat) 'cond*-expander)
         ;; Treat result as a subpattern.
         (cond*-subpat (funcall (get (car subpat) 'cond*-expander) subpat)
                       cdr-safe bindings backtrack-aliases data))

do we have reasons to believe that it's flexible enough?
For `pcase`, I needed to add the `app` pattern before `pcase-defmacro`
was actually usable for more realistic extensions.  And to be sure it
was flexible enough I restructured the code so that patterns like
backquote are not hardcoded in Pcase but are implemented using this
extension facility.

But more importantly, I really fail to see why we can't just use Pcase
patterns here (potentially with adjustments to Pcase)?

Let's look at them one by one:

    CONSTANT: nil, t, a keyword, a quoted constant,
    VARIABLE: a symbol
    A backquoted cons cell
    (or SUBPATTERNS...)
    (and SUBPATTERNS...)
    (rx RX-PATTERN)

these are identical to Pcase (well, maybe not 100%, since that depends on
details of the implementation which I haven't been able to test yet,
but to a large extent at least).

    MACRO-CALL: a list which is a call to a macro.

I used that trick in `gv.el` for places and it served us well, but I'm
wondering if it'll work as well here.  Can you think of macros for which
this will do something useful?  If it's rarely useful, then we have to
take into account the potential drawback of having "mysterious" behavior
or errors when the programmer didn't expect this macro expansion.

    A vector

Pcase currently doesn't support exactly this, but does support it within
the backquote pattern, i.e. instead of

    [PAT1 PAT2 ...]

you have to use

    `[,PAT1 ,PAT2 ...]

and with the other difference that it only matches if the two vectors
have the same length.  I resisted the addition of a vector pattern
because I didn't know which kinds of patterns would be most useful
(e.g. in terms of how the length should be constrained, ...).

We could add a pattern like the one you have in `match*` to Pcase, but
the experience with Pcase suggests that patterns like that on vectors
are *very* rarely use(d|ful), so I don't think it's worth the trouble.

    (cdr-safe BACKQUOTED-CONS-CELL)
    (cdr BACKQUOTED-CONS-CELL)

These would be easy to add to Pcase, of course.

    A string

In Pcase patterns, strings are considered to fall within the first case
above, i.e. constants.  We have `re-match` of course, when we need to
match a regex against a string.  Which one deserves the shorter syntax of
"just a string" is debatable (of course, some readers may point out
that the question would be moot if we had a special syntax for
regexps).

    (rx RX-PATTERN VARS...)

For some reason currently our `rx` doesn't have such an option, but it
would be easy to add, AFAICT.

    A backquoted structure constructor

We don't currently support hat in Pcase, but neither does your code, and
Pcase could support it just as well.

    (PRED VARIABLE)  or, more generally,

We don't have that currently.  Instead we have

    (pred PRED)

and if you want to bind it to a var you need

    (and (pred PRED) VAR)

Extending `pred` so it can bind the value to a variable is definitely an
option.  It's fairly often useful.  That's one of the reasons why I was
careful to limit `pred` to a single argument so far (so it has room to
grow by giving some useful meaning to additional args based on experience).

E.g. we could easily add support for

    (pred PRED VARIABLE)

And of course we could also add support for your (PRED VARIABLE) syntax,
although until now I've resisted the urge to eat up Pcase pattern's
namespace this way.

    (PRED VARIABLE OTHER-ARGS...)

Without the VARIABLE part, in Pcase this is almost the same as

    (pred (PRED OTHER-ARGS...))

except that in Pcase, the value is passed as the last argument rather as
first argument.  I haven't seen a strong reason to prefer one over the
other.  I've considered extending `pred` to support the use of `_` in
OTHER-ARGS... in order to specify the argument position of the value.
If we combine that with the above (pred PRED VAR) extension, then we
could get the same behavior as (PRED VARIABLE OTHER-ARGS...) with

    (pred (PRED _ OTHER-ARGS...) VARIABLE)

and finally:

    (constrain VAR EXPRESSION)

This would be very easy to add.  It's basically

    (and VAR (guard EXPRESSION))

and just as for `pred` there is evidence that it would be desirable to
extend it so as to include the `and` behavior, so we could have

    (guard VAR EXPRESSION)

IOW, while your patterns aren't 100% covered by current Pcase patterns,
Pcase can easily accommodate them, and it would be a shame to introduce
a second pattern language that's almost-identical-but-not-quite.

Is there *any* reason why `cond*` needs a different pattern language?


        Stefan


PS: Is the code available for testing somewhere?  The code I have here
(from Jan 19) doesn't work:

    ELISP> (cond* ((match* `(a ,y) '(a 5)) y))
    *** Eval error ***  Symbol’s value as variable is void: ba162
    ELISP>




  reply	other threads:[~2024-01-28  4:16 UTC|newest]

Thread overview: 134+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-18  3:37 Code for cond* Richard Stallman
2024-01-18  4:59 ` Emanuel Berg
2024-01-20  3:39   ` Richard Stallman
2024-01-24 12:37     ` Po Lu
2024-01-24 19:12     ` Alan Mackenzie
2024-01-27  3:35       ` Richard Stallman
2024-01-18 15:44 ` Andrea Corallo
2024-01-19 10:42   ` João Távora
2024-01-21  3:04     ` Richard Stallman
2024-01-21 20:05       ` Adam Porter
2024-01-22  5:32         ` tomas
2024-01-23 13:39         ` Richard Stallman
2024-01-24  6:02           ` Po Lu
2024-01-24  9:48       ` Stefan Kangas
2024-01-24 10:09         ` Emanuel Berg
2024-01-24 11:30         ` João Távora
2024-01-24 12:08           ` João Távora
2024-01-24 12:09         ` Po Lu
2024-01-24 12:22           ` Ihor Radchenko
2024-01-24 12:33             ` Po Lu
2024-01-24 13:34               ` Ihor Radchenko
2024-01-24 13:52                 ` João Távora
2024-01-24 14:31                   ` Po Lu
2024-01-27  3:35                   ` Richard Stallman
2024-01-27  3:35                   ` Richard Stallman
2024-01-24 14:07                 ` Po Lu
2024-01-24 14:20                   ` Ihor Radchenko
2024-01-24 14:34                     ` Po Lu
2024-01-24 14:44                       ` Ihor Radchenko
2024-01-24 14:47                         ` João Távora
2024-01-24 15:28                         ` Emanuel Berg
2024-01-24 16:30                           ` Ihor Radchenko
2024-01-24 16:34                             ` Emanuel Berg
2024-01-25  9:06                           ` Po Lu
2024-01-25  9:55                             ` Alfred M. Szmidt
2024-01-25 10:38                               ` Eli Zaretskii
2024-01-25 11:29                               ` Po Lu
2024-01-25 12:04                                 ` Emanuel Berg
2024-01-25 13:32                                   ` Po Lu
2024-01-25 14:08                                     ` Emanuel Berg
2024-01-25 13:19                                 ` Alfred M. Szmidt
2024-01-25 13:43                                   ` Emanuel Berg
2024-01-25 14:31                                     ` Eli Zaretskii
2024-01-25 15:02                                       ` Emanuel Berg
2024-01-25 15:29                                         ` Eli Zaretskii
2024-01-25 15:33                                           ` Alfred M. Szmidt
2024-01-25 15:50                                             ` Eli Zaretskii
2024-01-25 16:01                                               ` Alfred M. Szmidt
2024-01-25 16:13                                                 ` Eli Zaretskii
2024-01-25 15:40                                           ` Emanuel Berg
2024-01-25 10:30                             ` Eli Zaretskii
2024-01-25 11:27                               ` Emanuel Berg
2024-01-24 12:15         ` Alan Mackenzie
2024-01-24 12:28           ` Emanuel Berg
2024-01-25  9:10           ` Po Lu
2024-01-25 11:56             ` Emanuel Berg
2024-01-25 13:21               ` Po Lu
2024-01-25 13:56                 ` Emanuel Berg
2024-01-25 23:32           ` Stefan Kangas
2024-01-20  3:39   ` Richard Stallman
2024-01-23 18:10 ` Stefan Monnier via Emacs development discussions.
2024-01-24  4:49   ` JD Smith
2024-01-24  9:45     ` Stefan Kangas
2024-01-24 15:29       ` JD Smith
2024-01-24 15:55         ` Stefan Monnier
2024-01-24 16:02           ` Stefan Monnier
2024-01-24 16:20           ` JD Smith
2024-01-24 17:08             ` Stefan Monnier
2024-01-24 16:35           ` [External] : " Drew Adams
2024-01-24 16:30     ` Drew Adams
2024-02-01  8:56       ` Madhu
2024-02-01 22:46         ` Emanuel Berg
2024-01-25  3:16     ` Madhu
2024-01-25 13:57       ` Stefan Monnier
2024-01-25 15:17         ` JD Smith
2024-01-25 15:37           ` JD Smith
2024-01-25 15:44             ` Alfred M. Szmidt
2024-01-25 16:00               ` JD Smith
2024-01-25 16:05               ` Stefan Monnier
2024-01-25 16:12                 ` Alfred M. Szmidt
2024-01-25 16:20                   ` Stefan Monnier
2024-01-25 16:33                   ` JD Smith
2024-01-29  3:19             ` Richard Stallman
2024-01-26  4:30   ` Richard Stallman
2024-01-28  3:06     ` Stefan Monnier via Emacs development discussions.
2024-01-30  3:59       ` Richard Stallman
2024-01-30 13:02         ` Stefan Monnier
2024-02-23  3:04           ` Richard Stallman
2024-01-26  4:30   ` Richard Stallman
2024-01-28  4:16     ` Stefan Monnier via Emacs development discussions. [this message]
2024-01-31  3:32       ` Richard Stallman
2024-01-31 13:20         ` Stefan Monnier
2024-02-03  3:32           ` Richard Stallman
2024-02-03  6:09             ` Stefan Monnier
2024-02-03  6:48               ` Emanuel Berg
2024-02-04  4:46               ` Richard Stallman
2024-02-04 14:04                 ` Stefan Monnier
2024-02-04  4:46               ` Richard Stallman
2024-02-04 13:58                 ` Stefan Monnier
2024-02-13  0:48                 ` Stefan Monnier
2024-02-13  2:27                   ` Stefan Monnier
2024-02-14 11:16                   ` Richard Stallman
2024-02-14 12:45                     ` Stefan Monnier
2024-02-22  3:05                       ` Richard Stallman
2024-02-22  4:08                         ` Stefan Monnier
2024-02-25  3:14                           ` Richard Stallman
2024-02-25 15:03                             ` Stefan Monnier
2024-02-29  3:50                               ` Richard Stallman
2024-02-29 18:07                                 ` Stefan Monnier
2024-02-29  3:50                               ` Richard Stallman
2024-02-13  0:41         ` Stefan Monnier
2024-02-23  3:04           ` Richard Stallman
2024-02-23 13:39             ` Stefan Monnier
2024-02-25  3:16               ` Richard Stallman
2024-02-25 14:57                 ` Alfred M. Szmidt
2024-02-25 15:38                   ` Stefan Monnier
2024-02-25 16:42                     ` Alfred M. Szmidt
2024-02-25 17:13                       ` Stefan Monnier
2024-02-25 17:22                     ` Alan Mackenzie
2024-02-25 17:46                       ` Alfred M. Szmidt
2024-02-25 18:13                         ` Stefan Monnier
2024-02-25 17:10                 ` Stefan Monnier
2024-02-27  3:11                   ` Richard Stallman
2024-01-24 12:39 ` Alan Mackenzie
2024-01-24 14:43   ` Emanuel Berg
2024-01-24 16:25   ` Manuel Giraud via Emacs development discussions.
2024-01-25 14:01   ` Code for cond* - cond*-match, cond*-subpat and backtrack-aliases Alan Mackenzie
2024-01-29  3:19     ` Richard Stallman
2024-01-29 12:16       ` JD Smith
2024-02-01  3:51         ` Richard Stallman
2024-02-01 14:54           ` JD Smith
2024-02-04  4:42             ` Richard Stallman
2024-01-29  3:19     ` Richard Stallman
2024-01-29  8:54       ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv34uignrp.fsf-monnier+emacs@gnu.org \
    --to=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.