Primitive (Co)Recursion and Course-of-Value - CiteSeerX

15 downloads 204 Views 160KB Size Report
Varmo Vene. Inst. of Computer Science, Univ. of Tartu, J. Liivi 2, EE-2484 Tartu, Estonia ... Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically. 3. 2. .... The datatype of lists over a given set A can be represented ...... V. Vene obtained the MSc degree in computer science from the University of. Tartu ...
INFORMATICA, 1999 Vol. 10, No. 1, 1–0

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically Tarmo Uustalu Dept. of Teleinformatics, Royal Inst. of Technology, Electrum 204, SE-164 40 Kista, Sweden E-mail: [email protected]

Varmo Vene Inst. of Computer Science, Univ. of Tartu, J. Liivi 2, EE-2484 Tartu, Estonia E-mail: [email protected]

Abstract In the mainstream categorical approach to typed (total) functional programming, datatypes are modelled as initial algebras and codatatypes as terminal coalgebras. The basic function definition schemes of iteration and coiteration are modelled by constructions known as catamorphisms and anamorphisms. Primitive recursion has been captured by a construction called paramorphisms. We draw attention to the dual construction of apomorphisms, and show on examples that primitive corecursion is a useful function definition scheme. We also put forward and study two novel constructions, viz., histomorphisms and futumorphisms, that capture the powerful schemes of course-of-value iteration and its dual, respectively, and argue that even these are helpful. Key words: typed (total) functional programming, category theory, program calculation, (co)datatypes, forms of (co)recursion

1. Introduction This paper is about a well-known categorical approach to typed (total) functional programming popular in the program calculation community. In this approach, datatypes and codatatypes are modelled as initial algebras and terminal coalgebras, and iteration and coiteration are modelled by constructions known as catamorphisms and anamorphisms. Primitive recursion, which universally recognized as an important generalization of iteration, is nicely captured by Meertens’ (1992) paramorphisms. With this paper, we aim to draw attention to the dual construction of apomorphisms, described in (Vos, 95) and (Vene and Uustalu, 1998), which models an undeservedly little appreciated scheme that we like to call primitive corecursion. We also put forward and study two novel constructions, viz., histomorphisms and futumorphisms, that capture course-ofvalue iteration and its dual, respectively, and argue that even these schemes are helpful for a declaratively thinking programmer and program reasoner who loves

2

T. Uustalu and V. Vene

languages of programming and program reasoning where programs and proof s of properties of programs are easy to write and read. The tradition of categorical functional programming that we follow in this paper started off with the Bird-Meertens formalism (Bird, 1987), which is a theory of the datatype of lists. Malcolm (1990) and Fokkinga (1992), inspired by (Hagino, 1987), generalized the approach for arbitrary datatypes and codatatypes. (Geuvers, 1992) contains a thorough category-theoretic analysis of primitive recursion versus iteration and a demonstration that this readily dualizes into an analysis of primitive corecursion versus coiteration. In general, however, it appears that primitive corecursion has largely been overlooked in the theoretical literature, e.g. (Fokkinga, 1992) ignores it. Apart from (Vos, 95) and (Vene and Uustalu, 1998), the sole discussion on primitive corecursion in a programming context that we know about is the laconic report in (Vesely, 1997) on a recent not very clean extension to the categorical functional language Charity in which it is possible to define functions by primitive recursion and primitive corecursion. The most probable reason for this situation is the young age of programming with codatatypes. We are not aware of any work whatsoever on categorical constructions modelling course-of-value (co)iteration. This paper is an intermediate report of an ongoing research project on program construction for typed functional languages in which all programs are guaranteed to terminate (only total functions can be programmed). For a recent defence of the merits of typed total functional programming, see (Turner, 1995). Part of the material of the present paper originates from (Vene and Uustalu, 1998). In another earlier paper, (Uustalu and Vene, 1997), we discussed programming and program construction with (co)datatypes in a different setting of intuitionistic natural deduction. We shall proceed as follows. In section 2, we present the setting and notation. In section 3, we give a short introduction to the standard categorical approach to (co)datatypes and (co)iteration. In section 4, we introduce a categorical construction for primitive recursion and prove several laws about it. Afterwards, we dualize this development, introducing a categorical primitive corecursor and stating the laws it obeys, and show the usefulness of primitive corecursion on several examples. In section 5, we introduce a novel construction for course-of-value iteration, prove the crucial laws about it, and hint at programming examples where course-of-value iteration is useful. Again, we also dualize the whole development, arriving at a categorical treatment of the dual of course-of-value iteration. In section 6, we indicate some possible directions for further work and conclude. Proofs in this paper are carried out in the structured calculational proof style (Grundy, 1996).

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

3

