unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: "Basil L. Contovounesios" <contovob@tcd.ie>,
	Ag Ibragimov <agzam.ibragimov@gmail.com>,
	emacs-devel@gnu.org
Subject: Re: Pattern matching on match-string groups #elisp #question
Date: Fri, 26 Feb 2021 11:24:48 +0100	[thread overview]
Message-ID: <258C930A-B183-4211-9917-0AD96C17A638@acm.org> (raw)
In-Reply-To: <jwvft1jeahy.fsf-monnier+emacs@gnu.org>

26 feb. 2021 kl. 05.31 skrev Stefan Monnier <monnier@iro.umontreal.ca>:

> Good question.  I'm not sure how best to explain or document it, sadly.
> One of the reasons is that predicates are presumed to be (mostly) pure
> functions, so `pcase` feels free to call them fewer times or more times
> as it pleases.
> 
> But that also largely applies to `app`, so that's not a very
> good explanation.
> 
> Maybe a better explanation is that `pcase-let` optimizes the pattern
> match code under the assumption that the pattern will match, so it skips
> the tests that determine whether the pattern matches or not.
> 
> [ That doesn't mean it skips all the tests: if the pattern is
>  (or `(a ,v) `(b ,_ ,v)) it *will* test to see if the first element is `a` in
>  order to decide what to bind `v` to, but it won't bother to check if
>  the first element is `b` since it presumes that the pattern does match
>  and it knows that there's no further alternative.  ]
> 
> Note that this explanation is not very convincing either because it's
> not clear if the test that it skipped is `(identity VAR)`
> or `(identity (string-match ...))` so it's unclear whether the
> `string-match` is eliminated.

Thank you, I think this is good enough -- I've pushed the fix (with tests, so it matters less whether I've understood it) to master. (If pcase one day gets uppity enough to optimise based on the target expression as well, then a lot of tests will become meaningless.)

A clearer but less efficient pattern would be something like

(app (lambda (s) (and (string-match REGEXP s)
                      (list (match-string 1 s)
                            (match-string 2 s)
                            ...)))
     `(,VAR1 ,VAR2 ...))

which would, unless I'm mistaken, be the only way if string-match returned a match object instead of setting global match data.
Of course a sufficiently optimising compiler would eliminate the consing!

> It's linked to the special undocumented pcase pattern `pcase--dontcare`
> (whose name is not well chosen, suggestions for better names are
> welcome)

pcase--give-up
pcase--!,fail




  reply	other threads:[~2021-02-26 10:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25  5:11 Pattern matching on match-string groups #elisp #question Ag Ibragimov
2021-02-25 14:55 ` Basil L. Contovounesios
2021-02-25 15:32   ` Stefan Monnier
2021-02-25 18:28     ` Mattias Engdegård
2021-02-26  4:31       ` Stefan Monnier
2021-02-26 10:24         ` Mattias Engdegård [this message]
2021-02-26 19:38           ` Stefan Monnier
2021-02-27 10:17             ` Mattias Engdegård
2021-02-27 14:39               ` Stefan Monnier
2021-02-27 18:10                 ` Mattias Engdegård
2021-02-27 20:32                   ` Stefan Monnier
2021-02-28 13:46                     ` Mattias Engdegård
2021-02-28 15:37                       ` Stefan Monnier

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=258C930A-B183-4211-9917-0AD96C17A638@acm.org \
    --to=mattiase@acm.org \
    --cc=agzam.ibragimov@gmail.com \
    --cc=contovob@tcd.ie \
    --cc=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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).