From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Pattern matching on match-string groups #elisp #question Date: Fri, 26 Feb 2021 14:38:51 -0500 Message-ID: References: <87v9agxkld.fsf@tcd.ie> <80CE2366-76F4-4548-B956-F16DFCE23E4C@acm.org> <258C930A-B183-4211-9917-0AD96C17A638@acm.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25285"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: "Basil L. Contovounesios" , Ag Ibragimov , emacs-devel@gnu.org To: Mattias =?windows-1252?Q?Engdeg=E5rd?= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 26 20:39:54 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 1lFixt-0006V7-RI for ged-emacs-devel@m.gmane-mx.org; Fri, 26 Feb 2021 20:39:53 +0100 Original-Received: from localhost ([::1]:59106 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFixs-0002lS-Q9 for ged-emacs-devel@m.gmane-mx.org; Fri, 26 Feb 2021 14:39:52 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41024) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFix0-0002Kb-Pv for emacs-devel@gnu.org; Fri, 26 Feb 2021 14:38:58 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:48491) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFiwy-0006HQ-7U for emacs-devel@gnu.org; Fri, 26 Feb 2021 14:38:57 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 5E1654419EF; Fri, 26 Feb 2021 14:38:54 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 8ABEA4419ED; Fri, 26 Feb 2021 14:38:52 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1614368332; bh=xM07OXRQgNUnmQuZfhZc8CSa6iY+DEQkg4qqmaFnXYc=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=OAr1JCYc4sLl8aY3ZaGwl3l12XduqNcZCExFo0/TRR1RGmTuO6lmQAEZnujw1jKaV 1fKwzflOjJ8Hbf8TIwkhFo2qku7TcPfoP3g44LRRLV/f/cIXefeiIxWezNWa8s761k 2SSJYTvMkoMoViLwKWaZ02ly4NxFsmDYF3ZLlwHnd48mLueaccHOMMganmI7GXEt3t 3PUUr/s9xpN+WerstN8jrWLExC1S+LeWR+q4ylcSGDq703QjtNARJ8e6fYBsSY5M6M TtDJt768ZKXnm+IKBCMWjogHL2UDEv50wOwr4FFYNDrCDvyHR7hMAeRdHehiU6eU9O 4HUwNsgvcL/lw== Original-Received: from alfajor (unknown [216.154.41.47]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 985F91202CA; Fri, 26 Feb 2021 14:38:52 -0500 (EST) In-Reply-To: <258C930A-B183-4211-9917-0AD96C17A638@acm.org> ("Mattias =?windows-1252?Q?Engdeg=E5rd=22's?= message of "Fri, 26 Feb 2021 11:24:48 +0100") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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:265691 Archived-At: > 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.) BTW, I was thinking about making the optimization more conservative, so it only throws away the actual `if` but keeps the computation of the test: diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index c7288b7fa2..0036c1882d 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -469,8 +469,8 @@ pcase--small-branch-p ;; the depth of the generated tree. (defun pcase--if (test then else) (cond - ((eq else :pcase--dontcare) then) - ((eq then :pcase--dontcare) (debug) else) ;Can/should this ever happen? + ((eq else :pcase--dontcare) `(progn ,test ,then)) + ((eq then :pcase--dontcare) `(progn ,test ,else)) (t (macroexp-if test then else)))) ;; Note about MATCH: and it does fix the `pcase-let` problem with your original code. The problem, OTOH is that we now end up with warnings like In toplevel form: progmodes/elisp-mode.el:896:38: Warning: value returned from (memq (type-of l) cl-struct-xref-elisp-location-tags) is unused and these are rather hard/inconvenient to fix (short of not using `pcase-let`). [ Of course, another problem is that we generate marginally less efficient code in some cases, but that should be very minor and that's more a failure of the byte-compiler than a problem in `pcase`. ] This said, there is a fundamental problem with both the previous and the new code: (pcase STR ((and (rx (group-n 1 (* "a"))) (guard (not (looking-at "^")))) nil) ((rx (let FOO (* "a"))) FOO)) It should macroexpand to something morally equivalent to: (cond ((not (stringp STR)) nil) ((not (string-match "\\(?1:a*\\)" STR)) nil) ((looking-at "^"") (let* ((x1464 (match-string 1 STR))) (let ((FOO x1464)) FOO)))) at which point you'll presumably notice that the match data is likely garbled when we try to use it. [ Cue old message about how side effects are bad for your health. ] > 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 ...)) That's indeed what I did in my code (tho using a vector instead of a list), probably because the above risk did occur to me back then. > Of course a sufficiently optimising compiler would eliminate the consing! Indeed, and it's not a difficult optimization (at least if you can presume that this data is immutable). >> 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 Hmm... probably not much more explanatory than "dontcare". > pcase--!,fail That begs the question of what does it mean for the overall pcase to "fail". I was thinking of `pcase--impossible` as well. Stefan