The definition of the area constant, in the example above, uses an expression.
We now define more precisely what types of expression are supported by PRISM.
Expressions can contain literal values (12, 3.141592, true, false, etc.),
identifiers (corresponding to variables, constants, etc.) and operators from the following list:
*, / (multiplication, division)
+, - (addition, subtraction)
<, <=, >=, > , =, != (relational operators)
! (not)
& (and)
| (or)
? (condition evaluation: condition ? a : b means "if condition is true then a else b")
All of these operators except ? are left associative
(i.e. they are evaluated from left to right).
The precedence of the operators is as found in the list above,
most strongly binding operators first.
Operators on the same line (e.g. + and -) are of equal precedence.
The notation for expressions is hence essentially equivalent to that of C/C++ or Java.
One notable exception to this is that the division operator / always performs floating point, not integer, division,
i.e. the result of 22/7 is 3.142857... not 3.
All expressions must evaluate correctly in terms of type (integer, double or Boolean).
Ranges
A shorthand notation for describing ranges is also supported. For example:
is equivalent to:
All the elements of such an expression (x, 1, 5, etc.) can themselves be expressions.
Replacing the = with an != is equivalent to negating the whole expression. So, for example:
is equivalent to:
Built-in Functions
Expressions can make use of several built-in functions, which are accessed via the func keyword.
Currently, the supported functions are:
min and max, which select the minimum and maximum value, respectively, of two or more numbers;
floor and ceil, which round down and up, respectively, to the nearest integer;
pow which raises one number to the power of another;
and mod for integer modulo operations.
The format is:
Examples of their usage are:
For compatability with older versions of PRISM, min, max,floor and ceil can still be used directly, e.g. max(a,b,c).
Use of Expressions
Expressions can be used in a wide range of places in a PRISM language description, e.g.:
This allows, for example, the probability in a command to be dependent on the current state: