We specify an interface for transmitting data between asynchronous devices. A sender and a receiver are connected as
flowchart LR Sender[Sender] -->|val| Receiver[Receiver] Sender -->|rdy| Receiver Receiver -->|ack| Sender
Data is sent on val, and rdy and ack lines are used for synchronization. The sender must wait for an ack for one data item before it can send the next.
In this behaviour, we made the decision that val and rdy should change in a single step. But voltages on wires can’t be changed at precisely the same instant. We ignore this detail here to simplify the implementation.
A specification is an abstraction. The main challenge of writing a specification is to ignore the unimportant details and add in only the important ones. We capture the essence of a system in the spec.
The First Specification
We begin with EXTENDS Naturals because our specification uses subtraction, so we get the - operator from the Naturals module.
What is the set of data that can be sent? We keep this as a parameter of the specification by using:
CONSTANT Data
To specify what values our variables val, rdy, ack can take:
TypeInvariant == (val \in Data) /\ (rdy \in {0, 1}) /\ (ack \in {0, 1})
We call the set {0, 1} the type of rdy, and TypeInvariant a type invariant. Let’s define these terms more precisely:
- A state function is an ordinary expression (with no prime or
[]) that can contain variables and expressions. - A state predicate is a Boolean-valued state function.
- An invariant
Invof a specificationSpecis a state predicate such thatSpec => []Invis a theorem. - A variable
vhas typeTis a specificationSpeciffv \in Tis in invariant ofSpec.
We can write the above formula better by
TypeInvariant == /\ val \in Data
/\ rdy \in {0, 1}
/\ ack \in {0, 1}
NOTE: Indentation is important in the above construct and determines which expressions belong together. Being in the same line means conjuncted together.
The type invariant won’t be a part of the specification. We do not assume that TypeInvariant is an invariant : the specification should imply that it is. In face, its invariance will be asserted as a theorem.
The initial predicate:
Init == /\ val \in Data
/\ rdy \in {0, 1}
/\ ack = rdy
For the Next predicate, the system can take 2 actions every step: Send or Receive. We encode them in two separate predicates Send and Recv. The Next predicate then becomes Send \/ Recv.
We say the action Send is enabled when the system is in a state where Send can be performed.
Send == /\ rdy = ack
/\ rdy' = 1 - rdy
/\ val' \in Data
/\ UNCHANGED ack
UNCHANGED vIt means that the expression
vhas the same value in the old and new states. More precisely,UNCHANGED vequalsv' = v, wherev'is the expression obtained fromvby priming all its variables.
Similarly, we can define Recv
Recv == /\ rdy # ack
/\ ack = 1 - ack
/\ UNCHANGED <<val, rdy>>
Angle brackets << and >> are used in TLA+ to enclose ordered tuples.
Finally, combining everything and allowing for stuttering steps:
Spec == Init /\ [][Next]_<<val, rdy, ack>>
We’ll define THEOREM Spec => []TypeInvariant also in the module AsynchInterface
Another Specification
The first specification is not well suited for helping specify systems that use the interface. We’ll rewrite it in a form that makes it convenient to use it as a part of larger specifications.
The first problem is that it uses three variables to describe a single interface. We’d rather use a single variable chan (channel) instead and have the three vairables be a part of it. TLA+ provides records apart from ordered tuples. The set of records is written as:
[val : Data, rdy : {0, 1}, ack : {0, 1}]
A system that uses the interface would may perform an operation that sends some data value d and perform some other change that depends on value of d. So, instead of having Send that can send any value, we have Send(d) that sends value d.
Next == (\exists d \in Data : Send(d)) \/ Rcv
To define chan' in Send(d), these are all equivalent:
chan' = [val -> d, rdy -> 1 - chan.rdy, ack -> chan.ack]
chan' = [chan EXCEPT !.val = d, !.rdy = 1 - chan.rdy]
chan' = [chan EXCEPT !.val = d, !.rdy = 1 - @]
Here, ! stands for the new record that the EXCEPT forms by modifying chan. In the expression that !.rdy equals, the symbol @ stands for chan.rdy.
Types: A Reminder
TLA+ is an untyped language
When we say a variable has type in specification iff is an invariant of . Thus, has type 1 .. 12 in the specification HC of the hour clock. This does not mean that the variable hr can only take values in 1 .. 12. A state is a random assignment of values to variables and that includes values like .
We’ll see why TLA+ doesn’t have a formal type system later.
Definitions
- The
Sendin theChannelmodule is an operator that takes a single argument. - Symbol means an identifier like
Sendor an operator symbol like+. - The scope of a variable or constant declaration, or a definition, is the part of the module that follows it
- In , there is a declaration of whose scope is the expression . The identifier does not have meaning within the expression .
- A symbol cannot be declared or defined if it already has a meaning.
Comments
- Mulitine comments:
(* This is a comment *) - Rest-of-the-line comment:
\* Everything that follows is a comment
When writing a specification, split it up into modules in a way that you want it to be read. Decide the reading order first and then design modules that way. Have comments in modules in a way that make sense when modules are read in order.