Top Banner
(Co-)Iteration for Higher-Order Nested Datatypes September 13, 2002 Andreas Abel 1? and Ralph Matthes 2?? 1 Department of Computer Science, University of Munich [email protected] 2 PPS, CNRS, Universit´ e Paris VII (on leave from University of Munich) [email protected] Abstract. The problem of defining iteration for higher-order nested datatypes of arbitrary (finite) rank is solved within the framework of system F ω of higher-order parametric polymorphism. The proposed so- lution heavily relies on a general notion of monotonicity as opposed to a syntactic criterion on the shape of the type constructors such as posi- tivity or even being polynomial. Its use is demonstrated for some rank-2 heterogeneous/nested datatypes such as powerlists and de Bruijn terms with explicit substitutions. An important feature is the availability of an iterative definition of the mapping operation (the functoriality) for those rank-1 type transformers (i. e., functions from types to types) arising as least fixed-points of monotone rank-2 type transformers. Strong normal- ization is shown by an embedding into F ω . The results dualize to greatest fixed-points, hence to coinductive constructors with coiteration. 1 Introduction What is iteration for nested datatypes? It is a disciplined use of least fixed-points of nested recursive definitions of types and type transformers which rewards the programmer with a guarantee of termination and can usually be expressed in terms of initial algebras. By “nested”, different concepts can be understood. The easiest would be to first introduce the type of natural numbers by the equation Nat =1+ Nat and then the lists of naturals by NatList =1+ Nat × NatList. Here, Nat is nested within NatList. In proof theory, the general principle behind it is called iterated inductive definitions [BFPS81]. More demanding would be nesting by help of parameters: Let List(A)=1+ A × List(A) be the type of lists over A. Then FinTree = List(FinTree) is nested in the sense of an interleaving of the definitions of FinTree and List(FinTree). Certainly, this can be represented as ? The first author gratefully acknowledges the support by the PhD Programme Logic in Computer Science (GKLI) of the Deutsche Forschungs-Gemeinschaft. ?? Both authors have benefitted from financial support by the EU TYPES project.
30

(Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) [email protected]

Jan 22, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

(Co-)Iteration forHigher-Order Nested Datatypes

September 13, 2002

Andreas Abel1? and Ralph Matthes2??

1 Department of Computer Science,University of Munich

[email protected] PPS,

CNRS, Universite Paris VII (on leave from University of Munich)[email protected]

Abstract. The problem of defining iteration for higher-order nesteddatatypes of arbitrary (finite) rank is solved within the framework ofsystem Fω of higher-order parametric polymorphism. The proposed so-lution heavily relies on a general notion of monotonicity as opposed toa syntactic criterion on the shape of the type constructors such as posi-tivity or even being polynomial. Its use is demonstrated for some rank-2heterogeneous/nested datatypes such as powerlists and de Bruijn termswith explicit substitutions. An important feature is the availability of aniterative definition of the mapping operation (the functoriality) for thoserank-1 type transformers (i. e., functions from types to types) arising asleast fixed-points of monotone rank-2 type transformers. Strong normal-ization is shown by an embedding into Fω. The results dualize to greatestfixed-points, hence to coinductive constructors with coiteration.

1 Introduction

What is iteration for nested datatypes? It is a disciplined use of least fixed-pointsof nested recursive definitions of types and type transformers which rewards theprogrammer with a guarantee of termination and can usually be expressed interms of initial algebras. By “nested”, different concepts can be understood. Theeasiest would be to first introduce the type of natural numbers by the equationNat = 1 + Nat and then the lists of naturals by NatList = 1 + Nat × NatList.Here, Nat is nested within NatList. In proof theory, the general principle behindit is called iterated inductive definitions [BFPS81]. More demanding would benesting by help of parameters: Let List(A) = 1 +A× List(A) be the type of listsover A. Then FinTree = List(FinTree) is nested in the sense of an interleaving ofthe definitions of FinTree and List(FinTree). Certainly, this can be represented as

? The first author gratefully acknowledges the support by the PhD Programme Logicin Computer Science (GKLI) of the Deutsche Forschungs-Gemeinschaft.

?? Both authors have benefitted from financial support by the EU TYPES project.

Page 2: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

a simultaneous definition. Nesting in the sense advocated in [BM98] is different:It is a definition of a family of types where the recursive calls may refer to othermembers of the family, e. g.,

Lam(A) = A+ Lam(A)× Lam(A) + Lam(1 +A)

as a representation of untyped lambda terms [BP99b,AR99]. This is just a hetero-geneous datatype because Lam itself is not used in determining to which familymember recursive calls are made. True nesting, called non-linear [BP99a], oc-curs in the representation of untyped lambda terms with explicit substitution asfollows:

Lam(A) = A+ Lam(A)× Lam(A) + Lam(1 +A) + Lam(Lam(A)),

see example 4 below.The aim of the present article is to shed more light on those nested inductive

types in terms of type theory, i. e., by a formulation in system Fω of higher-orderparametric polymorphism [Gir72]. We propose a system GMIC of general mono-tone inductive and coinductive constructors of arbitrary kind of Fω which hencealso covers greatest fixed-points with associated coiteration. The system will begiven in Curry-style, hence does not need any type information for the termrewrite rules. The well-known (at least, since [Wra89]) embeddings of induc-tive and coinductive datatypes into system F can be extended to an embeddingof GMIC into Fω by a syntactic analogue of Kan extensions (mentioned in thecontext of nested datatypes in [BP99a]).

A major effort has been made to ensure that there are iterative definitions ofclosed terms of types such as ∀A∀B.(A→ B)→ Lam(A)→ Lam(B) which hencewitness monotonicity or “functoriality” of Lam and the other type transformersthat are the solutions to these nested equations.

Unlike previous work of the second author [Mat01], we base our notion ofmonotonicity on a non-standard definition of inequality. In the case of F,G beingdefinable functions from types to types, it is

F 6 G := ∀A∀B.(A→ B)→ FA→ GB,

kindly suggested by Peter Hancock during his visit in Munich in late 2000. Arough categorical motivation can be given as follows: If F,G were functors, then∀A.FA → GA would be the type of natural transformations α from F to G.Then, given some f : A → B, we would have Gf ◦ αA and αB ◦ Ff as equal“morphisms” of type FA → GB. In our definition, we drop functoriality of Fand G but directly require the “diagonal” in the associated naturality diagramfrom FA to GB.

The article is organized as follows: The present section is concluded by ashort overview of system Fω (with a more detailed account in appendix A andthe Church version in appendix B). Section 2 contains the definition of GMICand its specializations to (co-)inductive types and to (co-)inductive functors,including several example for programming in GMIC. We sketch higher-order

2

Page 3: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

examples in 2.3 and discuss the achieved monotonicity of the fixed-points in 2.4.A short argument is given in 2.5 for subject reduction and confluence of GMIC.The syntactic analogue of Kan extensions forms section 3. They are in closerelation with our non-standard definition of 6 for type constructors and areuseful for our more advanced examples of the definition of the representation ofsubstitution for de Bruijn terms and for the resolution of explicit substitutionsby implicit ones in section 3.1. Section 3.2 shows that, logically, the notion ofmonotonicity in [Mat01] is a special case of the one in GMIC. Section 4 gives theproof of strong normalization by an embedding into system Fω. As mentionedbefore, appendices A and B give details of our version of system Fω. Appendix Cshows an implementation of the examples in Haskell. [It will be transferred toour web site in the final version.]

Acknowledgements: Many thanks to Peter Hancock for his suggestion ofthe unusual notion of the definition of F 6κ1 G, to Tarmo Uustalu for repeatedadvice on syntactic Kan extensions and the representation of substitution andto Thorsten Altenkirch for his valuable feedback on early versions of the presentarticle.

1.1 System Fω

Our development of higher-order datatypes takes place within a conservativeextension of Curry-style system Fω by finite sums and products and existentialquantification. It contains three syntactic categories:

Kinds. We denote kinds by the letter κ. For the pure kind of rank n we introducethe special name κn.

κ ::= ∗ | κ→ κ′

κ0 := ∗κ(n+ 1) := κn→ κn

Examples for pure kinds are κ0 = ∗, types, κ1 = ∗ → ∗, type transformersand κ2 = (∗ → ∗) → ∗ → ∗ transformers of type transformers. Note thateach kind κ′ can be written as κ → ∗, where we write κ for κ1, . . . , κn, setκ1, . . . , κn → κ := κ1 → . . . → κn → κ and assume that → associates to theright. Also set |κ1, . . . , κn| := n.

Constructors. Uppercase latin letters and the greek letters Φ and Ψ denoteconstructors, given by the following grammar.

A,B, F,G ::= X | λXκ.F | F G | ∀Fκ. A | ∃Fκ. A | A→ B| 0 | A+B | 1 | A0 ×A1 × . . .×An (n > 1)

We identify β-equivalent constructors. A constructor F has kind κ if there is acontext Γ such that Γ ` F : κ. The kinding rules for the constructors can befound in Appendix A. It also contains the typing rules for the terms and thereduction rules.

3

Page 4: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Preferably we will use letters A,B,C,D for constructors of rank 0 (types),F,G,H for constructors of rank 1 and Φ, Ψ for constructors of rank 2. If nokinds are given and cannot be guessed from the context, we assume A,B,C,D :∗, F,G,H : κ1 and Φ, Ψ : κ2. We write F ◦ G for constructor compositionλX.F (GX).

Objects (Curry terms). Lower case letters denote terms.

r, s, t ::= x | λx.t | r s | abort r | inl t | inr t | case (r, x. s, y. t)| 〈t〉 (for |t| > 0) | r.n (for n > 0) | open (r, x. s)

Most term constructors are standard; “open” eliminates existential quantifica-tion. As for kinds, there is a “vector notation” t for a list t1, . . . , tn of terms.Set |t| := n. The case of the empty list yields the term 〈〉 which serves as theinhabitant of type 1. The one-element list gives rise to the term 〈t〉, representingexistential introduction with term t. The polymorphic identity λx.x : ∀A.A→ Awill be denoted by id. We write f ◦ g for function composition λx. f (g x). Appli-cation rs associates to the left, hence rs = (. . . (rs1) . . . sn) for s = s1, . . . , sn.

A term t has type A if Γ ` t : A for some context Γ . The relation −→ denotesthe usual one-step β-reduction which is confluent, type preserving and stronglynormalizing. As mentioned above, the exact typing and reduction rules can befound in Appendix A.

Church terms. We consider Church-style terms (again, following the distinctionbetween the styles a la Curry and a la Church proposed in [Bar92]) as a linearnotation for typing derivations. For details see appendix B. Whenever we writea Church term, we mean the typing derivation of the corresponding Curry term.

In the following we will refer to the here defined system simply as “Fω”.

2 Monotone Inductive and Coinductive Constructors

For constructors F and G of kind κ we define the type F 6κ G by recursionon κ as follows. Then general monotonicity monκ F for constructor F can beexpressed in terms of 6κ, and will also be a type.

F 6∗ G := F → GF 6κ→κ′ G := ∀Xκ∀Y κ. X 6κ Y → F X 6κ′ GY

monκ F := F 6κ F

(When clear from the context, we will omit subscripts κ from mon and 6. We as-sume that 6 binds stronger than→ or ×.) The proposed (co-)inductive construc-tors will not rely on some syntactic notion of positivity (syntactic in the sense ofbeing a criterion on the shape of the constructor, used in most of the articles oninductive types [Hag87,Men87,Lei90,Geu92] although positivity is not needed inMendler-style, as observed in [UV97]), but exclusively on the notion of mono-tonicity, just defined, hence giving a logical flavour to the system. Monotonicity

4

Page 5: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

as an assertion/proposition is modelled as a type. The propositions-as-typesparadigm of type theory, first introduced as the Curry-Howard-isomorphism[How80] for minimal propositional logic, has been an important guide in thedevelopment of the system.

Having monotonicity, we can enrich system Fω with some constructor andterm constants to obtain higher-order (co)inductive datatypes. Closed termsreceiving a type of the form monκ F will be called monotonicity witnesses for F .

Inductive constructors.

Formation. µκ : (κ→ κ)→ κ

Introduction. inκ : ∀Fκ→κ. F (µκF ) 6 µκFElimination. Itκ : ∀Fκ→κ. monF → ∀Gκ. F G 6 G→ µκF 6 G

Reduction. Itκ→∗ms g (inκ→∗ f t) −→β s g (m (Itκ→∗ms)f t)

In the last line we require |f | = |g| = |κ|. As a first example, define

Mµκ := λm.Itκminκ : ∀Fκ→κ.monF → mon(µκF ).

Hence, there is a completely uniform way of inferring monotonicity of µκF fromthat of F .

Coinductive constructors.

Formation. νκ : (κ→ κ)→ κ

Introduction. Coitκ : ∀Fκ→κ. monF → ∀Gκ. G 6 F G→ G 6 νκFElimination. outκ : ∀Fκ→κ. νκF 6 F (νκF )

Reduction. outκ→∗ g (Coitκ→∗msf t) −→β m (Coitκ→∗ms) g (sf t)

Again, we require |f | = |g| = |κ|.Dually to the case of inductive constructors, define

Mνκ := λm.Coitκmoutκ : ∀Fκ→κ.monF → mon(νκF ).

Hence, also monotonicity of νκF follows uniformly from monotonicity of F .This completes the definition of the system GMIC of general monotone induc-

tive and coinductive constructors. To give a feel for the meaning of these densedefinitions we will specialize them to kinds κ0 and κ1 in the following sections.

2.1 (Co)Inductive Types

For kind κ0 we obtain monotone (co)inductive types as described in the secondauthor’s thesis [Mat98]. These include all interleaved positive datatypes, espe-cially all homogeneous datatypes like natural numbers, lists, trees etc. which arecommon in functional programming.

5

Page 6: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Inductive types (κ = ∗).

Formation. µ∗ : (∗ → ∗)→ ∗Introduction. in∗ : ∀F ∗→∗. F (µ∗F )→ µ∗FElimination. It∗ : ∀F ∗→∗. monF → ∀A∗. (F A→ A)→ µ∗F → A

Reduction. It∗ms (in∗ t) −→β s (m (It∗ms) t)

For the examples to follow, we will need some basic monotonicity witnesses:

pair : mon(λAλB.A×B) := λfλgλp. 〈f (p.0), g (p.1)〉fork : mon(λA.A×A) := λf. pair ffeither : mon(λAλB.A+B) := λfλgλx. case (x, a. inl (f a), b. inr (g b))maybe : mon(λA.1 +A) := either id

Example 1 (Binary trees). As a preparation for example 5, we show how toencode a type of binary trees BT with constructors leaf : BT and span : BT →BT→ BT and iterator ItBT : ∀A.(1 +A×A→ A)→ BT→ A.

BTF : ∗ → ∗ := λA. 1 +A×ABT : ∗ := µ∗BTF

leaf : BT := in∗(inl 〈〉)span : BT→ BT→ BT := λtλu. in∗(inr 〈t, u〉)mon BTF : ∗ = ∀A∀B. (A→ B)→ (1 +A×A)→ (1 +B ×B)btf : mon BTF := maybe ◦ forkItBT : ∀A.((1 +A×A)→ A)→ BT→ A := It∗ btf

Coinductive types (κ = ∗).

Formation. ν∗ : (∗ → ∗)→ ∗Introduction. Coit∗ : ∀F ∗→∗. monF → ∀A∗. (A→ F A)→ A→ ν∗FElimination. out∗ : ∀F ∗→∗. ν∗F → F (ν∗F )

Reduction. out∗ (Coit∗ms t) −→β m (Coit∗ms) (s t)

As for inductive types, these are just the usual definitions, with arbitrary mono-tonicity witnesses (sometimes also called strength) instead of canonical ones forpositive type transformers F . For the positive (covariant) case, their justificationfrom the point of view of category theory has first been given in [Hag87], a verygood presentation of the ideas is to be found in [Geu92].

2.2 (Co)Inductive Functors

If we specialize to kind κ1, we get heterogeneous (non-regular) and so-called(non-linear) nested datatypes. Prominent examples are powerlists [Hin00] and amonadic representation of de Bruijn λ-terms [AR99,BP99b].

6

Page 7: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Inductive functors (κ = κ1). Recall that κ1 = ∗ → ∗ and κ2 = κ1→ κ1.

Formation. µκ1 : κ2→ κ1

Introduction. inκ1 : ∀Φκ2∀A∀B. (A→ B)→ Φ (µκ1Φ)A→ µκ1ΦBElimination. Itκ1 : ∀Φκ2. monΦ→ ∀Gκ1. ΦG 6 G→

∀A∀B. (A→ B)→ µκ1ΦA→ GB

Reduction. Itκ1msg (inκ1 f t) −→β s g (m (Itκ1ms) f t)

The name “functors” is not by chance. Let Φ be monotone of kind κ2 andm : monΦ a monotonicity witness. Then Mµ

κ1m is a monotonicity witness forµκ1Φ, i. e.,

