Professional Documents
Culture Documents
I. Introduction
All programming languages have basic types; some have many, some have few,
some have only one basic data type. The most commonly used are: booleans,
integers, reals, procedures, labels, atoms, lists. They generally come equipped
with some operations: logical operations for booleans, arithmetical operations
for integers and reals, composition, evaluation and abstraction for procedures, a
defining facility for labels, and list-manipulation primitives for lists.
*This research was conducted at the University of Warwick while both authors were supported
by the Science Research Council grant B/RG 31948 to D. Park and M. Paterson. During the final
redaction of the paper the first author was partially supported by the National Science Foundation
grant MCS78-0746I.
+EDITOR'S NOTE: This paper is one of several invited for submission to this journal to
present different approaches to the subject of the semantics of programming languages.
97
0025//5661//81//0014-0097504.40
© 1981 Springer-Verlag New York Inc.
98 D.J. Lehmann and M. B. Smyth
In most data types the user has the facility to denote new objects by the use
of expressions combining old objects by operations. All these facilities are easy
to understand. The first nontrivial fact about data types is that, in certain data
types and in certain programming languages, objects can be defined implicitly;
they are defined by expressions that contain their own denotation. This is the
facility which is generally referred to as "recursive definition" but which we
prefer to call "circular definition". This facility is generally offered for proce-
dural data types only.
In the most advanced languages (called extensible) the user may define new
data types from old ones by the use of constructors. In A L G O L 68, for example,
these constructors are struct, proc, ref, row, union. New operations on data types
may also be defined by defining new procedures.
In some languages, such as A L G O L 68, new data types may also be defined
circularly. The following two definitions are examples of such circular defini-
tions in A L G O L 68:
mode tree = struct (int label, ref tree left, ref tree right)
mode fun = proc (fun) fun.
Almost all languages known to the authors that allow such circular defini-
tions of data types put stringent restrictions on the generality of such a facility.
An important exception is the language M L developed recently at Edinburgh:
Gordon et al. [13]. These restrictions have at least two drawbacks. They
complicate the definition of the language for no good semantic reason and they
keep the user from defining safer data types. Hoare [18], for example, showed
why the following definition: mode htree = struct (int label, htree left, htree right)
is less error prone than the definition for tree found above. Both points are
taken in some detail in Lehmann [23].
The question of the mathematical meaning of circular definitions inside
procedural data types was first answered, independently, by H. Bekic, D. Park
and D. Scott, who noticed that the functions defined were least fixpoints of
monotone functionals. The fact that mode-constructors are functors was men-
tioned in Scott [35], in a remark attributed to Lawvere, and later made explicit
by Reynolds and Wand [40]. Circular definitions of sets and languages had been
known to algebraists for a certain time when, in 1969, Scott gave a precise
meaning to circular definitions involving the function-space constructor (his
arrow, the A L G O L 68 proc). A general method to solve domain equations,
implicit in [35], was made explicit by Reynolds. The categorical nature of this
unified construction, only hinted at by Scott, was emphasized by Wand [40].
Abstract versions of this construction appear in Wand [41] and Plotkin and
Smyth [32]. The main idea behind the present work is that the problems involved
in defining data types can best be handled by an exact generalization of the
well-understood methods used in studying definitions of objects within a data
type. This involves generalizing from posets to categories, from monotone
functions to functors, and from least fixpoints of continuous functions to initial
fixpoints of continuous functors. The ADJ group concentrated on the problem
of defining functions on data types and insisted that data types do not consist
only of a set of elements, structured in some way (generally a partial order) but
Algebraic Specificationof Data Types 99
2. Mathematics
This section will introduce the basic notions and notations to be used in the
sequel.
~: T , ~ S s".
The function q~n associates with each symbol of arity n a map from S n into S.
The following notions of category theory will be assumed to be known:
category, object, arrow, domain, codomain, identity, composition, small cate-
gories, limits, colimits, products, coproducts, equalizers, coequalizers, monics,
epis, isomorphisms, initial and terminal objects, zero object, functors, cosepara-
tors, well powered categories. The reader is referred to MacLane [26] and
Herrlich-Strecker [!7] for the definition of those notions.
Definition 2.3. The category whose objects are the natural numbers:
{0, 1,2 ..... i ..... } and whose arrows are all couples (i, j ) of natural numbers
such that i <j, with the obvious identities and composition will be denoted by o~.
Definition 2.4. C is an o~-category iff C has an initial object and all o~-diagrams
have a colimit.
100 D.J. Lehmarm and M. B. Smyth
cCTc
,x $ ~ ~ T,~
d ~ Td
Proof The proof of this theorem is more or less routine arrow-chasing. As the
existence of o~-colimits will not be used in the sequel its proof will be left to the
reader. The existence of an initial T-algebra will be proved in detail. Let _1_ be
the initial object of C (its existence is ensured because C is an m-category) and
let _1_c be the unique arrow: _1_--~c.
Algebraic Specification of Data Types 101
a y c
Ta ~ Tc
Ty
Remark. From Theorem 2.1 we shall only use the existence of initial T-algebras
and the careful reader may have noticed that we did not prove the most general
possible result. Initial algebras may be proved to exist even in categories in
which not all to-diagrams have a colimit; it is enough to suppose that all
to-diagrams in a specified subcategory have colimits (in the large category), that
the initial element is in the subcategory and is initial in the subcategory and that
the subcategory is closed under T. These results may be of use when studying
certain categories which are not to-categories, but the simple version restricted to
to-categories is adequate for the purposes of this paper.
The next theorem is found in Arbib [4] and in Barr [7], where it is credited
to Lambek.
Theorem 2.3. Let T be a similarity type; then the associated functor T: Set--oSet
is an co-functor.
The proof is obvious. Theorems 2.1 and 2.3 then imply the existence of
initial algebras of any type and the fact that the initial algebra, as a function, is
an isomorphism. One knows that, in Set, not only initial algebras but also
arbitrary free algebras exist and also free algebras in arbitrary equational classes
of algebras, but this is of no interest to us. The question of equational classes of
algebras is the subject of an upcoming paper by the first author. The framework
of universal algebras is too restricted for data types and, as noticed by ADJ,
many-sorted algebras seem more suited.
n times
Set" is obviously cocomplete (and also complete) and so is an ~o-category. If
T: S e t " ~ S e t " the T-algebras are a generalization of what is called in the
literature many-sorted algebras, heterogenous algebras or algebras with a scheme
of operators. Theorems 2.1 and 2.2 imply the existence of free many-sorted
algebras, which is Proposition 2.1 of ADJ [3]. The present approach is more
general than the classical approach and provides a notation which is simpler to
handle than the diverse but cumbersome notations used by Birkhoff and Lipson
[8] or ADJ [3].
Many sorted algebras are closer than algebras to what one understands data
types shoud be, nevertheless the problems of circular definitions of objects
inside a data type cannot be tackled in Set" for lack of an order structure on the
objects (which are n-tuples of unordered sets). Burge [9] is probably the best,
though somewhat informal, account of what can be done with sets. The next
example is central to this paper.
cocomplete. We shall briefly pause here to prove this result for CPO*. The same
method would yield Markowsky's and Meseguer's results. The method used here
makes full use of general results and is a definite improvement on Markowsky's
and Meseguer's.
Cocompleteness Theorem. C P O * / s complete and cocorr~lete.
Proof Suppose f, g are two different arrows: A---~B. Then there exists an a 0 EA
such that f(ao)vag(ao) and by symmetry we may suppose f ( a o ) ~ g ( a o ) . Let h:
B---)2 be defined by
h ( b ) - { _1_ i f b E__g(ao)
T else
Y~-algebra for any ranked alphabet, which is the main result of ADJ [3].
Obviously the same holds for many-sorted continuous algebras, which are
to-functors in (CPO*) n. Categories slightly different from CPO*, for example
that of to-complete cpo's and to-continuous maps or A-complete cpo's and
A-continuous maps are, by similar proofs, seen to be complete and cocomplete
and the many initiality results of ADJ [3] can be obtained in a unified way. The
category whose objects are countably based algebraic posets with least elements
and whose arrows are bottom-preserving ~0-continuous maps which preserve
finite elements, inspired from Courcelle and Nivat [10] is also cocomplete (and
complete) and hence an to-category. This last result can be proved either by the
method used above or by noticing that the category is equivalent to that of
partial orders with a least element and strict monotone functions which is very
easily studied. The relation between Courcelle and Nivat [10] and the present
work will be studied in an upcoming paper by the first author.
In all preceding examples Theorem 1 guarantees the existence of a free
T-algebra over an arbitrary object X: it is the initial T'-algebra for T' = T + K x
where K x is the functor "constantly X." Our construction is then a special case
of Adamek's [1]. Our insistence that T be an to-functor guarantees that the free
T-algebras are obtained as colimits of to-diagrams. ADJ noticed that expressions
with variables represented objects in such free T-algebras and made totally clear
the way such objects yield maps from the environment to the obvious domain.
In the sequel we shall admit without further formalities that expressions built
out of constants, variables and (continuous) functions yield (continuous) func-
tions or more generally (continuous) functionals. CPO* is the central category
of interest but certain domain equations (those involving ~ ) cannot be solved in
CPO* and have to be solved in a subcategory which is the next example.
other is closed only under Smyth's P0 (Smyth [38]). Smyth [37] has defined a
subcategory of CPO-PR containing as objects only the effectively given continu-
ous consistently complete cpo's which is closed under ×, +, ~ and Po, and
certain w-colimits. Unlike all previous examples CPO-PR does not possess
products or coproducts and Adamek's [1] result about free algebras is inapplica-
ble. Yet by Theorem 1 initial T-algebras exist for arbitrary o.,-functors T. Now
some obvious lemmas will be stated with only hints of proof or without proof.
f L ( - L ~) = I B and fR(_I_ B) = _k A.
l_~nlma 6. If fL: A---~B and f n : B--~A are monotone functions such that f n is
w-continuous, f R o f L = 1a and f L o f n E I B, then f L is w-continuous.
The following lemmas deal wii-h t0-colimits in CPO-PR. Since the first
version of this paper, Plotkin and Smyth [32] proved these in the abstract setting
of O-categories. The proofs will therefore be skipped here.
Lemma 9. Let I' be an to-diagram in CPO-PR and bt: I'---~Ao~ be a cone such that
II iftiL ft R__
i --1A= ' then i~Li is a colimiting cone in CPO* for F L (the cone consisting of
the left parts of the arrows of F).
Lemma 10. With the same hypotheses as in L e m m a 9, pin is a limiting cone for the
to°P-diagram F R (the cone consisting of the right parts of the arrows of F).
Proof. Let F be an to-diagram on CPO-PR and l,: F--->B a colimiting cone (in
CPO-PR). By Lemma 8, 1,~p~_E~'i+/~I~'i+R1 and IliPiLl,~=I. But Tvi=T~,i+loTfi
and (Tpi)L(Tpi) R -~ ( T P i + l ) L ° (Tfz.) L o (Tfi) a o ( T P i + l ) R l [ ( T P i + l ) L ( T P i + l ) R. T h e
hypothesis then implies that tl i(Tp~)L(Tri) R ---1 and T~ is a colimiting cone by
Lemmas 9 and 11. []
A similar theorem may be formulated for bi-functors of type: CPO-PRX
C P O - P R ~ C P O - P R (the colimits in CPO-PR × CPO-PR are taken component-
wise).
CPO-PR can be considered as a (nonfull) subcategory of CPO*: if T':
C P O ~ C P O * preserves projection pairs its restriction to CPO-PR is a functor
T: CPO-PR--->CPO-PR. The following theorem shows that the initial T-algebra
is also an initial T'-algebra. This is the most useful connection between CPO-PR
and CPO*. Loosely speaking it shows that the initial E-algebra in CPO-PR is
also the initial E-algebra in CPO* and links our method which uses CPO-PR to
that of ADJ (75) which uses CPO*.
3.1 Definitions
For the sake of greater definiteness in the discussion, we will begin this section
by offering a precise definition of "data type." The definition probably errs on
the side of being too inclusive, that is, of covering many things which cannot
reasonably be considered as data types.
In the following definitions, C is to be understood as an appropriate
category of domains, which for the purposes of this paper means either CPO* or
CPO-PR.
Definition 3.1. A type (or signature) t consists of a natural number n > 0 and n
pairs of functors (Si, Ti), S i, T,: C--->C, i = 1..... n.
Note that even when we consider CPO-PR to be our base category and the
functors S~, Ti to be functors on CPO-PR, we do not ask that the ~'i's be arrows
of CPO-PR.
In the usual applications the functors S~ and T,. are "polynomials" (built up
from constant and identity functors by product and sum only). In these cases
our definitions are fairly close to the customary definitions in terms of domains
with operations or many-sorted (continuous) algebras.
The operations of a data type are, it seems, always strict functions. Thus we
may as well describe the ~i (Def. 2) as arrows of CPO*; likewise for f in the
following definition.
SiO 1--->TiD l
tt ff
Fig. 1. .L
i10 D.J. Lehmannand M. B. Smyth
There is no real problem in defining the basic data types of the usual program-
ming languages as objects of CPO* (CPO-PR) equipped with certain operations
- - a n d hence as data types in the sense of Definition 3.2. This may be illustrated
by the definition of Bool sketched above.
Because of the generality of circular definitions of data types and the
extended facilities available to define functions on data types, we think that it is
possible (though we in no way suggest that it should be the case in programming
languages) to use only a very few basic data types: say, the one-point domain 1
and the two-point domain 2 ( = 1±).
3.3 Constructors
This paragraph, and the next one, proceed entirely by examples. Their purpose
is to show that many usual data types are indeed circularly, and that circular
definitions define not only a certain domain but also certain operations on it.
1. Simple data types. These are the data types built from basic data types by
type constructors and circular definitions.
112 D.J. Lehmann and M. B. Smyth
Natural Numbers
In the preceding section the natural numbers were defined circularly as an initial
T-algebra for a functor T on Set. For computer science purposes it seems
preferable to define them as an initial T-algebra for a functor T on CPO-PR (or
CPO*). By analogy with the case of Set one could think of using T' = 1 + Icv~.pR.
This does not give a satisfactory solution: the initial T'-algebra is 1 (remember
that + is the coalesced sum). Scott has proposed, for the natural numbers, what
amounts to the initial T-algebra for T---K z + Icpo.P1t.
One may verify that the initial T-algebra ~1: 2+N±--->N± is such that
N± = { 1 , 0 , 1,2 ..... n,... } with _1_E n and n E n for any n ~ N±, those being the
only ordered pairs. See Fig. 2. And 7 is such that 7(2.) = _1_, B(-V)=-0 and
7 ( n ) = n + 1. By Theorem 2.2, 7/ is an isomorphism and the data type natural
numbers comes equipped with two arrows: 7: 2 + N± --->N± and 77- ]: N± --->2+ N±.
To convince the reader that 7 and 7 - i are sufficient to define all usual
arithmetic functions, just notice that 0: 2--->N± may be defined by 0 = 7 oil, suc:
N±--->N± by suc=~7oi2, null: N±--->Bool by n u l l = d o 7 -1, pred: N±--,N± by
pred( n ) = if d( 7 - l( n )) then O( _1_) else 7 - 1(n ). All recursive functions may then be
defined by circular definitions. In particular, a primitive recursive definition:
f(O, n)---h(n), f ( m + 1, n ) = g ( m , n, f ( m , n)) may be translated by: f ( m , n)---if
null(m) then h(n) else g'(m, n,f(pred(m), n)). It must be noticed that both
functions 7 and 7 - l must be given; ?7 alone does not allow the definition of null
and pred, in the absence of the equality predicate which we have no reason to
suppose given or even computable. Once pred and null are given, equality may
be defined either by the usual primitive recursive definition or by:
eq(m, n ) = i f null(m) then null(n) else if null(n) then
false else eq(pre(m),pred(n)).
The necessity of considering both ~ and 77-1 as given also appears, though in
a very different formalism, in de Roever [33].
The choice of the functor T=Kz+IcPo.pa ( K z is the functor constantly
2 = 1± ) may seem arbitrary. One may notice that it is the endo-functor CPO*--->
CPO* which corresponds to the functor S = K l+Is~ t used in the example 6 of
section 2, in the sense that To A b s = A b s o S. (That Abs: Set--->CPO* is a left
adjoint for the forgetful functor CPO*--->Set explains its importance). One may
O 1 2 . . . . n... T 0 2. n
N .t
2÷N i
Fig. 2.
Algebraic Specification of Data Types 113
also describe ~1 as defining the initial continuous algebra with one constant and
one strict unary operation: indeed in N±, _1_ is a solution of the equation
x=suc(x).
Another candidate for the data type natural number would be the initial
continuous algebra with one constant and one unary operation (not necessarily
strict). This is the initial T'-algebra for T'=K2+Ab. If A is an object, T'A = 1±
+ A ± which is the separated sum of I and A. The initial algebra ~7': T'N'---~N'
looks the following way: see Fig. 3. with ~?(_I_)=A_o,~(T)=0,~?(A_~)=
I ~+1, 7 / ( n ) = n + 1, ~1(_1_~o)= ± ~o. The first version N± we proposed is simpler
but the second one, N', shows how infinite objects such as ±oo may enter the
initial algebra. In N' the least solution of the equation x--- suc (x) is _k oo.
One may similarly see that the initial continuous algebra with one constant
and one binary (o~-continuous) function is given by the initial solution to the
equation: W - 1 ± + ( W × W ) ± . The initial continuous algebra with one constant
and one strict (bistrict or leftstrict) function is given by the initial solution to the
equation: W ~ 1 ± + W× W ( W ~ 1 ± + W® W, W-~ 1 ± + W® W_L respectively)
.I.o~
II
/o<~
Zll
N.
F~o 3.
114 D.J. Lehmann and M. B. Smyth
Stacks
The data type Stack A, whenever A is a domain, is the initial T-algebra for
T= K2+ KA®IcPo.PR. Note that we use the coalesced sums and products. It is
easy to check that the initial T-algebra, cs: 2 +A ®S--->S is the following.
S is the set of all finite sequences of elements of A different from _I_A,
ordered coordinatewise, and one bottom element _1_. cs is defined by: cs(_l_)-
_l_,cs(T)=( ) the empty sequence, cs(a, s ) = ( a , s) the concatenation of a and s,
if s:/: _1_, and a:/: _1_A" Clearly
It does not bother us that pop and top are only partial functions: everything
may be done in terms of cs - l . The above way of defining stacks may be
compared with ADJ [2] where stacks are defined as initial equational algebras.
Our approach answers three criticisms that could be made on ADJ's: It deals
with partial orders and not only sets, there is no need to introduce some special
object of type A to be top (A), and most important the equations laid down by
ADJ may be justified and shown to be sufficient to characterize stacks. Our
approach is also much closer to what programming languages do allow: circular
mode definitions are allowed in A L G O L 68 whereas no programming language
allows the definition of an initial equational algebra, though efforts are being
made in that direction. It also seems that functional data types are not definable
equationally and that the usual algebras whose specifications are very natural in
the equational framework, such as groups and rings, are not very useful for
programming.
The use of the coalesced product (®) instead of the usual one ( × ) enables
us to avoid the introduction of infinite stacks.
An equation similar to the previous one defines a data type which contains
infinite stacks. The initial solution of S---2 +A × S is a good candidate for the
data type "finite and infinite stacks." There, push (_1_a, s)4: I s
If, on the other hand, one wants push (_l_A,s)=_l_ s one should use
S-~2+A®S±. All three data types just presented should be clearly dis-
tinguished.
Lists
It is only in the next section that it will be shown that Stack is a mode-constructor
and that the above definition will be given its strict meaning.
Algebraic Specification of Data Types 115
Lisp-Lists
Lists in LISP may be informally defined by: a list is either empty or the
concatenation of a head and a tail, the head being either an atom or a list, the
tail being always a list. This suggests the following definition: LispA-----2+ ( A +
Lisp~)®LispA. Let T be the functor K2+(K4+lo, o.nR)@lcr,~p n and cl:
TL--->L the initial T-algebra. LispA does indeed, as the reader may care to check,
consist of what one could expect: one bottom dement and all finite nonempty
binary trees the fight leaves of which are unlabdled (they represent empty lists)
and the left leaves of which are either unlabelled or labelled by a nonbottom
element of A. If the tree consists of only one leaf it has to be considered a right
leaf. Examples are described in Fig. 4. Then cl: 2 + (A + LispA)®LispA--->LispA
is such that
Infinite Lisp-Lists
Friedman and Wise [11], Henderson and Morris [16] have found it useful to
consider infinite lists. The data type "finite and infinite lisp-lists" is better
defined as the initial solution of the equation I n - - 2 + ( A + I n ) × In.
/\. [[ ]]
/\ I.J
a
/\
[.t, [0]-- [['1°J
Fig. 4. •
/\ ix. im
116 D.J. Lehmann and M. B. Smyth
Binary trees
A labelled tree consists of a labelled node and a (possibly empty) forest. A forest
is either empty or consists of a tree and a forest. The two data types are defined
simultaneously by:
T-~ A ® F
F~2+T®F
T ~ A ® Stack T
F----- Stack T
2. Composite data types. Many data types are not simply built up from basic
data types with the help of type constructors and circular definitions but
defined, somewhat indirectly, by first defining a simple data type and then some
new functions on this data type and possibly forgetting some of the old
functions. The carrier of a composite data type will be the same as that of the
simple data type from which it is built but the operations will be different. Two
examples will be analysed here: Queues and Arrays.
Queues
Queues are of the same type as Stacks but the push and pop procedures interact
in a different way in the sense that the element popped will be the one which has
been pushed on first (and not the last one as in Stacks). The obvious idea is to
define Queues from Stacks by defining new pop and top functions and then
forgetting about the old ones. Stacks of A were defined as two inverse functions:
cs and dq are not inverses but properties may be proved about them as will be
seen in Section 5 when a characterization of Queues as an initial algebra in a
certain equational class will be proved. Let us define:
Arrays
Infinite one-dimensional arrays of A may be defined from the data type [N--->A]
of functions from the natural numbers to A. Arrays must be equipped with two
functions: access: Arr×N-->A and update: Arr×N×A--->Arr. The function
access may easily be seen to be the function eval. The function update is defined
by Currying and use of abstraction by:
In this section we will show that the transformation which sends an ~,-functor to
its initial fixpoint can itself be defined as an ~-functor. This will in turn be used
to show that other important constructs, notably data-types with parameters, are
oMunctors. As another application, we show that the well-known "reduction of
simultaneous recursion to iterated recursion" (de Bakker [6]) generalizes to
~-categories. This provides a useful technique for demonstrating the equivalence
of data types, as we shall see in Section 6.
We begin with some considerations on functor categories. The following
notation is adopted: C is an ~-category, while A, B are arbitrary categories.
[B~C] is the category with objects the a~-functors from B to C and arrows the
natural transformations (with "vertical" composition) between such functors.
118 D . J . L e h m a n n a n d M. B. S m y t h
Sketch of proof The initial object of [ A ~ C ] is the constant functor with value
I c. CA is an *o-category in which limits and colimits are computed pointwise
"r0 "r1
([26] Ch. V, 3). We seek to show that, if T = Fo ~ F 1 ---> • -- is an *o-chain in
[ A ~ C ] ; then the colimiting cone O: T ~ F of T in C A is also the colimit in
go
[A--->C]; in other words, that F i s *o-continuous. To see this, let F = A o ---> Al--->- • •
be any ,0-chain in A, with colimit ~: F---~Ao~, and let D be the infinite two-
dimensional diagram with rows F,T ( i = 0 , 1.... ) and with vertical arrows ('ri),4i:
F~Aj~F~+IA j. D commutes by naturality of the % Taking the colimit of D first
by rows and then by columns we find that F~# is the colimit of F~oF. []
A more detailed proof of this lemma may be found in Lehmann [22].
We will define the initial fixpoint operator, Y: [C~C]--->C, in terms of the
Lim Lim
colimit functor ~ : C~---~C. We recall ([26] Ch. IV, 2) that ~ may be defined
as the left adjoint of the diagonal functor A: C--~C ~'. (Strictly sPeaking, this is
not a proper definition, since adjoints are not uniquely determined. We have to
suppose that a particular adjunction is chosen once and for all.)
Proof. To show that S(z) is a natural transformation, we must show that the
i
diagram (note: ~-± means (i"i)±)
$ ~$ ~$ .... (1)
commutes. The 0 th (leftmost) square of (1) commutes trivially. Suppose that the
F/th square commutes. Then
F F " A_ Fff-~fFFn+l .L
FG ~ _L F~-~gFG~+ I _L
GG'~g
GG ~ l -~ GG ~*l ±
Algebraic Specification of Data Types 119
c o m m u t e s - - t h e upper half by applying F to the n th square, the lower half since ~"
is natural. But this means that the n + l th square of (1) commutes, since
~'~+l=~'Gkj_ oF~"k, ( k = n , n+ 1). Thus (1) commutes. That S preserves identities
is trivial, and that it preserves composition is an immediate consequence of the
interchange law. []
zo ~6
Lemma 3. Suppose that T = F o --~ F1---~.... T'=F~--~ F~---~... are co-chains in
[C--~C], with colimit cones o:T---~Foo,o:T'---~F~. Then the co-chain T " =
F~Fo ~ F~F1--*"" has the eolimit o"'. T"---~F~F~, where o, - o n*on.'
Proof Consider the infinite diagram of Fig. 5. Since the F,~ are co-continuous,
the colimits of the rows are as indicated. The colimit of the right-hand column is
constituted by the arrows ~,;F~: F~F=~F~F=, n = 0 , 1..... Hence the colimit of
the diagonal, computed by rows, is constituted by the arrows (a~Fo~) o (F~'a,)=
o~*o~ []
Lim
Definition 4.1. Y: [ C ~ C ] ~ C is ~ o S, where S is as defined in Lemma 1.
Lim
Proof ~, as a left adjoint, preserves all colimits. The rest of the proof is
TO ~'1
concerned with showing that S is o~-continuous. Let • = Fo ~ F l ~ . . - be an
w-chain in [C~C] with colimit o: qo~F~. The translation of o by S is the
infinite diagram of Fig. 6. By Lemma 3, together with the fact that colimits in
[ C ~ C ] are computed pointwise, the colimits of the rows are as indicated in the
diagram. Let us prove that the arrows F~f= mediate between the successive
cones, as indicated. In the first place, foo (the unique arrow from _1_ to Foo _1_)
mediates trivially. Now, for any n/> 1, k/> 0, the diagram of Fig. 7. commutes.
F~,
FoFo F°t°- FiFT F°tl- F~F, Iif..~ ~ F~F=
-TiF' FI"[, Fio', l -TiF~
_z' FIF1
Fig. 5.
120 D. J. Lehmann and M. B. Smyth
,L
fo fl f=
(O_k)'
(zo), (z_d, J --.,~F~.L
Fo = Fli Fkl
Fig. 6.
n
F;. t-°'kl" --- F"~
F~fk F"o+fk
n F Pn
FFI = F (YF)
F (YT)
T
YF'
t F'nL n'l
F'F' F'/J'n F' (YF')
F'
YF'
Fig. S.
122 D.J. Lehmann and M. B. Smyth
( A b s t ( z ) x ) r = "r<x,v >.
X ~ F(X,Z)
z -~ G ( x , z)
can be solved by first solving for Z in terms of X from the second equation, then
substituting in the first equation.
- -- ~G2
. ~ - F ( X , Z) Z <-- G(X, Z)
a~ ~L.F(a,13) C0) fl,~ ~/G(a,fl) (4)
X' +--F(X', Z') Z'--~ G(X', Z')
P q
Let ~" be the arrow from r/a. to the Gx,-algebra q: Gx,(Z')--,,Z'. Using (3), the
following commutes:
"OG~
z~ ~ G : z )
Za~, ~G(a, lz,)oG(I2, Z) = G(e~,Za)
rig x,
Zx, ~ Gx,(Z~,) (5)
~ ~G,,,(~) = G(Ix,,~)
z ' q Gx,(Z')
~G~ ~G~
Z <-- G~(Z)
,/3.1, ,~G (I2, fl) = G 2(fl) and (6)
z' ,-- G~( Z') Z' ,-- G~( Z')
q o G(a, 1~,) q oG(,~,1~,)
_ P
x ,-- F ( X ) = F ( 2 , Z~)
~$ SP(~) = e(~, zo) (7)
x' ,-- ? ( x ' ) = ~ ( x ' , Zx,)
pF(&,, ~;)
)t-notation for functors. In fact, we use this notation only in very restricted
contexts, where the interpretation is straightforward. The expression )~Z.G(X, Z),
for example, may be understood as follows: for any assignment of a value
(object of C) X 0 to X, )tZ.G(X, Z) denotes the functor H: C--->C defined on
objects by H ( Z ) = G ( X o, Z) and on arrows by H ( f ) = G ( l x o , f). To ensure that
this always defines an o>functor we need the (trivial) lemma that an c0-continuous
124 D.J. Lehmann and M. B. Smyth
X~Z
e(x,z)
Theorem 2 assures us that the "naive" solutions are correct. In detail: Gx is here
F(X, -), Z x is Y(XZ.F(X, Z)), and (X, Z ) =(Y(XX.Y(AZ.F(X, Z))), Zg) is
the carrier (codomain) of an initial solution. Then, interchanging the roles of
X, Z, we have, in a notation symmetrical to that used previously, whose
interpretation should be clear): Fz is AX.Z, X z is Z, G* is AZ.F(Z, Z), and
(X*, Z*)=(Y(AZ.F(Z, Z)), Y(AZ.F(Z, Z))) is the carrier of an initial solu-
tion. Since all initial algebras for (F, G ) are isomorphic, the result follows. []
Proof Consider the pair Of equations by which we define trees and forests
(Section 3):
T-~ A ® F
F~-- 2 + T ® F (8)
F~ 2+(A®F)®F.
X~A+Z
Z~2+X®Z.
Finally, let us note that it is interesting to solve (8) in the opposite order to
that considered in the proof of Corollary 2. The result is the ForA may be taken
as Stack(TreeA), where TreeA is the initial solution of
T ~ A × Stack T. []
5.1 Definitions
data type, the infinite binary tree of Fig. 9. can be defined by:
f°BF=goFf (1)
f = go Ffo~f I (2)
1 0 ~.9.
Algebraic Specification of Data Types 127
One of the themes of this work is the replacement of the usual set-theoretic style
of reasoning about data types by "diagram-chasing". An important case in point
is provided by the principle of structural induction for various data types. In this
paper we have adhered to the policy of eliminating structural induction in
favour of direct use of initiality: Theorem 4.2 and its applications provide
perhaps the best illustration. The purpose of this subsection is to provide a
general result showing that such an elimination can always be carried out. To
state this result we need an abstract (category-theoretic) formulation of induc-
tion. We work with the representation of (sub)sets as monomorphisms (rather
than as characteristic functions--as we did in the unsatisfactory treatment given
in the original version of this paper). The improved treatment given here owes
much to Plotkin.
We begin by looking at induction on the natural numbers. N is defined as
the initial solution of
x ~ 1 +x(= TX).
More precisely, the solution is q~: I+N--->N, where ~ may be considered as
combining the zero and successor operations (Section 2, Example 6. We still
suppose that we are working in Set.) "mathematical induction" may be ex-
pressed as:
I+M M
Ida, ~m Sm (4)
I+N ~-~N
factors through m (i.e. there is a map/3: TM--->M such that the diagram
TM ~-~M
TN O--~N
Remarks. (1) It is easy to check that (IP) gives us the standard induction
principles for other data types, such as Stacks. It may be said that the "standard"
principles are these which we get by allowing F to range over polynomial
functors. However, the range of application of (IP) is not restricted to the
polynomial functors, and it is interesting to see whether anything useful is
obtained when F is allowed to be a nonpoly~nomial functor. We do not have
much to report at present, but the answer appears to be affirmative: Plotkin has
observed (private communication) that a certain principle used by Milner, on an
ad hoc basis, for reasoning about processes is in fact a special case of (IP) ( F in
this case involves the weak powerdomain functor P0).
(2) Some care is needed when one tries to generalize the argument, by which
we passed from formulation (3) to (6), to cpo's (specifically, to the category
CPO*). The difficulty lies in the step from (4) to (5): without special conditions
on m, we cannot ensure the existence of the fill-in arrow ft. It is fairly easy to see
that the appropriate condition is that m is a (strict, continuous) injection such
that xE y if (and only if) m(x)E m ( y ) ; in brief, m is a strict order monic. Then we
have the following specialized-induction theorem for CPO*: Let ct: FX---~X be
initial, and m: Y--)X a strict order monic such that range(or o Tm)C_range(m).
Then m is an isomorphism.
(3) There is not universal agreement as to what structural il~,~l~ction is. For
example, Manna [28] formulates it in terms of well-founded orderings, and our
treatment is not coextensive with his. However we believe that our version,
restricted as it is to "simple" data types, is appropriate for computer science.
Algebraic Specificationof Data Types 129
(4) It is clear that the full converse of Theorem 1 (for arbitrary C, F ) does
not hold. Various partial converses, adequate at least for the cases studied in this
paper, may be formulated. The simplest is perhaps the following:
Theorem 2. Let the functor F: C--~C have the initial algebra 71r: FZ---~Z, and
suppose that for any F-algebra fl: FY---~Y the arrow m: Z---~Y given by initiality of
~lF is a monic (of C). Then if the induction principle holds for F, et (where a:
F X ~ X ) , a is initial.
Following our usual procedure, let us amalgamate front and remove into a map
q: Q ~ T Q ( = 2 + A ® Q ) :
T ifs=A
q(s)= (front(s), remove(s)) otherwise
130 D.J. Lehmann and M. B. Smyth
u(T) = A
u((a, s ) ) = push(a, s).
qo u ( T ) = T
qo u((a, s}) = if doq(s) -- ff then (a, u ( Y ) )
else (Pl o q(s), u(a, P2 oq(s))} (7)
du(T ) = T
du((a, T )) = (a, A )
d,((a,<b, s) >) = (b, u((a, s))>.
In the case that Q is StackA and u is cs a, this just says that q is the
csA dq
operation dq of the data type QueueA (i.e., StackA ~-- T(StackA)<---StackA; see
Section 3)--something which we could also have established without difficulty
by direct manipulation of (7). Thus QueueA certainly satisfies (8). Now, Guttag
asserts that his equations define the operations of QueueA. It is not quite clear
what he means by this. Possibly he means that they define front and remove in
terms of push, A and empty (in effect, in terms of cs~), and in that case his
position is close to ours. Alternatively, perhaps we should be thinking in terms
of initiality: QueueA is the initial algebra satisfying the equations. Let us see
how this can be verified in our framework. We work with the diagrammatic
form (8) of the equations.
U*
Let D*=Q*~-~ TQ* be any algebra satisfying (8). We want to show that
q*
there is a unique arrow from QueueA to D*. This means that we must show that
the arrow a from the T-algebra cs A to u* (given by initiality of CSA) also satisfies
Ta o dq = q* o a. (9)
S4 TS
dq \ T dq)
d
\
TS" TTS ",'\
\
\,
\,
T~
T~
' TQ*
\,
\qi ~ Tq
1
TO'.., d* T1Q*
Fig. 10.
The two end squares and the top rectangle commute by definition. One can
check that the bottom rectangle commutes; the main case is:
Ta o d ( ( a , ( b , s ) )) = (b, a o CSA((a , S ) ) )
= (b, u*((a, a ( s ) ) ) )
---- d * ( ( a , ( b , a ( s ) ) ))
Since the back square and the bottom rectangle commute, Ta o dq is an arrow
from cs A to d*. Since the front square and top rectangle commute, q*o a is an
arrow from cs A to d*. By initiality of cs A these arrows are equal; the result is
proved.
N o doubt the argument can be generalized. However, we should point out
that we regard the result as something of a cu~osity. We do not see any use for
the property of initiality, except in the case of simple types (for which initiality
is, as we have seen, equivalent to structural induction).
132 D.J. Lehrnannand M. B. Smyth
Sometimes one wants to be able to prove that certain operations on data type D'
enable him to simulate, represent or implement those of a data type D.
Typically, D will have been defined "abstractly", say by a circular definition,
while D' is more "concrete" (more like what is usually available in programming
languages). The representation of stacks and queues by arrays may be cited as
examples. An interesting example that does not quite fit this pattern is that of
the representation of forests by binary trees. Here the representation involves a
reduction in the complexity of description of the data type: viz., the replacement
of simultaneous equations for trees and forests by a single equation. (Alterna-
tively, if forests are defined as stacks of trees, the representation may be
considered as involving a reduction in "star-height"; this approach was adopted
in an earlier version of this paper, Lehmarm and Smyth [25].)
These examples are well-known, and an elementary treatment may be found
in Knuth [20]. Our purpose is to give an exact definition of "implementation"
which will cover these and other examples, and to exhibit some techniques for
proving that proposed implementations are correct. The idea of our definition is
very simple. When it said that data type D' implements data type D, this means,
first, that D' and D are of the same type (operations of D' and D are in one-one
correspondence); secondly, that the elements of D' are taken as representing
elements of D, and this consistently with the operations of D, D'. Thus:
Definition 6.1. An implementation of D by D', where D and D' are data types,
is a homomorphism (of data types, see Section 3) r: D'-->D.
Lemma 1. I f D is a data type with carrier A which contains as one of its operations
an initial T-algebra e~: TA---~A, then any implementation r: D'---~D is onto.
Proof. Consider
A f~- TA
4 Sr~
A' ff- TA'
r$ ~Tr
A ~- TA
Proof. By induction on m. The basis m--O is trivial. Suppose that (1) holds for
all H, a, k, with m = n. Then
r o p ( a , ( H , n ) ) = r(upd(H, n, a), n+ 1)
= push(acc(upd(H, n, a), n), r ( u p d ( I 4 , . , a), . ) )
= push(a, r ( H , n)) using Lemma 2
= cs A o T ( r ) ( a , ( H , n>). []
Define r: Z ~ S t a c k A by
Lemma 3. With pop' and top' defined as in See. 3, we have for n >11:
Proof By induction on n. For n--1 the proof is trivial in each case. For the
induction step we have:
phism. Put ~" = r - ~o ~ o Fr. Then the data type D' = Z" ~-, F Z ' q~,-i
~ Z ' is isomorphic
to D.
Proof. Obvious. []
This lemma shows that there is indeed an (isomorphic) implementation of
forests by binary trees (more precisely, by a data type with carrier the binary
trees). However, direct application of the lemma does not yield the implementa-
tion in a very convenient form. The representation can be obtained more
explicitly by looking at the proof of Theorem 4.2:
X~2+ZxX
Z~--AxX
cb
BtreeA <...- 2 + A x BtreeA x BtreeA
$~(~)
el(12 + ct X ]Fo,A)
ForA ~-- 2+AXFor A×ForA
X ~-- 2 + Z ® X ±
Z~--A®X±.
136 D.J. Lehmann and M. B. Smyth
Here we admit infinite branching as well as infinite length of branches. Then the
same argument gives us a representation of infinite forests, so defined, by
infinite binary trees (Section 3).
If we want to admit infinite length of branches but not infinite branching,
we have only to delete the subscript _1_ in the first equation of the pair for forests
and trees. The same argument now gives us a representation of these infinite
forests by infinite binary trees in which there are no infinite chains of right links.
The reader is invited to supply the details.
We think there are some practical conclusions to be drawn from our investiga-
tion. The fact that the definition of data types has such a simple and clear
mathematical semantics does speak in favor of a mathematical definition of
programming languages, rather than an operational definition. The main ad-
vantage of a mathematical definition is clarity: It is much easier for the user to
think in terms of abstract data types, for example initial fixpoints of functors,
than in terms of their representations--as he is forced to do if the definition of
the language has an operational flavor and stresses representations, in the
manner of the A L G O L 68 Report [van Wijngaarden et al [42]).
Quite often a language is difficult to learn not because of what is in the
language but because of what is left out; the restrictions, when they are not
solidly justified in semantic grounds, are most difficult to memorize. Our second
piece of advice would then be to remove all restrictions on the use of circular
definitions for specifying data types; again--as with nearly all the points to be
made in this s e c t i o n - - A L G O L 68 provides an illustration of what needs to be
improved upon. Apart from easing the learning of the language, the introduction
of arbitrary circular type definitions would allow the programmer to define data
types such as lists and trees without having to introduce references (or pointers)
explicitly. This would greatly facilitate proofs of correctness. Indeed, proofs of
correctness are intractable when sharing of values (sometimes calling aliasing)
occurs. One can try to exclude aliasing by means of syntactic restrictions on
programs; but this does not seem to be possible when references are permitted.
The greatest care should be taken in the design of a programming language
to provide the necessary facilities to denote useful basic data types, such as 1
and 2, and useful type constructors, such as sums and products. In particular,
the distinctions between product and coalesced product, coalesced sum and
separated sum should be emphasized. Union should be clearly recognized as
being a discriminated union.
The facility for circular definitions of data types should provide a canonical
way of defining the functions defined. More precisely, a circular definition
X ~ T X should enable the user to give a name to ~ 1 : X.--~TX and to ~r: TX--->X
the initial T-algebra. rl~-l is obviously necessary, but one could question the
necessity of */r and propose to make it an implicit "coercion."
Our last piece of advice is that a language should allow the user to specify
abstract data types on one hand and the way he intends his abstract data types
to be implemented (by way of a homomorphism of data types) on the other
Algebraic Specification of Data Types 137
8. Conclusion
T h e c a t e g o r y - t h e o r e t i c m e t h o d e n a b l e s us to p r e s e n t the s e m a n t i c s of d a t a types
as a precise g e n e r a l i z a t i o n of the usual p a r t i a l o r d e r semantics. T h i s is of g r e a t
value heuristically in f o r m u l a t i n g the b a s i c definitions a n d results of the theory.
M o r e specifically, it helps e x p l a i n the f u n d a m e n t a l role of initiality, b y s h o w i n g
t h a t this is j u s t (the g e n e r a l i z a t i o n of) the least fixpoint p r o p e r t y .
W h i l e we a r e sure of the s o l i d i t y of the m a t h e m a t i c a l f o u n d a t i o n s , large
g a p s r e m a i n in o u r t r e a t m e n t o f the a p p l i c a t i o n s . I m p l e m e n t a t i o n s of d a t a types
n e e d to be i n v e s t i g a t e d in far g r e a t e r variety t h a n we have a t t e m p t e d in Section
6. A n d it will n o d o u b t b e p o i n t e d o u t that specific design p r o p o s a l s are needed,
n o t j u s t g e n e r a l a d v i c e to l a n g u a g e designers.
Acknowledgments
Many people, not all of them mentioned in the text of this paper, have played an important role in
the genesis of the ideas and techniques used herein. We benefitted from direct conversations with the
theory group at Warwick University, and especially D. Park and W. Wadge. We are grateful to D.
Scott, J. Reynolds, and M. Arbib for their encouragement and their suggestions. Special thanks are
due to G. Plotkin for the many improvements he suggested and his very active interest.
References
1. J. Adamek, Free algebras and automata realizations in the language of categories, Commenta-
tiones Mathematicae Universitatis Carolinae 589-602 (1974).
2. ADJ: J. A. Goguen, J. W. Thatcher, E. G. Wagner, and J. B. Wright, Abstract data types as
initial algebras and the correctness of data representations, Proc. Conference on Computer
Graphics, Pattern Recognition and Data Structures (1975).
3. ADJ: J. A. Goguen, J. W. Thatcher, E. G. Wagner, and J. B. Wright, Initial algebra semantics
and continuous algebras, J. ACM 24, No. 1, 68-95 (1977).
4. M.A. Arbib, Free dynamics and algebraic semantics, Lecture Notes in computer Science 56,
212-227 (1977).
5. M.A. Al'bib and E. G. Manes, A categorist's view of automata and systems, category theory
applied to computation and control, Springer Lecture Notes in Computer Science, Vol. 25
(1974).
6. J. de Bakker, Reeursive procedures, Math. Centrum, Amsterdam (1974).
7. M. Barr, Coequalizers and free triples, Math. 2., i 16, 307-322 (1970).
8. G. Birldaoff and J. D. Lipson, Heterogeneous algebras, J. Combinatorial Theory 8, 115-133
(1970).
9. W.H. Burge, Reeursive programming techniques, Addison Wesley (1975).
10. B. Courcelle and M. Nivat, Algebraic families of interpretations, 17th Ann. IEEE Syrup. on
Foundations of Comp. Sci., 137-146 (1978).
11. D.P. Friedman and D. S. Wise, CONS should not evaluate its arguments, Proc. of 3rd CoIl. on
Automata, Languages, and Programming, Edinburgh (1976).
138 D.J. Lehmann and M. B. Smyth
12. M. Gordon, Models of pure LISP, Ph.D. thesis, Dept. of Artificial Intelligence, Edinburgh
University (1973).
13. M. Gordon, R. Milner, L. Morris, M. Newey, and C. Wadsworth, A metalanguage for
interactive proof in LCF, 5th Annual ACM Syrup. on Principles of Programming Languages,
Tucson, AZ (1978).
14. J.V. Guttag, Abstract data types and the development of data structures, Communications of
the ACM, 20, No. 6, 396-404 (1977).
15. J. V. Guttag and J. J. Homing, The algebraic specification of abstract data types, Acta
Informatica 10, 27-52 (1978).
16. P. Henderson and J. Morris, Jr., A lazy evaluator, 3rd ACM Symposium on Principles of
Programming Languages, 95-103 (1976).
17. H. Herrlich and G. E. Strecker, Category theory, Allyn and Bacon (1973).
18. C . A . R . Hoare, Recursive data structures, J. Computer and System Sciences 4, No. 2, 95-103
(1975).
19. A. Kanda, Data types as initial algebras, 19th Annual Symp. on Foundations of Computer
Science (1978).
20. D. Knuth, The art of computer programming, Vol. 1, Addison-Wesley (1969).
21. F . W . Lawvere, An elementary theory of the category of sets, Proc. National Academy of
Sciences 52, 1506-1510 (1964).
22. D . J . Lehmann, Categories for fixpoint semantics, Theory of Computation Report No. 15,
Dept. of Computer Science, University of Warwick (1976); see also proc. 17th Annual Symp.
on Foundation of Computer Science, IEEE (1976).
23. D.J. Lehraann, Models in ALGOL Y, Proc. 5th Annual I.I.I. Conference, May 1977, Guidel,
France, Publ, by I.R.I.A. (1977).
24. D.J. Lehmann, On the algebra of order, see extended abstract in Proc. 19th Annual Symp. on
F.O.C.S. IEEE, to appear.
25. D. Lehmaan and M. Smyth, Data types, Theory of Computation Report No. 19, University of
Warwick (1977).
26. S. MacLane, Categories for the working mathematician, Springer (1971).
27. S. MacLane and G. Birkhoff, Algebra, Macmillan (1967).
28. Z. Manna, Mathematical theory of computation, McGraw-HiU (1974).
29. G. Markowsky, Categories of chain-complete posers, IBM Research Tech. Report RC5100, to
appear in "llaeoretical Computer Science.
30. J. Mesguer, On order-complete universal algebra and enriched functorial semantics, Lecture
Notes in Computer Science 56, 294-301 (1977).
31. G . D . Plotkin, A powerdomain construction, SIAM J. Computing 5, No. 3, 452-487 (1976).
32. G.D. Plotkin and M. B. Smyth, The category theoretic solution of recursive domain equations,
Proc 18th Annual Symp. on F.O.C.S.I.E.E.E. (1977).
33. W. P. de Roever, Recursive program schemes: semantics and proof theory, Mathematical
Centre Tract No. 70, Amsterdam (1976).
34. D.S. Scott, The lattice of flow diagrams, in Semantics of Algorithmic Languages (E. Engeler,
ed.), Springer Lecture Notes in Mathematics 188, 311-368 (1971).
35. D.S. Scott, Continuous lattices, in Toposes, Algebraic Geometry, and Logic (F. W. Lawvere,
ed.), Springer Lecture Notes in Mathematics'274, 97-136 (1972).
36. D.S. Scott, Data types as lattices, SlAM J. Computing 5, No. 3, 522-587 (1976).
37. M.B. Smyth, Effectively given domains, Theor. Comp. Sci. 5, 257-274 (1977).
38. M.B. Smyth, Powerdomains, J. Comp. Syst. Sci. 16, 23-36 (1978).
39. J. Stoy, Denotational semantics, M.I.T. Press (1978).
40. M. Wand, On the recursive specification of data types, Category Theory Applied to Computa-
tion and Control, Springer Lecture Notes in Computer Science Vol. 25 (1974).
41. M. Wand, Fixed-point constructions in order-enriched categories, Technical Report No. 23,
Comp. Science Department, Indiana University (1975).
Algebraic Specification of Data Types 139
Received June 6, 1977; received in revised form January 22 and February 29, 1980.