unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* pcase: Semantics of atom QPATS
@ 2018-06-10  0:40 Michael Heerdegen
  2018-06-11 14:09 ` Stefan Monnier
  0 siblings, 1 reply; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-10  0:40 UTC (permalink / raw)
  To: Emacs Development

Hello,

isn't it strange that the pattern '.7 matches .7, but `.7 causes an
error when used?

Or

(pcase '(.7 1)
  ('(.7 1) t))
==> t

but

(pcase '(.7 1)
  (`(.7 ,_) t))
|-- error: `--pcase-macroexpander: Unknown QPAT: 0.7

IMO the analogy to the list of self-quoting atoms (strings, keywords,
integers - I'm all for that) is misguided here - something implicitly
quoted in a backquoted pattern (i.e. something that is not unquoted)
should behave like quoted with `'' (i.e. transformed into an `eq' test),
everything else feels inconsistent.

This corner case actually bit me when I was trying to refactor a piece
of code that included the atom .7 (as you might have guessed) with
el-search.


Thanks,

Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-10  0:40 pcase: Semantics of atom QPATS Michael Heerdegen
@ 2018-06-11 14:09 ` Stefan Monnier
  2018-06-11 14:45   ` Drew Adams
                     ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Stefan Monnier @ 2018-06-11 14:09 UTC (permalink / raw)
  To: emacs-devel

> isn't it strange that the pattern '.7 matches .7, but `.7 causes an
> error when used?

Yes and no: the pattern '<anything> is defined to match using (equal
x '<anything>), no matter what.  In contrast, ` behaves differently
depending on the type of the backquoted object and it only supports
a limited set of types (those documented in its docstring).

So it's normal for the two to behave differently.

> IMO the analogy to the list of self-quoting atoms (strings, keywords,
> integers - I'm all for that) is misguided here - something implicitly
> quoted in a backquoted pattern (i.e. something that is not unquoted)
> should behave like quoted with `'' (i.e. transformed into an `eq' test),
> everything else feels inconsistent.

[ It's an `equal` test we want, not `eq`.  ]

The issue is rather that not all types might want an `equal` test.
E.g. we could accept patterns like

    `#s(hash-table (data ,key1 ,val1))

Which is why I prefer signaling an error: it lets us extend the
semantics of ` without any fear of breaking backward compatibility.

But yes: it makes sense to extend pcase's ` pattern to accept floats and
match them with `equal` since it's easy and there doesn't seem to be
many other semantics to choose anyway.



        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: pcase: Semantics of atom QPATS
  2018-06-11 14:09 ` Stefan Monnier
@ 2018-06-11 14:45   ` Drew Adams
  2018-06-11 15:24     ` Stefan Monnier
  2018-06-11 20:53   ` Michael Heerdegen
  2018-06-13  3:41   ` Michael Heerdegen
  2 siblings, 1 reply; 14+ messages in thread
From: Drew Adams @ 2018-06-11 14:45 UTC (permalink / raw)
  To: Stefan Monnier, emacs-devel

> Yes and no: the pattern '<anything> is defined to match using (equal
> x '<anything>), no matter what.  In contrast, ` behaves differently
> depending on the type of the backquoted object and it only supports
> a limited set of types (those documented in its docstring).
> 
> So it's normal for the two to behave differently.

Can the behavior being discussed please be documented, if it
is not already?  Thx.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-11 14:45   ` Drew Adams
@ 2018-06-11 15:24     ` Stefan Monnier
  0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2018-06-11 15:24 UTC (permalink / raw)
  To: Drew Adams; +Cc: emacs-devel

> Can the behavior being discussed please be documented, if it
> is not already?  Thx.

It is.


        Stefan



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-11 14:09 ` Stefan Monnier
  2018-06-11 14:45   ` Drew Adams
@ 2018-06-11 20:53   ` Michael Heerdegen
  2018-06-13  3:41   ` Michael Heerdegen
  2 siblings, 0 replies; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-11 20:53 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> [ It's an `equal` test we want, not `eq`.  ]

Of course - that was a typo.

> The issue is rather that not all types might want an `equal` test.
> E.g. we could accept patterns like
>
>     `#s(hash-table (data ,key1 ,val1))
>
> Which is why I prefer signaling an error: it lets us extend the
> semantics of ` without any fear of breaking backward compatibility.

Ah, makes sense.


Thanks,

Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-11 14:09 ` Stefan Monnier
  2018-06-11 14:45   ` Drew Adams
  2018-06-11 20:53   ` Michael Heerdegen
@ 2018-06-13  3:41   ` Michael Heerdegen
  2018-06-13 12:52     ` Stefan Monnier
  2 siblings, 1 reply; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-13  3:41 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Which is why I prefer signaling an error: it lets us extend the
> semantics of ` without any fear of breaking backward compatibility.

May I add a comment like this to clarify?

 lisp/emacs-lisp/pcase.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index d3e8899706..32c36ab2cd 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -981,6 +981,8 @@ pcase--u1
           (app car ,(list '\` (car qpat)))
           (app cdr ,(list '\` (cdr qpat)))))
    ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
+   ;; In all other cases just raise an error so we can't break
+   ;; backward compatibility in the future:
    (t (error "Unknown QPAT: %S" qpat))))
 
 (provide 'pcase)
-- 


Michael.



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-13  3:41   ` Michael Heerdegen
@ 2018-06-13 12:52     ` Stefan Monnier
  2018-06-16  4:03       ` Michael Heerdegen
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2018-06-13 12:52 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

>> Which is why I prefer signaling an error: it lets us extend the
>> semantics of ` without any fear of breaking backward compatibility.
> May I add a comment like this to clarify?

Sure.  Feel free to add support for floats while you're there.


        Stefan


>  lisp/emacs-lisp/pcase.el | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
> index d3e8899706..32c36ab2cd 100644
> --- a/lisp/emacs-lisp/pcase.el
> +++ b/lisp/emacs-lisp/pcase.el
> @@ -981,6 +981,8 @@ pcase--u1
>            (app car ,(list '\` (car qpat)))
>            (app cdr ,(list '\` (cdr qpat)))))
>     ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
> +   ;; In all other cases just raise an error so we can't break
> +   ;; backward compatibility in the future:
>     (t (error "Unknown QPAT: %S" qpat))))
>  
>  (provide 'pcase)



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-13 12:52     ` Stefan Monnier
@ 2018-06-16  4:03       ` Michael Heerdegen
  2018-06-16  6:57         ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-16  4:03 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 154 bytes --]

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

> Sure.  Feel free to add support for floats while you're there.

Ok.  Is this patch ok for emacs-26?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-floats-as-pcase-QPATS.patch --]
[-- Type: text/x-diff, Size: 2366 bytes --]

From f3088c97472e5edc84303dcf4ee4130e0eae17fa Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
Date: Wed, 13 Jun 2018 04:37:38 +0200
Subject: [PATCH] Allow floats as 'pcase' QPATS

* lisp/emacs-lisp/pcase.el (\`): Extend semantics of QPATS to all
numbers.  Add a comment explaining why we disallow some atoms as
QPATS.
* doc/lispref/control.texi (Backquote Patterns): Update the paragraph
explaining QPATS.  Remove a sentence suggesting an analogy between
QPATS to self-quoting objects.
---
 doc/lispref/control.texi | 4 +---
 lisp/emacs-lisp/pcase.el | 7 +++++--
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 34f5f57044..975ab3d075 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -1090,12 +1090,10 @@ Backquote Patterns
 
 @item @var{symbol}
 @itemx @var{keyword}
-@itemx @var{integer}
+@itemx @var{number}
 @itemx @var{string}
 Matches if the corresponding element of @var{expval} is
 @code{equal} to the specified literal object.
-Note that, aside from @var{symbol}, this is the same set of
-self-quoting literal objects that are acceptable as a core pattern.
 
 @item ,@var{pattern}
 Matches if the corresponding element of @var{expval}
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index fa7b1de8b4..4a69244d26 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -919,7 +919,7 @@ pcase--u1
   ,PAT                  matches if the `pcase' pattern PAT matches.
   SYMBOL                matches if EXPVAL is `equal' to SYMBOL.
   KEYWORD               likewise for KEYWORD.
-  INTEGER               likewise for INTEGER.
+  NUMBER                likewise for NUMBER.
   STRING                likewise for STRING.
 
 The list or vector QPAT is a template.  The predicate formed
@@ -949,7 +949,10 @@ pcase--u1
     `(and (pred consp)
           (app car ,(list '\` (car qpat)))
           (app cdr ,(list '\` (cdr qpat)))))
-   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
+   ((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
+   ;; compounded values that are not `consp'
    (t (error "Unknown QPAT: %S" qpat))))
 
 (provide 'pcase)
-- 
2.17.1


[-- Attachment #3: Type: text/plain, Size: 12 bytes --]




Michael.

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  4:03       ` Michael Heerdegen
@ 2018-06-16  6:57         ` Eli Zaretskii
  2018-06-16  7:32           ` Michael Heerdegen
  2018-06-16 12:57           ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Eli Zaretskii @ 2018-06-16  6:57 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: monnier, emacs-devel

> From: Michael Heerdegen <michael_heerdegen@web.de>
> Date: Sat, 16 Jun 2018 06:03:16 +0200
> Cc: emacs-devel@gnu.org
> 
> Stefan Monnier <monnier@IRO.UMontreal.CA> writes:
> 
> > Sure.  Feel free to add support for floats while you're there.
> 
> Ok.  Is this patch ok for emacs-26?

Changes in documentation and comments are always okay there, but I'm
uneasy with changes that modify behavior.  Why is it important to
support non-integer numbers in Emacs 26.2?

> -   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
> +   ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)

Any reasons to use numberp and not number-or-marker-p?



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  6:57         ` Eli Zaretskii
@ 2018-06-16  7:32           ` Michael Heerdegen
  2018-06-16  7:49             ` Eli Zaretskii
  2018-06-16 13:42             ` Noam Postavsky
  2018-06-16 12:57           ` Stefan Monnier
  1 sibling, 2 replies; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-16  7:32 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: monnier, emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> > Ok.  Is this patch ok for emacs-26?
>
> Changes in documentation and comments are always okay there, but I'm
> uneasy with changes that modify behavior.  Why is it important to
> support non-integer numbers in Emacs 26.2?

It's not important, no I thought it would be harmless, but master is ok
for me.

> > -   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
> > +   ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)
>
> Any reasons to use numberp and not number-or-marker-p?

Is it likely that someone constructs a pcase pattern with a literal
marker in it?  Note this is about the pattern, not about the matched
value.


Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  7:32           ` Michael Heerdegen
@ 2018-06-16  7:49             ` Eli Zaretskii
  2018-06-17 13:35               ` Michael Heerdegen
  2018-06-16 13:42             ` Noam Postavsky
  1 sibling, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2018-06-16  7:49 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: monnier, emacs-devel

> From: Michael Heerdegen <michael_heerdegen@web.de>
> Cc: monnier@IRO.UMontreal.CA,  emacs-devel@gnu.org
> Date: Sat, 16 Jun 2018 09:32:10 +0200
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > > Ok.  Is this patch ok for emacs-26?
> >
> > Changes in documentation and comments are always okay there, but I'm
> > uneasy with changes that modify behavior.  Why is it important to
> > support non-integer numbers in Emacs 26.2?
> 
> It's not important, no I thought it would be harmless, but master is ok
> for me.

Then let's do this on master, please.

> > > -   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
> > > +   ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)
> >
> > Any reasons to use numberp and not number-or-marker-p?
> 
> Is it likely that someone constructs a pcase pattern with a literal
> marker in it?

I have no idea.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  6:57         ` Eli Zaretskii
  2018-06-16  7:32           ` Michael Heerdegen
@ 2018-06-16 12:57           ` Stefan Monnier
  1 sibling, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2018-06-16 12:57 UTC (permalink / raw)
  To: emacs-devel

>> > Sure.  Feel free to add support for floats while you're there.
>> Ok.  Is this patch ok for emacs-26?

I don't see any reason to push it to emacs-26, it's not a bug-fix.

>> -   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
>> +   ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)
> Any reasons to use numberp and not number-or-marker-p?

Yes, we want to accept floats but not markers.


        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  7:32           ` Michael Heerdegen
  2018-06-16  7:49             ` Eli Zaretskii
@ 2018-06-16 13:42             ` Noam Postavsky
  1 sibling, 0 replies; 14+ messages in thread
From: Noam Postavsky @ 2018-06-16 13:42 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Eli Zaretskii, Stefan Monnier, Emacs developers

On 16 June 2018 at 03:32, Michael Heerdegen <michael_heerdegen@web.de> wrote:

>> > -   ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
>> > +   ((or (stringp qpat) (numberp qpat) (symbolp qpat)) `',qpat)
>>
>> Any reasons to use numberp and not number-or-marker-p?
>
> Is it likely that someone constructs a pcase pattern with a literal
> marker in it?  Note this is about the pattern, not about the matched
> value.

It would be pretty difficult to do that since markers don't have a
read syntax. It's possible using a macro which expands to a pcase
form, if you can handle the nested backquotes...



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase: Semantics of atom QPATS
  2018-06-16  7:49             ` Eli Zaretskii
@ 2018-06-17 13:35               ` Michael Heerdegen
  0 siblings, 0 replies; 14+ messages in thread
From: Michael Heerdegen @ 2018-06-17 13:35 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: monnier, emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> Then let's do this on master, please.

Ok, done.


Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-06-17 13:35 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-10  0:40 pcase: Semantics of atom QPATS Michael Heerdegen
2018-06-11 14:09 ` Stefan Monnier
2018-06-11 14:45   ` Drew Adams
2018-06-11 15:24     ` Stefan Monnier
2018-06-11 20:53   ` Michael Heerdegen
2018-06-13  3:41   ` Michael Heerdegen
2018-06-13 12:52     ` Stefan Monnier
2018-06-16  4:03       ` Michael Heerdegen
2018-06-16  6:57         ` Eli Zaretskii
2018-06-16  7:32           ` Michael Heerdegen
2018-06-16  7:49             ` Eli Zaretskii
2018-06-17 13:35               ` Michael Heerdegen
2018-06-16 13:42             ` Noam Postavsky
2018-06-16 12:57           ` Stefan Monnier

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).