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: pcase generates an unprintable expansion for a form in test erc--restore-initialize-priors Date: Mon, 27 May 2024 14:23:55 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="704"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: emacs-devel@gnu.org To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon May 27 20:24:44 2024 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 1sBf1X-000AW9-KA for ged-emacs-devel@m.gmane-mx.org; Mon, 27 May 2024 20:24:43 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBf0y-0002vV-AD; Mon, 27 May 2024 14:24:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBf0s-0002td-VM for emacs-devel@gnu.org; Mon, 27 May 2024 14:24:02 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBf0p-000801-TW for emacs-devel@gnu.org; Mon, 27 May 2024 14:24:01 -0400 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 0FEA04428D7; Mon, 27 May 2024 14:23:58 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1716834236; bh=7BYb2FHO9UhwntXWSs9MIkh2/q8MBSKDCT7whm4XHtI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=K63HRmqgB6Ooja/HPPy3C3ddwSf3OmCssX9VC833nofw/oIQa9PNN1NKHUuiNsVrT FvPxNCxpzWCAWhrFfZTDV4MMK2l7RxeYBP9w52Dq3j1IpCJZBq283yqfps4/Sf9H5E UVzEnRluGY03xZ75m+6TieYQ2oFHwTV1ZmOkLLotAyjnAkEvE1vS1Q91eI8GyGHbAp tRx0FAtRJ2AaD9C28aOUaDm/vaHuzXufCMsDJbZNQDJzyv+bPklANnP+07Sw48cqWY XvDvX5oeASvDg39dkvj8detdsYfxGoQW7oS9hecNG1BkOwYrtRjq6mKXCIe1/AHgP2 g83zen4sba3AA== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 93DCF4428D0; Mon, 27 May 2024 14:23:56 -0400 (EDT) Original-Received: from alfajor (unknown [23.233.149.155]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 750021202C7; Mon, 27 May 2024 14:23:56 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Mon, 27 May 2024 13:39:47 -0400") 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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:319626 Archived-At: --=-=-= Content-Type: text/plain > I suspect it can be done with no change to the core pcase code by > rewriting > > (pcase-defmacro \` (qpat) > > so it does the recursion on its own (e.g. instead of expanding `(A . B) > to a pattern which contains `A and `B). Then at any level if it notices > that no `,` was found during the recursion it can just use `equal`. The patch below does that. For that ERC test, the resulting code is still very heavily nested, of course, but ... less so. Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=pcase.patch diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 1a58c60734a..bbc08493ec9 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -1124,21 +1124,35 @@ \` - True! (The second element can be anything, and for the sake of the body forms, its value is bound to the symbol `forum'.)" (declare (debug (pcase-QPAT))) + (pcase--expand-\` qpat)) + +(defun pcase--expand-\` (qpat) (cond ((eq (car-safe qpat) '\,) (cadr qpat)) ((eq (car-safe qpat) '\,@) (error "Unsupported QPAT: %S" qpat)) ((vectorp qpat) - `(and (pred vectorp) - (app length ,(length qpat)) - ,@(let ((upats nil)) - (dotimes (i (length qpat)) - (push `(app (aref _ ,i) ,(list '\` (aref qpat i))) - upats)) - (nreverse upats)))) + (let* ((trivial t) + (upats (mapcar (lambda (qpat) + (let ((upat (pcase--expand-\` qpat))) + (unless (eq (car-safe upat) 'quote) + (setq trivial nil)) + upat)) + qpat))) + (if trivial + `',qpat + `(and (pred vectorp) + (app length ,(length qpat)) + ,@(let ((i -1)) + (mapcar (lambda (upat) `(app (aref _ ,(setq i (+ i 1))) ,upat)) + upats)))))) ((consp qpat) - `(and (pred consp) - (app car-safe ,(list '\` (car qpat))) - (app cdr-safe ,(list '\` (cdr qpat))))) + (let ((upata (pcase--expand-\` (car qpat))) + (upatd (pcase--expand-\` (cdr qpat)))) + (if (and (eq (car-safe upata) 'quote) (eq (car-safe upatd) 'quote)) + `',qpat + `(and (pred consp) + (app car-safe ,upata) + (app cdr-safe ,upatd))))) ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat) ;; In all other cases just raise an error so we can't break ;; backward compatibility when adding \` support for other --=-=-=--