Mµκ1m : mon(µκ1Φ) = ∀A∀B.(A→ B)→ (µκ1Φ)A→ (µκ1Φ)B,

hence Mµκ1m is the “functorial action” or map function for µκ1Φ. Note, how-

ever, that the functor laws are beyond our intensional treatment. They wouldrequire to study some extensional equality theory such as the assumption of uni-versal properties of the iterator or parametric equality, defined by induction onthe types, using generalized induction for the case of inductive types; see, e. g.,[Alt99].

Example 2 (Powerlists). A famous example for a heterogeneous datatype arelists of length 2n, also called powerlists [BGJ00] or perfectly balanced, binaryleaf trees [Hin00]. This datatype is present in our system as the least fixed pointof the rank-2 constructor PListF = λFλA.A+ F (A×A).

We obtain the type of powerlists with its data constructors and its mono-tonicity witness in a schematic way.

PList : ∗ → ∗ := µκ1 PListFplistf : mon PListF := λsλf. either f (s(fork f))plist : mon PList := Mµ

κ1 plistf

zero : ∀A.A→ PListA := λa. inκ1 id (inl a)succ : ∀A. PList(A×A)→ PListA := λl. inκ1 id (inr l)

A reversal algorithm for powerlists is simply derived from a different monotonic-ity witness for PListF.

swap : mon(λA.A×A) := λfλp. 〈f (p.1), f (p.0)〉plistfrev : mon PListF := λsλf. either f (s(swap f))

rev′ : mon PList := Mµκ1 plistfrev

rev : ∀A. PListA→ PListA := rev′ id

Note that the freedom in using monotonicity witnesses demonstrated by theprevious definition goes beyond the capabilities of Functorial ML [JBM98]. Al-though that system uses constants witnessing monotonicity to describe iteration,the behaviour of those witnesses is fixed by the generic programming approachand consequently only yields the behaviour of fork in the case of λA.A×A.

7

Page 8: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Example 3 (de Bruijn terms). Bird & Paterson [BP99b] and Altenkirch & Reus[AR99] have shown that nameless untyped λ-terms can be represented by aheterogeneous datatype. In our system this type is obtained as the least fixedpoint of the monotone rank-2 constructor LamF.

LamF : κ2 := λFλA.A+ (FA× FA+ F (1 +A))lamf : mon LamF := λsλf. either f

(either (fork (s f)) (s (maybe f))

)Now we can define the datatype LamA of de Bruijn terms with free variables inA, plus its constructors var, app and abs. Furthermore, we provide an auxiliaryfunction weak which lifts each variable in a term to provide space for a freshvariable.

Lam : κ1 := µκ1 LamFlam : mon Lam := Mµ

κ1 lamf

var : ∀A.A→ LamA := λa. inκ1 id (inl a)app : ∀A. LamA→ LamA→ LamA := λt1λt2. inκ1 id (inr (inl 〈t1, t2〉))abs : ∀A. Lam(1 +A)→ LamA := λr. inκ1 id (inr (inr r))

weak : ∀A. LamA→ Lam(1 +A) := lam (λa. inr a)

Example 4 (de Bruijn terms with explicit substitutions). We consider an exten-sion of the untyped λ-calculus by explicit substitutions t{σ} where σ providesa term ti for each of the n free variables xi of t. We can view t{σ} as a termwhich has the same structure as t but with variables whose names are takenfrom the set {t1, . . . , tn}. This means that the variable names are itself λ-terms.Hence, for the data constructor exs which makes an explicit substitution a termthe type Lam(LamA) → LamA is justified. In this case we have to deal with atruly nested datatype.

LamF : κ2 := λFλA. LamFFA+ F (FA)lamf : mon LamF := λsλf. either (lamf s f) (s (s f))

The nesting of the type transformers F in Lam is reflected by the nesting ofthe transformations s in the respective monotonicity witness. The datatype Lamwith its constructors is now obtained mechanically.

Lam : κ1 := µκ1 LamF

lam : mon Lam := Mµκ1 lamf

var : ∀A.A→ LamA := λa. inκ1 id (inl (inl a))app : ∀A. LamA→ LamA→ LamA := λt1λt2. inκ1 id (inl (inr (inl 〈t1, t2〉)))abs : ∀A. Lam(1 +A)→ LamA := λr. inκ1 id (inl (inr (inr r)))exs : ∀A. Lam(LamA)→ Lam(A) := λe. inκ1 id (inr e)

8

Page 9: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Coinductive functors (κ = κ1).

Formation. νκ1 : κ2→ κ1

Introduction. Coitκ1 : ∀Φκ2. monΦ→ ∀Gκ1. G 6 ΦG→∀A∀B. (A→ B)→ GA→ νκ1ΦB

Elimination. outκ1 : ∀Φκ2∀A∀B. (A→ B)→ νκ1ΦA→ Φ (νκ1Φ)B

Reduction. outκ1 g (Coitκ1msf t) −→β m (Coitκ1ms) g (s f t)

Similar to the inductive case, functoriality of νκ1Φ is witnessed by the uniformconstruction Mν

κ1m for m any monotonicity witness for Φ.

Example 5 (Functions over binary trees). Thorsten Altenkirch [Alt01] showshow to encode functions over regular inductive types by elements of nested coin-ductive types of rank 2. In the following we present his example of functions overunlabelled binary trees (λA.BT→ A) as functor (TFun) in our system.

TFunF : κ2 := λFλA.A× F (FA)tfunf : mon TFunF := λsλf. pair f (s(sf))

The coinductive type TFun is now obtained as the greatest fixed point of TFunF.We can derive its two destructors head and tail from the general destructor outfor coinductive types.

TFun : κ1 := νκ1TFunFhead : ∀A.TFunA→ A := λb. (outκ1 id b).0tail : ∀A.TFunA→ TFun(TFunA) := λb. (outκ1 id b).1

Every function g : BT → A can be transformed via the function lamBT into aninfinite object lamBT g : TFunA. We implement lamBT by coiteration.

lamBT′ : (λA.BT→ A) 6 TFunlamBT′ := Coitκ1 tfunf (λfλg. 〈f (g leaf), λlλr. f (g (span l r))〉)lamBT : ∀A. (BT→ A)→ TFunAlamBT := lamBT′ id

Conversely, an object b : TFunA can be applied to a binary tree t : BT to yieldan element appBT tb : A. The application function appBT can be encoded byiteration on the tree t.

appBT : BT→ ∀A.TFunA→ AappBT := ItBT(λtλb. case (t, . head b, p. (p.1 (p.0 (tail b))))

2.3 Beyond Rank 2

To the knowledge of the authors, inductive datatypes having type transformersas arguments are very rarely considered. An example would be

λA.µκ1→κ1→∗

(λΦλFκ1λGκ1.F (FA) + (ΦF (G×G) + Φ(F ×G)(G×G))

)9

Page 10: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

with F × G := λA.FA × GA. It has been studied in [Oka99] for the efficientrepresentation of square matrices where the types ensure squareness without theuse of dependent types by which one would just express that there is an n suchthat the matrix has n rows and n columns. The argument to µκ1→κ1→∗ clearlyhas a monotonicity witness.

As a toy example, we mention µκ1→∗Ψ with Ψ := λFκ1→∗λGκ1.G(F (G◦G)).monΨ is inhabited by λsλt.t(s(t ◦ t)).

2.4 On Monotonicity of the Fixed-Points

The nearly trivial constructions Mµκ and Mν

κ that yield monotonicity witnessesof µκF and νκF out of a monotonicity witness for F , have been the major mo-tivation for the present work. (In the related approach of [Mat01], alluded toin section 3.2, the respective constructions have been much more complicated.)Therefore, the reader might be curious to know why the existence of monotonic-ity witnesses for µκ1Φ is considered such an easy problem in [BP99a,Hin00] fortheir treatment of inductive functors.

We express everything in terms of our system. Assume a closed term m oftype monΦ. We would like to construct a closed term m′ of type mon(µκ1Φ).

First observe that there is a closed term m0 of type ∀F.monF → mon(ΦF ),namely m0 := λn.mn.1 In [BP99a], one would have such a term by inspection ofthe admissible constructors Φ which have to be higher-order polynomial functors,in [Hin00], it is even given by the definition of map〈F 〉.

If we already had our term m′, the following reduction behaviour would benatural:

m′f(inκ1gt) −→∗ inκ1 id (m0m′(f ◦ g)t).

In Church terms, this would be, for f : A→ B, g : Z → A, t : Φ(µκ1Φ)Z,

m′ABf(inκ1ΦZAgt) −→∗ inκ1ΦBB idB(m0(µκ1Φ)m′ZB(f ◦ g)t

).

Since the argument g is a specialty of our approach, one should better only argueabout the instance with g := id. With in′ := inκ1 id, we would get

m′f(in′t) −→∗ in′(m0m′ft),

clearly a reasonable desideratum.If the right-hand side is β-reduced for the specific m0, −→∗ is replaced

by definitional equality, then, modulo the pattern-matching mechanism, we ar-rive at the definitions of nest : mon(µκ1λFλA.1 + A × F (A × A)) and host :mon(µκ1λFλA.1+A×F (A×FA)) in [BP99a, Examples 3.2,3.3]. [Hin00] is evenclearer in taking just the recursive equation system for the map terms. Althoughthis would be admissible in functional programming languages such as Haskell,the termination guarantee is lost, and we consider this guarantee a major advan-tage of a programming discipline based on iteration and coiteration as opposedto general recursion.1 If we had m : monΦ, see section 3.2, then the definition would be m0 := m.0.

10

Page 11: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Unfortunately, our approach cannot justify the termination of those defini-tions because Mµ

κ1 = λm.Itκ1minκ1 behaves differently:

Itκ1minκ1f(in′t) −→ inκ1f(m(Itκ1minκ1) id t).

The reduct one would like to see would be inκ1 id(m(Itκ1minκ1)ft). In the ex-tensional model underlying program verification in Haskell, e. g., in [BP99a],those two terms would be equal by naturality of inκ1. In other words, there is aprovably terminating implementation that solves the recursive equations.

2.5 Subject Reduction and Confluence

The extensions to system Fω we made to incorporate (co)inductive constructorsare orthogonal to the existing construction like function space, products, sumsetc. The new reduction rules do not interfere with any of the old ones andthere are no critical pairs. Hence, confluence of GMIC immediately follows fromstandard results. To show subject reduction for GMIC, we only have to makesure that the new axioms preserve typing.

Proposition 1 ((Co)iteration is type-preserving). Let κ = κ → ∗ be anarbitrary kind and F : κ → κ, G : κ and Xi, Yi, Zi : κi (for 1 6 i 6 |κ|) beconstructors. Furthermore let fi : Xi 6κi Yi and gi : Yi 6κi Zi be terms for1 6 i 6 |κ| and m : monF .

1. Provided terms s : FG 6 G and t : F (µF )X, the following typing derivationsare correct.

ItκFmGsY Z g (inκFXY f t) : GZ−→Church

β sY Z g(m (µκF )G(ItκFmGs)XY f t

): GZ

2. Provided terms s : G 6 FG and t : GX we have the following typings.

outκF Y Z g (CoitκF mGsXY f t) : F (νF )Z−→Church

β mG(νκF )(CoitκF mGs)Y Zg (sXY f t) : F (νF )Z

Proof. By trivial type-checking. Note that the notation is slightly sloppy: insteadof Y Z g, it should be Y1Z1g1 . . . Y|κ|Z|κ|g|κ|, and similarly for XY f .

Subject reduction would follow trivially for a corresponding formulation of thewhole term rewrite system a la Church (where −→Church

β above would give thereduction rules pertaining to iteration and coiteration), but consequently bystandard techniques also for our Curry-style presentation of GMIC. The desiredproperty of strong normalization requires more work; in the following we preparefor an embedding into system Fω.

3 Kan Extensions

In this section we define Kan extensions for constructors of arbitrary rank, showtheir most fundamental properties and demonstrate their use in programmingwith iterators. Finally, we use them to establish logical equivalence of the notionof monotonicity in [Mat01] with Φmonκ2 in GMIC.

11

Page 12: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Kan extensions along the identity. Let G : κ → ∗ and Yi : κi (0 6 i < |κ|) beconstructors. Then the right Kan extension of GY along the identity is a typedefined by recursion on the length of Y as follows:

RKanG (·) := GRKanG (Y κ,Y ) := ∀Zκ. Y 6κ Z → RKanGZ(Y )

Similarily, the left Kan extension of GY along the identity is the following type:

LKanG (·) := GLKanG (Y κ,Y ) := ∃Xκ. X 6κ Y × LKanGX(Y )

These definitions are a syntactic rendering of the Kan extensions in categorytheory [Lan98, chapter 10] which become especially simple due to the presenceof universal and existential types. We will not discuss any of their category-theoretic properties. At this point, let us only note how Kan extensions relateto our notion “6κ”.

Proposition 2. Let F,G : κ→ ∗. The following types are logically equivalent:2

1. F 6 G,2. ∀Xκ. FX → RKanG(X), and3. ∀Y κ. LKanF (Y )→ GY .

Proof. By induction on κ. Part 1. ⇐⇒ 2. is done by rearranging quantifiersand arguments, Part 2. ⇐⇒ 3. by currying and uncurrying.

Rank-1 right Kan extension along a functor H. For programming with iterators(see next section), we need the more general right Kan extension along H forsome type transformer H. We define for constructors F,G,H of pure kind κ1the type

RKanHG (A) := ∀B. (A→ HB)→ GB

For the special case of the identity functor H = λA.A we obtain the right Kanextension defined above. We will write RKanHG for λA.RKanHG (A) and RKanG forλA.RKanG(A). The left Kan extension could be modified similarly, but for ourpurposes the right Kan extension is sufficient.

3.1 Programming with Kan extensions

In this section we demonstrate how right Kan extensions provide a general toolto program with rank-1 inductive datatypes. It is known [BP99a,Hin00] that afunction f : µκ1Φ ◦ H 6 G for G,H : κ1 cannot be defined by iteration di-rectly if H 6= λA.A. The solution to this difficulty proposed in the cited articlesis generalized iteration (also called “generalized fold”). Another solution (butrelated to the first one as a method for the justification of generalized itera-tion [BP99a, section 6.2]), is to define a more general auxiliary function g oftype µκ1Φ 6 RKanHG , from which we get f by a trivial specialization. We willdemonstrate this technique by continuing our examples from Section 2.2 A and B are called logically equivalent if there are closed terms in system Fω of

types A→ B and B → A. It is thus equivalence in second-order propositional logic.

12

Page 13: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Example 6 (Summing up a powerlist). Assume a type Nat of natural numberswith addition “+”, written infix. A function sum which adds up all entries ina powerlist of naturals has type PList Nat → Nat. This is an instance of theproblem described above where G = H = λA.Nat. The right Kan extensionof GA along H simplifies to the type (A → Nat) → Nat, hence we define thefollowing auxiliary function.

sum′ : PList 6 (λA. (A→ Nat)→ Nat)sum′ := Itκ1 plistf stepSum

stepSum : PListF(λA. (A→ Nat)→ Nat) 6 (λA. (A→ Nat)→ Nat)stepSum := λfλtλg. case (t, a. g (f a), l. l λp. g (f (p.0)) + g (f (p.1)))

The summation function is easily obtained as sum := λl. sum′ id l id.

Example 7 (Substitution for de Bruijn terms). De Bruijn terms constitute aKleisli triple (Lam, var, subst) with unit var : ∀A.A→ LamA and bind operation

subst : ∀A. LamA→ ∀B. (A→ LamB)→ LamB.

The reader will note that the type of subst can also be written as ∀A. LamA→RKanLam

Lam(A). This suggests that substitution can be defined by iteration (provenby Altenkirch/Reus [AR99]).

subst′ : Lam 6 RKanLamLam

subst′ := Itκ1 lamf stepSub

stepSub : LamF RKanLamLam 6 RKanLam

Lam

stepSub := λfλtλσ. case (t, a. σ (f a), t′. case (t′,p. app (p.0 (σ ◦ f)) (p.1 (σ ◦ f)),r. abs (r λx. case (x, u. var (inlu), a. weak (σ (f a))))))

The substitution function is obtained by specialization: subst := λtλσ. subst′ id t σ.

From the “Kleisli triple” formulation of Lam we get the “monad” formulation(Lam, var, lam, join) mechanically. The map operation lam : mon Lam has beendefined already, join : ∀A. Lam(LamA) → LamA can be obtained from subst asjoin := λt. subst t id. For use in the next example we also give a more generalversion of join:

join′ : Lam ◦ Lam 6 Lamjoin′ := λfλt. subst′ (lam f) t id

Example 8 (Resolving explicit substitutions). The set of de Bruijn terms Lam

can be embedded into the set of de Bruijn terms Lam with explicit substitution.The embedding function emb : ∀A. LamA→ LamA can be defined by iterationin a straightforward manner. The other direction is handled by a function eval :

13

Page 14: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

∀A. LamA→ LamA which has to resolve the explicit substitutions.

eval′ : Lam 6 Lam

eval′ := Itκ1 lamf stepEv

stepEv : LamF Lam 6 Lam

stepEv := λfλt. case(t, t′. case

(t′,

a. var (f a), t′′. case(t′′,

p. app (lam f (p.0)) (lam f (p.1)),r. abs (lam (maybe f) r)

)),

e. join′ f e)

Note how join′ is used to carry out the substitutions. The evaluation function isjust eval := eval′ id.

3.2 Comparison with Special Monotonicity [Mat01]

In [Mat01], the second author has introduced another notion of monotone induc-tive and coinductive constructors with µκ and νκ exclusively for κ ∈ {∗, ∗ → ∗}.The associated notions of monotonicity are for constructors of kind κ1 and κ2.Certainly, for κ1, there is only the definition we use in the present article. How-ever, the notion for κ2 has been the following:

monΦ := (∀F.monF → monΦF )× (∀G∀H.G ⊆ H →(monG→ ΦG ⊆ ΦH)× (monH → ΦG ⊆ ΦH))

This “special” notion of monotonicity has been designed so that it is as closeas possible to what one expects from category theory, but departing from it asmuch as needed so that, by help of Kan extensions, inhabitation of monΦ →mon(µκ1Φ) could be shown. Moreover, many closure properties of monotonicitycould be established, going far beyond algebraic datatypes such as the ones inour present examples. Unfortunately, that notion does not seem to extend toother kinds of rank 2, not to speak of arbitrary ranks.

Here, we show that, from a logical point of view, there is no difference betweenspecial monotonicity monΦ and general monotonicity monΦ, i. e., both types arelogically equivalent (see footnote 2).3

In this subsection, F,G,H always have kind κ1 and Φ has kind κ2. Definethe type F ⊆ G := ∀A.FA→ GA.

3 With respect to the rewrite rules, there are differences which cannot be addressedhere for lack of space.

14

Page 15: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

The direction from general monotonicity to special monotonicity does notrequire Kan extensions and can be programmed as follows:

c00 : ∀Φ.monΦ→ ∀F.monF → monΦFc00 := λmλn.mn

monSubLeq : ∀G∀H.monG→ G ⊆ H → G 6 HmonSubLeq := λmλgλfλx.g(mfx)

subMonLeq : ∀G∀H.G ⊆ H → monH → G 6 HsubMonLeq := λgλmλfλx.mf(gx)

c10 : ∀Φ.monΦ→ ∀G∀H.G ⊆ H → monG→ ΦG ⊆ ΦHc10 := λmλ`λnλt.m(monSubLeqn`) id t