2. Preliminaries Throughout the paper C is the default category, which we require to be distributive, i.e., it must have finite products (; 1) and co-products (+; 0), and the products must distribute over the coproducts. The typical example of a distributive category is Sets — the category of sets and total functions. We make use the following quite standard notation. Given two objects A, B , we write fst : A  B ! A and snd : A  B ! B for the left and right projections for the product AB . For g : C ! A and h : C ! B , h g; h i denotes the unique morphism f : C ! A  B , such that fst  f = g and snd  f = h (pairing). The notation g  h is a shorthand for h g  fst; h  snd i. The left and right injections for the coproduct A + B are denoted by inl : A ! A + B and inr : B ! A + B . For g : A ! C and h : B ! C , [ g; h ] denotes the unique morphism f : A + B ! C , such that f  inl = g and f  inr = h (case analysis). The notation g + h abbreviates [ inl  g; inr  h ]. Also, ! and ¡ denote the unique morphisms f : C ! 1 and f 0 : 0 ! C . The inverse of the canonical map [ inl  idC ; inr  idC ] : (A  C ) + (B  C ) ! (A + B )  C is denoted by distr : (A + B )  C ! (A  C ) + (B  C ), and the inverse of the canonical map [ idA  inl; idA  inr ] : (A  B )+(A  C ) ! A  (B + C ) is denoted by distl : A  (B + C ) ! (A  B ) + (A  C ). Finally, given a predicate p : A ! 1 + 1, the guard p? : A ! A + A is defined as (snd + snd)  distr  h p; idA i. 3. (Co)datatypes and (co)iteration Categorically, datatypes (of natural numbers, lists, etc.) are traditionally modelled by initial algebras and codatatypes (of conatural numbers, colists, streams, etc.) by terminal coalgebras. The function definition schemes of iteration and coiteration are modelled by constructions termed catamorphisms and anamorphisms. The theory presented in this section has become a bit of folklore; a good reference source is (Fokkinga, 1992). Initial algebras and catamorphisms Let F be a functor from C to C . An algebra with the signature F (F -algebra, for short) is a pair (A; '), where A (called the carrier) is an object and ' : F A ! A (called the structure) is a morphism in C . For any two F -algebras (A; ') and (C; ), a morphism f : A ! C is said to be a homomorphism (between F -algebras) from (A; ') to (C; ) if f  ' =  F f. It is easy to verify that the morphism composition of two homomorphisms is also a homomorphism. Moreover, for any F -algebra (A; '), the identity morphism id : A ! A is a homomorphism from (A; ') to (A; '). It follows that the F -algebras and the homomorphisms between them form a category. The category Alg (F ) is the category whose objects are the F -algebras and morphisms are the homomorphisms between them. Composition and identities are inherited from C .

4

T. Uustalu and V. Vene

A F -algebra is said to be an initial F -algebra if it is initial (as an object) in the category Alg (F ). The initial F -algebra may or may not exist. It is guaranteed to exist if F is ! -cocontinuous (i.e., it preserves the colimits of ! -chains). All polynomial functors (i.e., functors built up from products, sums, the identity functor, and constant functors) are ! -cocontinuous and, hence, the initial algebras for them exist. Given a functor F , the existence of the initial F -algebra (F; inF ) means that, for any F -algebra (C; '), there exists a unique F -homomorphism of algebras from (F; inF ) to (C; '). Following Fokkinga (1992), we denote this homomorphism by (j ' j)F , so (j ' j)F : F ! C is characterized by the universal property

f  inF

=

'F f , f

j'jF

= (

cata-C HARN

)

The type information is summarized in the following diagram:

F F Ff 

FC

inF

F /

f

' /



C

Morphisms of the form (j ' j) are called catamorphisms (derived from the Greek preposition   meaning ‘downwards’); the construction (j  j) is an iterator. E XAMPLE 1. The datatype of natural numbers can be represented as the initial algebra (N; inN ) of the functor N defined by N X = 1 + X and N f = id1 + f . Write Nat for N . The function zero : 1 ! Nat equals inN  inl, the function succ : Nat : Nat ! Nat equals inN  inr. Given any two functions c : 1 ! C and h : C ! C , the catamorphism f = (j [ c; h ] j)N : Nat ! C is the unique solution of the equation system

f  zero c ^ f  succ h  f: =

=

Given a function n : 1 ! Nat , the function add (n) : Nat ! Nat (which adds n to its argument), for instance, can be defined as the catamorphism (j [ n; succ ] j)N .

E XAMPLE 2. The datatype of lists over a given set A can be represented as the initial algebra (LA ; inLA ) of the functor LA defined by LA X = 1 + (A  X ) and LA f = id1 + (idA  f ). Denote LA by List A . The function

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

5

! List A equals inLA  inl, the function cons A  List A ! List A equals inLA  inr. Given any two functions c ! C and h A  C ! C , j c; h j LA List A ! C is the unique solution of the the catamorphism f nil

: 1

:

: 1

= ( [

:

:

] )

equation system

f  nil c ^ f  cons h  =

=

idA  f );

(

i.e., foldr (c; h) from functional programming. Given a function h : A ! B , the function map(h) : List A ! List B (which applies h to every element of the argument), for instance, can be defined as the catamorphism

j nil ; cons  h  idList A j LA :

( [

(

)] )

Catamorphisms obey several nice laws, of which the cancellation law, the reflection law (also known as the identity law) and the fusion (or, promotion) law are especially important for program calculation (the cancellation law describing a certain program execution step):

j ' j F  inF '  F j ' j F j inF j F f ' F f ) f  j'jF (

)

idF

=

= (

(

)

)

=

(

)

j jF

= (

)

cata-C ANCEL cata-R EFL cata-F USION

The reflection and fusion laws are proved as follows:

2  f ' F f 66 f  j ' j F 66 –2 cata-C HARN – 66 f  j ' j F  inF 66 66 – cata-C HARN – 66 66 f  '  F j ' j F 66 66 –– 66 66  F f F j'jF 66 64 – F functor – 66 4 F f  j'j =

2 66 66 66 64

(

idF

2 4

(

– cata-C HARN –

=

=

– F functor –

inF

)

=

inF

j inF j F

(

)

=

 F idF

)

