11.2 Extra-Grammatical Constraints

Type constraints

A well-formed Stan program must satisfy the type constraints imposed by functions and distributions. For example, the binomial distribution requires an integer total count parameter and integer variate and when truncated would require integer truncation points. If these constraints are violated, the program will be rejected during parsing with an error message indicating the location of the problem.

Operator precedence and associativity

In the Stan grammar provided in this chapter, the expression 1 + 2 * 3 has two parses. As described in the operator precedence table, Stan disambiguates between the meaning \(1 + (2 \times 3)\) and the meaning \((1 + 2) \times 3\) based on operator precedences and associativities.

Typing of compound declaration and definition

In a compound variable declaration and definition, the type of the right-hand side expression must be assignable to the variable being declared. The assignability constraint restricts compound declarations and definitions to local variables and variables declared in the transformed data, transformed parameters, and generated quantities blocks.

Typing of array expressions

The types of expressions used for elements in array expressions ('{' expressions '}') must all be of the same type or a mixture of int and real types (in which case the result is promoted to be of type real).

Forms of numbers

Integer literals longer than one digit may not start with 0 and real literals cannot consist of only a period or only an exponent.

Conditional arguments

Both the conditional if-then-else statement and while-loop statement require the expression denoting the condition to be a primitive type, integer or real.

For loop containers

The for loop statement requires that we specify in addition to the loop identifier, either a range consisting of two expressions denoting an integer, separated by ‘:’, or a single expression denoting a container. The loop variable will be of type integer in the former case and of the contained type in the latter case. Furthermore, the loop variable must not be in scope (i.e., there is no masking of variables).

Only break and continue in loops

The break and continue statements may only be used within the body of a for-loop or while-loop.

PRNG function locations

Functions ending in _rng may only be called in the transformed data and generated quantities block, and within the bodies of user-defined functions with names ending in _rng.

Probability function naming

A probability function literal must have one of the following suffixes: _lpdf, _lpmf, _lcdf, or _lccdf.

Algebraic solver argument types and origins

The algebra_solver function may be used without control parameters; in this case

  • its first argument refers to a function with signature ( vector, vector, real[], int[]) : vector,

  • the remaining four arguments must be assignable to types vector, vector, real[], int[], respectively and

  • the fourth and fifth arguments must be expressions containing only variables originating from the data or transformed data blocks.

The algebra_solver function may accept three additional arguments, which like the second, fourth, and fifth arguments, must be expressions free of parameter references. The final free arguments must be assignable to types real, real, and int, respectively.

ODE solver argument types and origins

The integrate_ode, integrate_ode_rk45, and integrate_ode_bdf functions may be used without control parameters; in this case

  • its first argument to refer to a function with signature (real, real[], real[], real[], int[]) : real[],

  • the remaining six arguments must assignable to types real[], real, real[], real[], real[], and int[], respectively, and

  • the third, fourth, and sixth arguments must be expressions not containing any variables not originating in the data or transformed data blocks.

The integrate_ode_rk45 and integrate_ode_bdf functions may accept three additional arguments, which like the third, fourth, and sixth arguments, must be expressions free of parameter references. The final three arguments must be assignable to types real, real, and int, respectively.

Indexes

Standalone expressions used as indexes must denote either an integer (int) or an integer array (int[]). Expressions participating in range indexes (e.g., a and b in a : b) must denote integers (int).

A second condition is that there not be more indexes provided than dimensions of the underlying expression (in general) or variable (on the left side of assignments) being indexed. A vector or row vector adds 1 to the array dimension and a matrix adds 2. That is, the type matrix[ , , ], a three-dimensional array of matrices, has five index positions: three for the array, one for the row of the matrix and one for the column.