You are on page 1of 43

Math.

Systems Theory 14, 97-139 (1981)


Matl3ematical
Systems Theory

Algebraic Specification of Data Types: A Synthetic Approach *+

Daniel J. Lehmann ~ and Michael B. Smyth 2

~Mathematics Institute Hebrew University, Jerusalem, Israel.


2Department of Computer Studies, The University of Leeds, Leeds, LS2 9JT, U.K.

Abstract. A mathematical interpretation is given to the notion of a data


type, which allows procedural data types and circularly defined data types.
This interpretation seems to provide a good model for what most computer
scientists would call data types, data structures, types, modes, clusters or
classes. The spirit of this paper is that of McCarthy [43] and Hoare [18]. The
mathematical treatment is the conjunction of the ideas of Scott on the
solution of domain equations [34], [35], and [36] and the initiality property
noticed by the A D J group ( A D J [2] and [3]). The present work adds
operations to the data types proposed by Scott and proposes an alternative
to the equational specifications proposed by Guttag [14], Guttag and H o m -
ing [15] and A D J [2]. The advantages of such a mathematical interpretation
are the following: throwing light on some ill-understood constructs in
high-level programming languages, easing the task of writing correct pro-
grams and making possible proofs of correctness for programs or implemen-
tations.

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

consist also of certain functions. They understood the importance of initiality


and noticed that certain data types were initial algebras (or initial many-sorted
algebras) but were unable to include procedural data types in their treatment
and did not see the link with initial fixpoints of continuous functors. The
relation between our work and the authors just mentioned can be summarized as
follows. We provide a categorical version of Scott's domain constructions that is
simpler than Wand's. At the same time, we take full account of the ideas of
ADJ, but, rejecting their equational approach, we avoid the limitation to
nonprocedural data types that their method entails. As to the mathematical
results in the paper, most of these are fairly o b v i o u s - - o n c e one has grasped the
idea of systematically generalizing from posets to categories. The main purpose
of the work is, however, not to present detailed results, but to show that a clear
and rigorous basis for the theory and practice of data types can best be provided
by the concepts of o~-categories, ~-continuous functors, and initial fixpoints.

2. Mathematics

This section will introduce the basic notions and notations to be used in the
sequel.

Definition 2.1. A (similarity) type T is a ranked alphabet. The rank of a symbol


is called its arity.
A type is a set of symbols (intended to represent functions); to each symbol
is attached a natural number (intended to be the number of arguments taken by
the function represented), the arity of the symbol. If T is a type, T~ C_T is the set
of all symbols o f rank n.

Definition 2.2. A (universal) algebra of type T is a set S (called the carrier of


the algebra) and for each n E N a function

~: 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

Caution: the colimit of an ,0-diagram is a cone (strictly speaking a cocone)


not only an object.

Definition 2.5. A functor F: A---~B is an o~-functor iff F preserves all existing


colimits of o~-diagrams.
Clearly constant functors are o~-functors, the composition of o~-functors is an
o~-functor and. a bi-functor is an ~o-functor iff it is an o~-functor separately in
each argument.

Definition 2.6. If T: C---->Cis an endo-functor a T-algebra is an arrow (of C) of


the form q~: T c ~ c .
A similarity type T', as in definition 1, can be considered as a functor T in
the category Set. An example will show this better than a formal definition.

Let T' = (0, S) with rank(O) = O,rank(S) = 1.

The functor T would be defined by TA = 1 +A and Tf= I l + f . Then a T-algebra


would be a function if: I + S-->S and would correspond uniquely with a set (S)
equipped with one constant and one unary operation. The reader will easily see
how to generalize the above example to arbitrary similarity types (even infinite
ones).

Definition 2.7. If T: C-->C is an endofunctor the category of T-algebras is the


category whose objects are the T-algebras and whose arrows, from qs: Tc-->e to
q~: Td--->d are the arrows a: c ~ d of C such that aoq~=q~oTa.

cCTc
,x $ ~ ~ T,~
d ~ Td

If T corresponds to a similarity type T' the arrows of the category of


T-algebras are the homomorphisms of the universal algebras of type 7". A word
of caution is necessary here to warn the reader that our definition of a
T-algebra, though seemingly only an extension of the one found in MacLane
[26] where T is always supposed to be a monad, has in fact a different purpose.
The functor T that corresponds here to a similarity type T' is not the one
MacLane would consider (the one building the carrier of the free algebra). Our
notion of an algebra is identical to what Arbib and Manes [5] called a
T-dynamics. We are interested only in the case where T is an ~0-functor and C
an ~o-category.

Theorem 2.1. Let C be an ,o-category and T: C--->C be an o:-functor; then the


category of T-algebras is an ~o-category.

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

The following t0-diagram has a colimit (C is an to-category)

I ~ T_L ~ T2_L --~ T3_L--~ "''


Zr± T±r± T2..LT±

L e t #~: Ti_l_---~a be a colimiting cone. Till: Ti+l---~Ta is a colimiting cone


because T is an to-functor. T h e n there is a unique a: Ta---~a such that a o T#i---
/~i+ i. We claim that a is the initial T-algebra. Suppose t : Tc---~c is a T-algebra.
Define i,o = I c a n d ~'i+ l = fl ° Tvi. Then Pl o _L T± ~--" _ L c --'~ PO and b y induction on
i:

Pi+l ° T i - L T z = fl°T(pi°Ti-l-L T_L) = f l ° T l ' i - I = Pi'

so that the p/s form a cone. Suppose y: a---~c is an arrow of T-algebras

a y c

Ta ~ Tc
Ty

yo/.to=yo_l_a=_Lc=l'o and b y induction y o l ~ i + l = y o a o T # i = f l o T T o T # i =


