From: Adam Porter <adam@alphapapa.net>
To: emacs-devel@gnu.org
Subject: Re: [PATCH] pcase.el: Add type pattern
Date: Mon, 09 Mar 2020 16:54:18 -0500 [thread overview]
Message-ID: <87h7yxw5x1.fsf@alphapapa.net> (raw)
In-Reply-To: jwv1rq146l3.fsf-monnier+emacs@gnu.org
[-- Attachment #1: Type: text/plain, Size: 2566 bytes --]
Stefan Monnier <monnier@iro.umontreal.ca> 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 <TYPE> '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 '<TYPE> '<PROP> '<PRED>)
>
> 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.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Add pcase type pattern to cl-macs.el, etc. --]
[-- Type: text/x-diff, Size: 3482 bytes --]
From 38598a6b0e940e44d9c5cee84d9a87184c98d051 Mon Sep 17 00:00:00 2001
From: Adam Porter <adam@alphapapa.net>
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 <monnier@iro.umontreal.ca>
---
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)'.
+
\f
* 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
next prev parent reply other threads:[~2020-03-09 21:54 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-09 18:19 [PATCH] pcase.el: Add type pattern Adam Porter
2020-03-09 18:38 ` Eli Zaretskii
2020-03-09 19:20 ` Adam Porter
2020-03-09 19:35 ` Adam Porter
2020-03-09 18:38 ` Adam Porter
2020-03-09 18:45 ` Eric Abrahamsen
2020-03-10 15:48 ` Adam Porter
2020-03-10 17:40 ` Eric Abrahamsen
2020-03-09 18:53 ` Stefan Monnier
2020-03-09 19:31 ` Adam Porter
2020-03-09 21:00 ` Stefan Monnier
2020-03-09 21:54 ` Adam Porter [this message]
2020-03-09 22:22 ` Michael Heerdegen
2020-03-09 22:46 ` Adam Porter
2020-03-10 14:54 ` Michael Heerdegen
2020-03-10 15:01 ` Michael Heerdegen
2020-03-10 15:46 ` Adam Porter
2020-03-10 16:24 ` Michael Heerdegen
2020-03-10 17:03 ` Adam Porter
2020-03-10 17:43 ` Michael Heerdegen
2020-03-10 18:04 ` Adam Porter
2020-03-10 18:17 ` Adam Porter
2020-03-10 19:07 ` Michael Heerdegen
2020-03-10 19:19 ` Adam Porter
2020-03-10 19:10 ` Eric Abrahamsen
2020-03-10 19:21 ` Adam Porter
2020-03-11 2:08 ` Michael Heerdegen
2020-03-16 12:59 ` Stefan Monnier
2021-07-11 1:33 ` [PATCH] pcase.el: Add cl-type and type patterns Adam Porter
2021-07-11 2:12 ` Adam Porter
2021-07-11 6:11 ` Eli Zaretskii
2021-07-16 9:32 ` Adam Porter
2021-07-17 11:58 ` Eli Zaretskii
2021-07-16 22:41 ` Stefan Monnier
2021-07-17 16:07 ` Adam Porter
2021-07-30 21:24 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87h7yxw5x1.fsf@alphapapa.net \
--to=adam@alphapapa.net \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).