Skip to content

Operator precedence problem #2711

Closed
Closed
@VMatthijs

Description

@VMatthijs

Summary:

There is an inconsistency between the operator precedence as implemented in stanc2 and as described in the manual.

Description:

The manual describes that .* binds more tightly than *. stanc2 seems to bind * more tightly.

Reproducible Steps:

Try to compile

model {
  print(1.0 .* [1., 2.]');
}

Get error

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

  real .* vector

Available argument signatures for operator.*:

  vector .* vector
  row_vector .* row_vector
  matrix .* matrix

Expression is ill formed.
 error in 'stan/src/test/test-models/good/empty.stan' at line 2, column 25
  -------------------------------------------------
     1: model {
     2:   print(1.0 .* [1., 2.]');
                                ^
     3: }
  -------------------------------------------------

So clearly, the signatures for .* are
vector .* vector
row_vector .* row_vector
matrix .* matrix .

Now, if we try to compile

model {
  print([3., 4.]' * 1.0 .* [1., 2.]');
}

we would expect that to fail. Indeed, it should be parsed as

model {
  print([3., 4.]' * (1.0 .* [1., 2.]'));
}

according to the manual. However, it parses just fine and prints [3, 8] when it runs. This suggests strongly to me that it has parsed the model as

model {
  print(([3., 4.]' * 1.0) .* [1., 2.]');
}

instead.

Additional Information:

Rstanarm is currently exploiting this bug.

This also makes me worried about the other stated operator precedences. Has this been resulting in incorrect generated code?

Current Version:

v2.18.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions