From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Adam Porter Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] pcase.el: Add type pattern Date: Mon, 09 Mar 2020 16:54:18 -0500 Message-ID: <87h7yxw5x1.fsf@alphapapa.net> References: <874kuxxuez.fsf@alphapapa.net> <87r1y1wcj4.fsf@alphapapa.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="21810"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Mar 09 22:55:03 2020 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 1jBQMY-0005YI-Rz for ged-emacs-devel@m.gmane-mx.org; Mon, 09 Mar 2020 22:55:02 +0100 Original-Received: from localhost ([::1]:50224 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBQMX-0002AP-UK for ged-emacs-devel@m.gmane-mx.org; Mon, 09 Mar 2020 17:55:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46190) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jBQM0-0001aT-Kp for emacs-devel@gnu.org; Mon, 09 Mar 2020 17:54:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jBQLz-0003Xq-4L for emacs-devel@gnu.org; Mon, 09 Mar 2020 17:54:28 -0400 Original-Received: from ciao.gmane.io ([159.69.161.202]:53092) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jBQLy-0003X9-SX for emacs-devel@gnu.org; Mon, 09 Mar 2020 17:54:27 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1jBQLx-0004pS-GR for emacs-devel@gnu.org; Mon, 09 Mar 2020 22:54:25 +0100 X-Injected-Via-Gmane: http://gmane.org/ X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 159.69.161.202 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:245417 Archived-At: --=-=-= Content-Type: text/plain Stefan Monnier writes: >>> `cl-typep` does it but I think it's a mistake. >>> We should not rely on such heuristics when we can "do it right", >>> e.g. with a property along the lines of `cl-deftype-satisfies`. >> Understood, but I'm not sure exactly what you mean, as far as what I >> should do in that regard. I'm not very familiar with cl-lib's >> internals. :) > > The property `cl-deftype-satisfies` is very simple: > > (get 'cl-deftype-satisfies) > > gives you the predicate you need to call. Thanks, I didn't know about that property. > So we could use this property or choose a new property name > (without the `cl-` prefix) which works the same way. Then we need to > add the corresponding list of > > (put ' ' ') > > somewhere (probably in `subr.el`, tho if we keep the > `cl-deftype-satisfies` name, then it would also fit in > `cl-preload.el`). Thanks, now I understand. > But at the same time, I can't think of a good reason why we should use > a different notion of type than CL's, so I guess it does make sense to > use `cl-typep` (makes the overall system simpler). To avoid the > circularity, we can simply move the (pcase-defmacro type ...) outside of > `pcase.el` so it can easily depend on both `pcase` and `cl-lib`. > Another option is to do something like > > (pcase-defmacro type (ty) > (require 'cl-lib) > `(pred (pcase--flip cl-typep ',ty))) > > so `cl-lib` is only required lazily (when we actually expand a `(type > TYPE)` pattern), so the bootstrapping problem shouldn't bite us as long > as neither pcase.el nor cl*.el themselves use such a pattern. Sounds good to me. I found that cl-macs.el defines the pcase pattern for cl-struct, so it seemed like a natural place to put a pattern that uses cl-typep. Please see the new patch attached. I added a co-authored-by pseudo-header; I hope that's appropriate. >> Seems like a good idea to me, although the scope of those changes seem >> much larger than this patch, > > Indeed. But it's not enormous either (it's only an addition to what we > have, so there's no tricky business with backward compatibility and stuff). > >> and I'm not sure I'm the right person for that job. > > Based on the fact that it hasn't been done yet, I think we can assume > that noone is "the right person for that job", but you might still be > the closest there is. I'd be happy to help. If you'd still like me to work on that or the other ideas, please let me know, and I might be able to find time for that. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-emacs-lisp-cl-macs.el-Add-type-pattern.patch Content-Description: Add pcase type pattern to cl-macs.el, etc. >From 38598a6b0e940e44d9c5cee84d9a87184c98d051 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Mon, 9 Mar 2020 13:01:32 -0500 Subject: [PATCH] * lisp/emacs-lisp/cl-macs.el: Add type pattern * lisp/emacs-lisp/cl-macs.el: ((pcase-defmacro type)): Add 'type' pattern. * test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-type): Add test. * lisp/emacs-lisp/pcase.el: (pcase): Update docstring. * doc/lispref/control.texi (pcase Macro): Update manual. Co-authored-by: Stefan Monnier --- doc/lispref/control.texi | 6 ++++++ etc/NEWS | 7 +++++++ lisp/emacs-lisp/cl-macs.el | 6 ++++++ lisp/emacs-lisp/pcase.el | 1 + test/lisp/emacs-lisp/pcase-tests.el | 7 +++++++ 5 files changed, 27 insertions(+) diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index c601e3a..72bfd2e 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi @@ -555,6 +555,12 @@ pcase Macro Likewise, it makes no sense to bind keyword symbols (@pxref{Constant Variables}). +@item (type @var{type}) +Matches if @var{expval} is of type @var{type}. The comparison is done +with function @code{cl-typep}. + +Example: @code{(type integer)} + @item (pred @var{function}) Matches if the predicate @var{function} returns non-@code{nil} when called on @var{expval}. diff --git a/etc/NEWS b/etc/NEWS index 47b87af..9190bf1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -174,6 +174,13 @@ key binding / v package-menu-filter-by-version / / package-menu-filter-clear +** pcase.el + +*** Added Pcase 'type' pattern. + +A pattern like '(type integer)' is equivalent to one like +'(pred integerp)'. + * New Modes and Packages in Emacs 28.1 diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 4c2f589..d2ac485 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -3403,6 +3403,12 @@ cl-struct-slot-value (run-hooks 'cl-macs-load-hook) +;;; Pcase type pattern. + +(pcase-defmacro type (type) + "Pcase pattern that matches objects of TYPE." + `(pred (pcase--flip cl-typep ',type))) + ;; Local variables: ;; generated-autoload-file: "cl-loaddefs.el" ;; End: diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 36b93fa..a9ef779 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el @@ -128,6 +128,7 @@ pcase If a SYMBOL is used twice in the same pattern the second occurrence becomes an `eq'uality test. (pred FUN) matches if FUN called on EXPVAL returns non-nil. + (type TYPE) matches if EXPVAL is of TYPE. (app FUN PAT) matches if FUN called on EXPVAL matches PAT. (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. (let PAT EXPR) matches if EXPR matches PAT. diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el index 0b69bd9..5db8605 100644 --- a/test/lisp/emacs-lisp/pcase-tests.el +++ b/test/lisp/emacs-lisp/pcase-tests.el @@ -71,6 +71,13 @@ pcase-tests-member (ert-deftest pcase-tests-vectors () (should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3))) +(ert-deftest pcase-tests-type () + (should (equal (pcase 1 + ((type integer) 'integer)) + 'integer)) + (should-error (pcase 1 + ((type notatype) 'integer)))) + ;; Local Variables: ;; no-byte-compile: t ;; End: -- 2.7.4 --=-=-=--