Expression#
An expression is a typed formal statement that evaluates to a value. Expressions are present in two cases:
In formal Requirement to express a property that must hold
In Attribute definition to express the domain of the attribute
An expression composes several attributes in order to formulate assertions that constrain the used attributes.
Attention
An expression is not an algorithm. There are no instructions or sequences of actions — only properties that must hold.
Formalism#
req uses several formalisms in conjunction to produce expressions:
Name |
Examples |
|---|---|
Propositional logic |
|
LTL (discrete-time) |
|
FOL |
|
Set theory |
|
Common algebra |
|
Note
The current set of formalisms is a deliberate subset of what a specification may require. req will progressively be extended with additional constructs: the guiding criterion is that any new construct must be expressible in standard formal notation and relevant for systems engineers.
In req, every attribute is treated as a signal that may change over time — similar to a measured variable in a control system or a recorded parameter in a flight data recorder. Formally, the attributes used in expressions are considered time-dependent functions.
Suppose an attribute is defined by a requirement as:
where \(f\) denotes an expression. It is assumed that \(x\), \(y\), and \(z\) are functions of time, i.e.:
As a corollary, \(x\) is constant if and only if \(y\) and \(z\) are constants (such as literals).
In the following, we will use the notation \(x(t)\) and \(x_t\) interchangeably.
Note
Unless explicitly stated by temporal operators, attribute constraints hold for all \(t\) — that is, across the entire system lifecycle and all operating conditions. This scope can be narrowed by introducing attributes that represent specific operating conditions or lifecycle phases.
Type system#
The type of an expression defines which operators can be applied to it. An attribute has a type that is inferred from its domain definition.
Operators in req are strictly typed: applying an operator to an expression of an incompatible type is not allowed.
If a type cannot be inferred, the expression has the type undefined, which is not a true type but denotes the absence of typing information.
Note
The only types a specification can define are enumerations, with a type entity.
Number#
The type Number covers all numerical values, including integers and real numbers.
No distinction is made between them in the type system.
Boolean#
The type Boolean is applied to any boolean expression.
Set#
The type Set(T) is applied to any expression that evaluates to a set.
Sets are homogeneous in the sense that they can only contain elements of the same type.
Enumeration#
An enumeration type is defined by a type entity. Its values are the
members of the type, written with the path of the type: Door_state::closed. They can be compared
with = and <> and gathered in sets.
Literals#
A literal is a value written directly in the expression source.
Numbers#
Numbers are written in decimal format and may use exponential notation.
2
3.14
10e7
infinity represents an infinite value. Values are of type Number.
Note
No distinction is made between integers and real numbers at the type level.
Fractional expressions such as 1/3 are evaluated as arithmetic operations, not
written as decimal literals.
Boolean#
The Boolean literals true and false represent the two truth values.
true
false
Values are of type Boolean.
Set#
The set literal allows creating an ad-hoc set with a finite number of elements:
{ EXPRESSION (, EXPRESSION)* }
{1, 2, 3}
Values are of type Set. Expressions must have the same type.
Interval#
An interval is the set of the numbers between two bounds. A bracket that faces its bound includes it, a bracket turned away from it excludes it:
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
Closed interval |
\(\{x \mid a \leq x \leq b\}\) |
|
Open interval |
\(\{x \mid a < x < b\}\) |
|
Includes |
\(\{x \mid a \leq x < b\}\) |
|
Excludes |
\(\{x \mid a < x \leq b\}\) |
Values are of type Set(Number).
]0, 30]
[0, infinity[
Range#
A range is the set of the integers between two bounds, both included:
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
The integers from |
\(\{n \in \mathbb{Z} \mid a \leq n \leq b\}\) |
Values are of type Set(Number).
Built-in set#
A built-in set is a keyword that denotes a well-known set:
real
boolean
integer
Note
Built-in sets are most commonly used as the domain of an attribute.
Values are of type Set. Element type varies according to the built-in set.
Operators#
An operator is an expression that contains one or more operands (attributes or literals). Each operator is paired with a formal equivalent that translates the req expression into standard mathematical notation.
Note
Throughout this section, lowercase x and y denote scalar values, and uppercase X and Y denote sets (domains).
Grouping#
Any sub-expression may be enclosed in parentheses to make evaluation order explicit:
( EXPRESSION )
Parentheses always override the default precedence and associativity rules described in the Precedence section below.
Arithmetic#
Arithmetic operators act on Number and evaluate to Number.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
Addition |
\(x + y\) |
|
Subtraction |
\(x - y\) |
|
Multiplication |
\(x \cdot y\) |
|
Division |
\(x / y\) |
|
Remainder |
\(x \bmod y\) |
|
Exponentiation |
\(x^{y}\) |
|
Unary negation |
\(-x\) |
|
Unary identity |
\(+x\) |
|
Factorial |
\(x!\) |
Equality#
Equality operators act on every type and evaluate to Boolean. In req, equality is always by value, the concept of reference does not exist.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
True if x and y are equal |
\(x = y\) |
|
True if x and y are not equal |
\(x \neq y\) |
|
True if x holds the value of y immediately after the assertion must hold. |
\(\forall P P \Rightarrow x := y_t\) where t is the instant where P becomes true. |
|
True if x initially holds the value of y. |
\(x_0 = y_0\) |
Attention
In req, = is an equality test, not an assignment. The expression
altitude = 120 in a requirement body means “altitude shall be equal to 120”, not “set the altitude to 120”.
The := operator is named fix and notably allows to assert that an assignment has occurred.
It can therefore be used to specify recursive properties such as x := x + 1.
If the fix is not used with a condition, the initial value of y is taken.
Comparison#
Comparison operators act on Number and evaluate to Boolean.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
True if x is strictly less than y |
\(x < y\) |
|
True if x is strictly greater than y |
\(x > y\) |
|
True if x is less than or equal to y |
\(x \leq y\) |
|
True if x is greater than or equal to y |
\(x \geq y\) |
Propositional logic#
Propositional logic operators act on Boolean and evaluate to Boolean.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
True if both x and y are true |
\(x \wedge y\) |
|
True if at least one of x or y is true |
\(x \vee y\) |
|
True if exactly one of x or y is true |
\(x \oplus y\) |
|
True if x is false or x and y are true |
\(x \Rightarrow y\) |
|
True if x and y have the same truth value |
\(x \Leftrightarrow y\) |
|
True if x is false |
\(\neg x\) |
Set#
The following operators act on Set and evaluate to Set.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
Elements belonging to X or Y |
\(X \cup Y\) |
|
Elements belonging to both X and Y |
\(X \cap Y\) |
|
Symmetric difference of X and Y |
\(X \Delta Y = (X \cup Y) \setminus (X \cap Y)\) |
|
Elements of X that are not in Y |
\(X \setminus Y\) |
The following operators act on Set and evaluate to Boolean.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
True if x is an element of X |
\(x \in X\) |
|
True if Y is a subset of X |
\(Y \subseteq X\) |
Tolerance#
The tolerance operator acts on Number and evaluates to Set(Number): the
numbers that are within a tolerance of a nominal value.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
The numbers between x - y and x + y, both included |
\([x - y, x + y]\) |
Shaft::speed in 3000 tol 50
Temporal logic#
Temporal logic operators act on Boolean and evaluate to Boolean. They express properties over a discrete sequence of time steps. See LTL for the underlying formalism.
Syntax |
Description |
Formal equivalence |
|---|---|---|
|
True if x holds at every future time step |
\(\square x\) |
|
True if x holds at some future time step |
\(\lozenge x\) |
|
True if x held at the immediately preceding time step |
\(\ominus x\) |
|
True at the first time step where x becomes true |
\(\neg x_{t-1} \wedge x_{t}\) |
|
True at the first time step where x becomes false |
\(x_{t-1} \wedge \neg x_{t}\) |
|
True if x has been true at every step since the last step where y was true |
\(x \,\mathcal{S}\, y\) |
Note
The notion of time step does not refer to a particular time division or sampling rate. Time steps can be thought of as an arbitrary division that splits continuous time into event points at which state changes occur (i.e., the values of the attributes change).
Aggregator#
An aggregator reduces a list of values, or the values of an expression over a domain, to a single value:
AGGREGATOR EXPRESSION (, EXPRESSION)* end
AGGREGATOR EXPRESSION for IDENTIFIER in EXPRESSION (such that EXPRESSION)? end
Aggregator |
Description |
|---|---|
|
True if every value, at least one value, is true |
|
True if exactly one value is true |
|
The greatest, the smallest value |
|
The least upper bound, the greatest lower bound |
|
The sum, the product of the values |
|
The values of the variable where the greatest, the smallest value is reached |
|
The union, the intersection of the sets |
|
One of the values |
Over a domain, all and any are the quantifiers of first-order logic:
sum Battery::cell_1, Battery::cell_2 end
max p for p in { 8, 14, 20 } such that p <= 15 end
all Battery::cell_1 >= 3, Battery::cell_2 >= 3 end
Note
Boolean aggregators are the list counterparts of and and or: all e1, e2 end is
equivalent to e1 and e2, and any e1, e2 end is equivalent to e1 or e2.
Function call#
A function call applies an expression to a list of arguments:
EXPRESSION ( (EXPRESSION (, EXPRESSION)*)? )
Attention
Function calls are recognised by the parser but are not yet type-checked or evaluated by the compiler. This construct is reserved for future use.
Conditional#
A conditional is a notation for expressing a requirement that depends on one or more conditions. It is a direct equivalent of a propositional pattern and does not add expressive power — only readability.
When#
The when conditional is an expression governed by a set of unordered conditions:
when
EXPRESSION then EXPRESSION
(, EXPRESSION then EXPRESSION)*
(, otherwise EXPRESSION)?
end
when
Doors::open then Motion::speed = 0,
Weather::bad then Motion::speed < 10,
otherwise Motion::speed < 25
end
Let \(c_i\) denote the conditions, \(x_i\) the consequences for each condition and \(y\) the fallback. The when expression is strictly equivalent to:
If the otherwise branch is not defined, \(y\) is taken equal to true by convention, which simplifies the expression:
The conditions are not ordered: every case whose condition holds applies. A single case with an otherwise
branch reads as an if-then-else: when c then x, otherwise y end.
All expressions must be of type Boolean.
Note
This construct is particularly useful when multiple conditions govern a requirement simultaneously — for instance, when specifying an event-action mechanism such as a state machine.
Comprehension#
A comprehension builds a set from the elements of another set, its domain:
{ EXPRESSION for IDENTIFIER in EXPRESSION (such that EXPRESSION)? }
By convention, in this section, \(x\) denotes the identifier, \(X\) the domain (expression after the in),
\(P(x)\) the filter (expression after the such that) and \(f(x)\) the expression before for.
The comprehension is strictly equivalent to:
When the such that clause is omitted, \(P(x)\) is taken equal to \(\text{true}\).
package Wifi
part Radio
let channel in [[ 1, 13 ]]
part
requirement Regional_band top
on Radio
is
@@
The [[Radio]] shall only use channels that do not overlap.
@@
requirement
requirement Non_overlapping_channels bottom
on Radio::channel
refines Regional_band
is
Radio::channel in { 5 * k + 1 for k in [[ 0, 2 ]] }
requirement
package
Precedence#
The table below lists all operators from lowest to highest precedence. Operators on the same row have equal precedence. When in doubt, use parentheses to make evaluation order explicit.
Operators |
Associativity |
|---|---|
|
Left |
|
Left |
|
Left |
|
— |
|
— |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Right |
Unary |
— |
|
— |
|
Left |