From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Richard Stallman Newsgroups: gmane.emacs.devel Subject: pcase bindings in patterns with complicated logic Date: Fri, 12 Jan 2024 22:54:31 -0500 Message-ID: Reply-To: rms@gnu.org Content-Type: text/plain; charset=Utf-8 Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21705"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jan 13 04:55:01 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 1rOV6q-0005MA-3a for ged-emacs-devel@m.gmane-mx.org; Sat, 13 Jan 2024 04:55:00 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rOV6Q-0002xA-TL; Fri, 12 Jan 2024 22:54:34 -0500 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 1rOV6P-0002wx-FU for emacs-devel@gnu.org; Fri, 12 Jan 2024 22:54:33 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rOV6P-00072k-7U for emacs-devel@gnu.org; Fri, 12 Jan 2024 22:54:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=Date:Subject:To:From:mime-version:in-reply-to: references; bh=usYRXAJNsqZeBuAFqHPoc2VDRbyJSxFlT0fNm1pzmSs=; b=ErU+ShCx+Td+76 Kfx1hlvjSojrgHPKtnkMl4k4FcH4rVMQL4xsBqFnc/4C6fuQ6XCuy4TOi2Sw9yTeOzAii/V0xZjpT vkwTJrpUwuz5mTPSwtMW5GECdn8MFgeJb3jgAz+HvB4SDlB0CWRGL4L9Nz1kam8GaO1EtukXIvDpX /vhrUAjK1pjLlwavKyLt/W9j1mhw16olUOCfvb3KWWHXDBrTMg5fI6oBXZtCQIwPEJPdNnymzfLUW h16lNRg5+NasPElbHg9fo4ZPY8sIC0B8JZyROM+FJLfiu8Wpw7g9TrdE+x4j3nqEMv8KUxwJalG33 XCsEjr74W+NSGPUchzYA==; Original-Received: from rms by fencepost.gnu.org with local (Exim 4.90_1) (envelope-from ) id 1rOV6N-000505-5e; Fri, 12 Jan 2024 22:54:31 -0500 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:314919 Archived-At: [[[ To any NSA and FBI agents reading my email: please consider ]]] [[[ whether defending the US Constitution against all enemies, ]]] [[[ foreign or domestic, requires you to follow Snowden's example. ]]] I'm trying to figure out how pcase behaves in terms of which pattern variables are actually bound, depending on which parts of a case's pattern succeeds in matching. That wasn't clear to me from the docs I read. I tried this example: (setq x1 nil x2 nil y1 nil y2 nil) (pcase '(foo 5) ((or `(,(and (pred symbolp) x1) ,(and (pred symbolp) y1)) `(,(and (pred symbolp) x2) ,(and (pred numberp) y2))) `(,x1 ,y1 ,x2 ,y2))) => (nil nil foo 5) So it seems that unless a match for a variable is included in the branch of the matching tree that actually completes, its variable is not set. But then I tried (pcase '(foo 5) ((or `(,(and (pred symbolp) a1) ,(and (pred symbolp) b1)) `(,(and (pred symbolp) a2) ,(and (pred numberp) b2))) `(,a1 ,b1 ,a2 ,b2))) => (nil nil foo 5) That surrised me, since outside the pcase, all four variables are unbound and referring to them gets errors. So it seems that all the pattern variables in the curren clause are bound somehow if that clause succeeds, but only some of them get significant values. Is that an accurate general statement of how pcase handles binding pattern variables? If so, do people like that behavior? Is it easy to use? Would anyone prefer a different behavior for cases like this? The question is, how should cond* handle this? Just like pcase, or something different? -- Dr Richard Stallman (https://stallman.org) Chief GNUisance of the GNU Project (https://gnu.org) Founder, Free Software Foundation (https://fsf.org) Internet Hall-of-Famer (https://internethalloffame.org)