?Syntax wrong about `?`'s precedence ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

?Syntax wrong about `?`'s precedence ?

Ant F
Dear all,

`?Syntax` documents that `?` has the lowest precedence, right under `=`.

Indeed it reads:

*The following unary and binary operators are defined. They are listed in
precedence groups, from highest to lowest.  *

and ends the list with

*<- <<-* *assignment (right to left)*
*=* *assignment (right to left)*
*?* *help (unary and binary)*
I believe it to be wrong, `=` has lower precedence than `?`.

See the following example :

    `?` <- `+`
    x = 2 ? 3
    x
    #> [1] 5

We see that `2 ? 3` is evaluated first, then the result is assigned to x,
showing
higher precedence for `?`.

Compare it to the similar code using `<-` :

    x <- 2 ? 3
    #> [1] 5
    x
    #> [1] 2

Here first `x <- 2` is evaluated, then its output is added to 3, and the
result
`5` is printed. and we verify that `x` is still `2`. Showing lower
precedence
for `?` consistent with the doc.

Hadley Wickham's package `lobstr` makes it easy to compare the parse trees:

    lobstr::ast({x = 2 ? 3})
    #> o-`{`
    #> \-o-`=`
    #>   +-x
    #>   \-o-`?`
    #>     +-2
    #>     \-3

    lobstr::ast({x <- 2 ? 3})
    #> o-`{`
    #> \-o-`?`
    #>   +-o-`<-`
    #>   | +-x
    #>   | \-2
    #>   \-3

Best regards,

Antoine

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ?Syntax wrong about `?`'s precedence ?

S Ellison-2
> From: R-devel [mailto:[hidden email]] On Behalf Of Ant F
> Sent: 29 August 2019 12:06
> To: [hidden email]
> Subject: [Rd] ?Syntax wrong about `?`'s precedence ?
> ...
> See the following example :
>
>     `?` <- `+`

I'm curious; What did you expect to happen if you replace the function '?' with the operator '+' ?
? is surely now being evaluated as a user-defined function and not as an operator.
Would you expect the results of doing that to be the same as evaluation without replacement?

S Ellison




*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ?Syntax wrong about `?`'s precedence ?

R devel mailing list
Precedence is a property of the parser and has nothing to do with the
semantics assigned to various symbols.  Using just core R functions you can
see the precedence of '?' is between those of '=' and '<-'.

> # '=' has lower precedence than '?'
> str(as.list(parse(text="a ? b = c")[[1]]))
List of 3
 $ : symbol =
 $ : language `?`(a, b)
 $ : symbol c
> str(as.list(parse(text="a = b ? c")[[1]]))
List of 3
 $ : symbol =
 $ : symbol a
 $ : language `?`(b, c)
> # '<-' has higher precedence than '?'
> str(as.list(parse(text="a ? b <- c")[[1]]))
List of 3
 $ : symbol ?
 $ : symbol a
 $ : language b <- c
> str(as.list(parse(text="a <- b ? c")[[1]]))
List of 3
 $ : symbol ?
 $ : language a <- b
 $ : symbol c

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Aug 30, 2019 at 4:41 AM Stephen Ellison <[hidden email]>
wrote:

> > From: R-devel [mailto:[hidden email]] On Behalf Of Ant F
> > Sent: 29 August 2019 12:06
> > To: [hidden email]
> > Subject: [Rd] ?Syntax wrong about `?`'s precedence ?
> > ...
> > See the following example :
> >
> >     `?` <- `+`
>
> I'm curious; What did you expect to happen if you replace the function '?'
> with the operator '+' ?
> ? is surely now being evaluated as a user-defined function and not as an
> operator.
> Would you expect the results of doing that to be the same as evaluation
> without replacement?
>
> S Ellison
>
>
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:10}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ?Syntax wrong about `?`'s precedence ?

Kevin Ushey
See also: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16710

On Fri, Aug 30, 2019 at 9:02 AM William Dunlap via R-devel
<[hidden email]> wrote:

