bug: deparse sometimes omits parentheses for unary operators

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

bug: deparse sometimes omits parentheses for unary operators

Binder, Martin
When turning a language object into a string (e.g. using dput, deparse,
or just
printing it), R usually inserts parentheses so that the printed string
is an
accurate representation of the underlying AST.

Example (good):
> (expr = substitute(a * 10, list(a = quote(if (TRUE) 1 else 0))))
(if (TRUE) 1 else 0) * 10
> eval(expr)
[1] 10

The representation is incorrect, however, when unary operators around
language
constructs like "if", "for" etc. are involved.

Example (bad):
> (expr = substitute(-a * 10, list(a = quote(if (TRUE) 1 else 0))))
-if (TRUE) 1 else 0 * 10

The parentheses around the "if"-construct are missing. The expected
output
(the true representation of the expression) is, in fact:

-(if (TRUE) 1 else 0) * 10

as can be tested when evaluating it (which shows that this is not a bug
in
"substitute"):

> eval(expr)
[1] -10

The deparsed string on the other hand evaluates to:

> -if (TRUE) 1 else 0 * 10
[1] -1

Even when using deparse with control="all", which comes, per its help
file,
"closest to making 'deparse()' an inverse of 'parse()'", this bug
persists:

> deparse(expr, control="all")
[1] "quote(-if (TRUE) 1 else 0 * 10)"

Only some language constructs appear to be affected. Unary operators
applied to
function calls, other unary operators, or binary operators render as
expected:

> (expr = substitute(-a * 10, list(a = quote(!0))))
-(!0) * 10

I am using the R version 3.4.0 (2017-04-21) platform
x86_64-redhat-linux-gnu
package from Fedora 25.

Regards,

Martin Binder

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

Re: bug: deparse sometimes omits parentheses for unary operators

Radford Neal
> From: "Binder, Martin" <[hidden email]>
> To: [hidden email]
> Subject: [Rd] bug: deparse sometimes omits parentheses for unary
> operators
>
> Example (bad):
> > (expr = substitute(-a * 10, list(a = quote(if (TRUE) 1 else 0))))
> -if (TRUE) 1 else 0 * 10
>
> The parentheses around the "if"-construct are missing. The expected
> output (the true representation of the expression) is, in fact:
>
> -(if (TRUE) 1 else 0) * 10


It would also be OK for the result to be

  (-if (TRUE) 1 else 0) * 10

It's possible that producing this form is more natural for the deparser.

There are a number of other bugs in the deparser, many of which I
fixed in the pqR deparser when doing a major rewrite, though I missed
the one you report above.  You can see them at

    http://www.pqr-project.org/NEWS.txt

Look for BUG FIXES under VERSION RELEASED 2015-09-14.  Some are fixed
in R-3.4.0, but most remain.

   Radford Neal

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Loading...