(

)

=

(

)

=

(

(

)

F)

j jF F F ! F is an isomorphism with (

)

The important simple result that inF : the inverse (j F inF j)F : F ! F F was first recorded by Lambek (1968).

inF

 j F inF j F (

)

=

idF

^ j F inF j F  inF (

)

=

idF F

L EMMA -1

6

T. Uustalu and V. Vene

It is proved by the following two calculations:

2 j F in j  in F F F 66 – cata-C HARN – 66 F inF  F j F inF j F 66 – F functor – 66 F inF  j F inF j F 66 – above – 66 64 F idF – F functor – (

2 66 66 4

 j F inF j F

inF =

(

(

)

– cata-F USION –

)

)

=

j inF j F

(

)

(

– cata-R EFL –

=

)

=

(

)

)

=

idF

=

idF F

Define

1 in? F

j F inF j F

= (

in-inv-D EF

)

1 Lemma 1 gives us the following characterization for in? F :

inF

 in?F

1

=

idF

^ in?F  inF 1

=

idF F

in-inv-C HARN

1 E XAMPLE 3. Using in? predecessor function pred F , the 1 . be defined as [ zero ; idNat ]  in? N

:

Nat ! Nat can

Terminal coalgebras and anamorphisms We now dualize the material about initial algebras and catamorphisms. Let F be a functor from C to C . A coalgebra with the signature F (F coalgebra, for short) is a pair (A; '), where A (called the carrier) is an object and ' : A ! F A (called the structure) is a morphism in C . For any two F coalgebras (C; ) and (A; '), a morphism f : C ! A is said to be a homomorphism (between F -coalgebras) from (C; ) to (A; ') if '  f = F f  . The F -coalgebras and the homomorphisms between them form a category. The category C oAlg (F ) is the category whose objects are the F -coalgebras and morphisms are the homomorphisms between them. Composition and identities are inherited from C . A F -coalgebra is said to be a terminal F -coalgebra if it is terminal (as an object) in the category C oAlg (F ). The terminal F -coalgebra may or may not exist. It is guaranteed to exist if F is ! -continuous (i.e., it preserves the limits of ! chains). All polynomial functors (i.e., functors built up from products, sums, the identity functor, and constant functors) are ! -continuous and, hence, the terminal coalgebras for them exist. The existence of the terminal F -coalgebra (F; outF ) means that for any F -coalgebra (C; '), there exists a unique F -homomorphism of coalgebras from

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

7

C; ' to F; outF . This homomorphism is usually denoted by ' F , so ' F C ! F is characterized by the universal property

( [(

)

)]

(

)

[(

)]

:

outF

f F f ' , f =

'F

= [(

ana-C HARN

)]

The type information is summarized in the following diagram:

' / FC

C f

outF



F

/

Ff 

F F

Morphisms of the form [( ' )] are called anamorphisms (derived from the Greek preposition  meaning ‘upwards’; the name is due to Meijer), and the construction [(  )] is a coiterator. Like catamorphisms, anamorphisms enjoy various properties including the following cancellation, reflection and fusion laws:

outF idF

 ' [(

= [(

)]

F

=

F ' F ' [(

f F f ' ) =

Also, outF

:

F F ! F .

)]

outF )]F

F ! F F

1 out? F?1 = [( F outF )]F outF  outF = idF

^

[(

)]

F

f