fl o T ( y o # i ) = fl ° T1"i = Pi+ ~ and y has to be the unique arrow such that y o #,. = Pi.
On the other hand, by the universality of/~,., there is such a y. N o w

-[ o ol o Tl~i ~. y o p , i + 1 = Vi+ I = fl o Tp i = flo T y o T#i

By universality of T#i: y o a = f l o T7 and V is an arrow of T-algebras. []

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.2. L e t T be an endofunctor on C. I f a: Ta--~a is an initial T-algebra,


then a is an isomorphism.
We want to give examples of the application of the above theorems. Our
claim is that data types can always be considered to be objects in an appropriate
to-category, such that each constructor is an to-endofunctor of this category.

E x a m p l e 1.Set and universal algebras


Set is cocomplete (and also complete) and so is an to-category, x ; Set × Set
--->Set is an to-functor because it is a product and finite limits preserve directed
102 D . J . Lehmann and M. B. Smyth

colimits in Set (see MacLane [26] Theorem 1 p. 211). + : S e t × S e t , S e t is an


o~-functor because it is a coproduct, and so has a right adjoint and preserves all
colimits.

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.

Example 2. Set" and n-sorted algebras. (Set" = Set x Set x • • • x Set.)

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.

Example 3. CPO* and continuous algebras. To remedy the absence of order


structure on the objects, ADJ [3] have proposed to use many-sorted algebras
whose carriers are w-complete partial orders with a least element and whose
operations are w-continuous functions. Our objection to this is that the problem
of circularly defined data-types whose definition involves the arrow (or the
ALGOL 68 proc constructor) is not solved, simply because the arrow is not a
bi-functor in the above category: it is contravariant in the first argument. We
shall nevertheless show that our results allow a very simple proof of ADJ [3]'s
main technical result: the existence of initial continuous algebras. Let CPO* be
the category the objects of which are the c0-complete posets (every o~-diagram
a0[ a l E . - . E aiE ... has a 1.u.b.) with least element and the arrows of which are
the strict (bottom preserving) w-continuous functions. Markowsky [29] showed
that the full subcategory of CPO* which consists of all chain-complete posets
(which he calls CPC*) is complete and cocomplete. Meseguer [30] proved that
the category of co-complete posets and co-continuous functions is complete and
AlgebraicSpecificationof Data Types 103

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 To prove completeness it is enough to prove the existence of products


and equalizers of pairs (MacLane [26] p. 109). Products in CPO* are just like in
Set; it is a trivial task to check that the product of w-complete partial orders is
an w-complete partial order, that the projections are strict continuous functions,
that the unique mediating arrow from a cone of continuous functions is
continuous and that the unique mediating arrow from a cone of strict functions
is strict. Equalizers of pairs in CPO* are just like in Set. The equalizer of
f
A ~ B is h: A ' ~ A where A ' = {a[ a ~ A , f ( a ) = g ( a ) ) with the ordering induced
g
by the one on A and h is the injection. A' is an w-complete poset with least
element because f and g are strict which implies A_ ~ A ' and f and g are
w-continuous. The function h is obviously strict and c0-continuous. Now to prove
cocompleteness, by Herrlich and Strecker [17] (23. 14 p. 163) it is enough to
prove that CPO* is well-powered, and has a coseparator. []
The next lemma shows that CPO* is well-powered. For the sequel let
2-- ( A_, T ) be the two-points w-cpo ordered by _1_E T.

Lemma 1. 1f f: A---~B is a monic in CPO* then it is one-to-one.

Proof S u p p o s e f ( a l ) = f ( a 2 ) f o r f m o n i c . Let hi: 2--,A be defined for i-- 1,2 by


hi(_k)= 1 and hi( ) = a i. For i = 1,2 h i is a strict w-continuous function and
hi ° f = h 2 o f ~ h I = h z ~ a I =a 2. []
It is left to show that CPO* has a coseparator.

Lemma 2. 2 is a coseparator in CPO*.

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

Clearly h is monotone and continuous. But hf(ao)= _k and gk(ao)= T and


hf # hg. []
To a similarity type 7" can be associated a functor T: CPO*---~CPO* by the
method indicated following definition 6, using products and coproducts in
CPO*. For the reason indicated in Example 1 the product and coproduct are
w-functors. Our theorem 1 then implies the existence of an initial continuous
104 D.J. Lehma.l~and M. B. Smyth

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.

Example 4. CPO-PR and circularly defined data types.


Let CPO-PR (PR stands for projections) be the category the objects of
which are the to-complete partial orders with a least element (the same objects as
those of CPO*) and the arrows of which f: A ~ B are the pairs of to-continuous
mapsf= (fL, f R ) , f L : A--~B andfR: B-~A, such t h a t f R of L = i A a n d f L o f RE i s .
The reader may easily check that CPO-PR is indeed a category. The
identity on A is (I A, IA) and the composition h = f o g of two arrows in CPO-PR
is defined by h L = f L o g L and h R = g ~ o f R . The arrows of CPO-PR are the
projection pairs of Scott [35] and in one-to-one correspondence with the embed-
dings of Smyth [38]. Wand [40] seems to have been the first to state that the full
subcategory of CPO-PR consisting of complete lattices is an ~category. Plotldn
[31] noticed that +, × and the function space constructor (--~) preserved
colimiting objects of t0-diagrams. The authors noticed that +, × and ~ could
be considered as to-functors on CPO-PR (see section 3) thus simplifying Wand's
[40] treatment which uses a nonstandard notion of continuity. Lehmann [22]
proves a more general statement from which the fact that CPO-PR is an
to-category and ×, +,--~ are to-functors are instant corollaries. Certain full
subcategories of CPO-PR: the category SFP-R of Plotkin and the category of
algebraic consistently complete cpo's have been shown to be to-categories
themselves by Plotkin and Smyth. They also are closed under ×, + and --->.
SFP-R is also closed under the power domain constructor P of Plotkin [31]; the
Algebraic Specificationof Data Types 105

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.

Lemma 3. I f f = ( f L , f •) is an arrow in CPO-PR, then any one o f f L or f R


uniquely determines the other.

Proof Suppose (fL, fff) and ( f L f2R) are arrows in CPO-PR t h e n f ~ - - - f ~ f c f f f


Ef2R and symmetrically. []
It is now clear why we announced above that CPO-PR was a subcategory
(up-to-isomorphism) of CPO*

Lemma 4. lf f=(fL, f R) is an arrow & CPO-PR: A---~B then

f L ( - L ~) = I B and fR(_I_ B) = _k A.

Lemma 5. The one-point partial order, 1, is initial in CPO-PR.

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 7. Let F: Ao---~ Y°A1---~/'A2---~--~Ai---~Y'Ai+I---> . . . be an w-diagram in


L R L
CPO-PR. There exists a cone t~: F---~Aoo such that for any i E N ~ti~L i E/Li+Ift/R+I
and t.J il.tifti
L R -':'1.4.
In the next lemmas all w-sequences of functions for which 1.u.b.'s are used
can be easily checked to be ascending.

Lemma 8. Let F (as in L e m m a 7) be an to-diagram in CPO-PR. I f the cone v:


F--->B is a colimiting cone (in CPO-PR) then U iv?vff -~Is.

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).

Lemma 11. Let r be a to-diagram in CPO-PR and ft: F---~A~ be a commuting


cone such that izL is a colimiting cone in C P O * for F L then # is a colimiting cone (in
CPO-PR).
106 D.J. Lehmann and M. B. Smyth

Lemma 12. Let F be an to-diagram in CPO-PR and t~: F-->A~ be a commuting


cone such that t~n is a limiting cone for F n in CPO*, then tz is a colimiting cone (in
CPO-PR).

Definition. Let E L be the covariant embedding of CPO-PR in CPO* which


sends each pair of functions to its left part and E R be the contravariant
embedding of CPO-PR in CPO* which sends each pair of functions to its right
part.
Clearly E L and E n are the identities on objects.

Theorem 2.4. 1) CPO-PR is an to-category; 2) Ecpreserves and reflects ~o-coBmits;