c11 : ∀Φ.monΦ→ ∀G∀H.G ⊆ H → monH → ΦG ⊆ ΦHc11 := λmλ`λnλt.m(subMonLeq `n) id t

gmsm : ∀Φ.monΦ→ monΦgmsm := λm.〈c00m,λ`.〈c10m`, c11m`〉〉

We come to the interesting direction from special monotonicity to general mono-tonicity: Under the assumptions monΦ, G 6 H and A → B, we have to showΦGA → ΦHB. This is done via two intermediate types: We show ΦGA →ΦRKanH A → ΦRKanH B → ΦHB. The first step is an instance of ΦG ⊆ΦRKanH which follows from monΦ, G ⊆ RKanH and mon RKanH . The secondstep is just monotonicity of ΦRKanH , following from monΦ and mon RKanH ,while the third step comes from ΦRKanH ⊆ ΦH which in turn uses monΦ,RKanH ⊆ H and, once again, mon RKanH . Apparently, the crucial idea is theformation of the monotone (even non-strictly positive) constructor RKanH froman arbitrary type transformer H. The details can be read off the following defi-nitions and type assertions:

monRKan : ∀H.mon RKanHmonRKan := λfλkλg.k(g ◦ f)subRKan : ∀H.RKanH ⊆ HsubRKan := λk.k idleqRKan : ∀G∀H.G 6 H → G ⊆ RKanH (as an instance of proposition 2)leqRKan := λsλxλf.sfxG ⊆ ΦRKanHt1 : ∀Φ.monΦ→ ∀G∀H.Φt1 := λmλs.(m.1(leqRKan s)).1 monRKant2 : ∀Φ.monΦ→ ∀H.mon(ΦRKanH)t2 := λm.(m.0) monRKant3 : ∀Φ.monΦ→ ∀H.ΦRKanH ⊆ ΦHt3 := λm.(m.1 subRKan).0 monRKansmgm : ∀Φ.monΦ→ monΦsmgm := λmλsλfλx. t3m(t2mf(t1msx))

15

Page 16: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

4 Embedding into System Fω

The purpose of this section is a perspicuous proof of strong normalization ofGMIC. In fact, we show that the new constructor and term constants can alreadybe defined in system Fω so that typing is preserved, and that for the definedterms, one has in Fω that

Itκ→∗ms g (inκ→∗ f t) −→+ s g (m (Itκ→∗ms)f t)outκ→∗ f (Coitκ→∗ms g t) −→+ m (Coitκ→∗ms)f (s g t).

Therefore, if there is no typable term in Fω with an infinite reduction sequence,there is neither a typable term in GMIC allowing an infinite sequence of reductionsteps. In other words, strong normalization is inherited from that of Fω which inturn is a well-known fact [Gir72] (in that original work, only weak normalizationhas been proven but an extension to strong normalization is standard technologysince [Tai75]).

Let κ = κ→ ∗, set n := |κ|, and define for |f | = |g| = n

µκ := λFκ→κλY κ.monF → ∀Gκ.FG 6 G→ RKanG(Y )Itκ := λmλsλgλr.rmsg

inκ := λfλtλmλsλg.sg(m(Itκms)f t

)νκ := λFκ→κλY κ.monF × ∃Gκ.G 6 FG× LKanG(Y )Coitκ := λmλsλfλt.〈m, 〈〈s, 〈〈f1, . . . 〈〈fn, t〉〉 . . .〉〉〉〉〉outκ := λgλr.open (r.1, p0. open (p0.1, p1. . . . open (pn−1.1, pn. v) . . .))

with v := r.0(

Coitκ(r.0)(p0.0))g(p0.0(p1.0) . . . (pn.0)(pn.1)

)Compared with the classical encoding of (co-)inductive datatypes [Wra89,Geu92],the new ingredients are the relativization to monotone F and the use the Kanextensions. The purported reduction behaviour is a matter of trivial calculation,the typings are as follows:

ΛFκ→κλmmonFΛGκλsFG6GΛY κΛZκλgY 6ZλrµκFY .rmGsZg

proves that Itκ : ∀Fκ→κ. monF → ∀Gκ. F G 6 G→ µκF 6 G,

ΛFκ→κΛXκΛY κλfX6Y λtF (µκF )XλmmonFΛGκλsFG6GΛZκλgY 6Z .sYZgu

with u := m(µκF )G(ItκFmGs)XY f t proves inκ : ∀Fκ→κ. F (µκF ) 6 µκFsince, in the pending context, u has type FGY .

ΛFκ→κλmmonFΛGκλsG6FGΛXκΛY κλfX6Y λtGX .〈m, 〈G, 〈s, v〉〉〉

with v := 〈X1, 〈f1, . . . 〈Xn, 〈fn, t〉〉 . . .〉〉 : LKanG(Y ) proves that

Coitκ : ∀Fκ→κ. monF → ∀Gκ. G 6 F G→ G 6 νκF.

Finally, we prove outκ : ∀Fκ→κ. νκF 6 F (νκF ) byΛFκ→κΛY κΛZκλgY 6ZλrνκFY .open (r.1, Gκ.pG6FG×LKanG(Y )

0 . u) with u :=

open (p0.1, Xκ11 .p

X16Y1×LKanGX1 (Y2,...,Yn)1 . . . . open (pn−1.1, Xn.p

Xn6Yn×GXn . w) . . .)

16

Page 17: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

which in turn uses w :=

r.0G(νκF )(

CoitκF (r.0)G(p0.0))YZg

(p0.0X1Y1(p1.0) . . . XnYn(pn.0)(pn.1)

)where both u and w have type F (νκF )Z.

As in the proof of proposition 1, we wrote YZg for Y1Z1g1 . . . YnZngn. Like-wise, ΛY κΛZκλgY 6Z is written for ΛY κ1

1 ΛZκ11 λgY16Z1

1 . . . ΛY κnn ΛZκnn λgYn6Znn .Similar abbreviations are Zg, ΛZκλgY 6Z and the variants with X, Y and f .

By the reasoning above, this yields the following

Theorem 1 (Strong Normalization). Whenever Γ ` r : A in GMIC, then radmits no infinite reduction sequence r −→ r1 −→ r2 −→ . . . ut

