Our next example is a FIFO buffer. It’s a device in which a sender process transmits a sequence of values to a receiver. They use two channels in and out to communicate with the buffer:

flowchart LR
	Sender -->|in| Buffer -->|out| Receiver

The values are sent in and out using the asynchronous protocol specified by the Channel module we made in last chapter (the second one). Send and Recv steps on both the in channel and the out channel.

The Inner Specification

The specification of the FIFO first ENTENDS the modules Naturals and Sequences.

The Sequences module defines operations on finite sequences. We represent a finite sequence as a tuple, e.g. <<3, 2, 1>>. The Sequences module defines the following operators on sequences:

  • Seq(S): The set of all sequences of elements of the Set S. E.g. <<1, 2, 3>> is a member of Seq(Nat)
  • Head(s): The first element of sequence s
  • Tail(s): The tail of sequence s, which consists of s with its head removed
  • Append(s, e): The sequence obtained by appending element e to the tail of the sequence s. E.g. Append(<<3, 7>>, 3) equals <<3, 7, 3>>
  • s \o t: Sequence obtained by concatenating sequences s and t. (We typeset it as )
  • Len(s): Length of sequence s.

Spec begins with declaring the constant Message, which represents the set of all messages that can be sent. Declares variables in and out (channels), and a third q that represents queue of buffered messages. q is the messages that are sent by the sender but not yet received by the receiver.

InChan == INSTANCE Channel WITH Data <- Mesage, chan <- in
OutChan == INSTANCE Channel WITH Data <- Message, chan <- out

This defines InChan!chan to be in, thus the type invariant becomes in \in [val : Message, rdy : {0,1}, ack : {0,1}].

Init == /\ InChan!Init
		/\ OutChan!Init
		/\ q = <>          \* the empty sequence

TypeInvariant == /\ InChan!TypeInvariant
				 /\ OutChan!TypeInvariant
				 /\ q \in Seq(Message)

Hiding the Queue

The way to hide internal variables in the specification is to use the operator. In the previous spec, the queue q is an internal variable and we want to hide it from Spec. Since q is already defined in the module InnerFIFO, we define the module FIFO:

-----------------MODULE FIFO------------------
CONSTANT Message
VARIABLES in, out
Inner(q) == INSTANCE InnerFIFO
Spec == \exists q : Inner(q)!Spec
==============================================

A Bounded FIFO

We defined an unbounded FIFO above, the only change to make it bounded would be to add Len(q) < N in the BufRecv definition. While we can copy the definition of InnerFIFO and make a BoundedInnerFIFO to make it work, we can instead reuse the code.

Let’s see the module’s definition first:

--------------------------MODULE BoundedFIFO--------------------------
EXTENDS Naturals, Sequences
VARIABLES in, out
CONSTANT Message, N
ASSUME (N \in Nat) /\ (N > 0)
Inner(q) == INSTANCE InnerFIFO
BNext(q) == /\ Inner(q)!Next
			/\ Inner(q)!BufRecv => (Len(q) < N)
Spec == \exists q : Inner(q)!Init /\ [][BNext(q)]_<<in, out, q>>
======================================================================

Note that BufRecv => (Len(q) < N) is same as disabling receive step when the sequence is full!

ASSUME

The module also contains the statement

ASSUME (N \in Nat) /\ (N > 0)

Such an assumption has no effect on any definitions made in the module. However, it may be taken as a hypothesis when proving any theorems asserted in the module.

An ASSUME statement should only be used to make assumptions about constants. Why don’t we assert that Message is a set? Every value in TLA+ is a set! That includes the number 3. If we wanted to assert that Message is a finite set, we should assume ASSUME.

What We’re Specifying

Our specification describes a system consisting of the FIFO buffer and its environment. The behaviours satisying formula Spec of module FIFO represent those histories of the universe in which both the system and its environment behave correctly.

We can segregate these next-state actions of system and environment by

Next == SysNext \/ EnvNext

For the FIFO, we have

SysNext == BufRecv \/ BufSend
EnvNext == (\exists msg \in Message : SSend(msg)) \/ RRcv

This way of defining the next-state action has no formal significance.

  • Closed-System Specification: Describes the correct behaviour of both the system and its environment.
  • Open-System Specification: Only describes correct behaviour of the system.

A behaviour satisfies an open-system specification if it represents a history in which either the system operates correctly, or it failed to operate correctly only because its environment did something wrong.

We usually write closed system specs. There’s not much use in writing open system specs.