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: Sat, 27 Feb 2021 09:39:27 -0500 Message-ID: References: <87v9agxkld.fsf@tcd.ie> <80CE2366-76F4-4548-B956-F16DFCE23E4C@acm.org> <258C930A-B183-4211-9917-0AD96C17A638@acm.org> <288FFC66-E3BE-4E5F-AAD5-309A632F8058@acm.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7923"; 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 Sat Feb 27 15:40:25 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 1lG0lT-0001fz-Jo for ged-emacs-devel@m.gmane-mx.org; Sat, 27 Feb 2021 15:40:15 +0100 Original-Received: from localhost ([::1]:60978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lG0lS-0004Jg-K9 for ged-emacs-devel@m.gmane-mx.org; Sat, 27 Feb 2021 09:40:14 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56908) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lG0ku-0003tU-AW for emacs-devel@gnu.org; Sat, 27 Feb 2021 09:39:40 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:53091) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lG0kr-00030B-Oc for emacs-devel@gnu.org; Sat, 27 Feb 2021 09:39:39 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id EE45E441AFC; Sat, 27 Feb 2021 09:39:35 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 18843441AF7; Sat, 27 Feb 2021 09:39:34 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1614436774; bh=+o5zUrYk5zihcsMdCpE1q4+Xk9QP5AatDxLEQlwXq9o=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=kPM3YoG9MStx53g2eG7LtfRII9RB4jP7s9N/Z4Z16ZDXQ1p+TqVO0EsBORyaD9NWA Ky4W4zQO177u9QOlv+8lT9sQgE4vKRLFSCHmzXN99oySRV3vgUcHlsS+TzLqhGNUdl Uba35WbccUQ4n6LZ0xw+rJ8PvNjN35PqO9+wCf3XVrEgUCYgApBKwIIwbb1fSq/T5W pzTpVFx5OGkvkfZ1TSnXRHm+Szgp4w/HyS2V65G15kvnrFVbFgbK0C7AtTtNYes3wz 70YjWBdzbGCWpO0j+PG4rmNnBBHXJw+H4bQK2kx4+iOp+dX9t3bthFl1rTDSHq8K5o xyTe5nRnxNP7w== Original-Received: from alfajor (unknown [216.154.41.47]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 1A04612041A; Sat, 27 Feb 2021 09:39:34 -0500 (EST) In-Reply-To: <288FFC66-E3BE-4E5F-AAD5-309A632F8058@acm.org> ("Mattias =?windows-1252?Q?Engdeg=E5rd=22's?= message of "Sat, 27 Feb 2021 11:17:55 +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:265724 Archived-At: >> BTW, I was thinking about making the optimization more conservative, so >> it only throws away the actual `if` but keeps the computation of the tes= t: > [...] >> and it does fix the `pcase-let` problem with your original code. > Given the trouble I think we can defend not respecting side-effects in > something as functional as pcase! Nevertheless, I went ahead with this change (after remembering that wrapping the code in `ignore` should eliminate the extra warnings). >> It should macroexpand to something morally equivalent to: >>=20 >> (cond ((not (stringp STR)) nil) >> ((not (string-match "\\(?1:a*\\)" STR)) nil) >> ((looking-at "^"") >> (let* ((x1464 (match-string 1 STR))) >> (let ((FOO x1464)) FOO)))) > > Oh dear... perhaps we should just go with the intermediate list (or vecto= r) > and suffer the small allocation penalty? (At least we should treat the ca= se > of a single variable specially, since no consing would then be necessary.) It's clearly The Right Thing=E2=84=A2. > My guess is that a vector may be faster than a list if there are more tha= n N elements, for some N. I'll let you benchmark it to determine the N. > Should we use string-match-p when there are no variables bound in the rx = clause? We could. Tho, IIRC currently `string-match-p` is ever so slightly slower than `string-match` and since we clobber the match data in other cases, we might as well clobber the match data in this case as well: any code which presumes the match data isn't affected by some other code which uses regular expressions is quite confused. >>> Of course a sufficiently optimising compiler would eliminate the consin= g! >> Indeed, and it's not a difficult optimization (at least if you can >> presume that this data is immutable). > Right, although we would need some more serious data-flow infrastructure > first. It would be useful for pattern-matching two or more values at the > same time. I don't think it's much more complicated than your current constant folding: when you see a let-binding of a variable to a *constructor*, stash that expression in your context as a "partially known constant" and then do the constant folding when you see a matching *destructor*. The problems I see are: - you need to detect side effects between the constructor and the destructor. You could just consider any *read* of a variable holding such partial-constant as a (potential) side-effect (except for those reads recognized as part of destructors, of course). It should be good enough for the case under discussion. - more importantly in order not to duplicate code and its side-effects (and suffer risks linked to moving code into a different scope), you need to convert your constructor so all its arguments are trivial and "scope safe" (e.g. gensym'd variables, integer constants, symbols, ...). >>>> It's linked to the special undocumented pcase pattern `pcase--dontcare` >>>> (whose name is not well chosen, suggestions for better names are >>>> welcome) >>>=20 >>> pcase--give-up >>=20 >> Hmm... probably not much more explanatory than "dontcare". > > Well, 'dontcare' suggests that anything would do and the value not being > used, like '_', but that's quite misleading. Right, that's part of the problem with this naming: it doesn't want to mean that anything matches, like `_`, but that any behavior is acceptable when pcase has to try and match against this pattern. The other part is that it's not true: we wouldn't settle for "any" behavior, it still has to be sane-ish. >> I was thinking of `pcase--impossible` as well. > Yes, that looks acceptable. In any case, it isn't really a user-facing > symbol, is it? Otherwise we'd need crystal-clear semantics (and lose the > double dashes). I would settle for something a bit less than "crystal-clear", but yes to drop the "--" we'd need a clear enough semantics. Here's another idea for its name: `pcase--go-back` since it makes pcase go back to the last option it tried and accept it even though it failed to match. It still sucks, but maybe it'll give someone else a better idea? Stefan