fication method, using parameterized specifications and algebraic domain ... The main result is that algebraic domain equations always have an initial solution.
F. Gecseg, editor, Proc. FCT’81, LNCS 117, pages 120–129, Berlin, 1981. Springer–Verlag Specifying algebraic data types by domain equations
H.-D. Ehrich Abtolnformatik,Univ. 9ortmund, PF 500500,9-4600 Oortmund 50
ABSTRACT - The paper nrovides the theoretical foundation f o r a new algebraic specif i c a t i o n method, using parameterized specifications and algebraic domain equations, an algebraic analogon to the domain equations used in Scott's theory of data types. The main r e s u l t is that algebraic domain equations always have an i n i t i a l solution. Also, a parametric version of algebraic domain equations is investigated. In e i t h e r case, there is a simple syntactic solution method.
1. INTRODUCTION Scott's order theoretic approach to data types (SC 71 f f ) offers a characteristic s p e c i f i c a t i o n method: there are given basic types l i k e BOOL, INT, e t c . , and there is a fixed set of type constructors l i k e sum, product, function space, and mowerdomain. Data types are then defined by so-called domain equations that may be recursive~ e.g. ( c f . SC 72a, SP 77) N ~ N+I (natural numbers), L~(L~L)+D ( l i s t structures over D), F~[F--~F]
(a pure L-calculus model), S~S~O+I (stacks over D),
etc. In contrast, the algebraic approach to data types has hardly more to o f f e r than e x p l i c i t specifications: one has to give a l l sorts, a l l operations, and a l l axioms that describe the behaviour of the operations ( c f . ADJ 78). Some caution is required when comparing the order-theoretic and algebraic approaches to data types, since they do not model the same aspects and serve somewhat d i f f e r e n t purposes. I t may, however, be f r u i t f u l
to apply ideas from one approach to the f u r t h e r development of
the other one. The papers of Lehmann and Smyth (LS 77) and Kanda (KA 78) represent d i f f e r e n t attempts to do t h i s . Recently, an algebraic analogon to type constructors, called parameterized data types, has been investigated (BG 77, EH 79, EL 79a+b, BG 80, EKTWW 80a+b). So, in a sense, algebraic data types can be specified by expressions involving parameterized data types applied to actual parameters. The question n a t u r a l l y arises whether a meaningful and useful analogon to recursive domain equations can be established in the algebraic framework. This paper shows that the answer is p o s i t i v e . Our approach is inspired by the categorical versions of recursive domain equations due to Wand (WA 75), Lehmann (LE 76), and Sm~tth and Plotkin (SP 77). Thus, f i x p o i n t s
121 of functors play an important role. However, we do not need ~ - c o n t i n u i t y , and we have a simple syntactic method f o r solving domain equations. The solutions are algebraic data types, i . e . many-sorted algebras with a l l t h e i r operations. 2. FUNDAMENTAL NOTIONS A signature is a quadruple E=, where S and ~ are sets of sorts and operators ', respectively, and a r i t y : ~ --->S*, sort:~
~S are mappings. We w i l l v~ite
E= f o r short, assuming t a c i t l y the existence of the a r i t y and sort ma~ings. A signature morphism f:E
> E' is a pair of mappings f= S',f~:~
~'>
such
that a r i t y ( ~ f ) = a r i t y ( ~ ) f s and sort ( e l ) = s o r t ( ~ ) f s. For convenience, we often omit the index, w r i t i n g f f o r fs or f . Algebras are interpretations of signatures: a z-algebra A is an S-indexed family of sets, {SA}, the c a r r i e r of A, together with an ~-indexed family of mappings, { ~ A : a r i t y ( ~ ) A - - - - ~ s o r t ( ~ ) A } , the operations of A ( i f x=sls2...SnES * , x A denotes the cartesian product S1,A~...XSn,A}. A E-algebra morDhism m:A--~B is an S-indexed family of mappings @s:SA---~SB such t h a t , for each operator eE~ with a r i t y x and sort s, we have ~Ams =mx~B. Here, mx=mslx...XmSn
i f x=s1...s n. The class of a l l
z-algebras with a l l z-algebra morphisms forms a category z-alg. I t is well known that ~-alg has an i n i t i a l
algebra I~, having a unique morphism to any other algebra
in E-alg. If f:z---*E'
is a signature morphism, there is a corresponding forgetful functor
f - a l g : E ' - a l g -~->E-alg sending each ~'-algebra B to that E-algebra A such that SA=(Sf)B and ~A=(~f)B . LetE = be a signature . A ~-e~uation is a t r i p l e Xi
~i:XixXi---*B00L
P: ~ P : X I ~ . . . x X n ..... >p _(i)
: P ~X i : P~P --*B00L
eqs
x i ~ i xi = t r u e
p(i) = [i ( i ) = x i ~ p = true ~ = f a l s e ~ p = f a l s e
~ = Xl~X I . . . Xn~Xn
t24 Consider ( p , p - f r e e ) f o r n=2. Let D be a s p e c i f i c a t i o n of the integers (sort INT) and booleans (sort BOOL). Let f:X---~D be defined by XI~--~BOOL, X2~--~INT, XlW--~false, x2~---~O, mi~--~identity on BOOL or INT, r e s p e c t i v e l y , i=1,2. Then, the i n i t i a l boolean-integer algebra (D_-algebra) is sent to the algebra of ( b o o l e a n , i n t e g e r ) - p a i r s with one a d d i t i o n a l constant, a l l other operations of XP, and a l l boolean and i n t e ger operations on the components retained. The s p e c i f i c a t i o n of t h i s algebra is DP_P, the pushout object of p and f .
I t is obtained from XP by s u b s t i t u t i n g BOOL f o r X1,
false f o r X l ' INT f o r X2, 0 f o r x2,and the resoective i d e n t i t i e s f o r mi" Therefore, a suggestive notation f o r DP is BOOL×INT + 1. 3. ALGEBRAIC DOrIAIN EQUATIONS In the order t h e o r e t i c approach to data types, marameterized data types can be viewed as f u n c t o r s , domain equations as endofunctors, and t h e i r solutions as f i x p o i n t s of functors (WA 75, LE 76, SP 77). In the algebraic approach, parameterized data tvpes are e s s e n t i a l l y functors, too, but most often they are not endofunctors. T y p i c a l l y , the signatures of actual ~arameter and r e s u l t a n t algebras are d i f f e r e n t .
In order to get endofunctors, we define
algebraic domain equations to consist of a parameterized data type and a functor in the reverse d i r e c t i o n . We r e s t r i c t ourselves to free and strongly o e r s i s t e n t oarameterizations of the form ( p , p - f r e e ) and to algebraic reverse functors of the form e-alg f o r some spec morphism e. D e f i n i t i o n 3.1: An algebraic domain equation is a oair of smec morohisms (p,e)~ p,e:X---eXP, such that o is a s t r o n g l y p e r s i s t e n t parameterized s p e c i f i c a t i o n . Let P=p-free, P=p-alg, and E=e-alg. There are two endofunctors, namely PE on X-alg and EP on XP-alg. A f i x p o i n t is an ob.~ect that is sent to an isomormhic one. I t is immediate to see that the f i x p o i n t s of PE and EP are very c l o s e l y related: & is a f i x p o i n t of PE i f f
AP is a f i x p o i n t of EP, and vice versa.
For the d e f i n i t i o n of what we mean by a s o l u t i o n of an algebraic domain equation, we make use of the f o l l o w i n g r e s u l t . Let (q,Q) be the coequalizer in spec of D and e, ×
- -
P
q
,
e
and l e t Q=~-alg. Theorem 3.2: I f B is a f i x p o i n t of EP, then there is a unique (up to isomoruhism) ~-algebra C such that B=CQ. I t is convenient not tO take f i x p o i n t s of PE or EP as s o l u t i o n s , but these uniquely associated Q-algebras. D e f i n i t i o n 3.3: A s o l u t i o n of an algebraic domain equation (p,e) is a Q-algebra C such that CQ is a f i x p o i n t of EP.
125 The main r e s u l t can now be stated as f o l l o w s . Theorem3.4: The i n i t i a l
Q-algebra IQ is a solution of (p,e).
The proof is rather involved and requires some more technical machinery to be developed. I t w i l l be published elsewhere. Clearly, I~ is an i n i t i a l i t is i n i t i a l
in the f u l l
solution, i.e.
subcategory of Q-alg of a l l solutions of (p,e). A s p e c i f i -
cation of IQ is Q, the coequalizer ob,iect of p and e. There is a simple construction f o r Q, given p and e, based on the coequalizer construction in set applied to sorts and operators. Example 3.5: Consider the n-product with constant from example 2.1 f o r n=l. Let X -
P
~X+I
e
-
be defined by taking p as in example 2.1, and e sending sort X1 to P, Xl to p, and ~1 to m . Then the s o l u t i o n is IQ where Q is the f o l l o w i n g s p e c i f i c a t i o n obtained from X + 1 by i d e n t i f y i n g X1 and P, Xl and p, and ml and ~. For convenience, we rename P by N, ~, by O, by succ, and ( 1 ) sorts
by pred.
N
2P#
O: --*N succ: N---~N pred: N--->N : N-N --~BOOL pred(O) = 0
eqs
pred(succ(n)) = n OmO = true O~succ(n) = f a l s e succ(n)mO = false succ(n)msucc(m) = nmm This is a s p e c i f i c a t i o n of the natural numbers. Example 3.6: Let the above s p e c i f i c a t i o n be N. Let XxN + I be the parametric smecif i c a t i o n obtained from XI~X2 + 1 (example 2.1) by parameterized parameter massing with actual parameter ( f , ~ ) , where ~ : X •
>X + N and f sends XI to X ( f o r g e t t i n g the
index i ) , X2 to N, x2 to 0, and s 2 to m. Then, the algebraic domain equation
X -
P
• XxN+l
e
has stacks as s o l u t i o n s , specified by the f o l l o w i n g s p e c i f i c a t i o n (with obvious renamings):
126
sorts
S,N
ops
empty: --~S push : S , N - - - > S pop : S - - e S top : S---->N =
: SxS ---~BOOL
o.o (ops from N) eqs
pop(empty) = empty top(empty)= 0
pop(push(s,n))= s top(push(s,n))= n ...
(eqs from N and eqs f o r ~)
In a s i m i l a r way, we get trees w i t h a natural number attached to each node as s o l u t i o n s o f the domain equation X = XxXxN + 1, where = denotes an a p p r o p r i a t e mair (p,e) of morphisms, etc. 4. PARAMETERIZEDALGEBRAIC DOHAIN EQUATIONS Our theory so f a r gives a l g e b r a i c data types as s o l u t i o n s of a l g e b r a i c domain equations.
I t is natural to ask whether we can get parameterized data types as s o l u t i o n s
of parameterized a l g e b r a i c domain equations by a s i m i l a r method o f i m p l i c i t
soecifi-
c a t i o n and s y n t a c t i c s o l u t i o n . This works indeed, i f we proceed as f o l l o w s . D e f i n i t i o n 4.1:
A parameterized a l g e b r a i c domain equation is a t r i o l e
spec morphisms,
( r ; p , e ) of
P
y
r
> XY ~
>
~ e
XYP
such t h a t (1) (p,e) is an a l g e b r a i c domain equation, (2) r is a parameterized specification,
and (3) rp=re. ( r ; p , e )
is c a l l e d s t r o n g l ~ p e r s i s t e n t i f f
r has t h i s
property. Let (f,D) be an actual parameter f o r r . Then, using the mechanism of narameter passing as defined in the l a s t s e c t i o n , we can construct an a l g e b r a i c domain equation (p',e'), y
~
the (f_,D~-instance of ( r ; p , e ) ,
P
r
XY
(i)
Ifl ~
r ~ D .......
~
> XYP
(2) p
XD
> XDP e'
Fiiigure 4.
f2
as f o l l o w s ( c f . f i g u r e 4 . 1 ) :
127
i ) Let r ' , X_DD,fI be such that (1) is a oushout. 2) Let p',XDP_,f 2 be such that (2) is a pushout wrt p,p' 3) Define e' by r ' e ' are j o i n t l y D e f i n i t i o n 4.2:
= r ' p ' and f l e ' = e f 2 .
(e' is well defined s~nce r ' and f l
s u r j e c t i v e , and we e a s i l y prove that r f l e '
= fr'e'.)
A s o l u t i o n of a parameterized algebraic domain equation ( r ; p , e ) is
a parameterized data type (s,S) such t h a t , f o r each actual marameter ( f , ~ ) of r , (s,S) sends I D via f to the i n i t i a l
solution of the (f,D)-instance ( p ' , e ' )
of (r;p,e).
Of course, r and s must have the same source Y. Our main theorem 3.4 now extends to the parametric case as f o l l o w s . Theorem 4.3: Let: ( r ; p , e ) be a strongly persistent parameterized algebraic domain equation, and l e t (q,YQ) be the coequalizer of p and e. Then, i f s=rpq, ( s , s - f r e e ) is a s o l u t i o n of ( r ; p , e ) . Again, the proof is a l i t t l e
b i t lengthy and w i l l be published elsewhere. Examples
of i m p l i c i t parameteric s p e c i f i c a t i o n s are X=X×Y + i (stacks over Y), X=X~X~Y + I (trees over Y), etc. Here, = denotes a pair (p,e) of morphisms that are defined l i k e those in example 3.6. 5. CONCLUSIONS The theoretical results presented here provide a sound and consistent semantics f o r a new algebraic s p e c i f i c a t i o n method using parameterized s p e c i f i c a t i o n s and algebraic domain equations. The f e a s i b i l i t y
and usefulness of t h i s method f o r the development
of s p e c i f i c a t i o n methods and s p e c i f i c a t i o n languages should be subject to f u r t h e r study. Another possible area of a p p l i c a t i o n is the algebraic semantics of programming languages. In denotional semantics, domain equations are used e x t e n s i v e l y to specify the syntactic and semantic domains. Our theory can provide algebraic i n t e r p r e t a t i o n s f o r them. There i s , however, one d i f f i c u l t y : example ( i n i t i a l )
algebras of f i n i t e
we get only " f i n i t a r y "
sets or f i n i t e
solutions, for
functions. The central semantic
domains of environments and states usually are f i n i t a r y ,
so there seems to be no
problem. I t i s , however, not quite clear how to cope with cases l i k e procedure parameters. In p a r t i c u l a r , we cannot obtain a model f o r x-calculus with our method, l i k e Scott's r e f l e x i v e domain (SC 72b). For these and s i m i l a r cases, an extension of our theory to continuous algebras is necessary. This is subject to f u r t h e r study.
128 REFERENCES ADJ
77
Goguen,J.A./Thatcher,J.W./Wagner,E.G./Wright,J.B.: ! n i t i a l Algebra Semantics and Continuous Algebras. Journal AC~ 24,(1977), 68-95
ADJ
78
Goguen,J.A./ Thatcher,J.W./Wagner,E.G.: An I n i t i a l Algebra Agmroach to the SDecification, Correctness, and Implementation of Abstract Data Types.Current Trends in Programming Methodology,Vol IV (R.T. Yeh,ed.).Prentice Hall, Englewood C l i f f s 1978, 80-149
BG
77
Burstali,R.M./Goguen,J.A.: Putting Theories Together to Make Specifications. Proc. 5th Int. Joint Conf. on A r t i f i c i a l Intelligence, HIT, Cambridge (Mass.), 1977
BG
80
Burstall,R.N./Goguen,J.A.: The Semantics of CLEAR, a S~ecification Language. Proc. 1979 Copenhagen Winter School on Abstract Software Specifications (D. Bj6rner,ed.). LNCS 86, Springer-Verlag, Berlin i980, 292-331
EH
79
Ehrich,Ho-D.: On The Theory of Specification, Implementation, and Parameterization of Abstract ~ata Ty~es. Bericht Nr. 82/79, Ahtlg. Lnformatik,Univ. Dortmund 1979 (also to appear in Journal ACM)
EL
79a
Ehrich,H.-D./Lohberger,V.G.: Parametric Specification of Abstract Data Types, Parameter Substitution, and Graph Replacements. Gramhs, Data Structures, Algorithms (N.Nagl/H.-J. Schneider,eds.). Applied Computer Science 13,Hanser Verlag, MUnchen 1979, 169-182
EL
79b
Ehrich,H.-D./Lohberger,V.G.: Constructing Specifications of Abstract Data Types by Renlacements. Proc. Int. Workshop on Gramh Grammars and Their Application to Computer Science and Biology iV.Claus/ H.Ehrig/G.Rozenberg,eds.).LNCS 73,Springer-Verlag,Berlin 197- ,lo0-191
EKTWW
80a
Ehrig,H./Kreowski,H.-J./Thatcher,J.W./Wagner,E.G./Hright,J.B.: Parameterized Data Types in Algebraic Specification Languages. Proc.7th ICALP (J.~.deBakker/J.van Leeuwen,eds.) LNCS 85, Springer-Verlag, Berlin 1980, 157-168
EKTI:JW
80b
Ehrig,H./Kreowski,H.-J./Thatcher,J.W./Wagner,E.G./Wright,J.B.: Parameter Passing in Algebraic Specification Languages. Internal Re~ort, FB 20 TU Berlin, 1980
KA
78
Kanda,A.: Data Types as I n i t i a l Algebras: a Unification of Scottery and ADJery. Proc. 19th FOCS 1978, 221-230
LE
76
Lehmann,D.J.: Categories for Fixpoint Semantics. Proc.7th FOCS 1976, 122-126
LS
77
Lehmann,D.J./Smyth~M.B.:Data Types. Proc 18th FOCS 1977, 7-12
SC
71
Scott,D.S.:The Lattice of Flow Diagrams.Proc.Symp. on Semantics of Algorithmic Languages (E.Engeler,ed.).LNM 188, Smringer-Verlag, Berlin 1971, 311-372
SC
72a
Scott,D.S.: Lattice Theory,Data Types and Semantics.Formal Semanctics of Algorithmic Languages (R.Rustin,ed.).Prentice Hall, Englewood C l i f f s 1972, 65-106
SC
72b
Scott,D.S.: Continuous Lattices. Toposes, Algebraic Geometry and Logic (F.H.Lawvere,ed.).LNH 274,Springer-Verlag,Berlin 1972,97-136
129 SC
76
Scott,D.S.: Data Types as Lattices. SIAM Journal of Computing 5 (1976), 522-587
SP
77
Smyth~M.B./Plotkin,G.D.: The Category-Theoretic Solution of Recursive Domain Equations. Proc. 18th FOCS 1977, 13-17
WA
75
Wand,H.: On the Recursive Specification of Data Types. Proc. 1~t Int. Coil. on Category Theory A~plied to Computation and Control (E.G. Hanes,ed.). LNCS 25, Springer-Verlag, Berlin 1975, 214-217