On Sun, 7 Nov 2021 09:02:36 +0530
Deepayan Sarkar <deepayan.sar...@gmail.com> wrote:

> This sounds like a difference in precedence. The expression
> 
> if (log) 1 else dnorm(x, mean, sd) / sd
> 
> is apparently being interpreted differently as
> 
> d1: (if (log) 1 else dnorm(x, mean, sd)) / sd
> d2: if (log) 1 else (dnorm(x, mean, sd)) / sd)
> 
> It's unclear how environments could affect this, so it would be very
> helpful to have a reproducible example.

This seems to be caused by the deparser producing the same source text
for different expressions:

( x <- expression(`/`(`*`(a, if (b) c else d), e)) )
# expression(a * if (b) c else d/e)
( y <- expression(a * if (b) c else d/e) )
# expression(a * if (b) c else d/e)
all.equal(x, y)
# [1] TRUE

The expressions *seem* to be the same, but:

as.list(x[[1]])
# [[1]]
# `/`
# 
# [[2]]
# a * if (b) c else d
# 
# [[3]]
# e

as.list(y[[1]])
# [[1]]
# `*`
# 
# [[2]]
# a
# 
# [[3]]
# if (b) c else d/e

Perhaps it could be possible to make the deparser output extra
parentheses at the cost of slightly uglier output in cases when they
are not needed. all.equal.language uses deparse(), so it will behave
correctly when the deparse() output is fixed.

In the original example, as.list(body(d1)) and as.list(body(d2)) should
show different results, too.

-- 
Best regards,
Ivan

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to