all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: rms@gnu.org
Cc: emacs-devel@gnu.org
Subject: Re: pcase bindings in patterns with complicated logic
Date: Sat, 13 Jan 2024 19:58:07 +0000	[thread overview]
Message-ID: <87il3xt38w.fsf@localhost> (raw)
In-Reply-To: <E1rOV6N-000505-5e@fencepost.gnu.org>

Richard Stallman <rms@gnu.org> writes:

> (pcase '(foo 5)
>   ((or `(,(and (pred symbolp) a1) ,(and (pred symbolp) b1))
>        `(,(and (pred symbolp) a2) ,(and (pred numberp) b2)))
>    `(,a1 ,b1 ,a2 ,b2)))
>
> => (nil nil foo 5)
>
> That surrised me, since outside the pcase, all four variables
> are unbound and referring to them gets errors.
>
> So it seems that all the pattern variables in the curren clause are
> bound somehow if that clause succeeds, but only some of them get
> significant values.
>
> Is that an accurate general statement of how pcase handles binding
> pattern variables?

This looks like a bug.
The manual has a dedicated paragraph explaining the above scenario.
However, on the latest Emacs master, "void variable" errors are not
thrown, in contradiction with what the manual states.

  3. On match, the clause's body forms can reference the set of symbols
     the pattern let-binds.  When SEQPAT is ‘and’, this set is the union
     of all the symbols each of its sub-patterns let-binds.  This makes
     sense because, for ‘and’ to match, all the sub-patterns must match.

     When SEQPAT is ‘or’, things are different: ‘or’ matches at the
     first sub-pattern that matches; the rest of the sub-patterns are
     ignored.  It makes no sense for each sub-pattern to let-bind a
     different set of symbols because the body forms have no way to
     distinguish which sub-pattern matched and choose among the
     different sets.  For example, the following is invalid:

          (require 'cl-lib)
          (pcase (read-number "Enter an integer: ")
            ((or (and (pred cl-evenp)
                      e-num)      ; bind ‘e-num’ to EXPVAL
                 o-num)           ; bind ‘o-num’ to EXPVAL
             (list e-num o-num)))

          Enter an integer: 42
          error→ Symbol’s value as variable is void: o-num
          Enter an integer: 149
          error→ Symbol’s value as variable is void: e-num



-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



  reply	other threads:[~2024-01-13 19:58 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-13  3:54 pcase bindings in patterns with complicated logic Richard Stallman
2024-01-13 19:58 ` Ihor Radchenko [this message]
2024-01-14  6:58   ` Thierry Volpiatto
2024-01-14  7:27     ` Thierry Volpiatto
2024-01-14 15:42     ` Ihor Radchenko
2024-01-14 15:58       ` Thierry Volpiatto
2024-01-14 16:21         ` Ihor Radchenko
2024-01-14 17:54           ` Thierry Volpiatto
2024-01-16  3:32             ` Richard Stallman
2024-01-16 13:18               ` Ihor Radchenko
2024-01-18  3:37                 ` Richard Stallman
2024-01-18 13:13                   ` Ihor Radchenko
2024-01-20  3:39                     ` Richard Stallman
2024-01-20 12:48                       ` Ihor Radchenko
2024-01-21  3:06                         ` Richard Stallman
2024-01-21 14:06                           ` Ihor Radchenko
2024-01-23 13:40                             ` Richard Stallman
2024-01-23 15:08                             ` Stefan Monnier via Emacs development discussions.
2024-01-23 19:59                               ` Stefan Monnier via Emacs development discussions.
2024-01-15  3:13   ` Richard Stallman
2024-01-14  7:03 ` Thierry Volpiatto

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=87il3xt38w.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=emacs-devel@gnu.org \
    --cc=rms@gnu.org \
    /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.