3) E a transforms to-colimits into o~°P-limits and reflects ~o°P-limits into o~-colimits.

Proof 1) by Lemmas 5, 7, 9 and 11; 2) by Lemmas 8 and 9 and Lemma 11; 3)


by Lemmas 8 and 10 and Lemma 12. []
The next Theorem will be used to prove that functors (or bifunctors) in
CPO-PR are ~0-functors.

Theorem 2.5. I f T is an endo-functor on CPO-PR, T preserves o~-colimits if for


R
every sequence ~i: Ai--*B ( i = 1.... ) of arrows of CPO-PR such that ~iLvi~E v ~ lJ,i+ _L,
(T~)L(T~i) R E (T~i+ I)L(T~i+,)R and U ~[pR = I the following holds:
II i ( T p i ) L ( z p i ) R .~- I.

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*.

Theorem 2.6. Let T: CPO-PR--~CPO-PR be a functor and T': CPO*-->CPO*


an to-functor such that E L T = T ' E L. Then T is an ¢o-functor and if ¢p is an initial
T-algebra then ELep is an initial T-algebra.

Proof T' is an o~-functor by hypothesis and E L is an to-functor by Theorem 2.4.


Therefore T ' E L = E L T is an ~0-functor. But by Theorem 2.4 E L reflects o~-colimits
and then T is an to-functor. Theorem 2.1 asserts the existence of ~: TA-->A the
initial T-algebra, and the existence of ~p: T'B-->B the initial T'-algebra. The
Algebraic Specificationof Data Types 107

domain of ELq, is E•TA =T'ELA = T'A (E L is the identity on objects). The


codomain of E / ~ is ELA =AELep: T'A--->A is then a T'-algebra, and there exists
a unique arrow x: B ~ A in CPO* such that xoe/=EL4~oT'x. On the other hand,
by Theorem 2, tp is an isomorphism and X = (~, ~P- l) is an arrow of CPO-PR, X:
T'B--.B. But T ' B = T ' E L B = E L T B = T B and X is a T-algebra. There exists a
unique y: A---~B in C P O - P R such that y o ep=X ° Ty.
Then ELyox: B---~B is an arrow of CPO*, and we have:
ELyoxo~ p = ELyo ELd?o T'x = Ez.(xo Ty)o T'x = Ezxo ELTyo T'x
= tpoT'ElyoT'x = ~ o T ' ( E L y o x ).
By initiality of ~p then E l y O X = l s. But then x = E R y o E L y o x = E r y and
x o E L y = E R y o E L y = I A . Therefore x is an isomorphism and xo~p=EL6poT'x
implies that ELd? is an initial T'-algebra as is ~p. []

Remarks. Markowsky's (29) result on the cocompleteness of CPC* is not used.


Circularly defined data types the definition of which involves +(union),
× (struct) and ~(proc) can be seen to be initial algebras in CPO-PR, as will be
explained in the sequel (see Lehmann [23] for a preview oriented towards
A L G O L 68). Scott's original [35] solution to domain equations considered the
subcategory of C P O - P R whose objects are continuous lattices and whose arrows
are projection pairs of A-continuous functions. It can be easily checked that this
subcategory is closed under ~0-colimits. Scott [36] proposes another definition of
data types and reduces the solution of domain equations to least upper bounds.
Plotkin and Smyth [32] have recently shown that these 1.u.b's are ~o-colimits in a
suitable category which is equivalent to the subcategory of C P O - P R considered
originally by Scott [35].
Example 5. Dora and a more general notion of a data type.
Lehmann [22] defined a category Dora the objects of which are ~-categories
and the arrows of which are adjunctions with identity unit. Dora is an o>category
and x , + , ~ , and P can be defined to be o>functors. C P O - P R is a full
subcategory of Dora closed under ~-colimits, ×, +, and --~. The analogues of
Theorems 4 and 5 hold and relate Dora and o>categories and strict o>functors.
Dora provides a more general notion of a data type, useful when the powerset
constructor, or nondeterministic procedural types are allowed. We shall now
proceed to give one example showing how circular type definitions do indeed
define initial algebras. The algebraic aspects will be stressed: a circular type
definition does not only define a partially ordered set but also some functions on
this set. The example is only meant to familiarize the reader with a new
presentation and new notions. The base category is Set.

Example 6. The natural numbers.


The natural numbers used in mathematics may be defined by a circular
equation in Set. Our treatment is equivalent to Lawvere's [21], as reported in
MacLane-Birkhoff [27] (pp. 67-70).
Let our underlying category be Set, 1 be the one-point set (it is a terminal
object in Set and also a separator), _1_ the unique element of 1, + be the
coproduct (disjoint union) and let T be the functor defined by T = K~ + 1 where
K~ is the functor mapping every arrow into the identity on 1 and I the identity
108 D.J. Lehmann and M. B. Smyth

functor. Clearly T is a functor: Set->Set. It is indeed the functor associated with


the similarity type T' = (0, s) rank (0) =0, rank (s) = 1 described after Definition
6. T is an ~-functor, as are all functors associated with similarity types, as
explained in Example 1. By Theorem 2.1 there is an initial T-algebra, ~:
I+A--->A. Let i!: 1--->1+A and i2: A---,I+A be the canonical injections; it is left
to the reader to show that the universal algebra ( A , ( ~ o i 1)(_1_), ~ o i 2 ) is isomor-
phic to (N,0, suc) where N is the set of natural numbers, 0 is the number zero
and suc the function successor. The point we want to make here is that the type
N can be circularly defined by: N - ~ I + N or in A L G O L 68 notation mode
natural =union (void, natural). The initial fixpoint involves not only the set N
but also the constant 0 and the function successor. The induction rule for
natural numbers which is vital in proving properties of programs manipulating
natural numbers is nothing else than an initiality property. For other circularly
defined data types the initiality property may often be used directly in place of
an induction principle. In other cases it is the main tool in proving the
correctness of an induction principle. The question of the exact relation between
initiality and induction will be treated in Section 5.
All useful functions on natural numbers may be defined as the unique arrow
ct such that a o ~ = tk ° Ta for some function ~b. Two examples will be presented.
Let 0 be ~oi l, suc b e dP°i2, and ~ - l the inverse of ~ whose existence is asserted
by Theorem 2.
Let ~kl: I+[A-->A]-->[A-->A] be defined by tpl=[?~x.I, k f . ( s u c o f ) ] (the
notation is from Herrlich-Strecker [17] p. ll7 and means that ~bojlffiI N and
~ o j 2 = X f . ( s u c o f ) if Jl and ./'2 are the canonical injections). The unique a:
A-->[A-->A] such that a o~=~b I o Ta is (up-to-isomorphism) kn~km (n+m).
Let B = ( t r u e , false} and ~b2: I+[A-->B]-->[A-->B] be defined by ~b2--[g, h]
with g(_l_)=?~a, if a = ( ~ o i l ) (_l_) then true else false and h ( f ) = ~ a , if a = ( ~ o i l )
( l ) then false else ( f o ~ - l ) (a). Then the unique fl: A-->[A-->B] such that
floep=t~E OTfl is k n ~ m . n = m .

3. Data types as algebras

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.

Definition 3.2. A data type D of type t--(n, S i, Ti) consists of an object D of C,


the carrier of D, and n (w-continuous) functiong ~i: SiD-->TiD.
Algebraic Specification of Data Types 109

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.

Definition 3.3. Let D~, D 2 be data types of signature t. A homomorphism from


D l to D 2 is a function f: DID 2 such that the following diagrams commute
(i-- 1..... n):

SiO 1--->TiD l

In Section 6 we will define representations as homomorphisms of data types.


For the moment, just notice that we have defined a category of data types of
signature t.

Preliminary examples. The purpose of these preliminary examples is solely to


bring out certain points in the preceding definitions. Some typical computer
science examples are treated in a more leisurely fashion in the sequel.
(1) Bool. The domain will naturally be taken as described in Fig. 1.
As operations we will take, say, the constant (O-ary operation) tt, unary
operation ~ , and binary operation V. The constant tt can be considered as a
function from 1 into Bool. Thus the data type will have signature ((?~D.1, I),
(1,1), (XD.D×D, 1)), using an informal X-notation whose meaning is, we
hope, clear. The X-notation for functors will be considered further in Section 4.
Note that for XD.1 we also have the notation K v
(2) Sum (disjoint union, discriminated union). Given the cpo's A, B, define
the data type D to have the carrier A + B (coalesced sum) and operations the
injections i1: A--->A+B, i2" B--->A+B and the discriminator d: A + B--->Bool. The
type of D is taken to be ((XX.A, 1), (XX.B, I), (L XX.Bool)). Note that A and

tt ff

Fig. 1. .L
i10 D.J. Lehmannand M. B. Smyth

B appear in the type of the data type defined, as a consequence + cannot be


considered to be a functor from C P O * ~ C P O * to the category of data types of
a certain type. Note also the use of constant functor here. It has the consequence
that a homomorphism (and therefore a representation, or implementation, as we
shall define it) in this type does not change the A, B or Bool components. This
may seem strange at first sight, but we believe that it is reasonable given the
usual understanding of implementation. The next, slightly more realistic exam-
ple, should help to make this view more plausible.
(3) Stacks (of natural numbers). This data type may be considered to have
the carrier Stack N and the operations push: N × Stack N-~Stack N, pop: Stack
N ~ S t a c k N+(error}, top: Stack N ~ N + { e r r o r } , empty: Stack N ~ B o o l (at
least). Here, {error} is perhaps to be construed as the two-point domain. We
have the signature ((?tX.NxX, I), (I, XX.N+ {error}) .... ) This, we maintain,
gives the right notion of representation: a representation of stacks may be
expected to change the carrier, Stack N, while keeping N, {error} and Bool
fixed.
(4) Stacks (simplified definition). The framework provided by Definitions
1-3 permits us to define Stack N as having just the two operations cs:
2 + N × Stack N ~ S t a c k N and cs-~: Stack N - - . 2 + N x Stack N, with signature:
( ( 2 t X . 2 + N x X , I), (I, ~ , X . 2 + N × X ) ) . More details will be given later.
In the detailed discussion of examples to follow, we shall not trouble to
formulate things strictly in accordance with Definitions 1-3. We hope it is clear
how this can be done in each case.
The following notations will be used. If A is a partially ordered set then A ±
will denote the partially ordered set obtained by adding a new bottom element
to A. One may define a functor Ab: C P O * ~ C P O * which adds a new bottom
element to objects and extends functions into strict functions. Similarly one may
define a functor Abs: Set--,CPO* which adds a new bottom element to sets
(making them flat cpo's) and extends functions into strict functions. The
categorical product in CPO* (cartesian product) will be denoted x and the
projections Pi and P2. The categorical sum in CPO* (coalesced sum) will be
denoted by + and the injections by i I and i 2.

3.2. Basic data types

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

As explained in Section 2, a constructor is an o~-endofunctor in the category of


domains; we shall list below some of the most interesting ones.
Algebraic Specificationof Data Types 111

X: C P O - P R x C P O - P R ~ C P O - P R , corresponds to the categorical product


in CPO*. If A and B are cpo's A x B consists of all pairs of elements, the first
one in A, the second one in B ordered componentwise. If f: A--)A' and g: B--~B'
are arrows in C P O - P R then f X g = ( f r X g I " , f R X g R ) where the symbol x on
the right-hand side denotes the product in CPO*. That x is an o~-functor is
easily checked with the help of Theorem 2.5, and likewise the to-continuity of all
functors to b e defined now. A slightly different product will be needed for
defining stacks and lists.
®: C P O - P R x CPO-PR--~CPO-PR corresponds to the categorical product
in CPO** (the arrows are very strict t0-continuous functions, those functions
which send to bottom only the bottom element). If A and B are cpo's then A ® B
consists of only those couples (a, b) with a ~ A and b ~ B such that a~ a _k A and
b~_l. B or a=_L A and b=_l_ B. It is easily seen to be a cpo. If f: A ~ A ' a n d g :
B-~B' are arrows in C P O - P R then f ® g is the restriction o f f x g to A®B.
This nonstandard product has already been used by Gordon in his thesis
[12]. The obvious arrows in CPO*, Pl: A®B-~A and P2: A®B-~B, may be
defined.
+ : C P O - P R X C P O - P R - - ~ C P O - P R corresponds to the categorical sum in
CPO*, and in CPO**. A + B is the coalesced sum of A and B. Arrows il:
A--~A +B, i2: B--*A +B and d: A +B---~Bool may be defined (in CPO*).
The separated sum of A and B may be defined by A ± + B±. ~ : C P O - P R ×
C P O - P R - ~ C P O - P R is the function space functor. ---~(A, B) is [A---)B] in our
notation.
Note that all these functors, except for ~ , could easily have been defined
over CPO*. The reason for restricting to C P O - P R is to have a uniform
treatment which accommodates ~ . In some passages in the sequel (for example,
See. 5.2: definition of functions by initiality), it is necessary to suppose that we
are dealing with a functor defined over CPO*; in these cases the function space
functor is of course excluded from discussion.

App: [A---~B]×A--~B, Abst: [A×B---~C]---~[A--~[B---)C]]

and Y: [A~A]--.A may be defined (as arrows in CPO*). As was mentioned


above a power constructor P, or more precisely a number of such constructors
have been studied; they will not be used in the present work. The next
subsection exemplifies circular definitions of data types. For example it will be
shown how, given a data type A, it is possible to define the data type Stacks of A
(Stack A). It is only in the next section that it will be shown how these
definitions amount to making Stack a type constructor.

3.4 Circularly defined data types

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)

Fun-- proc(fun) fun

The data type defined by the equation X ~ [ X ~ X ] , corresponding to the


A L G O L 68 definition of the title is the initial T-algebra for T=~X.[X~X]. It is
1, the one-point domain, the arrow being the identity. This example is given, not
because of its usefulness, but because it is one of the circular definitions which
could not be understood in the framework set by the ADJ group.

.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

/~ = c s ( T ) , for nonempty s pop(s) = P2 o cs - l(s)


push = cs o i 2, for nonempty s top(s) = Pl ° cs- l(s)
empty = d o cs- l

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.

Finite and 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

The intuition suggests:

List A -- A + Stack(List A).

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

cl(J_) = _k, cl(T) = ( ) , cl(a, 1) = (a.l)

2 + (A + LispA)® LispA = 2 + (A ®LispA + LispA ®LispA)


= (2 + A ® LispA) + LispA ® LispA, which is obviously desirable.

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.

1 the empty list

/\. [[ ]]

/\ I.J
a

/\
[.t, [0]-- [['1°J
Fig. 4. •
/\ ix. im
116 D.J. Lehmann and M. B. Smyth

Binary trees

Labelled binary trees suggest the following definition:


BtreeA -----2 + A ® BtreeA ® BtreeA.
The reader may care to check that the initial algebra consists of all finite
binary trees all the nodes of which are labelled by nonbottom dements of A,
and one bottom element. The arrows obtained construct (cb: 2 + A ®BtreeA ®
BtreeA---)BtreeA) and decompose (cb- ~: BtreeA ~ 2 + A ®BtreeA ® BtreeA) trees.
Had we chosen to solve T~--A+A@T±@T± we would have defined an
initial algebra on a domain containing finite and infinite labelled binary trees.

Trees and Forests

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

It will be shown in Section 4 that forests would equivalently be defined as stacks


of trees, having first defined a tree to be a labelled node and a stack of trees. In
other words we can eliminate the mutual recursion, defining T and F by:

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: 2 +A ®StackA--->StackA the initial algebra and


cs - l: Stack A--~2 + A ® Stack A, the [empty, (top, p o p ) ] arrow.
Algebraic Specificationof Data Types 117

We may define dq: StackA--~2+A®StackA by: dq(s)=if empty(s) or


empty(pop(s)) then c s - l ( s ) else i2(( p l(dq(pop( s ))), push(top(s),
P2(dq(pop(s)))))).
The above definition simply translates the idea that the new top and pop
operations operate at the end of the stack. It would also have been possible to
leave top and pop unchanged and define a new push operation. Our formalism
enables us to prove the equivalence of these two ways of defining queues but we
shall not attempt to do that here. The data type Queues then consist of a domain
QueueA = Stack A and two functions:

cs: 2 + A ® QueueA --~ QueueA


dq: QueueA --~ 2 + A ® QueueA

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:

top': QueueA-->A by top'(s)=pl(dq(s)) for nonempty s, and


pop': QueueA--~QueueA by pop'(s)=p2(dq(s)) for nonempty s.

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:

update(g, n, a) = Am.if m = n then a else eval(g, m)

It is now obvious that: access(update(g, n , a ) , m ) = i f m=n then a else


access(g, m).

4. The Initial Fixpoint Operator

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

For composition of natural transformations we follow the notation of Herrlich


and Strecker: o for the vertical, • for the horizontal composition.

Lemma 1. [A---~C] is an *o-category.

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.)

Lemrna 2. For any *o-functor F: C---~C, let S(F) be the *o-diagram


f Ff
_1_--> F_I_--~ F21---~ . . . . For any natural transformation ~': F-->G (G an *o-
functor) let S('r), ( f o r n = 0 , 1 , . . . ) be zn: Fn_I_---~G"_I_, where T" is the n-foM
composition "r*z*. . . .'c. Then S is a functor from [C---~C] to C "°.

Proof. To show that S(z) is a natural transformation, we must show that the
i
diagram (note: ~-± means (i"i)±)

Z J~FZ Ff F 2 .A_ --~


--~

$ ~$ ~$ .... (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.

Theorem 4.1. Y is an 6o-functor.

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

FofoI Flil i Fkf F® f=,

F~, ~F~, F"=,


F"ofo Fakfk F="f=

Fig. 6.

n
F;. t-°'kl" --- F"~

F~fk F"o+fk

F~+! 1 F~Fk~ F~t.ok~


,q~,Fk,
Fig. 7.
Algebraic Specification of Data Types 121

Since F~(ok)±o(o~,)r~±=(o~,+l)j, it follows that F~f~ mediates, as stated.


This shows that the diagram of Fig. 6 is a colimiting diagram for S ~ .
The following l e m m a gives a characterization of the action of Y on arrows
which is often more convenient to work with than Definition I:

Lemma 4. Suppose that z: F--~F' is a natural transformation, where F, F' E [C--~


C]. Then Y¢ is the unique arrow from the initial F-algebra ~IF to the F-algebra
~F" o ,FyF,"
F/
Proof. Let A be the chain _L f F --->.... with colimiting cone/~: A--->YF; and
similarly for A',/~'. Define the cone e: FA--->YF" by: v,-/~n+~
- ' o ~'±
,+1 • We shall
prove that Y~o ~/F and ~IF"° "rrF' ° F(Y¢) are equal by showing that each is the
mediating arrow from F/~ to v. For Y~-o ~/r this is immediate. For ~F" o TrF, o F(Y~),
consider the commuting diagram (for each n ~ 0) of Fig. 8. Since Tr, ± o F(t-~_ ) =
z±n+ l, the perimeter of this diagram gives the desired result.
The next l e m m a introduces an abstraction operator for functors.

Lemma 5. Define Abst: [A × B--->C]-->[A--~[ B--->C]] on objects (i.e. functors F:


A xB--->C) by:
Abst(F)(X) = F(X,-), forXEA
A b s t ( F ) ( a ) y = F(a, Iy), for cz: X--->X';

n F Pn
FFI = F (YF)

F (YT)

FF,nl FP'n (YF')

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

and on arrows (natural transformations) by:

( A b s t ( z ) x ) r = "r<x,v >.

Then Abst is an &omorphism.


For the proof we refer to Herrlich and Strecker [17], Theorem 15.9; it is
routine to check that various functors which appear in the proof are w-
continuous, so that the change in the meaning of [. • • ~ . . . ] does not materially
affect the proof.
At last we are in a position to understand data type definitions with
parameters. A parameterized data type, in our view, is simply an to-functor
(from a category, thought of as the parameter category into a data type
category). One of the commonest situations is the following: an ~o-functor F:
A × C--->Cyields the pararneterized data type Yo (Abst(F)): A ~ C .

Example. Stack (Sec. 3) must be regarded as a parameterized data type, if we


are to make sense of the suggested circular definition

ListA -------A + Stack(ListA). (2a)

Here, Stack is an ~0-functor, and (2a) defines List as Yo (Abst(F)), where F is


X(A, C ) . A +StackC. It may be asked, what is the exact relation between List
and Lisp (as defined in Sec, 3)? We will find that such questions can be
answered by the help of Theorem 2.
Theorem 2 deals with the reduction of simultaneous recursion to iterated
recursion. It says, in effect, that a pair of simultaneous "equations"

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.

Notation. F,G: CxC--->C are ~-functors. ( F , G ) : CXC---~CXC: V--->(FV,


G V ) . Gx is XZ.G(X, Z) O.e. G(X,-_)). Z_ is the functor yo(AbstG); _flauswe
write Zx, Z o (where O: X--->X'), etc. F is XX.F(X, Z~), 3(is Y(F), and Z is Z~.
Some remarks on the X-notation used here follow the proof of the theorem.

Theorem 4.2. QI,~,:'/6~) /s initial for ( F , G ) .

Proof. Note first that, by Lemma 4, Z may be characterized as follows. Zx is


Y(Gx); if 8: X--->X', then Z o is determined by

Zx ,-- GAzD = G(X, Z,~)


zo,l, $cAzo) = G(~,zo) (3)
Z x, <-- G~(Zx, ) = G ( X , Zx, )
n~.. G(O, Iz~, )
Algebraic Specificationof Data Types 123

Now, suppose thatp: F(X', Z')-->X',q: G(X', Z')--->Z', so that ( p , q ) is an


(F, G}-algebra. We will prove that there is at most one arrow from ( ~ , ~/G~} to
(p, q}. Suppose, then that (a, 13} is such an arrow, i.e.,

- -- ~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')

(4b) and (5) give, respectively:

~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~,)

By initiality of ~/6~, fl--~" ° Z~.


Next, (4a) gives

_ P
x ,-- F ( X ) = F ( 2 , Z~)
~$ SP(~) = e(~, zo) (7)
x' ,-- ? ( x ' ) = ~ ( x ' , Zx,)
pF(&,, ~;)

By initiality of 7/f, a is uniquely determined; hence fl is also.


On the other hand, we see that if we start by defining a as being given by
(7), and put fl=~" o Z~, then all diagrams (4)-(7) commute, and in particular
(a, fl} is an arrow from (*/f, ~/c~) to ( p , q } .

)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

functor of several arguments is to-continuous in each argument separately (the


converse of this lemma is also true).
For a more formal treatment, capable of handling less trivial A-expressions,
we would take the meaning of a (typed) X-expression to be an 0~-functor from a
category of environments into the appropriate functor category built from C
(appropriate, that is, to the type of the expression). This complication in the
handling of environments is needed for giving the semantics of A-abstraction.
Since we apply A-abstraction only to variables of lowest type (C), the environ-
ment category, EnvL, can here be taken to be a product C x C x . . . indexed by
the variables of lowest type. For the other variables we can work with a set of
environments, as usual. In the formal treatment, the meaning of XZ.G(X, Z)
would thus be a mapping from Envn (the set of environments for the higher-type
variables) into the functor category EnvL~(C--->C ). We will not go into the
details, but we note by way of example that Z_ could have been defined as
~Xo Y(XZ.G(X, Z)). Corollary 1 provides a simple result expressed in terms of
these h-expressions.
With a little more preparation--in particular, defining the to-continuous
evaluation functors--we can make good sense of a typed A-calculus for ~-
functors. However, nothing so elaborate as this will be needed in the present
work.

Corollary 1. For any to-functor F: C X C-->C,

Y()~X.Y( AZ.F( X, Z))) ~-- Y(?~X.F( X, X)).

Proof Consider the two ways (orders) of solving the pair

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. []

Corollary 2. BtreeA -----ForA.

Proof Consider the pair Of equations by which we define trees and forests
(Section 3):

T-~ A ® F
F~-- 2 + T ® F (8)

By Theorem 2 we may solve by substituting T=A ®F in the second equation,


Algebraic Specificationof Data Types 125

showing that ForA may be taken as the initial solution of

F~ 2+(A®F)®F.

But this is (up to isomorphism) the equation for B treeA. []


As we shall see in Section 6, this actually gives us the standard "representa-
tion of forests by binary trees". A similar remark applies to:

Corollary 3. ListA ~ A + LispA.

Proof. Similar to Corollary 1, starting with the pair

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. Methods of Definition and Proof

Within the framework established in this paper, we have a number of methods


available for defining data types, data structures (elements of data types) and
functions over data types, and for proving properties of defined entities. Some of
these methods are brought together in the present section. The short subsection
on definitions is little more than a summary of points made in scattered form
elsewhere in the paper. Subsection 2 is concerned with induction versus initiality
as a method of proof. Subsection 3 takes up the question of how we might prove
that our definitions of particular data types yield the "right" properties.

5.1 Definitions

For definitions of data types we can make a fourfold classification. First, we


have the ad hoc definition of some basic data types. Secondly, we can apply a
functor--either one of the standard functors or a "parameterized data type" to
an already defined type. Thirdly, a data type can be introduced as the initial
fixpoint of a functor. Finally, we can define "composite" data types, in which
the carrier is taken to be given by one of the preceding three methods, but new
defined operations are introduced. Examples of all these methods were provided
in Section 3.
Turning to the question of specifying (defining) an element of a given data
type, the only point worthy of special comment is that arbitrary recursive
specifications are admissible. For procedural data types this is commonplace. As
a simple example of a nontrivial recursive definition within a nonprocedural
126 D.J. Lehmann and M. B. Smyth

data type, the infinite binary tree of Fig. 9. can be defined by:

x = cb(0,cb(1,A, /x ), x), where A = c b ( i l ( ) )

The basic type-constructors have, in view of their categorical definition,


certain functions associated with them: projections and injections associated
with product and sum, evaluation and abstraction maps associated with the
function space, and so on. And, of course, every functor gives us the means to
introduce functions: if a function f: X ~ Z and functor F have been defined,
then we have also the function Ff: FX-~FZ. So, for example, functions defined
on the natural numbers extend at once to arrays and stacks of numbers. APL is
probably the best-known example of a programming language that makes
systematic use of this possibility.
Initiality provides yet another way of defining functions. The most im-
portant example is the definition of the inverse isomorphism ~/~1 for an initial
F-algebra ~F: FXo~Xo. Once the inverse is available, further uses of initiality
for defining functions can often by eliminated in favour of recursive definitions.
In detail: suppose we have a definition of a function f: Xo-~X by initiality,
namely as the unique arrow of CPO* satisfying

f°BF=goFf (1)

where g: F X ~ X is defined previously. Here we have to suppose that F is defined


as an endofunctor of CPO*. We also assume that F is locally continuous on
morphism sets: for every pair of objects Y, Z of CPO*, F is ~0-continuous as a
map from [Y~Z] to [FY-->FZ]. (This condition is readily verified for a wide
range of functors F. It obviously holds for polynomial functors.) Then (1) can be
rewritten as

f = go Ffo~f I (2)

and this has the form f = Gf for a continuous functional

G: [ Xo',X] --,[ Xo-,Xj.

1 0 ~.9.
Algebraic Specification of Data Types 127

Thus f may alternatively be introduced as the (least) solution of (2), construed as


a recursive definition.
What we lose by this reduction is the unicity of the solution of ( 2 ) - - a n
aspect that may be of great importance in proving properties of f, or of the
domains X 0, X. Indeed, it is as a principle of proof, rather than of definition,
that initiality is most significant.

5.2. InitialiO, and induction

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:

For any M C N: if O E M and for any n(n EM---~n + 1 ~ M ) then M = N.


(3)
Given MC_N, let m: M - ~ N be the injection. Then the antecedent of the
conditional in (3) can be expressed as:

range(e? o Tm) C_r a n g e ( m ):

I+M M
Ida, ~m Sm (4)
I+N ~-~N

This in turn is equivalent to:

¢?o Tm is a subobject of m. (5)

(5) is just an abbreviated way of saying that ~, o Tm is a monomorphism which


128 D.J. Lehmann and M. B. Smyth

factors through m (i.e. there is a map/3: TM--->M such that the diagram

TM ~-~M

TN O--~N

commutes). Thus we find that it is possible to express (3) by:

For any monic m with codomain N: if qjoTm is a


(6)
subobject of m then m is an isomorphism.

More generally, for any category C, endofunctor F of C and F-algebra a:


FX--~X, let us say that the Induction Principle for F, a is
(IP) Any monic m: Y---~X, such that a o Fro: F Y ~ X factors through m, is an
isomorphism.
The induction theorem now reads:

Theorem 1. If a: FX---~X is an initial F-algebra over C, then the induction


principle for F, ct holds.

Proof. By two easy lemmas: (i) If m is an arrow in the category of F-algebras


over C, then m is monic provided it is monic as an arrow of C; (ii) In any
category, any monomorphism whose codomain is initial is an isomorphism.
Details omitted. []

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.

Proof. Immediate on taking ct for ft.


(5) Although Theorem 1 gives us the possibility of systematically replacing
uses of structural induction by initiality, the replacements actually made in this
paper were done on an ad hoc basis, and do not seem to fit the pattern. It
remains an open question how useful (IP) and Theorem 1 are as practical means
of proof.

5.3. Correctness of Data Type Definitions

In the context of the "abstract" algebraic specification methods advocated by


ADJ, Guttag, Zilles and others, our data type definitions may be considered
"concrete" in character. Apart from the simple types (those given directly by
recursive definitions), our data types are supposed to be defined by explicit
construction--and even the simple types are defined explicitly if, in accordance
with Section 4, we work with an initial fixpoint functor chosen once for all. It is
in this sense that our approach is "synthetic."
The question we have to consider now is, whether we can justify our
concrete definitions (which some might see as merely implementations) by
showing that they yield the properties usually required of the data types in
question. We are inclined to argue that no such justification is really needed;
but, on the supposition that it is, we will here provide a sample of how it might
go.
It happens that, for most of the data types introduced in Section 3, such a
justification is easily provided. Indeed, for simple types given by polynomial
functors, there is a uniform method of translation from our initial fixpoint style
of definition to the initial algebra definitions of ADJ (see Kanda [19]). As an
example of what may happen in the case of composite types, we will study the
example of Queues.
A list of the properties of (the operations of) QueueA has been given by
Guttag [14]. Changing the notation slightly, we have:
empty(A) = tt empty(push(a, s)) = f f
front(A) = remove(A) = error
front(push(a, s)) -- if empty(s) then a else front(s)
remove(push(a, s)) = if empty(s) then A else push(a, remove(s))

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

Similarly, push and A are replaced by u: TQ--->Q,where

u(T) = A
u((a, s ) ) = push(a, s).

Finally, the predicate empty disappears in favour of the standard predicate d


associated with + (Section 3). The equations are thus transformed into:

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)

For greater readability of these equations we would naturally write empty(s)


instead of do q(s)=ff, and A instead of u(Y).
It will be convenient to have (7) in diagrammatic form. Indeed, (7) is
equivalent to the commuting of
U
Q~--TQ
q~ ,LTq
du
TQ~---TTQ = 2+A®(2+A®Q) (8)

where d u is defined by:

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)

Consider the diagram of Fig. 10 in which S, d, d* abbreviate StackA, des., d,..


Algebraic Specification of Data Types 131

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 ) ) ))

= d*o TTa((a,(b, 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

6. Implementation of data types

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.

Remark. The representation map r need not be one-to-one (Example 1 below).


Moreover, r need not be onto, the most one can say, in general, is that every
element of D that is definable by means of the operations of D has a representa-
tive in D'. Examples would be provided by implementations of the reals, or of
procedural data types. However, in all the examples to be discussed here, the
map r is onto (a consequence of Lemma 1 below).
Despite its great simplicity, Definition I seems to us to be adequate to its
purpose. Its rationale is somewhat as follows. Suppose that, in the course of
developing an executable program for computing a function f, we replace the
(relatively "abstract") program ir by a program ~r', where ~r' differs from ~r in
that it uses certain data types which "represent" the corresponding types in ~r.
Correctness of a program relative to f means, presumably: Whenever an input of
the program represents an argument x, the program halts ifff(x) is defined, and
in that case the output represents f(x). If the correctness of ~r is known already,
representation maps satisfying the conditions of Definition 1 are just what we
need to establish the correctness of ~r'.
Some authors give definitions that are less direct than ours. Specifically,
they do not require that the operations of D' correspond directly with those of
D; they allow that D' represents D provided that, from the given operations of
D', suitable operations can be built (by specified means) which match those of
D. However, we prefer to treat the question" of exactly what methods may be
used in defining derived operations of a data type as a separate issue (discussed
only informally and by examples in this paper).
Algebraic Specification of Data Types 133

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

By initiality, r o a = I A; hence r is onto. []


We continue with a detailed treatment of the examples mentioned in the
opening paragraph of this section.

Example 1. Representation of stacks and queues by arrays.


CS A CSAI
(i) Stacks. Let D be StackA .-- T(StackA) ~ StackA, where T i s 2 + A x - .
Let Z be the set of pairs ( H , n ) , where H is an infinite array of A and n is a
natural number, with the obvious ordering; more precisely, Z is the coalesced
sum of countably m a n y copies of ArrA. Let D ' be Z ~ T Z q Z, where

p(T) = (H0,0) ( H o an arbitrarily chosen array)


p ( a , ( H , n ) ) = ( u p d ( H , n, a), n + 1)
q( H, n) = if n = 0 then T else ( a c e ( H , n - 1),(H, n - 1) ) .

Define r: Z---~StackA recursively by:

r( H, m) = if m = 0 then A else p u s h ( a c e ( H , m - 1), r( H, r n - 1)).

We shall need an elementary property of r which we state as

Lemma 2. For all H, a, m, k, we have

r(upd(H, m + k , a), m ) = r ( H , m) (1)

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 ( u p d ( H , n+ 1 + k , a), n+ l) = push(acc(upd(H, n + k + 1, a), n),


r ( u p d ( H , n + k + 1, a ) , n))
= p u s h ( a c c ( H , n), r ( H , n))
= r ( H , n + 1).
134 D.J. Lehmann and M. B. Smyth

We have to show that r o p = c s a oT(r). But r o p ( T ) = c s A oT(T) trivially, and

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>). []

An equally easy verification shows that T(r) o q = cs A ~o r; thus D' is an imple-


mentation of D. Note that in this implementation the functions p, q are not
inverse to each other.
(ii) Queues. We represent a queue by an array H together with two indices
re, n, such that the items of the queue are (from head to tail) H ( m + n -
CSA dq
1)..... H(m). Formally, D is StackA ~ T(StackA)~--StackA, Z is the coalesced

sum of a double sequence of copies of ArrA, and D' is Z p TZ q Z, where

p(T) = (H0,0,0) ( H 0 arbitrary)


p(a,(H,m,n)) = (upd(H,m+n,a),m,n+ 1)
q ( H , m, n) -- if n = 0 then T else ( a c c ( H , m ) , ( H , m + 1, n - 1 ) )

Define r: Z ~ S t a c k A by

r( H, m, n) = if n = 0 then/~ else push(acc(H, m + n - 1), r( H, m, n - 1))

Corresponding to Lemma 2, we have: r( upd( H, m + n + k, a ), m, n )= r( H, m, n ).


Then r o p = c s A oT(r) is proved just as before. To complete the proof that D'
implements D, we need

Lemma 3. With pop' and top' defined as in See. 3, we have for n >11:

top' or(H, m, n) = acc(H, m)


p o p ' o r ( H , m, n) = r(H, m + 1, n - 1).

Proof By induction on n. For n--1 the proof is trivial in each case. For the
induction step we have:

top' o r ( H , m, n + 1) = top' o push(acc( H, m + n ), r( H, m, n))


top' o r ( H , m, n)
a c c ( H , m)
pop' o r ( H , m, n + 1) -- pop' o push(acc(H, re+n), r( H, m, n))
push(acc(H, m + n), p o p ' r ( H , m, n))
push(acc(H, re+n), r( H, m+ 1, n - l ) )
r(H,m+l,n).

Using this lemma, it is immediate that T(r)o q---dq o r.


Algebraic Specification of Data Types 135

Our second main example is the representation of forests by binary trees. As


pointed out in Corollary 2 of Section 4, BtreeA = ForA. This is not sufficient for
the representation (= implementation), since it does not indicate how the opera-
tions of ForA are to be implemented. The deficiency can be repaired with the
aid of the following easy lemma:

Lemma 4. Let D = Z *~- F Z ~-- Z be a simple data type, r: Z'--->Z an isomor-

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:

Example 2. Representation of forests by binary trees. Take the defining pair


for forests and trees as

X~2+ZxX
Z~--AxX

with initial solution (cf: 2 +TreeA x ForA--~ForA, ct: A X ForA---LTreeA). In the


notation_of Theorem 4.2,G x is now X Z . A x X , Z _ is A X . A × X , F is A X . 2 + A X
X X X , X is BtreeA, and Z is A x BtreeA. We take (p, q) as (cf, ct). Then ~cx. is
1Ax VorA and we see that ~ =q ( = ct). So a, the desired representation function, is
given by the diagram

cb
BtreeA <...- 2 + A x BtreeA x BtreeA
$~(~)
el(12 + ct X ]Fo,A)
ForA ~-- 2+AXFor A×ForA

This gives the following recursive definition for r (=a):

r ( x ) = if empty(x) then /'x


else cf(ct(root(x), r o It(x)), r o rt(x)).

Notice that this argument is sufficiently abstract that it yields further


representation theorems at no extra cost. In particular, consider the following
pair of equations for domains of (finite and) infinite forests and trees:

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.

7. Advice to Language Designers

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

h a n d . T h e r e s p o n s i b i l i t y for c h e c k i n g that the p r o p o s e d i m p l e m e n t a t i o n is


i n d e e d a h o m o m o r p h i s m c o u l d be either left to the user, o r the c o m p i l e r c o u l d
b e a s k e d to check a p r o o f of t h a t fact given b y the user.

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

42. A. van Wijngaarden, B. J. Mailloux, J. E. L. Peck, C. H. A. Koster, M. Sintzoff, C. H. Lindsey,


L. G. L. T. Meertens, and R. G. Fisker, Revised report on the Algorithmic Language ALGOL
68, Acta Informatica 5, 1-236 (1975).
43. J. McCarthy, A basis for a mathematical theory of computation, in Computer Programming
and Formal Systems (P. Braffort and D. Hirschberg, eds.), North-Holland, Amsterdam (1963).

Received June 6, 1977; received in revised form January 22 and February 29, 1980.

You might also like