5 Conclusions and Further Work

The system GMIC presented in this article is an idealized programming languagewith support for arbitrarily nested datatypes of arbitrary kind (i. e., higher ordertype transformers). The key ingredient is a notion of monotonicity which is widerthan any notion of positivity and goes far beyond polynomial or algebraic higher-order functors. We have shown that typical examples can be treated easily.

We would hope for many more examples that exploit the capabilities ofGMIC— examples that use

– nesting in the second sense of our introduction, i. e., simultaneous inductiveand coinductive constructors, combined with heterogenity,

– non-strict positivity, i. e., arguments that occur an even time to the left ofan arrow →,

– monotonicity of µκ1Φ for the formation of new datatypes such as µ∗(µκ1Φ),– µκ and νκ for rank rk(κ) > 1.

The next goal is a system of generalized (co-)iteration in the spirit of [BP99a]which has all the good meta-theoretic properties of GMIC, that is subject reduc-tion, confluence and strong normalization.

References

[Alt99] Thorsten Altenkirch. Logical relations and inductive/coinductive types. InGeorg Gottlob, Etienne Grandjean, and Katrin Seyr, editors, Computer Sci-ence Logic, volume 1584 of Lecture Notes in Computer Science, pages 343–354. Springer Verlag, 1999.

[Alt01] Thorsten Altenkirch. Representations of First Order Function Types as Ter-minal Coalgebras. In Fifth International Conference on Typed Lambda Cal-culi and Applications, 2001.

[AR99] Thorsten Altenkirch and Bernhard Reus. Monadic presentations of lambdaterms using generalized inductive types. In Computer Science Logic, 1999.

17

Page 18: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

[Bar92] Henk P. Barendregt. Lambda calculi with types. In Samson Abramsky,Dov M. Gabbay, and Tom S. E. Maibaum, editors, Background: Computa-tional Structures, volume 2 of Handbook of Logic in Computer Science, pages117–309. 1992.

[BFPS81] Wilfried Buchholz, Solomon Feferman, Wolfram Pohlers, and Wilfried Sieg.Iterated Inductive Definitions and Subsystems of Analysis: Recent Proof-Theoretical Studies, volume 897 of Lecture Notes in Mathematics. SpringerVerlag, 1981.

[BGJ00] Richard Bird, Jeremy Gibbons, and Geraint Jones. Program optimisation,naturally. In Millenial Perspectives in Computer Science, Palgrave, 2000.

[BM98] Richard Bird and Lambert Meertens. Nested datatypes. In Johan Jeuring,editor, Mathematics of Program Construction, MPC’98, Proceedings, volume1422 of Lecture Notes in Computer Science, pages 52–67. Springer Verlag,1998.

[BP99a] Richard Bird and Ross Paterson. Generalised folds for nested datatypes.Formal Aspects of Computing, 11(2):200–222, 1999.

[BP99b] Richard S. Bird and Ross Paterson. De Bruijn notation as a nested datatype.Journal of Functional Programming, 9(1):77–91, 1999.

[Geu92] Herman Geuvers. Inductive and coinductive types with iteration and re-cursion. In Bengt Nordstrom, Kent Pettersson, and Gordon Plotkin, ed-itors, Proceedings of the 1992 Workshop on Types for Proofs and Pro-grams, Bastad, Sweden, June 1992, pages 193–217, 1992. Only publishedvia ftp://ftp.cs.chalmers.se/pub/cs-reports/baastad.92/proc.dvi.Z.

[Gir72] Jean-Yves Girard. Interpretation fonctionnelle et elimination des coupuresdans l’arithmetique d’ordre superieur. These de Doctorat d’Etat, Universitede Paris VII, 1972.

[Hag87] Tatsuya Hagino. A typed lambda calculus with categorical type constructors.In D. H. Pitt, A. Poigne, and D. E. Rydeheard, editors, Category Theory andComputer Science, volume 283 of Lecture Notes in Computer Science, pages140–157. Springer Verlag, 1987.

[Hin00] Ralf Hinze. Efficient generalized folds. In Johan Jeuring, editor, Proceedingsof the Second Workshop on Generic Programming, WGP 2000, Ponte deLima, Portugal, July 2000.

[How80] W. A. Howard. The formulae-as-types notion of construction. In J. P. Seldinand J. R. Hindley, editors, To H. B. Curry: Essays on Combinatory Logic,Lambda Calculus and Formalism, pages 479–490. Academic Press, 1980.

[JBM98] C. Barry Jay, Gianna Belle, and Eugenio Moggi. Functorial ML. Journal ofFunctional Programming, 8(6):573–619, 1998.

[Lan98] Saunders Mac Lane. Categories for the Working Mathematician, volume 5of Graduate Texts in Mathematics. Springer Verlag, second edition, 1998.

[Lei90] Daniel Leivant. Contracting proofs to programs. In Piergiorgio Odifreddi,editor, Logic and Computer Science, volume 31 of APIC Studies in DataProcessing, pages 279–327. Academic Press, 1990.

[Mat98] Ralph Matthes. Extensions of System F by Iteration and Primitive Recursionon Monotone Inductive Types. PhD thesis, Ludwig-Maximilians-University,May 1998.

[Mat01] Ralph Matthes. Monotone inductive and coinductive constructors of rank 2.In Laurent Fribourg, editor, Proceedings of CSL 2001, volume 2142 of LectureNotes in Computer Science, pages 600–614. Springer Verlag, 2001.

18

Page 19: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

[Men87] Nax P. Mendler. Recursive types and type constraints in second-orderlambda calculus. In Proceedings of the Second Annual IEEE Symposiumon Logic in Computer Science, Ithaca, N.Y., pages 30–36. IEEE ComputerSociety Press, 1987.

[Oka99] Chris Okasaki. From fast exponentiation to square matrices: An adventure intypes. In Proceedings of the fourth ACM SIGPLAN International Conferenceon Functional Programming (ICFP ’99), Paris, France, September 27-29,1999, volume 34 of SIGPLAN Notices, pages 28–35. ACM, 1999.

[Tai75] William W. Tait. A realizability interpretation of the theory of species. InR. Parikh, editor, Logic Colloquium Boston 1971/72, volume 453 of LectureNotes in Mathematics, pages 240–251. Springer Verlag, 1975.

[UV97] Tarmo Uustalu and Varmo Vene. A cube of proof systems for the intuition-istic predicate µ-, ν-logic. In M. Haveraaen and O. Owe, editors, SelectedPapers of the 8th Nordic Workshop on Programming Theory (NWPT ’96),Oslo, Norway, December 1996, volume 248 of Research Reports, Departmentof Informatics, University of Oslo, pages 237–246, May 1997.

[Wra89] G. C. Wraith. A note on categorical datatypes. In D. H. Pitt, A. Poigne, andD. E. Rydeheard, editors, Category Theory and Computer Science, volume389 of Lecture Notes in Computer Science, pages 118–127. Springer Verlag,1989.

A System Fω

In the following we present Curry-style system Fω enriched with binary sums andproducts, empty and unit type and existential quantification over constructors.Although we choose a human-friendly notation of variables, we actually mean thenameless version a la de Bruijn which identifies α-equivalent terms. (Capture-avoiding) Substitution of an expression e for a variable x in expression f isdenoted by f [x := e].

Kinds and rank.

κ ::= ∗ | κ→ κ′

rk(∗) := 0rk(κ→ κ′) := max(rk(κ) + 1, rk(κ′))

Constructors. (Denoted by uppercase letters)

A,B, F,G ::= X | λXκ.F | F G | ∀Fκ. A | ∃Fκ. A | A→ B| 0 | A+B | 1 | A0 ×A1 × . . .×An (n > 1)

Equivalence on constructors. Equivalence F = F ′ for constructors F and F ′ isgiven as the compatible closure of the following axiom.

(λX.F )G =β F [X := G]

We identify constructors up to equivalence, which is a decidable relation due tonormalization and confluence of simply-typed λ-calculus (where our constructorsare the terms and our kinds are the types of that calculus).

19

Page 20: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Objects (Terms). (Denoted by lowercase letters)

r, s, t ::= x | λx.t | r s | abort r | inl t | inr t | case (r, x. s, y. t)| 〈t〉 (for |t| > 0) | r.n (for n > 0) | open (r, x. s)

Contexts. Variables in a context Γ are assumed to be distinct.

Γ ::= · | Γ,Xκ | Γ, x :A

