## 3.2.4 Machines

A machine describes the dynamic behavior of a model by means of variables whose values are changed by events.

There are two basic things that must be proven for a machine:

1. The machine must be consistent, i.e. it should never reach a state which violates the invariant.

2. The machine is a correct refinement, i.e. its behavior must correspond to any machines that it refines.

### 3.2.4.1 Refinement and Abstract machines

A machine can refine at most one other machine. We refer to the refined machine as the abstract machine and refer to the refinement as the concrete machine. More generally, a machine can be refined by machine , refined by and so on. The most concrete refinement would be .

Basically, a refinement consists of two aspects:

1. The concrete machine’s state is connected to the state of the abstract machine. To do this, an invariant is used to relate abstract and concrete variables. This invariant is called a gluing invariant.

2. Each abstract event can be refined by one or more concrete events.

The full invariant of the machine consists of both abstract and concrete invariants. The invariants are accumulated during refinements.

How to use Refinement: Refinement can be used to subsequently add complexity to the model - this is called superposition refinement (or horizontal refinement). It can also be used to add detail to data structures – this is called data refinement (or vertical refinement). We’ve seen both types of refinement in the tutorial (Chapter 2).

### 3.2.4.2 Seeing a context

If the machine sees a context, the sets and constants declared in the context can be used in all predicates and expressions. The conjunction of axioms can be used as hypotheses in the proofs.

### 3.2.4.3 Variables and invariants

Variables can be declared by adding their unique name (an identifier) to the Variables section. The type of the variables must be inferable by the invariants of the machine. We denote the variables of the abstract machines with a and the variables of the concrete machine with a .

An invariant is a statement that must be valid at each state of the machine. Each invariant consists of a label and a predicate . An invariant can refer to the constants as well as the variables of the concrete and all abstract machines.

We write to denote the conjunction of all invariants of the abstract machines and for the conjunction of the concrete machine’s invariant.

Invariants that are marked as theorems derive their correctness from the preservation of other invariants, so their preservation does not need to be proven. The proof obligation can be found in Section 3.2.6.2.

If an invariant contains a well-definedness condition, a corresponding proof obligation is generated (3.2.5.2).

#### 3.2.4.3.1 Common variables between machines

With some restrictions, the abstract variables and concrete variables can have variables in common. If a variable is declared in a machine , it can be re-used in the direct refinement . In that case, it is assumed that the values of the abstract and concrete variable are always equal. To ensure this, special proof obligations are generated (3.2.4.4). Once a variable disappears in a refinement, i.e. it is not declared in machine , it cannot be re-introduced in a later refinement.

### 3.2.4.4 Events

A possible state change for a machine is defined by an event. The condition under which an event can be executed is given by a guard. The event’s action describes how the new and old state relate to each other.

Events occur atomically (i.e. one event happens at a time) and to a new state. Two events never happen at the same time. Time is also not factored into the execution of the event.

An event has the following elements:

• Name

• Parameters

• Guards

• Witnesses

• Actions

• Status (ordinary, convergent or anticipated): The status is used for termination proofs (see Section 3.2.4.8 for details).

An event can refine one or more events of an abstract machine. To keep things simple, we will first consider events with only one refined event. If there are several refinement steps, we describe events from the refined machines as abstract events and the event from the concrete machine as the concrete event. For example, if an event is refined by and is refined by , we call and the abstract events and the concrete event. Likewise, if we refer to the parameters of the abstract events, we mean all the parameters of all the abstract events (e.g., the parameters of and ).

#### 3.2.4.4.1 Parameters

An event can have an arbitrary number of parameters. Their names must be unique, i.e. there must be no constant or variable with the same name. The types of the parameters must be declared in the guards of the event. We denote the parameters of all abstract events with and the parameters of the concrete event with .

Similarly to variables, an event can have parameters in common with the event it refines. If the refined event has a parameter which is not a parameter of the refinement, a witness for the abstract parameter must be specified (3.2.4.4.4). All free identifiers in must be either constants, concrete or abstract variables, concrete parameters or the abstract parameter .

#### 3.2.4.4.2 Guards

Each guard consist of a label and a predicate . All free identifiers in must be constants, concrete variables or concrete parameters. Variables or parameters of abstract machines are not accessible in a guard.