= [(

'F )]

ana-C ANCEL ana-R EFL ana-F USION

is an isomorphism with the inverse

1 outF  out? F = idF F

[(

F outF F )]

:

out-inv-D EF out-inv-C HARN

E XAMPLE 4. The codatatype of streams over a given set A is nicely represented by the terminal coalgebra (SA ; outSA ) of the functor SA defined by SA X = A  X and SA f = idA  f . Write Stream A for SA . The functions head : Stream A ! A and tail : Stream A ! Stream A equal fst  outSA and snd  outSA , respectively. Given any two functions c : C ! A and h : C ! C , the anamorphism [( h c; h i )]SA is the unique solution f : C ! Stream A of the equation system

head  f = c ^ tail  f = f  h: The function nats : Nat ! Stream Nat , which returns the stream of all natural numbers starting with the natural number given as the argument, is the

8

T. Uustalu and V. Vene

unique solution of the equation system

head  nats = idNat ^ tail  nats = nats  succ ; and is thus definable as the anamorphism [( h idNat ; succ i )]. The function zip : Stream A  Stream A ! Stream AA that zips the argument streams together is characterized as follows:

head  zip = (fst  fst)  (outSA  outSA ) ^ tail  zip = zip  (snd  snd)  (outSA  outSA ) This function can, therefore, be defined as [(

h fst  fst; snd  snd i 

outSA

(

 outSA

) )]

SA

:

E XAMPLE 5. The codatatype of colists over a given set A can be represented as the terminal coalgebra (LA ; outLA ) of the functor LA . Write List 0A for LA . Given any function g : C ! 1 + (A  C ), the anamorphism ([ g ])LA is the unique solution f : C ! List 0A of the equation outLA f = (id1 +(idA f ))g , i.e. the function unfold (g ) from functional programming. 4. Primitive (co-)recursion Paramorphisms E XAMPLE 6. The factorial function fact : Nat ! Nat is most neatly characterized as the unique solution of the equation system

fact  zero = succ  zero ^ fact  succ = mult  h fact ; succ i: This function is not a catamorphism. The problem is that its value for a given argument depends not only on the values for the immediate subparts of the argument, but also on these immediate subparts directly. Meertens (1992) showed that any function which can be characterized similarly to the factorial function is definable as the composition of the left projection and a catamorphism. The relevant result is the following:

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

For any two morphisms f

f inF

=

:

9

F ! C and ' F C  F ! C , we have

'F h f; idF i , f

:

=

fst(j h '; inF

(

)

 F snd i j F )

L EMMA -2

It is proved by the following calculations:

2  f  in '  F h f; idF i 66 f F 66 – 66 fst–pairing h f; idF i 66 – cata-C – 66 2 h f;HARN id i  inF F 66 6 – pairing – 66 66 h f  in ; in 66 F Fi 66 – F functor – 66 66 66 66 h f  inF ; inF  F idF i 66 – pairing – 66 66 66 h f  inF ; inF  F snd  h f; idF i i 66 66 – , F functor – 66 66 h '  F h f; idF i; inF  F snd  F h f; idF i i 66 4 – pairing – 64 h '; inF  F snd i  F h f; idF i fst  j h '; inF  F snd i j F =

=

=

=

=

=

(

)

=

=

(

)

2  f fst  j h '; in  F snd i j F 66 f  inF 66 –– 66 fst  j h '; inF  F snd i j  inF 66 HARN – 66 fst–cata-C h '; in F  F snd i  F j h '; inF  F snd i j F 66 – pairing, 2x – 66 '  F h fst  j h '; inF  F snd i j F ; snd  j h '; inF  F snd i j F i 66 – USION – 66 2 , cata-F snd  h '; inF  F snd i 66 4 – pairing – 66 66 inF  F snd 66 '  F h f; j inF j F i 4 – cata-R EFL – '  F h f; idF i =

(

)

=

(

)

=

(

)

=

(

)

=

=

(

=

)

(

)

10

T. Uustalu and V. Vene

E XAMPLE 7. For the factorial function, we have that

fact  inF = [ succ  zero ; mult  h fact ; succ i ] = [ succ  zero ; mult  (idNat  succ ) ]  N h fact ; idNat i: and consequently we get the definition of factorial

fact = =

fst  (j h [ succ  zero ; mult  (idNat

 succ ; inF  N snd i j N fst  j h succ  zero ; mult  idNat  succ ; zero ; succ  snd i j N (

[

)]

(

)

)] [

]

)

To make programming and program reasoning easier, let us introduce a new construction and study its properties. For any morphism ' : F (C  F ) ! C , define the morphism hj ' jiF : F ! C by

hj ' jiF

=

fst  (j h '; inF

 F snd i j F

para-D EF

)

Morphisms of the form hj ' jiF are called paramorphisms (  — Greek preposition meaning ‘near to’, ‘at the side of’, ‘towards’; the name is due to Meertens). The construction hj  jiF is a primitive recursor. From Lemma 2, we get the characterization of paramorphisms by the following universal property:

f  inF

=

'  F h f; idF i , f hj ' jiF

para-C HARN

=

The type information is summarized in the following diagram: inF

F F F h f;idF i 

F C  F (

)

'

F /

/

f 

C

E XAMPLE 8. The paramorphism hj [ succ  zero ; mult  (idNat  succ ) ] jiN defines the factorial function. More generally, given any two functions c : 1 ! C and h : C  Nat ! C , the paramorphism f = hj [ c; h ] jiN : Nat ! C is the unique solution of the

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

11

equation system

f  zero c ^ f  succ h  h f; idNat i: =

=

The calculational properties of paramorphisms are similar to those of catamorphisms. In particular, we have the following “paramorphic” cancellation, reflection and fusion laws:

hj ' jiF  inF '  F h hj ' jiF ; idF i idF hj inF  F fst ji f '  F f  idF ) f  hj ' jiF hj jiF

para-C ANCEL para-R EFL para-F USION

=

=

=

(

)

=

The reflection and fusion laws are proved as follows:

2 66 66 66 66 66 66 66 64

idF

–2 para-C HARN –

=

66 66 66 66 64

inF

=

– F functor –

inF =

inF =

 F idF

– pairing –

F

fst  h idF ; idF

(

– F functor –

i

inF  F fst  F h idF ; idF hj inF  F fst jiF

)

i

2  f  '  F f  idF 66 f  hj ' jiF 66 – para-C HARN – 2 f  hj ' ji  in 66 F F 66 66 – para-C HARN – 66 66 f  '  F h hj ' jiF ; idF i 66 66 –– 66 66  F f  idF  F h hj ' jiF ; idF i 66 66 – F functor – 66 66  F f  idF  h hj ' jiF ; idF i 66 64 66 – pairing – 4  F h f  hj ' jiF ; idF i =

(

)

(

)

=

=

=

=

((

=

hj jiF

)

)

12

T. Uustalu and V. Vene

By definition, every paramorphism is the composition of the left projection and a catamorphism. In converse, paramorphisms can be viewed as a generalization of catamorphisms, in the sense that every catamorphism is definable as a certain paramorphism:

j ' j F hj '  F fst jiF

(

)

para-C ATA

=

This law is verified by the following calculation:

2 66 66 66 66 66 66 66 64

j'jF

( =

)

–2 para-C HARN –

j ' j F  inF 66 – cata-C HARN – 66 '  F j ' j F 66 – 66 '  –Fpairing fst  h j ' j F ; idF i 64 – F functor – '  F fst  F h j ' j F ; idF i hj '  F fst jiF (

)

=

(

)

=

(

(

)

)

=

(

)

Actually, every morphism whose source is the carrier of an initial algebra is a paramorphism:

f hj f  inF  F snd jiF

para-A NY

=

This observation, first recorded in (Meertens, 1992), is proved as follows:

2 f 66 –2 para-C HARN – 66 f  inF 66 66 – F functor – 66 66 f  inF  F idF 66 66 – 66 66 f  –inpairing  F snd  h f; idF i F 66 64 – F functor – 64 f  inF  F snd  F h f; idF i hj f  inF  F snd jiF =

=

=

(

=

)

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

13

From this law, it is a very short step to the following definition of the mor1 phism in? F as a paramorphism: 1 in? F

=

hj F snd jiF

para-I N -I NV

The proof proceeds as follows:

2 in?1 F 66 – para-A NY – 66 hj in?F 1  inF  F snd jiF 4 =

=

– in-inv-C HARN –

hj F snd jiF

Apomorphisms Let us now dualize everything we know about paramorphisms. For any morphism ' : C ! F (C + F ), define the morphism [h ' i]F : C ! F as a composition of a certain anamorphism with the left injection:

h ' iF [

]

= [( [

'; F inr  outF F  inl

apo-D EF

] )]

Morphisms of the form [h ' i]F are the dual of paramorphisms. Both in (Vos, 95) and (Vene and Uustalu, 1998), these morphisms are called apomorphisms ( o — Greek preposition meaning ‘apart from’, ‘far from’, ‘away from’) 1. The construction [h  i]F is, of course, a primitive corecursor. The characterizing universal property for apomorphisms is following:

outF

 f F f; idF  ' , f h ' iF =

[

]

= [

apo-C HARN

]

The type information is summarized in the following diagram:

C

'

/F

C F

(

+

f

)

F [ f;idF ] 

F

outF

/



F F

1 A curious thing is that we wrote (Vene and Uustalu, 1998) unaware of the earlier existence of (Vos, 95), but happened to come up with the exactly the same new name

14

T. Uustalu and V. Vene

The laws for apomorphisms are just the duals of those for paramorphisms. The cancellation, reflection and fusion laws are these:

 h ' iF F h ' iF ; idF  ' h F inl  outF iF  f F f idF  ' ) h iF  f h ' iF

outF idF

[

]

=

[[

]

= [ =

]

]

(

+

)

[

]

= [

]

apo-C ANCEL apo-R EFL apo-F USION