>
> Precedence is a property of the parser and has nothing to do with the
> semantics assigned to various symbols.  Using just core R functions you can
> see the precedence of '?' is between those of '=' and '<-'.
>
> > # '=' has lower precedence than '?'
> > str(as.list(parse(text="a ? b = c")[[1]]))
> List of 3
>  $ : symbol =
>  $ : language `?`(a, b)
>  $ : symbol c
> > str(as.list(parse(text="a = b ? c")[[1]]))
> List of 3
>  $ : symbol =
>  $ : symbol a
>  $ : language `?`(b, c)
> > # '<-' has higher precedence than '?'
> > str(as.list(parse(text="a ? b <- c")[[1]]))
> List of 3
>  $ : symbol ?
>  $ : symbol a
>  $ : language b <- c
> > str(as.list(parse(text="a <- b ? c")[[1]]))
> List of 3
>  $ : symbol ?
>  $ : language a <- b
>  $ : symbol c
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Fri, Aug 30, 2019 at 4:41 AM Stephen Ellison <[hidden email]>
> wrote:
>
> > > From: R-devel [mailto:[hidden email]] On Behalf Of Ant F
> > > Sent: 29 August 2019 12:06
> > > To: [hidden email]
> > > Subject: [Rd] ?Syntax wrong about `?`'s precedence ?
> > > ...
> > > See the following example :
> > >
> > >     `?` <- `+`
> >
> > I'm curious; What did you expect to happen if you replace the function '?'
> > with the operator '+' ?
> > ? is surely now being evaluated as a user-defined function and not as an
> > operator.
> > Would you expect the results of doing that to be the same as evaluation
> > without replacement?
> >
> > S Ellison
> >
> >
> >
> >
> > *******************************************************************
> > This email and any attachments are confidential. Any u...{{dropped:10}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ?Syntax wrong about `?`'s precedence ?

Peter Dalgaard-2
...and 14955, which seems to have the explanation (but was marked as closed/fixed??). The parser does list '?' as lower precedence than '=', but '='-assignments are not normal 'expr's which can appear as arguments to '?'. (Presumably because of named arguments: f(a=b) differs from f(a<-b).)  

Other tokens which have lower precedence than assignments are flow-control items, IF ELSE WHILE FOR REPEAT, but I don't see any way to confuse them in the same way as '?'.

It might be possible to resolve the situation by specifying '?' syntax explicitly as
expr_or_assign '?' expr_or_assign, but, well, "There be Tygers here"...

-pd


> On 30 Aug 2019, at 18:32 , Kevin Ushey <[hidden email]> wrote:
>
> See also: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16710
>
> On Fri, Aug 30, 2019 at 9:02 AM William Dunlap via R-devel
> <[hidden email]> wrote:
>>
>> Precedence is a property of the parser and has nothing to do with the
>> semantics assigned to various symbols.  Using just core R functions you can
>> see the precedence of '?' is between those of '=' and '<-'.
>>
>>> # '=' has lower precedence than '?'
>>> str(as.list(parse(text="a ? b = c")[[1]]))
>> List of 3
>> $ : symbol =
>> $ : language `?`(a, b)
>> $ : symbol c
>>> str(as.list(parse(text="a = b ? c")[[1]]))
>> List of 3
>> $ : symbol =
>> $ : symbol a
>> $ : language `?`(b, c)
>>> # '<-' has higher precedence than '?'
>>> str(as.list(parse(text="a ? b <- c")[[1]]))
>> List of 3
>> $ : symbol ?
>> $ : symbol a
>> $ : language b <- c
>>> str(as.list(parse(text="a <- b ? c")[[1]]))
>> List of 3
>> $ : symbol ?
>> $ : language a <- b
>> $ : symbol c
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>>
>> On Fri, Aug 30, 2019 at 4:41 AM Stephen Ellison <[hidden email]>
>> wrote:
>>
>>>> From: R-devel [mailto:[hidden email]] On Behalf Of Ant F
>>>> Sent: 29 August 2019 12:06
>>>> To: [hidden email]
>>>> Subject: [Rd] ?Syntax wrong about `?`'s precedence ?
>>>> ...
>>>> See the following example :
>>>>
>>>>    `?` <- `+`
>>>
>>> I'm curious; What did you expect to happen if you replace the function '?'
>>> with the operator '+' ?
>>> ? is surely now being evaluated as a user-defined function and not as an
>>> operator.
>>> Would you expect the results of doing that to be the same as evaluation
>>> without replacement?
>>>
>>> S Ellison
>>>
>>>
>>>
>>>
>>> *******************************************************************
>>> This email and any attachments are confidential. Any u...{{dropped:10}}
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ?Syntax wrong about `?`'s precedence ?

S Ellison-2
In reply to this post by R devel mailing list
> From: William Dunlap [mailto:[hidden email]]
> Precedence is a property of the parser and has nothing to do with the
> semantics assigned to various symbols.  

Yes, but output depends on evaluation as well as precedence.  Seeing different things wasn't an immediate surprise.

Your own parse() examples are much clearer; thanks for that.

S


*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify [hidden email]
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel