The Univensity of Warwick - Department of Computer Science

0 downloads 0 Views 3MB Size Report
Great Britain. Adoress from September L977: Department of Mathematics, University of. Southern Cal-ifornia, Univensity Pa::k, Los Angeles, California, 90007 by ...
The Univensity of

Warwick

THEORY OF COMPUTATION NO.19

REPORT

TI

ATA

TVPES

BY

JInTIEL.J,LEHMANN AND

Departrnent

of

Computen Science

Unjversity of

Wanwick

COVENTRY CV4 7AL ENGLAND.

11

TcHAEL

B,St',t

yrH

DATA,TYPES

by

Daniel ,J. Lehman#and Michaef B.

Department

of

University of

Smlrth

Computen Science War.wick

Coventry, West Midlands, CV4 7AL

Great Britain

Adoress from September L977: Department

of

Mathematics, University of

Southern Cal-ifornia, Univensity Pa::k, Los Angeles,

California,

90007

Abstract

A Mathematical interpnetation Ttre main noveltSr

is given to the notion of a data .tJrpe.

is in the genenality of the nathematical treattnent

which allows procedural data't5pes and cincular"ry defined data tlpes. What

is

rneant

by data type is p:rettxr close to what any corputen

scientist would r:ndenstand by this mode, clustenr

cl-ass.

terrn or by data stnuctune, tJlpe,

The mathennatical trreatment

is the conjunction

of the ideas of D. Scott on the solution of domain equations (Scott (71), (72) and (76)) and the initiality properry noticed by the ADJ gnoup (ADI

(75),

to the data types of

ADrl

to

ADJ

(77')).

pnoposed

The present wonk adds openations

by Scott and generalizes the data tlpes

procedur,al types and anbitrary ci:rcurar tJpe

The advantages

definitions,

of a mathematical interp:retation of data

tjrpes are

those of mathematical semantics i:: genenal : throwing ]ight on sore

iIl-rndenstood constnucts in h-iglr-leveI pnognarrning languages, easing

the task of wr.iting conr€ct

pnograms and naking possible pr"oofs

cornectness fon pnograns on inplernentations"

'l

of

I" Introduction Ali

pr"ogramming languages have

some have

few,

sorne

comrnnly used ane

have only one basic

: booleans, integers,

Iabe1s, atomso lists. openations

basic data types,

some have many,

data'type.

The most

r:ea1s, procedures,

They generally come equipped

with

sorne

: logical openations fon booleans, a::ithmetical

operations fon intege:rs and reaIs, compositionn evealuation

and

abstnaction for" pnocedunes, a defining facility fon labels,

and

list-rnanipulation pnimitives fon iists.

fn

most data..qpes the user. has

the facitity to denote new objects

by the use of expnessions combining o1d objects by openationso Ai-l these facilities are easy to undenstand. The fi::st non-

tr"ivial fact about

data. B(pes

in centain prograrming

is that, in ce::tain data types

languagesn objects can be defined

and

implicitly;

they ane defined by e:pnessions that contain thein own denotation" This is the facility which is gener"ally ::efe::ned to as |tnecu::sive

definitionf' but which we prefen to cail "circu.l-ar definitiont'. This facility is generally offened for pnoceduna.l- data types only. ln the most advanced

ianguages (caIled extensible) the user

may

define new data ty-pes fnom old ones by the use of eonstnuctol"s. In

68, for example, these constnuctors are gq]}g!, !!!!r ref row, fp$. New ope::ations on data'Wpes may also be defined by

ALGOL

3

defi.ning new procedures.

z.

In

some languages,

like

ALGOL

68, new data types

may

also

be

defined cir"cula::Iy" The following two definitions a::e examples

of such cincula:: definitions in ALGOL 68: ( iE Iabel, nef t.ree. 1eft, € mo.g g = gg! moi.e fun = puoc ( frn ) firn A11 languages known -

to the authors that

lgeg ::ight

)

al-Iow such circulan

definitions of data types put stringent ::estrictions on the genenality of such a facility.

Lehmann

circul-a:: definitions not allowed in

(77) showed that

ALGOL

many

68 are meaningful and

ve::y useful.

The question

of the mathematical

meaning

of cincula:r definitions

inside pr.ocedural data types was finst answened, independently,

by

H. Bekic, D. Pank and D. Scott, who noticed that the funetions defined wene least fi>ooints of rpnotone functionals. The fact that mode-constructons ane functors was mentioned nemank

attributed to Lawvere, and later

and Wand

( 74

been known

).

Cir^cular.

explicit by

a

Reynolds

definitions of sets and languages

had

to algebraists for" a certain time when, in 1969, Scott

gave a pnecise neaning space constructor

to solve

made

in Scott (ZZ), in

to ci::cuLar definitions involving the function-

(his arnow, the ALGOL

dornain equationso

irplicit

Bggg). A general method in Scott (72), was rnade e>glicit 68

by Relmolds. The categorical natune of this rnified construction, only hinted at by Scott, was emphasized by behind the pnesent wor:k

is that the

Wand

(74).

pnoblems involved

The main idea

in defining

oata rypes can best be handled by an exact generalization of the well-understood methods used in studying definitions of objects

within a data tjpe.

This involves genenalizing fuom posets to

categorieso fnom monotone functions to frr::rctons, and fuom least

fixpoints of continuols functions to initial fi>rpoints of continuous frnctors. The ADJ gnoup concentrated on the p::oblem of defining frnctions on data types and insisted that data types do not consist only of a set of elements, st:3uctur"ed in some way (generally a partial onden) but consist also of centain frstctions. They understood the impontance

of initiality

and noticed

rnany-sonted algebnas) but wene unable

initial algebras (on initial to include

procedr::na1 data types

the link with initiaL between our" wonk and

as fo11ows"

We

that certain data t5rpes wene

in thei:r tl:eatment and did not see

fi:3

f 'l,l ) =u n ul'l = !"1 uf cr,

L R ! ui "i ) is an amow in cpoa .

Lenma

12 : Let I be an ardiag:ram in

D

CpOa and

U : | + A_ be

that irR is a limiting then u is a colimlting eone (in

a

comnuting cone such

cone fon IR

in

cpoa).

ar-CPo*r

E_::oof : Dual of Lemma lI. Definition : Let E, be the covariant which sends each pain

enbedding

of

cpoa

in

ur-cpo*

of functions to its left par:t and

E* be the contravaniant embedding of

CpOa

in

r,l-Cpo* which

of fr:nctions to its niglrt pant. cleanly E, and E* ane the identities on objects. sends each pain

Theorem

4 : l)

CPOa

2) E, 3

is

an 0r-qatsg6rSr

pnesenves and

::eflects ar-co-Ifunits

) E* t::ansforms or-co-limits into aroP-linits ,oP-lirnits into ar-co-1imits.

Pnoof

: 1) by Lemrnas 5, ?, 9 and IL. 2) by Lernrns 8 and 9 and Lernma lL. 3) by Lernnas 8 and 10 and Leruaa

19.

12.

and neflects

The next Theonem

in

CPOa

Theonem

wilI be used to plove that frnctons (on bi-functor.s)

ane o;-functor6.

5 : If T is CPOa

x

a.n

endo-functor on

CPOa

+

cPoa) o

CpOa Ca

T presenves a,-colimits . if fon

c * *1 J every sequence = *^o J'o I':_

rl { H :

Proof

=

By Lemmas

r

}

e*

**+16CPO

: rrr'-CPO"

is a fr.rrcton T :

(r ri) L (r r.;R = 1.

a Cnon-full)

subcategory

pnesenves adjunctions

CPOa

- -'L'8 =""3'i 'i l: ""

8, 9 and 11.

CPO-, can be considened as

Tr

U

bi-fr.mctor

+ CPOa.

The

of

its restniction to

following

; if

o-CPO

theonem shows

CPOa

that

the initial T-algebna is also an initial Tr-algeb:ra. It is useful

to

d,raw mone

r.adical initiatity

pnopenties fon the data epes

ci:nculanIy defined by definitions involving only +

and x (at

the

exception of +), as ttrose eonsidered in ADJ (75).

