From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?Q?Mattias_Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.devel Subject: Re: Pattern matching on match-string groups #elisp #question Date: Fri, 26 Feb 2021 11:24:48 +0100 Message-ID: <258C930A-B183-4211-9917-0AD96C17A638@acm.org> References: <87v9agxkld.fsf@tcd.ie> <80CE2366-76F4-4548-B956-F16DFCE23E4C@acm.org> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25527"; mail-complaints-to="usenet@ciao.gmane.io" Cc: "Basil L. Contovounesios" , Ag Ibragimov , emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 26 11:26:30 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lFaKM-0006ZB-9q for ged-emacs-devel@m.gmane-mx.org; Fri, 26 Feb 2021 11:26:30 +0100 Original-Received: from localhost ([::1]:53920 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFaKL-000223-Co for ged-emacs-devel@m.gmane-mx.org; Fri, 26 Feb 2021 05:26:29 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50362) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFaIx-0001SH-9R for emacs-devel@gnu.org; Fri, 26 Feb 2021 05:25:03 -0500 Original-Received: from mail33c50.megamailservers.eu ([91.136.10.43]:32874) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFaIq-0004jB-4r for emacs-devel@gnu.org; Fri, 26 Feb 2021 05:24:58 -0500 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1614335091; bh=ioqrW0bo+IqN/yIQ7bqybyA6ZURRtlQgX+ByKELk4vE=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=XZyUoOGhn8NTDsbVZCS6/CB/nOEFOggb/6WYH21JgnweAe1xkhyMn5Xcz+w5WCLwI q/opwHX82RMubi6cKFkk/mkHpzA28ICKFWdqWF3byBMEghAEBVFUWzkrBQVF/Pf5Pp jWbFWoFy25vSqTkWAtM0MuTs66hGg9Cq8yofm2VA= Feedback-ID: mattiase@acm.or Original-Received: from stanniol.lan (c-b952e353.032-75-73746f71.bbcust.telenor.se [83.227.82.185]) (authenticated bits=0) by mail33c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 11QAOm6R017360; Fri, 26 Feb 2021 10:24:50 +0000 In-Reply-To: X-Mailer: Apple Mail (2.3445.104.17) X-CTCH-RefID: str=0001.0A742F1F.6038CC73.0032, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=euahMbhX c=1 sm=1 tr=0 a=von4qPfY+hyqc0zmWf0tYQ==:117 a=von4qPfY+hyqc0zmWf0tYQ==:17 a=kj9zAlcOel0A:10 a=M51BFTxLslgA:10 a=iRZporoAAAAA:8 a=4VkTLNmM1AFXgxwC958A:9 a=CjuIK1q_8ugA:10 a=NOBgFS-JBQ2l-kSd6-zu:22 X-Origin-Country: SE Received-SPF: softfail client-ip=91.136.10.43; envelope-from=mattiase@acm.org; helo=mail33c50.megamailservers.eu X-Spam_score_int: -11 X-Spam_score: -1.2 X-Spam_bar: - X-Spam_report: (-1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:265674 Archived-At: 26 feb. 2021 kl. 05.31 skrev Stefan Monnier : > 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. >=20 > But that also largely applies to `app`, so that's not a very > good explanation. >=20 > 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. >=20 > [ 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. ] >=20 > 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