We write for the conjunction of all guards of all abstract events.

Like axioms and invariants, guards can also be marked as theorems. The proof obligation can be found in Section 3.2.6.3. If the guard contains WD-conditions, a proof obligation is generated. See Section 3.2.5.3 for the proof obligation.

#### 3.2.4.4.3 Actions

An action is composed of a label and an assignment. Section 3.3.8 gives an overview of how they are assigned. They can be put into two groups: deterministic and non-deterministic assignments. Each assignment affects one or more concrete variables.

If an event has more than one action, they are executed in parallel. An error will occur if a new value is assigned to a variable in more than one action. All variables to which no new value is assigned keep the same value in new and old state.

We now define the before-after-predicate of the actions together. Let be the before-after-predicate of the event’s assignments. Let be the variables that are assigned by any action of the event. Let be the variables of the concrete machine that are not modified by any of the event’s actions (i.e. ). Then the before-after-predicate of the concrete event is .

Please note that Rodin optimizes proof obligations when a before-after-predicate is a hypothesis. is replaced directly by when is not changed by the event and replaced by when is assigned to deterministically.

#### 3.2.4.4.4 Witnesses

Witnesses are composed of a label and a predicate that establishes a link between the values of the variables and parameters of the concrete and abstract events. Most of the time, this predicate is a simple equality.

Unlike other elements in Event-B that have a label, the label of a witness has a meaning and cannot be chosen arbitrarily.

If the user does not specify a witness, Rodin uses the default witness .

Witnesses are necessary in the following circumstances:

• The abstract event has a parameter that is not a parameter of the concrete event. In this case, the label of the witness must be , and the witness has the form . All identifiers of must be either constants, concrete or abstract variables, primed concrete variables (i.e.  for each concrete variable ), concrete parameters or the abstract parameter .

• The abstract event assigns non-deterministically (using or ) a value to a variable that is not a variable of the concrete machine. In this case, the label of the witness must be , the witness has the form . All identifiers of must be either constants, concrete or abstract variables, primed concrete variables (i.e.  for each concrete variable ), concrete parameters or the primed abstract variable . denotes the new value of .

We denote the conjunction of all witnesses of an event with .

The feasibility of the witness must be proven, i.e. that there is actually a value for which the predicate holds.

 Witness feasibility for a parameter Name eventlabel//WFIS Goal
 Witness feasibility for an abstract variable Name eventlabel//WFIS Goal

A witness may contain well-definedness conditions. See 3.2.5.4 for the corresponding proof obligation.

#### 3.2.4.4.5 Initialisation

The initialisation of a machine is given by a special event called INITIALISATION. Unlike other events, the initialisation must not contain guards and parameters. The actions must not make use of variable values before the initialisation event occurs. All variables must have a value assigned to them. If there is no assignment for the variable , Rodin assumes a default assignment of the form .

#### 3.2.4.4.6 Ensuring machine consistency

The following proof obligations are generated for events:

The assignment of each action must be well-defined when the event is enabled. See 3.2.5.5 for the corresponding proof obligation.

If the event’s guard is enabled, every action must be feasible. This is trivially true in the case of the deterministic assignments. For a non-deterministic assignment , the feasibility must be proven. The feasibility operator is described in Section 3.3.8.

 Action feasibility Name eventlabel/actionlabel/FIS Goal

For each invariant with the label invlabel that contains a variable affected by the assignment, it must be proven that the invariant still is still valid for the new values.

 Invariant preservation Name eventlabel/invlabel/INV Goal

Rodin simplifies this proof obligations by replacing with for variables that are not changed and by for variables that are assigned by a deterministic () assignment.

There are special proof obligations generated for the initialisation event:

 Action feasibility (in the initialisation) Name INITIALISATION/actionlabel/FIS Goal
 Invariant establishment Name INITIALISATION/invlabel/INV Goal

### 3.2.4.5 Ensuring a correct refinement

An event can refine one or more events of the abstract machine. We first consider the refinement of only one event. For refining more than one event (i.e. merging events), please refer below to Section 3.2.4.6.