: Let T :

Theonem 6

+

CPOa

CPOa

an a.r-fi:ncton such

be a fi.rrcton and Tr :

that

ErT =

ar-CPo?t+ ar-CPO*

TrE, then T is

an

if 6 is the initial T-algebra then El,0 is the initial Tr-algebna. : T t is an 4v*fplsfoD by hypothesi.s and E, is an arfi:ncton ar-frmcton and

Pnoof

4 =) TtE, = ELT is an ar-fi:ncton. But by Theonen 4 E, r.eflects ercslimits and then T is an r^r-firrctor" Theonem I by

Theonem

assents the existence pnoof

of

Theorem

of g :

TA

+ A the initial T-algebna.

The

1 shows that { is the r:nique arnow in cpoa sueh t}rat

d o Tti = pi*1 fon p : i + A the col_imiting

r 3 r?tTr Tt+;oo. *Tir*..

tttrr.

20.

Ti+1r

cone

fon I

By Theonem

4, E, pnesenves arcolimits and

colimiting cone.

Because

the initial

Ei,!

object in

initial in ar-cPO'!, E,t is the following

Erf i r ,* Ttrh..r? l'', * .... * t'i : -TtL t *TtL

diagnam

of

Theor"em

cPOa

in

isa

is also *

aJ-CPO

:

,ri+1 * "...

-.

lrtrrr,

The pnoof

:Erf+A

1 shows that the (up to isomonphism) initial