Judgments. (Simultaneously defined)

Γ cxt Γ is a wellformed contextΓ ` F : κ F is a wellformed constructor of kind κ in context ΓΓ ` t : A t is a wellformed term of type A in context Γ

Wellformed contexts. Γ cxt

· cxt

Γ cxt

Γ,Xκ cxt

Γ ` A : ∗

Γ, x :A cxt

Wellkinded constructors. Γ ` F : κXκ ∈ Γ Γ cxt

Γ ` X : κ

Γ,Xκ ` F : κ′

Γ ` λXκ.F : κ→ κ′

Γ ` F : κ→ κ′ Γ ` G : κ

Γ ` F G : κ′

Γ,Xκ ` A : ∗

Γ ` ∀Xκ. A : ∗

Γ,Xκ ` A : ∗

Γ ` ∃Xκ. A : ∗

Γ ` A : ∗ Γ ` B : ∗

Γ ` A→ B : ∗Γ cxt

Γ ` 0 : ∗

Γ ` A : ∗ Γ ` B : ∗

Γ ` A+B : ∗Γ cxt

Γ ` 1 : ∗

Γ ` Ai : ∗ for 0 6 i 6 n with n > 1

Γ ` A0 ×A1 × . . .×An : ∗

Welltyped terms. Γ ` t : A

(x :A) ∈ Γ Γ cxt

Γ ` x : A

Γ, x :A ` t : B

Γ ` λx.t : A→ B

Γ ` r : A→ B Γ ` s : A

Γ ` r s : BΓ,Xκ ` t : A

Γ ` t : ∀Xκ.A

Γ ` t : ∀Xκ.A Γ ` F : κ

Γ ` t : A[X := F ]Γ ` t : A[X := F ] Γ ` F : κ

Γ ` 〈t〉 : ∃Xκ.A

Γ ` r : ∃Xκ.A Γ,Xκ, x :A ` s : C

Γ ` open (r, x. s) : CΓ cxt

Γ ` 〈〉 : 1

Γ ` t : A Γ ` B : ∗

Γ ` inl t : A+B

Γ ` t : B Γ ` A : ∗

Γ ` inr t : A+B

Γ ` r : A+B Γ, x :A ` s : C Γ, y :B ` t : C

Γ ` case (r, x. s, y. t) : C

Γ ` r : 0 Γ ` C : ∗

Γ ` abort r : CΓ ` ti : Ai for 0 6 i 6 n with n > 1

Γ ` 〈t〉 : A0 ×A1 × . . .×An

Γ ` r : A0 ×A1 × . . .×An i 6 n

Γ ` r.i : AiIn the last but one rule, t stands for t0, . . . , tn.

20

Page 21: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Reduction. The one-step reduction relation t −→ t′ between terms t and t′ isdefined as the closure of the following axioms under all term constructors.

(λx.t) s −→β t[x := s]case (inl r, x. s, y. t) −→β s[x := r]case (inr r, x. s, y. t) −→β t[y := r]〈t〉.i −→β ti if 0 6 i < |t|open (〈t〉, x. s) −→β s[x := t]

We denote the transitive closure of −→ by −→+ and the reflexive-transitiveclosure by −→∗.

The defined system is a conservative extension of system Fω. Reduction istype-preserving, confluent and strongly normalizing.

B Notation for Typing Derivations

As is usual, typing derivations for Fω will be communicated in a short-hand form,namely by the raw terms of a Church-style variant for which, given a context,typing and type-checking is a trivial matter. Here, we introduce those raw termssimultaneously with their typing rules.

The rules for x, rs, 〈〉, 〈t〉 and r.i are the same as for Fω. The others are:

Γ, x :A ` t : B

Γ ` λxA.t : A→ B

Γ ` r : 0 Γ ` C : ∗

Γ ` abortC r : C

Γ,Xκ ` t : A

Γ ` ΛXκt : ∀Xκ.A

Γ ` t : ∀Xκ.A Γ ` F : κ

Γ ` tF : A[X := F ]Γ ` t : A[X := F ] Γ ` F : κ

Γ ` 〈F, t〉∃Xκ.A : ∃Xκ.A

Γ ` r : ∃Xκ.A Γ,Xκ, x :A ` s : C

Γ ` open (r, Xκ.xA. s) : CΓ ` t : A Γ ` B : ∗

Γ ` inlA,B t : A+B

Γ ` t : B Γ ` A : ∗

Γ ` inrA,B t : A+B

Γ ` r : A+B Γ, x :A ` s : C Γ, y :B ` t : C

Γ ` case (r, xA. s, yB . t) : CAlthough in general important for type-checking, the index ∃Xκ.A in the ex-istential introduction rule will be suppressed if it is clear from the context ofdiscourse.

C Haskell code [to be omitted in the final version]

The following code is just an illustration that the examples can be representedin current Haskell implementations, e. g., it can be interpreted with hugs -98which allows rank-2 extensions to the type system. We apologize for severalformatting errors (due to automated conversion of the original Haskell sources)which are always visible and can easily be resolved by looking in the main text.[The code is included here in order to keep the referee(s) anonymous.]

21

Page 22: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

C.1 (Co)inductive types

module Rank1 where

Inductive types.

data Mu1 f = In1 (f (Mu1 f ))it1 :: Functor f ⇒ (f a → a)→ Mu1 f → ait1 s (In1 t) = s (fmap (it1 s ) t)

Coinductive types.

data Nu1 f = forall a.Coit1 (a → f a) aout1 :: Functor f ⇒ Nu1 f → f (Nu1 f )out1 (Coit1 s t) = fmap (Coit1 s) (s t)

C.2 (Co)inductive functors

module Rank2 (Functor2 ,Mu2 , In, it ,Nu2 ,Coit , out) where-- module Rank2 (Functor2, Mu2, it, Nu2, out) where

Rank 2 monotonicity.

class Functor2 ff whereffmap :: (forall a b. (a → b)→ f a → g b)→

(a → b)→ ff f a → ff g b

Inductive functors.

Formation and introduction.

data Mu2 ff b = forall a. In(a → b)(ff (Mu2 ff ) a)

Elimination and computation.

it :: Functor2 ff ⇒ (forall a b. (a → b)→ ff g a → g b)→(a → b)→ Mu2 ff a → g b

it s g (In f t) = s g (ffmap (it s) f t)

Functoriality.

instance Functor2 ff ⇒ Functor (Mu2 ff ) wherefmap = it In

22

Page 23: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Coinductive functors.

Formation and introduction.

data Nu2 ff b = forall g a.Coit(forall a b. (a → b)→ g a → ff g b)(a → b)(g a)

Elimination and computation.

out :: Functor2 ff ⇒ (a → b ) → Nu2 ff a → ff (Nu2 ff ) bout f (Coit s g t) = ffmap (Coit s) f (s g t)

Functoriality.

instance Functor2 ff ⇒ Functor (Nu2 ff ) wherefmap = Coit out

C.3 Powerlists

Powerlist reversal First, the version of powerlists where the map operationperforms a reversal of the whole list.

module RevPowerlist whereimport Prelude hiding (succ)import Rank2 (Functor2 ,Mu2 , In, it ,Nu2 ,Coit , out)

The constructor of pure kind 2 which we need to define powerlists. We take thefreedom and use labelled sums.

data PListF f a = Zero a | Succ (f (a, a))

We give a monotonicity witness which performs reversal.

swap :: (a → b)→ (a, a)→ (b, b)swap f (a1, a2) = (f a2, f a1)pListFRev :: (forall a b. (a → b)→ f a → g b)→

(a → b)→ PListF f a → PListF g bpListFRev s f (Zero a) = Zero ( f a)pListFRev s f (Succ l) = Succ ( s (swap f ) l)

instance Functor2 PListF whereffmap s f l = pListFRev s f l

Inductive type of powerlists.

23

Page 24: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

type PList a = Mu2 PListF a

Powerlist constructors.

zero :: a → PList azero a = In id (Zero a)succ :: PList (a, a)→ PList asucc l = In id (Succ l)

Fast powerlist reversal.

This is just the map function.

pListRev :: PList a → PList apListRev = fmap id

[Comment: begin comment

Convert to list.

unzipMap :: (a → b)→ [(a, a) ] → [b ]unzipMap f [ ] = [ ]unzipMap f ((a1, a2) : as) = f a1 : f a2 : unzipMap f as

stepList :: (a → b)→ PListF [ ] a → [b ]stepList f (Zero a) = [f a ]stepList f (Succ l) = unzipMap f l

toList :: PList a → [a ]toList l = it stepList id l

Test.

pl1 = succ (succ (succ (zero ( ( (1, 2), (3, 4)), ((5, 6), (7, 8))))))tl1 = toList pl1

end comment ]