If an event does not refine an abstract event, it is implicitly assumed that it refines skip, the event that is always enabled (i.e. its guard is ) and does nothing (i.e. all variables keep their values).

#### 3.2.4.5.1 Guard strengthening

A concrete event must only be enabled if the abstract event is enabled. This condition is called guard strengthening. For each abstract guard with label guardlabel, the following proof obligation is generated:

 Guard strengthening Name eventname/guardlabel/GRD Goal

#### 3.2.4.5.2 Action simulation

If an abstract event’s action (with before-after predicate ) assigns a value to a variable that is also declared in the concrete machine, it must be proven that the abstract event’s behaviour corresponds to the concrete behaviour. The behaviour of the concrete event is given by the concrete before-after-predicate , and the relevant abstract behaviour is given by the before-after-predicate . The relation between abstract and concrete event is specified by witnesses.

 Action simulation Name eventname/actionlabel/SIM Goal

When the assignments are deterministic or the witnesses are equations, the proof obligation can usually be simplified by Rodin.

#### 3.2.4.5.3 Preserved variables

If is a variable of both the abstract and concrete machine and the concrete event assigns a value to but the abstract event does not, it must be proven that the variable’s value does not change. Let be the before-after-predicate of the action that changes .

 Equality of a preserved variable Name eventname//EQL Goal

### 3.2.4.6 Merging events

Refining more than one abstract event by a single event is called merging of events. To merge events, two conditions must be taken into account.

• If two abstract events declare the same parameter, they must be of the same type.

• All abstract events must have identical actions.

Instead of the guard strengthening proof obligation, the following proof obligation is created with as the abstract guards of the merged events and as their parameters.

 Guard strengthening (merge) Name eventlabel/MRG Goal

The other proof obligations are the same as for refining a single event. Also, the same rules for defining witnesses apply.

### 3.2.4.7 Extending events

Instead of refining another event, an event can extend it. In this case the refined event will implicitly have all the parameters, guards and actions of the refined event. It can have additional parameters, guards and actions. The same effect can be achieved by manually copying the parameters, guards and actions.

This is especially useful when additional features are gradually introduced into a model by refinement (also called “superposition refinement”).

### 3.2.4.8 Termination

Event-B makes it possible to prove how an event will terminate. Termination means that a chosen set of events are enabled only a finite number of times before an event that is not marked as terminating occurs. To support proofs for termination, a variant can be specified in a model. All free identifiers in must be constants or concrete variables. A variant is an expression that is either numeric () or a finite set (, where is an arbitrary type).

Events can be marked as:

ordinary

when the event may occur arbitrarily often and does not underlie any restrictions regarding the variant.

convergent

when the event must decrease the variant.

anticipated

when the event must not increase the variant.

Informally, termination is proven by stating that the convergent events strictly decrease the variant which has a lower bound. If a model contains a convergent event, a variant must be specified. If only anticipated events are declared, it is sufficient to create a default constant variant so that all anticipated events do not increase the variant. When an event is marked as anticipated, one must just prove that the event does not increase the variant. The proof of termination is then delayed to the refinements of the anticipated event. A refinement of an anticipated event must be either anticipated or convergent.

If the convergence of an event is proven, the convergence of its refinements is also guaranteed due to guard strengthening.

A variant must be well-defined. The corresponding well-definedness proof obligations can be found in Section 3.2.5.6. The following other proof obligations are generated:

#### 3.2.4.8.1 Numeric variant

If the variant is numeric, an anticipated or convergent event must only be enabled when the variant is non-negative.

 Numeric variant is a natural number Name eventlabel/NAT Goal

A convergent event must decrease the variant

 Decreasing of a numeric variant (convergent event) Name eventlabel/VAR Goal

and an anticipated event must not increase the variant.

 Decreasing of a numeric variant (anticipated event) Name eventlabel/VAR Goal

#### 3.2.4.8.2 Set variant

If the variant is a set t, it must be proven that the set is always finite:

 Decreasing of a variant (anticipated event) Name FIN Goal

A convergent event must remove elements from the set

 Decreasing of a set variant (convergent event) Name eventlabel/VAR Goal

and an anticipated event must not add elements.

 Decreasing of a set variant (anticipated event) Name eventlabel/VAR Goal