A memory system consists of a set of processors connected to a memory by some abstract interface, which we label memInt.
flowchart LR P1[Processor] <--> Interface[memInt] DotDot[...] <--> Interface P2[Processor] <--> Interface Interface <--> Memory
The Memory Interface
We abstract away details like what kind of handhshake protocol is there, etc. We just have a Send operator when a Processor wants to send data to memory and a Receive operator when it wants to fetch data. We leave these unspecified by making them a parameter in the specification.
We resresent state of memory interface by the value of the variable memInt. A Send step does something to memInt but we don’t want to specify what. Since TLA+ only allows parameters of CONSTANT and VARIABLE types and not action types, we declare it to be a constant operator Send(p, d, memInt, memInt') which returns TRUE when the action is allowed. Here p is the processor that wants to send data d.
CONSTANT Send(_, _, _, _)
This doesn’t tell us what Send is and what it returns. We can assert the return by the assumption
ASSUME \forall p, d, miOld, miNew :
Send(p, d, miOld, miNew) \in BOOLEAN
The only way to formally state what the value of Send means would require us to define it and we don’t want to do that. So we informally say what it means (via comments in the spec).
Among others, we define a constant parameter Val which is the set of values a processor sends to and receives from the memory. We will have the memory respond to a write request with a value and it seems nice to have a value which can’t be returned by a read request. This will be NoVal. We could define this as a constant parameter and define NoVal \notin Val but it’s best to avoid introducing parameters.
NoVal == CHOOSE v : v \notin Val
The expression CHOOSE x : F equals an abritrary value that satisfies F. If there’s no such value, then it can equal any arbitrary value.
Following is the complete specification of the interface:
----------------------------MODULE MemoryInterface-----------------------------
VARIABLE memInt
CONSTANT Send(_, _, _, _),
Reply(_, _, _, _),
InitMemInt, \* The set of possible values of memInt
Proc, \* The set of processor identifiers
Adr, \* The set of memory addresses
Val \* The set of memory values
ASSUME \forall p, d, miOld, miNew : /\ Send(p, d, miOld, miNew) \in BOOLEAN
/\ Reply(p, d, miOld, miNew) \in BOOLEAN
-------------------------------------------------------------------------------
MReq == [op: {"Rd"}, adr: Adr] \union [op : {"Wr"}, adr: Adr, val: Val]
\* The set of all requests
NoVal == CHOOSE v : v \notin Val \* An arbitrary value not in Val
================================================================================
Functions
- A function
fhas a domain, writtenDOMAIN f, and it assigns toxthe valuef[x]. - For any sets
SandT, the set of all functions fromStoTis written[S -> T]. - TLA+ defines
[x \in S |-> e]to be the functionfwith domainSs.t.f[x] = efor everyx \in S.
A record is a function whose domain is a finite set of strings. The record`
[val |-> 42, ack |-> 1, rdy |-> 0]
can be written
[i \in {"val", "ack", "rdy"} |-> IF i = "val" THEN 42 ELSE IF i = "ack" THEN 1 ELSE 0]
The EXCEPT construct for records is a special case of the general EXCEPT construct for functions. !.c is an abbreviation for !["c"]
[f EXCEPT ![c] = e]
- The expression
ecan constain the symbol@where it meansf[c]. - Can also write:
[f EXCEPT ![c1] = e1, ..., ![cn] = en]
- We can write
[f EXCEPT ![c][d] = e]. Think of TLA+ functions as arrays, so this becomes a 2D array with one element changed
Since TLA+ defines records as functions, it is possible to manipulate them in ways that have no counterparts in programming languages. We can define an operator R s.t. R(r, s) is the record obtained from r by replacing the value of each field c that is also a field of the record s with s.c.
R(s, s) == [c \in DOMAIN r |-> IF c \in DOMAIN s THEN s[c] ELSE r[c]]
[g EXCEPT ![a, b] = e] is the function g_hat that is the same as g except with g_hat[a, b] equal to e.
A Linearizable Memory
This is a very simple memory system in which a processor p issues a memory request and then waits for a response before issuing the next request.
- In our spec, the request is executed by accessing a variable
mem, which represents the state of the memory - We let access of
memoccur any time between the request and response. This specifies what is called a linearizable memory.
There are also internal variables ctl and buf per processor:
Tuples as Functions
<<a, b, c>>is a 3-tuple with componentsa,b, andc.- TLA+ provides the cartesian product operator
\X(rendered ) - The
Sequencesmodule defines finite sequence to be tuples. - A sequence of length
nis a function with domain1 .. n.
Some operators from the module:
Head(s) == s[1]
Tail(s) == [i \in 1 .. (Len(s) - 1) |-> s[i+1]]
s \o t == [i \in 1 .. (Len(s) + Len(t)) |-> IF i <= Len(s) THEN s[i] ELSE t[i - Len(s)]]
Recursive Function Definitions
We can define the factorial function fact by:
fact[n \in Nat] == IF n = 0 THEN 1 ELSE n * fact[n - 1]
In general, a definition of the form f[x \in S] == e can be used to define a recursively a function f with domain S.
For multiple arguments:
Acker[m, n \in Nat] ==
IF m = 0 THEN n + 1
ELSE IF n = 0 THEN Acker[m-1, 0]
ELSE Acker[m-1, Acker[m, n-1]]