As paramorphisms generalized catamorphisms, apomorphisms are a generalization of anamorphisms. [(

'F )]

h F inl  ' iF

= [

]

apo-A NA

Also, any morphism whose target is the carrier of a terminal coalgebra, is an apomorphism.

f h F inr  outF  f iF = [

]

apo-A NY

1 The morphism out? F has this nice “apomorphic” definition: 1 out? F

h F inr iF

= [

]

apo-O UT-I NV

E XAMPLE 9. The function maphd (h) : Stream A ! Stream A , which modifies any input stream by applying a function h : A ! A to its head while leaving the tail unchanged, is definable as the composition of a certain anamorphism with the left injection that constructs the value for a given argument from the value of h for its head and from the elements of its tail.

maphd (h) = [( [ = [( [

h h  head ; inr  tail i; h head ; inr  tail i SA  inl h h  head ; inr  tail i; SA inr  outSA SA  inl ] )]

] )]

A much more natural definition of this function is given by an apomorphism that constructs the value for a given argument from the value of h for its head and from the whole of its tail.

maphd (h) = [h h h  head ; inr  tail i i]SA E XAMPLE 10. Assume that a given set A is linearly ordered by a predi() : A  A ! 1 + 1. For any function x : 1 ! A, the function insert (x) : Stream A ! Stream A , that inserts the element x into an input strean

cate

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

15

immediately before the first element that x is less than or equal to (so that the returned stream will be sorted, if the given stream is), is characterized by the equation

h head ; tail i  insert x  ys (

)

 =

h x; ys i x  head  ys h head ; insert x  tail i  ys if

(

)

otherwise

The function insert (x) is most naturally defined as an apomorphism that constructs the value for a given argument from its certain initial segment elementwise and the remainder as one whole:

insert (x) = [h [ h x  !; inr i; (idA  inl) ]  test(x) i]SA where test(x) : Stream A

test x (

! Stream A SA Stream A is +

fst + snd)  (() h x  !; fst  snd i)? h idStream A ; h head ; tail i i

) = (

E XAMPLE 11. For any function ` : 1 ! List 0A , the function append (`) : 0 List A ! List 0A , which appends the colist ` to an input colist, is naturally defin-

able as an apomorphism which constructs the value for a given argument from the elements of the argument and from the whole of `:

append (`)

h inl; inr  idA  inr  outLA  `; inr  idA  inl  outLA iLA h LA inr  outLA  `; inr  LA inl  outLA iLA

= [ [[

(

)] ]

= [ [

(

)]

]

]

5. Course-of-value (co)iteration Histomorphisms E XAMPLE 12. The famous Fibonacci function bo : Nat ! Nat is most smoothly characterized as the unique solution of the equation system

bo  zero = one ^ bo  one = one ^ bo  succ  succ = add  h bo  succ ; bo i; where one = succ  zero . This very nice characterization does not give us any definition of bo in terms of catamorphisms. The problem is that the value of bo

16

T. Uustalu and V. Vene

for a given argument is defined not via the values for the immediate subparts of the argument, but via the values for its subparts of depth 2. But the characterization of bo together with the function bo 0 : Nat ! Nat  Nat (which, for any argument n, returns the pair formed of the value of bo for n and either zero or the value of bo for the predecessor of n) by a much trickier equation system, viz.,

bo = fst  bo 0 ^ bo 0  zero = h one ; zero i ^ bo 0  succ = h add ; fst i  bo 0 ; leads to a definition of bo as the composition of the left projection and a catamorphism:

bo = fst  (j h [ one ; add ]; [ zero ; fst ] i j)Nat : To make programming and program reasoning easier, we could introduce a new construction that would capture the natural definition scheme of the Fibonacci function and closely similar functions, and start studying its properties. But this would only provide us with a partial solution to the problem manifested by the Fibonacci example, as one can imagine functions whose value for a given argument is naturally defined via the values for its subparts of depth three, four, etc. Instead of this, we introduce a construction that captures course-of-value iteration, which is a function definition scheme where the value of a function for a given argument may depend on the values for just any of its subparts. We start with the following result, which, to our knowledge, is novel: Let FA denote the functor FA X = A  F X , FA h = idA  F h. Then, for any two morphisms f : F ! C and ' : F (FC ) ! C , we have

f  inF

'  F h f; in?F 1 i F  , f fst  outF   j out?F 1  h '; id i j F =

[(

=

)]

(

)

L EMMA -3

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

Proving it is quite tricky.

2  f  in '  F h f; in?F 1 i F  66 f F 66 – 66 fst–pairing ?1 i h f; in F 66 – pairing – 66 66 fst  id  F h f; in?F 1 i F   h f; in?F 1 i – ana-C HARN – 66 66 fst  outF   h f; in?F 1 i F  –2 cata-C HARN – 66 66 h f; in?F 1 i  inF 66 66 – out-inv-C HARN – 66 out?1  outF   h f; in?1 i F   inF 66 F F 66 66 – ana-C HARN – 66 out?1  id  F h f; in?1 i   h f; in?1 i  in 66 F F F F F 66 66 – pairing – 66 66 ?1 ?1 ?1 66 outF   h f; F h f; inF i F   inF i  inF 66 – pairing – 66 66 66 out?F 1  h f  inF ; F h f; in?F 1 i F   in?F 1  inF i 66 – , in-inv-C HARN – 66 66 66 out?F 1  h '  F h f; in?F 1 i F  ; F h f; in?F 1 i F  i 66 4 – pairing – 66 1 4 out? i  F h f; in?F 1 i F  F   h ';?id 1 fst  outF   j outF   h '; id i j F =

[(

)]

=

=

(

[(

)]

)

=

[(

)]

=

[(

)]

=

[(

)]

=

(

[(

)]

)

=

[(

)]

=

[(

)]

=

[(

)]

[(

=

[(

(

)]

)

)]

17

18

T. Uustalu and V. Vene

2  f fst  out   j out?1  h '; id i j F F F 66 f  inF 66 –– 66 fst  outF   j out?1  h '; id i j  inF F 66 – cata-C HARN – 66 fst  out   out?1  h '; id i  F j out?1  h '; id i j F F F F 66 – out-inv-C HARN – 66 66 fst  h '; id i  F j out?F 1  h '; id i j F – pairing – 66 66 '  F j out?F 1  h '; id i j F – ana-C HARN – 66 2 out   j out?1  h '; id i j 66 F F F 66 66 – pairing – 66 hfst  outF   j out?F 1  h '; id i j F ; 66 66 1 66 snd  outF   j out? F   h '; id i j F i 66 66 , in-inv-CHARN – 66 h f; –snd 66  outF   j out?F 1  h '; id i j F  inF  in?F 1 i 66 66 – cata-C HARN – 66 66 ?1 66 hf; snd  outF   outF?1  h '; id i 66 F j outF   h '; id i j F  in?F 1 66 66 – out-inv-C HARN – 66 66 1 ?1 h f; snd  h '; id i  F j out? 6 66 F   h '; id i j F  inF i 6 66 – pairing – 66 66 h f; F j out?F 1  h '; id i j F  in?F 1 i 66 4 66 – pairing – 1 4 id  F j outF?  h '; id i j F  h f; in?F 1 i ? 1 '  F h f; inF i F  =

(

)

=

(

)

=

(

)

=

(

)

=

(

)

=

(

)

=

(

)

(

)

=

(

)

=

(

)

=

(

)

=

(

)

=

(

(

[(

)

)]

For any morphism ' : fj ' jgF : F ! C by letting

fj ' jgF

=

)

F FC ! C , let us now define the morphism (

)

1 fst  outF   (j out? F

 h '; id i j F )

histo-D EF

Morphisms of the form fj ' jgF are called histomorphisms in this paper. The construction fj jgF is easily seen to be a course-of-value iterator. By Lemma 3, the characterizing universal property for histomorphisms is the following:

f  inF

=

'  F h f; in?F 1 i F  , f fj ' jgF [(

)]

=

histo-C HARN

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

19

The type information is summarized in the following diagram: inF

F F F [( h f;in?F 1 i )]F 

F FC (

f

'



)

F /

/



C

E XAMPLE 13. The following is a neat “histomorphic” definition of the Fibonacci function:

bo = fj [ one ; [ one  snd; add  (id  (fst  outN  )) ]  distl  outN  ] jgN The cancellation, reflection and fusion laws for histomorphisms are these:

fj ' jgF  inF '  F h fj ' jgF ; in?F i F  idF fj inF  F fst  outF  jgF f '  F f  id  outF  F  ) f  fj ' jgF fj jgF =

=

1

[(

(

=

)]

)

[( (

)

)]

=

histo-C ANCEL histo-R EFL histo-F USION

The reflection law is proved by the following calculation:

2 66 66 66 66 66 66 66 66 66 66 66 66 4

idF =

2 66 66 66 66 66 66 66 66 64

– histo-C HARN –

inF =

– F functor –

inF =

inF =

1 fst  h id; in? F i)

(

F

1 ?1 fst  (id  F [( h id; in? F i )]F  )  h id; inF i)

(

– ana-C HARN –

inF =

F

– pairing –

inF =

 F id

– pairing –

F

fst  outF 

(

– F functor –

 h id; inF? i [(

1

)]

F)

inF  F (fst  outF  )  F [( h id; inF?1 i )]F  fj inF  F (fst  outF  ) jgF

20

T. Uustalu and V. Vene

The fusion law is proved as follows:

2  f  '  F f  id  outF  F  66 f  fj ' jgF 66 –2 histo-C HARN – 66 f  fj ' jgF  inF 66 66 – histo-C HARN – 66 66 f  '  F h fj ' jg; in?1 i F  F 66 66 –– 66 66  F f  id  outF  F   F h fj ' jgF ; in?F 1 i F  66 66 – F functor – 66 66 66  F f  id  outF  F   h fj ' jgF ; in?F 1 i F  66 66 – ana-F USION – 66 2 f  id  out   h fj ' jg ; in?1 i  66 66 F F F F 66 66 – ana-C HARN – 66 66 66 f  id  id  F h fj ' jgF ; in?F 1 i F   66 66 66 66 h fj ' jgF ; in?F 1 i 66 66 66 – 66 66 f – pairing 66 F h fj ' jgF ; in?F 1 i F   h fj ' jgF ; in?F 1 i 66 66 66 – pairing – 66 66 66 ?1 ?1 66 64 h f  fj ' jgF ; F h fj ' jgF ; inF i F   inF i 66 – pairing – 66 64 1 ?1 id  F h fj ' jgF ; in? 66 F i F   h f  fj ' jgF ; inF i ? 1 4  F h f  fj  jgF ; inF i F  =

[( (

)

)]

=

=

[(

)]

=

[( (

)

)]

[(

)]

=

([ ((

)

(

)

(

)

(

[(

)]

[(

)]

)

=

[(

)]

=

(

[(

)]

)

=

)]

)

=

[(

)]

=

(

[(

[(

fj jgF

)]

)

)]

Similarly to paramorphisms, histomorphisms can be viewed as a generalization of catamorphisms:

j ' j F fj '  F

(

)

=

fst  outF  ) jgF

(

histo-C ATA

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

21

This fact is verified by the following calculation:

2 66 66 66 66 66 66 66 66 66 66 66 66 4

j'jF

(

)

2 66 66 66 66 66 66 66 66 64

– histo-C HARN –

=

j ' j F  inF

( =

)

– cata-C HARN –

'F j'jF (

=

=

=

)

– pairing –

'F

1 fst  h (j ' j)F ; in? F i)

(

– pairing –

'F

1 ?1 fst  (id  F [( h (j ' j)F ; in? F i )]F  )  h (j ' j)F ; inF i)

(

– ana-C HARN –

'  F fst  outF   h j ' j F ; in?F 1 i F  – F functor – '  F fst  outF   F h j ' j F ; in?F 1 i F  (

=

fj '  F

[(

(

fst  outF  ) jgF

(

)

)

[(

)]

(

)

)

)]

(

Futumorphisms We now introduce a construction dual to histomorphisms. Let FA+ denote the functor FA+ X = A + F X , FA+ h = idA + F h. Then, given a morphism ' : C ! F (FC+ ), we define the morphism [f ' g]F : C ! F by letting

f ' gF [

]

= [( [

'; id  in?F +1 F  inF +  inl ]

futu-D EF

)]

Our name for morphisms of the form [f ' g]F is futumorphisms. The construction [f  g]F is the dual of a course-of-value iterator (a “cocourse-of-argument” coiterator). The characterizing universal property for futumorphisms is the following:

outF

 f F j f; out?F j F +  ' , f f ' gF =

1

( [

] )

= [

]

The type information is summarized in the following diagram:

C

'

/F

f 

F

outF

/

FC+

(



)

F (j [ f;out?F 1 ] j)F +

F F

futu-C HARN

22

T. Uustalu and V. Vene

A straightforward dualization of the laws for histomorphisms gives laws for futumorphisms:

 f ' gF F j f ' gF ; outF? j F +  ' futu-C ANCEL f F inF +  inl  outF gF futu-R EFL  f F j inF +  f id j F +  ' ) f gF  f f ' gF futu-FUSION ' F f F inF +  inl  ' gF futu-A NA E XAMPLE 14. The function exch Stream A ! Stream A , which pairwise

outF idF

[

= [ =

( [

)]

]

= [

=

( [[

(

(

)

(

(

1

]

+

] )

]

) )

)

[

]

= [

]

]

:

exchanges the elements of any given argument, is characterized by the equation system

head  exch = head  tail ^ head  tail  exch = head ^ tail  tail  exch = exch  tail  tail : This function is nicely definable as a futumorphism:

exch = f[ h head  tail ; in(SA )+  inr  h head ; in(SA )+  inl  tail  tail i i g]SA : 6. Conclusion and further work We presented a categorical treatment of the function definition schemes of primitive recursion and course-of-value iteration and their duals. These schemes are generalizations of more basic schemes— iteration and coiteration. While the value of an iterative function for a given argument depends solely on the values for its immediate subparts, the value of a primitive recursive function may additionally depend on these immediate subparts directly; the value of a course-of-value iterative function depends on the values for any subparts of the argument. Dually, the argument of a coiterative function for a value may only determine the argument for the immediate subparts of the value, whereas the argument of a primitive corecursive function may alternatively determine these immediate subparts directly; the argument of a “cocourse-ofargument coiterative” function may determine the arguments for any subparts of the value. Primitive (co)recursion and course-of-value (co)iteration, being more “liberal” than (co)iteration, make it easier to write programs and to prove properties about programs. Codatatypes and the associating notions such as coinduction and bisimulation are used widely in the analysis of processes specifiable by transition systems or state machines (Jacobs and Rutten, 1997). If processes are understood as functions from states (and, optionally, streams of input tokens) to behaviors, sequential composition of processes becomes a natural example of a function elegantly

Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically

23

definable as an apomorphism, whereas any concrete process specified by a finite transition or state machine becomes a natural example of a function elegantly definable as a futumorphism. This leads us to believe that apomorphisms and futumorphisms may turn out viable constructions in the modelling of processes. To check out this conjecture is one possible direction for continuing the work reported here. Acknowledgement The work reported here was partially supported by the Estonian Science Foundation grant no. 2976. The diagrams were produced using the Xy-pic macro package by Kristoffer H. Rose. Bibliography Bird, R. S. (1987). An introduction to the theory of lists. In M. Broy (Ed.), Logic of Programming and Calculi of Discrete Design (NATO ASI Series F, Vol. 36). Springer-Verlag, Berlin. pp. 5–42. Fokkinga, M. M. (1992). Law and Order in Algorithmics. PhD thesis, University of Twente. iv+161 pp. Geuvers, H. (1992). Inductive and coinductive types with iteration and recursion. In B. Nordström, K. Pettersson, and G. Plotkin (Eds.), Informal Proceedings of the Workshop on Types for Proofs and Programs, Båstad, June 1992. Dept. of Computer Sciences, Chalmers Univ. of Technology and Göteborg Univ. pp. 193–217. URL ftp://ftp.cs.chalmers.se/pub/cs-reports/baastad.92/. Grundy, J. (1996). A browsable format for proof presentation. Mathesis Universalis, 2. URL http://saxon.pip.com.pl/MathUniversalis/2/. Hagino, T. (1987). A Categorical Programming Language. PhD thesis CST-47-87, Univ. of Edinburgh. vii+180 pp. Jacobs, B., and J. Rutten. (1997). A tutorial on (co)algebras and (co)induction. Bulletin of the EATCS, 62, 222–259. Lambek, J. (1968). A fixpoint theorem for complete categories. Mathematische Zeitschrift, 103, 151–161. Malcolm, G. (1990). Data structures and program transformation. Science of Computer Programming, 14(2–3), 255–279. Meertens, L. (1992). Paramorphisms. Formal Aspects of Computing, 4(5), 413–424. Turner, D. A. (1995). Elementary strong functional programming. In P. H. Hartel and R. Plasmeijer (Eds.), Proceedings of the 1st Internat. Symp. on Functional Programming Languages in education, FPLE’95, Nijmegen, Dec. 1995 (Lecture Notes in Computer Science, Vol. 1022). SpringerVerlag, Berlin. pp. 1-13. Uustalu, T., and V. Vene. (1997). A cube of proof systems for the intuitionistic predicate -, -logic. In M. Haveraaen and O. Owe (Eds.), Selected Papers of the 8th Nordic Workshop on Programming Theory, NWPT’96, Oslo, Dec. 1996, Research Report 248, Dept. of Informatics, Univ. of Oslo. pp. 237–246. Vene, V., and T. Uustalu. (1998). Functional programming with apomorphisms (corecursion). Proceedings of the Estonian Academy of Sciences: Physics, Mathematics, 47(3), 147–161. Vesely, P. (1997). Typechecking the Charity term logic. Unpublished notes. 21 pp. URL http://www.cpsc.ucalgary.ca/projects/charity/home.html. Vos, T. (1995). Program Construction and Generation Based on Recursive Types. MSc thesis INF/SCR-95-12, Univ. of Utrecht. 235 pp.

Received December 1998

24

T. Uustalu and V. Vene

T. Uustalu received the MSc degree in system and computer sciences from the Tallinn Technical University in 1992 and the PhD degree in computer science from the Royal Institute of Technology, Stockholm, Sweden, in 1998. Presently, he is a senior lecturer at the Royal Institute of Technology. His scientific interests include structural proof theory and theorem proving, semantics of programming languages, program verification, transformations, and construction, philosophy of logic and computing. V. Vene obtained the MSc degree in computer science from the University of Tartu, Estonia, in 1994. He is a researcher and PhD student in computer science at the University of Tartu. His scientific interests are programming language design and implementation, functional programming, type theory, semantics based program manipulation, category theory.

Suggest Documents