stan-dev/stan — pull request #1048 by bob-carpenter (2014-09-27 22:25:54+00:00)
Title: Bugfix/issue 593 op short circuit

#### Summary:

Enables short-circuit evaluation of boolean conjunction (`&&`) and disjunction (`||`) and thus fully resolves issue #593.

#### Intended Effect:

To have models compiled with these boolean operators short circuit in the same way as C++. Boolean conjunction and disjunction are the only two short-circuiting operators in C++ (aside from the ternary conditional operator, which Stan doesn't have).

The fix involved writing a template function `primitive_value()` that returns a primitive value as is or returns the primitive value inside a `var` or a potenntially nested `fvar` autodiff variable.

#### How to Verify:

There are unit tests for the new `primitive_value()` function with all standard invocations. There is also a test for the changes to generator for both operators. The existing signature tests will make sure everything compiles.

If you don't trust C++ to work as advertised and want to make sure it works, you can try this model:

```
transformed data {
real sigma;
real mu[1];
sigma <- 1;
if (sigma < 10 || mu[3] > 5) // succeeds fast
mu[1] <- 2;
if (sigma > 10 && mu[3] > 5) // fails fast
mu[1] <- 2;
}
parameters {
real y;
}
model {
real z;
z <- 1;
if (z < 10 || mu[3] > 5) // succeeds fast
y ~ normal(mu[1], sigma);
if (z > 10 && mu[3] > 5) // fails fast
y ~ normal(mu[3], -3);
}
```

which samples y from a normal(1,1) density. It tests both the double and var-based versions.

#### Side Effects:

This changes the behavior of the two binary logical operators to the way they have been documented to bheave since Stan 1.0.

#### Documentation:

Removed the warning in the 2.4 documentation that short-circuiting was broken but would be fixed in the future.

#### Reviewer Suggestions:

Anyone.