Powerlist summation

module Powerlist whereimport Prelude hiding (succ, sum)import Rank2 (Functor2 ,Mu2 , In, it ,Nu2 ,Coit , out)

pair :: (a → b)→ (a, a)→ (b, b)pair f (a1, a2) = (f a1, f a2)

24

Page 25: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

The constructor of pure kind 2 which we need to define powerlists. We take thefreedom and use labelled sums.

data PListF f a = Zero a | Succ (f (a, a))instance Functor2 PListF where

ffmap s f (Zero a) = Zero ( f a)ffmap s f (Succ l) = Succ ( s (pair f ) l)

Inductive type of powerlists.

type PList a = Mu2 PListF a

Powerlist constructors.

zero :: a → PList azero a = In id (Zero a)succ :: PList (a, a)→ PList asucc l = In id (Succ l)

Summing up a powerlist of Integers.

We make use of the right Kan extension. Unfortunately, we cannot use a typedefinition and need a datatype instead.

newtype RKanInt a = RKanInt ((a → Integer)→ Integer)

Step term.

stepSum :: (a → b)→ PListF RKanInt a → RKanInt bstepSum f (Zero a) = RKanInt (λ g → g (f a))stepSum f (Succ (RKanInt l)) = RKanInt (λg → l (λ(a1, a2)→ g (f a1) + g (f a2)))

sum ′ :: (a → b)→ PList a → RKanInt bsum ′ = it stepSum

sum :: PList Integer → Integersum l = k id

where (RKanInt k) = sum ′ id l

[Comment:

Convert to list (simple version).

unzipMap :: (a → b)→ [(a, a) ] → [b ]unzipMap f [ ] = [ ]unzipMap f ((a1, a2) : as) = f a1 : f a2 : unzipMap f as

25

Page 26: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

stepListS :: (a → b)→ PListF [ ] a → [b ]stepListS f (Zero a) = [f a ]stepListS f (Succ l) = unzipMap f l

toListS :: PList a → [a ]toListS l = it stepListS id l

Convert to list.

newtype RKanList b = RKanList( forall c. (b → [c ])→ [c ])stepList :: (a → b)→ PListF RKanList a → RKanList bstepList f (Zero a) = RKanList (λg → g (f a))stepList f (Succ (RKanList l)) = RKanList (λg →

l (λ(a1, a2)→ g (f a1) ++ g (f a2)))toList :: PList a → [a ]toList l = k (λa → [a ]) where (RKanList k) = it stepList id l

Tests.

mkPairs :: [a ]→ [(a, a)]mkPairs [ ] = [ ]mkPairs (a1 : a2 : as) = (a1, a2) : mkPairs as

mkPList :: [a ]→ PList amkPList [ ] = error "not a powerlist"mkPList [a ] = zero amkPList l = succ (mkPList (mkPairs l))

l1 = mkPList [1, 2, 3, 4]l2 = mkPList [0, 1, 2, 3, 4, 5, 6, 7]

l1sum = sum l1l2sum = sum l2

pl1 = succ (succ (succ (zero ( ((1, 2), (3, 4)), ((5, 6), (7, 8))))))tl1 = toList pl1tl1S = toListS pl1

end of comment ]

C.4 De Bruijn terms

module DeBruijn whereimport Prelude hiding (abs)import Rank2

26

Page 27: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Rank 2 type constructor for a de Bruijn representation of lambda-terms.

data LamF f a = Var a | App (f a) (f a) | Abs (f (Maybe a))instance Functor2 LamF where

ffmap s f (Var a) = Var (f a)ffmap s f (App t1 t2 ) = App (s f t1 ) (s f t2 )ffmap s f (Abs r) = Abs (s (fmap f ) r)

Type of de Bruijn terms over a variable set A.

type Lam a = Mu2 LamF a

De Bruijn term constructors.

var :: a → Lam avar a = In id (Var a)app :: Lam a → Lam a → Lam aapp t1 t2 = In id (App t1 t2 )abs :: Lam (Maybe a)→ Lam aabs r = In id (Abs r)

Weakening.

weak :: Lam a → Lam (Maybe a)weak t = fmap Just t

Parallel substitution.

Step term.

newtype RKanLam b = RKanLam (forall c. ((b → Lam c)→ Lam c))stepSub :: (a → b)→ LamF RKanLam a → RKanLam bstepSub f (Var a) = RKanLam (λsigma → sigma (f a))stepSub f (App (RKanLam t1 ) (RKanLam t2 )) = RKanLam (λsigma →

app (t1 (sigma. f )) (t2 (sigma. f )))stepSub f (Abs (RKanLam r)) = RKanLam (λsigma →

abs (r (maybe (var Nothing) ( weak . sigma. f ))))

Substitution in general form.

subst ′ :: (a → b)→ Lam a → RKanLam bsubst ′ = it stepSub

Substitution (monad operation).

27

Page 28: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

subst :: Lam a → (a → Lam b)→ Lam bsubst t sigma = k sigma

where (RKanLam k) = subst ′ id t

Join operation.

join ′ :: (a → b)→ Lam (Lam a)→ Lam bjoin ′ f t = k id where (RKanLam k) = subst ′ (fmap f ) t

join :: Lam (Lam a)→ Lam ajoin t = subst t id

[Comment: begin comment

Example.

t = x(λy.yx)σ = [z(λw.zw)/x]

t1 :: Lam Intt1 = app (var 0) (abs (app (var Nothing) (var (Just 0))))sigma :: Int → Lam Stringsigma 0 = app (var "z") (abs (app (var (Just "z")) (var Nothing)))t1 ′ :: Lam Stringt1 ′ = subst t1 sigma

Printing.

newtype RKanString b = RKanString ((b → String)→ String)

stepPrint :: (a → b)→ LamF RKanString a → RKanString bstepPrint f (Var a) = RKanString (λp →

p (f a))stepPrint f (App (RKanString t1 ) (RKanString t2 )) = RKanString (λp →"(" ++ t1 (p. f ) ++ " " ++ t2 (p. f ) ++ ")")

stepPrint f (Abs (RKanString r) ) = RKanString (λp →"\\" ++ r (liftf p))where liftf p Nothing = "0"

liftf p (Just a) = "S" ++ p (f a)

printLam :: Lam a → (a → String)→ StringprintLam t p = k p where (RKanString k) = it stepPrint id t

instance Show a ⇒ Show (Lam a) whereshow t = printLam t show

end comment ]

28

Page 29: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

C.5 Functions over binary trees

module BTFun whereimport Prelude hiding (span, head , tail)import Rank1import Rank2

Binary trees without content.

data BTF a = Leaf | Span{ left :: a, right :: a }instance Functor BTF where

fmap f Leaf = Leaffmap f (Span t u) = Span (f t) (f u)

type BT = Mu1 BTFleaf :: BTleaf = In1 Leafspan :: BT → BT → BTspan t u = In1 (Span t u)

Functions over binary trees as coinductive datatype. (Thorsten Altenkirch)

data TFunF f a = Cons{hd :: a, tl :: (f (f a))}instance Functor2 TFunF where

ffmap s f (Cons a t) = Cons (f a) (s (s f ) t)type TFun a = Nu2 TFunF a

Destructors.

head :: TFun a → ahead b = hd (out id b)tail :: TFun a → TFun (TFun a)tail b = tl (out id b)

Creating a TFun from a function over BT by coiteration.

newtype BTto a = BTto (BT → a)

stepLam :: (a → b)→ BTto a → TFunF BTto bstepLam f (BTto g) = Cons (f (g leaf ))

(BTto (λt → BTto (λu → f (g (span t u)))))lamBT ′ :: (a → b)→ (BT → a ) → TFun blamBT ′ f g = Coit stepLam f (BTto g)lamBT :: (BT → a)→ TFun alamBT g = lamBT ′ id g

29

Page 30: (Co-)Iteration for Higher-Order Nested Datatypesabel/itOmega.pdf · 2002. 12. 17. · 2 PPS, CNRS, Universit e Paris VII (on leave from University of Munich) matthes@informatik.uni-muenchen.de

Applying a TFun to a BT.

newtype TFto = TFto (forall a.TFun a → a)

stepApp :: BTF TFto → TFtostepApp Leaf = TFto (λb → head b)stepApp (Span (TFto l) (TFto r) ) = TFto (λb → r (l (tail b)))

appBT :: BT → TFun a → aappBT t = g where (TFto g) = it1 stepApp t

30