A variable by itself is a well-formed expression of the same type as
the variable. Variables in Stan consist of ASCII strings containing
only the basic lower-case and upper-case Roman letters, digits, and
the underscore (
_) character. Variables must start with a
A--Z) and may not end with two underscores
Examples of legal variable identifiers are as follows.
a, a3, a_3, Sigma, my_cpp_style_variable, myCamelCaseVariable
Unlike in R and BUGS, variable identifiers in Stan may not contain a period character.
Stan reserves many strings for internal use and these may not be used
as the name of a variable. An attempt to name a variable after an
internal string results in the
stanc translator halting with an
error message indicating which reserved name was used and its location
in the model code.
The name of the model cannot be used as a variable within the model.
This is usually not a problem because the default in
is to append
_model to the name of the file containing the
model specification. For example, if the model is in file
foo.stan, it would not be legal to have a variable named
foo_model when using the default model name through
bin/stanc. With user-specified model names, variables cannot
match the model.
The following list contains reserved words for Stan’s programming language. Not all of these features are implemented in Stan yet, but the tokens are reserved for future use.
for, in, while, repeat, until, if, then, else, true, false, target, struct, typedef, export, auto, extern, var, static, lower, upper, offset, multiplier
Variables should not be named after types, either, and thus may not be any of the following.
int, real, complex, vector, simplex, unit_vector, ordered, positive_ordered, row_vector, matrix, cholesky_factor_corr, cholesky_factor_cov, corr_matrix, cov_matrix, array
The following built in functions are also reserved and cannot be used as variable names:
print, reject, profile, get_lp, increment_log_prob, target
The following block identifiers are reserved and cannot be used as variable names:
functions, model, data, parameters, quantities, transformed, generated
Variable names will also conflict with the names of distributions
_ccdf, such as
normal_lcdf_log. No user-defined variable can take a name
_lupmf even if a corresponding
_lpmf is not
Using any of these variable names causes the
to halt and report the name and location of the variable causing the
Stan primarily generates code in C++, which features its own
reserved words. It is legal to name a variable any of the
following names, however doing so will lead to it being renamed
_stan_public) behind the scenes (in the
generated C++ code).
alignas, alignof, and, and_eq, asm, bitand, bitor, bool, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, decltype, default, delete, do, double, dynamic_cast, enum, explicit, float, friend, goto, inline, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, short, signed, sizeof, static_assert, static_cast, switch, template, this, thread_local, throw, try, typeid, typename, union, unsigned, using, virtual, volatile, wchar_t, xor, xor_eq, fvar, STAN_MAJOR, STAN_MINOR, STAN_PATCH, STAN_MATH_MAJOR, STAN_MATH_MINOR, STAN_MATH_PATCH
The legal characters for variable identifiers are given in the identifier characters table.
Identifier Characters Table. id:identifier-characters-table The alphanumeric characters and underscore in base ASCII are the only legal characters in Stan identifiers.
|characters||ASCII code points|
||97 – 122|
||65 – 90|
||48 – 57|
Although not the most expressive character set, ASCII is the most portable and least prone to corruption through improper character encodings or decodings. Sticking to this range of ASCII makes Stan compatible with Latin-1 or UTF-8 encodings of these characters, which are byte-for-byte identical to ASCII.