is the r:nique arrow 0 : Vie/[ { o TtELIi = Etui*:. Tr-algebna

But Elui+t = EL 0 o E, T u.

TtA + A

of

ar-CPO*

such that

= EL 0 o TfEru.

and ,t=Et0. tr

Remank

:

Ma::kowskyts (7+) r'esu1t on

* the cocompleteness of CPC is

not used. Cinculanly defineo data.types the definition of invol-ves + (r:nion), " (E;ggg) and * (9,)

initial algebras in Lehmann

CPOao

the subcategory of

checked

domain

CPOa whose

whose arrows are pains

ALGOL

68).

and

can be

cLosed under o-col-imits.

definition of data tlpes

and neduces the

solution of comain equations to least upper bornds. Plotkin Snryth have necently shown

be

Scottts

objects a:re continuous lattices

of A-continuous pr.ojections. It

anoth-en

to

equations consisted of considening

that this subcategory is

Scott (ZO) proposes

can be seen

will be expiained in the sequel (see

tZZ) for a preview oriented towards

original (ZZ) solution to

easily

as

which

that these l.u.brs

al.e r,r-col-inits

and

in

a

suitable category of adjr:nctions which is equivalent to the subcategory

of

CPOa

considened oniginalJ.y by Scott (72)"

21.

Ex€inple Lehmann

5 : Dom and a mone genenal notion of a data-t54pe. (76) defined a catego.r'y Don the objects of wh-ich ane

orcategories and the arnows of which a:re adjr.rnctions with identiry

nnit.

Dan Ls an ar-category

ar-frtrctor.s.

and X, *, + and p can be defined to

is a furl subcategony of Dom closed unden a.rco-lirnits, X, tr and + . The eornespondents of rheonerns 4 be

cPoa

and 5 hold and nelate Dorn and

and,

stnict ar-functons. bm

ar-Cat, the category

provLdes

of

ar-categonies

a more genenal notion of

a

data typen useful when the powerset constnuctor, ou non-deterministic pnocedu:raI Qpes are allowed.

we

shall

now proceed

ci::cu1a:r type

to give a nurnben of

examples showing how

definitions do indeed define initial algebras.

The

algebnaic aspects will be stnessed : a cincular t5rpe definition

not only define a pantiari-y

ondened

set but also

sorne

fi:nctions

does on

this set.

Exanrpfe

6:

Ttre

natr:ral

numbers.

naturel numbens, or even the integens, are generally thought to be a basic data'tJpe. I{e shall now demonstrate how they can be T'l:e

cir"culanly defined" our treatment is equivarent to Lawverers (64), as reponted in Maclane-Binkhoff (62) (pp. 62-70).

Let oul: r:nder.lying category be Set, I be the one-point set (it is a terminal object in Set and aLso a generaton) , r the .;nique eiement

of J, + be the co-pnoduct (disjoint urion) and

22.

iet

T be the functor defined

if

e isaset

if

f :

"1* "2

by

:

Te:J+e is a function Tf :

Tea+ Te,

is the fu:action defined hy (Tf)(u) = ft if a e ea r if aeJ In category theonetic notation Tf = I, 1 5 Clea::Iy T is a fr.:nctots : Set + Set. It is indeed the firrcton type Tt = { OrS } nank (O) = O, nank (s) = 1 descnibed afte:: Definition 6" T is an orfuncton, as all firnctons associated with sinilanity tlrpes, as explained associated with the simiLanity

in

E>hism,

the fr:nction

zeno anci ne/l/

to n + 1 = suc(n).

Lemma

O

is an initial

T-algeb::a,

property of g cha::acterizes, up to

+ suc z 1+ N + N which

gsnds,

lef to

1 : 0 + suc : 1 + N + N is the initial T-algebra.

Proof :Suppose {:J+B+B Suppose q, : y'/ + B. If (f) o " (O + suc) = 0 o (Ir+ cr) then (z) a (o) = ( o o (0 + suc)) (r) = ( 0 "(tr+ a)) (r) = 6 (r) (S) o (n + 1) = ( o o(O + suc))(n) = ( O .(rr+ a)(n) = {(o(n)) Conversely if o ve::ifies (Z) and (3) suc)(r) = c (o) = 6 (r) u o(,O + suc)(n) = cr (n + 1) = 0(cr(n)) u o(0 + suc) = 0 o(Ir+ c). c

o(O +

23.

and

By

the induction nule fon the natuml

nr-unbens

frnction a : il + B ver:ifying (2) and (3)

thene exists exactly

and

this

pnoves

one

the initiatity

ofO+suc:L+N+fl. The

point we want to

make hene

defined by : tr = 1 + N or in union (t"fr, natunal).

The

is tiut the tlpe /[

ALGOL

68

can-

be ci:ncularly

notation mode natrr:ral =

initial ffurpoint involves not only

the

set fl but also the constant 0 and the function successor" The induction ::ule fon natural nunbens which is vital" fon pr:oving ptopenties of

prog:rams

manipulating natural numbens is nothing else than an initial-iry propenty.

fn other wo::ds the fact that the rnique o : // + B inplied by

Theor"em 1

is a total fturction is the main tool in prrcving that centain fi:nctions are total-, in contr"adistinction with the more general pantial fi:nctions which may be defined cincularly continuous rirnctionals

Fo:: othen

inside

[

/il

+ B] by means of arbitnar:y

: [ff + BI +:[/t/ + B] "

cincurarly defined data types the initiality

pnopenty may often

be used Cirectly in place of an ihclrlction principle. fn othen cases it is

the main tool in pnoving the connectness of an induction principle. question of the exact relation between initielity tro:f

pd in

Soni-r'nn

Lemma

1.

nr.unbens may

The usual

r:.'' be neadil-y transfonrned to fit this if m=0 n+m=r suc(n + mr) if may be

will

be

q

A-1 useful fr:nctions on natu::a1

with the help of

and induction

The

obtained the following way.

24.

be defined f::om 0 + suc

definition of addition for fr^amework.

m = suc(mr)

example

Let T = trf cr: * f) and u: T(Iffdlll).+ by : ,lt = TN + lf (suc o f). Then thene

[N+N]be defined

is a unique u : d *t il+ryl :naking the foll-owing

diagnam comnste.

N t$t J.+N

i" ! r.,.'[,

rr*o

[ff*fl] S L+[N+Nl The commutation

of the

above diag::am

is equivalent to

a(O) = If

and a(suc n)

a(O)(m) = m

and c(suc n)(m) =

=

:

Suc o cCn)

Suc (a(n)(m)).

o is the addition" More genenally any

function defjned by a pnimitive necunsive

is the unique annor{ rnaking a sfunilan

diag::arn

scheme

commute" Ce::tain such

diagnams, howeven, define functions which a::e not pnimitive necursive"

Theonem

2 assents that 0 + suc is an {g.omonphismrits invense is obviously

nuii*pr"ed:

N+7tN

defineciby (nutt+pned) (O) =rand

hull- + pned) (n+1) : The p::oof

As a

of

fheorem 2 shows how

to define pned in terms of

further example we shaII show that

.v may be defined as

n

suc.

even the equal_ity pnedicate on

the rnique anrow f:rom an initiar algebna.

25.

= I + I/ + fl + S. The initial T-algebra is 0 : f + N + N + N x /y' = (J.+N)(I+W1 + d x lV defined by d = Co+suc) r(olsuc).

Let

TS

Let 3= {tr:ue, false} andrl,: J +N+ /[+B+Bbe definedby 0(r) = tnue, $(nr) = OCnZ) : false and 4(b) = b" (o+suc)xCo*suc)

NxN

7+N+N+NxN

I

I

I

I

I Ir+ Irr+ I,,+ c

lrwtv

I

+

lL

b

L+N+N+B

a is the r:nique o(OrO) =

ar.tsow

: I x il + B

such that:

tnue, c(n+1rO): c(0rm+1) = false and

c(n+1rm+1)

= c(nrm).

a is the equality pr.edicate.

Examole

7 : Context-fnee

languages.

All- l-east fi:points methods previously

special cases of the presented

mone gene::al

rr^sed

in

Computen Science ane

category-theo::etic initial

fixpoints

hereo In partieular the charaetenization of context free

languages as

least solutions of a set of equations

can

be ca:rried

th:rough.

Let ) be an alphabet (not necessarily finite), then P(x*), ttrc set of aII languages oven X, ondened by inelusion, standand methods, an 0r-categor..yo Let

is a complete lattice and, by

V* be an alphabet of non-terrninals

(not necessanily finite) and p be a fi:nction : V* -r E where E is the set a-:. expr"essions built fnorn V*, f,, concatenation and uniono 0n P(ft') concatenation and i:nion ar"e additive Cthey pneserve arbiilrany 1.u.bts)

zo.

and so they ce::tainly ar?e &r-. fr:nctors

I rt lVnt T : P(f")' "l * Pcf,")r N' ,',

lVu

I

and the

i,, r - A of subsets of PCI -. rt-) lv*l-ulle

Exarnple

8 : Context-fnee

p clear"ry defines

an al-fwrctor

:

initial T-algebra TA c A is the 1east

such

that

TA

c A.

By theonem 2 TA = d.

g?ammars.

A much mone intenesting example concerfns context-free grarmars (as opposed

to languages). A context-fnee

can be viewed as an rrr-endo-furcton

to

example T above, but when

granman

with n non-terminals

T : setn + Setn, in a manne:: similar:

u (of sr$sets of r't) i"

nepLaced

by

+

(aisjoint union) and . (concatenation of subsets of x't) by r (pnoduct). Fo:: example

the context-fnee

gramrnar

: s + alasafassal

can be ]-ooked

at as the functon : T : ),S. {a} + ta} x S x' {a} +' {a} x S x S r' {a} The initial T-algebna g : TA + A consists of a set A isomorphic to {a} + ta} * A x' {a} +' {a} x A x e x' {a}, venifying the initiality property. A is isomorphic to the set of all parse treeso $ constructs and 0-1 decomposes parse trees. Note her.e that the fuaction fnontier : fr. : A * f,* which assigns to each parse tnee the

panse tnees

wond generated

is not one-to-one

multiplicity of

w.

27.

and fo:r wexft

: lrr-1{w)l is

the

3.

Data types as aLgebnas

we now want

to

make

our thesis pnecise: a data type is an object in

a suitable category of domains but

Dom

(CpOa wil-l- do

or othen categonies could be

for alL applications

considened) equipped

hene

with centain

operations.

Def.initionJ: A type t consists of a natural numben n>o and n pai::s of functons (SirT.), SirTi , CPOa + CpOa, i=lr...rn Definition 2: A data type D of type t:(nrS. rT. ) consists of an or-CPO D (with light notational ambiguity) and n (6-continuous)

fr:nctions d. 'l- :

S.D l_

+ T.D. t_

rn practicat applications the functors s. and r. used are always "polynomial'r (ttrey are built f:rom pnoducts and sums onry) and indeed

a data type is a domain equipped with a finite

numben

of functions.

Definijion 3: A homonorphism from D, to D, of type t is a fr.nction f : Dl * D2 such that the following diagr.am commute: o-.1 -+ S,D. tl-

s.r 11

J

"i"2

T.Dl_l_

i r.r . 2 'i"2 I

i,

9i

wirl be used in section 6 to study inplernentations. For the moment just notice that we have defined a categony of Homomorphisms

.iata types of type t.

28.

?asic Sata_jg>gs The::e

is no neal pnobrem in defining the basic data types of

pnog::amming languages

The data CPO

t]pe

boorean

as objects in

for

CPOa

usual

equipped lr-ith cer:tain openations.

example wouLd werl be rrrde:rstood as the

Bool repnesented below:

ffue false ^u, / Bool

with the constant tnue, the unary openation (-true=faIse, -g"1se=tnue, ;I=I) and the binary ope:ration

equipped

v

(rv.L=1v1pue=truevl-=falsevl=lvfalse=I, ttueVtrue=tr:uevfalse=falsevtrue=true,

falsevfalse=false ). A11 othe:: connectives: Tha nrrac+-lnn ^nr1ld u

De rrue i:-nsteaci

Ar+r$ etc..e

be'aised I qfesu whether nrls Lr.cl. !s

of r).

cED

be defined from the above thnee"

we wq: shourd DlrLrulq

not ItuL (lgJ.J.lle define rvtnue IvIrue to

This latten pnoposal is conceivable but would

contraci-ct the operational meaning that some want to give to i

J_

a non-terminating computation ).

of the general-ity of cincuia:: definitions of data types and the extended facilities available to define functions on data types we think that it is possibre (though we in no way suggest that it should be the Seeause

case

in

pnognamming languages)

to

use only a very few basic data qpes:

7 the one point domain, and 2 the two points domain for: exanpre. .T

I

.I

.I 2

29.

rgr

Type-constnuctors As explained

the

1n

Section 3 a Qqpe-constnucton is an ar-endo-fi:nctor in

catego::y of domains' we

shalI list

of the

below some

most interesting

ones.

@ : cPOaxcPOa+cPOa, cornesponds

to the categorical pnoduct in

ur:cpo't

if II is the categonical product in or-cpor' ). If A and B ane cpots AEB consists of all pains of objectso the fir"st one in A, the (E- oQ=1o(ErxEr)

second one

in

in

cpoa then

If f:A+Ar

B ondened componentwise.

f6g=(rlngl, fnt*l

and g:&+Bt are arrolrs

for (aIIb)(xry)=(ax,by).

o-functon is easily checked with the herp of

Theonem

That @ is an

Sr,and so the

o-continuity of all fr:nctons to be defined now. There ane sone obvious arrows attached to @: P],@B*A, P2:A6B+B. A

srightly differ:ent

pnoduet

will- be needed for^ defining stacks and 1ists.

x:

cPoarcPOa*cPoa conresponds

(the

anr"ows

to the catego::icaI product in

ur-cpo**

are very stnict o-continuous frnctions, those functions which

to bottom only the bottom element). If A and B are cpors then AxB consists of only those couples (arb) with aeA and beB such that u*tA arrd

send

b*r, oo a=IO

anci

b=rB It is easily

g:b'tBt are annows in

CPOa

seen

to be a cpo. If

then fxg is the nestniction of fgg to AxB.

This non-standand pr"oduct has al-neady been used by M.

thesis. The obvious

+:

cPoaxcPoa->cPOa. J.r.

in i,r-cPO .

A+B

f:A+A? and

Gondon

in his

anrows pa:AxB+A and pr:AxB+B nay be defined.

conresponds

to the categonicar

is the coalesced

sum

of

ir:B+A+B and d:A+B+Bool_ may be defined.

an

A and

B.

sum

in

rrr-cpo*, and

Arrows ir:A+A+B,

Q: cPOaxcPOa+ cPoa is the separated sum. itri2 and d may be defined as above.

+:

CPOaxcPOa+ cPOa

is the fi:ncton space functo::.

+(AnB)

is

[A-+B]

in

oun notation.

App: [A-+B]QA+B, Abst: [AxB+C]-+[fi+[l+f ]l and Y: [a*A]+A may be defined. /ts was mentioned above a power constnuctor P, or more precisely a

number"

such constnuctor.s have been studied; they

pnesent work.

The nexl pa::agnaph exemplifies cincula::

will not be used in the

of

definitions of data types. Fon example

it will be sho'nrn how, given a data type A, it is possible to define the

data

type Stacks of A (StackA). It is only in the next section that it will be shown how these

definitions

amount

to

rnaking Stack

a

t5ape

constructor.

C!ncu1a.r1y defined data tfpes Thi.s panagnaph, and

is to

show

that

the next one, pnoceed uniguely by examples. Their

many usual data types ane indeed defined

circularly,

purpose

and that

cinculan definitions define not only a centain domain but also centain operations on

it.

i) Simple data types Those are anci

the data types built

f::om

basic data types by type constnuctons

cinculan definitions.

Natunal

In the

numbens

preceCing section the natunal numbers were defined circula::Iy as

initial T-algebna for" a fr:nctor T on Set. For see-.is pnefenable CFOa. By

to define

them as an

initial

computen science purposes T-a1gebr"a for-

an

it

a functor T on

analory with the case of Set one coulpect: one bottom element and atl finite non-empty binary tnees

the night leaves of which ane uniabetled (they nepr"esent empty iists)

and

the lef-t leaves of which are eithen r:rrlabelled or. label1ed by a non-bottom element

of A. If the tnee consists of only

one 1eaf

it has to be considered

a r ight leaf.

ExampJ-es:

() the empty list

. . - /\

(a)

q.

.

(o)

./l (a' (b) ) = (a'b

' - /\ o' i, b.': /\/\

it /.'\

)

(a).(b)) = ((a), l)

a";'

ci:

2+(A+LispA)xlispA+LispA

cl(r)=.r_ ,

is

cl(r)=O,

such that: ci(a,,Q,)=(a.4)

2+(A+LispA ) xl,ispA=2+ (Rx1,1stA+LispAxLispA ) =( 2+AxlispA )rLispAxlispA

which

is obviously desinable.

9ina:rv tgees Labei-ied binary trees suggest the following

definition:

BtreeA = A+AxBtreeAxBtneeA. The ::eader may car:e

to

check

that the initial- algebra consists of

aii flnite binary trees a1l- the nodes of which el-ements

of A,

and one bottom

element.

(cb : A+AxBtreeAx!13s.A'+BtneeA) and tnees.

ane iabelled by non-bottom

The annows obtained construct

decompose ("b-1:Bt::eeA+A+AxBtrreeAxBtreeA)

Had we chosen

on

a

to solve T = A+AgIdI

domain containing

Tr"e.e-s

finite

and

we would. have defined. an

infinite taberled binary

initiat

algeb::a

tnees.

and_E€stF

A iabell-ed tree consists eithen node and

of a single labelled

A+AxF

I=

z+TxF

of a labelled

a fonest. A for:est is eithen empty or" consists of a tnee and

fo:est. ?rees ancj forests are defined by the pair: of T=

node on

a

equations:

It will be shown in Section 4 that forests could equivalently be defined as stacks of tnees , after having defined tr€es as either a single labelled node

or a labelled

defineo by:

node and a stack

E

of t::ees. In other

wonds T and F may be

A+A Stack(T)

F= Stack(T) whene the mUtUallrr nr'n^,r'i2n rt-€inition

'r Composite- data twes v ,i.y data types are not simply

haS been eliminated.

2

of t5pe constructons

built up f::om basic data Wpes with the

and ci::cular

definitions but defined,

by -'irst defining a sirnple data type and then type and possiSly for^getting composite data t5pe

which

some

will be the

some new

indir:ectly,

fgrrctions on this data

of the oid finctions.

sane as

somewhat

help

The canr.ier

of

that of the simple data type

it is built but the operations will be diffenent.

T\uo examples

a

from

will

be analysed he::e: Queues and Ar::ays. Qqerres-

Qr,o,res a:re

of the

same

type as Stacks but the push and pop pnocedunes intenact

36.

in a diffe::ent

in the sense that the

way

fi::st

which has been pushed on

erement popped

(and not the

last

one as

alre a ve::y useful and intenesting data t5rpe which seems

attention of p::evious nesearchers.

The obvious idea

witt be the

one

in Stacks). Queues to

have escaped the

is to define

Queues firom

Stacks by defining new pop and top fr:nctions and then forgetting about the

old ones. Stacks of A wene defined as two inrrense functions:

cs:

the initial algebna

2+AxStackA+StackA

cs-f:

and

StackA-+Z+AxStackA the empty+topxpop arnow.

define dq: StackA+2+AxStackA by:

We may

dq(s): if empty(s) or empty(pop(s)) ttren push(top(s The above

),

p2(dq(pop(s

)))

to

ichain in [C-+C] wrth col-imit r:Q-rF, . g = FO-j9"F1-:l-r Tne

translation of cr by s is the following infinite r*_______)

diagram:

i_r

l

1*_ Jrv*

t

"!

,'l!

vv

- ",-1 |

UUI

,

KK'

v

t-qni !'n€ I '^'^i

ittr

d?

i

F:- ('TLi...

Fnf ' 4'4 rl

oi.l \" !t/,

rirr;-=-\ij-

nnr KKI v I

!

i

'

i

nn+

'rt'?ut'L,

v t

i

Tr together with the fact ihat colimits in [C-tC] are computed ^::.::tra:.;e j t,r're coiimits of the rows are as indicated in the diagran. Let

By

Leruma

us ....iove ihal- the arrows q;{"

mediate between the successive conest

:is :.:dicr,.teo. In tre iirst piace, L ithe uniilue arrow from -l to Frirnecj-a;es triviaii-y. Nos, for any n ) 1' k Z O' the foilowing

=

is the carrier (codomain) of an initial solution. Then, interchanging the roles of XrZ, we trave (inp notation syrametrical to that used previously,

whose

interpretation should be clear): F, is

AX.?,,

4:tz'> = d()z.F(7.&)rl(,\z.r(z&)> is the caryier of an initia 1 solution. Since all initial algebras for 4.1G) 4'e

X" is Z, G' is ),2.t(zrz)r

Eurd

isomorphic, the result follows.

Corollary 2. BtreeA i ForA .

;|. Apply Corollary 1, with F(X'Z) as 2 + A xX x The mere existence

of an isomorphism

itseif a ver)r interesting result. closer inspecti.on reveals

between BtreeA and ForA

However, as we

much more

Z.

shall

see

in Section 5,

a

than this: it gives us the "representation

of forests by binary tree6rr. Likewise for: Corollary>)

= d. TI c((a, ). Since the back square and the bottom rectan,qie commute, h, dq is an arrow from cso

to d*. since the front

arl affow from cso

resul-t is proved.

square and top rectangre commute, e*

to d{. By initiality

o is

of cso these arrows are equal;

the

5" Inpiernentation of

crata types'

to prove that certa:.i: openations on data type D' iype DN enabLe him to sj-rnulater represent or implemer't those of a data Typioally, D will have been defined'eabstractlyrr, say by a circular defj'nit'ion' sornetlmes one wants t,o be abie

prograrnming while D,is morerrconcretet'(rnore like what is usually availabl-e in cited as Ianguages). The:.epresentation of stacks and queues by arrays may be

that exanples. An int,eresting exanple that does not quite fit this pattern is of the representation of forests by binary trees. Here the representation

involves, as it were' a reduction of rrstar-height't: in the definition of the Btrees data type Forests there occurs already a paraneterized data type' while

is sefined via a purely polynonial functor (see Sec.3t ot

end

of Sec'4)'

These exanples are weli-known, an