Professional Documents
Culture Documents
Editor
Ronald V. Book, University of California
Editorial Board
Erwin Engeler, ETH Zentrum, Zurich, Switzerland
Jean-Pierre Jouannaud, Universite de Paris-Sud, Orsay, France
Robin Milner, Cambridge University, England
Martin Wirsing, Universität Passau, Passau, Germany
ALGOL-like Languages
Volume2
Peter W. O'Hearn
Robert D. Tennent
Editors
Cover design by R. D. Tennent and Typesmith, with apologies to Guy Steele, designer of
the cover for The Little Lisper, by Daniel Friedman, Science Research Associates (1974).
ISBN 978-1-4757-3853-7
9 R7 6 5 4 3 2 1
Table of Contents
Contributors vü
FUNCTOR-CATEGORY SEMANTICS
Chapter 11
Functor Categories and Store Shapes
Frank]. Oles
Contents
1 Introduction 3
2 The Utility of Functor Categories 3
3 A Category of Store Shapes 8
4 Concluding Remarks 11
References 12
1 Introduction
An intuitive grasp of the nature of variable declarations and of what is hap-
pening as one enters and exits from blocks is essential to programming in
an ALGOL-like language. However, a precise semantic description of the con-
structs involved is difficult, and it is particularly difficult if one wants seman-
tics for block structure that mesh elegantly with semantics for procedures.
Our goal is to outline in general terms how functor categories can be used
to explain the semantics of ALGOL-like languages which possess a rich type
structure, higher-order procedures whose types may be arbitrarily complex,
imperative capabilities, and block structure. It is our aim to draw a sharp dis-
tinction between change of state and change of underlying structure. Block
entrances and block exits in connection with variable declarations change the
shape of the store (underlying structure), whereas commands alter the store
(state). It is our feeling that, far from being abstract nonsense, functor cate-
gories are a natural vehicle for giving mathematical substance to these intu-
itions about stack discipline.
not actually to have any phrases of type 8-acc, it is still useful to introduce
that phrase type in order to explain 8-var.
Also, for any phrase types T and (}, there is a phrase type T => (} that is
assigned to procedures accepting arguments (i.e., having formal parameters)
of type T and producing results (i.e., having calls) of type 0. The symbol =>
will intentionally be used also to denote exponentiation in Cartesian closed
categories.
An integral part of the semantics of a typed language is the assignment to
each phrase type T of a meaning, denoted Mng T. One might suppose Mng T
is a set; however, the possible existence of nonterminating programs, which
lead to an "undefined" state, provides an inducement to partially order Mng T,
where the relation x ~ y means x is more undefined than y; for instance, see
[Sco71, Rey77]. The need to give meanings to recursively defined expressions
of type T causes us to require that directed subsets of Mng T have least upper
bounds, i.e., that Mng T be a predomain. Also, we generally want Mng T to have
a minimal element, but we must tread cautiously at this point to avoid becom-
ing overcommitted to the use of Vom (the category of domains) rather than
Paom (the category of predomains). As we shall see later, Vom is technically
inadequate.
Suppose we try to give the semantics of programs which do not contain
block entrances and exits. We start by positing the existence of a set S of
possible stores. Regard sets as discretely ordered predomains (i.e., x ~ y iff
x = y). Since a command is a transformation of S that possibly may not
terminate, and a function from S to Sj_ (i.e., toS with a new least element .L) is
the same as a continuous function from S to Sj_, we expect
Mngcomm = S= S.c.
where => is the intemal hom functor (exponentiation) for Paom. Also, for
each data type 8,
Mng8-exp = S=> (Val8)j_,
Mng8-acc = Val8=> Mngcomm,
Mng8-var = Mng8-acc x Mng8-exp,
where Val8 is the set of values of data-type 8. In other words, a 8-expression
attempts to compute a value of type 8 from the current store, a 8-acceptor
uses a value of type 8 to update the current store, and a 8-variable may be
used as either a 8-expression or a 8-acceptor. Finally, for all phrase types T
and (}, we expect
Mng(T => {}) = MngT => Mng (};
i.e., the predomain of meanings for the procedural phrase type T => (} is the
predomain of continuous functions from Mng T to Mng (}.
Although the approach of the preceding paragraph is attractively compre-
hensible, it is inadequate for the semantics of block structure because the set
S is fixed throughout the exposition. The whole point of block structure is to
permit S to vary during program execution. For instance, if we view stores as
being functions from finite sets of locations in memory to the set of data-type
values, then the domains of those functions may be regarded as store shapes.
Frank]. Oles 5
Variable declarations at the start of a block alter the shape of the store by
adding locations to it, whereas block exit restores the shape of the store to
its condition at block entrance. The semantics of a language obeying a stack
discipline should reflect this dynamic behaviour.
Therefore, let ~ be the collection of all store shapes. To each X E ~. there
is a set StX of storesofthat shape. Since the meaning of phrasetypeT varies
with the store shape, Mng T is not a predomain, but is rather a ~-indexed
collection of predomains. For instance we might arrange matters so that
MngcommX = StX=> (StX).l
Mngö-exp X = StX=> (Valöh
Mngö-accX = Valö=>MngcommX
Mng ö-var X = Mng ö-acc X x Mng ö-exp X,
where X e ~ and ö is a data type.
It is important to realize that, for any phrase type T and store shapes X and
Y, the predomains Mng T X and Mng T Y cannot be arbitrarily different. After
all, we want the notion of command to have a uniform meaning for all store
shapes, or else the definition of operations like ; (concatenation of commands)
will be bizarrely complicated. For instance, consider the program skeleton
begin
int-var x;
X:= 3;
begin
bool-var y;
x:= 3;
end;
end
Suppose X is the store shape corresponding to the outer block and Y is the
store shape corresponding to the inner block. Then Mng comm X is relevant
to the first occurrence of the assignment command x := 3, while Mngcomm Y
is relevant to the second occurrence. However, both occurrences are meant
to alter the contents of the same location. Roughly speaking, the fact that
X can be "expanded" to give Y induces a function from Mngcomm X to
Mngcomm Y. So it becomes important to contemplate the notion of an ex-
pansion from a store shape X to a store shape Y. Certainly expansions should
be composable. The composition should be associative. For each store shape
X there ought to be an identity expansion which involves "doing nothing" to
X. In short, we assert that we erred in letting ~ be the collection of store
6 Chapter 11. Functor Categories and Store Shapes
shapes. From now on, take ~ to be the category of store shapes. The mor-
phisms of ~ are called expansions. Furthermore, for each phrase type T,
we should require that Mng T be a functor from ~ to 'Ptfom. This will el-
egantly take care of how an expansion1 u E X ~ Y induces a function
Mng T U E Mng T X p;r;;;;i Mng T Y.
Procedural phrase types are a bit tricky. Let T and 9 be phrase types. Recall
that in the simpler setting the predomain of meanings of type T => e was the
set of continuous functions from Mng T to Mng 9. One might hope that in the
more sophisticated setting, where Mng(T => 9) is tobe a functor, the set of
procedural meanings of type T => 9 in the context of a store shape X would be
the set of continuous functions from Mng T X to Mng 9 X; i.e.,
Mng(T=> O)X = MngT X=>MngOX.
Alas, this does not define a functor because => in 'Ptfom is contravariant in
its first argument. Another idea might be to recall that Mng T and Mng e are
objects in a functor category and to try letting Mng( T=> 0) be the set of natural
transformations from Mng T to Mng e. That is plain nonsense, because there
is no way to regard such a set of natural transformations as a functor. We
are, however, getting closer to the heart of the matter: the functor category
~ => 'Ptfom is Cartesian closed. Therefore, the appropriate equation governing
meanings of procedural types is just
Mng(T=> 9) = MngT=> Mng0,
where the heavy arrow on the right is exponentiation in the functor category
~=> 'Ptfom.
The reader may wonder why we did not engineer this discussion so as to
end up with the functor category ~ => 'Dom. Unfortunately, contrary to the
claim in [Rey81], it does not appear that ~=> Vom is Cartesian closed, in spite
of the fact that Vom is Cartesian closed.
This all sounds nice enough, but we have lost somewhere the idea that
phrases of type T should have denotations which are elements of Mng T, be-
cause Mng T does not seem to have any elements if it is a functor. The solution
is to reject, even in the simpler setting where block structure is ignored, the
intuition that a phrase of type T denotes an element of Mng T. Actually, this
is a rather conventional notion. This is where environments enter semantics.
In the simpler setting, complete specification of a language requires not only
a set S of stores, but also a type assignment function A from the set of identi-
fiers to phrase types. The function A has two uses. First, it is used to assign
types to free occurrences of identifiers, enabling the type of the entire phrase
to be determined. Second, A determines a predomain E of environments by
taking E to be the product in 'Pdöm of the Mng(A x) over all identifiers x.
Thus each element of E associates a meaning of the right type to each iden-
tifier. Then, even in the simpler setting, we conceive of the denotation of a
phrase of typeT as being a (continuous) function from E to MngT. Wehave
thus been led to the idea that the semantics of a phrase is a morphism in a
category from an environment object to a meaning object. We will give more
1We use X~ Y to denote the set of morphisms from X to Y in category ~-
Frank]. Oles 7
details shortly, but at least we need not worry that Mng T is a functor rather
than a set of elements.
The idea that each identifier possesses a unique phrase type is workable in
some contexts, but it is always unpalatable because it robs the programmer of
the ability to bind any identifier to any phrase type. This Ieads us to introduce
phrase-type assignments, functions from finite sets of identifiers to phrase
types. Since any phrase f may contain free occurrences of identifiers, a type
can be assigned to f only in the context of a phrase-type assignment oc.
Each phrase-type assignment oc determines an environment object Env oc,
which is a functor in~= 'Pdom, defined by taking the product of the functors
Mng(oc x) over all identifiers x in the domain of oc. If Xis a store shape, then
Env oc X is the product in 'Pdom of the predomains Mng( oc x) X over the same
set of identifiers x, and this is a collection of "conventional environments."
The denotation of a phrase f in the context of a phrase-type assignment oc
will be a morphism (natural transformation) in the functor category ~= 'Pdom
from Envoc to MngT, where T is the type of f in context oc. In particular, the
denotation of f in the context of phrase-type assignment oc and store shape
X is a function from "conventional environments" in Env oc X to "conventional
meanings" in MngT X.
Our final topic in this section is a discussion of how this approach lends it-
self to an explanation of the interactions between procedures and block struc-
ture. However, we must preface this with the definition of the functor
hom:!: E ~op - (~ = 'Pdöm)
The usual hom functor is
hom:!: E ~op x ~ -Set
where Set is the usual category of sets. Let E E Set - 'Pdom be the embed-
ding functor that gives a discrete partial order to each set. By composing, we
get
E o hom:!: E ~op x ~- 'Pdom
Then curry to get
hom:!: E ~op - (~ = 'Pdom)
Thus, for store shapes X and Y, hom:!: X Y is hom:!:(X, Y) =X~ Y, equipped
with a discrete partial order.
Let us Iook at a phrase f, which, in the context of an appropriate type as-
signment, has a procedural type, say T = lJ, where T and lJ are phrase types.
Suppose the procedure f is defined in a program in a block whose store shape
is X. In this context the denotation of the procedure is a function from Env oc X
to (Mng T= Mng O)X. The definition of exponentiation in the functor category
~ = 'Pdom teils us that the predomain (Mng T = Mng O)X is obtained by par-
tially ordering the set of natural transformations from (hom:!: X) x (Mng T)
to Mng e. Then the denotation of the procedure applied to a conventional
environment e existing when f is defined is a natural transformation from
(hom:!: X) x (MngT) to Mng8.
Now, suppose the procedure f is called in an interior block whose store
shape is Y. Thus there is an expansion o- E X~ Y, and the Y component
8 Chapter 11. Functor Categories and Store Shapes
(~1) cf> (p x y) = x}
(~2) p (cf> y) y = y, and
(B) p X (p x' y) = p X y,
necessary to check that their composite also satisfies them. Let x, x' E X and
z E Z. For (D), we have
(c/> o cJ>') {(Dz o (c/>'---+- p') o p) x z)
(c/> o cJ>') (p' (p x (c/>' z)) z)
c/> (c/>' (p' (p X ( c/>' Z)) Z))
cp (p x (c/>' z)), by (D) for (c/>',p'),
X, by (D) for (c/>, p).
X
;(]>~
cp Y rr Y/-
Given oc and ß, we must show there exi.sts a unique function ;y making the
diagram commute. Letz E Z. Define ;y by ;y z = p (oc z) y, where y E Y is any
element suchthat rr y = ß z. That ;y is well-defined is immediate from the
definition of -. Compute:
(cp o :y) z = cp (;y z) = cp (p (oc z) y) = oc z, by (~1).
and
(rr o :y) z = rr (:y z) = rr (p (oc z) y) = ß y,
because, for all x E X,
p X (p (OC z) y) = p X y, by (B).
X
;(]>~
cp Y rr Y/-
commutes; we must show ;y = ;y'. Again, letz E Z. Then ;y z- ;y' z, because
rr ()' z) = ß z = rr(y' z). Therefore, ·
;yz p (cp (;y z)} ()' z), by (~2),
p (oc z) (:y z)
p (oc z) (:y' z), since ;y z - ;y' z,
p (cp (;y' z)} (:y' z)
;y' Z, by (~2).
4 Concluding Remarks
We hope that this work will eventually find application in constructing im-
plementations of languages and in proving correctness of programs. Also it
leads us to speculate that there may be important results to be found relating
classes of languages (with semantic descriptions, of course!) with the "struc-
ture of their implementation," much like results relating formallanguages to
the automata that recognize them.
12 Chapter 11. Functor Categories and Store Shapes
References
[Ole82] F. j. Oles. A Category-Theoretic Approach to the Semantics of Programming
Languages. Ph.D. thesis, Syracuse University, Syracuse, N.Y., 1982.
[Rey77] j. C. Reynolds. Semantics of the domain of flow diagrams. ]. ACM, 24(3):484-
503, 1977.
[Rey81] J- C. Reynolds. The essence of ALGOL. In j. W. de Bakker and j. C. van Vliet,
editors, Algorithmic Languages, Proceedings of the International Sympo-
sium on Algorithmic Languages, pages 345-372, Amsterdam, October 1981.
North-Holland, Amsterdam. See Chapter 3.
[Sco71] D. S. Scott. The lattice of flow diagrams. In E. Engeler, editor, Sympo-
sium on Semantics of Algorithmic Languages, volume 188 of Lecture Notes
in Mathematics, pages 311-66. Springer-Verlag, Berlin, 1971. Also Technical
Monograph PRG-3, Oxford University Computing Laboratory, Programming
Research Group, Oxford.
Part V
SPECIFICATION LOGIC
Chapter 12
Using Functor Categories to Generate
Intermediate Code
]ohn C. Reynolds
In the early 80's Oles and Reynolds devised a semantic model of ALGOL-
like languages using a category of functors from a category of store
shapes to the category of predomains. Here we will show how a vari-
ant of this idea can be used to define the translation of an ALGOL-like
language to intermediate code in a uniform way that avoids unnecessary
temporary variables, provides control-flow translation of boolean expres-
sions, permits online expansion of procedures, and minimizes the stor-
age overhead of calls of closed procedures. The basic idea is to replace
continuations by instruction sequences and store shapes by descriptions
of the structure of the run-time stack.
Contents
1 Introduction 14
2 Types and Syntax 14
3 Functor-Category Semantics 15
4 Stack Descriptors 18
5 The Intermediate Language 19
6 From Semantics to Compilation 21
7 Commands 22
8 Integer Expressions 23
9 Variable Declarations 25
10 Boolean Expressions and Conditionals 26
11 Open Procedures 27
12 Closed Subroutines 28
13 Compiling Subroutines and their Calls 32
14 Subroutines for Product Types 34
15 Completions and Iteration 35
16 AnExample 36
17 Conclusions 37
Acknowledgements 37
References 37
First appeared in Con(erence Record of POPL '95: 22nd ACM SIGPLAN-SIGACT Symposium on
Principles of Programming Languages, pages 25-36, San Francisco, January 1995. ACM, New York.
© 1995 Association for Computing Machinery, reprinted by permission.
P. W. O’Hearn et al. (eds.), Algol -like Languages
© Springer Science+Business Media New York 1997
14 Chapter 12. Using Functor Categories to Generate Intermediate Code
1 Introduction
languages with a finite number of syntactic types). On the other handl there
is a considerable body of work using the continuation-passing transformation
to structure compilers for the specific case of call-by-value languages such as
SCHEME and ML [21 3].
ln this paperl we will describe a method of structuring the translation of
ALGOL-like languages that is based on the functor-category semantics devel-
oped by Reynolds [4] and Oles [51 6].
An alternative approach using category theory to structure compilers is
the early work of F. L. Morris [7]1 which anticipates our treatment of boolean
expressionsl but does not deal with procedures.
int(eger)acc(eptor) int(eger)var(iable) I
and that the set 8 of types is the least set containing these primitive types and
closed under the binary operation -.
We write :::;; for the least preorder suchthat
intvar s intexp intvar s intacc
If Oi s 01 and 02 s 02 then 01 - 02 s Oi - 02 .
When 0 :::;; 0' 0 is said to be a subtype of 0' •
1
3 Functor-Category Semanlies
The basic assumption that an ALGOL-like language is a species of typed lambda
calculus is captured by using a cartesian closed category to provide its se-
mantics. More specifically, we assume that there is a functor [- ], from 8
(preordered by the subtype relation and viewed as a category) to a cartesian
closed semantic category X, that interprets the type constructor- as the ex-
ponentiation operation of X:
[0- 0'] = [0] 7 [0'].
(If the type system includes type constructors for tuples or records, these will
be interpreted by products in X. Intersection types would be interpreted by
pullbacks, as discussed in [8, 9, 10].)
The functor [-] interprets types as objects of the semantic category X.
In addition, whenever 0 is a subtype of 0' (i.e. 0 s 0'), it maps the unique
morphism from 0 to 0' into an "implicit conversion morphism", which we
denote by [0 s 0'], from the meaning of 0 to the meaning of 0'.
The meaning of type assignments is specified by a functor [- ]*, from 8*
(preordered pointwise and viewed as a category) to X, that maps each type
assignment into an appropriate product in the semantic category:
[rr]* = n X
1edomrr
[TTL].
[intexp]S = s - zl.
[intacc]S = Z- (S- Sl.)
[intvar]S = [intacc]S x [intexp]S.
Similarly, the semantics of a phrase was a family of continuous functions pa-
rameterized by state sets:
[P]rreS E [rr]*S- [O]S.
Consider, for example, the .ALGOL-like program
new x:intvar in (x:= x + 1; ...
new y: intvar in (y := x + y; x := x + 1 ; ... )) .
In the outer block, where only a single variable is declared, the appropriate
set of states is the set of integers, while in the inner block, where a second
variable is declared, the appropriate set of states is the set of pairs of integers.
Thus the semantics of the two occurrences of x := x + 1 are provided by dif-
ferent members of the family [x := x + 1]rr,comm· The member of this family
appropriate to the occurrence in the outer block is
[x :=X+ 1]rr,commZ E [rr]* Z- (Z- Zl.),
which maps an environment appropriate to states that are integers into a
state-transition function on integers. 1f 17 is an environment specifying that
x denotes the integer that is the entire state, then [x := x + 1]rr,commZI7 will be
the function that increases an integer by one.
On the other band, the member of [x := x + 1 Drr,comm that is appropriate to
the occurrence in the inner block is
[x :=X+ Hrr,comm(Z X Z) E [rr]* (Z X Z) - ((Z X Z) - (Z X Z)l.),
which maps an environment appropriate to states that are pairs of integers
into a state-transition function on such pairs. If 17 is an environment specifying
]ohn C. Reynolds 17
that x denotes the first component of the state, then [x:=x+ 1]rr,comm<Z x Z)17
will be the function mapping a pair (x, y) into (x + 1, y).
In both cases, command execution is described by a state transition that
preserves the shape of the state. Indeed, this is generally true since, for any
command c,
[C]rr,commS E [rr]* S- (S - S.d
implies that [c]rr,commS17 preserves the shape S.
From the viewpoint of category-theoretic semantics, parameterization by
state sets is realized by taking the semantic category tobe the functor category
X=PDOM:!:,
where ~ is a category whose objects are state sets and PDOM is the category
of predomains and continuous functions. Of course, this implies that the
meanings of types are functors that act on morphisms of ~ as weil as objects,
and that the semantics of phrases are natural transformations between such
functors. In this brief synopsis, however, we will only remark that a morphism
in S T S', called an "expansion" in [4, 5, 6], shows how a small state in S can
be extracted from a large state in S' and how a small-state transitioninS - S .L
can be extended to a large-state transition in S' - s:.
It is shown in [5, 6] that the functor category PDOM:!: is cartesian closed
(actually for any ~). In particular, exponentiations are functors whose action
on objects of ~ is
(F ==:Jf G)S = hom:!:S x F x G.
(Here pointwise ordering is used to regard the set on the right as a predomain,
hom:!: is the curried hom-functor for the category ~. and hom:!:SS' = S T S' is
regarded as a discretely ordered predomain.)
To see how this exponentiation captures the interaction of procedures and
block structure, suppose
p E [81 - 02]S = ([81] 7 [02])S = hom:!:S x [81] x [82],
is the meaning of a procedure of type lh - 82. Then pisanatural transfor-
mation such that
pS' E (S T S') x [81]S'- [Oz]S'.
Here S is the state set that is appropriate to the point in the program
where the procedure is defined (and that contains states specifying the values
of any variables occurring globally in the procedure). For example, if p were
the meaning of the procedure in
new x: intvar in
Iet silly =.\c:comm. (c ;x := x + 1; c) in
new y:intvar in silly(x := x + y)
then S would be a set of integers specifying the global variable x.
However, as illustrated by the procedure call in the above program, the
procedure with meaning p can be called from an inner block where a set S' of
larger states is appropriate, and these larger states, rather than the members
18 Chapter 12. Using Functor Categories to Generate Intermediate Code
4 Stack Descriptors
During program execution, the variables and other information accessible to
the program will lie in a sequence of contiguous blocks, called frames, con-
tained within stack; when this sequence has length n, we will denote its mem-
bers by frame counts between 0 and n - 1, in order of their their position from
the bottom to the top (most recently allocated and highest addressed portion)
of the stack. We assume that the frames are organized as a linked list called
a static chain, specifically that a register SR points to the base of frame n - 1
]ohn C. Reynolds 19
and that the first (least addressed) word in each frame except frame 0 points
to the base of the previous frame.
For simplicity, we also assume that integer variables and pointers both oc-
cupy single words, and that addressing is by words. (In fact, our approach
extends straightforwardly to more complex cases where different kinds of
variables require fields of different sizes, and these fields must be aligned
on different-sized word boundaries.)
During compilation, for each variable the compiler will know the count Sr
of the frame containing the variable, and the displacement Sd, which is the
distance from the base of the containing frame to the location of the variable.
This pair S = (Sr, Sd) of nonnegative integers is called a stack descriptor.
As one would expect, the stack descriptors of variables will be embedded
(implicitly) in a compile-time environment describing the free identifiers of the
phrase to be compiled. However, compilation will also be influenced by more
general information about the stack that is not particular to any variable; in the
simple case considered in this paper this compile-time information consists of
the total number of frames (minus one) and the size of the top frame. We call
this pair of integers, which will depend upon position in the intermediate code
being compiled, the current stack descriptor scurr. Note that the current stack
descriptor describes the beginning of the free portion of the stack, i.e. the
position of the next variable to be allocated.
Stack descriptors are ordered lexicographically:
Thus the requirement that a variable described by sv must lie within a frame
in the currently active portion of the stack implies that sv !> scurr - 1.
The key to moving from a functor-category description of semantics to
an analogous description of intermediate-code generation is to replace Oles's
category ~ of store shapes by the ordered set of stack descriptors (viewed as
a category), which we will also denote by ~.
the conversion to actual machine code, all but at most one occurrence of such
a reference is replaced by a jump to the code obtained from its value, rather
than a copy of such code.
From the viewpoint of this paper, however, the treatment of loops, the
avoidance of code duplication, and the distinction between instructions and
their addresses are questions of representation. The mathematics of compila-
tion is much cleaner if we abstract away from finite instruction sequences with
references or jumps, to the possibly infinite sequences that they represent.
then
pS' E (S T S') x FS'- GS'.
But the morphism set S T S' contains a single morphism when S ::5 S' and is
empty otherwise. Thus a simpler but equivalent condition is that pS' belongs
to FS' - GS' when S ::5 S' and is the empty function otherwise. We indicate
this by
p(S' ~ S) E FS' - GS'
or
p(S' ~ S)(x E FS') E GS'.
We are skirting over the requirement that both the morphisms in X and
the members of (F 7 G)S should be natural transformations. In fact, this
requirement must be relaxed: Where naturality would require pairs of instruc-
tion sequences to be equal, we will only require them to have the same deno-
tational behavior, i.e. to denote the same functional continuation from states
22 Chapter 12. Using Functor Categories to Generate Intermediate Code
to final outputs. In some cases these instruction sequences will differ opera-
tionally, say by popping the stack at different steps.
A similar Situation holds with regard to coherence. Just as with seman-
tics, the translation [P]rro is defined by structural induction on the proof of
the typing rr 1- p : 0. However, different proofs of the same typing arenot
required to Iead to the same translation, but merely to translations with the
same denotational behavior. Suchtranslations may vary in the points where
implicit conversions are invoked.
Except for completions, which are translated into instruction sequences,
and integer variables, which are translated into acceptor-expression pairs,
each phrase of the input language is translated into a functional value in
the compiler that will be applied at compile-time to produce instruction se-
quences. Moreover, the type of the phrase in the input language will deter-
mine the type of its translation within the Compiler. However, this categorical
type discipline uses dependent function spaces that cannot be expressed in
most languages (such as ML) in which the compiler might be written. In such
languages, one must give translations a singletype (e.g. a recursive functional
data type in ML) that includes all the kinds of translations described above, as
weil as a variety of nonsensical translations that are guaranteed by the cate-
gorical discipline not to occur during any compilation.
7 Commands
If the phrase c has type comm under the type assignment rr, then
[C]rr,comm E [rr]* x [comm],
so that
[c]rr,commS(1J E [rr]* S) E [comm]S = ([compl] 7 [compl])S,
andthus
[C]rr,commS(1] E [rr]*S)(S' ~ S)(K E (Is•)) E (Is•).
Thus the translation of c is a function that accepts an environment 11 appro-
priate to the stack descriptor S and an instruction sequence K appropriate to
a possible larger stack descriptor S', and retums an instruction sequence ap-
propriate to S'. In the absence of jumps, K will describe the computation to
be performed after c, so that the result of this function will be obtained by
prefixing instructions for performing c to the sequence K. We will call K (like
its semantic counterpart) a continuation.
The translation of skip retums its continuation argument K without
change:
[skip]rr,commS1JS' K = K.
On the other hand, the translation of c1 ;cz first prefixes instructions for c2 to K,
and then prefixes instructions for c1. Put the other way round, the translation
of c1 ; c2 is the translation of c1 using a continuation that is the translation of
cz using the continuation K that is tobe performed after c1 ; c2 :
[Cl; Cz]rr,commS1JS' K = [Cl]rr,commS1JS' ([cz]rr,commS1JS' K).
]ohn C. Reynolds 23
8 Integer Expressions
If the phrase e has type intexp under the type assignment rr, then
[e]rr,intexpS(TJ E [rr]*S)(S' 2:: S)(ß E [intcompl]S') E Os•),
where ß E [intcompl]S' implies
ß(S" 2:: S')(r E (Rs")) E Os").
In essence, the translation of e must fill the hole in ß by applying ß to a right-
hand side r that will evaluate to the value of e, and then prefix to the resulting
instruction sequence any instructions needed to set up temporary variables in
r.
The translation of a constant, when given an integer continuation ß, simply
fills the "hole" in ß by applying it to an appropriate literal:
[7]rr,mtexpS1JS' ß = ßS' (lit 7).
24 Chapter 12. Using Functor Categories to Generate Intermediate Code
9 Variable Declarations
The other construct for which translation involves storage allocation is the
variable declaration. Suppose c is a command in which free occurrences
of the identifier L have type intvar. Then the translation of the command
new t: intvar in c gives an instruction sequence that allocates a new variable,
initializes it (say, to zero), executes c, deallocates the new variable, and finally
executes the continuation to be done after the whole command. Notice that
the deallocation is done by an adjustdisp instruction prefixed to the continu-
ation:
[new t:intvarinc]rr,commSTJ(S' <': S)(K E (IS')) =
sv := lit 0 [1]; [C][rrlt:intvar],commS"i]S"(adjustdisp[ -1]; K).
Here
sv = S' and S" = sv + 1 ,
so that the new variable is placed just above the stack described by S', and S"
describes the extended stack containing this variable. The environment used
to translate c is
i] = [ [rr]*(S ::s; S")TJ I t: (a,e)],
which is the extension of 1J that maps L into an acceptor-expression pair de-
scribing the new variable. (We will explain shortly why [ rr] * (S ::s; S") 11 occurs
here rather than TJ.)
The expression component e E [intexp]S" fills the hole in ß with the
stack descriptor sv for the new variable:
eS'" ß = ßS"' sv ,
while the acceptor component a E [intacc]S" prefixes to its continuation K'
an assignment of a hole to sv:
aS"' K 1 S"" r = sv := r[S:l' - s;;"] ; K 1
•
Notice that the functor-category discipline insures that S"' is larger than S",
so that the new variable lies within the stack described by S"'. The descriptor
S"" may be stilllarger, since it must include any temporafies occurring in r.
26 Chapter 12. Using Functor Categories to Generate Intermediate Code
{
adjustdisp[Sd- S,j] ; K when s;. = Sf
[compl](S s S')(K E (Is)) =
popto S ; K otherwise .
For exponentiations, the function f E (F 7 G)S, whose domain is the set of
stack descriptors greater than S, is restricted to the set of stack descriptors
greater than S':
(F =:Jf G)(S s S')f = fl{S" I S" ~ S'}.
For products, the morphism parts are defined componentwise:
(F xx G) (S s S') (a, e) = (F(S s S')a, G(S s S')e) .
Similarly, the morphism parts of the meanings of type assignments, which
are products of meanings of types over sets of identifiers, are also defined
componentwise:
[rr]*(S s S')I1L = [TTL](S s S')(17t).
11 Open Procedures
The functor-category semantics of the lambda-calculus aspects of ALGOL-like
languages is described by the following semantic equations, which are deter-
mined by the cartesian closed nature of X and the definition of Iet L p in p' =
by the redex (.\L. p')p:
[L]rr,mS1J = 1]L
[PlP2]rre•S1} = [pl]rr,e-e•S1]S([p2]rreS1})
[.\t: 8. P]rr,e-e•SnS' a = [p][rriL:OJ,e'S'[ [rr]* (S s S')n I L: a]
=
[Iet L P in p']rre•Sn = [p'][rriL:OJ.e'S[ 11 I L: [P]rreSn].
There is also an equation for implicit conversion from one procedural type to
another:
[81 - 82 s 8i_ - 82]S(f E [81 - 82]S)(S' ~ S)(a E [8i_]S') =
[82 s 82]S'(fS'([8i_ s 81]S'a)) whenOi_ s 81 and02 s 02.
These equations all carry over to compilation, where they describe the transla-
tion of (nonrecursive) procedures into open or "inline" code. (They have been
written above in the simplified form that is appropriate when ~ is a partial
order.)
Essentially, these equations describe a compiler where the lambda-calculus
aspects of the source language are completely reduced at compile-time, leav-
ing target code that is purely imperative. This is in pleasant contrast with
conventional approaches to compiling conventional languages, where inline
implementation of procedures is notoriously hard to get right.
28 Chapter 12. Using Functor Categories to Generate Intermediate Code
12 Closed Subroutines
Closed subroutines are necessary for the implementation of procedures, and
other types of phrases, that are defined recursively. (lt would be Straightfor-
ward to also provide a "letclosed" definition for nonrecursive entities that are
to be implemented by closed subroutines.)
We use the term procedure for lambda expressions and their meanings,
and the term subroutine or closed subroutine for instruction sequences that
may be called from several points in the intermediate-code program. It is
important to distinguish these concepts for two reasons: As we have already
seen, a procedure may be implemented by inline expansion rather than by a
subroutine, and on the other hand, because of the use of call by name, a phrase
such as a command or an expression, even though it is not a procedure, will
be implemented by a subroutine if it is defined by a recursive definition or is
a parameter to a procedure that is implemented by a subroutine.
Similarly, we will distinguish between parameters, which are source-
language phrases passed to procedures, and arguments, which are instruction
sequences (actually subroutines) passed to subroutines.
While procedures are classified by types, subroutines are classified by sim-
ple types:
frame Iist appropriate to the calling program to a frame Iist appropriate to the
subroutine, and since arguments may be passed by placing them in a vector
accessible from the new frame Iist. In general, to call a subroutine one must
specify
To execute the call, when there are one or more arguments, one switches con-
text to a frame Iist that is formed by adding a new frame (allocated on top of
the current stack) to the global frame Iist, and then sends control to the sub-
routine. The context switch causes the register SR to increase by an amount ö
that is the current frame displacement srr at the time of the call.
The new frame contains two words: the lower word points to the global
frame Iist, while the upper word points to a call block, which in turn contains
the argument Iist and the quantity ö. The latter is the distance between the
base of the frame pointing to the call block and the old frame Iist, which is in
turn the global frame Iist to be used when calling the arguments.
Notlee that the contents of the call block are the same for all executions of
a particular call, and are known at compile-time. Thus a single copy of the call
block can be placed in code space, rather than placing multiple copies (in the
case of a recursive call) on the stack.
It is important to distinguish the special case when the subroutine being
called takes no arguments, since in this case the information in the new frame
is vacuous, so that it is more efficient simply to take the new frame Iist to be
the global frame Iist. This will remove from the stack everything above the
most recent frame of the global frame Iist, but it is easy to see that this data
is inaccessible (since all pointers in the stack point downward). lndeed this
is the "stack pop" that occurs when a subroutine passes control to its return
address.
The specific method of achieving all this depends upon whether the sub-
routine being called is the result of compiling a definition, or is an argument
to a subroutine containing the call. In the first case, the identity of the sub-
routine being called is known at compile time, and its global frame Iist is a tail
of the current frame Iist. If there are arguments, then the call is performed by
executing the instruction sequence
where i is the subroutine being called, f is the frame count of the (top frame
of the) global frame Iist, and a1, ... , an are the arguments. This instruction
sequence changes
30 Chapter 12. Using Functor Categories to Generate Intermediate Code
an
a1
f- ö
SR 7
to
}·
t
and sends control to i. If the subroutine being called takes no arguments,
however, then in place of call i f () one simply resets SR to point to frame f
and sends control to i.
On the other hand, if the Subroutine being called is an argument then it will
occur, say in position j, in a call block pointed to by a frame, say with frame
count f, in the current frame list, and the relevant global frame list will also
be pointed to by the frame f. If the subroutine being called takes arguments,
then the call is performed by the instruction sequence
acall j f (a1, ... , an),
which changes
an
SR +;T
-- a1
ö
aJ
to }· ai
frame
f t '-+- ö'
lt ~ ö'
which changes
ai
SR
-- -+-- ö' to
}6'
and sends control to aj.
In summary, three new forms of instruction sequence have been intro-
duced for calling subroutines:
(Is) ::= call (lf+) f ((SRr 1 ), ••• , (SRrn>)
I acall j f ((SRr 1 ), ••• '(SRrn>)
I ajumpj
where 1 f ::s; s,, f+ = (f + 1, 2), n <!:: 1, and j <!:: 1. Here sRr denotes a subroutine
of simple type cp whose global frame list is described by the stack descriptor
S:
(SRr> ::=Os) when cp E {compl,intcompl}
(sRr> ::= Os+> otherwise,
where 1 (Sf,Sd)+ =(Sr+ 1,2).
In passing, we note that the calling conventions described here differ from
those used in traditional ALGOL compilers [13, 14] in that all arguments to
a subroutine are associated with the same global frame list, instead of each
argument having its own global frame list. The advantage of our approach
is that it reduces the amount of stack storage used to call subroutines. The
disadvantage is that, when a recursive call has an actual parameter that is
identical with the corresponding formal parameter, e.g. the parameter x in
letrec p = i\n. i\x. · · · p (n- 1) x · · · in · · · ,
an nth-level evaluation of the parameter will invoke a chain of n subroutines.
However, this inefficiency can be avoided if the programmer (or an optimizing
compiler) eliminates the parameter by using a global identifier.
In any event, although wehavenot pursued the matter, we expect that
more traditional calling conventions should also be expressible within the
functor-category framework.
1To correct an error in the previously published version of this paper, the author has changed
the displacements in the definitions of f+ and s+ from 3 to 2.
32 Chapter 12. Using Functor Categories to Generate Intermediate Code
while a subroutine is mapped into a call (more precisely into a function that 1
gives a call of an argument of simple type cp that is the jth argument in the
call block accessed from the top frame of the frame list described by S.
These three families of functions are defined by mutual induction on sim-
ple typesl reflecting the fact that compiling a subroutine involves compiling
calls of its argumentsl and compiling a call involves compiling subroutines for
arguments:
mk-subrcomp!S K = K
mk-callcomp!S i = i
mk-argcallcomp!S j = ajump j 1
call i Sr
(mk-subrcp 1 Sn([cpi](S 1 :S sn)ai) 1
mk-subrcp"Sn([cpn](Sn :S Sn)an))
mk-subrcp"Sn([cpn](Sn :S Sn)an)).
Further equations deal with the cases where cp ends in intcompl rather
than compll which arise when function procedures or expressions are com-
piled into closed subroutines. Here the special register sbrs is used to trans-
mit integer results. Letsaveres e [intcompl] T [compl] be the function
]ohn C. Reynolds 33
suchthat
saveresS ß = sv := sbrs [S~- Sd]; ßS' sv I
where
and S' = sv + 1.
Then mk-subrmtcomp!S ß = saveres S ß
mk-callmtcomp!S i S' r = sbrs := r [Sd- S~]; i
mk-argcallintcomp!S j S' r = sbrs := r [Sd- S~]; ajump j 1
mk-subr<PnS"([cpn](S" ~ S")an))
mk-subr<PnS"([<pn](S" ~ S")an)).
Using these functionsl it is Straightforward to translate recursive defini-
tions:
=
[letrec L p in p'Drre'STJ = [p'][rrlt:q>J.O'Sr7' 1
where
rl' = [ TJ I t: mk-c~Si]
i = mk-subr<PS([p][rrlt:q>J.q>STJ').
Here TJ' and i are mutual fixed points. This can be represented in the compiler
by making the instruction sequence i a loopl e.g. by making the i-field of the
call instruction generated by mk-call a reference whose value is eventually set
to i.
The reader may wonder why the contents of the register sbrs is stored in a
temporary variable immediately upon return from a subroutine that produces
a result. The reasonl illustrated by the integer expression
letrec x =··· in letrec y =· ·· in x + y I
is that sbrs may be reset by later calls before its contents is used.
34 Chapter 12. Using Functor Categories to Generate Intermediate Code
in = mk-subrcpnS an
(ai, ... • an)= <I>eS([p][rrlt:8],8SI7').
Here 17' and h, ... , in are mutual fixed points that can be represented in the
compiler by using a reference for each of the instruction sequences i11 ••. , in.
It is clear that the method outlined in this section could also be used to deal
with source language types, such as record or object types, that are defined by
products. To treat binary products, for example, one would take
ne x e'> = (re) o <re'),
where o denotes concatenation of sequences of simple types.
gives a simpler translation with the same denotational behavior that some-
times pops the stack sooner.
16 An Example
The following example illustrates many of the aspects of translation discussed
in this paper. Let P be the command
new x: intvar in
letrec pr =.\c: comm. (c; x := x + 1; if x ::s 10 then pr(c; c) eise skip)
in new y: intvar in pr(y := y + x x x) .
Then [P][],comm• applied to appropriate arguments for translating a complete
program, is 2
[P][],comm (0, 0) [] (0, 0) stop =
(0, 0) := lit 0 [1]; (0, 1) := lit 0 [1];
(0,0) (0,1)
call i 0 ( (1, 2) := (0, 0) x (0, 0) [1]; (0, 1) := (0, 1) + (1, 2) [ -1] ;ajump 1,
(0,2) (1,2) (1,3) (1,2)
2To correct errors in the previously published version of this paper, the author has reduced
several displacements in this example.
]ohn C. Reynolds 37
where i is
acall1 1 ( (0, 0) := (0, 0) + lit 1 [0] ;
(1,2) (1,2)
eise cijump 2) .
(1,2)
Under each instruction sequence in the above displays, we have placed the
stack descriptor that will be current when the sequence begins execution.
17 Conclusions
In the spring of 1994, the basic approach described here was used in an under-
graduate compiling course to construct an intermediate-code generator, writ-
ten in Standard MI.., for a simple ALGOL-like language. In the coming months,
we hope to extend it for use in implementing the language FORSYTHE.
A major research question is to what extent the approach can be extended
to generate more efficient intermediate-langnage code. We suspect that a
richer form of stack descriptor can be devised that will provide information
about the caching of variables in registers and the use of displays (groups of
registers pointing directly to active frames). On the other hand, the approach
depends so heavily on the use of continuations that it may be difficult to vary
evaluation order (say, by interleaving the evaluation of subexpressions) with
sufficient flexibility, especially for RISC machines.
A second question is whether the approach will lend itself to a proof of
compiler correctness. One would expect that the close connections between
functor-category semantics and our approach to code generation would lead
to simple proofs of the relationship between semantics and compilation. How-
ever, wehavenot yet pursued this topic beyond intuitive arguments.
Acknowledgements
This research was sponsored in part by National Science Foundation Grant CCR-
8922109 andin part by a fellowship from the Science and Engineering Research Coun-
cil.
References
[1) jones, N. D. and Schmidt, D. A. Compilergeneration from denotational semantics.
In Semantics·Directed Compiler Generation, Proceedings of a Workshop, Aarhus,
Denmark, January 14-18, edited by N. D. Jones. Lecture Notes in Computer Sci-
ence, vol. 94, Springer-Verlag, Berlin, 1980, pp. 70-93.
38 Chapter 12. Using Functor Categories to Generate Intermediate Code
[2) Steele Jr., G. L. RABBIT: A Compiler for SCHEME (A Study in Compiler Optimiza-
tion). Report no. AI-TR-474, Massachusetts Institute of Technology, Artificial
Intelligence Laboratory, May 1978, iii+272 pp.
[3) Wand, M. Deriving target code as a representation of continuation seman-
tics. ACM Transactions on Programming Languages and Systems, vol. 4 (1982),
pp. 496-517.
[4] Reynolds, J. C. The essence of ALGOL In Algorithmic Languages, Proceedings
of the International Symposium on Algorithmic Languages, Amsterdam, October
26-29, edited by J. W. de Bakker and J. C. van Vliet. North-Holland, Amsterdam,
1981, pp. 345-372. See Chapter 3.
[5) Oles, F. j. A Category-Theoretic Approach to the Semantics of Programming Lan-
guages, Ph. D. Dissertation. Syracuse University, August 1982, vi+240 pp. See
Chapter 11.
[6) Oles, F. J. Type algebras, functor categories, and block structure. In Algebraic
Methods in Semantics, edited by M. Nivat and J. C. Reynolds. Cambridge University
Press, Cambridge, England, 1985, pp. 543-573. See Chapter 11.
[7) Morris, F. L. Correctness of Translations of Programming Languages-An Alge-
braic Approach, Ph. D. Dissertation. Stanford University, August 1972.
[8) Reynolds, j. C. Conjunctive types and ALGOL-like languages (abstract of invited
lecture). In Proceedings Symposium on Logic in Computer Science, Ithaca, New
York, June 22-25. 1987, p. 119.
[9) Reynolds, j. C. Preliminary Design of the Programming Language FoRSYTHE. Re-
port no. CMU-CS-88-159, Camegie Mellon University, Computer Science Depart-
ment, June 21, 1988. See Chapter 8.
[10) Reynolds, j. C. The coherence of languages with intersection types. In Theoretical
Aspects of Computer Software, International Conference TACS '91, Proceedings,
Sendai, Japan, September 24-27, 1991, edited by T. Ito and A. R. Meyer. Lecture
Notes in Computer Science, vol. 526, Springer-Verlag, Berlin, 1991, pp. 675-700.
[11] Scott, D. S. The lattice of flow diagrams. In Symposium on Semantics of Algo-
rithmic Languages, edited by E. Engeler. Lecture Notes in Mathematics, vol. 188,
Springer-Verlag, Berlin, 1971, pp. 311-366.
[12] Goguen, j. A., Thatcher, J. W., Wagner, E. G., and Wright, j. B. Initial algebra
semantics and continuous algebras .. Journal of the ACM, vol. 24 (1977), pp. 68-
95.
[13) Dijkstra, E. W. Recursive programming. Numerische Mathematik, vol. 2 (1960),
pp. 312-318.
[14) Naur, P. The design ofthe GIERALGOL compiler, part I. BIT, vol. 3 (1963), pp. 124-
140. Reprinted in Goodman, Richard, editor, Annual Review in Automatie Pro-
gramming, Vol. 4, Pergarnon Press, Oxford (1964) 49-85.
Chapter 13
Semantical Analysis of Specification Logic
Robert D. Tennent
The specification logic of J. C. Reynolds is a partial-correctness logic for
ALGOL 60-like languages with procedures. It is interpreted here as an
intuitionistic theory, using a form of possible-world semantics first ap-
plied to programming-language interpretation by Reynolds and F. J. Oles
to give an abstract treatment of stack-oriented storage management. The
model provides a satisfactory solution to all previously-known problems
with the interpretation of specification logic; however, unexpected new
problems have been discovered in doing this work, and these remain un-
solved.
Contents
1 Introduction 41
2 Syntax 44
3 Possible Worlds 47
4 Semantic-domain Functors 48
5 Semantic Valuations 53
6 Formal System and Soundness 58
7 Concluding Remarks 60
Appendix 61
Acknowledgements 62
References 62
1 Inttoduction
In the beginning, C. A. R. Hoare (1969) created a programming logic for spec-
ification triples of the form {P} C {Q}. And Hoare's logic is good (for simple
imperative programming languages without procedures or jumps). It is both
sound (Hoare and Lauer, 1974) and, independently of how the vexed ques-
tion of its completeness might be answered (de Bakker and Meertens, 1975,
Cook, 1978, Wand, 1978, Bergstra et al., 1982, Goldblatt, 1982, Clarke, 1984,
Leivant, 1985, Pasztor, 1986, Leivant and Femando, 1987), usable (Reynolds,
1981a), providing a framework for rigorous specification, development, and
verification of practical programs.
Many attempts have been made to extend Hoare's logic to languages with
procedures or jumps (Hoare, 1971, Clint and Hoare, 1972, Arbib and Alagic,
1979, de Bakker et al., 1980, Gries and Levin, 1980, Olderog, 1984, Trakht-
enbrot et al., 1984, Sieber, 1985). The specification logic of J. C. Reynolds
(1981a, 1982) is perhaps the most important of these, because its general-
ity, usability, and coherence have been clearly demonstrated. The aim of the
research described here is to do the same for its soundness.
First appeared in Information and Computation, 85(2):135-162, 1990. Reprinted with the permis-
sion of Academic Press.
where x0 satisfies P. Let X' be the subset of X for which R has the same value
as it does at x0 • Then assumption C # R ensures that the execution from x0 to
x 1 can also take place in X'. Because-Of examples like (.\C:comm.skip)(C),
it does not in general follow from C#R that t#R, even when t occurs in C;
however, in possible world X' execution oft cannot interfere with R so that,
using the new interpretation of implication, the second assumption ensures
that x1 satisfies Q.
Our interpretation of non-interference also validates certain Non-
interference Decomposition axioms. For example, for any command C and in-
teger expression E, the following is an instance of Rightside Non-interference
Decomposition that is valid in our model.
C#E '* C#(E > 0),
where C # E for an expression E means that any execution of C preserves the
value of E invariant. However, the formal system also indudes "higher-order"
instances of these axioms, such as
G#E '* G#(E > 0),
for G: comm - comm (i.e., G is a procedure whose arguments and calls are
commands). When G: comm- comm and E is an expression-like phrase such
as an expression or an assertion, Reynolds de(ined G # E to be equivalent to
the following formula:
VC:comm.C#E '* G(C)#E
But, consider the following G: comm - comm:
.\C:comm.if i = 1 then (C; if i > 1 then i := 0);
G#i is true using Reynolds's definition, yet G#(i > 0) is false, because, for
example,
(if i > 0 then i := i + 1) #(i > 0)
is true and
G(ifi > Otheni := i + 1)#(i > 0)
is false. Hence, higher-order instances of Rightside Non-interference Decom-
position do not hold in general (using Reynolds's interpretation of higher-
arder non-interference). We will return to this problern in Section 6.
2 Syntax
The type structure for the language to be considered is given in Table I.
For simplicity, we have avoided coercions and conventional variables (but
have retained acceptors, which are the "updating" components of variables;
see (Reynolds, 1980, Reynolds, 1981b). lnformally, a data type T (such as
Boolean or integer) denotes a set [ T] of values appropriate for some accep-
tor or expression, whereas a phrase type )' denotes a set or poset [y] of
meanings appropriate for some kind of phrase. Assertional phrase types and
specifications are distinguished from ordinary phrase types because mean-
ings of "logical" phrases need not be computable. All phrase types except
Robert D. Tennent 45
Metavariables
T data types
(} ordinary phrase types
oc assertional phrase types
ß bindable phrase types
y general phrase types
Productions
(} .. - val[T] values
exp[T] expressions
comm commands
acc[T] acceptors
( } - (}' ordinary procedures
oc .. - assert assertions
ß-oc assertion procedures
ß .. - 91oc
)' .. - ß
spec specifications
Value phrase types val[T] are introduced to allow quantification over val-
ues, as suggested in (Sieber, 1985); for example, if C:comm and E:exp[T],
C#E can be defined tobe Yt:val[T].C#(E =T t), for any L not free in C or
E, assuming the obvious coercion from val[T] to exp[T]. A "good-variable"
specification and non-interference specifications for acceptors and procedures
will be defined later.
3 Possible Worlds
In order to treat procedures and variable declarations conveniently, the model
described formally in the rest of this paper is based on a category-theoretic
formulation of possible-world semantics (Goldblatt, 1979). In this framework,
possible worlds and changes of possible world form a category, X, and seman-
tic domains are replaced by semantic-domain functors from X to either S, the
usual category of sets and functions, for "logical" phrase types, or the cate-
gory D of directed-complete posets and continuous functions for the "compu-
tational" phrase types. Then each phrase, Z, of the language is interpreted as a
natural trans(ormation, [Z], from an appropriate "environment" functor, Env,
to an appropriate semantic-domain functor, D, so that, if x and y are possible
worlds and f: x - y is a change of possible worlds, the following diagram
commutes in S or D:
X Env(x) --=['-Z-=-](-'-x~)- • D (x)
Ellv(f)j JL>(f)
y Env(y) [Z](y) D(y)
In the introduction, changes of possible world were restrictions to subsets.
But if the programming language has variable declarations, it must also be
possible to expand the set of states, as in (Reynolds, 1981b) and (Oles, 1982,
Oles, 1985). The appropriate generalization seems tobe the following cate-
gory: the objects are sets, interpreted as the sets of states allowed in each
possible world, and a morphism from X to Y is a pair f, Q having the follow-
ing properties:
(i) f is a function from Y to X;
(ü) Q is an equivalence relation on Y; and
(ili) f restricted to any Q-equivalence dass is injective; i.e., for all y,y' E Y,
if yQy' and f(y) = f(y') then y = y'.
Intuitively, f extracts the small stack embedded in a larger one, and Q relates
large stacks with identical "extensions." The category described in (Oles, 1982)
is similar, but there the function f is required tobe bijective on equivalence
classes, so that Y ~X x Y /Q.
The identity morphism idx on an object X has as its two components: the
identity function on X, and the universally-true binary relation on X. The com-
position (in diagrammarte order) f, Q; g,R:X- Z of morphisms f, Q:X- Y
and g,R:Y- Z has as its two components: the functional composition of
48 Chapter 13. Semantical Analysis of Specifi.cation Logic
4 Semantic-domain Functors
Our aim in this section is to define the semantic-domain functors for our
model. Webegin by defining a number of general constructions in categories
of sets, complete partially-ordered sets, and functors. Let S be the usual
category of sets and functions. Let D be the category of directed-complete
partially-ordered sets and continuous functions (Reynolds, 1977); the objects
of this category will be termed here domains, whether or not they have least
elements. Throughout, "id"" will denote the identity morphism on object x
and ";" will denote composition of morphisms or functors in diagrammatic
order.
For any domains D and D', we define new domains as follows.
(i) Product: D x D' is the Cartesian product of D and D', ordered
component-wise.
(ü) Lifting: D .L is (an isomorphic copy of) D augmented by a new least ele-
ment, denoted .L
(ili) Exponentiation: D - D' is the set of all continuous functions from D to
D', ordered point-wise.
(iv) Partial exponentiation: D -- D' is the set of all continuousi partial func-
tions from D to D', ordered point-wise; i.e., f ~D-D' 9 iff, for all x e D,
if f(x) is defined, then 9(x) is defined and f(x) ~D' 9(x).
We also use exponentiations in category S: if SandS' are sets, S - S'
is the set of all functions from S to S', and S -- S' is the set of all partial
functions from S to S'.
1 We will only use this constructlon with discretely ordered D so that all partial functions are
continuous, but the following is a general definitlon (from Plotkin, 1985): a partial function from
D to D' is continuous just if the inverse image of any Scott-open subset of D' is Scott-open in D,
where a subset U of a domain Dis Scott-open ilJ (i) if u e U and u !;;D d, then d e U, and (ü) for
every directed subset S of D, if US e U then d e U for some d e S.
Robert D. Tennent 49
= {m n (F(y)- G(y)) I
E f:x-y
forallf:x-yandg:y-z,}
m(f); G(g) = F(g); m(f; g) '
= {m n (F(y) -
E f:x-y G(y)) I for all f: x - y and g: y - z, }
m(f); G(g) s;; F(g); m(f; g) '
F(g,y-z)j JG(!f.y-z)
F(z) m(f;g:x- z) G(z)
=
{m E n (F(y)
f:x-y -
G(y))
I form(f;all f:x- y and g:y- z, } ·,
g) ; G(g) s;; F(g) ; m(f)
F(g,y-z)l IG(g,y-z)
F(z) m(f;g:x- z) G(z)
is required whenever the partial function at the bottom gives a defined result.
We define the morphism part to yield covariant functors; for example,
(F - G)(f) (m E (F - G)(x)) = m(f; g).
These constructions will be used to construct meaning functors [ y] for the
phrase types y of our language from the following "primitive" functors, where
X is now the category of possible worlds defined in Section 3:
(i) S is the contravariant functor from X to D defined as follows: for any
X-object X, S(X) = X, discretely-ordered, and, for any X-morphism
f,Q:X- Y, S(f, Q) is the function f E Y - X. This is the only functor
that depends directly on details of category X.
(ü) For every data typeT, [T] is a constant functor (covariant or contravari-
ant, as appropriate) from X to D such that, for every X-object x, [ T]x is
the discretely-ordered domain of values of type T.
Robert D. Tennent 51
(ili) T is the (contravariant) constant functor from X to S such that, for every
X-object x, T(x) is the set {true,false}.
(iv) 1 is the (covariant) constant functor from X to S such that, for every
X-object x, 1 (x) = A, for some singleton set A.
(v) F is the (covariant) functor from D to S that forgets partial-orderings
and treats continuous functions as arbitrary functions.
(vi) Eis the (covariant) functor from S to D that embeds sets into the cate-
gory of domains by discretely ordering them and treating arbitrary func-
tions as continuous functions.
Note that E and F preserve products, E preserves exponentials, and E; F is the
identity functor on S.
We can now define functors [6]:X- D, and [oc], [spec]:X- S. We
first consider [comm]. In earlier work (Reynolds, 198lb, Oles, 1982, Tennent,
1985), command meanings were, for any possible-world X, partial functions
or binary relations on X. But it turns out tobe simpler to use elements of
(S - S)(X), where S is the states functor defined above. Then, command
meanings are X-morphism-indexed families of partial functions, and it is no
Ionger necessary to depend on the morphism part of the functor to define such
families "implicitly." The uniformity condition for such families allows com-
mand execution to become less-defined in more-restricted possible worlds,
while ensuring that, when such execution is defined, the results are consis-
tent with execution in less-restrictive worlds. However, we shall not define
[comm](X) tobe (all of) (S - S)(X), because this would permit commands
that have been defined non-locally to modify the values of local variables. The
constraint in the following definition precludes this.
For any X-object X,
[comm]X
[exp[Tl](g; f{z})(e)(h)(w)
e(g; f {z} ; h) (W) by the definition of [exp[Tl]
e(g) (S(f {z}; h)(w)) by the definition of -
e(g)(z) by the definition of r{z}
eo(g)(z) by the definition of e0
[exp[Tl] (g; f {z} )(eo)(h)(W) as with e.
Now, we prove the proposition as follows:
p(f) (e) (g) (z)
p(f) (e)(g; r{z} )(z)
by the definition of [assert]
p(f;g; r{z})([exp[Tl](g; f{z})(e))(id{zJ}(Z)
by the definition of [exp[T]- assert]X
p(f;g; f{z})([exp[Tl](g; f{z})(eo))(id{zJ)(Z)
by the lemma
p(f) (eo)(g)(z)
as with e
I
Robert D. Tennent 53
[absurd]xuf = false
[SI &S2]xuf = [SI]xuf and [S2]xuf
[SI=> S2]xuf = for all g:y- z, if[Sdxu(f;g) then [S2]xu(f;g)
['t't:ß.S]xuf
= for all g:y- z and m E [ß]z, [S]z(([rr]; F)(f; g)(u) lt- m) {idz)
5 Semantic Valuations
We may now interpret the phrases of the language as natural transformations
from environment functors to meaning functors:
[(rr 1-- 0)]: [rr]--:... [0]
[ (rr I- IX)]: ([rr]; F) --:... [IX]
Table V: Assertions
[skip]xuf = ids(y)
[Cl; C2]xu{ = [Cdxuf; [C2]xu{
rA ._ E] f _ { [A]xufv(idy)Yo. if [E]xufy0 = v E [T]y;
l ·-T x u Yo - undefined, otherwise
[{P}C{Q}]xuf
= for all Yo.YI E S(y), if [P]xufyo and [C]xufyo = Yl then [Q]xufyl
[C#P]xuf
= for all g:y- z and v E {true,false},
S<r Zv); [C]x u(f; g) = [C]x u(f; g; rzv); S(r Zv)
where Zv = {zv E S(z) I [P]x u(f; g)zv = v}
f, Q j
x([T]Y)
jexpand, (f, QJ
Y _ _____:..=--='--'----+- expandT ( Y)
We can now define the acceptor and expression components
aT(X) E ([T]- [comm])(expandT(X))
and
eT (X) E [ exp[ T] ]( expandT (X))
of a "new" variable of data typeT in an expanded possible world expandT (X).
For X-morphisms f, Q: expandT(X)- Y and g,R: Y - Z, and v1 E [T]Y,
aT(X)(f,Q)(vi)(g,R)(zo E Z) = Z1 E Z suchthat
(i) f(g(zd) = (x, v1) where (x, vo) = f(g(zo)), and
(ü) zoRz1 and g(zo)Qg(zl ),
and eT(X) (f, Q)(yo E Y) = v, where (x, v) = f(Yo). In the definition of aT, a
56 Chapter 13. Semantical Analysis of Specification Logic
state ZI satisfying the two conditions might not exist and then the result is un-
defined; however, if such a state does exist, it must be unique by the injectivity
property of X-morphism f, Q; g, R. Intuitively, the effect of assigning a value
VI to the acceptor is to replace the old value vo in the appropriate component
of the stack by vi, without changing more-local components (condition (ü)) or
more-global components (i.e., the x in condition (i)).
The semantic equation for block commands is then
[new[T] t, t' in C]x u f Yo
YI, if [C] (expandT (x)) (u') (expandT(f)) (yo, vT)
{ = (YI. v) E S(y) X [T]y,
where u' = ([rr]{x([T]x))(u) I t - aT(x) I t'- eT(x))
undefined, otherwise,
where vT is a "standard" initial value for variables of type T. The following
three propositions show, respectively, that
(i) assignments to a local variable do not affect non-locally-defined asser-
tions;
(ü) a local variable is not affected by non-locally-defi.ned commands; and
and
A#Q - VE2:exp[T]. (A :=T E2)#Q
for any identifier E2 not free in A, E1. or Q.
Proof: Consider any X-morphism f:X' - Y, eo E [exp[T]]Y,
p E [exp[T]- assert]Y, and let rr" be
to derive that
[A#E1 QA#P(Et)]Z([rr"]gu" I E1 ..... e~)(idz)
But then
[(A :=T E2)#P(EI))Z([rr"]gu" I E1 ..... e~ I E2 ..... [exp[T]]geo)(idz)
and1since execution of the assignment preserves the value of P(E1)1
[exp[T]- assert](g)(p)(idz )(e~)(idz Hzo)
= [exp[T]- assert](g)(p)(idz)(e~)(idz)(zt)
i.e. 1p(g)([exp[T]]gev)(idz)(zo) = p(g)([exp[T]]gev)(idz)(zt)
i.e.1 [assert]g(p(idz Hev)) (idz )(zo) = [assert]g(p(idz Hev)) (idz Hz1)
i.e.1p(idy)(ev)(g)(z0 ) = p(idy)(ev)(g)(zt)
and thenl by using Proposition 1 twicel we get that
p(idy )(eo)(g)(zo) = p(idy) ([ exp[Tl] (f) (eT (X))) (g)(zi) I
7 Concluding Remarks
Specification logic is an ALGOL-like (Reynolds, 1981b) programming logic. It
is statically-typed. It avoids making references explicit. It treats substitution,
binding and scope correctly. It includes the laws of the (typed) Iambda calculus
as a sub-system. It requires expressionstobe side-effect-free. It distinguishes
between the logic for data types and the logic for phrase types. It provides
"generic" facilities such as quantification and non-interference uniformly for
all relevant phrase types. And, finally, we have shown here that it "obeys
a stack discipline" (though this operational terminology is inappropriate for
logical phrase types) in that "local" changes to storage structure, such as state-
set expansions and restrictions, can be allowed for by using possible-world
semantics without making the worlds explicit in the logicallanguage.
The difficulties with non-interference for higher-order phrases appear tobe
fundamental. One possible approach, suggested to the author by P. O'Hearn,
is to re-interpret G#P for higher-order G to capture the intuition that G(C)
can interfere with P only by using C; with Reynolds' interpretation, G ( C) is
unconstrained when C interferes with P. A satisfactory interpretation along
these lines would be most welcome because it would necessitate only very
minor changes to the formal system of specification logic. Further evidence
that the problems may arise from Reynolds's definitions of higher-order non-
interference is the fact that when both operands of # are higher-order, the
reductions are applicable in two ways, and the results do not seem to be equiv-
alent.
Another direction that could be explored is suggested by the realization
that a kind of "weak converse" of Non-interference Decomposition is needed,
along the lines of Non-interference Composition. Since non-interference for-
mulas are typically used as assumptions for Hoare-triple formulas, this might
be feasible, but would require significant modifications to the logic.
The most drastic "solution" to the problems of non-interference might be
to avoid them completely by adopting a syntactic method of interference con-
trol, along the lines of (Reynolds, 1978). In fact, the most unsatisfactory as-
pect of specification logic in practice is the inconvenience of having to deal
explicitly with non-interference and good-variable assumptions. Syntactic con-
straints to achieve interference control would restriet expressivity somewhat,
but would be helpful to language implementers as weil as program verifiers.
Specification logic, in its present state of development, also seems inade-
quate to deal with Situations in which representational abstraction is involved,
as in Chapter 5 of (Reynolds, 1981a). For example, in the absence of concur-
rency it should be possible to allow for "benevolent" side effects (Hoare, 1972)
Robert D. Tennent 61
Appendix
The following example, from a personal communication to the author by
J. C. Reynolds, demonstrates the undesirable consequences of adopting full classi-
cal (rather than intuitionistic) logic as the logical basis for specification logic. From
the rules for assignment, skip , and statement compounding, one derives
gv(x) => {x = 3} skip; x := 4 {x = 4}
By mathematical-fact introduction, static implication, and consequent weakening:
gv(x) & {x = 3} => {x = 3} skip; x := 4 {false} .
LetS be the formula {x = 3} skip; x := 4 {false}. Then,
gv(x) & ~s & {x = 3} .. absurd
and, since absurd implies anything,
gv(x) & ~s & {x = 3} => {true} skip {false} .
Now, Strong Constancy gives us
skip#(x = 3) & ( {x = 3} => {true} skip {false}) => {x = 3} skip {false}
and so, since skip#(x = 3), we get
gv(x) & •S => {x = 3} skip {false}
However, by assignment, gv(x) => {false} x := 4 {false} so that statement compounding
gives us
gv(x) & •S => {x = 3} skip; x := 4 {false} ;
i.e., gv(x) & •S => S, so that gv(x) => ••S. Finally, with classicallogic, we get gv(x) => S,
which asserts that the assignment x := 4 does not terminate if x initially has a value
other than 4.
2 Editors' note: the assertion is incorrect. In fact, this particular equivalence does hold in the
model; see Chapter 14 in this volume.
62 Chapter 13. Semantical Analysis of Specification Logic
Acknowledgements
I am extremely grateful to John Reynolds for bis many contributions to tbis work. I
am also grateful to Gordon Plotkin, Eugenio Moggi, David McCarty, Kevin Tobin, and
Peter O'Hearn for tecbnical discussions, and to Robin Milner for bis hospitality during
a productive sabbatical year at the University of Edinburgh. A preliminary report on
tbis work appeared as (Tennent, 1985).
References
ARBIB, M. A. AND ALAGIC, S. (1979). Proof rules for gotos. Acta In{ormatica, 11:139-
148.
BENCIVENGA, E. (1986). Free logics. In Handbook of Philosophical Logic, vol. m: Alter-
natives in Classical Logic (D. Gabbay and F. Guenthner, editors), number 166 in
Synthese Ubrary, pages 373-426, D. Reidel, Dordrecht, Holland.
BERGSTRA, j. A., TIURYN, j., AND TUCKER, j. (1982). Floyd's principle, correctness the-
ories, and program equivalence. Theoretical Computer Science, 17:129-147.
CLARKE, JR., E. M. AND KOZEN, D., editors (1984). Logics o{Programs 1983, volume 164
of Lecture Notes in Computer Science, Springer-Verlag, Berlin, 1984.
CLARKE, jR., E. M. (1984). The characterization problern for Hoare logics. In Mathe-
matical Logic and Programming Languages (C. A. R. Hoare and j. C. Shepherdson,
editors), pages 89-106, Prentice-Hall International.
CLINT, M. AND HaARE, C. A. R. (1972). Program proving: jumps and functions. Acta
In{ormatica, 1:214-224.
CooK, S. A. (1978). Soundness and completeness of an axiomatic system for program
verification. SIAM]. on Computing, 7:70-90. Corrigendum, 10:612 (1981).
DE BAKKER, J. W. AND MEERTENS, L. G. L. T. (1975). On the completeness of the inductive
assertion method. ]. Comput. Sys. Sei., 11:323-357.
DE BAKKER, j. W., DE BRUIN, A., AND ZUCKER, j. I. (1980). Mathematical Theory of
Program Correctness. Prentice-Hall International, London.
DUMMETT, M. (1977). Elements of lntuitionism. Oxford University Press.
GoLDBLATT, R. (1979). Topoi, The Categorial Analysis of Logic. North-Holland, Ams-
terdam.
GoLDBLATT, R. (1982). Axiomatising the Logic of Computer Programming, volume 193
of Lecture Notes in Computer Science. Springer-Verlag, Berlin.
GRIES, D. AND LEVIN, G. (1980). Assignment and procedure call proof rules. ACM
Trans. on Programming Languages and Systems, 2(4):564-579.
GRIES, D., editor (1978). Programming Methodology, A Collection of Articles by IFIP
WG 2.3. Springer-Verlag, New York.
HaARE, C. A. R. AND LAUER, P. E. (1974). Consistent and complementary formal theories
of the semantics of programming languages. Acta Informatica, 3(2):135-153.
HaARE, C. A. R. (1969). An axiomarte basis for computer programming. Comm. ACM,
12(10):576-580 and 583.
HaARE, C. A. R. (1971). Procedures and parameters: an axiomatic approach. In Sym-
posium on Semantics of Algorithmic Languages (E. Engeler, editor), volume 188 of
Lecture Notes in Mathematics, pages 102-116, Springer-Verlag, Berlin.
HaARE, C. A. R. (1972). Proof of correctness of data representations. Acta Informatica,
1:271-281. Reprinted in (Gries, 1978), pages 269-281.
Robert D. Tennent 63
REYNOLDS, J. C. (1982). IDEALIZED ALGOL and its specification logic. In Tools and No-
tions {or Program Construction (0. Neel, editor), pages 121-161, Cambridge Uni-
versity Press, Cambridge, 1982. See Chapter 6.
Scorr, D. S. (1967). Existence and description in formallogic. In Bertrand Russell,
Philosopher of the Century (R. Schönmann, editor), pages 181-200, Allen & Unwin,
London.
Scorr, D. S. (1979). Identity and existence in intuitionistic logic. In Applications of
Sheaves (M. P. Fourman, C. V. Mulvey, and D. S. Scott, editors), volume 735 of
Leerure Notes in Mathematics, pages 660-696, Springer-Verlag, Berlin.
Scorr, D. S. (1980). Relating theories of the Iambda calculus. In (Seldin and Hindley,
1980), pages 403-450.
SELDIN, J. P. AND HINDLEY, J. R., editors (1980). To H. B. Curry: Essays in Combinatory
Logic, Lambda Calculus and Formalism. Academic Press.
SIEBER, K. (1985). A partial correctness logic for procedures (in an ALGOL-like lan-
guage). In (Parikh, 1985), pages 320-342.
STOUGHTON, A. (1988). Fully Abstract Models of Programming Languages. Research
Notes in Theoretical Computer Science, Pitman, London, and Wiley, New York.
TENNENT, R. D. (1985). Semantical analysis of specification logic (preliminary report).
In (Parikh, 1985), pages 373-386.
TRAI<HTENBROT, B. A, HAI.PERN, J. Y., AND MEYER, A. R. (1983). From denotational to
operational and axiomatic semantics for ALGOL-like languages: an overview. In
(Clarke and Kozen, 1984), pages 474-500.
VANDALEN, D. (1983). Logic and Structure. Springer, Berlin, second edition.
WAND, M. (1978). A new incompleteness result for Hoare's system. ]. ACM, 25:168-
175.
Part VI
Contents
1 lntroduction 65
2 Categories and Functors 71
3 Semantics of Non-lnterference 74
4 Non-lnterference Decomposition 76
4.1 Leftside Decomposition 76
4.2 Rightside Decomposition 77
4.3 Passive and Chaste Types 78
5 Local-Variable Declarations 81
6 Non-lnterference Abstraction 85
7 Concluding Remarks 90
Acknowledgements 91
References 91
1 Introduction
Specification logic (Reynolds, 198la, Reynolds, 1982) is a formal system
for proving partial-correctness properties of programs in ALGOL-like lan-
guages (Reynolds, 198lb) with higher-order procedures. It is essentially a
many-sorted first-order theory with phrase types such as comm (command),
exp[int] (integer expressions), var[int] (integer variables), and assert (asser-
tions) as the sorts, and Hoare triples {P} C {Q} as atomic formulas. (The
"top-level" formulas, such as Hoare triples, are termed specifications, whereas
the pre and post-condition formulas P and Q in the Hoare-triple specification
{P} C {Q} are termed assertions.)
First appeared in Information and Computation, 107(1):25-57, 1993. Reprinted with the permis·
sion of Academic Press.
P. W. O’Hearn et al. (eds.), Algol -like Languages
© Springer Science+Business Media New York 1997
66 Chapter 14. Semantical Analysis of Specification Logic, 2
Assignment:
gv(V} & V#P => {P(E)} V:= E {P(V)}
where gv(V) is a specification asserting that V:var[T] is a good variable,
P:exp[T]- assert (i.e., a procedure whose arguments are expressions for
T values and whose calls are assertions), and E: exp[ T] (i.e., an expression for
values of data type T). The consequent here is essentially like Hoare's axiom,
but the good-variable and non-interference hypotheses prevent unsound infer-
ences when V is a bad variable or when assignments to V can indirectly affect
the truth value of the pre and post-conditions. For example, theinvalid spec-
ifications discussed above are not derivable because, respectively, y#x falls
(when x and y are aliased), y#f(2) falls (when y is a non-local variable in the
definition of f), and gv(a[a[i]]) falls.
In fact, Reynolds defined the specification gv(V) as follows:
Vp:exp[T]- assert. Ve:exp[T].
V#p => {p(e)} V:= e {p(V)}
(where identifiers p and e are not free in V). In effect, this defines a good
variable to be one for which Hoare-style reasoning is possible whenever there
is no inappropriate interference.
A second role for non-interference formulas is to allow reasoning about
invariance properties of phrases, as in the following axiom:
Constancy:
C#R & ({R} => {P}C{Q}) => {PandR}C{QandR}
Non-Interference Composition:
C#E & (X#E • {P} C {Q}) • {P} C {Q}
where {... , c,, .. .} includes all identifiers with free occurrences in C, and
{... , eJ. ... } includes all identifiers with free occurrences in E. Essentially, this
axiom asserts that all channels of interference are named by identifiers; in
particular, C#E when either C or Eis closed.
This decomposition can be refined by taking into account that all phrases
of certain types are passive, in that they cannot interfere with anything, and
that all phrases of some types are chaste, in that they cannot be interfered
with. Then, C # E whenever C is a phrase of passive type or E is a phrase of
chaste type.
Finally, there is a way to discharge good-variable and non-interference as-
sumptions about locally declared variables; for clarity, we present it as an
inference rule (Reynolds, 1981a), but in natural-deduction format:
Local-Variable Declaration:
[ v#E~~~:~#Em ]
C1#v, ... ,Cn#v
{P} C {Q}
{P} new[T] v. C {Q}
the axiom {true} diverge {false}, and the usual axioms for ü and sequencing
to derive
gv(x) & x#f ~ {f(1) = 2}
ü f(l) = 2 then
(x := 1; ü f(x) = 2 then diverge)
{false}
and then the desired specification follows by use of the Local-Variable rule.
We may now turn to an informal consideration of the semantics of non-
interference. A satisfactory interpretation of C # E was presented in (Tennent,
1990) for the cases when Cis a variable or command and Eis an expression or
assertion; however, procedural phrases were problematical. Reynolds treated
procedural phrases by interpreting C # E syntactically, using an induction on
types. Consider the following procedure-calllaws, which can be derived from
Non-lnterference Decomposition: if C: (}- (}' and identifier cisnot free in C
orE,
C#E ~ (Vc: 9. c#E ~ C(c)#E),
and, if E: (}- (}' and identifier eisnot free in C or E,
C#E ~ (Ve: 9. C#e ~ C#E(e) ).
Essentially, these assert that if a procedure does not interfere with (or, re-
spectively, is not interfered with by) a phrase, no call of the procedure will do
so unless the argument does. Reynolds replaced the main ~ connective by
70 Chapter 14. Semantical Analysis of Specification Logic, 2
is true and
P(if i > 0 then i := i + 1) # (i > 0)
is false. In short, non-interference is not a "logical" relation (Plotkin, 1980,
Statman, 1985); nevertheless, weshall show that a very satisfactory interpre-
tation is possible.
The interpretation of C#E in (Tennent, 1990) (for the case that C:comm
and E: exp[ T]) is based on comparing
• executions of C that are restricted to subsets of the set of states for which
the value of E is invariant, with
• unrestricted executions of C.
If the restricted executions are less defined than the corresponding unre-
stricted executions, this difference is attributable to C interfering with E. The
new approach to be presented in detail in this paper generalizes this to arbi-
trary phrases C by comparing
• unconstrained uses of C.
[rr]fj j[O]f
y [rr]y [X]rre(y) [ 8 ]y
[9]gj 1[9']g
[6]Z p(f; g) [6']Z
The morphism part of [6- 6'] is defined as follows: for any f:X ...!... Y,
p E [6- 6']X, and g: Y ...!_. Z,
[6- 6']fpg = p(f ;g).
S is the contravariant functor from X to D suchthat S(X) =X, discretely-
ordered, and S(f, Q) = f. The functors for types exp[T] and assert are es-
sentially of the form S-V, for appropriate constant functors V, but the
contravariance of S forces minor modifications as follows. For any world X,
e E [exp[Tl]X is a family e( ·) of functions, indexed by X-morphisms, such
that e(f:X...!... Y) is a function from S(Y) to the flat domain of T-values, and
the family satisfies the following uniformity condition: for all f: X ...!... Y and
g:Y...!... Z,
e(f; g) = S(g) ; e(f),
Peter W. O'Hearn and Robert D. Tennent 73
which is commutativity of
e(f)
S(Y) v~
S(g) 1 lidv,
e(f; g)
S(Z) v~
where v~ is the "lifted" domain of T-values. Notice the reversal in the direction
of the vertical arrows; however, the morphism part of [exp[T]] is defined
to make it covariant as follows: for any f:X ....!... Y, e E [exp[T]]X, and
g:Y ....!_. Z,
[exp(T]]feg = e(f;g).
The definition of [assert] is similar, but with the set {true, false} of truth val-
ues replacing V~.
For any world X, c E [comm]X is a family of partial functions, indexed
by X-morphisms with domain X, so that c(f:X....!... Y) is a partial function
on S(Y). The uniformity condition on the family is the following "semi-
commutativity" requirement: for all f:X....!... Y and g: Y....!... Z,
c(f; g); S(g) s;; S(g); c(f),
where the s;; relation is graph inclusion of partial functions:
_;__ _ _ y
_ _ _c(f)
y
S(g)1 2 1 S(g)
--
z - - -c(f; g)
--z
The semi-commutativity allows command meanings to become less-defined in
more-restricted worlds; however, the family must also satisfy the following
commutativity requirement arising from the equivalence-class component of
X-morphisms. For any (f, Q):X....!... Y and y E S(Y), Iet
Y' = {y' E S(Y) I y Qy'}
(the set of states Q-reachable from y); then
c(f, Q) _
y _ ___;:_...:_:c;__ _.. y
S([Y') 1 1s([Y')
c( (f, Q); rY')
Y' --------'---+- Y'
must commute (and not just semi-commute). This ensures that, when it is
defined, c(f, Q) preserves the Q-equivalence dass of its argument. Of course,
this is also the case when Y =X and (f, Q) is an endomorphism, such as lR
for any equivalence relation R on X.
74 Chapter 14. Semantical Analysis of Specification Logic, 2
The morphism part of [comm] is defined as follows: for any f:X 2.... Y,
c E [comm]X, and g: Y 2.... Z,
[comm]fcg = c(f;g).
3 Semantics of Non-lnterference
In this section, we define relations # ~ C(X) x E(X) for arbitrary functors
C,E:X- D and world X, and then use these to interpret non-interference
specifications in specification logic.
Two key ideas underlie our new treatment of non-interference. The first
is that the use of state-change constraint endomorphisms in X allows us to
constrain semantical entities to executing within equivalence classes of states
(without changing worlds). If c E C(X) and R is an equivalence relation on
X, C( lR) (c) is a restriction of c such that, when c causes a state change from
so to s1 for which so R s1 falls, C (l R )( c) diverges. Because C (l R )( c) and c are
both elements of C(X), they can be compared for equality.
For example, if R; is the equivalence relation on X that relates states for
which variable i has the same value, and u is any appropriate environment (in
which i denotes a good variable),
[comm] ( lR;) ([i := O]Xu) [ü i * 0 then diverge]Xu
* [i := O]Xu.
An attempt to change the value of i in an execution constrained by R; leads to
divergence.
As an example of the effect obtained with procedural phrases, consider
again
P = .\c:comm.ü i = 1 then (c; ü i > 1 then i := 0).
Let fJ = comm- comm; by naturality of [P], we have the commutativity of
[ rr ]X _....;:[:.....P..::.c]rr"-'e_X_ _.. [ fJ]X
[rr](IR,)j j[O](IR;)
The second key idea is that, for any functor E: X - D, world X, and
e E E(X), we can define an equivalence relation [e] s;; X x X such that, in-
tuitively, s[e]s' if and only if the effect of using ein an expression evaluated
at state s is equivalent to the effect of using e in that expression evaluated at
s'. This is possible because of a result in (Tennent, 1990) which shows that,
in keeping with the principles of ALGOL-like languages in (Reynolds, 198lb),
expressions do not cause side-effects, not even "temporary" ones: the value
of e' E [exp[T]]X at a state s is equal to the value of [exp(T]](f{s})(e') at
s. (The map f {s} precludes side-effects because there are no other states in
{s} to change to!) This means that whenever an arbitrary e E E(X) is used in
an expression evaluated at s, only the restricted part E(f{s})(e) E E({s}) is
relevant.
To define the desired equivalence relation, we should compare trans-
formed meanings of the form E(f{s})(e) and E(f{s'})(e). To obtain mean-
ings in the same domain, we note that, in the category X of possible worlds,
there is exactly one morphism s' ...... s from the world {s'} to {s}, and it is an
isomorphism. Therefore, the map
E({s'}) E(s' ...... s) E({s})
is an isomorphism, and E(f {s'}; s' ...... s) (e) is a meaning at the world {s}, but
the values of (non-local) variables are evaluated as if the state were s'.
We therefore define the relation [e] as follows:
s[e]s' = E(f{s})(e) = E(f{s'} ;s' ...... s)(e)
In the case that X = 0, [e] is the empty binary relation. It is Straightforward
to verify that [e] is an equivalence relation, and the following can be proved
by a routine calculation.
Proposition 1 Forany(unctorE:X-D, morphismf:X 2... Y, andex E E(X),
l[ex] ; f = f; l[ey] = l[ex] ; f; l[ey ],
where ey = E(f)(ex).
The above definition of (e] is fairly abstract. A more concrete definition
could be used to interpret specification logic; we illustrate this with two sam-
ple cases.
• IfE=[exp[T]],s[e]s'ifandonlyife(idx)(s) = e(idx)(s').
• If E = [exp[T]- exp[T]], s[e]s' if and only if, for all a E [exp[T]]X,
s[e(idx) (a) ]s' whenever s[a]s'.
We will work with the abstract definition because it leads to simpler and more
general proofs without induction on types.
We may finally define # s;; C(X) x E(X) for any functors C,E:X- D and
world X as follows:
c#e =
C(l[e])(c) = c
The notations c#e and [e] do not explicitly indicate the relevant functors
(C, E) or object (X), but thesewill always be clear from context.
76 Chapter 14. Semantical Analysis of Specification Logic, 2
I
We can now apply our definition of non-interference to the interpretation
of specification logic. We first introduce the following syntax rule:
Non-Inter(erence Sped{ication:
TT f- C: (} TT f- E: (}'
rr r- C#E:spec
where rr is a phrase-type assignment and (} and (}' are arbitrary phrase types
(other than spec). This syntax rule assumes there are no coercions. If a
de-referencing coercion were allowed, it would be necessary to use type sub-
scripts on # to disambiguate cases when the C or E could be either a variable
or an expression.
Now, for any worlds X and Y, environment u E [rr]X, and morphism
f:X2...Y,
[C#E]Xuf = ([9](f)([C]Xu) # [9'](f)([E]Xu))
Proposition 2 may be used to prove that [C#E]Xu E [spec]X, and it is then
routine to show that [C#E] is a natural transformation from [rr] to [spec].
In the following sections, we use this interpretation to show the validity of the
axioms discussed in Section 1.
4 Non-lnterference Decomposition
We first consider separately the left and right operands of the # operator, and
then account for phrases that, because of their types, either cannot interfere
with, or cannot be interfered with by, any phrase.
Proposition 3
(a) For any E:X- D, world X, and e E E(X),
ifc E l(X) then c#e.
(c)
C' (! [e]) (17(X) (c)) 11 (X)( C (l [ e]) (c)) (naturality of 11)
17(X) (c) (c#e)
I
We can show the validity of the leftside-decomposition axiom above as
follows. Assurne that, for some environment u E [ rr ]X, u (c;) # e for all c; free
in C, where e = [E]Xu. Let rr' be the restriction of rr to the c; and u' E [rr']X
be the similar restriction of u. lf Cis closed, u' E 1 (X) and so u' # e by part (a)
of the proposition; otherwise, u' # e by repeated use of part (b). By part (c)
and naturality of [C], it follows that ([C]Xu') #e. By the usual "coherence"
property of valuations, we have that [ C]Xu = [ C]Xu', and we are done.
(b) For any C,E1.Ez:X- D, world X, (e1, ez) E (EIXEz)(X), and c E C(X),
i{c#e1 andc#ez thenc#(ei,ez).
78 Chapter 14. Semantical Analysis of Specification Logic, 2
c
(c) We first prove the following lemma: if s[e]s' then S[IJ(X)(e) ]s'.
E'(Us})(IJ(X)(e))
IJ({s})(E(Us})(e)) (naturality of 17)
IJ({s})(E(f{s'} ;s' ..... s)(e)) (s[e]s')
E' (f {s'}; s' ,_ s) (IJ(X)(e)) (naturality of IJ)
Then
C( l[IJ(X)(e)]) (c)
C( l [17(X) (e)]) (C( l [e]) (c)) (c#e)
C( l[e]; l[IJ(X)(e)]) (c) (functoriality of C)
c( l([e] n [17(X)(e)])) (c) (composition in X)
C(l [e]) (c) (lemma)
c (c#e)
I
Validation of the rightside decomposition axiom follows the same pattern as
that for leftside decomposition in the preceding sub-section, and is omitted.
4.3 Passive and Chaste Types
The decomposition axioms in (Reynolds, 1982) take into account the fact that
certain phrases are not "command-like" and cannot cause interference, or are
not "expression-like" and cannot be interfered with. Our treatment of these
issues here is closer to the approach used in (Reynolds, 1978).
A phrase type 8 is termed passive just if c # e for every functor E: X - D,
world X, c E [O]X, and e E E(X). Intuitively, a phrase having a passive type
cannot interfere with anything.
Peter W. O'Hearn and Robert D. Tennent 79
Proposition 5
(a) val[T] (Tennent, 1990), exp[T], and assert are passive.
Proof:
(a) val[T] is passive because [val[Tl] is a constant functor. We prove the
result for exp[T]; the proof for assert is similar. For any f:X .....!.. Y,
e E E(X), and c E [exp[T]]X,
[exp[T]]( He]) (c)(f)
c(l [e]; f) (morphism part of [exp[TJ])
c(f; l[E(f) (e)]) (Proposition 1)
c(f) (uniformity for [exp[TJ])
(b) Consider any worlds X and Y, p E [0- (}']X, g:X .....!.. Y, a E [O]Y,
and ex E E(X). Let ey = E(g)(ex ); then
[0- O'](t[ex])(p)(g)(a)
p(Hex] ;g)(a) (morphism part of [0- (}'])
p(g; l[ey ])(a) (Proposition 1)
[0']( l[ey]) (p(g; t [ey])a) ((}' is passive)
p(g; l[ey]; t[ey ])([O]( t[ey ])a) (uniformity for [ (} - (}'])
p(g; t[ey])([O](t[ey])a) (Proposition 1 with f = idy)
[0']( t[ey]) (p(g)(a)) (uniformity for [ (} - (}'])
p(g)(a) ((}' is passive)
I
Similarly, a phrase type (} is termed chaste just if c # e for every functor
C:X- D, world X, c E C(X), and e E [O]X. Intuitively, a phrase having a
chaste type cannot be interfered with by anything.
Proposition 6
(a) If[O] is (isomorphic to) a constant functor, then (} is chaste.
Proof: We will use the following fact: a functor F:X- Dis isomorphic to a
constant functor iff F (f) is an isomorphism and F{f) = F (f'), for all worlds
X
X, Y and maps f,f':X - Y. (This fact depends on X being a connected
category.)
80 Chapter 14. Semantical Analysis of Specification Logic, 2
(A -B)(p9a
p(f; 9)(a) morphism part of A-B
k- 1(k(p(f; 9) (a))) k ; k- 1 is an identity
k- 1(p(f; 9; !z) (A(!z) (a))) uniformity for A - B
k- 1(p(f'; 9; !z) (A(!z) (a))) f ; 9; !z = f' ; 9; !z
k- 1(k(p(f' ;9)(a))) uniformity for A - B
p(f'; 9Ha) k; k- 1 is an identity
(A-B)f'p9a morphism part of A - B
where !y: Y 2.... 0 and !y': Y' ....!_. 0 are the unique morphisms with
co-domain 0, and B(!y' )- 1 is the inverse of the isomorphism B(!y' ); this
defines q. That q and (A-B) (f) areinverse isomorphisms can then be
proved by a Straightforward calculation. 1
Because [val[Tl] is a constant functor, the proposition shows that val[T] and
all phrase types of the form 61 - ( Oz - · · · - (On - val[ T]) · · ·) are chaste.
1t can be proved that these are the only chaste types according to the interpre-
tation of types given in (Tennent, 1990), but we have not been able to prove
the conjecture that 6 - 6' is chaste for any interpretation of types such that
6' is chaste.
Note that if the states functor S were covariant, [exp[Tl] = S - [val[T]]
would be a constant functor and exp[T] would be chaste! But the proposition
only applies to covariant functors, and S is contravariant In fact, [ exp[ T]] is
not constant and, as expected, exp[ T] is not chaste. This discussion perhaps
gives a partialjustification for the contravariance of the states functor in (Oles,
1982) and (Tennent, 1990).
Peter W. O'Hearn and Robert D. Tennent 81
The definitions of passive and chaste types justify the following axiom: if
rr 1- C: e and rr 1- E: 0', C # E whenever e is passive or (}' is chaste. We can
now use the rule of free substitution to derive the following "practical" form
of Non-Interference Decomposition:
· · · & c; # ei & · · · => C # E
where {... , c;, ... } is the set of identifiers having at least one free occurrence
in C that is outside of any sub-pbrase of passive type, and {... , ei, .. .} is the
set of identifiers having at least one free occurrence in E that is outside of any
sub-pbrase of chaste type. Identifier occurrences are themselves sub-pbrases
and so no c; has passive type and no ei has chaste type. Also, C and E are sub-
pbrases of themselves, and so this includes the special case that C # E when C
has a passive type or E has a chaste type.
In (Reynolds, 1982) commands are regarded as pbrases that cannot be
interfered with, whereas comrn is not, in our terminology, chaste. But, to
prove that pbrases of type comrn cannot be interfered with, a tedious struc-
tural induction over the entire language is necessary; if, for example, a block-
expression form, as in (Tennent, 1986), were added to the language, the result
would fail. Our definitions of passivity and chastity are expressed in terms of
general properties of (the semantics of) types themselves, rather than merely
capturing properties of pbrases in a specific language.
5 Local-Variable Declarations
If an identifier of pbrase type e is free in the block
new[T] x.C
then its meaning c' E [O](XxV) in the body oftheblock is [O](xV)(c) for
some c E [O]X, where xV:X-...!.. XxVisanexpansion morphism. That is, c'
comes from a non-local world X, so it should be independent of the V-valued
component of XxV used to hold the value of the local variable.
On the other hand, the meaning v' of identifier x in C is concemed entirely
with the V-valued component, and should be independent of X. One way to
view this is to consider v' as coming from the world V, just as c' comes from
X, and so v' should not depend on X at all. That is, v' E [var[TJ](XxV)
arises from some meaning v E [var[TJ]V as
v' = [var[T]](xX;V-X)(v)
where xX: V -...!.. VxX ; note the exchange resulting from use of
V ......... X: VxX __!_. XxV.
I
In the empty world, c # v trivially, because the only endomorphism on the
empty set is the identity.
Wehave shown that local variables (however these might be defined, so
long as they are truly local) do not interfere with non-local entities, and vice
versa. To validate the Local-Variable Declaration axiom, it remains only to
show that a local variable is a good variable; a proof of this may be found
Peter W. O'Hearn and Robert D. Tennent 83
in (Tennent, 1990), and the proof is still valid for our new interpretation of
non-interference.
Similarly, the remaining axioms of the system described in (Reynolds,
1982) were validated in (Tennent, 1990) and remain valid with our new in-
terpretation of non-interference because of the following result, which shows
that, at base types, our new interpretation of # is equivalent to that in (Ten-
nent, 1990).
Proposition 8 If rr f- C: comm and rr f- E: 9, then, for all worlds X and Y,
u E [rr]X and f:X 2.... Y, [C#E]Xuf if and only if, for all g: Y 2.... Z and
Z E Z,
if[C]Xu(f; g)z = z' then [C]Xu(f; g; f Ze)z = z'
where Ze = {ze E Z I Ze[e]z} fore = [O](f;g)([E]Xu).
z' c(idz)z
[comm](l[e])(c)(idz)z (c#e)
c( l [e])z (morphism part of [comm])
c( l [e]; f Ze)Z (commutativity for [comm])
[comm]( l[e]) (c)( f Ze)Z (morphism part of [comm])
c<fZe)Z (c#e)
Examplel
Consider the block new[T] x.C, where Cis a free identifier of type comm.
This block is equivalent to C in our model, (without additional constraints
on command meanings, contrary to the remark in (Tennent, 1990). To show
the equivalence, consider the X-morphism f = (g, Tx):X x VT ....!.. X such
that g(x) = (x, vT) for each x e X, where vT is the initial value for T-valued
variables. Let c e [comm]X and s e X. If c(idx)(s) is defined and equal tos',
then c(xVT; f)(s) = s' because xVT; f = idx. The uniformity condition on
elements of [comm]X then implies that c(xVT)(s, vT) = (s', vT):
c(idx)
x---~;:..;._--..x
S(xVTll 2 fs(xVTl
c(xVT)
X X VT ---'-.:...:.....--...X X VT
X
·I 2
c(xVT ;f)
X------'--'----+- X
I·
i.e., c(xVT )(s, vT) has the same effect on the X-valued component of the stack
as does c(idx). In the case that c(idx)(s) is undefined, the uniformity condi-
tion guarantees that c(xVT)(s, vT) is also undefined. The desired equivalence
follows.
Example2
The following blocks are equivalent:
new[int] x.x := 0;
new[int] y.y := 0;
Q(x)(y)
and
new[int] x. x := 0;
new[int] y.y := 0;
Q(y)(x) •
where Q is a free identifier of phrasetype var[int]- (var[int]- comm).
The equivalence can be shown by a Straightforward calculation using an en-
domorphism that exchanges the two V -valued components in a world of the
form XxVxV, thus exchanging the declared variables.
Example3
Our final equivalence is subtler than the previous two; the only model known
to us which validates it is the one presented here.
The block
new[int] x.x := 1;
P(x) ,
Peter W. O'Hearn and Robert D. Tennent 85
I
p(idxxv) (e) (idxxv)
X X V _ __:_:.__:.:..:..:..:._.:__:.__.:__:._-=..:::..:.:...:._._ X xV
S([Y,) fs([Y,)
p(idxxv) (e) ( rYe)
Ye Ye
In particular, if s is astatein which e evaluates to 1, then Ye is the set of states
in which e evaluates to 1. In the restricted world Ye, the meaning of expression
1 is equal to e-both are (families of) functions that return 1 in any state-so
e can be replaced by 1 in the call top. The equivalence follows.
A further equivalence for local-variable declarations is discussed in the
following Section.
6 Non-Interference Abstraction
When writing a program P ( C) in which there is used an operation C that ma-
nipulates instances of an abstract type, it is often convenient to view the pro-
gram from the level of abstraction where C is considered as an indivisible
action. If R is an invariant of C and P doesn't affect R except by using C, then,
at this Ievel of abstraction, R is true throughout execution of P(C) if it is true
initially. In (Reynolds, 1981a), this is the intuitive justification for a method of
reasoning in which invariant relationships, like R, are used as "local" mathe-
matical facts when reasoning about an abstract program.
This kind of reasoning is similar to that provided by the Constancy axiom,
in that an assertion is factored out by treating it as a local mathematical fact;
however, Constancy would require a hypothesis of the form P(C) #R, which
would not be justified:
• at the level of the implementation of C, R might be false at an interme-
diate state because we only assume {R} C {R}; and
• even if C were indivisible, we would not necessarily have P(C)#R, be-
cause C is required to preserve only the truth, and not the falsity, of
R.
In this section we present a new axiom that also uses a local assumption, but
in a way that does not require it to be true at the level of concrete operations.
The new axiom is as follows.
86 Chapter 14. Semantical Analysis of Specitication Logic, 2
Non-Inter(erence Abstraction:
P#R & {R}C{R} & {Q}C{D} &
(Vt:comm. {R} & {Q}L{D} ~ {A}P(t){B})
~{R andA} P(C) {R andB}
where P: comm - comm, C: comm, and L is not free in P or in the assertions
A,B,R,D, or Q. The idea isthat if P#R, {R} C {R}, andR is true before execut-
ing P(C), then, throughout the execution of P(C), R should hold continuously
at the Ievel o( abstraction at which C is considered to be an indecomposable
action. When treating R as a mathematical fact, it is essential to confine all rea-
soning about P(C) tobe at this same level of abstraction, because R might ac-
tually be false at an intermediate state of the execution of C. Non-lnterference
Abstraction ensures that reasoning is carried out at this level by using an iden-
tifier L which, unlike C, doesn't have any "parts" to give us information about
intermediate states. Furthermore, the meaning of L is arbitrary and, for all we
know, could be that of an indivisible action because it is universally quantified.
The specification {R} is essentially a local "atomicity" assumption.
A number of examples of reasoning that can be formalized by this axiom,
or minor variations thereof, can be found in Chapter 5 of (Reynolds, 198la).
Typically, the assertion R describes an invariant relationship between the con-
crete values of a representation and the abstract values being represented,
and C is an operation that manipulates the values in a way that preserves the
invariant.
We will use a special case of the axiom to show that the following block is
equivalent to diverge:
new[int] x.
x:= 0;
p(x := x + 2);
if evenx then diverge
where p is an identifier of type comm - comm. This example is from (Meyer
and Sieber, 1988). Intuitively, the block diverges because the only access to
x that can result from the call to p must come from using the argument
x := x + 2. This argument preserves the evenness of x and x is even before
the call, so it should be even on termination. Therefore, the test in the condi-
tional will succeed (if the call to p terminates) and the blockwill diverge; if the
call does not terminate, neither will the block, and so in every case the block
diverges.
We will derive {true} C {false}, where Cis the block above. First, note that
the following scheme is a special case of Non-Interference Abstraction:
P#R & {R} C {R} ~ {R} P(C) {R}
An instance of this is
p#evenx & {evenx} x := x + 2 {evenx}
~ {evenx} p(x := x + 2) {evenx}
Using the usual axioms and mathematical facts about numbers, we can then
derive
gv(x) & p # x .,. {true}
x:= 0;
p(x := x + 2);
if even x then diverge
{false}
and the desired specification follows from the Local-Variable rule.
Notice that this development would continue to go through if we were to
replace x := x + 2 with x := x + 1 ; x := x + 1, even though this latter command
does interfere with evenx. The reason is that Non-Interference Abstraction
requires only {R} C {R}, and not C#R.
The derived axiom used in this derivation is related to the principle
of "preservation of invariants" discussed in (Meyer and Sieber, 1988). It
says essentially that R is a "higher-order invariant" of P whenever P#R.
This axiom can be generalized to P's whose arguments are procedures
that accept "expression-like" parameters, but we have been unable to val-
idate further generalizations to procedures of higher-order types such as
(comm- comm) - comm.
To validate Non-Interference Abstraction, we adopt a slight refinement
of our model. Consider the modified category of worlds X' in which maps
X'
(f,R): X - Y aresuchthat
• R is a reflexive and transitive (but not necessarily symmetric) relation on
Y,and
• f e Y - Xis a function such that, for each s e Y, the restriction of
f to the set Ys = {s' E Y I s R s'} of states reachable from s via R is an
injection.
The injectivity requirement on morphisms is needed for the definition of local
variables, as in (Tennent, 1990). The definition of composition of X-morphisms
given earlier also works in X'.
The definition of command meanings needs to be modified slightly:
c( ·) e [comm]X is again a suitably uniform family of partial functions, but
the additional commutativity requirement is that, for all (f,R): X ..l!!..... Y and
s E Y,
c(f,R)(s) = c((f,R); rYs)(s),
where Ys = {s' e Y I s R s'}, so that the following diagram commutes:
c(f,R)
~y y
tsl Js([Ys)
~)
S(r {s} Ys
c((f,R); rYs)
Ys
[comm]X is ordered pointwise; i.e., c1 !;;;; c2 if and only if, for all f:X ..l!!..... Y
and s e Y, c1 (f) (s) = c2 (f) (s) whenever c1 (f) (s) is defined. All of our other
definitions and results go through as before.
88 Chapter 14. Semantical Analysis of Specification Logic, 2
Proposition 9
(a) For all worlds X, c E [comm]X, and q, d, r E [assert]X, if {r} c {r}
and {q} c {d}, there exists c' E [comm]X suchthat c !;;; c', c' # r, and
{q} c' {d}.
if c # r then c # r.
(b) Note that [r] !;;; (r), and so l [r] = l[r]; l (r).
(c) [O'](l(r)}(p(idx)(c)}
p(! (r)) ([0] (! (r)) (c)) (uniformity for [0- 0']
p(l (r) }(c) (c # r)
[0- 0'] ( l (r)) (p) (idx) (c) (morphism part of [0- 0'])
p(idx )(c) (p # r)
I
We can now validate Non-lnterference Abstraction. Let c E [comm]X,
p E [comm- comm]X, and q,r,a,b,d E [assert]Xbe the meanings of C, P,
Q, R, A, B, and D, respectively, in an environment u E [rr]X. Suppose
p(idx)(c)(idx)(sl) = s2.
If the conditions in the antecedent of Non-lnterference Abstraction hold, then,
by Proposition 9(a), there exists c' E [comm]X suchthat
(1) p(idx)(c')(idx)(si) = s2, by monotonicity of p(idx),
7 Concluding Remarks
The main contribution of the work described here has been to provide a uni-
form semantic definition of Reynolds's concept of non-interference for higher-
arder imperative programming languages. This has been used to validate all
of the axioms proposed by Reynolds (except of course the unsatisfactory syn-
tactic definitions of procedural non-interference which our semantic interpre-
tation replaces), and also a new axiom formalizing a method of reasoning used
in (Reynolds, 1981a). Furthermore, the interpretation of local-variable deda-
rations in our model has been shown to validate a number of equivalences
proposed by Meyer and Sieher (1988), as well as an additional equivalence
which seems to fall in other models.
These results vindicate virtually all of Reynolds's intuitions about local-
variable declarations and non-interference, and also, we believe, provide a
decisive demonstration of the usefulness of the functor-category form of
possible-world semantics for imposing "local" constraints on semantic enti-
ties in a systematic way. Further evidence of this may be found in (O'Hearn,
1990) where the "symmetric" form of non-interference from (Reynolds, 1978)
is also treated using possible worlds.
However, we should mention that the category-theoretic credentials of
the "contra-exponentiation" (used to define covariant functors from the con-
travariant states functor) are undear at present. Also, we do not believe that
all of the principles underlying local-variable declarations have been uncov-
ered.
For example, the following variant of Example 7 from (Meyer and Sieber,
1988) fails in all models known to us:
new[int] x.
x:=O;
P(x :=X+ 1)
whereas
p(xV)([x := x + 1](X x V)(u'))(idxxvHs,O)
is undefined, where u and u' are the appropriate non-local and local environ-
ments, respectively.
Our definition of non-interference can also be applied with continuation
semantics, as in (Tennent and Tobin, 1991). The procedural instances of
the Non-lnterference Decomposition and Variable-Declaration axioms take on
added importance in the continuation framework because commands them-
selves become higher-order objects mapping continuations to continuations.
The proofs given here for these axioms continue to go through, as they don't
depend on the interpretations of base types (such as comm). However, it
should be noted that our treatment of Non-Interference Abstraction is much
less uniform-the proof of its validity relies on the structure of [comm]-and
the status of this axiom in continuation semantics is unresolved at present.
The fixed-point induction axiom for recursive "command-like" procedures
from (Reynolds, 1982) is valid here with minor changes to the syntactical for-
mulation of admissible predicates; our interpretation of C # E is admissible
in C, but is not continuous (for truth-values ordered by true !;;;; false), and so
admissibility clauses for implication and quantification need to be adjusted.
A more difficult problern is presented by recursive "expression-like" proce-
dures. We speculate that a version of Scott's "logic for computable functions,"
(Scott, 1969, Milner, 1972, Milner et al., 1975, Statman, 1990), could be used in
the assertion language, so that assertional quantification could coexist with the
continuity properties needed for computational types. Boolean expressions
would be three-valued, whereas assertions would be properly two-valued. This
proposal seems similar to (Goerdt, 1985), but is in a richer semantical frame-
work that allows a form of side-effect-free block expression (Tennent, 1986).
It must be admitted that the functor-category form of interpretation ap-
pears somewhat remote from computational realities, and it would be desir-
able to show the "computational adequacy" of the denotational model rela-
tive to a suitable operational semantics. We believe this would be a relatively
Straightforward exercise along the lines followed for the symmetric form of
non-interference in (O'Heam, 1990). A more interesting and ambitious project
might be to develop a form of operational semantics that could be systemati-
cally derived from denotational possible-world models.
Acknowledgements
We would like to thank Prakash Panangaden, Edmund Robinson, and John Reynolds
for helpful technical discussions, Steve Brookes for his careful reading and comments
on an earlier version of this work (O'Hearn, 1990), and the referees for their comments.
References
BARRINGER, H., CHENG, ]. H., AND }ONES, C. B. (1984). A logic covering undefinedness
in program proofs. Acta Informatica, 21:251-269.
DAMM, W. AND ]OSKO, B. (1983). Asound and relatively*-complete Hoare-logic for a
language with higher-type procedures. Acta Informatica, 20:63-92.
92 Chapter 14. Semantical Analysis of Specification Logic, 2
GERMAN, S. M., CLARKE, E. M., AND liALPERN, J. Y. (1989). Reasoning about procedures
as parameters in the language 1..4. Information and Computation, 83:265-359.
GoERDT, A. (1985). A Hoare calculus for functions defined by recursion on higher
types. In (Parikh, 1985), pages 106-117.
HOARE, C. A. R. (1969). An axiomatic basis for computer programming. Comm. ACM,
12(10):576-580 and 583.
HOARE, C. A. R. (1971). Procedures and parameters: an axiomatic approach. In Sym-
posium on Semantics of Algorithmic Languages (E. Engeler, editor), volume 188 of
Lecture Notes in Mathematics, pages 102-116, Springer-Verlag, Berlin.
MEYER, A. R. AND SIEBER, K. (1988). Towards fully abstract semantics for local vari-
ables: preliininary report. In Conference Record of the Fifteenth Annual ACM Sym-
posium on Principles of Programming Languages, pages 191-203, ACM, New York.
See Chapter 7.
MILNER, R., MORRIS, L., AND NEWEY, M. (1975). A logic for computable functions with
reflexive and polymorphic types. In Proving and Improving Programs (G. Huet and
G. Kahn, editors), pages 371-394, INRIA, Rocquencourt, France.
MILNER, R. (1972). Implementation and applications of Scott's logic for computable
functions. In Proc. of an ACM Con(erence on Proving Assertions About Programs,
SIGPLAN Notices, 7(1) and SIGACT News, no. 14, pages 1-6, ACM, NewYork.
O'HEARN, P. W. (1990). The Semantics of Non-lnter(erence: A Natural Approach. Ph.D.
thesis, Queen's University, Kingston, Canada.
OLDEROG, E. (1984a). Hoare's logic for programs with procedures: what has been
achieved? In Logics of Programs 1983 (E. M. Clarke, Jr. and D. Kozen, editors),
volume 164 of Lecture Notes in Computer Science, pages 383-395, Springer-Verlag,
Berlin, 1984.
OLDEROG, E. (1984b). Correctness of programs with PASCAL-like procedures without
global variables. Theoretical Computer Science, 30:49-90.
OLEs, F. J. (1982). A Category-Theoretic Approach to the Semantics of Programming
Languages. Ph.D. thesis, Syracuse University, Syracuse, N.Y. See Chapter 11.
OLES, F. J. (1985). Type algebras, functor categories and block structure. In Algebrate
Methods in Semantics (M. Nivat and J. C. Reynolds, editors), pages 543-573, Cam-
bridge University Press, Cambridge, England. See Chapter 11.
PARIKH, R., editor (1985). Logics of Programs 1985, volume 193 of Lecture Notes in
Computer Science, Springer-Verlag, Berlin.
PLOTKIN, G. D. (1980). Lambda-definability in the full type hierarchy. In To H. B. Curry:
Essays in Combinatory Logic, Lambda Calculus and Formalism U. P. Seldin and J. R.
Hindley, editors), pages 363-373, Acadeinic Press.
REYNOLDS, J. C. (1978). Syntactic control of interference. In Conference Record of
the Fifth Annual ACM Symposium on Principles of Programming Languages, pages
39-46, ACM, New York. See Chapter 10.
REYNOLDS, J. C. (1981a). The Craft of Programming. Prentice-Hall International, Lon-
don.
REYNOLDS, J. C. (1981b). The essence of ALGOL.lnAlgorithmic Languages, Proceedings
of the International Symposium on Algorithlnic Languages U. W. de Bakker and
J. C. van Vliet, editors), pages 345-372, North-Holland, Amsterdam. See Chapter 3.
REYNOLDS, J. C. (1982). IDEALIZED ALGOL and its specification logic. In Tools and Na-
tions for Program Construction (D. Neel, editor), pages 121-161, Cambridge Uni-
versity Press, Cambridge, 1982. See Chapter 6.
Peter W. O'Hearn and Robert D. Tennent 93
Contents
1 lntroduction 97
2 Syntax of the Language ALG 98
3 A Cartesian Closed Category 99
4 The Denotational Model 101
5 Reasoning About Local Variables 103
6 Fuß Abstraction 104
7 Conclusion 106
References 106
1 Introduction
This paper solves a long-standing open problern concerning the semantics of
local variables. We present a denotational model for an ALGOL-like language,
ALG, which is fully abstract for the second-order subset of ALG. This means in
particular that all the problematic observational congruences for ALGOL-like
languages that have been presented in the Iiterature [MS88, Len93, OT95] can
be validated in our model. (The latter is also true for the parametric-functor
model in [OT95], but no full-abstraction result has been proved for it.)
The general technique which we use for our model construction has al-
ready been developed in [MS88], namely 'relationally structured locally com-
plete partial orders' with 'relation-preserving locally continuous functions'.
Our partiewar model differs from the one in [MS88] by having the 'finest pos-
sible relation structure', an idea which we have used in [Sie92] to construct a
fully abstract model for the second-order subset of PCF [Plo77]. The overall
structure of our full-abstraction proof is also taken from [Sie92]. The first step
is to show that, for every function fand every finite set B of arguments for f,
there is a definable function which coincides with f on B (Theorem 11). This
yields a sequence of definable functions which 'approximate' f in the sense
that they coincide with f on more and more arguments. But for proving full
First appeared in Mathematical Foundations of Computer Science, volume 841 of Leerure Notes
in Computer Science, pages 608-617, Kösice, Slovakia, August 1994. Springer-Verlag, Berlin.
Reprinted with the permission of Springer-Verlag.
the view that call by name should be the main parameter passing mechanism
for ALGOL-like languages [Rey81]. Besides that, we have parameters of type
loc (= 'location') which may be considered as reference parameters. They have
been added as a mere convenience, because we anyways need identifiers of
type loc as local variables.
The set of ALG-constants c and the type of each constant are
n: iexp for every n E 7L (numerals)
succ: iexp- iexp (successor)
pred: iexp- iexp (predecessor)
cont: loc- iexp (dereferencing)
asgn: loc - iexp - cmd (assignment)
skip: cmd (empty command)
conde: iexp-0-0-0 (conditional with zero test)
seq6 : cmd-0-0 (sequencing)
newe: (loc- 0)- 0 (new-operator)
Yu: (0"- 0")- 0" (fixed point operator)
pcond: iexp - iexp - iexp - iexp (parallel conditional with zero test)
We assume that there is an infinite set IdT of identifiers xT, yT, zT, ... for each
typeT; identifiers of type loc are called variables. Expressions M,N,P, ... are
just the well-typed .\-expressions over the ALG-constants with the restriction
that the body of a .\-abstraction must not be of type loc. A block with a local
variable x has the form new x in M and is considered as syntactic sugar for
new6 (.\x 1oc.M) where 0 is the type of M. As further syntactic sugar we use
! _, _ := _, if _ then _eise _, and _; _ instead of cont, asgn, conde and seq6 • A
program is defined to be a closed expression of type iexp.
For purely technical reasons we introduce so-called generalized expres-
sions. Let Loc be an infinite set whose elements 1 are called locations. General-
ized expressions may contain (besides the other ALG-constants) locations 1 as
constants of type loc. We use the samemetavariables M,N,P, ... for them as
for ordinary expressions. We let locns(M) denote the set of locations which
occur in M, and for every finite set L !:;;; Loc we let Expi denote the set of closed
generalized expressions with locns(M) !:;;; L.
T which only have access to LI and then define [ T] as the union of these dcpos
[T]r. This motivates the following definition.
Definition 1 Let (W s) be a directed set (of worlds w ).
I
(a) A W-locally complete partial order (W-lcpo) isapartial order (D1 ~) to-
gether with a family of subsets (Dw )wew such that D = Uwew Dw andl
forallvl w E W,
• V S W => Dv !;;;; Dw
• if 6 !;;;; Dw is directedl then Uv 6 exists and is contained in Dw ( hence
it is also the lub in Dw, i.e., (Dw 1 ~) is a dcpo)
(b) A function f: D- E between W-lcpos D and E is called locally continuous
if(f I Dw) E (Dw ...E... Ew) (or everyw E W.
W-lcpos and locally continuous functions form a Cartesian closed categoryl
which can be considered as a full subcategory of the functor category
(W => OCPO). But this is not yet the category that we need for our model
construction; we must still add 'relation structure to the W-lcpos.
1
text. A term T(d, el ... ) containing vectors Jl el ... of the same length n stands
for (T(dt. e1o ... ), ... , T<dn. en •... ) ). This notation is generalized as usual to
sets of tuplesl i.e.l relations: if Rl S are relations of the same arity nl then
T(RI SI ... ) Stands for {T(d, el ... ) I JE Rl e E SI ... }. Finallyl onD denotes the
diagonal {(dl··· ld) I d E D}!;;;; vn.
Definition 3 Let~ be a W-sorted signature.
(a) A W-~-lcpo isapair (DII), where D is a W-lcpo and I is a function that
maps every r E ~n to a relation I(r) !;;;; vn such that, for all w E W,
• r E ~w => onDw!;;;; I(r)
• I(r) n v: is closed under lubs of directed sets.
(b) A function f:D- E between W-~-lcpos W~I0 ) and (E 1 1E) is called a
~-homomorphism iff(J0 (r)) !;;;; IE(r) for all r E ~.
Theorem 4 The category W-~-LCPO of W-~-lcpos and locally continuous
~-homomorphisms is Cartesian closed. The terminal object and product are
defined worldwise, and the exponentiation (D - E) of two W-~-lcpos D and E is
given by
(D -E)w = {f:D -EI 'Vv ~ w.(f IDv) E (Dv ...E... Ev)
1\ 't/r E ~w.f(J0 (r))!;;;; JE(r)}
This is the category in which we will define our denotational model. It has
a certain similarity to the category of 'parametric functors and (parametric)
natural transformations' as defined in [OT95]. One difference is that we use
relations of arbitrary arity, and not only binary ones as in [OT95]. But-on the
one hand-the parametric-functor model can be extended to contain relations
of arbitrary arity and-on the other hand-binary relations might be sufficient
for reasoning about local variables (Conjecture 16). Thus our full-abstraction
proof may well be transferable to (an appropriate extension oO the model in
[OT95].
{
.L ifs=.L {
1. if d = 1. or s = 1.
Cont 1s = s l otherwise Asgn 1d s = s[d/1] otherwise
where next: Pr(Loc) - Loc is an arbitrary function with next(L) f/:. L for every
L E Pr(Loc) and supp(d) is defined tobe the set {LId E [T]r} for every n
d E [T]. The functions [c] are indeed contained in the model; more precisely,
Proposition 5 Jf c is a constant of type a, then [ c] E [ a] 0.
The latter Statement captures our intuition that a closed generalized expres-
sion has only access to those locations that explicitly occur in it, and not to
those that are temporarily bound to its local variables.
We finally remark that the particular choice of l in the clause for [newcmd]
does not play a role; i.e., instead of next(supp(f)) we can use any other loca-
tion l E Loc \ supp(f). Thus we obtain for every l E Loc \ supp([.\x.M]'1)
[newx in M]11 s
_ {([M](I1[l/x])(s[O/l]))[s lfl], if [M]('1[l/x])(s[O/l]) * .L
- .L, otherwise
This possibility to choose the new location l freely from an infinite set is an
important 'reasoning principle' which we will use in the following.
(b) fs * .L ~ fs l = s l
(c) SI =L s2 ~ (fsi = .L = fs2 v (fsi,fs2 E Stores A fsi =L fs2))
Proof: Each of the three properties is proved by choosing an appropriate
RE ~Land exploiting the fact that fRsto!;;;; Rsco. For (a), we take RE DEF~ with
Rsto = {.L}; for (b), we take R E DEFfll with Rsto = {.L} u {t E Stores I t l = s l};
and for (c), we choose some L' E W with Ln L' = 0 and SI =Loc\L' s2 and take
RE DEFf with Rsto = {.L} 2 u {f E Stores 2 I ti =Loc\L' t2}. I
104 Chapter 15. Full Abstraction for the Second-Order Subset
6 Full Abstraction
Notation: lf u = T t - · • · - Tk- sto => 8 (k ~ 0) and f E [u], then we let fd
denote the completely decurried version of f, i.e.,
fd: [TI] X · · · X [Tk] X Dsto- D 6 with fd (dt, ... , dk, S) = fdt · · · dk S
and ü p E ([u]...!.... [u]), then we let pD denote the corresponding function on
the completely decurried versions, i.e.,
pD: [u]d - [u]d with pDfd = (pf)d
The first step towards full abstraction is
Theorem 11 Letu = T t - · · · -Tk- (} (k ~ 0) with ord(u) :s; 2. Letf E [u]r
and Iet B ~ [ T 1 ] x · · · x [Tk] x vsro be finite. Then there is some M E Expf. with
[M]d =s fd.
From Theorem 11 we can obtain a sequence of definable functions that 'ap-
proximate' f in the sense that they coincide with f on more and more argu-
ment tuples. But for a full-abstraction proof we need approximations 'from
below'.
Definition 12
(a) Let D,E be sets, F ~ (D ...!.... E) and p E (F ...!.... F); B ~ D is called a
determining set for p, if p f is uniquely determined l1y f I B; i.e., if f = B g
implies p f = p 9 for all f, 9 E F; p is called finitely determined if it has a
finite determinin9 set.
(b) Let u be a procedure type and Iet L E W; an L-FD-sequence on u is a
sequence of expressions Pn E Expf-u such that [Pn ]D I ( [u 1L )d is finitely
determined for every n E ~ and ([Pn])neN is an w-chain whose lub is the
identity on [u]; u is called an L-FD-type if an L-FD-sequence exists on u.
Kurt Sieber 105
If the elements of [a]t were just functions from [cmd]t to [cmd]t, then
we could prove-by a similar argumentation as above-that [p~md][cmd]t x
[PJr0 ]StoresL is a (finite) determining set for [PJ[]D 1 ([a]L)d. But of course
this assumption is wrong and indeed it can be shown that [PJ[]D 1 ([a]L)d
does not have any finite determining set. Somewhat to our own surprise, a
slight modification of the expressions PJ{ suffices to solve this problem. Let
Pg = J..yU.J..zcmd.
newx inx := O;P~d(y (if !x < n then x := !x + 1;P~dz else !l))
106 Chapter 15. Full Abstraction for the Second-Order Subset
7 Conclusion
Wehave already mentioned that the parallel conditional is irrelevant for our
full-abstraction proof: we obtain an analogous full-abstraction result for se-
quential ALG if we remove the function Pcond from AUX and then proceed as
before. The resulting model allows us to prove additional semantic equiva-
lences like
[y skip 0. + y 0. skip] = [y 0. 0. + y 0. 0.] (with y: cmd - cmd - iexp)
which is a variant of the famous PCF-equivalence [Plo77, Sie92]. For the proof
of this equivalence one needs a ternary relation as in [Sie92] (no binary relation
suffices), and similar examples show that even relations of arbitrary arity n
are needed. The situation for ALG itself (with pcond) seems to be different.
Although we have used relations of arbitrary arity n in our full-abstraction
proof, there is strong evidence that (even very particular) binary relations are
sufficient:
Conjecture 16 Theorem 14 remains valid, ifwe use a smaller signature! for
our model construction, namely !~ = OUT~ and !~ = 0 (n * 2).
Finally the question arises, whether our result can be generalized to types
of order ~ 3. We conjecture that neither our model nor the model in [OT95]
are fully abstract for these higher types: reasoning about local variables is
closely related to the question of .\-definability (intuitively a global procedure
acts on a local variable like a pure .\-term), and it follows from [Loa93] that-at
least over finite ground types-.\-definability for functions of order ~ 3 cannot
be characterized with the aid of (logical) relations. As all the above models are
based on relations, it seems unlikely that one of them be fully abstract for
types of order ~ 3. Hence our result seems the best one may expect in the
current state of the art.
References
[Len93] Arthur F. Lent. The category of functors from state shapes to bottomless
cpos is adequate for block structure. In Proc. ACM SIGPIAN Workshop on
State in Programming Languages, Copenhagen, Denmark, pages 101-119,
Technical report YALEU/DCS/RR-968, Department of Computer Science, Yale
University, 1993.
[Loa93] Ralph Loader. The undecidability of .\-definability. Mathematical Institute,
Oxford University, June 1993.
Kurt Sieber 107
[MS88] Albert R. Meyer and Kurt Sieber. Towards fully abstract semantics for local
variables: Preliminary report. In Proc. 15th Annual ACM Symp. on Princi-
ples o( Programming Languages, San Diego, pages 191-203, ACM, 1988. See
Chapter 7.
[OT95] Peter W. O'Hearn and Robert D. Tennent. Parametricity and local variables.
Journal ofthe ACM, 42(3):658-709, 1995. See Chapter 16.
[Plo77] Gordon D. Plotkin. LCF considered as a programming language. Theoretical
Computer Science, 5:223-256, 1977.
[PS93] Andrew M. Pitts and Ian D. B. Stark. Observable properties of higher order
functions that dynarnically create local names, or: What's new? In Andrzej M.
Borzyszkowski and Stefan Sokolowski, editors, Proc. 18th International Sym-
posium on Mathematical Foundations o( Computer Science, Lecture Notes in
Computer Science, volume 711, pages 122-141, Springer, 1993.
[Rey81] John C. Reynolds. The essence of ALGOL. In J. deBakker and van Vliet, editors,
Int'l. Symp. Algorithmic Languages, pages 345-372. North-Holland, 1981. See
Chapter 3.
[Sie92] Kurt Sieber. Reasoning about sequential functions via logical relations. In
M. P. Fourman, P. T. Johnstone, and A. M. Pitts, editors, Applications o( Cate-
gories in Computer Science, London Mathematical Society Lecture Note Series
177, pages 2 58-269. Cambridge University Press, 1992.
[Sie96] Kurt Sieber. Full abstraction for the second order subset of an ALGOL-like
language. Theoretical Computer Science, 1996. To appear.
[WF93] Stephen Weeks and Mattbias Felleisen. On the orthogonality of assignments
and procedures in ALGOL. In Proc. 20th Annual ACM Symposium on Princi-
ples of Programming Languages, Charleston, pages 57-70, ACM, 1993. See
Chapter 5.
Chapter 16
Parametricity and Local Variables
Peter W. O'Hearn and Robert D. Tennent
We propose that the phenomenon of local state may be understood in
terms of Strachey's concept of parametric (i.e., uniform) polymorphism.
The intuitive basis for our proposal is the following analogy: a non-local
procedure is independent of locally-declared variables in the same way
that a parametrically polymorphic function is independent of types to
which it is instantiated.
A connection between parametricity and representational abstraction
was first suggested by J. C. Reynolds. Reynolds used logical relations
to formalize this connection in languages with type variables and user-
defined types. We use relational parametricity to construct a model for
an ALGOL-like language in which interactions between local and non-local
entities satisfy certain relational criteria. Reasoning about local variables
essentially involves proving properties of polymorphic functions. The
new model supports Straightforward validations of all the test equiva-
lences that have been proposed in the literature for local-variable seman-
tics, and encompasses standard methods of reasoning about data repre-
sentations. It is not known whether our techniques yield fully abstract
semantics. A model based on partial equivalence relations on the natural
numbers is also briefly examined.
Contents
1 lntroduction 110
2 Types 116
2.1 Syntax 116
2.2 Semantics 117
2.3 Recursion 119
3 Properties of Types 120
3.1 Basic Properties 120
3.2 Expansions 122
4 Valuations 123
5 Examples of Reasoning 128
6 Algebraic Aspects of First-Order Types 131
7 Relations and Reflexive Graphs 134
8 Parametrie Functors and Natural Transformations 139
9 When Parametricity lmplies Naturality 143
10 The PER Model 148
10.1 Store Shapes 149
10.2 Realizable Functors and Natural Transformations 150
10.3 Naturality and the Groupoid Interpretation 152
11 Conclusion 156
Acknowledgements 159
References 160
Appeared in]. ACM, 42(3):658-709, May 1995. © 1995 Association for Computing Machinery,
reprinted with the permission of the ACM. A preliminary version appeared in Con{erence Record
of the Twentieth Annual ACM SIGPLAN-SIGACT Symposium on Principles o{ Programming Lan-
guages, pages 171-184, Charleston, South Carolina, 1993. ACM, New York.
1 Introduction
One of the first things most programmers learn is how to "declare" a new
assignable local variable, arid facilities to support this have been available in
programming languages for over thirty years [Naur et al., 1963]. It might be
thought that there would by now be a satisfactory semantic interpretation for
so fundamental and apparently elementary a mechanism. But existing models
arenot completely satisfactory [Meyer and Sieber, 1988; O'Hearn and Tennent,
1992]. The problems arise when block bodies can contain calls of non-local
procedures, and the difficulty is in defining precisely the sense in which non-
local entities are "independent" of a locally-declared variable.
For example, consider the following (ALGOL 60) block [Meyer and Sieber,
1988]:
begin
integer z;
procedure inc; z := z + 1;
P(inc)
end
Although the unknown non-local procedure P can use its argument to change
the value of z, this value can never be read, and so the block should be equiva-
lent to P(skip), where skip does nothing, for every possible meaning of P. But
this equivalence falls in all previous denotational models of local variables!
The reader's reaction to this example might be that it is contrived, and that
it has no practical significance; after all, who would ever write such a program?
But consider the following slightly more complicated example:
begin
integer z;
procedure inc; z := z + 1;
integer procedure val; val := z;
z:= 0;
P(inc, val)
end
The local variable, the two procedure declarations, and the initialization
can be considered as constituting the concrete representation of an abstract
"counter" object. Procedure P, the "client," is passed only the capabilities
for incrementing and evaluating the counter, and cannot access the counter
representation in any other way. A more modern language would provide a
"sugared" syntax, and one could write something like
module counter(expons inc, val);
begin
integer z;
invariant z <::: 0;
procedure inc; z := z + 1;
integer procedure val; val := z;
z:= 0
end counter;
... counter. inc; ... counter. val . ..
Peter W. O'Hearn and Robert D. Tennent 111
but the unsugared form shows that, even without additional features, the com-
bination of local variables and procedures in ALGOL-like languages supports
a form of representational abstraction, which is one of the main themes of
modern programming methodology. (In fact, the same example is used in the
Appendix of [Reynolds, 1978) to make the same point.) See [Reynolds, 1981b;
Tennent, 1991) for discussion of ALGOL-like languages, and [Reynolds, 1975;
Cook, 1991) for camparisans of linguistic approaches to representational ab-
straction.
To a certain extent, the relevance of representational abstraction to the
semantics of local variables has already been exploited. The models described
in [Meyer and Sieber, 1988; O'Heam and Tennent, 1993b) support validation of
invariance principles often used for reasoning about data representations, as
in [Haare, 1972). For example, these models validate the following equivalence:
begin
integer z;
procedure inc; z := z + 1;
integer procedure val; val := z;
diverge
z := 0;
P(inc, val);
ü z ~ 0 then diverge
end
where diverge is a statement whose execution (in any state) never terminates.
Because P can be any procedure (of the appropriate type), the equivalence
demonstrates that z ~ 0 is an invariant of the counter representation; i.e.,
z ~ 0 is true before and after every call of inc from P.
But there is more to representational abstraction than preservation of this
kind of representation invariant. Consider the following block, which uses a
"non-standard" representation of a counter:
begin
integer z;
procedure inc; z := z- 1;
integer procedure val; val := -z;
z := 0;
P(inc, val)
end
This block should be equivalent to the block that uses the "standard" repre-
sentation. The equivalence illustrates the principle of representation indepen-
dence: one concrete representation of a data abstraction should be replaceable
by another, provided the relevant abstract properties are preserved; see, for
example, [Mitchell, 1986). It is clearly important to be able to validate changes
of representation; but existing semantic models of local variables almost al-
ways fall on such equivalences!
This failure is especially surprising because standard informal methods
for demonstrating correctness of data representations [Haare, 1972)[Reynolds,
1981a, Chapter 5) can easily be adapted to proving such equivalences. For our
example, consider the relation R between states for the two implementations
112 Chapter 16. Parametricity and Local Variables
such that, if z 0 and Zt are the values of the variable z in the standard and non-
standard implementations, respectively, R holds if and only if -z1 = z0 ~ 0
and all other variables have the same values. 1t can be shown that
• R is initially established by executing the two initializations (with iden-
tical non-local states);
• executions of (the two implementations of) inc preserve R; and
• evaluations of (the two implementations of) val in R-related states yield
the same result.
The conclusion is that R holds after execution of the calls toP, and so the
blocks have been "proved" to have equivalent effects on non-local variables.
But, although there is no reason to think these methods are invalid, they have
never been rigorously verified for a language with local-variable declarations!
This discussion of data abstraction motivates our link with the concept of
parametricity, introduced by Strachey [1967] in the following remarks:
There seem tobe two main classes [of polymorphism], which can be
called ad hoc polymorphism and parametric polymorphism.
In ad hoc polymorphism there is no single systematic way of deter-
mining the type of the result from the type of the arguments. There may
be several rules of limited extent which reduce the number of cases, but
these are themselves ad hoc both in scope and content. All the ordinary
arithmetic Operators and functions come into this category. It seems,
moreover, that the automatic insertion of transfer functions by the com-
piling system is limited to this dass.
Parametrie polymorphism is more regular and may be illustrated by
an example. Suppose f is a function whose argument is of type or and
whose result is of type ß (so that the type of f might be written or -. ß),
and that L is a list whose elements are all of type or (so that the type of
L is or list). We can imagine a function, say Map, which applies f in turn
to each member of L and makes a list of the results. Thus Map[f, L] will
produce a ßlist. We would like Map to work on all types of list provided
f was a suitable function, so that Map would have to be polymorphic.
However its polymorphism is of a particularly simple parametric type
which could be written
(or ". ß, oclist) ". ßlist
where oc and ß stand for any types.
Although a complete understanding of the ramifications of this notion of
parametricity is not yet available (cf., [Freyd et al., 1992b; Plotkin and Abadi,
1993)), Reynolds [1974, 1983] has emphasized the close relationship with rep-
resentational abstraction. The idea is that a parametric polymorphic function
must work in a way that is independent of the types to which it is instanti-
ated. For instance, (in the absence of recursion) the only parametric elements
of type Voc. oc - oc- oc are the two functions with two arguments that return
either the first argument or the second argument, respectively. On the other
band, a function that would return its first argument when instantiated to a
function on integers, and its second otherwise, is not parametric because it
works differently at different types. Intuitively, a parametric function cannot
Peter W. O'Hearn and Robert D. Tennent 113
I I
[rr]Wo --=-[P.....::]:......W.-"-
0 -+- [O]Wo
[rr ]R (8]R
Notice that double-headed arrows - - - are used here for (binary) relations,
and that this is not a conventional commutative diagram.
The connection between logical relations and polymorphic functions em-
phasized by Reynolds is that if the above relation-preservation property is to
hold in a polymorphic language, then values of V-types must be constrained
so as to satisfy similar relation-preservation conditions. 1n our model for local
variables, function typeswill themselves have a polymorphic flavour, and will
be constrained by such a parametricity condition.
Relational parametricity is commonly thought to prescribe necessary prop-
erlies that parametric functions must satisfy. What is less dear is whether,
particularly in the binary-relation form, it is sufficient to characterize the in-
tuitive concept. Another appealing approach to parametricity, possessing
a fairly coherent conceptual basis, uses partial equivalence relations (PERs);
e.g., [Longo and Moggi, 1991]. 1n the PER approach, polymorphic types are
interpreted as infinitary intersections, so that a (realizer for a) polymorphic
function is an untyped meaning that is type-correct for all instantiations of a
type variable. This captures, to a certain (not completely understood) degree,
the intuition that a polymorphic function is given by a uniform algorithm. On
the other hand, the relational approach captures, to a certain (not completely
understood) degree, intuitions about representation independence.
The larger part of our effort in this paper will be directed toward examin-
ing the relational approach of Reynolds. The semantic model we define will
represent quite directly the informal reasoning about local variables and data
abstraction alluded to above. However, we will also briefly outline how PERs
can be used to treat variable declarations. A comparison of the two models
will be given in Section 11.
Our method of incorporating parametricity builds on the functor-category
approach to local variables pioneered by Reynolds [1981b] and Oles [1982,
1985]. 1n the remainder of this lntroduction, we will briefly review the basic
elements of this approach, and indicate how relational parametricity will enter
the picture. (The expository article (O'Hearn and Tennent, 1992] and textbook
[Tennent, 1991] contain introductions to this approach.)
The key insight of the Reynolds-Oles work is that, in a language with local-
variable declarations, the concept of state is not constant-represented by a
single set of states-but rather varies as storage variables are allocated and
de-allocated. That is, there are different possible sets of states depending on
the "shape" of the run-time stack; i.e., the nurober and type of variables that
have been allocated.
Peter W. O'Hearn and Robert D. Tennent 115
rj [ rr lfj j[9]f
[ TT ]W1 1 - - [ 8] W1
--=-[P-::]:-W-
where f: Wo- W1 is a morphism of possible worlds, and [ 8] and [ rr] are type
and environment functors.
Notice that, in many respects, this is similar to the relational-parametricity
picture discussed earlier. Parameterization by possible worlds is roughly anal-
ogous to abstraction on a type variable. In fact, if we think of the possible
worlds as certain kinds of types, then [P] is a family of functions indexed
by these types and so is, in a certain sense, polymorphic. It is therefore cer-
tainly conceivable to require a family of this form to satisfy a parametricity
constraint.
This analogy between possible worlds and type variables suggests how
relational parametricity can be incorporated. We consider binary relations
between worlds, regarded as relating different "representations" of the store
shape, and the semantics of types is then arranged so that each such relation
induces a relation between the meanings of a type at different store shapes.
The meanings of terms are then families of maps satisfying a relational-
parametricity constraint.
The naturality requirements of Reynolds and Oles will not be abandoned.
However, to make the presentation more accessible, we will begin with a
116 Chapter 16. Parametridty and Local Variables
2 Types
2.1 Syntax
Our language is an ALGOL-like language in the sense of [Reynolds, 198lb].
The language does not include jumps or subtypes, but it raises the key issues
related to variable allocation. The types are as follows:
ö ::= int I booll · · · (data types)
ß ::= comm I exp[ö] I var[ö] (primitive phrase types)
o ::= ß I ö- ß (phrase types)
Data types are the types of values that can be stored, while phrase types
consist of meanings that can be denoted by identifiers and, more generally,
program phrases, but that cannot be stored. This distinction allows variable
declarations to obey a stack discipline.
Comm is the type of commands. Executing a command causes a state
change, but does not return a value. Var[ö] is the type of storagevariables that
accept ö-typed values. Exp[ö] is the type of expressions that return values of
type ö. Expressionsare "read only," in that they are state dependent but do
not cause side effects. So all state changes are concentrated in the type comm.
In particular, procedures use by-name parameters and can only change the
state indirectly, when used within a phrase of type comm.
In procedure types, Öis a non-empty vector of phrase types. This "uncur-
ried" formulation of the syntax of procedure types is not essential, but is most
amenable to a category-free description of the model.
Peter W. O'Hearn and Robert D. Tennent 117
2.2 Semanlies
We will regard a binary relation R as a triple (Wo, W1, S) where Wo and W1 are
sets (the domains of R) and S s;; Wo x W1 (the graph of R). Although we will
work exclusively with binary relations, our definitions (though not all of our
notation) generalize straightforwardly to n-ary relations for any n. We will use
Wo
We use W - X and W x X for the function space and product of sets. If Wo,
Wt. Xo, and X1 are sets and R: Wo - W1 and S: Xo ...... X1,
R!w1-r;-x1!s
to indicate that fo[R - S]fi· Notice that this notation makes the do-
mains of the relations, and the domains and codomains of the functions,
evident.
The collection ~ of "store shapes" is a set of sets that includes desired
data types, such as 2 = {true,false} and Z = { ... ,-2,-1,0,1,2, ... }, and all
finite (set) products of these. We won't be more specific about ~. except to
emphasize that it must be a small collection. Foreach data type ö, we assume
a set [ö] in~. with [int] = Z and [bool] = 2.
Following [Reynolds, 1983], we define a "twin" semantics of phrase types,
where each (} determines two functions
[0]: ~-Sets
point of definition of a procedure and the point of call. For example if the 1
an integer variable is declaredl then the shape of the stack for the call will be
wxz~notW.
[Ö](6w X R) 1 1 [ß](6w X R)
[Ö](R X S) 1 1[ß](R X S)
2.3 Recursion
The presentation thus far is for a recursion-free dialect of ALGOL. Recursion
can be dealt with by using domains in place of sets, as follows. (We still use
sets, or discretely-ordered predomains, for the store shapes.)
If D and E are partially ordered sets and R: D - E (i.e., R is a relation on
the underlying sets),
• R.1:D.1- E.1 is defined by d[R.1Je ~ d = e = .L or d[R]e, where D.1
is obtained from D by adding a new least element .L.
If D and E are directed-complete partially-ordered sets then a relation
R:D .......... Eis
• complete, if its graph is a directed-complete subset of the pointwise-
ordered product of the domains of the relation; and
• pointed, if D and E are pointed and R relates their least elements.
120 Chapter 16. Parametricity and Local Variables
3 Properlies of Types
3.1 Basic Properties
We now turn to some basii:: properties satisfied by this semantics. Theseare
all essentially consequences of the polymorphic view of phrase types sketched
in the previous section.
First, as in [Reynolds, 1983], each [8] preserves identity relations.
Lemma 1 (Identity Extension) For each phrase type 8 and store shape W,
[O]~w = ~l91W·
[B](~w x R) 1 1[ß](~w x R)
[O](Llw X R) 1 1[ß](Llw x R)
3.2 Expansions
There is further structure in the semantics that derives from the conception of
elements of I: as representing "shapes" of the run-time stack. Specifically, the
expansion of store shapes caused by variable declarations is accompanied by
mappings that convert semantic entities at a shape W to any expanded shape
WxX.
If W and X are store shapes, for each type 9 we define a function
expand8 (W,X): [9]W- [9](W x X)
This goes by induction on types.
expandcomm<W,X) c (w,x) = (cw ,x)
expandexpc 61 (W,X) e (w,x) = ew
Peter W. O'Hearn and Robert D. Tennent 123
[9]Rl 1[9](RxS)
4 Valuations
Whereas the category-free semantics of types is quite simple, the semantic
equations for terms will turn out to be comparatively complex. This is a pre-
sentation trade-off: the valuations in the categorical semantics given later are
much simpler, but require a more sophisticated interpretation of types.
A type assignment rr is a finite function from (an unspecified set of) iden-
tifiers to phrase types. Some typing rules are in Table 1. The rules are in a nat-
ural deduction format. The rules for abstraction and application are for the
uncurried syntax of types. The pairing construct uses the "object-oriented"
approach to variables. We write write rr 1- M : 9 to indicate that M : 9 is
derivable from (undischarged) assumptions rr.
The example blocks in the Introduction can easily be desugared into this
language. A block begin öx; C end is rendered as new5 (.\x:var[ö).C). We
will arrange matters so new always assigns an initial value to the variable
124 Chapter 16. Parametricity and Local Variables
[x: 6]
skip:comm
[derefV]Wu snd([V]Wu)
[skip]Wus s
[C2]Wu ([CdWus)
Table 2: Valuations
Notlee that g[X]: [Ö'](W x X) - [ß'](W x X), sog is certainly of the right
formtobe in [Ö' - ß']W. It will be shown to satisfy the necessary para-
metricity constraints in the course of proving the Abstraction Theorem below.
The semantics of application is
[M N]Wud = [M]Wug,d
The reader familiar with semantics in functor categories will notice that
expansions come into this uncurried style of presentation in the case of appli-
cation, whereas they appear when treating .\-abstraction when the semantics
is presented in a more conventional curried form.
Peter W. O'Hearn and Robert D. Tennent 127
Proof: The Abstraction Theorem and the well-definedness of [P] are proven
simultaneously by structural induction on P. Well-definedness is immediate
in all cases except application (which is the only case where the simultaneity
is used in a non-trivial way).
For the well-definedness of application, suppose rr r- M : fJ, ii- ß and
rr r- N : fJ. If fJ = ß' is a primitive type then the result is immediate, so
suppose fJ = Ö' - ß'. Well-definedness will be assured if we can show that
the family of functions
9[-]: [Ö'](W X - ) - [ß'](W X-)
satisfies the parametricity condition for [Ö' - ß']W. For S:X0 ....... X1 and
u E [rr]W define u 1 = expand1T(W,X1) u. By the IdentityExtensionLemma and
the Expansion Parametricity Lemma, u0 [ [ rr] (Llw x S)] u 1. (The evident version
of the expansion lemma for type assignments is a corollary of the one for
types.) By the Abstraction Theorem for N (induction hypothesis),
[N](W X Xo) uo[ [Ö'](Llw X S)- [ß'](Llw X S) ][N](W X XI) U1
and so
9[Xo] [ [Ö'](Llw X S)- [ß'](Llw X S) ]9[Xd .
This shows that 9 E [Ö'- ß']W, as desired.
For the Abstraction Theorem, we will consider application and new; all
other cases are routine.
For application we have rr r- M : fJ, ii- ß and rr r- N : fJ. Suppose
uo[[rr]R]ul and do[[Ö]R]d1. If fJ = ß' is primitive then the Abstraction The-
orem for N (induction hypothesis) guarantees that [N]Wo uo[[ß']R][N]Wl u1
and then the Abstraction Theorem for M implies that
[M]Wo uo ([N]Wouo),do[ [ß]R ][M]W1 u1 ([N]W1ud,dl.
In the case that fJ = Ö' - ß' we reason in the same manner, but use
9o [ [ Ö' - ß' ]R] 91. where 91 is the meaning determined by the environment
u,, as in the definition of application. This last property follows from the
Abstraction Theorem for N, with a proof similar to the well-definedness of 9
above using R in place of Llw.
For new, suppose Po[[var[ö] -comm]R]Pl and w0 [R]w1. We must show
that
fst(po[[ö]] (ao, eo) (wo, 8)) [ R ]fst(pl[[ö]] (a~. e1) (w~. 8)) .
The key property is (ao, eo) [ [var[ö]](R x Llr.sJ)] (a1, e1) for the new variables
(ao,eo) E [var[ö]](Wo x [6]) and (a1,e1) E [var[öl](W1 x [6]). This is
128 Chapter 16. Parametricity and Local Variables
5 Examples of Reasoning
In each of the examples that follow, an unknown non-local procedure is passed
a limited capability for accessing a local variable, in much the same way that an
abstract type gives to its "clients" a limited capability for accessing its repre-
sentation. The reasoning method employed involves choosing a relation that
is satisfied by different arguments to the procedure, and then applying the
parametricity property to infer a relational property that pairs of procedure
calls must satisfy.
For the sake of readability, we continue to use sugared notation for code
in the examples. The desugarings into the language of the previous sections
should be clear.
We begin by describing a dass of relations that can be used in several
examples. Suppose W is any store shape and E s;; Z, where, as before, Z is the
set of integers; we can then define RE: W ---- W x Z by
w[RE)(w',z) <= w = w' and z E E.
This means that the first component of p [ Z] ( inc) (w, z} is equal to p * (skip) w.
Clearly, then, the semantics of variable declarations ensures the first equiva-
lence considered in the Introduction:
begin
integer z;
procedure inc; z := z + 1; P(skip)
P(inc)
end
1t is important here that w = w' when w[RE](w', z): the parametricity prop-
erty always acts as the identity relation on non-local variables. This is where
the identity extension lemma and the use of identities in the parametricity
constraint on procedure types come into play.
We would like to emphasize that the reasoning method in this example is
simply an instance of reasoning about polymorphic functions using Reynolds
parametricity [Reynolds, 1983); seealso [Wadler, 1989) for numerous exam-
ples of this form of reasoning. The equivalence reduces to the following prop-
erty
fst(p[l](.\x.x)) fst(p[Z](.\y. (fsty, (sndy) + 1))}
for a polymorphic function p: 'Vy. (oc x y - oc x y ) - (oc x y - oc x y). This is
what we mean when we say that reasoning about local variables often amounts
to proving properties of polymorphic functions. Of course, it is fairly signif-
icant that the polymorphism that we are concemed with is predicative in na-
ture; but the point remains that the reasoning method we employ is exactly as
in [Reynolds, 1983; Wadler, 1989). These methods will be seen below to lead
to remarkably Straightforward validations of previously troublesome equiva-
lences.
Before continuing, it is worth pausing to explain why typical counterexam-
ples to this equivalence, which exist in previous models, are not present here.
Let W = {true, false}. One counterexample is essentially a family of functions
p[X]: [comm](W x X) - [comm](W x X)
suchthat
[X]( )(b x) = { (--.b,x}, if c(b,~} =1= (b,x}
P c ' (b,x), othel'Wlse
Such a p would break the equivalence, because the left-hand block would
negate the state (which consists of a single boolean), whereas P(skip) would
leave the state unchanged. However, this p falls to satisfy the parametricity
condition, for though skip[[comm]Rz]inc and w[R](w,z), it is not the case
that
p* (skip)(b) [ Rz ]p[Z](inc)(b, z} ,
as p* (skip)b is b, while p[Z](inc)(w, z) is (--.b, x}. The equality test on states
is the culprit in the definition of p: any two states (w,z) and {w,z'} are "in-
distinguishable" from the point of view of the second domain of the relation
Rz, so branching on the equality test violates parametricity.
Our second example demonstrates that the invariant-preserving properties
of the models described in [Meyer and Sieber, 1988; O'Heam and Tennent,
130 Chapter 16. Parametricity and Local Variables
end
Our last example using relations of the form RE is
begin
integerz;
z:= 0; P(O)
P(z)
end
where P: exp[int] - comm; we have left the de-referencing coercion (deref)
from var[int] to exp[int] implicit in the argument of the call. The intuition
here is that the value of z will be 0 each time it is used during execution of the
call P(z), because P cannot write to z. Therefore, this should be equivalent to
simply supplying 0 as an argument instead of z.
To validate this we can use RIO)· The denotation of 0 is the constantly 0
function in [exp](W x Z), and the denotation of z, as an expression, is the
projection W x Z - Z. Thesedenotations are then related by [exp]R{o!; i.e.,
w -----=0=----- z
R1oJ! !~z
Wxz-----"z--Z
because if argument states are related, the Z-valued component is always 0.
We can then use the parametricity of P, as in the other examples, to conclude
that (the denotations of) the calls P(O) and P(z) are [comm]R 1o1-related, and
the equivalence follows from the valuation for variable declarations.
Next we consider a relation that does not fit into the RE pattem: the rela-
tion ~w x R, where R: Z - Z is defined by
Zo[R]ZI = -Zl = Zo ~ 0.
This can be used to validate the equivalence between blocks that use non-
negative and non-positive implementations of a counter in exactly the manner
discussed in the lntroduction. The representations of the procedures inc and
val are directly related by ~w x R, and we can use the parametricity property
of procedures to conclude that the calls to non-local procedure P are related.
This implies the desired equivalence because the semanlies of new disposes
of the Z -valued component of W x Z on termination, and we are left with
~w-related results.
Peter W. O'Hearn and Robert D. Tennent 131
and wehave
{c1, c2}[[comm]ßoc x R]{ci, cons}
where here we are using {f,g} : A-B x C to denote the tupling function
obtained from f :A-B and g : A - C. Then if p E [comm- comm]oc we
get that
p[Z] c (s, z) [ ßoc x R ]p[list[oc)] {ci, cons} (s, €) ,
Let Vnat be the vertical natural numbers, i.e., the natural numbers with the
usual "less than" order, and with an extra top element oo. VnafP is the vertical
naturals with the ordering reversed. Then
[comm-comm]l _ N.J. ® VnafP
where ® is the smash product.
An outline of the proof of this isomorphism is as follows. Using the iso-
morphism 1 x oc ~ oc, a meaning in [comm- comm]l will be a family of
continuous functions
p[oc]: (oc- oc.J.)- (oc- oc.J_)
satisfying the parametricity condition. If p[N] succ 0 = .L then p corresponds
to (.L, oo). If p[N] succ 0 = n then there will be a smallest m such that
p[N] (succ[n + m]) 0 = n, where (succ[k)) a = a + 1 if a < k and .L oth-
erwise. In this case p corresponds to (n, m). The desired isomorphism can
then be shown using parametricity with an argument similar to the one used
by Plotkin for the Church numerals: we define an appropriate relation that
relates an argument in oc- oc.J. to a succ[k].
As before, every element of this domain can be defined by a closed term,
with the appropriate boolean tests and a term diverge: comm that denotes the
constantly-.L function. Specifically, (.L, oo) is defined by diverge, and (n, m) is
defined by
.\c: comm. if (do cn+m result 1) = 1 then c" else skip
The test (do cn+m result 1) = 1 will converge, and return true, iff cn+m con-
verges. The skip branch never gets executed.
It is now possible to appreciate the role of VnafP. lt concems "lookahead,"
in the sense that we look to see if n + m executions of c will converge and, if so,
we execute c n times. This illustrates how a semantics that properly captures
single threading could perhaps lead to simpler representations. For example,
the closed terms of type comm - comm definable without do - result - are,
semantically, in correspondence with N.J., which is considerably simpler than
N.J. ® VnafP.
• The "vertex" category, Sv, is the usual category of sets and functions.
• The "edge" category, Se, has binary relations on sets as objects and
relation-preserving pairs of functions as morphisms; i.e., a morphism
with domain R: Wo ........ W1 and co-domain S:Xo ........ X1 is a quadru-
ple (R, ( 0 , (1, S) such that fo [R - S)f1 . We will use the relational-
fo
R! !s
Wo---Xo
parametricity diagram to depict such a morphism. The
w1-----r;--x1
fo 9o
R! !S S! !
Wo---Xo Xo-Yo
composite of and T is then defined as
fo;go
!T,
Wo-~..:::..::..-Yo
w1 ---=---
f1;91
Y1
idw0
Wo-Wo
is R! !R.
w1 --=----d
1 w1
w1
and
S6; ( R! !s
Wo .J!!._ Xo )
= (; .
w1AX1
• Functor S1:Sv - Se is such that S1(W) = ßw and SI(f: W - X)
w_t_x
ßw! !ßx
w~x
Category Se is the category of relations over sets presented in [Ma and
Reynolds, 1991]. Furthermore, the S6; are similar to the forgetful functor U
used there in a categorical treatment of the (first-order) "abstraction theorem,"
and S 1 is similar to the functor J used there in a categorical treatment of the
"identity extension lemma." Hence, some of the key entities introduced in [Ma
and Reynolds, 1991] are incorporated in the reflexive graph S.
As our second example, we define a reflexive graph 2> (domains) as follows.
• 2>v is the category of directed-complete partially-ordered sets and con-
tinuous functions.
• 2>e has complete binary relations as objects, and relation-preserving
pairs of continuous functions as morphisms. Composites and identities
are evident.
• The functors 2>6;:2>e -2>v for i = 0,1 and 2>1:2>v -Ve are defined
exactly as for S.
Finally, we define a reflexive graph W (worlds) having the small category
l: of "state shapes" described in [Oles, 1982] as its vertex category Wv. The
category l: is as follows.
• The objects are (certain) sets, including desired data types, such as
{true, false} and {-2, -1, 0, 1, 2, ... }, and allfinite (set) products of these.
• The morphisms from W to X are all pairs 4>, p such that
- 4> is a function from X to W;
- p is a function from W x X to X, where the x here (and throughout
this example) is the set-theoretic Cartesian product;
- for all X E X,
p(4>(x),x) = x;
- for all XE X and W E W,
4>(p(w,x)) w;
- for all XE X and W, w' E W,
p(w,p(w',x)) p(w,x).
138 Chapter 16. Parametricity and Local Variables
Wo
~
______2__... Xo
A<fu>
Fo(wo) ----=-.:....::..:...-Fo(xo)
wl - f i xl Fo(wt) Fo(xt)
F1(f1)
The first three conditions say that F0 and F1 constitute a conventional functor
from Wv to Sv; the next two conditions say that Fo and F2 constitute a "re-
lator" [Mitchell and Scedrov, 1992; Abramsky and Jensen, 1991]; and the last
condition is a parametricity constraint. This last condition is closely related to
the Expansion Parametricity Lemma and is crucial for function types to behave
properly; e.g., for currying to satisfy relevant parametricity conditions. We will
adopt the usual notational abuse of using a single symbol such as F to denote
all three mappings.
If F and G are parametric functors from W to S, 11 is a parametric natural
transformation from F to G if it maps Wv-objects to Sv-morphisms such that
• for every Wv-object w, 17(w):F(w)- G(w);
l l
F(w) I7(W) G(w)
We write hx (-) for the parametric functor that sends Y to hxY, f to hidx fand
Rto hAx(R).
Exponentiation is then defined on store shapes as follows:
GF X= Hom8 w(hx xF,G);
on morphisms,
(GF f p)[Z](g, a) = p [Z](f; g, a) ;
and on relations, p[GFR]q iff
VS:Wo- W1.p[Wol[hRS x G(S)- F(S) ]q[Wtl
lt is not difficult to show that GF satisfies the functor and relator requirements,
and the condition that a parametric functor send related Wv-morphisms to
related Sv-morphisms.
The application and currying maps are exactly as in presheaf categories.
The application map app:F x GF....:.... Gis app[W] {a,p) = p[W]{idw,a). Nat-
urality follows as usual. To see that it is parametric, assume p 0 [(GF)R]p1 and
Peter W. O'Hearn and Robert D. Tennent 141
idw0
Wo---Wo
ao[F(R)]ai. As Rl !R, we have that (idw0 ,ao)[hRR x F(R)](idw11 ai),
w1 --:----+-d
1 Wt
w1
and
• for every R: Wo - W1,
«exp[ö]))R R -ß[8J.
For commands:
• for every Wv-object W,
«comm))W = w-w;
• for every Wv-morphism (<j>,p):W- X, x EX, and c E «comm))W,
«comm))(<j>,p) c x = p(c(<j>(x)},x),
and
• for every R: Wo ........ W1,
«comm))R = R -R.
For the morphism part what we do is execute c on the small part of the stack,
i.e., c(<j>(x)), and then use p to replace the small part of x with the resulting
final state.
The parametricity conditions on these functors are easily verified. It is
noteworthy that these pointwise definitions are actually isomorphic to what
is obtained by introducing the obvious contravariant "states" functor S and
defining
142 Chapter 16. Parametricity and Local Variables
(exp[Ö]) = S - D[Ö)
(comm) = s-s
using a parametric version of "contra-exponentiation" [O'Hearn and Tennent,
1992], where o D is the constant functor whose object, morphism, and relation
parts always yield D, idn, and lln, respectively. This is an indication of the
effectiveness of the parametricity constraints.
For storage variables:
(var[ö])X ([ö] - (comm)X) x (exp)X
Here, (Ö) is the product of (Oi) for the components of the vector. Of course,
as sw is a ccc we could ignore vectors and interpret procedure types in a
curried syntax: (0- O') = (O')CB).
The interpretations of terms can be given exactly as in [Oles, 1982]. We
have already seen the application and currying maps in the proof of Proposi-
tion 7, and these are exactly as in functor categories. We will define
new: (var- comm) ...:.. (comm)
to indicate how variable declarations are treated. For Wv-object W,
p E (var-comm)W and w E W,
new.s W p w = fst(p[W x [ö]](f, (a,e)}(w,8))
where f: W - W x (ö) is an "expansion" morphism in Wv, 8 E (ö) is the
standard initial value of new variables, and (a, e) E (var)(W x Z) is the new
variable, defined as follows: a(z')(w,z) = (w,z') and e(w,z) = z.
We conclude this section by sketching a more general context for these def-
initions; it can be skipped without loss of continuity. A morphism M: 9 - :K
of reflexive graphs is a pair of functors Me and Mv that map edges to edges
and vertices to vertices in a way that preserves domains and identities; i.e.,
commutes for i = 0, 1.
component-wise.
Notice that a morphism of reflexive graphs is nothing other than a nat-
ural transformation between graphs viewed as functors. Furthermore, what
Peter W. O'Heam and Robert D. Tennent 143
w...!:...wxy....!....x.
Recall that the Isomorphism Functoriality Lemma played an important role in
the category-free semantics. A condition analogaus to it was not needed in the
definition of reflexive graph because of the following result which, it should
be noted, applies to any parametric functor (and not just definable ones).
Proof: Let f = (c/>, p). From the definition of related Wv morphisms we have
w~w w~w
ßw! !R and R! !ßw.
w-r-x x~w
As A is a parametric functor, we obtain
A(idw) A(idw)
A(W) A(W) A(W) A(W)
A(R) 1 1 (ß)(R) .
A(g) 1 1(ß)(g).
A(Y) p[Y] (ß)(Y)
Proof: Consider any g. By the Expansion Factorization Lemma it can be fac-
toredinto acomposite e;i, where e:X-XxWis anexpansionand i:XxW-Y
is an isomorphism. The result will follow if we can show commutativity of
A(X) -----=p-=[_X.:....]- - - (ß) (X)
A(e)J l«ßHe)
p[Xx W]
A(X X W) (ß)(X X W)
The commutativity of the bottom part follows immediately from the Isomor-
phism Correspondence Lemma and the parametricity property for p. We will
show the commutativity of the top part for ß = comm; the other base types
are treated similarly.
Consider anyw E W. DefineRw:X -XxWby x[Rw](x', w') iffx = x' and
l l
X idx X
X e XxW
functor, for any a E A(X) we have that a[A(Rw)]A(e)(a), and so, using the
parametricity of p, we get
p[X] ax [Rw] p[X x W](A(e)a) {x, w)
for any x EX. From the definition of Rw. and of (comm) on morphisms, this
implies that
(comm)(e) (p[X] a) {x, w) = p[X x W](A(e)a) {x, w).
As this argument works for any w E W, the commutativity of the top part of
the diagram follows. I
Note that the naturality constraints in (Ö- ß)X arealso superfluous by this
result, taking A = hx x ( Ö).
We are finally in a position to see where the category-free semantics of
types given earlier comes from. First, in a type Ö - ß we can do away with
all naturality constraints, as these are implied by parametricity. Second, by
the Expansion Factorization Lemma any Wv·morphlsm factors into a "true
expansion" followed by an isomorphism. Further, by the Isomorphism Cor-
respondence Lemma the action of a procedure meaning on the isomorphism
part of such a factor is completely determined by the action of parametric
functors on relations. Thus, when defining a procedure meaning p at store
shape W we do not need to consider all Wv·morphisms out of W, but only the
"true expansions" of the form W- W x X. (An analogous property for certain
functor categories has been observed by I. Stark.)
satisfying the following parametricity condition: for all R:Xo ...... X1,
p[Xo]
A(W X Xo) _......:.....::........::..::...__..... (ß)(W X Xo)
A(Aw X R) l 1 (ß)(Aw X R)
Proof: Let D denote the collection of p's satisfying parametricity. We will set
up an isomorphism f: (ß)AW - D with inverse g. First we have f m [X] =
m[W x X]{e, ·), where e:W- W x Xis the expansion. Conversely, if we
have a map (cf>, p): W - Z then this factors into an expansion followed by an
isomorphism W ...!. W x Y ....!..... Z. Then we set
gp[Z]((cf>,p),a) = (ß)(i) (p[Y](A(r 1 )a))
l l~w
W e WxX
using the fact that ~w x R, for any R, and for e the expan-
W e WxX
sion. That g p satisfies the parametricity condition for (ß)A W follows from
the parametricity condition on p, together with the Isomorphism Correspon-
dence Lemma and the Isomorphism Factorization Lemma. Naturality is then a
result of the Naturality Theorem. Thus we see that f and g are well-defined.
We can show that they areinverse as follows.
g(fm) [Z] ((cf>,p),a) (ß) i (f m [Y] (A(i- 1 )a))
«ß» i (m [W x Y] (e, (AW 1 )a)))
m [Z] ((e; i), a)
m[Z] ((cf>,p),a)
where the second-last step uses naturality of m and the fact that i and ;- 1 are
inverse isos. Conversely, the definitions of f and g give us
f (gp)[X] a = gp [W X X]((e:W- W X X),a)
= «ß> i (p[X]a)
and in the factorization of e (in the last step) we can take i as the identity
(because e; id = e), so f (g p)[X]a = p[X]a. I
Thus we see that the calculation of (the object part of) function types in the
category-free semantics is isomorphic to what is obtained from exponentiation
in the parametric-functor semantics. It is also not diffi.cult to see that the
relation parts of the two semantics are isomorphic, and that the expand maps
correspond to the morphism parts of parametric functors. Furthermore, the
semantics of .\-abstraction and application that were given are precisely those
obtained (after suitable uncurrying) from the Cartesian closed structure of S w.
The details of these aspects of the correspondence should be abundantly clear
to a reader who has followed so far, and are sufficiently routine to warrant
omission.
There is one final matter that we must clear up. We have thus far stead-
fastly adhered to an "uncurried" presentation of the semantics of types,
Peter W. O'Hearn and Robert D. Tennent 147
whereas in the ccc Sw this is of course not necessary. The uncurried presen-
tation is needed, however, for the category-free semantics to work properly.
The reason is that parametricity does not imply naturality in general, but only
for parametric functors of a specific form.
lt will be simpler if we discuss this relationship between parametricity and
naturality first in the context of the category-free semantics, and then translate
to the categorical one. Consider the type comm - comm, and the family of
elements m [-] E [ comm - comm][-] defined by
m[X][Y] c (x,y} = (x,y'), where c(x,y} = (x',y'}.
This family of elements is "parametric" in the following sense: for all relations
R: X - X' between store shapes, m[X] [[comm-comm]R]m[X']. Following
the analogy with polymorphism, m is essentially an element of
V ()("i/}'. (()(.X}'-+()(. X}'}-+ (()(.X}'-+()(. X}')
id l l «comm- comm}}e
{*} m[X x Y] «comm- comm}}(X x Y)
falls, for e an expansion, using essentially the same counterexample as above.
That is, for
e': X X Y -+ (X X Y) X Z
the state m[X](*)[(X x Y) x Z]((e; e'),c} ((x,y},z} will not necessarily be
equal to the state m[X x Y](*)[(X x Y) x Z](e',c} ((x,y},z}.
148 Chapter 16. Parametricity and Local Variables
diagl lp
X id X
tdxpl lp
WxX p X
IDENTITIES. The identity on X is (idx, fst), where fst is the first projec-
tion.
150 Chapter 16. Parametricity and Local Variables
is m such that
m·C·S = g· (S,C· (f·S)).
For variables,
[var[ö]]A = ([<5] ~ [comm]A) x [exp[ö]]A
and
[var[ö]] l<f,g)l = {(id[oJ~([comm] l<f,g)l), ([exp[ö]] l<f,g>l>}
where we are using ~ on morphisms in the usual way and {·, ·} is the pairing
associated with the Cartesian structure of PER; the required realizer should be
evident.
Procedure types are defined using the exponential in PER~: [0- 0'] =
[0']1111.
These definitions of types are almost exactly as in [Oles, 1982). The seman-
tics of terms is also essentially similar. Weillustrate by defining the semantics
of new. First we define the standard local variable locvar.
We need a realizer acc for the acceptor part of a local variable. It is given
by
acc · n · (s, m) = (s, n)
The number (s, m) is thought of as a state, where s is the non-local part of the
stack.
The expression part of a local variable should map (s, m) to m, so it is
simply snd. We then define
locvar = ( acc, snd ).
Notice that locvar E dom ([var]XxY), for any ~-objects X and Y. The Stan-
dard local variable is "uniformly given" for all worlds.
For new.5, we need a realizable natural transformation of type
[var[ö]- comm]....:.. [comm].lts realizer new0 is as follows:
newo · p · s = fst · (p · (expand, locvar) · (s, 8))
Once again, 8 is a standard initial value for variables of type <5. We could, of
course, do away with this standard value by accepting the initialization as an
argument to a new block. Then the realizer for new would be independent of
<5 altogether.
A(lexpandl) I 1
[ß](lexpandl)
and Rosolini [1993). They show that any realizable natural transformation
between realizable functors PER1·so - I PER - F PER and PER1·so - I PER - G PER,
where I is the embedding, determines a natural transformation between F and
G. Our counterexample simply shows that the analogous property does not
hold for composites PERiso _E. ~ ll PER.
We conclude the section with an example of reasoning about local variables
using PERs. Recall the abstract "switch" from the end of Section 5:
begin begin
Boolean z; integer z;
procedure flick; z := true; procedure flick; z := z + 1;
Boolean procedure on; Boolean procedure on;
on:= z; on:= z ~ 1;
z := false; z := 0;
P(flick, on) P(flick, on)
end end
Let 2 be aPER of booleans: its equivalence classes are {0}, regarded as false,
and {1}, regarded as true. By the semantics of new and the Representation
Theorem for PERs, we can show the following equivalence of polymorphic
functions:
fst (p[N) ((id x .\n. n + 1), .\s. snd(s)(~ 1)) (s, 0))
- fst (p[2) ((id x .\n.1), .\s. snd(s)( = 1)) (s, 0))
for p: Vy. (0< X y--+ 0< X y) X (0< X y--+ 2)--+ 0< X y--+ 0< X y). Here, ~1 and =1
are the obvious functions that return 0 or 1 depending on the values of their
arguments.
To reason about these functions we consider a number of realizers. Let
flickl besuchthat flickl · (w, n) = (w, n + 1). Similarly, flick2 · (w, n) = (w, 1),
onl · (w, n) = ifn ~ 1 then 1 else 0 and on2 · (w, n) = ifn = 1 then 1 else 0. lf
m is a realizer for p then
m · (flickl, onl) E dom (W x N => W x N)
and
m · (flick2, on2) E dom (W x 2 => W x 2).
Consider the PER N+ that relates 0 to itself and all positive numbers to one
another. Then flickl[W x N+ => W x N+]flick2 and onl[W x N+ => 2]on2. Since
m E dom((WxN+ => WxN+) x (WxN+ =>2) => WxN+ => WxN+))
The pertinent aspects of PERs that we have used here are that the same
number realizes instantiations of p at different types, and that realizers in
instantiations 2 and N for different arguments to p are "equivalent" in the
PER N+. All of the examples from Section 5 can be validated using similar
reasoning.
11 Conclusion
In this work we have argued that the phenomenon of local state is intimately
linked to Strachey's notion of parametric polymorphism, and we have shown
that reasoning about local variables often amounts to proving properties of
polymorphic functions. The Straightforward treatment of a number of test ex-
amples, and representations of first-order types obtained from parametricity,
lend a measure of support to our position. However, as is the case with mod-
els of polymorphism, little is known about the semantics at higher types, and
we do not know ü full abstraction can be achieved using our methods.
No previous model of local variables correctly handles all of the test equiv-
alences that we have demonstrated here. However, Sieher [1993] (building on
the earlier paper [Meyer and Sieber, 1988]) has recently constructed a model
which also treats all of them correctly. Sieber's model is similar in many re-
spects to our relational-parametricity model: it also is based on functors and
logical relations; however, the exact connection between the models is not
clear to us. Firstly, Sieber's approach is tightly tied to locations. Our approach
can also be applied with a location-oriented semantics (as we did in the pre-
liminary version [O'Hearn and Tennent, 1993a]), but a location-free semantics
is much cleaner, as predicted in [Reynolds, 1981b]. A more substantive differ-
ence has to do with identity relations. Sieher allows for non-identity relations
on the set of natural numbers; this ties up with the treatment of sequentiality
in [Sieber, 1992]. And there is also some question concerning the respective
roles of identities in treating function types.
Our identification of parametricity as the central notion connected to lo-
cality provides, in our opinion, a sonnder conceptual basis for explaining why
and how this form of uniformity arises in local-variable semantics. In the
Sieher and Meyer-Sieber work, logical relations appear primarily as an ad hoc
method of cutting down a model. The fact that many of the subtleties in local-
variable semantics involve the form of data abstraction that can be achieved
with procedures and local variables gives a fairly coherent explanation as to
why parametricity and logical relations should be relevant. And, as we have
seen, reasoning about local variables often amounts to proving properties of
polymorphic functions. The PER model serves to further underscore our posi-
tion.
But, independently of this, we would like to acknowledge the influence
of [Meyer and Sieber, 1988] on this work. For one, contemplation of their
equivalences-which incidentally are primarily responsible for a wider under-
standing of the subtleties involved in local-variable semantics-played a part
in leading us to propose parametricity as a central theme. For another, their
use of functors and logical relations certainly had an influence, albeit indi-
rectly, on our development of the relational-parametricity model.
Peter W. O'Hearn and Robert D. Tennent 157
Honsell, Mason, Smith and Talcott [1995] have developed a logic for rea-
soning about state based on operational, rather than denotational, semantics;
see also the earlier paper [Mason and Talcott, 1992]. Once again, we feel that
the conceptual principles underlying their formal rules for reasoning about
local state are not as clear as, and lack the coherence of, our parametricity-
locality connection. Their logic appears tobe quite powerful, however, and
many of the subtle local-variable equivalences can be proven in the logic. It
would be interesting to see if a suitable representation-independence property
for local state could be derived in their logic, or if such a property could be
formulated in a way that could be added to their reasoning framework.
Wehave used the framework of reflexive graphs mainly to examine the
specific structure of our model, but it may have more general interest. Re-
flexive graphs could conceivably be of use in studying the connection between
relational parametricity and naturality in a more general context, or in clari-
fying the mathematical significance of using diagonal relations as "identities."
It may be that our Cartesian closure result can be considered as an instance
of a reflexive-graph version of the usual result that the functor category cx is
Cartesian closed whenever C is Cartesian closed and complete (the results of
[Freyd et al., 1992b] could be relevant here). Similar kinds of structure have
been used by Pitts [1993] in bis study of relations and recursive domain equa-
tions, and by Pitts and Stark [1993a, 1993b] in their study of dynamic alloca-
tion. Dynamic allocation poses challenging problems beyond those considered
here, where we have considered variable declarations that obey a stack disci-
pline. (Some examples from [Pitts and Stark, 1993b] suggest that parametric-
ity, by itself, might not be sufficient for understanding dynamic allocation.)
The problern of single threading is deserving of further attention. It is
interesting that most work on the semantics of state, including that of the au-
thors, has concentrated on local variables. In our opinion, the single-threaded
nature of state is at least as fundamental an issue as the nature of local vari-
ables. In this paper, the main aim was to examine the phenomenon of locality,
and we feel that it is reasonable to study this in isolation from single thread-
ing. However, ideally a semantics of state should exclude the kind of state
backtracking found in the block expression.
(A. Meyer has pointed out that the "single threading" terminology can be
misleading. The issue does not concem single versus multiple threads of exe-
cution, but rather "backtracking within a single thread." Since the term "single
threading" is now used extensively in the functional programming community,
we continue to use it here to avoid needless terminological differences. The
reader should be warned, however, of the possible confusion that may arise if
one thinks of the more common programming usage of the term "thread.")
A simple equivalence which illustrates the problern is the following:
if x = 0 then f(O) eise 1 if x = 0 then f(x) eise 1.
Acknowledgements
Some of this research was carried out at the Labaratory for Foundations of Computer
Science, Department of Computer Science, University of Edinburgh. A preliminary re-
port on some of this work appeared as [O'Hearn and Tennent, 1993a). The realization
160 Chapter 16. Parametricity and Local Variables
Contents
1 Introduction 165
2 ldealized ALGOL 168
3 The Parametrie Logical Relation 173
4 Example Equivalences 179
5 Related and Further Work 182
References 184
1 Introd.uction
The Observable properties of sequentially executed imperative programs in-
volving higher order procedures and locally declared state can be quite subtle.
This is so even when the use of local state is quite severely constrained, as it is
in languages which are ALGOL-like in the sense of Reynolds [Rey81]-i.e., when
the state just consists of variables storing first order values (as opposed to
function closures, for example) and local-variable declarations are only permit-
ted in comm.ands (not in expressions), are statically scoped, and are executed
using a stack discipline. The subtleties of the extemally observable behaviour
of such locally declared state are such that, despite the considerable efforts
of a nurober of researchers [MS88, Ole85, OT95, Red95, OR95a, Sie95a], no
concrete denotational model of ALGOL has yet been constructed which exactly
First appeared in Proceedings Eleventh Annual IEEE Symposium on Logic in Computer Sdence,
Brunswick, New Jersey, July 1996, pp. 152-163. ©1996 IEEE, reprinted with permission.
relation, whose definition and properties are the main teehnieal eontribution
of this paper. This takes the form of a parametrie family of relations between
closed ALGOL terms of equal type. Roughly speaking, the parameter ranges
over relations between states (whieh in this ease are just assignments of inte-
ger values to global variables). The definition of the logieal relation proeeeds
by induetion on the strueture of types and involves the struetural operational
semanties of the language. Eaeh clause embodies both extensionality prop-
erties and the kind of relational parametrieity eonsidered in a denotational
setting by O'Hearn and Tennent [OT95].
The Operational Extensionality Theorem follows from the faet that eon-
textual equivalenee coincides with the parametrie logieal relation when its pa-
rameter is instantiated to the identity relation on states. This faet is in turn
derived from the logieal relation's 'Fundamental Property' (cf. [Mit90]), namely
that it is preserved by the various term-forming operations of ALGOL. The
proof of the Fundamental Property (Theorem 3.11) is non-trivial beeause of
the presenee of reeursively defined terms in the language. At this point in a
denotational development one ean use the familiar eharaeterisation of least
fixed points (used to model reeursive terms) as least upper bounds of eertain
aseending ehains and apply Seott lnduetion [Seo93, Seetion 3]. Here we de-
velop an operational analogue of this method. This is similar to the approaeh
taken by Smith et al [MST],exeept that we have to deal with an imperative lan-
guage and we use a struetural operational semanties based upon an evaluation
(or 'big-step') relation, rather than a transition (or 'small-step') relation. At the
heart of the proof is a eertain 'eompaetness' property of evaluation with re-
speet to the eanonieal sequenee of approximations to a reeursive term (see the
proof of Proposition 3.7).
2 ldealized ALGOL
We will define the parametric logical relation for 'Idealized ALGOL' (IA), a
small ALGOL-like language which has been used by several authors for il-
lustrative purposes. It is a simply typed lambda calculus over ground
types bool (booleans), int (integers), var (variables for storing integers), and
cmd (commands for changing state). It contains terms (of the appropri-
ate types) for lambda abstraction (Ax: u. M), application (FA), conditionals
(if B then M1 eise M2), fixpoint recursion (fixx: u. M), integer and boolean
constants (n, b), and some arithmetic operations and relations (NI * N2). We
choose to make dereferencing explicit in the syntax: !V is the IA term of
type int denoting the contents of a term V of type var. In addition to de-
numerable sets of identifiers of each type (xa), there is a denumerable set of
global variables(.v), each of which is an IA term of type var. Finally, IA con-
tains cmd-building constructs for no-op (skip), integer assignment (V := N),
sequencing (CI; C2), and local-variable blocks (new x := N in C end), in addi-
tion to the conditional and fixpoint constructs which are available at all types
(and which permit one to define various recursive control structures, such as
while - do - ). We write M: u to indicate that the IA term M has type u.
Remark 2.1 (Binding and substitution) The IA terms for A-abstraction, fix-
point recursion, and local-variable blocks are all identifier-binding constructs:
free occurrences of xa in the term M become bound in Ax: u. M and in
fixx: u.M; and free occurrences of xvar in the command C become bound
in new x := N in C end. Henceforward, we will identify IA terms up to
oc-conversion of their bound identifiers.Then M[M' ;xa] will denote the result
(well-defined up to oc-equivalence) of substituting a term M' of type u for all
free occurrences of the identifier xa in the term M. Similarly, M[v' /v] will
denote the result of substituting the global variable v' for all occurrences of
the global variable v in M. We write fi(M) for the finite set of free identifiers
of M, and gv(M) for its finite set of global variables. Let
IAa(W) def {M:ulfi(M)=0 & gv(M)~w}
denote the set of closed IA terms of type u with global variables in the set w.
We specify the operational semantics of IA in terms of an inductively de-
fined evaluation relation of the form
w 1- s;M ~a s';R. (2)
Here w is a finite set of global variables-we call such sets worlds,because
they are an operational trace of the Kripke-style 'possible world' seman-
tics of block structure using functor categories introduced by Reynolds and
Oles [Ole85]. In (2), M and R are elements of IAa(w), and s,s' are statesof
world w-i.e., functions assigning integers to the global variables in w. We
write States(w) for the set of all such states. The intended meaning of (2) is
that given the initial assignment s of values to the relevant global variables,
evaluation of M yields the final result R and state s'. The rules for inductively
generating the evaluation relation are given in Figure 1. They are quite stan-
dard, apart from some notational choices: wv denotes the set w augmented
Andrew M. Pitts 169
w f- s;V ~varS 1 ;V . 1
1 Ifn=s (v)
s; .V ~ int s ; n
1
w f-
Lemma2.2
(Side-effect free expressions) I( (2) holds and 0' f. cmd, then s = S 1•
(Equivariance). Given a bijection rr: w = W write M[ rr] (or the result of re- 1
,
In view of the first part of the lemma, when a -! cmd we abbreviate (2) to
Wf-S;M~uR. (3)
Similarly, since the only result term of type cmd is skip, when a = cmd we
abbreviate (2) to
w f- s;M ~ s'. (4)
Finally, we write
w f- s;M il'u (5)
to indicate that w f- s; M ~u s'; R does not hold for any s' and R.
Having fixed the syntax and operational semantics of our ALGOL-like lan-
guage, we can give the formal definition of contextual equivalence. As usual, a
contextC[ -u] is a term in which a Subexpression of type a has been replaced
by a 'hole', -u· The expression resulting from filling the hole with an expres-
sion M: a will be denoted by C[M]. Since -u may occur within the scope
of identifier-binding constructs, free identifiers of M may become bound in
C[M]. We write traps(C[ -u]) for the set of identifiers that occur in C[ -u]
associated to binders containing the hole -u within their scope. This 'capture'
of identifiers in fi(M) n traps(C[ -u]) means that although the operation of
substituting M for -u in C[ -u] respects oc-conversion of bound identifiers in
M, it does not necessarily respect oc-conversion of bound identifiers in C[ -u ].
Therefore we do not identify contexts up to oc-conversion. As for terms, so
for contexts we write C[ -u ]: a' to indicate that a' is the type of the context;
fl(C[ -u ]) and gv(C[ -u]) denote the finite sets of free identifiers and global
variables of the context. A closedcontext is one with no free identifiers.
Definition 2.3 (lA contextual equivalence) If M1 and Mz are lA terms of type
a with free identifiers contained in a set r of identifiers, and global variables
contained in a set w of global variables, we write
w,r f- M1 =u Mz
to indicate that the terms are contextually equivalent.By definition this
means that for all worlds w' 2 w, all closed contexts C[ -u ]: cmd with
gv(C[ -u ]) ~ w' and r ~ traps(C[ -u ]), and all states s, s' E States(w'),
w' f- C[MI],s ~ s' = w' f- C[Mz],s ~ s'.
(In case r = 0, i.e., when the Mi are closed terms, we just write w f- M1 =u Mz
for w, 0 f- M1 =u Mz.)
Thus two terms are contextually equivalent if occurrences of them in some
closed command can be interchanged without affecting the meaning of the
Andrew M. Pitts 171
Example 2.6 (Meyer-Sieber [MS88, Example I]) If C: cmd has its free identi-
fiers contained in r and its global variables contained in w, and if xvar rf. r,
then
W, [ 1-- (new X:= n in C end) ~cmd C.
Proof: According to Theorem 2.5, it suffices to show for all worlds w,
all closed commands C E IAcmd(w), and all states s, s' E States(w) that
w 1-- s; C Us' holds if and only if w 1-- s; (new x := n in C end) U s'. Because
of the structural nature of the rules in Figure 1, the only way that the second
evaluation can be deduced is from
wv 1-- (s ® v:= n);C[v/x] U s' ®V:= n' (6)
for some v rf. w and some n'. Since Cis closed, C[v/x] = C; in particular
gv(C[v/x]) = gv(C) s;; wand so by the 'Weakening and Strengthening' prop-
erty of U (Lemma 2.2), (6) holds if and only if w 1-- s; C U s', as required. I
Example 2.7 (Meyer-Sieber [MS88, Example 3]) Suppose C: cmd has free
identifiers in rxyarx~ar and global variables in w. Define:
CI2 def
= new XI := ni in C2I def
= new X2 := n2 in
new X2 := n2 in new XI := ni in
c C[x2,XI/XI,X2]
end end
end end
Then w,f 1-- C12 ~cmd C2I·
Proof: The argument is similar to that for the previous example, but using the
'Equivariance' property of U given in Lemma 2.2. I
Recall that in logics of partially defined terms, two partial terms are often
called 'Kleene equivalent' if whenever one term is defined so is the other and
in that case they are equal. following a suggestion of Harper, we adopt this
terminology for programming language expressions that may diverge. For IA
this Ieads to the following, rather strong notion of equivalence.
Definition 2.8 (Kleene equivalence) Closed terms MI,M2 E IAu(w) are said
to be Kleene equivalent,and we write
w 1-- MI~~ M2
if, for all s,s' and R, w 1-- s;MI Uu s';R holds if and only if w 1-- s;M2 Uu s';R.
The following Iemma is easily proved by induction on cr.
Lemma 2.9 Ifw 1-- MI ~~ M2 then w 1-- MI ~~ M2.
Thus in view of Theorem 2.5, any Kleene equivalent IA terms are contextually
equivalent.Here are a number of examples, singled out because they will be
needed later.
Examples 2.10 The following pairs of terms are Kleene equivalent and hence
also contextually equivalent. In (vii)-(x), .Lu is an abbreviation for fixx: cr. x.
(i) !(if B then VI eise V2) and if B then !VI eise !V2.
Andrew M. Pitts 173
RI ® Rz = Rz ® RI,
1dw®R = R.
Armed with these notions we can give the principal definition of the paper.
174 Chapter 17. Operationally-Based Reasoning About Local Variables
Definition 3.2 (Parametric logical relation) For each finite set w of global
variables, each type er, and each relation 'RE Rel(w), we define a binary rela-
tion between closed lA terms of type er with global variables in w, denoted
(M1,M2EIAu(w))
WI-M1'RuM2
The relations are defined simultaneously, for all w and 'R, by induction on the
structure of er as follows. (In giving the clauses, we make use of the extension
of the evaluation and divergence relations to lifted states mentioned above.)
• If er = bool (respectively er = int), then w 1- M1 'Ru M2 is defined
to hold if, for all boolean (respectively integer) constants c1, c 2 and all
(SI, S2) E 'R,
W 1- S1;M1 ~u C1 & W 1- S2;M2 ~u C2 => C1 = C2,
Remark 3.3 The last clause in this definition is an operational version of the
kind of relational parametricity for functions used previously by O'Hearn and
Tennent (see [OT95, Sect. 2.2]); it also embodies the typical feature of 'logical
relations'-that related functions map related arguments to related results.
The way the logical relation takes account of divergence in the clauses for
bool, int, cmd, and var reflects recent work of O'Hearn and Reynolds [OR95b],
and is crucial for Example 4.1. There is a more elegant formulation of those
clauses, given below. We chose to take the more concrete form in Defini-
tion 3.2, because it is useful for calculations.
The proof of the following two properties is a tedious, but essentially
Straightforward case analysis (making use of the determinacy of evaluation,
Lemma 2.2).
Andrew M. Pitts 175
(i) Let Vala(w) denote the set of closed syntactic valuesof type u with
global variables in the set w-i.e., theset of those IA terms R appear-
ing on the right-hand side of evaluations (2). (For example, Valcmd(w)
is just {skip}.) For each s E States(w).l. and M E IAa(w), let
s ®ME (States(w) x Vala(w)) .L be defined by
(ü) w r- V1 Rvar V2 holds if and only if w r- !V1 R;nr !V2 and, moreover, for
all n, w f- <V1 := n) Rcmd <V2 := n).
Proof: This follows directly from Definitions 2.4 and 3.2, by induction on the
structure of u. I
Definition 3.5 Extend the parametric logical relation to open terms as fol-
lows. Given R E Rel(w) and terms M 1,M2: u with free identifiers in
r = {xa1, ... , x:i"} and global variables in w, write
w,r r- M1 Ra M2
to mean that, for all R' E Rel(w') with w' disjoint from w and for all closed
terms An, A;2 E IAa; ( ww') (i = 1, ... , n),
This definition reduces to the one in Definition 3.2 in the case that r = 0,
because of the following weakening property of the parametric logical relation.
Lemma 3.6
(i) I{R E Rel(w) and R' E Rel(w') with w n w' = 0, and if r and [' are
disjoint sets of identifiers, then
Proof: Part (i) reduces (using the associativity of ®) to proving the correspond-
ing property of the relation between closed terms:
w 1- Mt Xu M2 => ww' 1- Mt (X® X')u M2.
This is proved by induction on u, using the corresponding weakening property
of evaluation (Lemma 2.2). Part (ü) follows immediately from this property too.
I
Proposition 3.7 The parametric logical relation preserves the term-forming
operations of IA:
(i) For c
= b, n, and skip one has 0 1- c (1d 0 ) u c (where u = bool, int, and
cmd respecttvely).
(ii) Ifv E w, then w 1- V (1dw)var v.
(iii) Ifw,r 1- Bt Xboo1 B2, w,r 1- Mt Xu M2, andw,r 1- Mi Xu M~. then
w,r 1- (üBt thenM1 eise Mi> Xu (ü B2 thenM2 eise M~).
rather than
wv,r 1- Ct[V/Xvar] (X® 1d{vj)crnd Cz[V/xvar].
The latter is a weaker assumption than the former, and so (x) as stated is a
stronger form of preservation than one might expect. It reflects the fact that
new - :=N in C[-] end is really a binding operation on variables rather than
identifiers. From a metalogical point of view, the difference between 'variables'
and 'identifiers' (which a metalogician might well prefer to call 'constants' and
'variables' respectively) lies in the structural rules satisfied by the judgements
ofthe form
178 Cbapter 17. Operationally-Based Reasoning About Local Variables
for any A: CT with free identifiers in r and global variables in w' 2 w, and
w 1- M1 :=u Mz ~ w 1- M1 ::lfit Mz (9)
Repeated use of (8) reduces the converse of (7) to the special case when r = 0,
which is (9). Thus together these properties yield the required bi-implication.
Proof of (7).Suppose that w,r 1- M1 :=lfi1 Mz and hence by the above Corol-
lary that w,r 1- M1 (1dw)u Mz. We wish to show that w,r 1- M1 ==u Mz,
i.e., thatfor all w' 2 w, all closed contexts C[ -u ]: cmd with gv(C[ -u]) s;; w'
and r s;; traps(C[ -u ]), and all states s, s' E States(w'),
w' 1- C[MI],s ~ s' = w' 1- C[Mz],s ~ s'.
But given such a context, by Theorem 3.11(ii) (and Lemma 3.6(ii)) we have
w' 1- C[Md (1dw•)cmd C[Mz]. Since (s,s) E 1dw•. it follows from the definition
of the parametric logical relation at type cmd and from the definition of 1dw'
that the above bi-implication holds.
Proof of (B).If w, rxu 1- M1 ==u• Mz, then straight from the definition of contex-
tual equivalence we get w',f 1- (.\x:CT.MI)A ==u-u' (.\x:CT.Mz)A The result
follows by transitivity of :: together with the fact that ß-conversion (Exam-
ple 2.10(v)) isavalid Kleene equivalence, hence isavalid extensional equiva-
lence (by Lemma 2.9) and so isavalid contextual equivalence, by (7).
Proof of (9).1t is Straightforward to show that ==u satisfies the defining clauses
for :=lfi1 in Definition 2.4, by induction on the structure of CT. I
Remark 3.13 (Doing without J.) Wehave developed a logical relation param-
eterised by relations between liftedstates, because that seems necessary for
proving some contextual equivalences (specifically, Example 4.1). However,
many examples and the operational Extensionality Theorem itself, can be de-
duced using a simpler logical relation, call it w 1- - 'Ru -, which is parame-
terised merely by binary relations on states, R s;; States(w) x States(w). The
defining clause at type cmd is: w 1- C1 'Rcmd Cz holds if and only if for all
(si,Sz) E 'Rand s~.s; E States(w)
WI-S1;Cl~Si ~ 3s~(WI-Sz;Cz~S~ & (si,s~)E'R)
w 1- sz; Cz ~ s~ ~ 3si (w 1- s1; C1 ~ si & (si, s;) E 'R).
The defining clause at types CT = bool, int is: w 1- M1 'Ru Mz holds if and only
if for all (s1, sz) E 'R and constants c
w 1- s1;M1 ~u C = w 1- Sz;Mz ~u C.
At type var we can define 'Rvar as in Remark 3.3(ii). Finally, the definition
of 'Ru 1 -u2 is the same as for 'Ru1 -u2 (except that one is quantifying over a
different kind of state-relation).
4 Example Equivalences
The Fundamental Property of the parametric logical relation (Theorem 3.11)
and its relationship to contextual equivalence (Corollary 3.12 plus Theo-
rem 2.5) enable one to use the definition of the logical relation at function
types to reason about properties of procedures with respect to local variables.
Here are some examples.
180 Chapter 17. Operationally-Based Reasoning About Local Variables
Then by definition of ('ldw ® :R)cmd and :R, if (15) holds for some n, then (16)
holds with n' = -n, and vice versa. I
For our final example we combine use of the logical relation with some
equational properties of contextual equivalence-namely its congruence prop-
erty (evident from its definition) and validity of ß-conversion (established in
Example 2.10(v)). Note in particular that these two properties imply that con-
textual equivalence is preserved by the operation of substituting terms for
identifiers.
Example 4.4 (Sieber [Sie95a, page 55]) Given any world w, any global vari-
able v ft w, and any term PE lAcmd-cmd(wv), let
F def .\n: int. new x := 0 in
P (x := 1 ; v := n);
ü !x = 1 then l.cmd eise skip
end
Then for any world w' 2 wv and terms N,N' E IAtnr<w'h one has
w' t- F N <=cmd F N'.
Proof: Let G def .\n: int. .\c: cmd.P (c;v:= n). Applying the substitutionprop-
erty of r= mentioned above to Example 4.1, we have
w' t- C1[GNJp] =cmd C2[GNjp].
Using ß-conversion and the congruence property of r= we deduce that
w' t- C1[GN/p] <=cmdFN
References
[HMST95] F. Honsell, I. A. Mason, S. F. Smith, and C. L. Talcott. A variable typed logic
of effects. Information and Computation, 119(1):55-90, May 1995.
[MS88) A. Meyer and K. Sieber. Towards fully abstractsemanlies for local vari-
ables. In Proc. 15th Symp. on Principles of Programming Languages, San
Diego, pages 191-203. ACM, 1988. See Chapter 7.
[Ole85] F. J. Oles. Types algebras, functor categories and block structure. In M. Ni-
vat and J. C. Reynolds, editors, Algebraic Methods in Semantics, chapter 15,
pages 543-574. Cambridge University Press, 1985. See Chapter 11.
[OR95a] P. W. O'Hearn and U. S. Reddy. Objects, Interference and the Yoneda Em·
bedding. In S. Brookes, M. Main, A. Melton, and M. Mislove, editors, Math·
ematical Foundations of Programming Semantics, Eleventh Annual Confer-
ence, volume 1 of Electronic Notes in Theoretical Computer Science, Tulane
University, New Orleans, Louisiana, March 29-April 1 1995. Elsevier Sci-
ence.
[PS] A. M. Pitts and I. D. B. Stark. Operational reasoning for functions with local
state. In A. D. Gordon and A. M. Pitts, editors, Higher Order Operational
Techniques in Semantics. In preparation.
Andrew M. Pitts 185
2 Syntax
2.1 Passive Uses
The treatment of passivity in [33] is based on designating certain phrase types
(such as "state reader" expression types) as being passive, and then, for any
phrase R, determining
the set of identifiers which have at least one free occurrence in R
which is outside of any subphrase of passive type.
These are considered to be the acttvely occurring free identifiers of R. Unfor-
tunately, this definition, being context-independent, cannot take account of
the fact that, when R itself occurs within a passive phrase, none of its free
identifiers can be used actively. This means that the syntactic constraints on
procedure calls are unnecessarily restrictive, which results in anomalies when
types combine passive and non-passive capabilities.
For example, a storage variable is used passtvely when it is read from, as
on the right-hand side of an assignment Statement, and acttvely when it is
assigned to. Suppose that identifiers x and w are of type var[ T] (i.e., they
are T-valued variable identifiers, with T a data type such as int or bool), and
consider the following command:
(.\z: T. x := (.\y: T. w)z}(w) (1)
where typings of the formt: T indicate that L is a T-valued expression identifier.
Although w occurs in both the procedure and argument parts of the outer call,
the phrase is legal because both occurrences are in expressions and hence
regarded as passive. However, the command ß-reduces to
x := (,\y: T. w)w (2)
in which the right-hand side is illegal, according to Reynolds's treatment, be-
cause variable identifier w is deemed to occur actively in the procedure (which
P. W O'Hearn, A. ]. Power, M. Takeyama and R. D. Tennent 193
IDENTITY
---,------.,. Axiom
1 c e f- 1: e
STRUCTURE
TIIt:9,frP:cf>
-------'-- Passification
TI, 1: e r f- P: e·
1
-------.,. Activation
TI, t: e I r f- P: cf> TI 1 c e, r f- P: e'
TI Ir f- P: e TI I r~ f- P: e Exch ange
-----=------=--- Weakening ~
TI, TI' 1 r,r' f- P: e TI Ir f- P: e
TI,t:e,t':e 1 r f- P:e'
---'--="-'--".....:.,,......-,----:-:- Contraction
TI, t: e I r f- [P](t' - t): 9'
has type T - var[ T ]), and also occurs in the argument. But the procedure
call is actually an expression, and so there cannot be any interference via w;
indeed, the assignment ß-reduces tothelegal x := w.
It can be argued that the anornaly in this example could be avoided if
dereferencing coercions were explicit; however, rnore cornplex examples, as
in [33], show that the problern is a fundamental one. (An example of this
kind frorn loc. cit. will be discussed in Section 2.4). The problern arises essen-
tially because the context-independent notion of active occurrence cannot be
sensitive to Situations in which the context ensures passive use of potentially
non-passive entities. To avoid the anornalies, it is necessary to consider when
identifiers occur actively in instances of phrases, taking context into account.
A subset (primp) of the primitive types is singled out as passive, and this
generates the passive types as follows:
cp ::= (primp) 1 cp ® cf>' 1 cp x cf>' 1 e- cp e· - p e.
1
There are two products: fJ x fJ', for which the cornponents can interfere, and
e ® fJ', for which the cornponents rnust be non-interfering. There are also two
exponentials: 0' - fJ, which is the type of ordinary procedures (which cannot
interfere with, or be interfered with by, their arguments), and fJ' - p e, which
is the type of passive procedures. A passive procedure does not assign to any
global variables (though a call of a passive procedure rnay be active, if the
argument of the call is).
194 Chapter 18. Syntactic Control of Interference Revisited
TI 1 r 1- P: eo
TI 1 r 1- Q: e1 TI 1 r 1- P: eox e1
---'------'----.,..--- XE; ( i = 0, 1)
--------------~-- xl TI 1 r 1- rr; P: e;
r
TI 1 1- (P,Q):eo x e1
TI 1 r 1- P: eo ® e1
---'------==---~ ®E; ( i = 0, 1)
TI Ir1- rr;'
19 P: 0;
TI 1 1- Q: e' - e r
TI 1 1- Q: - p e' e
-pl ------------~---- -pE
TI I 1- promote Q: e' - p e r
TI I 1- derelict Q: e' - e
2.4 Examples
Weillustrate the operation of the rules by presenting derivations of some typ-
ing judgements.
Consider first the (unreduced) example (1) discussed in Section 2.1. The
assignment can be typed as follows:
I w:var[T]t- w:var[T] Weake .
I w:var[T],y:T 1- w:var[T] rung
~--~~~------~~ -I
I w:var[T]t- ,\y:T. w:T- var[T] I z:T 1- z:T
I w:var[T],z:T 1- (,\y:T. w)z:var[T]
-E
.
----,,---.......:....-7--:---.......:....-:----'--:-__:.....:. Dereferencmg
I w:var[T],z:T 1- (,\y:T.w)z:T
-----,"'------'---- Passification
I x:var[T]t- x:var[T] w:var[T]I z:T 1- (,\y:T.w)z:T :=
w:var[T]I x:var[T],z:T 1- x := (,\y:T.w)z:comm
where the last step abbreviates use of the := constant, xi, -E and Weakening.
Note that after Dereferencing of the right-hand side, w can be moved to the
passive zone. The typing is then completed as follows, using a Contraction:
I w':var[T]t- w':var[T]
w:var[T]I x:var[T],z:T 1- x := (,\y:T.w)z:comm I w':var[T]t- w':T
-I Pass.
w:var[T]I x:var[T]t- ,\z:T. x := (,\y:T. w)z:T- comm w':var[T]It- w':T
w, w':var[T]I x:var[T]t- (,\z:T. x := (,\y:T. w)z) (w'):comm
-E
--'--~~~-~~~---~~~~--'---'--- Contraction
w:var[T] I x:var[T]t- (,\z: T. x := (,\y: T. w)z) (w): comm
The following shows how to derive a typing for the right-hand side of the
"illegal" assignment (2) in Section 2.1:
I w:var[T]t- w:var[T]
I w:var[T],y:T 1- w:var[T] Weak. I w':var[T]t- w':var[T] & •
-:---'-----:--7---'7-------"--':~ -I Dere.erencmg
I w:var[T] 1- ,\y:T.w:T- var[T] I w':var[T]t- w':T
I w, w':var[T] 1- (,\y: T. w)w':var[T] .
-E
------,--'--"'-----..,--'--- Dereferencmg
I w,w':var[T]t- (,\y:T.w)w':T
--'-.:.....,..--:-=-:-=:---'-:-'-------'--:-- Passification
w,w':var[T]It- (,\y:T.w)w':T
--'----::-"-::-7-'---::--'------:-- Contraction
w:var[T]It- (,\y:T.W)W:T
-:----~-:------'-:-'--'---',..---- Activation
I w:var[T]t- (,\y:T.w)w:T
Even though the types of w and w' are active, Contraction can be applied
when they are in the passive zone; but Dereferencing must be used before
these identifiers can be passified. The assignment can then be typed as usual:
where the derivation for z := x' is similar to that for y := x, and the step for II
uses the introduction rule for ® followed by the elimination rule for - with
the constant 11. This can then be combined with non-passive use of x, as in the
following derivation:
The final example is essentially the problematic example from [33, p. 44).
Suppose n, y: var[int); then, the parallel command in
rro (n + 1, (n := 0 II y := rro (n, n := 0)))
is illegal in the treatment of [33) because n is used on both sides of 11. However,
the entire term is of type int, and so these uses should be regarded as passive.
To type this in our system, we can proceed as follows:
The first line can be typed straightforwardly because the identifiers on either
side of II are distinct.
Notice that the subterm (n := 0 II y := rro(n, n := 0)) does not itself
have any typing in the SCIR type system. But it can nevertheless appear in
a larger term because Contraction can be applied when a similar term with
occurrences of n renamed apart appears within a passive phrase. This subtle
interaction of Contraction and Passilication is what allows the subject reduc-
tion problems from [33) tobe solved. An equivalent type system that does not
use Contraction explicitly can be formulated, but replaces this subtle interac-
tion by explicitly accounting for the "semi-typed" status of phrases such as
(n := 0 II y := rr0 (n, n := 0)) or, more simply, (Ax:comm.x; y)y.
Table 3: ß-reductions
rr2(P,Q) -ß Q
rrf(P ® Q) -ß Q
C[P]-ß C[Q]
nlJ ... ,nm.fi.····fn I fn+l.····fm 1- [P](LI .... QI ••.. ,Lm .... Qm):8.
Proof: The proofis byinduction on the size ofthe derivationfor P. We discuss
only the key cases of structural rules that make use of the separation of a type
assignment into zones.
Case Contraction: the last step is
LI: ei •... ' Ln: 8n. L: 8n I Ln+ I: 8n+Io ...• Lm: 8m 1- P': e
LI:8I, ... ,Ln:8n I Ln+I:8n+I····•Lm:8m 1- P:8
where P = [P'](L .... Ln). By the induction hypothesis,
ni, ... ,nm.n.ri, ... ,rn.r I fn+I.····rm
1- [P'](LI,... QI, .•• ,Lm,... Qm,L,... Q):ß
where n I r t- Q: 8n is a variant of Dn I fn t- Qn: 8n with fresh identifiers not
appearing in any n; or f;. Then, n and r, being in the passive zone, can be
contracted to Dn and fn, respectively, using Contractions (and Exchanges), and
the resulting judgement is the desired conclusion.
Case Activation. The last rule is
LI: el. ...• Ln+ I: On+ I I Ln+2: 8n+2o ... ' Lm: 8m 1- P: e
LI: ei •... ' Ln: 8n I Ln+ I: 8n+Io ... 'Lm: 8m 1- P: e
By the induction hypothesis,
nlJ ... ,nm.rlJ ... ,fn+I I fn+2o•••ofm 1- [P](LI .... QI •..• ,Lm .... Qm):8
While this syntax is perhaps appropriate for "!" in linear logic, it seems overly
heavy, with no conceivable justification, from the point of view of interference
control.
The rules of Passilication and Activation are what allow us to avoid these
syntactic complications, retaining a relatively simple syntax possessing the
subject reduction property. (Compare the implicit syntax mentioned above
with that just given for Promotion!) These two rules do not correspond to any
rules in linear logic, or Lu; this difference will be seen again when we consider
categorical models of the SCIR type system.
P. W. O'Heam, A. ]. Power, M. Takeyama and R. D. Tennent 201
3 Semantics
The permeability rules of Passification and Activation can exhibit subtle be-
haviour (as we saw in Section 2.4), and if we are to be sure that the syntax
makes sense, it is crucial to have a semantic analysis of these rules. In this
section, we define a dass of categorical models of the SCIR type system. We
do not attempt to formulate a most general possible notion of model. Rather,
we focus on a particularly cohesive dass, which we term "bireflective" models,
that are sufficient to secure our basic aim of showing a sound interpretation.
A concrete model for the programming language of Section 2.3 will be
presented in Section 4.
As usual, the types and terms of the language are to be interpreted as objects
and morphisms, respectively, of an appropriate semantic category C. We re-
quire, first, that C come equipped with a symmetric monoidal dosed structure
(1, ®, __" ), and finite products. This enables us to interpret the non-interfering
product, the interfering product, and function types in standard ways. For
example, the dosed structure will provide application maps
app(A,B): (A __" B) ® A ___.. B
for all objects A and B, and, for every map f: A ®B ___.. C, a curried map
f*: A ___.. (B __" C)
To interpret the Weakening rule, the tensor product ® must allow for pro-
jection maps, rr~: A ®B ___.. A and rrf: A ® B ___.. B. We therefore require that
e.
the unit I for ® be a terminal object 1 of C; then rro is (idA®!B); where !Bis
the unique map from B to 1, and e: A ® 1 ___..Ais the unity isomorphism, and
similarly for rrf.
To treat passivity, we begin by assuming a full subcategory P of C, to be
thought of as the subcategory of passive objects. The typing context in the
passive zone will be interpreted as a passive object. Thus, every judgement
rr 1 r 1- P: (} will be interpreted by a map
S[IT] ® [ r ] - [0]
where S[IT] is an object of P, and [r] and [0] are objects of C. To treat both
Contraction and Weakening in the passive zone, we simply require that ® be a
categorical product in P. The interactions of permeability rules and rules for
the passive function type are accounted for by making a further assumption
onP.
202 Chapter 18. Syntactic Control of Interference Revisited
being the identity, where 11 is the unit of adjunction S -; J and E' is the counit
of]-; S.
This definition is from [10], where its categorical properties are studied. Our
main concern here is to explain its connection to the SCIR type system.
The adjunction S -; J is used to interpret the permeability rules of Pas-
silication and Activation. For Passification, consider first the special case in
which there is only one identifier in the active zone and none in the passive
zone:
I t: 0 1-- M: cJ>
t:O Ii- M:cf>
The adjunction determines a transformation of maps
f:A-JP
passify(f): SA- P
where P is any object of P, and A is an arbitrary C-object. This interprets
the indicated instance of the rule, and instances involving more than one con-
textual identifier can be dealt with by assuming that S be a strong monoidal
functor; i.e., that it preserves tensor products up to (coherent) isomorphism:
S(A ® B) == SA ®SB and 51 == 1 [9, 18]. Additionally, the unit of the left ad-
junction gives us a natural family of maps IJA: A - SA to interpret the Activa-
tion rule.
The right adjunction J -; S is utilized in the treatment of -+p. Clearly,
we would like - p to behave like a function type. But, as evidenced by the
introduction rule -pl, these functions are subject to constraints ensuring the
passive use of free identifiers within them. If we set A - p B = S (A ~ B) then,
using J-; S, this determines an adjunction
]P®A-B
P - [A -+p B]
where Pisa passive object. (That is, (-)®A: P-Cis left adjointto S(A ~ (-) ),
for all C-objects A.) Thus, we have an interpretation of - P that takes into
account both passive use and functional properties such as ß and IJ.
The further requirement of bireflectivity-the coincidence of the left and
right adjoints to J and the coherence condition-implies certain equations re-
lating the left and right adjunctions. First, as the analysis in [10] shows, bire-
flectivity implies that the transformation of maps f ..... passify(f) associated
with the left adjunction S -; J can be calculated using the counit E~: SA - A
(where SA = ]SA) of the right adjunction J-; S:
passify(f) = E~ ; f (3)
P. W. O'Hearn, A.]. Power, M. Takeyama and R. D.-Tennent 203
where f:A-- P. Similarly, the transformation associated with the right ad-
junction
g:P--A
promote(g):P-- SA
can be calculated using the unit 11A:A-- SA (where SA = ]SA) of the left
adjunction:
promote(g) = g; 17A . (4)
The simplifying effect of these equations is dramatic.
For instance, in [1) it is emphasized that naturality requirements lead to a
syntactic treatment of promotion rules such as - pl that involve binding, much
like the rule discussed in Section 2.7. But by interpreting -pl using composi-
tion on the right, as in equation (4), all necessary naturality requirements are
met by the simpler form of syntax rule that we use. Similarly, the interpre-
tation of the Passification rule can now be given simply by composing on the
left as indicated by (3). This will be a great aid in establishing the connection
between model and syntax, as given by the coherence theorem below.
Definition 5 (Bireflective Model) A bireflective model of SCIR is given by the
following data:
(i) a symmetric monoidal closed category (C,1, ®,......,) with finite products
(1, x); and
(ii) a bireflective subcategory]: P .... C in which ( 1, ®) is a finite-product struc-
ture and the bireflector S: C -- P is a strong symmetric monoidal functor
{or which S -1 J -1 S are monoidal adjunctions.
Note that the reflective full subcategory P is closed under cartesian products,
so P is a monoidal category and the inclusion ] is a strong monoidal func-
tor with the comparison morphisms ]P ® ]Q-- ](P ® Q) and 1-- ]1 being
identities. An adjunction is monoidal when certain equations hold involving
the units and counits and the comparison morphisms SA ®SB-- S(A ® B)
and 1-- S1 [9, 18). Monoidal functors and adjunctions are useful for treating
rules involving typing contexts [1).
The conditions that S be strong monoidal and that S -1 ] and ] -1 S be
monoidal adjunctions are equivalent to the condition that, for A and B in C,
17 A:. :. ®:. : B____ ] S (A ® B)
A ® B -----"
For Activationl suppose f: S[IT t: 9]®[[] ..... [9']; then we define the desired
1
lld®n!OI®ld
lld®c'[9]®id
l id ® duplicate(S(9]) ® id
l I
fo ® f1
S[flo] ® [fo] ® S[fld ® [fd _ ___:__::___.;...:'----+- ([0'] ---o [0]) ® [0']
y app([9']. [9])
1 q([9'] _ [9])
For rule -pE, suppose f:S[n] ® [f]- S([O']--<> [0]); then the desired
mapis
l
S[n] ® [f] ---=-f___ S([O'] --<> [0])
E'([O']- [9])
3.3 Coherence
Notice that the presence of structural rules in the type system allows for mul-
tiple proofs of a typing judgement, and it is important to show that this does
not lead to semantic ambiguity. In this section we verify that the semantics is
in fact coherent; i.e., all proofs of any syntax judgement have the same inter-
pretation.
Theorem 10 (Coherence) Let 'l'o and '1'1 be proofs of I1 I f 1- P: 0; then
['l'o] = ['l'd.
The proof occupies the remainder of this section.
It will be convenient to have a notation for certain composite proofs. Sup-
pose 'I' is a proof of judgement rr 1 r t- Q : 0, and that we can extend 'I' by
applications 'I'' of only the structural rules of Contraction, Exchange, Weaken-
ing, Activation and Passification to obtain a proof of Il' I f' 1- Q' : 0. We write
'I'; 'I'' for the composite proof, and call 'I'' a structural extension of 'I'.
Notice that, because all structural rules are interpreted by composing on
the left, the denotation of any proof '1';'1'' of Il' I f' t- P' : 0 can be decomposed
so that
['I'; 'I''] = h ; ['I']
for a map h: S[Il'] ® [ f ' ] - S[n] ® [f] induced by structural rules in 'I''. We
often write ['I''] to denote a map of this form induced by a proof extension. If
'I'' is empty then we declare ['I''] to be the identity.
One important property to isolate is coherence of structural extensions.
Lemma 11 (Coherence of Structural Extensions) Suppose that 'I' is a proof of
rr I r
t- Q : o,
and that 'I'; '1'1 and 'I'; 'l'z are structural extensions that prove
judgement IT' I r' t- Q': 0; then ['I'; 'l'd = ['I'; 'l'z].
This is really a Statement about the maps induced by structural extensions,
and is independent of 'I', Q, and Q'. A structural extension determines a
function p from variables in rr I r to those in rr' I r' with p(x) being the
variable to which x contracts. (We omit a formal definition, which is a simple
induction on derivations). The desired result, with data as in the statement of
the lemma, is then:
208 Chapter 18. Syntactic Control of Interference Revisited
(*) If structural extensions '1'1 and '1'2 determine the same p, then
(A) E'; ['l'd = E'; ['1'2], where E' here is an appropriate component of
the counit of J -1 S, and
(B) ['1'1] = ['1'2] if (} is non-passive (so neither derivation uses Passifi-
cation).
It is easy to verify that this formulation (which now has more the flavour of a
categorical coherence result) implies the Coherence of Structural Extensions.
Note that we cannot generally ask for equality of the ['l'i] (because of Passifica-
tion). In cases where (} is passive, we use (A) and the property f = g : A - JP
iff E'; f = E'; g to conclude the lemma. We indicate the proof of (*).
Proof: Given (} and a function p from TI I r to TI' I f', we can define a canonical
extension '1'1 (that determines p) as follows.
1. Passify all identifiers if (} is passive.
2. Performall Contractions indicated by p.
3. Activate allvariables in the intersection of the image of p and the do-
mainoff'.
4. Perform appropriate Weakenings for variables not in the image of p.
Step (ü) assumes that all Contractions indicated by p are for identifiers in the
passive zone (this is an assumption on p and 6). We thus obtain an extension
'1'1 = P ; C; A ; W consisting of Passifications, followed by Contractions, Activa-
tions, and Weakenings (with some Exchanges sprinkled throughout). We prove
*),
the property ( for '1'1 a canonical extension, by induction on the length of
'1'2. We consider two sample cases.
Base case: length 0. ['1'2] is the identity, whereas '1'1 is either empty or a
sequence P ; A of Passifications and Activations (if (} is passive). (B) is trivial,
and (A) follows from the identity 11; E'; f = f, where f:X- ]P. This equation
in turn follows from the identities E' ; f = passify(f) and 11; passify(f) = f, the
former a consequence of bireflectivity and the latter of S -1 ].
Case: last rule is Passification. Part (B) is trivial. For (A), the induction
hypothesis gives us E' ; ['l'i ] = E' ; ['I'~], where 'l'i = P' ; C' ; A' ; W' is canonical
and '1'2 = 'I'~ ; p, with p an instance of Passification. Suppose that x is the
identifier moved by p. There are three subcases to consider:
1. no rule in 'l'i explicitly involves x,
2. x was introduced in the active zone through a Weakening step in W', or
3. x was moved into the active zone through an Activation step in A'.
In subcase 1 we mean that x is not moved by Passification or Activation, or
introduced via Cantaction or Weakening. Clearly one of these three cases must
apply: note that if x was involved in Contraction, Activation, or Passification,
then subcase 3 would apply. Subcase 1 is Straightforward since x is interpreted
by an identity in ['l'i]; we concentrate on 2 and 3.
P. W. O'Hearn, A. ]. Power, M. Takeyama and R. D. Tennent 209
A®B
Thus, ['1'2] = [P'; C'; A'; W"], and P'; C'; A'; W" is of the form prescribed above
for the canonical extension. Simple permutations within each component P',
A', C', W" suffice to show that it is semantically equal to the prescribed exten-
sion (in any case, there is some trivial imprecision, involving order of rules, in
the prescription (i)-(iv) for extensions).
For subcase 3, we first move p to the left of W', and then compose the
resulting instance of Passilication with the instance of A' that activates x; this
composition yields the identity. The equations involved are
Tf~ id
E'l/
SA SA--==----+SA
and
IE'
A ®B ----::---+- A A
Tf~
Thus [P'; C'; A"; W"] = [P'; C'; A'; W'; p] where A" has the mentioned occur-
rence of Activation removed (so later rules in A" and W" are slightly adjusted),
and the desired result follows as in subcase 2.
Other rules are treated in a similar fashion, using the induction hypothesis
and various identities to reduce a proof to a canonical extension. 1
With coherence of structural extensions, we may deduce the desired theo-
rem as a corollary of the following result.
Lemma 12 Suppose n I r 1- P: 6 is derivable both from llo I fo 1- Po: 6 and from
n1 I f1 1- P1: 6, using only the structural rules. Suppose further that, for i = 0, 1,
'I't is any proof of n, Ir, 1- P,: 6; then
['l'o; '1'~] = ['1'1 ; '1'{] : S[TI] ® [f] - [ 6]
for all structural extensions '1'[ such that 'I't ; '1'[ proves TI I r 1- P: 6, for i = 0, 1.
Note that, for i = 0, 1, P = [Ptlut for identifier substitutions u 1 introduced by
Contractions.
Proof: The proof is by induction on the sum of the sizes of proofs '1'0 and '1'1.
The main base case is when '1'0 and '1'1 are both instances of the Axiom
for identifiers. This case follows from the coherence of structural extensions.
210 Chapter 18. Syntactic Control of Interference Revisited
The other base cases, for constants, are immediate if any constant C: 0 is
interpreted as a map [C]: 1 - [0]
If the last step in 'l'o is an instance of a structural rule then we prove the
result as follows. Suppose that RR is the last rule applied in 'l'o, and consider
any appropriate structural extensions 'I';, i = 0, 1. We want to show
['l'o ; 'I'~] = ['I'I ; 'l'i ]: S[II] ® [f] - [ 0] .
Since the last rule in 'l'o is RR, which is one of the rules permissible in proof
extensions, this means that 'l'o; 'I'~ is the same proof as '1'2; '1'~, where '1'2 is 'l'o
with the final instance of RR stripped off and 'I'~ is 'I'~ with the corresponding
instance of RR placed on the front. (We have simply moved the break-point ";"
indicating a structural extension.) Since the proof '1'2 is smaller than 'l'o, the
induction hypothesis applies and we may conclude
['1'2 ;'I'~]= ['I'I ;'l'i]:S[II] ® [ f ] - [0].
The result follows from the identity 'l'o ; 'I'~ = '1'2 ; 'I'~. The case when 'I'I ends in
a structural rule is symmetric.
The only remaining cases are when both '1'0 and 'I'I end in a non-structural
rule for a type constructor. There are two groups of rules to consider: those
that involve disjoint hypotheses, and those that do not.
For the latter group, we consider one example: xi. Suppose the last rules
of '1'0 and 'I'I are xi, with proofs 'I'iJ of their premises.
'Yoo 'Yoi 'l'u
. .
Ilo I fo r- Po : 0 Ilo I fo r- Qo : 0 rri 1 ri r- PI : e rri 1 ri r- Q1 : e
Ilo I fo f-- (Po,Qo): 0 X 0' rr1 1 ri r- (PI. Qd : o x O'
Let hk = ['I'~] : S[Ilk] ® [fk] - S[II] ® [f], k = 0, 1, be the maps induced
by the structural extensions. Then by the induction hypothesis, ho ; ['l'o1] =
h 1 ; ['I'IJ ], j = 0, 1. The desired result is then immediate from the usual identity
hk; (f, g) = (hk ;f, hk ;g). Other rules not involving disjoint contexts are proven
similarly using the induction hypothesis and an additional identity: for ®E and
xE, use h; (f; rr) = (h; f); rr; for -I, use h; f* = ((h ® id); f)*; for -pi, use
h;(f;IJ) = (h;f);IJ;for-pE,useh;({;E') = (h;f);E'.
For the rules involving disjoint contexts we consider ®I; -E is similar.
In the following, we will content ourselves with skimming over the details
of some of the (long) syntactic constructions involved. The basic idea will
be to postpone certain Contractions until the end, so that we can apply the
induction hypothesis to disjoint terms, and conclude the desired result using
the coherence of structural extensions.
Suppose the last rule in each of 'l'o, 'I'I is ®I, i.e.,
'l'oo '1'10 'l'u
We have structural extensions 'I'~ and 'I'{ to consider, where 'I'i ; 'I'; proves
II I r t- p ® q : fJo ® fJI. Since identifiers in Pi and qi are disjoint, there are
(other) structural extensions 4>i, 4> possessing the following properties.
• 'I'i; 4>i proves a sequent II' I f' 1- p' ® q', for i = 0,1, where p' and q'
have no free identifiers in common, and
• 'l'i ; 4>i ; 4> proves rr I r t- p ® q, for i = o, 1.
That is, we are performing just enough Contractions to identify Po and PI.
and q0 and qlt postponing the identification of identifiers in both p's and q's
until the 4> stage. The reader may wish to use the following picture (where the
contexts have been omitted):
4>o 4>I
Po ® qo _ _____.::....____... p' ® q' ---=---- PI ® qi
~!~/. p®q
Next, from 4>i we can obtain proofs 4>pi and 4>qi suchthat [4>pi] ® [4>qd = [4>d:
: 4>pi : 4lqi
ITpi I fpi 1- p' : fJo ITqi I fqi 1- q' : ei
II' I f' t- p' ® q' : fJo ® fJI
Theseare obtained by copying instances of rules that concem p or q, as appro-
priate. Finally, we may apply the induction hypothesis to conclude the middle
equality in the following
['l'o ; 4>o] ['l'oo; 4>po] ® ['I'OI ; 4>qo]
['l'w; 4>pd ® ['l'u ; 4>qd
where we have suppressed some symmetry isos. The outer two equalities
follow from the identity (h ® h') ; (f ® g) = (h; f) ® (h' ; g) and the indi-
cated construction of 4>pi and 4>qi· The desired result ['l'o; 'I'~] = ['I'I ; 'I'{]
then follows immediately from the coherence of structural extensions, using
[4>i;4>] = ['1';]. I
The Coherence theorem then follows directly by taking IIo = III = II, fo = fi =
f, and Po = PI = P.
Having established that the semantics is well-defined, we can note that it
satisfies the reductions listed in Table 3.
Proposition 13 The reductions in Table 3 preserve equality in any bire{lecttve
model of SCIR. I
=
For instance, the equivalence derelict(promoteM) M follows from the iden-
tity f = f; J7A; E~ where f:]P- A, which is true by virtue of J -1 S and
equation (4).
A fuller treatment of equivalences will not be given here. However, it is
worth noting that many additional equations beyond these ß laws are valid in
212 Chapter 18. Syntactic Control of Interference Revisited
YsA.<•I js<y..-1
SB® SA ---=m::::-B-.A---+- S(B ® A)
by replacing S(-) by C - p (-), where Cis a passive type. For instance, the
map mA.B would be replaced by the term
Af: (C --+p A) ® (C --+p B).
promote(.\x: C. (derelict (rr~f)x) ® (derelict (rr~f)x))
See [1, 3) for discussion.
Verifying coherence proved tobe quite a Iot of detailed work, even with
certain isomorphisms left implicit and with the skimming over of some syn-
tactic constructions. We wonder whether type theoretic coherence could be
better approached in a more general setting; see [30) for discussion and refer-
ences.
3.4 Discussion: Non-Bireflective Models
Wehave included the qualifi.cation "bireflective" in Definition 5 because there
are models of the SCIR type system in which the left and right adjoints to
the inclusion do not coincide. The first, and foremost, examples are given
by the models in [31, 32). Others are given, for example, by arrow categories
c-. The models that we know of have the form of two categories and three
functors between them, like so:
L
( I '\
P---='---+-C
\_ R )
with I fully faithful and L -1 I -1 R. Additional conditions that a more gen-
eral (not necessarily bireflective) model of SCIR should satisfy have not been
formulated. Coherence is the minimal requirement for any general notion of
model of SCIR, and is particularly subtle because of the intricate interactions
between the permeability rules and other rules.
4 A Functor-Category Model
In this section, we present a concrete model of the illustrative ALGOL-like pro-
gramming language of Section 2.3.
The main challenge is to define non-interference and passivity for enti-
ties such as commands, expressions and procedures, which are convention-
ally modelledas input-to-output functions. In [39, 25), the similar problems
that arise in treating the non-interference predicates in specification logic
P. W. O'Hearn, A. ]. Power, M. Takeyama and R. D. Tennent 213
St(g)1 2 1St(g)
z -----:::------+-Z
c(g; f)
The semi-commutativity allows command meanings to become less-defined in
more-restricted worlds; however, the family must also satisfy the following
commutativity requirement arising from the equivalence-class component of
X-maps. For any X-map (f, Q): Y - X and y E St(Y), let
Y' = {y' E St(Y) I yQy'}
(i.e., the set of states Q-reachable from y); then
Y _ _c...::.(f....:...,Q..::.;);__._... Y
St([Y')1 1St([Y'I
Y' _c..:...:.(f_Y'.....:..;. ::..((..:. Q=):.-.)
., ..... Y'
must commute (and not just semi-commute), where rY': Y'- Y is the X-map
with components: the insertion function from Y' to Y, and the total relation
on Y'. This requirement is imposed to ensure that, when c(f, Q) has a defined
result, it preserves the Q-equivalence dass of its argument.
P. W. O'Hearn, A. ]. Power, M. Takeyama and R. D. Tennent 215
Sl(f{w)) 1
{w}
where the vertical arrow is the insertion of {w} into W. There can be no side
effects during evaluation of e(w) because, in world {w}, there are no other
states to change to!
The behaviour of commands under restrictions is quite different. Consider
the command meaning c( ·) E [comm](W x Z) corresponding to an assign-
ment Statement z := z + 1, where z accesses the Z-valued component in X x Z.
The partial function for c (idwxz) maps (w, n) to maps (w, n + 1). But we
also need to define c(f) for all other X-maps f into W x Z, including restric-
tion maps. In particular, if we consider c( r{(w, n}}) then this component of
c cannot produce an output state, because {w, n + 1) is not an element of the
world {(w, n} }. More generally, c(f)s can be defined only if (w, n + 1} is in the
range of St(f). In contrast to the previous example, command meanings are
not completely determined at Singleton worlds, just because they may change
the state.
Suppose now that we restriet to the world
Y = { (w, n) E W x Z I n is even}
and consider the composite z := z + 1 ; z := z + 1, and its semantic counterpart
c; c. Sequential composition is interpreted componentwise, so for command
meaningsc1 and c2, (c1 ;c2 }(f) is justthe composition ct(f) ;c2 (f) of the partial
functions for the components. Thus, we get that (c ;c}(idwxz)(w, n) = (w, n}.
However, (c;c)<rY)(w, n} is undefined, because c<rY)(w, n} is undefined. The
attempt to "stray" out of Y, even at an intermediate state, leads to divergence.
4.3 Non-Interference
4.3.1 Tensor Product
Intuitively, meanings a E A(W) and b E B(W) are non-interfering if neither
makes active use of any memory used by the other. We formalize this intuition
216 Chapter 18. Syntactic Control of Interference Revisited
j[
comm)(f ; rro) f 1 [comm](f ; rr,) j
c1 E [comm]W W c2 E [comm]W
Define ci x c~ E [comm](X ® Y) tobe the component-wise product map; i.e.,
(ci x c~) (g) = ( ci g) x (c~ g), using the morphism part of the cartesian product
x in the category of sets and partial functions. To get a meaning at world W
we use map f, as follows:
II (W)(c1,c2) = [comm](f)(ci x c~).
Here, X, Y, c1 and c2 are not uniquely determined, but the functoriality re-
quirements on [comm] are sufficient to ensure that this is a good definition.
The f map is what allows for a limited amount of sharing. To illustrate
this, suppose X= Y = Z ® Z, ci (id)(n1, n2) = (n2 + 1, n2) and c~(id) (n1, n2) =
(n1r n 1 + 3). Then, we can form a composite command in which ci and c~
operate on disjoint portians of the state:
ci x c~ E [comm](Z ® Z ®Z ® Z) .
Sharing can be achieved via a diagonal map
Z®Z®Z®Z
1idz ® Öz ® idz
Z®Z®Z
yielding the meaning
c = [comm](idz ® 8z ® idz)(ci x c~).
We find that c(id) (n 1, n2, n3) = (n2 + 1, n2. n2 + 3): the two middle components
in the product Z ® Z ® Z ® Z get identified, which is to say, shared, by the
diagonal map. Intuitively,
• ci corresponds to a command x := y + 1
• c~ corresponds to a command z := y' + 3, and
• c corresponds to the command x := y + 1 II z := y + 3, obtained by
parallel composition followed by Contraction of y and y',
where the identifiers correspond to evident components in Z4 and Z 3.
Thus, the semantics of II is given by combining functions on disjoint state-
sets, followed by sharing. This corresponds closely to how parallel commands
are typed: first, commands with no identifiers in common are combined, and
then sharing is introduced using the Contraction rule.
218 Chapter 18. Syntactic Control of Interference Revisited
4.3.3 Exponential
An exponential construction right adjoint to ® makes nX"P a closed cate-
gory; (A - B) (W) is defined to be the set (ordered pointwise) of families
q(X):A(X)- B(W ®X) of continuous functions indexed by worlds X, such
that, for all X-maps f: Y - X, the following naturality diagram commutes:
A(X) ---=-q..:...(X..:...>_..... B(W ®X)
A(f) j JB(ldw ® ()
be defined only if c(idw )s = s, and so, for this c, we obtain that [comm](aw )c
is everywhere-undefined.
The effect of state-change constraints on expression meanings is quite dif-
ferent. Foreach world Wand e E [T]W, [T](aw)e = e. State-change con-
straints have no effect here because expressions cannot cause side effects.
These examples suggest the following definition: a E A(W) is passive if
and only if A(aw )a = a. For example, [skip]W (a family of identity functions)
and [diverge]W (a family of everywhere undefined functions) arepassive ele-
ments of [comm] W.
The following results establish the connections between passivity and non-
interference.
Proposition 15 lfp E P(W) and q E Q(W) are passive, p # q.
Proof: The "only if" part follows from the preceding Proposition.
In the other direction, suppose that a # a; then there exist worlds X and Y,
ax E A(X), ay E A(Y), and an X-map f: W- X® Y suchthat A(f; TTo)ax =
a = A(f; rri)ay. Let Qi for i = 0, 1 be the equivalence-relation components
of f; Tri; then Uw, Qi) ; f; Tri = f; Tri, and so we get, by functoriality of A, that
A(Iw, Qi)a = a for i = 0, 1. This gives us that A((lw, Qo); Uw, Q1) )a = a; but
Uw, Qo); Uw, Qd = aw, and so a is passive. I
An object A of I)X"P is passive iff, for every world W, every a E A(W) is
passive. For example, a terminal object 1 is passive because it is a constant
functor, and [T] isapassive object because, for anyworld Wand e E [T ]W,
[T](aw)(e) = St(aw);e morphismpartof[T]
= e St( aw) is the identity function
exchange
[comm ® comm] ------=----..... [comm ® comm]
'I
[comm x comm] -----=-se~q==-u:-::e""n""""ce~--- [comm]
jn
(n1edomn
S[llt](Wt)) x ( n
1edomr
[ft](Wt)) - [()]( n
LEdom<n.n
Wt)
Note that the products in the domain of </>(W) are cpo products, whereas the
product in the co-domain is a set product. This form of semantic interpreta-
tion seems intuitively appealing because it makes the disjointness of distinct
identifiers very explicit; but it is highly non-standard.
ln this section, we show that we can define a bijection between the Stan-
dard form of semantics discussed in earlier sections and this non-standard
form. To simplify the treatment, we will consider natural transformations
17:A®B..:... C
and families of functions
</>(X, Y):A(X) x B(Y)- C(X x Y)
where X and Y are X-objects. From a natural transformation 17, we can define
a family </>(X, Y) of functions as follows:
</>(X, Y)(a',b') 17(X x Y)(A(xY)a',B(Xx)b'}
ln the other direction,
17(W){a,b) = C(f)(4>(X, Y)(a',b')}
X
where f: W - X x Y, a' E A(X) and b' E B(Y) such that A(f; rro)a' = a
and B(f; rrdb' = b must exist because (a, b) E (A ® B)(W). Naturality of 17
is straightforward, provided that 17(W)(a,b) is uniquely defined; for this, the
family </>(X, Y) must be suitably uniform. We will not bother to formulate an
explicit definition of the condition; it is a routine exercise to do so, and to
verify that the mappings 17 ...... <1> and <1> ...... 17 just given are mutual inverses,
given such a condition on the </>( · ).
5 Concluding Remarl{S
The type system presented here was actually worked out by the first author
in 1991, but lay dormant foranurober of years because it contained features
for which no satisfactory semantic explanation existed. Its revival was mo-
tivated by [31], which exhibited structure corresponding very directly to the
permeability rules in Section 2.2, and provided the first example of a model of
the SCIR type system. Reddy made the key realization that the permeability
rules of Activation and Passification correspond in his semantics to a monad
222 Chapter 18. Syntactic Control of Interference Revisited
structure. The models described in [38, 24] do not possess this structure asso-
ciated with passivity, and, in particular, only the comonadic aspect of passivity
is present in [24]. It subsequently became clear that the approach to passiv-
ity in [39, 25] has the requisite properties for the permeability rules, with the
crucial step forward being the utilization of Day's tensor product construction
(the relevance of which was suggested by A. Pitts).
Another novel aspect of Reddy's approach is that different identifiers de-
note independent "objects," where the state is implicitly represented in "his-
tories of Observations." In our model, though it is not stateless, there is also a
move away from the viewpoint of a common "global store" that programs act
upon. Specifically, in the presentation sketched in Section 4.6, and implicitly
in the standard presentation, each identifier is associated with its own state
set, disjoint from the state-set associated with other identifiers; intuitively,
each identifier denotes an object acting upon a piece of "local" state.
Syntactic control of interference is an important step toward the ideal of a
"clean" form of imperative programming. It retains basic principles of ALGOL-
like and functional programming, including equational laws such as the ß
law; this it has in common with recent work emanating from the functional-
programming community (see, e.g., [28, 21, 19]). But interference control also
begins to address some of the problems of state, such as aliasing. Functional
principles alone do not make state significantly easier to reason about, as is
abundantly clear, for example, from specification logic. Controlling interfer-
ence addresses some of the most prominent difficulties.
At present, syntactic control of interference has developed to the point
where it possesses quite satisfactory type systems and models. Nevertheless,
there are many issues that need to be addressed before the ideal of a clean
and practical form of imperative programming can be realized. The following
is a partlallist of immediately relevant issues.
1. Our example programming language does not have facilities for pro-
gramming dynamically-reconfigurable data structures of the kind often
implemented using pointers or references. Simple languages of this
form can serve as a useful testbed for ideas on integrating imperative
and functional programming, but extending the basic approach of Sei
to support coding of dynamic data is clearly crucial. It is not obvious
what the best way to do this might be.
2. A call-by-value version of Sei could have some interest. Achallenge for
such a design is to maintain a controlled form of side effects.
Acknowledgement
We are grateful to Uday Reddy for numerous discussions that have substantially influ-
enced the content and presentation of this paper.
References
[1] N. Benton, G. Bierman, V. de Paiva, and M. Hyland. A term calculus for intuition-
istic linear logic. In M. Bezen and J. F. Groote, editors, Typed Lambda Calculi
and Applications, volume 664 of Lecture Notes in Computer Science, pages 75-90,
Utrecht, The Netherlands, March 1993. Springer-Verlag, Berlin.
[2] P. N. Benton. A mixed linear and non-linear logic: proofs, terms and models
(preliminary report). Technical Report 352, University of Cambridge Computer
Laboratory, October 1994.
[3] G.M. Bierman. What is a categorical model of intuitionistic linear logic? In Pro-
ceedings of Second International Con(erence on Typed A.-calculi and Applications,
volume 902 of Lecture Notes in Computer Science, pages 78-93, Edinburgh, Scot-
land, April1995. Springer-Verlag, Berlin.
[4] R. Bird and P. Wadler. IntroducNon to Functional Programming. Prentice-Hall
International, London, 1988.
[5] P. Brinch Hansen. Structured multiprogramming. Comm. ACM, 15(7):574-78,
1972.
[6] F. Brown, editor. The Frame Problem in Artificial Intelligence. Proceedings of the
1987 workshop. Morgan Kaufmann, 1987.
[7] M. Coppo and M. Dezani. A new type assignment for A.-terms. Archiv. Math. Logik,
19:139-156, 1978.
224 Chapter 18. Syntactic Control of Interference Revisited
[8] B. J. Day. On closed categories of functors. InS. Mac Lane, editor, Repons o(the
Midwest Category Seminar, volume 137 of Leerure Notes in Mathematics, pages
1-38. Springer-Verlag, Berlin-New York, 1970.
[9] S. Eilenberg and G. M. Kelly. Closed categories. In S. Eilenberg et al., editors,
Proceedings o( the Con(erence on Categorical Algebra, pages 421-562, La Jolla,
California, 1965. Springer-Verlag, NewYork, 1966.
[10] P. Freyd, P. W. O'Hearn, A. J. Power, M. Takeyama, and R. D. Tennent. Bireflec-
tivity. In S. Brookes, M. Main, A. Melton, and M. Mislove, editors, Mathematical
Foundations of Programming Semantics, Eleventh Annual Con{erence, volume 1
of Electronic Notes in Theoretical Computer Sdence, Tulane University, New Or-
leans, Louisiana, March 29-April11995. Elsevier Science.
[11] j.-Y. Girard. Linear logic. Theoretical Computer Science, pages 1-102, 1987.
[12] J.-Y. Girard. On the unity oflogic. Annals o(Pure andApplied Logic, 59:201-217,
1993.
[13] C. A. R. Hoare. An axiomatic basis for computer programming. Comm. ACM,
12(10):576-580 and 583, 1969.
[14] C. A. R. Hoare. Hints on programming-language design. In C. Bunyan, editor,
Computer Systems Reliability, volume 20 of State of the An Repon, pages 505-34.
Pergamon/lnfotech, 1974. Also pages 193-216 of [15].
[15] C. A. R. Hoare and C. B. Jones, editors. Essays in Computing Sdence. Prentice Hall
International, 1989.
[16] P. Hudak. Conception, evolution, and application of functional programming
languages. Computing Surveys, 31:359-411, 1989.
[17] j. Hughes. Why functional programming matters. The Computer Journal, 32:98-
107,1989.
[18) G. M. Kelly. Doctrtnal adjunctions. In G. M. Kelly, editor, Category Seminar:
Proceedings, Sydney Category Theory Seminar, 1972/73, volume 420 of Leerure
Notes in Mathematics, pages 257-280. Springer-Verlag, New York, 1974.
[19) j. Launchbury and S. Peyton jones. State in HAsKELL. LISP and Symbolic Compu-
tation, 8(4):293-341, December 1995.
[20] S. Mac Lane. Categories {or the Working Mathematician. Springer-Verlag, New
York, 1971.
[21] M. Odersky, D. Rabin, and P. Hudak. Call by name, assignment, and the Iambda
calculus. In POPL [29], pages 43-56.
[22] P. W. O'Hearn. The Semantics o( Non-lnter{erence: A Natural Approach. Ph.D.
thesis, Queen's University, Kingston, Canada, 1990.
[23] P. W. O'Hearn. Linear logic and interference control. In D. H. Pitt et al., editors,
Category Theory and Computer Science, volume 530 of Leerure Notes in Computer
Science, pages 74-93, Paris, France, September 1991. Springer-Verlag, Berlin.
[24] P. W. O'Hearn. A model for syntactic control of interference. Mathematical Struc-
tures in Computer Sdence, 3(4):435-465, 1993.
[25] P. W. O'Hearn and R. D. Tennent. Semantical analysis of specification logic, 2.
Information and Computation, 107(1):25-57, 1993. See Chapter 14.
[26] P. W. O'Hearn and R. D. Tennent. Parametricity and local variables. ]. ACM,
42(3):658-709, May 1995. See Chapter 16.
[27] F. J. Oles. A Category-Theoretic Approach to the Semantics o( Programming Lan-
guages. Ph.D. thesis, Syracuse University, Syracuse, N.Y., 1982. See Chapter 11.
P. W. O'Hearn, A. ]. Power, M. Takeyama and R. D. Tennent 225
Contents
1lntroduction 227
2lnterference-controlled ALGOL 233
3Objects 242
4A Domain-Theoretic Model of Objects 251
4.1 Object spaces 255
4.2 Functions on objects 257
4.3 Semanties 262
5 Passivity 264
5.1 Active-passive spaces 265
5.2 Semantics 269
6 The Structure of Object Spaces 270
7 Example Equivalences 274
8 Extensions 278
9 Conclusion 281
Acknowledgements 282
References 282
A Categorical Structure of the Model 287
B Proof of Operational Adequacy 292
1 Introduction
Semantics of programs that deal with state ("imperative programs") is a long-
standing research problern in the theory of programming languages. Tradi-
tionally, such programs are given semantics in terms of global states. (See, for
example, [Sto77].) A global state is an element of a suitably large set that gives
the value of every storage variable at a given instant of execution, e.g., the set
of all sequences of data values (State = Val*). The allocation of a new local
variable is interpreted as enlarging the sequence by adding a new component,
and deallocation is interpreted as shrinking the sequence by deleting the new
component.
First appeared in LISP and Symbolic Computation, 9(1):7-76, 1996. Permission to reprint granted
by Kluwer Academic Publishers.
articles [OT92, Ten95] and the text [Ten91] give good overviews of the extant
techniques.) Essentially, all these solutions view commands not as state trans-
formers, but as families of state transformers indexed by state sets (formal-
ized as functors in the categorical sense). Procedures are viewed as families of
functions with appropriate uniformity conditions (typically naturality). While
these solutions lose some of the simplicity of the global-state semantics by
moving to second-order concepts (like functors and natural transformations),
they are still unable to represent local-variable abstractions sufficiently to val-
idate equivalence (1).
Another issue that one must address in the semantics of imperative
programs is that state changes are actually "changes," an issue first raised
in [OT95]. Running a command causes the state of the program tobe destruc-
tively overwritten by a new state, and it is not possible, in general, to revert
to the original state. If, for example, the procedure p in (1) runs its argument
command, the value of x would get incremented and there is no way for the
procedure to revert to the original value of x. The following equivalence, due
to P. W. O'Hearn, makes this explicit:
(new[int] x. x := 0; p(x := x + 1); ü x > 0 then diverge else skip)
(2)
- p(diverge)
The equivalence must hold because both sides diverge if p runs its argument.
(Again, one can use commands with observable effects in place of diverge to
create similar examples.) But, state transformers are mathematical functions.
They have no notion of "change" built-in. The state transformer representing
p(x := x + 1) may very weil yield the original state even after computing new
states. The following function p of type [State- State] - [State- State]
represents such a "snap-back" operation:
(f) = ,\ { s, if f(s) is defined
P s· undefined, otherwise
Thus, the equivalence (2) falls in all known semantic models of imperative
programs.
We believe that most of these difficulties accrue from a premature focus on
states. Our intuitions suggest that states have to do with the intemal structure
of implementations, not the Observable behavior. For example, in classical au-
tomata theory, the internal structure of machines is defined in terms of states,
but the observable behavior is defined in terms of languages or sequential
functions that the machines recognize or compute. If the meaning of program
phrases is similarly specified in terms of observable behavior, we might fi-
nesse all the intricate mathematical issues regarding state sets in higher-order
programming languages.
The author initiated a fresh approach to the semantics of imperative pro-
grams in the summer of 1992 with these considerations. The inspiration came
from linear logic [Gir87a] and its close connection to Reynolds' syntactic con-
trol of interference [Rey78] as espoused by O'Hearn [O'H91]. Being a radically
different approach to the semantics of imperative programs, it could initially
be stated only in very theoretical terms [Red93c]. However, it is now possible
230 Chapter 19. Global State Considered Unnecessary
Related work
The previous work on semantics of local variables was already mentioned. In
recent work, carried out almost in parallel with the present work, O'Hearn and
Tennent [OT95] have used the ideas of relational parametricity to character-
ize the necessary uniformity conditions for procedures. Sieher [Sie93, Sie94]
presents a similar model in the context of locally complete partial orders.
These recent models do validate equivalence (1) and other equivalences having
3The application of object-based semantics to full ALGOL is explored in the later work [OR95].
However, our experience suggests that this necessarily involves global states and functor
categories.
232 Chapter 19. Global State Considered Unnecessary
Since we use concepts of "objects" pervasively in this work, one might won-
der if there is some link with the semantic models of object-oriented programs.
It is not yet clear if such is the case. The majority of the semantics work in
object-oriented programming seems focused on unraveling inheritance. The
issues of state are rarely addressed. The work that does address state, such
as [RK94], uses the traditional state-based approach. Some of the work on
inheritance [PT93], however, suggests that the issues of state and inheritance
might indeed be related.
Overview
We first review the language of interference-controlled ALGOL, which forms the
subject of this study (Section 2). In Section 3, we introduce the basic concepts
of object-based semantics informally and motivate the issues. Sections 4 and
5 define the formal aspects of the model. Section 6 is devoted to a brief study
of the semantic domains obtained. In Section 7, we illustrate the semantics by
reasoning about several example equivalences. Section 8 briefly reviews some
possible extensions to the language and semantic framework.
In Appendix A, we gather together the salient mathematical structure of
the model in a categorical framework. Appendix B contains a proof of compu-
tational adequacy of the semantics.
2 Interference-controlled ALGOL
lDEALIZED ALGOL, due to Reynolds [Rey8lb], is a clean integration of fune-
rtonal and imperative programming features. It is a typed lambda calculus
whose primitive types allow for state manipulation. These primitive types in-
clude:
• var[6]: variables holding 6-typed values,
• exp[o]: state-dependent expressions giving 6-typed values, and
• comm: state-modifying commands.
Thus, the type system of ALGOL is described by the following syntax of types:
data types o .. - int I bool I ...
(phrase) types e .. - var[o] I exp[o] I comm I el Xßz I el - ßz
-----Id
li 1r, x: 9 1- x: 9
li 1 r, x: 9 1- P : 9'
li Ir 1- ?..x.P: 9- 9' - 1
li I 1- P : 9I - 82 li 1 r 1- P : 9I - p 82
" Promote
" - p 172
li I 1- P : 17I li I r 1- p : 9I - 92 Derelict
The two rules Activate and Passify let identifiers move between zones. The
rule Activate says that any passive free identifier can be regarded as an active
free identifier. (This is necessary for binding it by the rule -1.) The rule
Passify says that any active free identifier can be regarded as a passive free
identifier as long as it is used in a passive phrase. (Note that this is the only
place in the type system where passive types are distinguished from other
types.) This allows us to derive, for example:
v: var[int] I 1- deref v : exp[int]
Even though the identifier v is of an active type var[int], it is used passively in
deref v because the latter is of a passive type exp[int]. This curious form of
the Passify rule has a direct correspondence with the semantics described in
Section 5.4 Since v is regarded as a passive free identifier, one can use Contr
to further derive
v:var[int] I 1- plus (derefv) (derefv): exp[int]
Finally, the last two rules allow a function phrase to be given a passive
function type provided it has no active {ree identi{iers and to forget the fact
that a function phrase is passive.
The following is a sample of constants one finds in an ALGOL-like language.
0 exp[int]
+ exp[int] X exp[int] - p exp[int]
skip comm
comm x comm - p comm
comm - p (comm- comm)
new[ö] (var[ö] - comm) - p comm
:=o var[o] x exp[ö] - p comm
derefo var[ö] - p exp[ö]
ife exp[booll x e x e - p e
rece ((} - p e> - p e
For convenience and clarity, we associate with them the concrete syntax shown
in Table 2. The parallel composition combinator C1 II Cz is added to show the
analogy with function application: the two subcommands C1 and Cz cannot
share active free identifiers. The construct new[8] x. C creates a local variable
and binds it to the identifier x within the scope of C. Since it is a binding
construct, it is formally treated as a second-order function. We often omit
writing the combinator deref except for clarity in some examples. We use
undef as a short form for the diverging phrase rec x. x.
4 The author is informed that P. W. O'Hearn developed this type system in 1991, but it was not
made public because no semantic justification for the Passify rule was known. Its revival was
made possible by the present semantics-more precisely, a variant of it presented in [Red94],
which was itself motivated by the subject-reduction issues with SCI. This mutual reinforcement
is an excellent example of the interplay between syntax and semantics.
Uday S. Reddy 237
rr 1 r 1- Et : exp[int] rr 1 r 1- E2 : exp[int]
rr 1 r 1- o : exp[int] rr 1 r 1- Et + E2 : exp[int]
rr 1 r 1- v: var[ö] rr 1 r 1- E: exp[ö]
rr I r 1- skip : comm rr I r 1- v := E : comm
rr 1 r 1- E : exp[bool] rr 1 r 1- Pt : e
rr 1 r 1- P2 : e rr 1 x: e 1- P : e
rr I r 1- if E then Pt eise P2 : (} rr I 1- rec X. p : (}
Discussion
lf we disregard the higher types such as lh x lh and lh - lh we obtain the
language of basic imperative programs. This language is essentially defined by
the combinators of Table 2. Its programs have standard semantics in terms of
state transformation (or execution). When we form a command using higher-
type abstractions, the semantics of lambda calculus becomes applicable. The
command phrase is interpreted by first reducing it, using the standard re-
duction rules such as ß-reduction, until one obtains a command phrase that
can be executed. 5 Thus, the semantics of IDEALIZED ALGOL is an orthogonal
combination of typed lambda calculus and the language of basic imperative
programs [Rey8lb, WF93].
As a simple example, consider the function
twice: comm- comm
twicec = c;c
When applied to a command c, the function produces a command that has the
effect of running c twice. For example, twice (print * produces a command 1 1
)
that prints two *'s. It is important to note that the function twice itself does
not "run" any commands. It is an honest-to-goodness function that produces a
command as its result. This result must then be used in a context where it will
be executed to produce any effect. lf it is used in a context where it is ignored,
no effects are produced. For example, if ignore is the function .\c. skip, then
ignore ( twice ( print * 1 ) ) produces no effect even when it is run.
1
Since it has no active free identifiers, it cannot cause any state modifications
to non-local variables.
As a generalization of twice, consider a function that maps a command c
to an n-fold composition of c with itself:
times: exp[int] -p comm - p comm
times n c = if n = 0 then skip
else c; times (n - 1) c
If n is any natural number, times n is a function of type comm - p comm.
This shows that every natural number can be encoded as a function of type
comm - p comm. It turns out that the converse is true as well, i.e., every func-
tion of type comm- p comm, other than the undefined function, is equivalent
to timesn, for some natural number n. Thus, functions of type comm-pcomm
are similar to Church numerals.
Control structures for commands can be defined as functions on com-
mands. For example, the function:
while: exp[bool] x comm - p comm
while (b, c) = if b then c; while (b, c)
eise skip
defines a while loop control structure. Novel control structures can be defined
just as easily. For example, by adding recursive data types like lists, we may
define higher-order operations for commands analogous to map:
mapdo: (9- comm) -P (Iist e- comm)
mapdo f [ ] = skip
mapdo f (x :: xs) = fx; mapdo f xs
A counter object has two methods: the first, of type exp[int], returns the
current value of the counter and the second, of type comm, increments the
value of the counter.
Uday S. Reddy 239
We often use sugared record notation for product types so that mnemonic
names for the fields are available. ln this notation, we write mkcounter as:
counter = [val: exp[int] x inc: comm]
mkcounter:var[int] - p counter
mkcounterv = [val = derefv, inc = (v := v + 1)]
We would then write c.val and c.inc for the methods of a counter c.
To build and use a counter object in a larger program, we use one of two
techniques. By the first technique, we might use an existing variable v to
build a counter object, bind the counter to an identifier c, and then use c in a
command. This corresponds to a program structure of the following form:
Iet c = mkcounter v
inP
where P is a command phrase. The let-block is desugared in the Standard
fashion: (.\c.P) (mkcounterv). ln this form of a program, we must think of
the variable v as the "raw material" used for building the counter c. While
the counter is in use, we cannot directly access this raw material. Doing so
would cause interference. Indeed, SCI ensures that v does not occur free in
the phrase P. However, the variable v can be used in the commands that
precede or follow the let-block. ln this sense, mkcounter builds a temporary
"scaffolding" araund the variable v which is in effect during the execution of P.
The second technique is to create a new variable and erect a permanent
scaffolding araund it in the form of a counter. This is expressed by the follow-
ing function:
newcounter: (counter- comm) - p comm
newcounter k = new[int] v.
v:= 0;
k ( mkcounter v)
This function takes as its parameter a counter "consumer" k. It creates and
initializes a new variable v and supplies the consumer with a counter built
from v. To create and use a counter object, we use a program structure of the
form:
newcounter .\c.
p
where P is a command phrase. This corresponds to what would be written as
{counter c; P}
in a more traditional object-oriented programming language. The function
newcounter corresponds to a "dass". Its task is to create new instance objects
of its dass.
We dose this discussion with a Ionger example that puts many of these
ideas together. A "histogram object" is an array of counters, i.e., a counter-
valued function on a finite range of integers, while a histogram is an array of
integers:
histobj = exp[int] - counter
histogram = exp[int] - exp[int]
240 Chapter 19. Global State Considered Unnecessary
where ~~~' isavariable type context xl:var[c5d, ... l ... ,xn:var[c5nl· We refer
to such phrases as semi-closed phrases. If ~I~' is a variable type context,
a ~I~' -state isafinite map of the form [x1-h •... ,Xn-inl. where each ik is a
data value of type Ök. Note that the variables mapped by the state are precisely
the variables in the type context ~I~'. We use the symbol u to range over such
states. The notation u[x-i] means u with its x component updated to i, and
u 1 e u 2 denotes the join of two states with disjoint domains.
We defi.ne two families of relations:
• (u,E) .u.~.~~ ..s i, where u is a ~1~'-state, E an expression with typing
~W t- E: exp[c5], and i a data value of type c5.
• (u, C) .u.~.~'.comm u', where u and u' are ~IA'-states and Ca command
with typing ~I~' t- C: comm.
The first family of relations denotes the evaluation of an expression in a state,
and the second family denotes the execution of a command in a state to give a
new state. Table 3 gives an inductive definition of the relations for well-typed
phrases. For the parallel composition C1 II C2, execution involves partitioning
the state into u1 and u2 based on the free identifiers occurring in C1 and C2.
For the new[c5] combinator, we are assuming that x has been renamed tobe
distinct from the variable identifiers in u. The execution involves adding a
new component for x in the state and discarding it after the execution of C.
This represents the "stack discipline" of local variables in ALGOL. The new
components are initialized to a designated data value init[c5] upon creation.
The last two rules are for phrases derived by the Contr rule. Strictly speaking,
we should also have rules for phrases derived by Activate and Passify, but
these rules do not affect the execution relations.
Proposition 3 For all semi-closed expressions E and commands C:
1. I( (u, C) .U. u' then, (or allpassive (ree identifiers x o(C, u(x) = u' (x).
2. I((u,E) .U. i and (u,E) .U. i' then i = i'.
3. I((u,C) .U. u' and (u,C) .u. u" then u' = u".
Proof: By induction on the type derivation of the phrase. I
242 Chapter 19. Global State Considered Unnecessary
(u,EI) U h (u,E2) U i2
(u,O) U 0 (u,E1 +E2) U h +i2
3 Objects
A central tenet of the present semanlic model is that states are not simply
values, but rather attributes of entities that persist in time. To this end, we
postulate entities called objects.6 An object has an intemal store (which we
6 In an earlier version of this paper [Red93a), two kinds of values called "passive" and "active•
values were mentioned. "Objects" of the present paper subsume both these values, though our
focus is mainly on active values.
Uday S. Reddy 243
Figure 1: Objects
form a sequence. This gives a local notion of "time" for each object. The oper-
ations occurring earlier in the sequence are ''before" the operations occurring
later.
Thesis 2 The behavior of an object is in generat a(fected by its past history of
operations.
This is the sense in which objects are "dynamic" or "have state". The past
history determines the current state of the object which, in turn, determines
the current behavior.
State machines as in classical automata theory have both these properties.
We use them as a metaphor for motivating the various issues surrounding ob-
jects. Figure 2 shows the state transition diagrams of several example objects.
The nodes in these diagrams should be interpreted as states and arcs as state
transitions.
The first object, called a "stepper," has a single operation that changes the
intemal state and produces an integer. (It incrementally "steps through" a
sequence of integers over a period of time.) We label each transition by the
integer that is produced. In any given run of a program, a single sequence of
integers can be extracted from the object. Similarly, for any object, a single
sequence of observations can be made in any given run of a program. We will
call such sequences of observations traces, in analogy with Hoare's terminol-
ogy for processes [Hoa85], and the set of all traces that can be observed from
the object its trace set. Note that the trace set is a state-free description of the
observable behavior of an object, much like the notion of a "language" of an
automaton. The trace set of the stepper object consists of sequences:
{), {0), {0,1), {0,1, 2), ...
The trace set is prefix-closed because every state of the object is (implicitly)
regarded as a terminal state.
Figure 2(b) shows the behavior of the counter object discussed in Section 2.
Invoking its val operation retums the current value of the counter and makes
no change to the state. Invoking the inc operation changes its state and retums
a dummy completion signal that we write as *· So, the trace set of the counter
object is:8
{), {val.O}, {inc.*}, {val.O,val.O}, {val.O,inc.*},
{inc.*,val.1}, {inc.*,inc.*}, ...
assuming the counter starts with an initial value of 0.
Our thesis is that all types of ALGOL similarly give rise to objects. Fig-
ures 2(c-e) show canonical example objects corresponding to the primitive
types of ALGOL. A command object is an object with a single command as
its operation. Invoking it potentially changes the intemal state and returns a
completion signal. For example, defining:
inc:var[int]-p comm
inc(x) = (x := x + 1)
8 The notation "val.O" means the value 0, tagged with a symbol "val" for identification purposes.
Mathematically, it is just an ordered pair (val, 0).
Uday S. Reddy 245
C7
in Figure l(b) is quite appropriate. At the same time, the function carrying out
the simulation has no direct access to the internal state of the original object.
It can only effect state changes via the operations of the object.
This fact allows us to ignore the internal states of objects and work entirely
at the Ievel of operations and behaviors. It Ieads to a domain-theoretic model
of interference-controlled ALGOL presented in the next two sections. Yet, im-
perative programs are often thought of as acting on state sets. In the remain-
der of this section, we indicate how object behaviors relate to an explicit-state
point of view.
write (x,y) :: (x',y'), when this property holds. Then a function f:A- Bis
nothing but a relation f !;;;; A x B whose input-output pairs are pairwise con-
sistent. The causality information missing in relations can be brought back by
defining a separate consistency relation in this fashion. The advantage is that
complex causality relationships can be expressed using consistency relations.
So, in a first attempt, we Iet each type 0 determine a set of values ~ and
a binary consistency relation :: on Q x ~ x Q. A transition map is a relation
oc !;;;; Q x ~ x Q such that the tuples of oc are pairwise consistent. For command
objects, we Iet ~ = { *} and define the consistency relation for transition maps
tobe:
(q}, *,qi_):: (q2, *·q~) = (q1 = q2 = qi_ = q~)
For integer-expression objects, Iet ~ = Z and define the consistency relation
for transition maps to be:
(qi.a1,qi_):: (q2,a2,q~) = (q1 = q2 = a1 = a2 A qi_ = q~)
For integer-variable objects, ~ is defined to be the disjoint union of sets
Z + Z = { get.a : a e Z } u { put.b : b e Z }
The consistency relation for transition maps is:
(q1.h.ai.qi_):: (q2,12.a2,q~)=
(h = 12 = get = q1 = q2 = a1 = a2 A qi_ = q~) A
(h = 12 = put = q1 = q2 A a1 = a2 = qi_ = q~)
Note how the consistency relation bandies the fact that the get opera-
tions have output information and the put operations have input informa-
tion. More generally, suppose that types 01 and 02 have sets of values ~ 1
and ~ 2 and transition-consistency relations :: 1 and :: 2 respectively. Then,
for the product type 01 x fh, we Iet ~ be the disjoint union ~1 + ~2 =
{La: a e ~1} u { 2.b: b e ~2 }. The consistency relation for transition maps
is
(qi. i.a1, qi_) :: (q2,j.a2, q~) = (i = j = (qi. a1, qi_) ::, (q2, a2, q~))
In all these cases, the consistency relation for transition maps can be given
uniformly, provided we admit a consistency relation for values themselves.
This Ieads to a second attempt where we associate with each type 0, a set
of values ~ and a binary consistency relation :: on ~ itself. The consistency
relation for transition maps is then defined uniformly by:
(q1.a1,qi_):: (q2,a2,q~) = (q1 = q2 = a1:: a2) A (4)
(q1 = q2 A a1 = a2 = qi_ = q~)
lf we assign the following sets of values and consistency relations for the types
mentioned above:
comm ~ {*} identity relation
exp[int] ~ z identity relation
var[int] ~ Z+Z 1.a :: 1' .b = 1 = 1' = get = a =b
01 X 02 ~ ~1 +~2 i.a :: j.b = i =j = a ::1 b
250 Chapter 19. Global State Considered Unnecessary
it can be verified that definition (4) gives the same consistency relation for
transition maps as defined earlier. The consistency relation assigned to value
sets may be intuitively understood as follows: a 1 :: a 2 means that either a 1
and a2 have differing input information or they have the same output informa-
tion. This motivates the condition in (4) that, whenever a 1 and a 2 are observed
in the same state of an object, they must be consistent. Conversely, define the
inconsistency relation:::: on ~ by a1 ::: a2 iff a1 = a2 v ...,(a1 :: a2). Now, a 1 :::: a 2
signifies that a 1 and a2 have the same input information.
Next, we extend the consistency relations to sequences ~*. The extension
of transition maps to sequences is the natural one: (q, (a 1, ... ,an),q') E a iff
there exist states qo, ... , qn suchthat q = qo, qn = q' and
(qo,a1,qi), ... ,(qn-loan,qn) E OC
The relation a must be a transition map consistent in the sense of (4).
This forces a consistency relation on sequences in ~*, viz., (a~o ... , an} ::
(a;, ... , a:n) iff
(a~o ... , ai-1} = (a;, ... , a;_ 1} => a, :: a; for all i = l, ... , min(n, m) (5)
Note Thesis 2 at play here. The past events in a trace a1, ... , at-1 determine
the future event a1 (modulo its own input part). It is easy to see that whenever
oc !;;;; Q x ~ x Q is a transition map, its extension a !;;;; Q x ~* x Q is also a
transition map.
As an example, a storage cell object with the behavior shown in Figure 2(e)
can be defined by taking the state set to be the set of integers and the transi-
tion map given by:
(i,get.j,i') E oc = i = j "i = i'
(6)
(i,put.j,i')Eoc = i'=j
It may be verified that this is a proper transition map satisfying the consis-
tency condition (4).
The trace set of an object (Q, oc) at a state q E Q is defined by
La(q) ={XE~*: 3q' E Q. (q,x,q') E Li}
This is a pairwise consistent set by the above consistency relation for ~*.
Finally, we consider "functions" on objects. A function of type lh - 62
must map objects with transition maps oc1 !;;;; Q x ~1 x Q to objects with tran-
sition maps oc2 !;;;; Q x ~2 x Q. As argued previously, any such "function" must
be determined by a pattem map h !;;;; ~i x ~2. The map records the sequence of
input instructions (in ~n required to simulate an output instruction (in ~ 2 ). To
see what kind of a consistency relation must be satisfied by h, Iet (x, b) E h.
First, the input information of b must uniquely determine the input informa-
tion of x. So, if (x', b') E h isanother pair, we must have:
b:::: b' => x::: x' (7)
Second, for any given input information of b, the output information of x must
uniquely determine the output information of b, i.e.,
b :::: b' 1\ x = x' => b = b' (8)
Uday S. Reddy 251
where
(q,b,q') E CX2 = 3X E ~j. (X,b) Eh 1\ (q,x,q') E CX1
Note that x in the above existential is unique.
An object of a function type lh - lh is similar to a function. Its value
set is ~F = ~i x ~2 and its consistency relation is defined by (9). However,
being an object, it can have its own intemal store and use it to control the
Simulation of operations in a history-sensittve fashion. So, its application to
an argument object of type lh yields an object whose behavior is controlled
by both the store of the function object and the store of the argument object.
So, we can view a function object (Qp, txp !;;; Qp x ~F x Qp) as determining a
function from objects (Q1, cxd to objects (Qp x Q1. cx2) with transition maps
CX2 !;;; (Qp X QI) X ~2 X (Qp X Q1) given by
Note that down-closure implies that lub's and glb's are given by union and
intersection respectively. The bottom element is 0. Experts in domain
theory might note that, up to order-isomorphism, coherent spaces are pre-
cisely dl-domains that are coherent (all pairwise-consistent sets have lub's) and
atomic (the elements covering .L form a sub-basis). A linear function f: A - r B
between coherent spaces is a continuous function such that
1. xu y E A => f(x n y) = f(x) n f(y), and
• A binary relation of "consistency" ::: A f; lAI x lAI, defined by a ::: A a' <=>
{a,a'} E A. Note that :::Ais reflexive and symmetric.
We can recover the domain A from the web. The elements of A are all pairwise
consistent sets (also called "coherent sets") over lAI. So, webs (countable sets
with reflexive-symmetric relations) form canonical representations for coher-
ent spaces, and we use them as such.
We argued in Section 3 that the types of interference-controlled ALGOL
should determine sets with binary consistency relations. We now see that such
sets with consistency relations represent coherent spaces-a well-known dass
of domains. The tokens of a coherent space should be regarded as atomic
pieces of information about its elements which can be extracted in a single
"use" of an element. The consistency relation ::: A states whether two such
pieces of information can coexist in an element.
Table 4 gives the definitions of coherent spaces we use in this paper (via
their web representations). The spaces bool and int are flat domains. The
domain A1 x A2 is the product of A1 and A2. A token of A1 x A2 carries an
atomic piece of information about either an element of A1 or an element of A2.
So, IA1 x A2l is the disjoint union IA1I + IA2I- The token fst.a corresponds to
the element conventionallywritten as (a, .L) while snd.a corresponds to (.L, a).
Since the two kinds of tokens are considered consistent, the domain A1 x A2
Uday S. Reddy 253
{fst.al, snd.a2}
{fst.ad
/~ {snd.a2}
~/ {}
When we use Iabelied products, such as [h:A1 x lz:Az], we use the Iabels h
and lz as the tags for tokens instead of fst and snd.
The domain A ® B is called the tensor product. It is an "eager" kind of
product in the sense that each token of A ® B contains information about both
an A component and a B component. 10 Sometimes, we use Iabelied tensor
products such as [h:Al ® • • • ®ln:Anl· In that case, the tokens ofthe coherent
space are treated as finite maps [h -a1o ... , ln-an] where each a, E lAd. The
domain T has a single undefined element and the domain 1 is a two point
lattice. It is easily verified that T and 1 are the units of x and ® respectively:
A X T :: A :: T X A, A ® 1 :: A :: 1 ® A.
Under the action of linear functions, the product type behaves as a "choice"
rather than a conventional product. lf f: A x B - L C is a linear function, then
the action of f on {fst.a, snd.b} is determined by its action on {fst.a} and
{snd.b}. In other words, every atomic piece of information in the output of
f is derived from either the A or the B component of the pair, but not both.
For instance, there is no linear function of type int x int - L int that represents
10The elements of A®B do not correspond to any kind ofpairs (whichjustifies the name "tensor"
product). We use this construction in the context of linear functions (Section 4.2), where it will be
seen that the elements are not of much consequence.
254 Chapter 19. Global State Considered Unnecessary
that linear maps are one-to-one with linear functions, there is a corresponding
partial order on linear functions which may be expressed as follows:
f!;;;;; g = Vx,y E A.x ~ y = f(x) = f(y) ng(x)
This is called the stable order or Berry order [Ber78]. It is notably different
from the pointwise order commonly used with continuous functions on Scott
domains. 11
There is an isomorphism of linear functions:
A ® B - r C :: A - r (B _.., C) (11)
which establishes that coherent spaces and linear maps form a (symmetric
monoidal) closed category. The maps in the two directions give the stan-
dard combinators for higher-order functions. For the left-to-right direction, if
f: A ® B - r C is a linear map, the corresponding map (curry f): A - r (B _.., C)
is defined by
curryf = {a- (b- c): (a,b)- c E f}
It can be verified that this is a proper linear map and that curry is invertible.
11 The stable order is often looked upon with suspicion because it gives inappropriate results
for functional programming languages like PCF. However, we find that the stable order matches
quite weil with imperative programming languages. See Section 6.
256 Chapter 19. Global State Considered Unnecessary
where ii, ... , in stand for arbitrary integers in Iintl. The notation ( * )n means
a sequence of n *'s.
This map must be understood as saying that whenever the command
inc(x) is demanded n times, an n-fold repetition of a get-put sequence is de-
manded on x, thereby causing the corresponding changes in the internal state
of x.
Note that there is no requirement in the above map that each ik+I is equal
to ik + 1. While this property is satisfied by storage cells, the meaning of inc
should be applicable to all objects of type tvar[int], not only storage cells.
There is another ingredient to object functions. They produce an object
behavior from the behaviors of their input objects, but other than this, they
do not depend on anything else. They do not have any hidden state in which
they can remernher information. So, if they simulate a particular operation of
the output object by a series of Operations on the input object, they should
also simulate any future instance of the same operation by the same series of
operations on the input object. Notice this in the linear map of the function
inc in the above example. Every "*"in the output behavior "comes from" a
sequence (get.i, put. (i + 1)) in the input behavior. This is independent of the
position of "*" in the output trace. Functions that behave in this form may
be described as being "passive", "history-free", or "regular". This leads us to
postulate:
Thesis 4 Object functions are regular functions.
The notion of regular functions is formalized as follows:
Definition 10 Given object spaces t A and t B, a regular function f: t A - R tB
is a linear function (viewed as a linear map) that satisfies:
1. (SI ,... ti), ... , (Sn,... tn) E f implies (SI··· Sn,... ti · · · tn) E f, and
f implies there exists a decomposition s
2. (s .... ti · · · tn) E = SI · · ·Sn
suchthat (SI .... ti), ... , (Sn .... tn) E f.
Uday S. Reddy 259
Multipleargum ents
The above considerations generalize to object functions with multiple argu-
ments in a Straightforward fashion. Consider a function with argument objects
of types t A1, ... , t An and result of type t B. This should be visualized as in
Figure 1(b) except that there aremultiple inner objects involved in building the
result object. Carrying out an operation of the result object involves perform-
ing some number of operations on potentially all the inner objects. Therefore,
the function must first be a linear function of type tA1 ® · · · ® tAn -r tB.
The elements of its linear map are pairs of the form s ,..... t where s is a tuple
of traces s1, ... , Sn. Secondly, the function must be a regular function in the
following sense:
Definition 12 A regular map f: tA 1 ® · •· ® tAn -R tB isalinear map satis-
fying:
1. s1 ,..... t1, ... , Sn ,..... tn E f implies (sl · · · Sn ,..... t1 · · · tn) E f, and
2. (s ,..... h · · · tn) E f implies there exists a decomposition s = s1 · · ·Sn
suchthat (SI ,..... t1 ), ... , (Sn ,..... tn) E f.
where concatenation s 1 · · · Sn on tuples of traces is defined pointwise.
Note that the product t A1 x · · · x t An would not be useful here since a linear
function can only use information from one of the components of a product;
cf. the discussion of Table 4.
Proposition 11 generalizes as:
(Q9 t A;) - L B ~ ( Q9 t A;) - R tB
i i
The latter acts on a single object with two operations of types A and B respec-
tively. The element fixA [f] denotes the least fixed point of an object function
f:A- A, i.e., the least element x E A suchthat f(x) = x.
In Table 8, we list the functions involved in interpreting the constants of
interference-controlled ALGOL . The function seq (which interprets ";") acts on
an object with two command operations and produces a command that runs
the two commands in sequence. The function par (which interprets "II ") acts
on two independent command objects and runs them in parallel (by extracting
*
a token from each). The remaining functions can be understood similarly,
except for new.s which involves a higher·order type discussed below.
Function types
An object function of typeA-B isalinear function tA -r B. Since allsuch
functions are representable by the coherent space t A - B, this directly gives
us a representation of the function space for interference-controlled ALGOL.
Explicitly, define the function space A => B as a coherent space with:
lA =>Bi = { s ..... b: s E ltAI Ab E IBI}
(S ,_ b) =A=B (S' ,_ b') <=> (S =tA S' ==> b ::B b' A
s =tA s' Ab= b' ==> s = s')
The function space satisfies an order-isomorphism:
X, A - B ~ X- (A => B) (14)
as a direct consequence of isomorphism (11). Suppose X is the se-
quence X1, ... ,Xn. Then, an object function X,A- Bis a linear function
tX1 ® • • • ® tXn ® tA -r B. By (11), such linear functions are one-to-one with
linear functions t X1 ® • • • ® t Xn - r ( t A - B) and these are nothing but ob-
ject functions X- (A => B). The curry combinator that maps f:X,A- B to
(curry f):X- (A => B) is given by
curryf = {r- (s ..... b): (r,s ..... b) ef}
262 Chapter 19. Global State Considered Unnecessary
skip:comm {*}
seq: comm x comm- comm {{fst.*,SDd.*) ~ *}
par. comm, comm- comm {((*), (*)) .... *}
condA: exp[bool] x A x A - A { (fst.tt, snd.a) .... a : a e lAI } u
{ (fst.ff, third.a) .... a: a e lAI }
assign6 :var[8] x exp[8]- comm { (snd.i,fst.put.i) .... *: i e 181}
deref6 : var[8] - exp[8] { (get.i) .... i: i e 181}
new.s: (var[8] - comm) - comm { (s .... *) .... *: s E cellinit[c5]}
id e
[II 1r, x: 1- x: 01 {17o[x-(a))- a}
x1 [II 1 r 1- (P,Q): 61 x 62] { 11 - fst.a : 11 - a E [II I r 1- P : Od } u
{ 11 - snd.a : 11 - a E [II I r 1- Q : 62]}
xlX [II I f 1- fst(P): 911 { 17 - a : 17 - fst.a E [II I r 1- p : 91 X 92]}
x2X [II I r 1- snd(P) : 62] { 11 - a : 11 - snd.a E [II I r 1- P : 61 x 62]}
-1 [II Ir 1- Ax:9.P: 9- 9'] { 11- (s- b) :
11$ [x-s]- b E [II 1r,x:e 1-P: 9']}
-x [II,II' 1 r,r' 1- PQ: O'] { 11 $ 17' - b: 3s.
11 .... (s .... b) E [II I r 1- P : 9 - 9' 1 "
er
11' .... s e [II' 1r' 1- Q : l
is a coherent set of input-output pairs each of the form [x1-s1. ... , Xn-snl .....
a. We call the input part of such a pair a "trace environment." The metavari-
able T] is used to range over such trace environments. The symbol TJo denotes
a trace environment where allvariables are mapped to (), TJ[x-s] denotes T]
with the x component updated tos, and T] ED TJ' denotes the join of two trace
environments T] and TJ' with disjoint domains. If T] and TJ' are trace environ-
ments with identical domains, then T] • T]' denotes the trace environment with
x mapped to T](x) · T]' (x).
Table 9 gives the semantic interpretations of the basic phrases. For the
most part, the combinators used in the interpretation have already been men-
tioned earlier. Recall that the notation [TI' lf' 1- Q : Of means the regular ex-
tension of the object function.
The interpretation of the primitive phrases is obtained by applying the con-
stants shown in Table 8. For example, the meaning of [TI Ir 1- C1; C2: comm]
is obtained by
seq o ([TI Ir 1- C1 : comm], [TI I r 1- C2 : comm]) .
For convenience, we show some of these derived meanings in Table 10. Note
in particular the difference between sequential and parallel composition. The
interpretation of new[o] x. C is to apply the meaning of the function term
.\x. C to the object behavior cellinit[liJ (which is the behavior of a storage cell
with initial value init[o]). The denotation of .\x. C then uses a unique trace s
from this behavior to run the command C.
Example 13 Consider the ALGOL function
mkcounter: var[int] - counter
mkcounter(x) = [val = deref x, inc = (x := x + 1)]
Its meaning is an object function of type var[int] - counter containing the
pairs
(get.i) val.i
(get.i,put.(i + 1)) inc.*
264 Chapter 19. Global State Considered Unnecessary
5 Passivity
The framework defined in Section 4 models interference-controlled ALGOL
without any notion of passive values. This has some undesirable conse-
quences:
Uday S. Reddy 265
lexp[6]1p lexp[c5] I
lcommlp 0
lvar[c5] lp {get.i: i E 161}
IAxBip {fst.a: a E IAip} u {snd.b: b E IBip}
IA®Bip { (a, b) : a E IAlp 1\ b E IBip}
ITip ITI 0
lllp 111 = {*}
Taking the passive subspace of an already passive space has no effect, i.e.,
PP= P for any passive space P. In particular, t'PA =PA.
An object (Q, a s;; Q x lAI x Q) for an active-passive space Ahasan addi-
tional condition on its transition map:
(q,a,q') E a" a E IAip = q = q'
This represents the intuition that passive transitions do not change state. Most
of our development from Sections 3 and 4 can be carried over to active-passive
spaces. There are only two changes. We modify the definition of object spaces
using the fact that passive transitions do not change the state. And, we restriet
object functions to account for passivity.
Active-passive object spaces
To arrive at the new definition of object spaces, we make the following obser-
vations:
1. If successive operations on an object are passive operations, their rela-
tive order is insignificant, i.e., we want to regard the sequences (a, a')
and (a', a) as the same trace whenever a and a' are passive tokens.
2. If successive operations on an object are identical passive operations,
their number is insignificant, i.e., we want to regard the sequences (a, a)
and (a) as the same trace whenever a isapassive token.
These identifications have a deeper significance than just ignoring order and
number. Since the tokens represent entire operations, not merely atomic
events, the identifications have the effect that passive operations on objects
can in fact be done concurrently. Thus, to model passivity, we revise Thesis 1
to the effect that objects can in general be used sequentially, except that mul-
tiple passive operations can be done concurrently. This matches dosely with
the syntax of interference-controlled ALGOL, in particular the Contr type rule.
The identifications mentioned above are formalized as follows:
Definition 16 Let A be an active-passive space. The trace monoid of Ais the
quotient monoid lAI* /= where = is the least monoid congruence generated
by the equivalence relation:
(a, a') (a', a) for all a, a' E IAlp
(a,a) (a) for all a E lAI!'
An element of lAI* /= is an equivalence dass of sequences, called an (active-
passive) trace. The equivalence dass containing a sequence s is denoted [s].
The unit trace (or empty trace) is [ () ]. The multiplication of traces is defined
by [s] · [s'] = [ss']. (We often omit writing "·".)
Traces of this kind (and more general ones) have been studied extensively
in the context of Petri nets [AR88, Maz89]. One useful result from this theory
is the existence of so-called Foata-normal form. Applied to the current Situa-
tion, it means that every trace [s] can be written in the form [sd· [sz] · · · [sn].
where each [s1] is nonempty and the consecutive segments altemate between
active and passive traces. Note that an active trace is a singleton equivalence
Uday S. Reddy 267
The eonsistency relation represents the intuition that only aetive transi-
tions from the past ean have a eausal e:lfeet on future transitions. A eoherent
traee is a traee that is eonsistent with itself. For example, {get.O,get.1) is not
a eoherent traee of tvar[ö] beeause the tokens get.O and get.1 are ineonsis-
tent even though their aetive past is identieal (empty in both eases). Thus, our
objeet spaees now eontain only meaningful traees.
Note that the t eonstruetion preserves passivity. For any passive spaee
P, t P is passive. The traees of t P are finite eoherent sets of tokens. So,
for passive spaees, the t eonstruetion coincides with Girard's ! eonstrue-
tion [Gir87a, GLT89]. 12 An important result of Girard's isthat linear funetions
!A-LB are order-isomorphie to stable funetions A -s B. This result obvi-
ously earries over to passive objeet spaees as well. Linear funetions t P - L A
are order-isomorphie to stable funetions P- s A whenever P is a passive spaee.
Object functions
If f:A- B models a funetion phrase and f(x) isapassive value, then no aetive
token of x eould have been "used" in produeing f(x). Astateehange operation
eannot be involved in a "pure reader" of state information. This leads to our
final thesis regarding a model of interferenee-eontrolled ALGOL:
Thesis 5 The linear maps of object functions must be passivity-re(lecting.
We formalize this notion as follows:
12 In a previous version of this paper (Red93a], the ! and t constructions on coherent spaces
were used to model "passive values" and "active values" respectively. The move to active-passive
spaces allows us to model both kinds of values under one heading. This gives a more general
treatment of interference-controlled ALGOL than previously possible.
268 Chapter 19. Global State Considered Unnecessary
If b is passive then (q, b, q') E oc2 implies q = q'. Hence, for the unique s such
that s .... b E f, (q,s,q') E oc1 implies q = q'. Since this must hold uniformly
for all objects, we require that s should be passive.
5.2 Semantics
We now complete the definition of semantics for interference-controlled
ALGOL using active-passive spaces. The semantics of phrases given in Sec-
tion 4.3 using coherent spaces can be easily refined to active-passive coherent
spaces. The interpretation of a type [ 9] is now understood to mean an active-
passive space (using Table 11). The passive function type is interpreted by
[9 - p 9'] = [9] ~p [9'].
The interpretation of a phrase TI I r 1- P : 9 was given as an object function
of type [TI], [f]- [9] over coherent spaces. We now understand this tobe
an object function over active-passive spaces, i.e., a passivity-preserving linear
map of type (@[TI])® (@[f]) - L [9]. It can be verified that the interpretation
in Table 9 gives passivity-preserving maps. Moreover, any input-output pair
1'11 E9 172 ...... a in the interpretation of P (where 171 is a trace environment of
[TI] and 172 one for [f]) will only have passive traces in 171· An elegant way of
stating this is that the interpretation of P is an object function of type
.!'[TI], [f] - [0]
Again, this can be verified for the interpretation in Table 9. For the rule Id,
the base case, 171 maps all identifiers to 0 which is a passive trace. The other
inductive cases preserve this property.
Table 12 gives the interpretation of the structural rules. The interpretation
of rule Contr says that, if an identifier x occurs multiply in independent con-
texts of a phrase, the information used from x is the union of the information
used in each occurrence. Note that all these occurrences are passive uses of
the identifier. So, the information used is represented by passive traces (finite
coherent sets).
The remaining rules cause no change to the semantics of the phrase. In
a sense, this is to be expected because we would not want to assign multiple
interpretations to the same phrase. We must verify, however, that these inter-
pretations are of the right types. For the rule Activate, the meaning of P on the
right is an object function of type .!'[II],x:.!'[9], [ f ] - [9']. Clearly, it is also
of type .!'[TI],x: [9], [f]- [9']. (We merely forget that the binding of x is pas-
sive.) For the rule Passify, the meaning of P on the right is an object function of
type .!'[TI], [f], x: [ 9]- [ cf> ]. Since [ cf>] isapassive space (and object functions
270 Chapter 19. Global State Considered Unnecessary
Operational adequacy
To show that the semantics defined here agrees with the operational behavior
of interference-controlled ALGOL, we prove that it is sound and adequate with
respect to the operational semantics:
Proposition 22 If C is a closed command phrase, [ 1-- C : comm] is nonempty
(i.e., has an input-output pair [I .... *) if and only i(C terminates.
Recall from Section 2 that the operational semantics of ALGOL is defined to
work in two stages: reduction and execution. The command C terminates iff
there is a closed command phrase C' such that C - * C' and ( [ I, C') ~ [ 1.
Our proof of operational adequacy is similarly factored into two stages. See
Appendix B for details.
play with: whatever state the command c might act on. We can affect this state
by running c some number of times. This gives phrases of the form
n times
c" = c; · · · ;c
The various functions i\c. c" are observationally inequivalent. We can observe
the difference between them by putting them in program contexts Pn[] of the
form
new[int] x.
x:= 0;
[ ](x := x + 1);
if x = n then skip else undef
So, the elements of the function space comm => comm should have the struc-
ture of flat naturals, N1.P
Now, here is the paradox. If comm has only two elements, how can
comm => comm have an infinite number of elements? 14
This observation suggests that the notion of "elements" in a semantics of
ALGOL is a delicate issue. There must be different kinds of elements which
come to the fore depending on the context. The "elements" that correspond
to meanings of closed phrases are regular elements mentioned in Section 4.1.
Note that the meanings of closed phrases are functions of the form f: 1 - L A.
Such functions correspond to elements x = { a : * ...
a E f } of A. Their
regular extensions f: 1 - R t A similarly correspond to elements x * of t A. So,
the regular elements of t A form a domain isomorphic to the domain A, and
both the domains represent meanings of closed phrases. Armed with this
notion, let us analyze some of the basic types of our model.
Fact 1 There are precisely two regular elements in tcomm.
The two elements are diverge = 0 * = {() } and skip = {*}*.
Fact 2 The elements of (comm = comm) form a domain isomorphic to N 1..
A token of comm => comm is of the form (*)" ...... * where n ~ 0. Denote
such a token by fz. Since ( *)" ::: (*
)m in tcomm, fz ::: mif and only if n = m. ln
other words, the consistency relation of comm => comm is the identity relation
making it a flat domain. Since each fz corresponds to a natural number n, the
structure is that of N 1..
Note that this structure is right for interference-controlled ALGOL. The
element 0 of comm => comm models i\c. undef and an element {fz} models
i\c. c".
This fact is unprecedented in the semantics of imperative programs. All
other known models fall to capture comm => comm accurately. The model that
13 While it has been possible, in principle, to calculate the structure of such simple domains by
syntactic means, there is no evidence that anybody has done so. The first instance where simple
domains are calculated is in [OT95), where the calculation is done using semantic tools.
14 This observation means that Milner's context lemmafalls for ALGOL (as well as most other pro-
gramming languages with mutable variables), a known fact of folklore. From a categorical Stand-
point, this means that the fully abstract model of ALGOL is not a "well-pointed" category [Mit90).
The functor-category semantics of Reynolds and Oles [Ole85, Rey8lb) seems tobe the first work
to reflect this fact.
272 Chapter 19. Global State Considered Unnecessary
comes closest is the parametricity-based model of [OT95] (as weil as the re-
lated model of Sieher [Sie93, Sie94]). In addition to the ALGOL functions men-
tioned above, the parametricity-based model contains functions expressible
using the so-called "snap back" operator. The command snap-back combina-
tor is a constant try:comm - p comm with the semantics that try C runs the
command C and then snaps the state back to the original state. (By adding
such a combinator, we can express new functions of the form i\c. try cn+m; c"
which diverge for cases where i\c. c" would terminate.) Note that the snap-
back operator is counter to one's intuition that state changes are irreversible.
The reason for the presence of such operators in the parametricity model (as
weil as other models) is that commands are modeiled by state-to-state func-
tions. We avoid this pitfall because, as stated in Section 3, we do not treat
states as entities. They are at best derived attributes of objects, and do not
play a central role in our model.
Notlee how our model answers the puzzle mentioned at the beginning of
this section. The regular elements of tcomm are the values that are directly ex-
pressible. The other elements of t comm such as {( *) n} remain hidden. They
come to the fore in contexts where their difference becomes distinguishable
(such as the argument positions of procedures).
One might wonder if the other elements of objects spaces are express-
ible in some other form. Indeed, an important dass of elements, viz., active
elements mentioned in Section 4.1, correspond to object behaviors. Such ele-
ments arise as the values of phrases with active free identifiers.
Fact 3 The active elements of tcomm form a domain isomorphic to VNat, the
domain of "vertical" natural numbers (ordered by numerical order).
The element corresponding to a natural number k is the set of all prefixes of
( *) k. Such elements arise as the meanings of phrases:
x:var[int] 1- if x s k then x := x + 1 else undef: comm
Applying the meaning of this phrase to cello, we obtain the element with all
prefixes of ( *) k.
Let us examine a second-order type:
Fact 4 The domain (comm = comm) = comm is isomorphic to a subdomain of
P (N* ) that contains all subsets of N* without any mutual pre(ixes.
= =
A token of (comm comm) comm is of the form (ih, ... , iik) - * where fit
=
are tokens of comm comm mentioned previously. Two such tokens are con-
sistent if and only if the sequences on the left are inconsistent, i.e., not prefixes
of each other. So, every set of such tokens that excludes mutual prefixes on the
=
left is an element of (comm comm) = comm. The diligent reader would be
able to verify that all finite elements of this domain are expressible by closed
phrases. For example, the element {(n) · s1 - *• (n) · s2 - *• (m) · t - *},
*
with n m, is expressible by
i\p. new[int] x.
x:= 0;
p(x := x + 1);
if x = n then 9s(P)
eise if x = m then 9r(P)
elseundef
Uday S. Reddy 273
where g5 and g 1 are terms expressing the elements {s1 ...... *, Sz ...... *} and
{t ...... *} respectively.
Regarding the semantics of passive types, we have the following observa-
tion. Notice that the fragment of interference-controlled ALGOL involving only
passive types is a functional programming language. In this fragment, the dis-
tinction between the active and passive free identifiers vanishes because the
free identifiers can freely move between the passive and active zones of typ-
ing judgements. So, the language is essentially the same as PCF (with product
types). We have:
Fact 5 The object-based semantics of the passive (ragment coincides with the
stable semantics of PCF [Ber78].
The semantics of this fragment involves only passive spaces, for which the
function space tP __., Q is identical to the stable function space [P -s Q].
More important to our concerns is the interaction between the active and
passive sublanguages. With regard to this, we have:
Fact 6 The domain (comm => exp[8]) is isomorphic to 8.
This fact holds because of the passivity-preservation property of functions.
The only passive token of tcomm is the empty trace. Since exp[8] isapassive
space, the tokens of comm => exp[8] are of the form() ...... i for some i E 181.
In other words, the elements of comm => exp[8] are all constant functions.
Note that this is indeed the case for IDEALIZED ALGOL. Any nontrivial use of a
command in an expression would amount to a side effect and expressions of
IDEALIZED ALGOL do not have side effects.
Most other models of ALGOL fall to model comm => exp[8] accurately. In
fact, they support an expression snap-back combinator (more general than
the try C operator mentioned above) of the form do C result E, where C is a
command and E an expression. See the discussion in [OT95). This combina-
tor embeds commands inside expressions and causes the breakdown of most
reasoning principles of expressions. 15 The only other known model that sat-
isfies Fact 6 is Tennent's model of specification logic [Ten90). Our techniques,
however, seem markedly different from his.
We make a few remarks regarding the domain order of function spaces.
Most semantic models of programming languages use continuous function
spaces with pointwise order. In contrast, we are using linear functions (a sub-
dass of stable functions) ordered by Berry's stable order. A question arises as
to whether this is an appropriate choice. Indeed we find that the stable order
matches quite closely with the semantic structure of interference-controlled
ALGOL. Note that, in view of the discussion at the beginning of this section,
the fully abstract model is not extensional. So, it cannot be order-extensional
either. Moreover, the pointwise order is clearly inappropriate for the obvious
possibilities. Given that comm is a two-point lattice, the continuous function
space [ comm- comm] would order the function Ac. c below .\c. skip. But, they
are distinguishable in the language. Even the function space [VNat.L- comm]
does not have the right order to represent comm - comm. In contrast, the
15 Note that the full-abstraction result claimed in [Sie94] is for a language with such a snap-back
combinator.
274 Chapter 19. Global State Considered Unnecessary
7 Example Equivalences
An important application of a semantic model is to test the equivalence of
program fragments. The accuracy of a model can be gauged by the variety of
equivalences it can validate. Since the object-based model is accurate for many
first-order types, we expect that most equivalences involving free identifiers of
such types can be validated using it. ln this section, we show several examples
of such equivalences. ln all of these, an unknown non-local procedure p is
passed an object built from local variables.
Consider a variant of equivalence (1) from Section 1:
new[int] x. new[int] x.
x:=O; x:= 0;
p(x:=x+1) p(x := x -1)
where the free identifier p is of type comm - comm. It is easy to see that the
equivalence holds in the object-based model. Both sides of the equivalence
denote the following object function of the type (comm => comm) - comm:
{{ii)-*:n;::O}
Uday S. Reddy 275
Recall from Section 6 that ii is short for (*)" .... *• a token carrying the infor-
mation that the procedure runs its argument n times.
A variant of the above example with a more practical interest is:
new[int] x. new[int] x.
X:= 0; X:= 0;
p[val = x, inc = (x := x + 1)] p[val = -x, inc = (x := x -1)]
where p: counter - comm. The procedure p is being passed two different
implementations of a counter object. Since the two implementations have the
same Observable behavior, we expect the equivalence to hold.
The meanings of the two sides of the equivalence are:
{ [p- (s .... *)] .... *: s E mkcounter((cell0 )}
{ [p- (s .... *)] .... *: s E mkcounter2~(cello)}
where mkcounter1 and mkcounter2 are the meanings of the two counter
phrases regarded as functions of a variable object, and cell0 is the trace set
of a storage cell with an initial value 0. So, the equivalence reduces to the
equality of mkcounter 1 ~(cello) and mkcounter2~(cel10 ). The two functions are
given by the linear pattems:
mkcounter1 { (get.i) .... val.i : i E Iintl } u
{ (get.i,put.(i + 1)) .... inc.*: i E lintl}
mkcounter2 { (get.i) .... val.(-i): i E lintl} u
{ (get.i,put.(i -1)) .... inc.*: i E lintl}
It is easy enough to see that the two object functions produce the same counter
trace set when applied to cell0 . But, let us see how one can show this formally.
Theorem mkcounter((cello) = mkcounter2~(cello).
that the t1 and t2 of the two existentials are unique whenever they exist.
For a trace t e cell0 , say that t represents a state i ü t · (get.i) e cell0 •
Define a relation R !; cello x cello by
t1 R t2 ~ 3i. t1 represents the state i "
t 2 represents the state -i
contexts [II I f] in the obvious fashion. Let x range over active elements of
[II I f], 17 over trace environments and u over x-states of the form x/11. We
say that an assertion A holds in u if there exists 17 E u such that 17 .... tt E [A].
Then, the satisfaction of Hoare triples may be defined as follows:
where gv(V) is a formula stating that V is a "good variable" [Rey81a], i.e., the
regular function [Vf gives 0 or cellt for some i E 181. 1t is easy to verify that
the axiom is satisfied by all active elements x E [II,a:var[ö]- assert I f],
showing that it is valid.1s
8 Extensions
In this section, we briefly consider some extensions to the basic language con-
sidered in this paper.
Sum types
A type constructor for sum types ih + lJ2 can be added with the usual syn-
tax. Its interpretation is [ lJ1 + lJ2] = t [ lJ1] + t [ lJ2] where + is the coproduct
of coherent spaces (also called the "direct sum" [GLT89, Section 12.1]). This
interpretation is in the same spirit as the lifted sum construction used with
Scott domains and Girard's "linearized" sum in [GLT89, Section 12.5]. The
interpretation of terms is
The interpretation of case makes clear the reasons for the presence of t in the
interpretation of lJ1 + lJ2.
16The soundness of such proof rules is not in question as other models already exist for spec·
ification logic [OT93, Ten90]. The point of interest is that such reasoning principles are valid in
our model, while they would be invalid in models that support snap-back expressions.
Uday S. Reddy 279
Independent product
The product type 01 X Oz of interference-controlled ALGOL represents a compo-
sition of interfering components. One can add a separate type constructor for
independent products el ® ez which puts together independent components.
The term syntax for independent products [OP1T95] is given by:
TI1 I r1 1- P : e1 Tiz I fz 1- Q : Oz
----~------------~~- ®1
TI1.Tiz I r1.rz 1- P ® Q: e1 ® ez
TI 1 r 1- P : e1 ® ez TI I r 1- P : 01 ® Oz ®21"
® 1T
--~----_.:;..--~
-TI-'I-f-1--fs-t-(P.:c.)-:-,e~l TI I r 1- snd(P) : Oz
The independent product allows one to write "uncurried" procedures by using
the isomorphism: 01 ® Oz- 9' ;: 01 - {ez- e'). To give semantics to this
extension, we need to extend the semantic framework as well. Essentially, we
must enlarge the notion of "objects" to include composite objects that have
multiple individual objects as components. Appendix A describes a frame-
work of "finitary object spaces" which incorporates this extension. Another
approach in terms of "dependence spaces" is described in [Red94].
Acceptors
In [Rey81b], Reynolds defines a type of acceptors corresponding to the 1-values
of variables. We postulate a primitive type acc[6] for acceptors (of 6-typed
data values). The type of variables may then be defined as the product:
var[6] = [put:acc[6] x get:exp[6]]
The assignment operator := then has the (more general) type
acc[o] x exp[6] - comm
with an implicit coercion whenever a variable is used as the left-hand side
of:=.
To interpret the acceptor type, we define an active-passive coherent space
acc[6] as follows:
lacc[6]1 161, :::acc[oJ
Note that our original interpretation of variables satisfies var[ 8]
[put: acc[6] x get: exp[6]]. So, there is no substantial change to the seman-
tics.
Reynolds also gives a treatment of acc[6] as the function space
exp[8]- comm. This treatment, however, is not useful for the interference-
controlled language. We often want to apply the acceptor of a variable to
an expression that involves the same variable, e.g., x := x + 1 desugared as
x.put(x.get + 1). Such phrases arenot legal in interference-controlled ALGOL.
On the other hand, we see that Reynolds's conception is very well represented
in our semantics as the isomorphism acc[6] ;: 8 __., comm.
280 Chapter 19. Global State Considered Unnecessary
9 Conclusion
We have shown that it is possible to give semanlies to higher-order impera-
tive languages without involving a notion of a global state. Such semantics
seems to have an intuitive "feel" of being abstract, and this is corroborated by
the relative ease with which we are able to validate equivalences which have
traditionally failed in conventional approaches.
The main difference between our semantics and the conventional ap-
proaches is that we model objects by their observable behavior without ref-
erence to their intemal states. This decision obtains theoretical economy and
allows us to formulate a ground-level domain-theoretic model of object be-
haviors. In contrast, conventional approaches use explicit states and model
objects as state transformers. But, to represent local-variable abstractions
correctly, they must characterize object functions as being "uniform" in the
underlying state sets.
The functor-category approaches [Ole85, Rey8lb, Ten90) attempt to model
uniformity by natural transformations. This approach has not yet led to sat-
isfactory solutions because the functors involved have "mixed variance" and
naturality is not enough to capture uniformity. In contrast, the relational para-
metricity models [OT95, Sie93) model uniformity by preservation of relations.
The recent full-abstraction result of Sieher [Sie94) suggests that this approach
can be quite successful.
Stepping back, we can see that the idea of objects with local states is at the
heart of the above approaches as well (though it may not have been explicitly
stated in those terms). The main difference is then the treatment of state sets.
By eschewing the explicit treatment of states, we are able to avoid second-
order concepts like functors and finesse the issue of uniformity. On the other
hand, we believe that both the explicit state view and the behavior-based views
have useful roles to play. The insights obtained in the current study should
prove useful for resolving the outstanding issues in the explicit state view.
The most important of these issues is modeHing the irreversibility of state
changes. All known explicit state models (for higher-order languages) contain
282 Chapter 19. Global State Considered Unnecessary
the snap back operator try. All but Tennent's model [Ten90] also contain the
more troublesome do-result operator (which invalidates common reasoning
for expressions). The present model is the first to avoid such state change
reversals. Note, however, that Proposition 5 already gives an indication of
how irreversible state changes can be modelled in an explicit state view. This
should be explored further in the future work.
Our model also brings to light important connections between the seman-
lies of state and other subjects in programming language theory. One of
them, viz., connection to linear logic, is studied in [Red93c]. This should
pave the way for good operational models of stateful languages such as ge-
ometry of interaction [AJ92, Gir87b], games [AJM94, H094] and sequential
algorithms [CCF94]. A second connection, to parametricity, arises in studying
the relationship to the explicit state model. At least for the types that arise
in the semantics of interference-controlled ALGOL, our approach shows that
the independence of a parametric type on its type parameters can be explicitly
modelled (via a suitable generalization of Proposition 5). It is an open question
whether this can be carried further for more general parametric types. A third
connection is to concurrency theory. Note that the behavior-based implicit
state view taken here is a common practice in concurrency [Hoa85, Mil89].
The use of Simulations as morphisms is also found in recent models of con-
currency [BG90, Gup94]. These connections should prove useful for studying
concurrent imperative languages.
Finally, an important issue that is left untouched in this work is the
development of uniform reasoning principles. In a sense, the traditional
methods for reasoning, including assertion-based methods and relational
param.etricity-based methods, can be said to reflect an explicit state view of
the semantics. What reasoning methods would be contributed by the behavior-
based view taken here?
Acknowledgements
This work owes much to the encouragement and inspiration provided by Peter O'Hearn
throughout its development. The feedback from him, Bob Tennent and Phil Wadler
were crucial for the development of the current presentation. Anonymaus referees
pointed out many a rough spot. Special thanks go to William Harrison and Howard
Huang for their help in proof-reading.
Thanks to Christian Retore for providing the foundational ideas of pomset logic
(which are implicit throughout this work) and Samson Abramsky for pointing me
in this direction. I thank Guo-Qiang Zhang for explaining his constructions for
dl-domains (used in Finitary Object Spaces, Appendix A).
This research was supported by National Science Foundation under grant NSF-
CCR-93-03043.
References
[Abr94) S. Abramsky. Interaction categories and communicating sequential pro-
cesses. In A. W. Roscoe, editor, A Classical Mind: Essays in Honor of
C. A. R. Hoare, pages 1-16. Prentice-Hall International, 1994.
[Agh86) G. Agha. Actors: A Model of Concurrent Computation in Distributed Sys-
tems. The MIT Press, 1986.
Uday S. Reddy 283
[Gup94] V. Gupta. Chu Spaees: A Model of Concurrency. PhD thesis, Stanford Uni-
versity, August 1994.
[Hen94] M. Hennessy. A fully abstract denotational model for higher-order pro-
cesses. Information and Computation, 112(1):55-95, July 1994.
[HMT83] j. Y. Halpem, A. R. Meyer, and B. A. Trahktenbrot. The semantics of local
storage, or what makes the free list free? ln Tenth Annual ACM Symposium
on Principles o{Programming Languages, pages 245-257. ACM, 1983.
[H094] j. M. E. Hyland and C.-H. L. Ong. On full abstraction for PCF (preliminary
version). Manuscript, Cambridge University, October 1994.
[Hoa69] C. A. R. Hoare. An axiomatic basis for computer programming. Comm.
ACM, 12:576-583, 1969.
[Hoa71] C. A. R. Hoare. Procedures and parameters: An axiomatic approach. ln
E. Engeler, editor, Symposium on Semanties of Algorithmie Languages, vol-
ume 188 of Leerure Notes in Mathematies, pages 102-116. Springer-Verlag,
1971.
[Hoa72] C. A. R. Hoare. Proof of correctness of data representations. Acta Infor-
matiea, 1:271-281, 1972.
[Hoa85] C. A. R. Hoare. Communieating Sequential Processes. Prentice-Halllnterna-
tional, London, 1985.
[JM91] T. Jim and A. R. Meyer. Full abstraction and the context lemma. ln T. Ito
and A. R. Meyer, editors, Theoretieal Aspects of Computer Software, vol-
ume 526 of Leerure Notes in Computer Scienee, pages 131-151. Springer-
Verlag, 1991.
[KL85) R. M. Keller and G. Lindstrom. Approaching distributed database imple-
mentations through functional programming concepts. ln International
Conferenee on Distributed Computing Systems. IEEE, May 1985.
[KM77) G. Kahn and D. B. MacQueen. Coroutines and networks of parallel pro-
cesses. ln Information Processing 77, pages 993-998, 1977.
[Lam87] j. Lambek. Multicategories revisited. ln Gray and Scedrov [GS89].
[Mac71] S. Mac Lane. Categories (or the Working Mathematician. Springer-Verlag,
1971.
[Maz77] A. Mazurkiewicz. Concurrent program schemes and their interpretations.
DAIMI Report PB 78, Aarbus University, Aarhus, 1977.
[Maz89] A. Mazurkiewicz. Basic notions of trace theory. ln de Bakker et al.
[dBdRR89], pages 285-363.
[Mil71] R. Milner. An algebraic definition of simulation between programs. ln Proc.
Seeond International Joint Con(erenee on Artificialintelligenee, pages 481-
489, London, 1971. The British Computer Society.
[Mil89] R. Milner. Communieation and Coneurrency. Prentice Hall, 1989.
[Mit90] J. C. Mitchell. Type systems for programming languages. ln J. van Leeuwen,
editor, Handbook of Theoretieal Computer Scienee, Volume B, pages 365-
458. North-Holland, Amsterdam, 1990.
[MS88) A. R. Meyer and K. Sieber. Towards fully abstract semantics for local vari-
ables. ln Fi(teenth Annual ACM Symposium on Prineiples o( Programming
Languages, pages See Chapter 7. 191-203. ACM, 1988.
[Nau60] P. Naur (editor), J. W. Backus, et al. Report on the algorithmic language
ALGOL 60. Comm. ACM, 3(5):299-314, May 1960.
Uday S. Reddy 285
1-tl
{* . . <*>": n ~ 0}
288 Chapter 19. Global State Considered Unnecessary
hcj tf J•D
tC ---'-'--- tD
What we call "object spaces" are (spedal classes of) t -coalgebras and "regular (unctions"
are coalgebra morphisms.
A free coalgebra isapair (tA,dupA:tA- ttA) for an object A of C. Notlee the
(natural) isomorphism
ct (C, tA): C(UC,A)
where U:ct -Cis the "underlyjng object functor". The maps in the two directions
are:
f = C~tC___iL..tA foranyf:UC-A
g UC~tA~A
f
Morphisms o( the form are called "regular ex.tensions" o( linear maps in Section 4.
Likewise, g is the "linear pattem" o( a regular (unction. The semantics of interference-
controlled ALGOL is given using free coalgebras.
If C and D are coalgebras, their tensor product C ®D can be made into a coalgebra
by associating the structure map:
hc ® hD mergec,D
C®D tC® tD t(C®D)
The tensor unit is (l,merge1 : 1 - tl). This gives a symmetric monoidal structure to
ct. However, C ®Dis not a free coalgebra even if C and D are. So, we cannot simply
work in the subcategory of free coalgebras. This is the reason for using a multicategory
structure in Section 4. We require, however, that (l,merge1 ) is the terminal object in
ct. This gives a unique coalgebra morphism
discardc: C - R1
Thus, in the words of [BBdPH92], "the full subcategory of finite tensor products of free
coalgebras forms a symmetric monoidal closed category" whenever C is symmetric
monoidal closed and t is a symmetric monoidal comonad.
If C has products (which is the case for our base categories), the free coalgebras
have products. The product of tA and tB is t(A x B). The maps involved in this
structure are:
trr1 trrz
• t(A x B) _....:_.:__.,.. tA and t(A x B) _....:_=---.... tB; and
• c _ __.;:;..._
hc t<i.9>
tC -"'--'-=--+- t (A X B).
Recall that 1 is already the terminal object and is now seentobe isomorphic to tT.
It is easily verified that, by considering arrows f: t A1 ® • • • ® t An - B of C as
multi-arrows {:A1. ... ,An- B, we can form a multicategory. We call this the "Kleisli
multicategory" of C under t and denote it by Ch>t· The semantics of Section 4.3 is
given in such a multicategory.
All of this captures the surface structure of the model, given an appropriate
comonad t. It does not, however, explain t (which is where the novelty of the present
modellies). An earlier paper [Red93b] defines t as the free comonoid comonad with
respect to an additional (non-symmetric) monoidal structure (t>, 1). The bifunctor t>
called "before" represents a notion of sequential composition. See [Ret93] for a proof-
theoretical study of this bifunctor.
Passivity
The category of active-passive coherent spaces used for modelling passivity has im-
portant elements which can also be expressed at the categoricallevel.
Our prototypical base category C (such as APCohL) has a full subcategory P
for modelling passive types. This subcategory is reflective as weil as co-reflective.
(See [Mac71], Sec. IV.3.) This means that there are functors P, P: C - C taking values
in P such that we have the natural isomorphisms:
passify- 1 P(PA,P) C(A,P) : passify
promote- 1 P(P,PB) C(P,B) : promote
By the first isomorphism, we obtain a natural transformation pasA:A-PA as the unit
of the reflection, and, by the second isomorphism, derB: PB - B as the co-unit of the
co-reflection.
For active-passive coherent spaces, the functor P assigns, to each space A, its
passive subspace PA and, to each (passivity-reflecting) linear map f: A-LB, the corre-
spondingrestrictionPf:PA-rPB. Theunit pasA is given by {a ~ a: a E IAiv }. The
isomorphism passify and the unit pas are used in interpreting the type rules Passify
and Activate respectively.
The functor P assigns to each space A, the passive space PA with IPAI = IPAiv =
lAI and the same coherence relation as A. A (passivity-reflecting) linear map f:A -r B
is mapped to the same map regarded as an arrow PA-L PB. The co-unit derB is given
by { b ~ b : b E IB I }. The isomorphism promote and the co-unit der are used in
interpreting the type rules Promote and Derelict respectively.
Note that the functor P is full. This makes the unit pasA a split epi. The right
inverse of pasA is then a monic, actA:PA- A. We may thus call PA the "passive
subobject" of A (terminology first used in [O'H93]).
The full subcategory P is closed under all the constructions mentioned previously:
®, 1, A __., (- ), x, T, and t. Further, the reflector P preserves all of this structure on
290 Chapter 19. Global State Considered Unnecessary
the nose:
PI 1 P(A ®B) PA®PB
PtA WA P(A-B) A-PB
PT T P(A x B) PAx PB
Since P preserves the monoidal structure, the associated reflection is a monoidal ad-
junction. This fact is also used in the interpretation of Passify and Activate.
Finally, the passive sublanguage of interference-controlled ALGOL is a functional
programming language. Therefore, the tensor product in Pht must be the categor-
ical product. One way of ensuring this is to notice that free t coalgebras in P have
"diagonals", i.e., there is a monoidal natural family of coalgebra morphisms:
dupp: t P - R tP ® tP = {PI u P2 - (PI. P2) :PI u P2 E It PI }
suchthat (P, dupp, discardp) forms a commutative comonoid. Further, all coalgebra
morphisms are comonoid morphisms. It then follows that tensor products in Pht
are isomorphic to products by [BBdPH92, Theorem 9]. In other words, we have an
isomorphism
tP® tQ t(PxQ)
given by
packp,Q: tP ® tQ- t(P X Q) = { (pll,p12)- p: p E lt(P X Q)l}
where p 11 = {fst.a : a E p } and p 12 = { snd.b : b E p } .
Note that the Kleisli multicategory Pht is equivalent to the Kleisli category
Pt because multicategory maps t PI ® • • • ® t Pn - Q are one-to-one with maps
t(PI x · · · x Pn)- Q. The Kleisli category is cartesian dosed.
There exists a preorder on the monoid of tokens defined by x :Sc y = 3x'. xx' = y.
Note that, if a product xy is defined, then xy' is defined for all y' :S y. Secondly, x :S y
implies x' ::: y' for all x' :S x, y' :S y. In particular, ec ::: x for all x E ICl. We call an
object space finitary if :S is a finitary partial order. (The set { y : y :S x } is finite for all
x E ICl.) Regular maps f: C -R D are coalgebra morphisms, as usual. Thus, we form
a full subcategory FOb of CohL t.
A free object space t A is dearly a finitary object space. Its finitary partial order is
the prefix order. It is easy to see that finitary object spaces carry the structure of prime
event structures which, in turn, correspond to dl-domains [Win89). Regular maps are
special cases of linear maps on dl-domains.
The tensor product C ® D of object spaces is the tensor product of the underlying
coherent spaces with the monoid structure being the monoid product ICl x IDI. The
object space 1 has the one-element monoid structure. Note that 1 is the terminal
object of FOb. The product C x D of finitary object spaces has the token-monoid
IC x Dl =I Cl e IDI, the monoid coproduct of ICl and IDI.
To define the exponent C = D of of finitary object spaces, we need to consider
another dass of maps called active maps. An active map f: C - A D is a linear map
suchthat
1. ec - ev E f, and
2. x- y E fand y' :S y implies there exists (unique) x' :S x suchthat x' - y' E f.
Note that active maps properly indude regular maps. (They still form a subdass of
linear maps on dl-domains.) The tokens of C = D are active maps with a maximumpair
x - y. Call them prime active maps. For an active map f: C-A D and pair x - y E f,
Iet fx- Ylr denote the set
fX - Y 1{ = { x' - y' E f : x' :S X 1\ y' :S y }
Then all prime active maps can be written as fX - y 1{ for some f: C - A D and
x - y E f. The exponent C = D is defined as follows:
IC=DI = {fx-Ylr:f:C-ADAx-yEf}
fx1- Yllf1 ::: fxz- Y2lr2 = 3f:C -A D. fx1- Yllf1 u fxz- Yzlr2 ~ f
with the monoid structure:
ec~v = {ec- ev}
fx1 - Yll{1 • fxz - Y21{2 = fx1x2 - YlY21{
where f = fx1 - Yllf1 u { x1x' - Y1Y' : x' - y' E fxz - Yzlr2 }. (Note that the
above product of prime active maps is defined if and only if the products x 1xz and
YlY2 are both defined.) This construction dosely parallels the construction of expo-
nents for dl-domains [Zha93). To see that C = D is indeed the exponent in FOb,
suppose f:B ® C -R Dis a regular map. For any x E IBI, we can define an active map
fjx:C -AD by
fix= {y'- z': 3x' ~ x. (x',y')- z' E f}
Then, the natural isomorphism B ® C- R D ;: B- R ( C = D) is given by the combinators:
A(f) B-R (C=D)
{x- fy-zlr 1x:(x,y) -zEf}
applvc.v (C=D) ®C-RD
{(fy- Zlf.Y)- Z :f:C-AD 1\Y- z Ef}
Lengthy calculations show that these maps are regular and give the required isomor-
phism.
292 Chapter 19. Global State Considered Unnecessary
Todefine active-passive object spaces APFOb, we equip object spaces (ICI, :c)
with designated (partial) submonoids ICLv such that, for all x, y E ICip, and x' E ICl,
(Since xy : yx, the product xy of passive tokens is defined only if x : y.) The arrows
are the passivity-reflecting regular maps.
Passive object spaces are those with ICip = ICl. The full subcategory of passive
object spaces is reflective and coreflective. The functors 83, P: APFOb- APFOb (taking
values in the subcategory) are defined as follows: !'C is the passive subspace of C and
!'(f: C -R D) is the restriction of f to !'C -R t'D. Todefine PC, we consider the least
equivalence relation on ICI such that, for all x,y E ICI, xy = yx and x ::> y => xy = y.
Then,
IPCI { [x]: 'ltX1.X2 E [x].XI :c xz}
[x] :pc [y] = 3x' E [x],y' E [y].x' :c y'
This space is equipped with the submonoid IPCip = IPCI to make a passive object
space. The action of P on a map f: C - R D is
Pf:PC-RPD = {[x]-[y]:x-yE(, [x]EIPCI}
It can be verified this is a regular map (in particular, [y] E IPD I), and that the action
is functorial. The counit is then given by
derc:PC -R C = { [x]- x: x EI Cl, [x] E IPCI}
To relate states involved in the operational semantics and traces involved in the
denotational one, we define some relations. For s E ltvar[8]1. define a relation
s !; 181 x 181 inductivelyby
i ....!1...;1
i1
= i = i1
(get.J} ·s
i1
= i= j 1\ i...!... i1
(put.j} ·S
= . s
J-+-1
.,
We extend this to states and trace environments (with respect to a context 6161 ) by
u...!!.. ul
= u(x) ~ U 1 (x) for all identifiers x in 616 1
Note the following facts:
1. The relation ..!!.. is single-valued, i.e., u ..!!.. U 1 and u ..!!.. u" implies U 1 = u".
3. u ..!!.. U for some u and U if and only if each 17(x) is a storage cell trace.
1 1
111 e 112 - *
E [CI II Cz].
Suppose P = new[8] x. C. Then (u,P) ~ U 1 iff there exists i such that (u e
[x-init[8]], C) ~ U 1 e [x-i]. By the inductive hypothesis, this is equivalent to the
existence of 11 and s suchthat u e [x-init[8]] 'lfD[x-sl U 1 e [x-i] and 11 e [x-s]-
* E [C]. So, we have u..!!.. U 1 and, since s E cellinit[6]• 1J- E [new[8] x. C]. I *
Lemma 25 Semantics is sound (or reduction, i.e., i(P -* P 1
then [P] = [P 1 ] .
294 Chapter 19. Global State Considered Unnecessary
Proof: By induction on the length of the reduction sequence P - * P' and the struc-
ture of P. It only remains tobe seen that each reduction rule is semantics-preserving.
Tilis is a Straightforward verification. As an example, we show the property for the
last propagation rule:
if(Po,PI,Pz) := Q - if(Po, P1 := Q, Pz := Q)
Suppose 17 - * is in the denotation of the left-hand side. Then there is a decomposi-
tion 17 = 1'/r · 110 · 111 suchthat 1'/r- i E [Q] (for some i E 181), '70- t E [Po] (for some
t E {tt,ff}), and 111- put.i E [Pk] (for k = 1,2 depending on t). Now, I'Jr and 110 are
*
passive traces (because i and t arepassive tokens). So, 17 = 110 · 1'/r · '11· Clearly, 17- is
in the denotation of the right-hand side. Moreover, the argument is invertible. Hence,
the two sides have the same denotation. 1
For the opposite direction, we define a notion of computable phrases:
Definition 26 A phrase P is said to be computable if one of the following conditions
holds:
• Suppose Pisa semi-closed phrase of type e. If (J is a base type, Pis computable
if there exists P' such that P - * P' and [P'] is adequate for execution (in the
sense of Lemma 24, forward direction). If (J is var[ 8], P is computable if deref P
is computable and P := E is computable for all semi-closed computable expres-
sions E. If (J is B1 x Bz, Pis computable if fst(P) and snd(P) are computable. If
(J is B1 - Bz, Pis computable if, for all semi-closed computable phrases Q, PQ
is cornputable.
• A general phrase x1: B1. ... , Xn: Bn f- P : (J is computable if, for all semi-closed
computable phrases Q1, ... ,Qn of appropriate types, [Q!, ... ,Qn/X!.····Xn]P
is computable.
Lemma 27 If P - P' and P' is computable then P is computable.
Proof: By induction on the type of P. I
Lemma 28 If[P] = 0 then P is computable.
Proof of Proposition 22: Suppose C terminates, i.e., there exists C' such that
C -* C' and ([ ], C') ~ [ ]. It is easy to see (by induction on the structure of
C') that ([ ], w(C')) ~ [ ]. Lemma 25 gives [C] = [C'], and Lemma 24 shows
([] .... *) E [w(C')]. Since [w(C')] s;; [C'], we have ([] .... *) E [C'].
Conversely, if ([] .... *) E [C], by Lemma 29, there exists a phrase C' suchthat
C - * C' and [ C'] is adequate for execution. Since [ ] is the only state for the empty
type context and [] .!l [ ], we have that ([ ], C') ~ []. Hence, C terminates. I
Chapter 20
Linearity, Sharing and State: A Fully Abstract
Game Semantics for IDEALIZED ALGOL with
Active Expressions
Samson Abramsky and Guy McCusker
Contents
1 Introduction 297
2 Models for IA 299
3 The Functional/Imperative Boundary 302
4 TheSemanticsofnew 303
5 Games 306
6 IA- {new} 314
7 Modelling new 316
8 Computational Adequacy 318
9 Full Abstraction 324
10 The Extensional Model 324
11 Related Work 326
12 Further Directions 327
References 328
The manipulation of objects with state which changes over time is all-
pervasive in computing. Perhaps the simplest example of such objects
are the program variables of classical imperative languages. An impor-
tant strand of work within the study of such languages, pioneered by
John Reynolds, focusses on IDEALIZED ALGOL, an elegant synthesis of
imperative and functional features.
We present a novel semantics for IDEAUZED ALGOL using games,
which is quite unlike traditional denotational models of state. The model
takes into account the irreversibility of changes in state, and makes ex-
plicit the difference between copying and sharing'of entities. As a formal
measure of the accuracy of our model, we obtain a full-abstraction theo-
rem for IDEALIZED ALGOL with active expressions.
1 Introduction
Our starting point is the elegant synthesis of functional and imperative pro-
gramming due to John Reynolds [27]. His approach is to take an applied
simply-typed A-calculus in which, by suitable choices of the base types and
constants, imperative features can be represented. The resulting language is
referred to as IDEALIZED ALGOL (lA for short).
The language is in fact parametrized by the choice of basic data types and
operations on them. ln Reynolds's approach, a sharp distinction is drawn be-
tween these basic data types-which can be regarded as discrete sets-and
An extended abstract of this work is to appear in the Proceedings of ünear Logic 1996, Electronic
Notes in Theoretical Computer Sdence, volume 3, Elsevier Sctence, 1996.
P. W. O’Hearn et al. (eds.), Algol -like Languages
© Springer Science+Business Media New York 1997
298 Chapter 20. linearity, Sharing and State
the pbrase types of the programming language. For each basic data type
(i.e., set) X, there will be two basic pbrase types of the language:
• exp[X]-the type of expressions that yield values in the set X.
• var[X]-the type of (assignable) program variables that can contain
values from the set X.
In addition, there is a basic type c om of commands. Reynolds makes a sharp
distinction between expressions, which he does not allow to have side-effects,
and commands, which are executed precisely to have side-effects. We shall
relax this distinction, and can consequently identify com with exp[1] where
1 is a one-point set-the "unit" type.
The syntax of basic types is then
B ::= exp[X] I var[X] I com.
The general types are defined by
T ··= BIT=> T.
The constants of the language will be as follows:
• Recursion combinators YT: (T => T) => T foreachtype T.
• Conditionals
condB: exp[B] => B => B => B
for each base type B.
• For each operation
{:X1 X··· X Xk- X
on the basic data types, a constant
f: exp[Xd => • • • => exp[Xk] => exp[X].
• Command sequencing:
seqx: com => exp[X] => exp[X]
for each set X. Taking X= 1 this yields
seq: com => com => com
as a special case. Also,
skip:com.
• Variable dereferencing and assignment:
derefx: var[X] => exp[X]
assignx:var[X] => exp[X] => com.
• Block structure:
newx.Y: (var[X] => exp[Y]) => exp[Y].
Again, taking Y = 1 yields
newx: (var[X] => com) => com
as a special case.
Samson Abramsky and Guy McCusker 299
The first three items mean that we can regard lA as an extension of PCF [24],
identifying exp[N]. exp[B] with the basic types nat, bool of PCF. Reynolds's
version of the language, in which expressions cannot have side-effects, is ob-
tained by removing all constants seqx, newx,Y except for seq, newx. With
these constants, we can encode the usual imperative constructs.
Examples
1. x := x + 1 is represented by
x:var[N]t- assignx (succ(derefx)): com.
2. while •(x = 0) do x := x- 1 is represented by
x: var[N]t- Ycom(i\c: com. cond (iszero(deref x))
skip
(seq (assignx (pred(derefx)))
c)): com.
2 Models for lA
What should a model for lA look like? Since the language is an applied simply
typed i\-calculus, we should expect to model it in a cartesian-closed category
C [9]. To accommodate the recursion in the language, we can ask for C to
be cpo-enriched [5], or more minimally, to be rational [2]. This says that C
is enriched over pointed posets, and that the least upper bounds of chains
{fk o .L I k E w} of iterated endomorphisms f: A- A exist in a suitably enriched
and parametrized sense (see [2] for details).
As regards the basic data types, following [1] we can ask that the functor
C(l, -):Co- Set.
300 Chapter 20. Iinearity, Sharing and State
[ se~] = [g. I • E { •} ]: 1: ~ A ~ A
where g. = [.\x:A.x].
To understand this modelling of sequencing, think of 1: as a type with
a single defined value (the denotation T of skip); this value being re-
turned when a command is invoked corresponds to the successful ter-
mination of the command. Now just as a conditional firstly evaluates
the test, and then selects one of the branches for execution, so seq as
a "unary case statement" will firstly evaluate the command, and then,
provided the command terminates successfully, proceed with the con-
tinuation.
What about variables? Here we can make use of another important idea due
to Reynolds [27]. 1f we analyze the "methods" we use to manipulate variables,
in the spirit of object-oriented programming, we see that there are just two:
• the reading (or dereferencing) method:
derefx: var[X] ~ exp[X] .
• the writing (or assignment) method:
assignx:var[X) ~ exp[X) ~ com.
and
r = (rx I x EX): (exp[X] => com) - acc[X]
where rx: (exp[X] => com)- com is defined by:
exp[X] => com::;;;; (X:,=> com) x 1 ~<X:.=> com) x X:.~ com.
If these definitions are worked out in the case of Cpo, it will be seen that r is
"strictification", while c is just inclusion of strict functions, as expected.
Given these definitions, we can now define
[deref] = snd
[assign] = c o fst.
Since we have omitted products from our version of lA for notational simplic-
ity, it also makes sense to define a variable constructor
mkvarx: (exp[X] => com) => exp[X] => var[X]
by
[mkvarx]fe = (r o {, e).
the syntax, but that actual models so arising do faithfully reflect the concepts
in the language, and capture the operational behaviour of programs. We can in
fact do this in a very strong sense. As we shall see in Section 6, the categories
of games used to give the first syntax-independent constructions of fully ab-
stract models for PCF [2, 11], when used to give models for lA- {new} in the
way we have described, again yield fully abstract models. Moreover, the proof
of full abstraction is a very easy extensionofthat for PCF, and can be given at
the axiomatic level introduced in [1]. This latter point means that any model
of the axioms in [1] yields a fully abstract model of lA- {new}.
Firstly, however, we shall turn to the question of modeHing the new con-
stants.
• Irreversibility is addressed, since the state of the storage cell will change
as C interacts with it.
How can we formalize this idea in our current framework? A first attempt is
to consider introducing a constant
cellx: 1 - var[X]
such that, if f: var[X] - A, new(f) is given by the composition
new(f) = 1 ~ var[X] ..1.. A.
The idea is that cellx gives the "behaviour" of our storage cell process. How-
ever, recalling that
var[X] = comx x exp[X]
this is clearly hopeless, since a constant of this type, which in particular will
supply a constant value every time we read from the variable, is clearly just
what we don't want!-We need to take account of the changing state of the
variable.
At this point we produce our deus ex machina: linear Logic! Up to this
point, we have been working exclusively with intuitionistic types; since every-
thing except new was essentially functional, this was all we needed, at least to
get a model. But now we need a loop-hole to get some access to the dynamics,
and linear Logic provides such a loop-hole. Suppose then that our cartesian
closed category C arises as C = K,(.L), the co-Kleisli category of a linear cate-
gory L with respect to the ! comonad [6, 28]. The intuitionistic function types
we have been using get their standard decompositions into the linear types:
A~B=!A-oB.
!der
!var[X] _., exp[A]
!~
(!var[X] _., exp[A]) ®I
lidecellx
!Ap
exp[A]
Samson Abramsky and Guy McCusker 305
(Here der is the derelietion map (the eounit of !), and Ap is the linear appliea-
tion.)
Note that Linear types really are neeessary here. If we had a eonstant
cellx: var[X] in the language, and tried to define
new f = f cellx,
then this would be interpreted in K, (L) by
cellÖ f
I - !var[X] - - A
cellx:I- !var[X]
that does eapture the behaviour of a storage eell objeet, then we have eom-
pleted our semanlies of lA. In Seetion 7 we shall see that this ean indeed be
done for a suitable eategory of games, and by this means we will obtain the
first fully abstraet model of lA.
The point tobe emphasized here is how small an inerement from the mod-
elling of PCF is required to eapture lA, provided a suffieient handle on the
dynamies is present in our semanlies in order to define the cell morphism.
The key feature of game semanties is that the dynamics is already there.
How is sharing represented in this approaeh? Firstly, the multiple refer-
enees to a variable are interpreted using the eoeommutative eomonoid strue-
ture of !var[X], i.e., the eontraetion rule, so that the interpretation of a bloek
new x in C looks like:
!var[X]
cell
/®
I !var[X] : !Cl com
~®
!var[X]
The eontraetion merges the aeeesses to the variable x arising from the various
oecurrenees of it in C into a single "event stream". The task of the cell
morphism is to impose the appropriate eausality on this event stream, so that
in particular a read will return the last value written.
306 Chapter 20. linearity, Sharing and State
5 Games
We will describe three categories of games in this paper:
• {7: a linear category of games;
XA(m) = OQ = .\A(m) = PQ
and so on.lf .\~P(m) = 0, we call man 0-move; otherwise, m is a P-move;
• 1- A is a relation between MA + { *} (where * is just a dummy symbol)
and MA, called enabling, which satisfies
(el) *1-Am=>.\A(m)=OQA[nl-Am = n=*];
(e2) mI-An 1\ .\~(n) = A => .\~(m) = Q;
(e3) m 1-A n " m /. * => .\~P (m) /. .\~P (n).
Samson Abramsky and Guy McCusker 307
The idea of the enabling relation is that when a game is played, a move can
only be made if a move has already been made to enable it. The * enabler
is special-it says which moves are enabled at the outset. A move m such
that * 1- A m is called initial. Conditions (e2) and (e3) say that answers are
enabled by questions, and that the protagonists always enable each other's
moves, never their own.
Given an arena, we are interested in sequences of moves of a certain kind.
Before defining these, let us fix our notation for operations on sequences. If s
and t are sequences, we write st for their concatenation. We also write sa for
the sequence s with element a appended. Sometimes we use the notation s · t
or s · a when it aids legibility. The empty sequence is written as E. The prefix
ordering on sequences is written as ~;.
Definition A justified sequence in an arena A is a sequence s of moves of A,
together with an associated sequence of pointers: for each non-initial move m
in s, there is a pointer to a move n earlier in s such that n 1- A m. We say that
the move n justifies m. Note that the first move in any justified sequence must
be initial, since it cannot possibly have a pointer to an earlier move attached
to it; so by (el), justified sequences always start with an opponent question.
Given a justified sequence s' define the player view rs, and Opponent
view LsJ of s by induction on lsl, as follows.
rE' E.
rsm' . . s,m, if m is a P-move.
rsm' m, if * 1-m.
rsmtn' rs'mn, if m justifies n and n is an 0-move.
LEJ E.
5.2.2 Strategies
Definition A strategy a for a game A is a non-empty set of even-length posi-
tions from PA, satisfying
(sl) sab E a ==> s E a.
(s2) sab, sac E a ==> b = c, and the justifier of b is the same as that of c. In
other words, the justified sequences sab and sac are identical.
The identity strategy for a game A is a strategy for A __" A defined by
idA = {s E Pl~~Az I Vt ~;;even S. (t t A1 = t t Az)}.
We use subscripts to distinguish the two occurrences of A, and write t ~;;even s
to mean that t is an even-length prefix of s.
All that idA does is to copy the move made by Opponent in one copy of
A to the other copy of A. The justifier for Player's move is the copy of the
justifier of Opponent's move. lt is easy to checkthat this does indeed define a
strategy.
5.2.3 Composition
The categories we will work in have games as objects and strategies as mor-
phisms. Therefore, given strategies a:A __"Band T:B __" C, we would like to
compose them to form a strategy a; T: A __" C. First, some auxiliary definitions
are necessary.
Definition Let u be a sequence of moves from games A, B and C together
with justification pointers from all moves except those initial in C. Define
u t B, C to be the subsequence of u consisting of all moves from B and C; if a
pointer from one of these points to a move of A, delete that pointer. Similarly
define u t A,B. We say that u is an interaction sequence of A, B and C if
u t A,B E PA-Band u t B, CE PB-C· The set of allsuch sequences is written
as int(A, B, C).
Suppose u E int(A, B, C). A pointer from a C-move must be to another
C-move, and apointer from an A-move a must be either to another A-move,
or to an initial B-move, b, which in turn must have a pointer to an initial
C-move, c. Define u t A, C tobe the subsequence of u consisting of all moves
from A and C, except that in the case outlined above, the pointer from a is
changed to point to c:
A B
b/
__,c C
becomes
A
/c c
a
/ a
Given strategies a: A __" B and T: B __" C, define a II T tobe
{u E int(A,B,C) Iu t A,B E a 1\ u t B,C E T}.
We are now ready to define the composite of two strategies.
Definition If a:A __"Band T:B __" C, define a; T:A __" C by
a ;T = { u t A, C I u E a II T}.
310 Chapter 20. Linearity, Sharing and State
We can now define natural isomorphisms unit, assoc and comm with com-
ponents unitA: A ® I - A, assocA,B,c: A ® (B ® C) - (A ® B) ® C and
commA,B: A ® B - B ® A given by the obvious copycat strategies-in each case
the set of moves of the domain game is isomorphic to the set of moves of the
codomain game. It is then trivial to verify the foHowing.
5.2.8 Products
Given games A and B, define a game A &B as follows.
MA&B MA +MB
AA&B [.\A, AB]
* 1-A&B n <=> *1-AnV*I-Bn
m 1-A&B n <=> m1-Anvm1-Bn
PA&B {s E LA&B Ist A E PA Ast B = E}
u {s E LA&B I s t BE PB A s t A = E}.
We can now define projections rr1:A&B- A and rr2:A&B- B by the obvious
copycat strategies. Given u: C-A and T: C- B, define (u, T): C- A&B by
(u, T) {s E Lc-A&B Ist C,A Eu Ast B = E}
u {s E Lc-A&B Ist C,B ETAs t A = E}.
Proposition 4 A & B is the product of A and B in both {i and {imn, with projec-
tions given by rr1 and rr2.
1t should be clear how this definition generalizes to give all products.
5.3 Exponential
Definition Given a game A, define the game !A as follows.
M!A MA
A!A AA
1-!A I- A
P!A {s E L!A I for each initial move m,s t m E PA}.
5.3.1 Promotion
Given a map u: !A - B, we wish to define its promotion ut: !A - !B to be a
strategy that plays "several copies of u". However, in general this cannot be
done because there is no way for ut to know how the many threads of dialogue
in !A - o !B should be grouped together to give dialogues in !A - o B. There is a
dass of games B for which this can be done, however: the well-opened games.
Definition Agame A is well-opened iff for all sm E PA with m initial, s = E.
In a well-opened game, initial moves can only happen at the first move, so
there is only ever a single thread of dialogue. Note that if B is well-opened
then so is A - o B for any game A, so while !A is not well-opened except in
pathological cases, the game !A - o Bis well-opened whenever Bis. We are
going to construct a cartesian closed category in which all games are well-
opened and exponentials (in the ordinary sense, not the linear logic one) are
given by !A - o B, so this Observation is important.
Given a map u: !A-B, define its promotion ut: !A- !B by
ut = {s E L!A-!B I for all initial m,s t m Eu}.
Proposition 5 If A and B are well-opened games, and u is a strategy for
!A - o B, then ut is a strategy for !A - o !B. If u is innocent then so is ut.
312 Chapter 20. Linearity, Sharing and State
5.3.2 Dereliction
For well-opened games A, we can define derA: !A- A tobe the copycat strategy
{s E Pt'Z~A I Vt !;;even s.t r !A = t r A}.
Dereliction and promotion behave as expected where they are defined.
Proposition 6 Let A, B and C be well-opened games, and Iet u: !A - B and
T: !B - C be strategies. Then
• ut ; derB = u,
• der1 = id,A, and
• ut;Tt=(ut;T)t.
We now note an important Iemma.
Lemma 7 (Bang Lemma) If B is well-opened and u: !A - !B is innocent then
u = (u;derB)t.
5.3.3 Contraction
We define conA: !A - !A ® !A. For any s E P!Ao-!At @!Az, Iet I be the set of
occurrences of initial moves in A1 and J be the set of occurrences of initial
moves in A2. Let s1 = s r I and s2 = s r ]. Then define conA as
{s E P,'Z:~!At@!Az I Vt !;;even s. (tl r !Ao = tl r !AI) A (t2 r !Ao = t2 r !A2)}.
5.3.4 Exponential isomorphisms
These reduce to identities in the present setting:
!(A&B) !A ® !B.
I = !I.
6 lA- {new}
The structure exhibited for G1nn in the previous section allows us to model
lA- {new} according to the definitions given in Section 2. Since innocent
strategies correspond precisely to functional programs, as shown in [11, 13,
14], this provides some immediate confirmation of our thesis that lA- {new}
is a pure functionallanguage.
The aim of this section is to prove the following result.
Theorem 12 (Innocent Definability) Let T be an IA type. Every compact inno-
cent strategy u on the game [ T] is definable in IA- {new}; i.e., for some closed
term1-M: T, [M] = u.
We will in fact prove a more general result at the axiomarte Ievel introduced
in [1]. (We shall assume the material in [1] as background.)
Theorem 13 (Definability for Normed Sequential Categories) Let C be a
normed sequential category with all small products (where it is understood that
rr-atomicity now re{ers to arbitrary products rather than just finite ones). Any
such category gives rise to a model of IA- {new} as specified in Section 2. Then
for any IA typeT and compact morphism f: 1 - [T] in C, f is definable in
IA- {new}.
Samson Abramsky and Guy McCusker 315
Since Cmn is a normed sequential category, as shown in [1] and [13], Theo-
rem 13 implies Theorem 12.
Proof: The proof is a Straightforward extension of the proofs of the Decom-
position Theorem (Theorem 4.1) and the Definability Theorem for Normed
Categories (Theorem 8.2) in [1]. We shall just describe the additional steps in
the argument. For notational simplicity, we shall assume that the only basic
data types are N and the unit type.
Suppose then that T = T1 ~ T2 ~ . . . ~ Tk ~ B is an IA type, where
Ti = Ui.l ~ ... ~ Ui,q1 ~Bi, 1 ::5 i ::5 k, and that
f: 1- [T]
is a compact morphism in C.
Suppose first that [B] =X:. (so that B = exp[N] or B = com = exp[l ]).
Since [B] is discrete, we can apply a case analysis:
• f =.L. Then f = [,\x: f.n], where we write f = T1, ... , Tk.
• f is (the exponential transpose of)
!(T1 & · · · & Tk) --.. 1 ___i_.. .f:.
for a (necessarily unique) c E X. Then if B = exp[N], f = [A.x: f.c],
while if B = com, f = [,\x: f. skip].
• f =Ci({!, ... ,fqp (go, . .. ,g,_I)) where Bi= exp[N]. Then
f = [A.x: f. case,(xi(Mlx) · · · (Mq1x))(Pox) · · · (P1-1x)]
where [Mj] = fi for 1 ::5 j ::5 qit and [Pj] = 9i for 0 ::5 j < l.
7 Modelling new
Returning to the discussion in Section 4, to complete our semantics for lA, we
must specify a morphism
cellx:I- !var[X].
Plays in !var[X] have the form
read · vo · write(vt) · ok · read · v2 ...
where each re ad and each wri t e (-) is initial, and all other moves are justi-
fi.ed by the immediately preceding move. Note that the type !var[X] imposes
no causality constraints between the result returned by a read and the value
previously written. Thus in the above example we could have v2 :/= v1 • The
definition of cell as a deterministic strategy on this game is quite clear; it
should respond to a write(v) with ok, and to a read with the value last
written, if any. If there has been no write performed, the uninitialized cell
will have no response to a read, while an initialized cell cellx.vo with initial
value v0 will return v0 in this case.
This strategy clearly implements the required behaviour, and is a well-
defi.ned deterministic strategy. However, it is not innocent. Indeed, note
that since all Opponent moves are initial, the P-view when Opponent has just
moved consists only of the move Opponent has just played. Thus innocent
and history-free strategies coincide on this game. This lack of innocence is
exactly what allows cell to take account of the previous accesses to the vari-
able, and hence to correctly implement sharing rather than copying.
Thus we find that our model of lA- {new} lives entirely in the subcate-
gory Cmn (and implicitly in ~mn), while to model new we have to use the larger
category ~ of knowing strategies. An obvious question then arises: what sub-
category of ~ is generated by ~inn and cell? The answer is provided by the
following result.
Theorem 14 (Innocent Factorization) Let A be agame suchthat
sa E p~dd A •sa,b E PA ==> sab E PA,
and u: I - A be any (knowing) strategy. Then there exists a set X, x 0 E X, and
an innocent strategy T: !var[X] - A such that
0' = cellx,x0 ; T.
Moreover, if u is compact, T is innocently compact.
Note that this is a weak orthogonality property in the sense of factorization
systems [7].
Samsan Abramsky and Guy McCusker 317
Proof: The idea is that T simulates a using its view of the play in A together
with (a code for) the full previous history of the play, which it keeps in the
variable. Thus we use state to encode history, a standard idea in automata
theory; the interesting thing here is that we find a point of contact between
machine simulations and factorization systems.
The set X is taken to be (a set of codes for) the positions of A (so it must
be a set of cardinality greater than or equal to that of the set of positions of
A), and x0 tobe the code for the initial position E. Note that the P-view of a
position s in !var[X] _" A contains the string 's t A,. At each 0-move a in A,
following a previous play s, the strategy T behaves as follows:
If a does not have a response at any position of A with player view
'sa t A,, T has no response. Otherwise, it reads code(t) from the variable.
Let b = a(ta). If 'sa t A'b f/; PA, T has no response. Otherwise it writes
code(tab) back into the variable, and then plays bin A.
Note that the assuroption on A together with the fact that a is a valid
strategy ensures that T is well-defined. It is clear that T is innocent, and that
the composite cellx,xo; T = a, as required. Further, if a is compact, it only
has a responsebat a finite nurober of positions ta. ln this case, the strategy T
only has a response at a finite nurober of views, so T is innocently compact. I
We must now show that for any lA type T, the game [T] satisfies the
hypothesis of the above theorem.
Lemma 15 For any IA type T, the valid positions of the game [ T] are precisely
the legal positions with at most one initial move.
Proof: By induction on the structure of T. The base cases are trivial, while
for the inductive step we make use of several technicallemmas from [13].
Suppose T = A ~ B, so that [T] = ![A] __., [B]. It is clear that each valid
position contains at most one initial move and is legal. For the converse, we
proceed by induction on the length of a legal sequence containing at most one
initial move. Again the base case is trivial, so consider a legal position sa with
at most one initial move, and suppose that s E P[T]. To complete the proof we
must show that sa t ![A] E P!IAI and sa t [B] E P[BJ· By the outer inductive
hypothesis, it suffices to show that each of these sequences is legal. For the
altemation condition, in the case a is an 0-move, the visibility condition in [T]
implies that a is in the same component as the last P-move ins, so both so
the altemation condition is satisfied. If a is a P-move, the "state diagram" of
Lemma 3.2.1 of [13) shows that 0 played last in both components, so P cannot
possibly violate the altemation condition. The bracketing condition is clearly
satisfied in each component. For the visibility condition, one can adapt the
proof ofLemma 3.1.6 of [13] to show that 's, t [B] is a subsequence of 's t B',
and similarly for the 0-view, and for the other component. Therefore, since the
justifier of a is in the appropriate view of s, it is also in the appropriate view
of s restricted to the component of a, so the visibility condition is satisfied. I
Proposition 16 For every IA type T, the game [T] satis(ies the hypothesis of
the Innocent Factorization Theorem.
318 Chapter 20. Linearity, Sharing and State
Proof: We must show that if sa E P[f1 and 'sa'b E P[TJ then sab E PITI.
Since r sa' b E Prn. we know that sab is a justified sequence and that players
altemate. The bracketing condition holds in sab because the pending question
at sa is the same as that at r sa,. The visibility condition for sab is clearly
satisfied. Therefore sab is a legal position, and contains at most one initial
move since sa does, and b is a Player-move and therefore not initial. So by the
previous lemma, sab E P[T). I
Theorem 17 (Definability) Every compact strategy er: I - [T] in C, where T
is an JA type, is definable in JA.
Proof: By Theorem 14 and Proposition 16, er factors as
er = cellx,v0 ; T
8 Computational Adequacy
1n this section, we introduce the operational semantics of lA and demonstrate
that the the games model is both sound and computationally adequate, yield-
ing an inequational soundness theorem. We present the operational semantics
as a "big-step" evaluation relation, with an auxiliary relation of "structural con-
gruence" (cf. [17)) denoted by =. The structural congruence isthat generated
by ß-conversion and all instances of
M=YM.
We shall assume that the only program variables are of type N for simplicity.
Let us introduce some notation. A var[N]-context r is one of the form r =
xi: var[N], ... ,xn: var[N], with the xi distinct. Ar-store isapartial function
s: {XI, ... , Xn} - {n I n E N}. We write St(f) for the set of all such stores.
If s E St(f,x:var[N]), then s 1 x is the r-store obtained by restricting the
domain of s, while (s I x .... n) denotes the result of updating the contents of x
instobe n. Note that this operation may extend the domain of s, if s(x) was
previously undefined, or may override an existing value for x.
We now define relations of the form
r,s 1-M .(J.B c,s'
where
Samson Abramsky and Guy McCusker 319
• r is a var[N]-context
• s,s' e St(f)
• B is a base type
• f 1- M : B is a term-in-context
• c is a canonical form such that r 1- c : B is derivable.
The canonical forms are:
• at type exp[N], the numerals n, and similarly at other expression types;
• at type com, the command skip;
• at type var[N], variables x and expressions mkvar f e.
The relations are defined inductively below. We omit the type tags, and leave
the reader to fill in the rules for the extra, unspecified, operations on base
types.
Structtrrrucongruence----------------------------------~
r,sl-clJ.c,s
Conditional-------------------------------------------,
f,s 1-M 1J. tt,s' f,s' 1- N 1J. c,s" f,s 1-M 1J. ff,s' f,s' 1- P 1J. c,s"
r,s 1- cond MN P 1J. c,s" r,s 1- condM NP 1J. c,s"
Aritlnnetic------------------------------------------,
f,s 1-M 1J. n,s' f,s 1-M 1J. n+ l,s'
f,s 1-succ M 1J. n+ l,s' f,s 1- predM 1J. n,s'
~quendmg----------------------------------------~
~ferenc~----------------------------------------,
As~gmnent------------------------------------------,
· f,si-E~n.s' [,S'i-V~x.s"
r,s' 1- assign V E ~ skip, (s" I x- n)
f,s 1- E ~ n,s' f,s' 1- V~ mkvar f e,s" f,s" 1- fn ~ skip,s"'
r,s' 1- assign V E ~ skip,s"'
Blocksttucnue---------------------------------------,
f,x:var[N],s 1-M~ c,s'
r,s 1- new ll.x.M ~ c,s' 1x
At first sight, the rule for new may seem restricted, since it applies only
to terms of the form new ll.x.M. However, there is no loss of expressive
power. In our denotational model, the q-law is valid, so for any term M of
type var[X] ~ exp[Y], the terms new M and new ll.x.Mx have the same de-
notation. In the light of the adequacy result to follow, this means that these
two terms are observationally equivalent, so our rule is sufficiently general.
A program is a closed term of type com. We write P~ as an abbreviation
for, () 1- P ~ skip, ()in this case, and Pft for ...,p~. Let Trm(f, T) be the set
of all M suchthat r 1-M: T is derivable. The program contexts Ctxt(f, T) are
those contexts C[-] suchthat C[M) isaprogram for all ME Trm(f, T). The
observational preorder S on Trm (f, T) is then defined by
MSN iff VC[-] E Ctxt(f, T). C[M] ~ => C[N] ~.
8.2 Soundness
We are now in a position to state the soundness lemma.
Lemma 18 For any var[N] -context r, any term r 1- M : A and any s E St([),
iff, s 1- M JJ. c, s' then each sequence qt in [s] II [M] has the form qt1 t2, where
t 1 is a sequence of reads and writes in the [r] component leaving state s', and
[s'] II [c] contains the string qt2 • Further, each sequence in [s'] II [c] arises in
this way.
Notice that [s] interpreted as a r,x : var[N] state is the extension of the
interpretation of [s] as a r state by a single uninitialized cell, that is
I 2-o I® I [s]®cell [r] ® !var[N].
322 Chapter 20. linearity, Sharing and State
By the inductive hypothesis, the sequence [s] II [M] has the form
[M]
I~ [f] ® !var[N] ----o com
run
t
done
where t leaves state s'. But by definition of [new], the strategy [new(,\x.M)]
behaves as cell; [M], so [s] II [new(,\x.M)] behaves as required.
The cases for all the other constants are similar. We consider that of seq
for illustration. The rule in question is below.
f,s 1-M .U skip,s' f,s' 1- N .U c,s"
r,s 1- seqMN .u c,s"
By the inductive hypothesis, [s] II [M] has the form run · t1 • done, where t 1
leaves state s', and the sequences of moves in [s'] II [N] have the form qt2t3,
where t 2 leaves state s". The sequences in [s"] II [c] are precisely those of the
form qt3 • Because of the copycat nature of [seq], it is then easy to show that
[s] II [seqMN] consists of strings of the form qt1tzt3, giving the result. I
The following simple corollary expresses the sense in which our model is
sound.
Proposition 19 If r, s 1-M .U c, s' then [s]; [M] = [s']; [c].
• a ~~ar[N] M iff
a; 1T1 ~~xp[NJ deref M 1\ a; 7Tz; 1Tn ~~om assign Mn.
In the above, andin what follows, given a strategy u: A ___., (!var[N] ___., B), we
use cell; A-l (u) as an abbreviation for the map
A 2-.o A ®I id®cell A ® !var[N] ~ B.
The extra clause in the case of var[N] ~ com reflects the fact that program
variables can be bound in two ways: either by application to a term of type
var[N], or by allocation of a new local variable.
Lemma 20 Let r, ~ 1- M : T, where r is apart of the context containing only
variables of type var[N] while ~ may contain variables of any type (including
var[N]). Suppose ~ = y 1 : T1 , .•• , Yn: Tn and let O"; be strategies and N 1 be terms
suchthat CT; ~~; N; (or i = 1, ... , n. Write if; ~ [M] for the composite
[f] (id,UJ, ... ,Un) [f, ~] ~ [T],
9 Full Abstraction
With the Inequational Soundness and Definability results in place, it is a rou-
tine matter to show that C1:::, is a fully abstract model of lA.
Theorem 24 (Full Abstraction) Let M,N E Trm(f, T); then
[M]:::, [N] = MSN.
Proof: The left-to-right direction is the Inequational Soundness result above.
For the converse, suppose that [M] :f; [N]. Without loss of generality we may
assume M and N are closed terms; then by definition of the intrinsic preorder
there exists a strategy oc: ![T] - ~ suchthat [M] ~ oc = T and [N] ~ oc =1...
Clearly oc can be chosentobe compact. Since the game ~ is the same as [com],
the Definability Theorem implies that there is an lA term x: T 1- C[x] : com
suchthat [C[xl] = oc. Therefore [M] ~ oc = [C[M]], and similarly for N. By
Computational Adequacy, C[M] U and C[N] 1t. Therefore M ~ N, completing
the proof. I
Say that a play in a game is complete ü it is maximal and all questions have
been answered. Note that, for any lA typeT, the complete plays in [T] are
exactly those in which the initial question has been answered. We call agame
simple in this case. We write compA for the set of complete plays in A. If u
is a strategy on A, we write comp(u) = u n compA. We write Pref(S) for the
prefix-closure of a set of sequences S.
Theorem 25 (Cbaracterization Theorem) Let u and T be strategies on a sim-
ple game A in C. Then
u ::5 T = comp(u) ~ comp(T)
= Pref(comp(u)) ~ Pref(comp(T)).
Proof: Suppose that s = m1 •.. mn E comp(u) \ comp(T). We define a strategy
01: A - ~ that "follows the script of s" (playing as 0) as long as P does. If s is
completed, it replies in ~=
a
Note that 01 is a well-defined (but not in general innocent) strategy. Then by
assumption u; 01 = T while T; 01 =.L. Thus u ::5 T => comp(u) ~ comp(T).
For the converse, suppose that ut; 01 = T and Tt; 01 =.L. The interaction
-
between u and 01 has the form
-u
!A
()(
~
q
m1
mn
a
The sequence s = m1 ... mn can be decomposed into the "threads" s1 = s t it,
••• , Sk = s t ik, where i1, ... , ik are the occurrences of initial moves ins. Each
St must be a complete play in A by the bracketing condition. Clearly we must
have s1 ff. T for some j, 1 !> j !> n, since otherwise we would have Tt; 01 = T.
Finally, since Pref is monotone, comp(u) ~ comp(T) => Pref(comp(u)) ~
Pref(comp(T)). Since the elements of comp(u) aremaximal plays, they form
an anti-chain under the prefix ordertng; Pref is order-reflecting on anti-chains,
yielding the converse. I
Now given the dl-domain C(l ,A), where A is a simple game, define
p:C(l ,A)- C(l ,A) by
p(u) = Pref(comp(u)).
Proof: Note that p(u) = I (u n compA). 1t is easily seen from this description
that p has the stated properties. I
Note that it follows from the Characterization Theorem that
C(A,B)I~ ;;:;; ({p(u) Iu E C(A,B)},~).
11 Related Work
There have been two main strands of work addressing the issue of local-
ity of store in programming languages from a semantic point of view. The
first, based on the use of functor categories, was pioneered by Reynolds and
Oles [21], and has since been considerably refined, notably by O'Hearn and
Tennent [20]. The idea is essentially to take a traditional "global state" model
and parameterize it with respect to store shapes, to account for the allocation
and later deallocation of local variables. Stark has also used functor categories
to model ML-style references [30], and similar ideas have led to denotational
models of the TT-calculus [10, 31]. The second, perhaps computationally more
compelling, method has been termed "object-based semantics" by Reddy [25].
In this view, commands, procedures and variables are seen as objects or pro-
cesses that interact with one another during the course of a computation. Mil-
ner adopted this approach in an operational setting, translating a language
with local-variable declarations into the process calculus CCS [16], while Reddy
realized the same ideas denotationally, using coherence spaces [25]. Our work
can be seen as the next stage in this line of enquiry.
Samsan Abramsky and Guy McCusker 327
12 Further Directions
As we mentioned in the Introduction, Reynolds's original version of lA does
not allow side-effects in expressions. To capture this restricted language, we
must reflect the distinction between acttve and passive types in the model [26,
19]. A passive type P does not allow side-effects to the environment in the
course of computing a value of type P. This distinction is also crucial to the
Syntactic Control of Interference [26], which is a type discipline for controlling
interference and aliasing in imperative programs.
It is possible to refine the model we have presented here to incorporate
the active/passive distinction, and hence to get a fully abstract model for
Reynolds's originallanguage with passive expressions. This will be described
in a sequel to the present paper. From the point of view of game seman-
tics, what is interesting about this refined model is that, instead of a simple
innocent/knowing dichotomy, we get a whole spectrum of possibilities, with
innocent strategies (when all moves are passive) and knowing strategies (when
all moves are active) as the extreme cases.
This model should also prove a good setting for studying the Syntactic
Control of Interference from a semantic point of view.
Another point for further investigation is suggested by the following dia-
gram:
•l A •B
""'/
I A •B •I AB
lAB
Here I denotes innocence and B the bracketing condition. The category {]1nn
embodies both constraints on strategies, and very successfully captures pure
functional programming [11, 13, 14]. As we have seen in the present paper,
the category of knowing (but well-bracketed) strategies captures lA. If we con-
versely retain innocence but weaken the bracketlog condition then we get a
328 Chapter 20. Ilnearity, Sharing and State
model of PCF extended with non-local control operators [8, 22]. Thus we be-
gin to develop a semantic taxonomy of constraints on strategies mirroring the
presence or absence of various kinds of computational features.
References
[1) S. Abramsky. Axioms for full abstraction and full completeness. Submitted for
publication, 1996.
[2) S. Abramsky, R. Jagadeesan, and P. Malacaria. Full abstraction for PCF. Submitted
for publication, 1996.
[3) S. Abramsky and A. Jung. Domain theory. In S. Abramsky, D. Gabbay, and T. S. E.
Maibaum, editors, Handbook of Logic in Computer Science, Volume 3, pages 1-
168. Oxford University Press, 1994.
[4) A. Asperti and G. Longo. Categories, Types and Structures : An introducnon to
category theory for the working computer sdentist. The MIT Press, 1991.
[5) G. Berry, P.-L. Curien, and J.-J. Levy. Full abstraction for sequentiallanguages:
the state of the art. In M. Nivat and J. C. Reynolds, editors, Algebraic Semantics,
pages 89-132. Cambridge University Press, 1986.
[6) G. Bierman. What is a categorical model of intuitionistic linear logic? In Proceed-
ings ofthe Second International Con(erence on Typed i\-calculi and Applications,
pages 78-93. Leerure Notes in Computer Sdence, volume 902, Springer-Verlag,
1995.
[7) F. Borceux. Handbook of Categorical Algebra, volume 1. Cambridge University
Press, 1994.
[8) R. Cartwright, P.-L. Curien, and M. Felleisen. Fully abstract semantics for observ-
ably sequentiallanguages. Information and Computation, 111(1):297-401, 1994.
[9) R. Crole. Categories (or Types. Cambridge University Press, 1994.
[10) M. P. Fiore, E. Moggi, and D. Sangiorgi. A fully abstract model for the rr-calculus.
In 11th AnnuallEEE Symposium on Logic in Computer Science, pages 43-54. IEEE
Computer Society Press, 1996.
[11) M. Hyland and C.H. L. Ong. On full abstraction for PCF. Submitted for publication,
1996.
[12) R. Loader. Finitary PCF is undecidable. Unpublished manuscript, available from
http: I/info. ox. ac. uk/..:.loader/,1996.
[13) G. McCusker. Games and Full Abstraction (or a Functional Metalanguage with
Recursive Types. PhD thesis, Imperial College, University of London, to appear.
[14) G. McCusker. Games and full abstraction for FPC. In 11th AnnuallEEE Symposium
on Logic in Computer Science, pages 174-183. IEEE Computer Society Press, 1996.
[15) R. Milner. Processes: a mathematical model of computing agents. In Logic Collo-
quium '73, pages 157-173. North Holland, 1975.
[16) R. Milner. A Calculus o(Communicating Systems. Springer-Verlag, 1980.
[17) R. Milner. Functions as processes. Mathematical Structures in Computer Sdence,
2(2):119-142, 1992.
Samson Abramsky and Guy McCusker 329
[18) P. W. O'Hearn and U. Reddy. Objects, interference and the Yoneda embedding.
In M. Main and S. Brookes, editors, Mathematical Foundations of Programming
Semantics: Proceedings of 11th International Con(erence, Electronic Notes in The-
oretical Computer Science, volume 1. Elsevier Science, 1995.
[19) P. W. O'Hearn, M. Takayama, A. ]. Power, and R. D. Tennent. Syntactic control
of interference revisited. In M. Main and S. Brookes, editors, Mathematical Foun-
dations of Programming Semantics: Proceedings o( 11th International Confer-
ence, Electronic Notes in Theoretical Computer Science, volume 1. Elsevier Science,
1995. See Chapter 18.
[20) P. W. O'Hearn and R. D. Tennent. Parametricity and local variables. Journal of the
ACM, 42(3):658-709, May 1995. See Chapter 16.
[21) F. ]. Oles. Type categories, functor categories and block structure. In M. Nivat and
]. C. Reynolds, editors, Algebraic Semantics. Cambridge University Press, 1985.
See Chapter 11.
[22) C.-H. L. Ong and C. A. Stewart. A Curry-Howard foundation for functional com-
putation with control (summary). Preprint, ]uly 1996.
[23) A. M. Pitts. Reasoning about local variables with operationally-based logical rela-
tions. In 11th AnnuallEEE Symposium on Logic in Computer Science, pages 152-
163. IEEE Computer Society Press, 1996. See Chapter 17.
[24) G. Plotkin. LCF considered as a programming language. Theoretical Computer
Science, 5:223-255, 1977.
[25) U. Reddy. Global state considered unncessary: Introduction to object-based se-
mantics. LISP and Functional Programming, 9(1):7-76, 1996. See Chapter 19.
[26) ]. C. Reynolds. Syntactic control of interference. In Conf. Record 5th ACM Sym-
posium on Principles of Programming Languages, pages 39-46, 1978. See Chap-
ter 10.
[27] ]. C. Reynolds. The essence of ALGOL. In ]. W. de Bakker and ]. C. van Vliet,
editors, Algorithmic Languages, pages 345-372. North Holland, 1981. See Chap-
ter 3.
[28) R. A. G. Seely. Linear logic, *-autonomous categories and cofree coalgebras. In
Category theory, Computer Science and Logic. American Mathematical Society
Contemporary Mathematics, volume 92, 1989.
[29) K. Sieber. Full Abstraction for the Second-Order Subset of an ALGOL-like Language.
Technischer Bericht A/04/95, FB14, Universität des Saarlandes, 1995. See Chap-
ter 15.
[30) I. Stark. Namesand Higher-Order Functions. PhD thesis, University of Cambridge,
December 1994.
[31) I. Stark. A fully abstract domain model for the rr-calculus. In 11th AnnuallEEE
Symposium on Logic in Computer Science. IEEE Computer Society Press, 1996.
[32) R. D. Tennent. Denotational semantics. InS. Abramsky, D. M. Gabbay, and T. S. E.
Maibaum, editors, Handbook of Logic in Computer Science, volume 3, pages 169-
322. Oxford University Press, 1994.
[33) G. Winskel. The Formal Semantics of Programming Languages. The MIT Press,
1993.
Chapter 21
The Essence of PARALLEL ALGOL
Stephen Brookes
Contents
1 Introduction 331
2 Syntax 333
3 Possible Worlds 334
4 Semantics of Types 335
5 Semantics of Phrases 338
6 Relational Parametricity 343
7 Conclusions 347
Acknowledgements 348
References 348
1 Introduction
The programming langnage ALGOL 60 has had a major impact on the theory
and praclice of langnage design and implementation. lDEALIZED ALGOL, as
characterized by John Reynolds [Rey81), augments a simple sequential imper-
ative langnage with a procedure mechanism based on the simply-typed call-
by-name .\-calculus; procedure definitions, recursion, and the conditional con-
struct are uniformly applicable to all phrase types. Reynolds identified these
features as embodying the "essence" of ALGOL.
lDEALIZED ALGOL is a sequential programming langnage. Nowadays there
is interest in langnages for shared-variable parallel programming, to permit
the design of efficient programs that exploit parallelism inherent in a problern
by running parallel commands that cooperate by reading and writing to shared
First appeared in Proceedings Eleventh Annual IEEE Symposium on Logic in Computer Sdence,
Brunswick, New Jersey, July 1996, pp. 164-174. © 1996 IEEE, reprinted with permission.
2 Syntax
2.1 Types and type environments
The type structure of our language is conventional [Rey81]: data types repre-
senting the set of integers and the set of booleans; phrase types built from
expressions, variables, and commands, using product and arrow. We use T as
334 Chapter 21. The Essence of PARALLEL ALGOL
a meta-variable ranging over the set of data types, and (} to range over the set
of phrase types, as specified by the following abstract grammar:
(} ::= exp[T] I var[T] I comm I (0- 0') I (} x (}'
T ::= int I bool
Let L range over the set of identifiers. A type environment rr is a partial func-
tion from identifiers to types. We write dom(rr) for the domam of rr, i.e.,
the set of identifiers for which rr specifies a type. Let (rr I t: 0) be the type
environment that agrees with rr except that it maps L to (}.
3 Possible Worlds
The category W of possible worlds [Ole82] has as objects a (small) collection of
sets called "worlds" or "store shapes", representing sets of allowed states. We
assume that Ob(W) is closed under (binary) cartesian product, and contains
Vmt = { ... ,-1,0,1, ... } and Vbool = {tt,ff}.
The morphisms from W to W' are pairs h = (f, Q) where f is a function
from W' to W and Q is an equivalence relation on W', such that the restriction
of f to each equivalence dass of Q is a bijection with W:
• Vx', y'. (x' Qy' & fx' = fy' ~ x' = y');
• 'Vx E W. Vy' E W'. 3x'. (x'Qy' & fx' = x).
Intuitively, when (f,Q):W - W', we think of W' as a set of "large" states
extending the "small" states of W with extra storage structure; f extracts the
small state embedded inside a large state, and Q identifies two large states
when they have the same extra structure.
Stephen Brookes 335
rr r- skip : comm
rr r- X: var[T] rr r- E: exp[T]
rr r- X:=E: comm
rr r- PI : comm rr r- Pz : comm
rr r- PI;Pz: comm
rr r- PI : comm rr r- Pz : comm
rr r- PI II Pz : comm
rr f- P : exp[bool) rr f- PI : 0 rr f- Pz : 0
rr f- Ü P then PI eise Pz : 0
rr r- B : exp[bool] rr r- P : comm
(P atomic)
rr r- await B then P : comm
rr, t:var[T] r- P: comm
rr r- new[T] L in P: comm
Tr f- L: 0 (when Tr(L) = 0)
rr f- p : Oo X OI
rr r- fst P : Oo
rr r- Po : Oo rr r- PI : OI
rr f- (Po, PI): Oo X OI
rr f- p : Oo X OI
rr f- snd p : OI
rr,t:Or-P:O
rr r- rec t.P: 0
Tr, L: 0 f- P : 0'
rr r- .\L: O.P : (0- 6')
rr f- p : 0 - 0' rr f- Q : 0
rr r- P(Q) : 0'
The identity morphism on W is the pair (idw, W x W), where idw is the
identity function on the set W. Foreach pair of objects W and V there is an
"expansion" morphism- x V: W - W x V, given by
- x V= (fst:W x V- W,Q),
where Q = {((w0 , v), (WI, v)) I w0 , WI E W & v E V}. The composite of mor-
phisms h = (f, Q): W- W' and h' = (g,R): W'- W", denoted h; h': W- W",
is the pair given by:
(f o g, {(zo,zd ER I (gzo,gzi) E Q} ).
4 Semantics of Types
Each type (}will be interpreted as a functor [(}] from W to the category P of
predomains and continuous functions, such that:
336 Chapter 21. The Essence of PARALLEL ALGOL
4.1 Commands
We interpret the type comm using "transition traces" [Bro93], but instead of
assuming a single global state set we parameterize our definitions in terms of
worlds. Foreach world W, [comm]W will consist of sets of traces over W.
A finite trace (w0, w0)(w1, wi) · · · (wn, w~) of a command represents a termi-
nating computation from state w0 to w~, during which the state was changed
externally n times (by interference from another command running in paral-
lel), the ich interruptionchanging the state from w;_ 1 to Wt. An infinite trace
((wn. w~))~=o of a command represents an infinite execution, again assuming
repeated interference.
When A is a set, we write A * for the set of finite sequences over A, A +
for the set of non-empty finite sequences over A, A 00 for the set of (countably)
infinite sequences over A, and A oo = A + u A 00 • Clearly, each of these operations
extends to a functor (on Set), the morphism part being the appropriate "map"
operation, which applies a function to each element of a sequence.
Using the above notation, a command denotes a subset of (WxW) More- 00 •
over, the trace set of a command is closed under two natural operations: stut-
tering and mumbling. The two closure conditions correspond to the use of
-*, the reflexive and transitive closure of the one-step transition relation, in
a conventional operational semantics for the programming language. As a re-
sult, each element (w, w') of a trace represents a finite sequence of atomic
actions performed by the relevant command. The use of closed sets of traces
guarantees full abstraction for the simple shared-variable language [Bro93].
We formalize this notion of closure as follows. We define relations
stutA,mumA s;;; (A x A)+ x (A x A)+ as follows:
stutA = {(ocß, oc(a, a)ß) I a E A & ocß E (A x A)+}
We write rt for the closure of T, that is, the smallest closed set of traces
containing T as a subset. Thus, commands denote closed sets of traces.
Let pt ( (W x W) 00 ) denote the set of closed sets of traces over W, ordered
by set inclusion. This forms a domain, in fact a complete lattice, with least
element 0, greatest element the set of all traces, and lubs given by unions.
For a morphism h = (f, Q): W - W', [comm]h should convert a set c of
traces over W to the set of traces over W' that "project back" via f to a trace
in c and respect the equivalence relation Q in each step. We therefore define
[comm]W = pt ((W x W) 00 ) ,
[comm](f,Q)c = {a' I map(f x f)a' E c &map(Q)a'},
where map(Q)a' indicates that each step in a' respects Q. 1t is Straightfor-
ward to check that this is indeed a functor.
4.2 Expressions
Our treatment of expressions is similar, using a slightly simpler form of trace
to reflect the assumption that expression evaluation does not cause side-
effects, but with enough structure to permit a fine-grained semantics in which
expression evaluation need not be atomic. We also allow for the possibility
that expression evaluation may be non-deterministic.
A finite trace of the form (woWt · · · Wn, v) represents an evaluation of an
expression during which the state is changed as indicated, terminating with
the result v. 1t suffices to allow such cases only when n is finite, since we
assume fair interaction between an expression and its environment: it is im-
possible for the environment to interrupt infinitely often in a finite amount
of time. On the other hand, if an expression evaluation falls to terminate the
state may be changed arbitrarily many times and no result value is obtained;
we represent such a case as an infinite trace in ww. Note in particular that
the trace w 00 represents divergence when evaluated in state w without inter-
ference.
Thus we will model the meaning of an expression of type T, in world W,
as a subset e of w+ x VT u W 00 ; this subsetwill be closed under the obvious
analogues of stuttering and mumbling. Let pt (W+ x VT u W00 ) denote the col-
lection of closed sets of expression traces, ordered by inclusion. Accordingly,
we define
[exp[T]]W = pt (W+ X VT u W00 ),
[exp[T]](f,Q)e= {(p',v) I (mapfp',v) Ee}
u {p' E W' 00 I mapfp' E e}.
whenever h': W' - W", p(h); [O']h' = [O]h'; p(h; h'). This unüormity con-
dition amounts to commutativity of the following diagram, for h: W - W' and
h':W'- W":
l l
[O]W' ----'p'-(_h)_ _ [O']W'
[Olh' [O'lh'
[O]W" [O']W"
p(h; h')
The domain [ 0 - 0'] W is ordered by
p(-) ~;;; q(-) ~ 'VW'. 'Vh:W- W'.p(h) ~;;; q(h),
the obvious parametrized version of the pointwise ordering.
The morphism part of [ 0 - 0'] is defined by:
[0- O'](h:W- W')p = ..\h':W'- W".p(h;h').
4.4 Variables
For variables we give an "object-oriented" semantics, in the style of Reynolds
and Oles. A variable of type T is a pair consisting of an "acceptor" (which
accepts a value of type T and returns a command) and an expression value.
This is modelled by:
[var[T]]W = (VT- [conun]W) X [exp[i]]W
[var[T]]h = ..\(a, e). (..\v. [comm]h(a v), [exp[T]]h e).
5 Semanlies of Phrases
A type environment rr determines a functor [ rr] as an indexed product. A
member u of [ rr] W is an environment mapping identifiers to values of the
appropriate type: ü rr(L) = 0 then u L E [O]W.
When rr f- P : 0 is a valid judgement, P denotes a natural transformation
[P] from [rr] to [0]. That is, for all environments u E [rr]W, whenever
h: W- W', [O]h([P]Wu) = [P]W'([rr]h u). This is expressed by commuta-
tivity of the following diagram for all h: W - W':
[ rr ]W ---=[=-P-=]_W_..... [ O]W
[rrlhl l[Olh
5.1 Expressions
We omit the semantic clauses for expressions, except for one representative
case to illustrate the use of expression traces. The following clause specifies
that addition is sequential and evaluates its arguments from left to right:
[E1 + E2]Wu =
HP1P2. v1 + v2) I (Plo v1) E [E1]Wu & (p2, v2) E [E2]Wu} t
u {PlP2 I (pl. VI) E [El]Wu & P2 E [E2]Wu n W00 } t
u {p E ww I p E [E1]Wu}t
5.2 skip
skip has only finite traces consisting of stuttering steps:
[skip]Wu = {(w, w) I w E w}t.
5.3 Assignment
We specify a non-atomic interpretation for assignment, in which the source
expression is evaluated first:
[X:=E]Wu =
{(map ~wp)ß I (p, v) E [E]Wu & ß E fst([X]Wu)v} t
u {map~wP I p E [E]WunW 00 }t.
5.4 Sequential composition
Sequential composition corresponds to concatenation of traces:
[P1 ;P2]Wu = {a1a2l tX1 E [P1]Wu&a2 E [P2]Wu}t.
5.5 Parallel composition
Parallel composition of commands corresponds to fair interleaving of traces.
For each set A we define the following sets:
bothA = {(a,ß, aß), (a,ß,ßa) I a, ß E A+}
oneA = {(a,€, a), (€, a, a) I a E A""}
fairmergeA = both~ · oneA u both'%,
where € represents the empty sequence and we use the obvious extension of
the concatenation operation on traces to sets of triples of traces:
to · t1 = {(aoai,ßoßiol'ol'd I (ao,ßo,)'o) E to & (aloßl,)'l) E td.
Similarly we use the obvious extensions of the Kleene iteration operators on
traces. Thus, for instance, both~ is the set of all triples obtained by concate-
nating together a finite sequence of triples from bothA. 1
1Equivalently, (airmergeA can be characterized as the greatest fixed point of the monotone
function F(t) = bothA ·tu oneA on the complete lattice :J'<A"' x A"' x A"'). The least fixed point
of this functional is the subset of triples (oc, ß, y) from (airmergeA in which either or both of oc
and ß are finite.
340 Chapter 21. The Essence of PARALLEL ALGOL
5.7 Conditional
For conditional phrases we define by induction on e,
fort E [exp[bool]]W
and PI.Pz E [O]W, an element if t then PI eise pz of [O]W.
• For e = comm, if t then PI eise P2 is
{(map ~wp)oci I (p, tt) E t & OCI E pdt u
{(map ~wp)ocz I (p,ff) E t & ocz E pz}t u
{map ~w p I p E t n ww}.
• For 9 = ( Oo - OI ), (if t then PI eise pz) (-) is the indexed family given
by
(if t then PI eise pz )(h) =
A.p.if [exp[bool]]h t then pi(h)p eise pz(h)p.
• For 9 = var[T], we define
Stephen Brookes 341
We then defi.ne
[if B then P1 eise P2]Wu =
if [B]Wu then [P1]Wu eise [P2]Wu.
Naturality is easy to check, by induction on the type.
5.9 .\-calculus
The semantic clauses for identifiers, abstraction, and application are standard:
[L]Wu = UL
5.10 Recursion
It is possible to give a least-fixed-point interpretation for recursion, but this
would only account for finite traces and would therefore preclude reason-
ing about safety and liveness properlies of programs. lnstead we make use
of greatest fixed points to obtain a model containing both finite and infinite
traces. We know from Tarski's theorem [Tar55] that every monotone function
on a complete lattice has a greatest fixed point. This might suggest that we
begin by establishing that each domain [O]W is a complete lattice. Unfortu-
nately this is not generally true. Although [comm]W is a complete lattice for
each world W, the functions [comm]h do not generally preserve tops. For
instance, when h = (f,Q):W- W',
[comm]h(tOP[comm]W) [comm]h(:P((W X W) 00 ) )
6 Relational Parametricity
Although the above semantics validates many laws of program equivalence re-
lated to locality in parallel programming, there remain equivalences for which
we can give convincing informal justification, yet which are not valid in this
model. Consider for example the following phrase:
new[int] L in (t:=O; P(t:=t + 1)),
where P is a free identifier of type comm - comm. No matter how P is instan-
tiated this should have the same effect as P(skip). As observed by O'Hearn
and Tennent, this equivalence holds for the sequential language yet is not
validated by the sequential possible-worlds semantics. O'Hearn and Tennent
344 Chapter 21. The Essence of PARALLEL ALGOL
Rl Js
w1 idw1
w1
Composition in this category of relations is defined in the obvious way, build-
ing on composition in the category of worlds.
For each type (} we define a parametric functor [ 0] from worlds to predo-
mains, i.e., a functor [0] from W toP equipped with an action on relations,
such that:
• whenever R: Wo- W1. [O]R: [O]Wo - [O]W1;
• for all W, [O]ßw = ß[e]w;
• whenever
Stephen Brookes 345
[81Rj j[eJs
[O]W1 -[-O]-h-
1 -+ [O]X1
by which we mean that
(do, di) E [O]R => ([O]hodo, [O]h1dd E [O]S.
The first two conditions above say that [ 0] constitutes a "relator" [MS93,
AJ91]. The last condition is a parametricity constraint.
Each well-typed phrase denotes a parametric natural transformation [P]
between the parametric functors [rr] and [0], i.e., a natural transforma-
tion obeying the following parametricity constraints: whenever R: Wo - W1o
(uo, ui) E [rr]R => ([P]Wouo, [P]W1ud E [O]R, as expressed by the fol-
lowing diagram:
[rr]Wo [P]Wo [O]Wo
[rrJRI j[O]R
[rr]W1 -[P_]_W-1-+ [O]W1
Parametrie natural transformations compose in the usual pointwise man-
ner. The category having all parametric functors from W to P as ob-
jects, and all parametric natural transformations as morphisms, is cartesian
closed [OT95] 2 •
To adapt these ideas to the parallel setting, we must give trace-theoretic
interpretations to types comm, var[T], and exp[T]. (Here we will cover
only comm.) We define [comm]W and [comm]h as before. To de-
fine [comm]R: [comm]Wo- [comm]W1 when R: Wo - W1o we first Iet
map(R)!;;;; Wö x Wi be the obvious extension of R to sequences of the same
length; we then define (co, c1) E [comm]R iff, for all (po, Pl) E map(R),
(V' oco E co. map fst oco = Po =>
3ocl E c1.map fst oc1 = P1 & (map snd oc 0 , map snd oci) E map(R))
& (V' oc1 E c1. map fst oc1 = P1 =>
3oco E co.map fst oco =Po & (map snd oco, map snd oci) E map(R))
This is intended to capture the following intuition: [comm]R relates two com-
mand meanings iff, whenever started in states related by R and interrupted in
related ways, the commands respond in related ways.
2 Technically, we actually work with the full subcategory consisting of the functors F such that:
for all W, FW is a domain; and for all h: W - W', Fh is strict. This subcategory is also cartesian
closed.
346 Chapter 21. The Essence of PARAllEL ALGOL
and
new[int] x in
(x:=O; P(x:=x + 2);
if even(x) then diverge eise skip),
where diverge is a divergent command, are equivalent in sequential ALGOL but
not in PARALLEL ALGOL. For example, if P is ,\c. c II c, then the first phrase has
an execution in which each argument thread reads x as 0, then each sets x to 1,
and then the two final increments occur sequentially, leaving x with the value
3, causing termination; the other phrase, however, must diverge. The relation
(w, (w', z)) E R = w = w' & even(z) works for the sequential model but
not for the parallel.
Indeed, in sequential ALGOL, the phrase
new[int] x in
(x:=O; P(x:=x + 2);
if even(x) then diverge eise skip)
discussed above is equivalent to diverge. For PARALLEL ALGOL, this equiva-
lence falls because our semantics "observes" intermediate states during exe-
cution. Instead the phrase is equivalent to P(skip);diverge.
In the O'Hearn-Tennent model
if x = 0 then f(x) eise 1
and
if x = 0 then f(O) eise 1
fall to be semantically equivalent, because the model includes procedure
meanings that violate the irreversibility of state change [OT95], yet the phrases
behave identically in all sequential contexts. In contrast the equivalence
should (and does) fall in our parallel model, because expression evaluation
may not be atomic. For example, if f is ,\y. y and the phrase is evaluated in
parallel with a command that may change the value of x from 0 to 2, the first
case might yield the result 2.
7 Conclusions
Our semantics inherit both the advantages and limitations of the correspond-
ing sequential models and of the trace model for the simple shared-variable
language. At ground type comm we retain the analogue of the full abstraction
properties of [Bro93]: two commands have the same meaning if and only if
they may be interchanged in all contexts without affecting the behavior of the
overall program. The extra discriminatory power provided by the ,\-calculus
facilities does not affect this. However, like their sequential forebears, our
models still include procedure values that violate the irreversibility of state
change [OR95], preventing full abstraction at higher types. Recent work of
Reddy [Red93], O'Heam and Reynolds [OR95] incorporating ideas from linear
logic appears to handle irreversibility for sequential ALGOL; we conjecture that
similar ideas may also work for the parallellanguage, with suitable generaliza-
tion.
348 Chapter 21. The Essence of PARAllEL ALGOL
Acknowledgements
Much of this work was developed during a three-month visit Uuly-September 1995)
to the Isaac Newton Institute for the Mathematical Sciences, Cambridge, England, as
part of a research programme on Semantics of Computation. Thanks to Peter O'Hearn,
John Reynolds, Edmund Robinson, Pino Rosolini, Philip Scott, Bob Tennent, and Glynn
Winskel for helpful discussions and comments.
This work was sponsored in part by the Office of Naval Research, under Grant No.
N00014-95-1-0567, andin part by the National Science Foundation, under Grant No.
CCR-9412980.
References
[AJ91] S. Abramsky and T. P. Jensen. A relational approach to strictness analysis for
higher-order polymorphic functions. In Conf. Record 18th ACM Symposium
on Principles of Programming Languages, pages 49-54. ACM Press, 1991.
[Bro93] S. Brookes. Full abstraction for a shared-variable parallellanguage. In Proc.
ffh AnnualiEEE Symposium on Logic in Computer Science, pages 98-109.
IEEE Computer Society Press, June 1993.
[HMT83] j. Y. Halpern, A. R. Meyer, and B. A. Trakhtenbrot. The semantics of local
storage, or What makes the free Iist free? In ACM Symposium on Principles
of Programming Languages, pages 245-257, 1983.
[MS93] j. C. Mitchell and A. Scedrov. Notes on sconing and relators. In E. Boerger,
editor, Computer Science Logic '92, Selected Papers, volume 702 of Lecture
Notes in Computer Science, pages 352-378. Springer-Verlag, 1993.
[Ole82] F. J. Oles. A Category-Theoretic Approach to the Semantics of Programming
Languages. PhD thesis, Syracuse University, 1982. See Chapter 11.
[OR95] P. W. O'Hearn and j.C. Reynolds. From ALGOL to Polymorphie Linear Lambda-
calculus. Talk given at Isaac Newton Institute for Mathematical Sciences,
Cambridge, August, 1995.
[OT95] P. W. O'Hearn and R. D. Tennent. Parametricity and local variables. ]. ACM,
42(3):658-709, May 1995. See Chapter 16.
[Par79] D. Park. On the semantics of fair parallelism. In D. Bjomer, editor, Ab-
stract Software Speci{ications, volume 86 of Lecture Notes in Computer Sci-
ence, pages 504-526. Springer-Verlag, 1979.
[Red93] U. S. Reddy. Global state considered unnecessary: object-based semantics
of interference-free imperative programming. In ACM SIGPLAN Workshop
on State in Programming Languages. Technical Report YALEU/DCS/RR-968,
Department of Computer Science, Yale University, 1993. See Chapter 19.
[Rey81] J. C. Reynolds. The essence of ALGOL In Algorithmic Languages, pages 345-
372. North-Holland, Amsterdam, 1981. See Chapter 3.
[Rey83] J. C. Reynolds. Types, abstraction, and parametric polymorphism. In Infor-
mation Processing 83, pages 513-523. North-Holland, Amsterdam, 1983.
[Tar55] A. Tarski. A lattice-theoretical fixpoint theorem and its applications. Paci{ic
Journal o( Mathematics, 5, 1955.
Contents of Volume 1
Contributors vü
Introduction 1