COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR ...

0 downloads 130 Views 460KB Size Report
Jan 6, 2014 - over relative comonads. The other, more important difference between the inductive and the coinduc- ... Tr
COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

arXiv:1401.1053v1 [cs.LO] 6 Jan 2014

BENEDIKT AHRENS AND RÉGIS SPADOTTI

Abstract. Infinite triangular matrices and, in particular, the redecoration operation on them, were studied by Matthes and Picard. In their work, redecoration is characterized as the cobind operation of what the authors call a “weak constructive comonad”. In this work, we identify weak constructive comonads as an instance of the more general notion of relative comonad. Afterwards, building upon the work by Matthes and Picard, we give a category-theoretic characterisation of infinite triangular matrices—equiped with the canonical bisimulation relation and a compatible comonadic cobind operation—as the terminal object in some category.

1. Introduction Simple inductive types—W-types—are characterised categorically as initial algebras of a polynomial functor [14]. Dually, infinite, coinductive types are characterised as terminal coalgebras of suitable functors. In the case of coinductive types, the meta-theoretic notion of equality is not adequate. Another notion of sameness for inhabitants of such types, bisimulation, was introduced by Aczel [4], and also a new proof principle called coinduction. Modulo this slight complication, it looks like it suffices to dualize the theory of inductive types in order to obtain a complete picture of coinductive types. The characterisation of inductive types as initial algebras has been extended to heterogeneous—also called nested —inductive datatypes, e.g., the type of λ-terms, in various formulations [8, 10]. The main goal of these works is not just to characterize the data type via a universal property, but rather the data type equiped with a well-behaved substitution operation. In this work we set off to characterize a specific coinductive heterogeneous datatype—the type family Tri of infinite triangular matrices as studied by Matthes and Picard [12], equiped with a cosubstitution operation—as a terminal object of some category. For this, we start dualizing the approach by Hirschowitz and Maggesi [10], where the crucial notions are the notion of monad and, more importantly, module over a monad. It turns out that more work than a simple dualization is necessary, for two reasons which we indicate in the following. For a beautiful description and analysis of the codata type of infinite triangular matrices we refer to the article by Matthes and Picard [12] which is available via open access. The reader is strongly urged to read (the first two sections of) that work before the present one; here we only recall the most important definitions: the codata type family Tri of infinite triangular matrices is parametrized by a fixed set for entries not on the diagonal, and indexed by another, variable, set for entries on the diagonal. The respective types of its specifying destructors top and rest are given 1

2

BENEDIKT AHRENS AND RÉGIS SPADOTTI

in Figure 1. A first difference to the inductive case of the lambda calculus springs to mind: while the lambda calculus is essentially a monad—hence an endofunctor—on sets, the codata type Tri cannot be considered as such; for a given set A, the type TriA, equiped with a suitable bisimulation relation, constitutes a setoid —a set equiped with an equivalence relation—rather than a set. Instead of considering comonads—as the dual notion to monads—we thus have to consider a notion of comonad whose underlying functor is not necessarily endo. This is precisely the raison d’être of relative comonads (defined below)—the dual notion to the relative monads defined in [6]. Fortunately, relative comonads have all the nice properties we could ask for; in particular, the notion of module over a monad of [10] extends to modules over relative monads [5], which can be dualized to obtain comodules over relative comonads. The other, more important difference between the inductive and the coinductive case stems from heterogeneity: Two dual operations are responsible for the heterogeneity of the data type of lambda terms and the codata type of infinite triangular matrices: while we precompose with taking a coproduct in the case of the abstraction of the lambda calculus—in order to account for context extension when passing under a binder—, we precompose with taking a product when giving the target of the rest destructor of Tri. However, as it turns out, the treatment of the rest destructor is far more complicated than a dualization of that of the abstraction constructor of the lambda calculus—see Remark 21 for a detailed explanation. Describing our approach to a category-theoretic treatment of the rest destructor is the main purpose of the present work. All our definitions, examples and lemmas have been implemented in the proof assistant Coq [7]. In this document, we hence omit the proofs and focus on definitions and statements of lemmas. Some more related work. Some related work has been mentioned in the introduction. Traditional comonads for functional programming with homogeneous codata types are studied in [16]. Redecoration for both finite and infinite triangular matrices is used in [1] to exemplify the expressivity of the studied recursion schemes. Organisation of the paper. In Section 2 we introduce some concepts and notations used later on. In Section 3 we define the category of comonads relative to a fixed functor. In Section 4 we define comodules over relative comonads and give some constructions of comodules. In Section 5 we explain an important difference between the lambda calculus and infinite triangular matrices as heterogeneous data types. We then set up the necessary definitions to state our main theorem (Theorem 30), characterizing the codata type Tri as terminal object in some category. In Section 6 we explain some details of the formalization of this work in the proof assistant Coq. A table with the correspondances between formal and informal definitions is given in Appendix A. 2. Preliminaries In this paper we work in a type-theoretic foundational system and thus use type-theoretic notation, writing t : T to indicate that t is of type T . Whenever an operation takes several arguments, we write some of them as indices; these indices might be omitted when they can be deduced from the type of the later arguments.

COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

3

We assume basic knowledge of category theory; any instances used are defined in the following. Definition 1 (Some categories). We denote by Set the category of sets and total functions. We denote by Setoid the category an object of which is a set equiped with an equivalence relation. A morphism between setoids is a set-theoretic function between the underlying sets that is compatible with the equivalence relations of the source and target setoids. Given a setoid T , we write t ∼ t0 for related elements t and t0 in T. Definition 2. The functor eq : Set → Setoid is defined as the left adjoint to the forgetful functor U : Setoid → Set. Explicitly, the functor eq sends any set X to the setoid (X, =X ) given by the set X itself, equiped with the equality relation =X on X. Remark 3 (Notation for product). We denote the category-theoretic binary product of objects A and B of a category C by A × B. We write pr1 (A, B) : C(A × B, A) and pr2 (A, B) : C(A × B, B) for the projections, and we omit the argument (A, B) when it is clear from the context. Given f : C(A, B) and g : C(A, C), we write hf, gi : C(A, B × C) for the induced map into the product such that pr1 ◦ hf, gi = f and pr2 ◦ hf, gi = g. Both of the categories of Definition 1 have binary products; they are cartesian monoidal, i.e. the terminal object is neutral with respect to the multiplication. Functors preserving the monoidal structure up to isomorphism are called strong monoidal : Definition 4 (Strong monoidal functor). A functor F : C → D between cartesian monoidal categories is strong monoidal if, for any two objects A and B of C, the morphism

 F αA,B := F (pr1 ), F (pr2 ) : D F (A × B), F A × F B is an isomorphisms. (Note that for cartesian monoidal categories, the family α of morphisms automatically makes commute the diagrams necessary to have a strong monoidal functor, since it is given by universal property. We shall not need more general strong monoidal functors between arbitrary monoidal categories.) Example 5. The functor eq : Set → Setoid of Definition 2 is strong monoidal. 3. Relative comonads and their morphisms Relative monads were defined by Altenkirch, Chapman, and Uustalu [6] as a notion of monad-like structure whose underlying functor is not necessarily an endofunctor. The dual notion is that of a relative comonad: Definition 6 (Relative comonad). Let F : C → D be a functor. A relative comonad T over F is given by • a map T : C0 → D0 on the objects of the categories involved; • an operation counit : ∀A : C0 , D(T A, F A); • an operation cobind : ∀A, B : C0 , D(T A, F B) → D(T A, T B) such that • ∀A, B : C0 , ∀f : D(T A, F B), counitB ◦ cobind(f ) = f ;

4

BENEDIKT AHRENS AND RÉGIS SPADOTTI

• ∀A : C0 , cobind(counitA ) = idA ; • ∀A, B, C : C0 , ∀f : D(T A, F B), ∀g : D(T B, F C), cobind(g) ◦ cobind(f ) = cobind(g ◦ cobind(f )). Just like relative monads, relative comonads are functorial: Definition 7 (Functoriality for relative comonads). Let T be a comonad relative to F : C → D. For f : C(A, B) we define liftT (f ) := cobind(F f ◦ counitA ). The functor properties are easily checked. Relative comonads over the identity functor are exactly comonads. A nontrivial instance of relative comonads is given by the coinductive datatype of infinite triangular matrices studied by Matthes and Picard [12]. We suggest the reader consults the first two sections of their work in order to understand this example. Example 8 (Matthes and Picard [12]). Let E be an arbitrary fixed set. Infinite triangular matrices over the (variable) type A are given by the destructors shown in Figure 1, equiped with a coinductively defined equivalence relation, a bisimulation. We write t ∼ t0 for related matrices t and t0 . t : Tri(A)

t : Tri(A)

topA (t) : A

restA (t) : Tri(E × A)

t ∼ t0

t ∼ t0

top(t) = top(t0 )

rest(t) ∼ rest(t0 )

Figure 1. Destructors and bisimulation for the coinductive family Tri Matthes and Picard [12] define a notion of weak constructive comonad, and show that the datatype Tri is an instance of such a comonad. Such a weak constructive comonad, however, is precisely a comonad relative to the functor eq : Set → Setoid. The counit is given by the operation top. The cobind operation—called redecoration or redec in [12]— of type redecA,B : Setoid(TriA, eqB) → Setoid(TriA, TriB) is defined corecursively, for f : Setoid(TriA, eqB) and t : TriA by  top redec f t := f t and   (3.1) rest redec f t := redec extend f (rest t) . Here, the family of functions extendA,B : Setoid(TriA, eqB) → Setoid Tri(E × A), eq(E × B)



is suitably defined to account for the change of the type of argument of redec when redecorating rest t : Tri(E × A) rather than t : TriA, namely extend(f ) := hpr1 (E, A) ◦ topE×A , f ◦ cutA i .

COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

5

The auxiliary function cutA : Tri(E × A) → TriA is defined corecursively via  top cut t := pr2 (top t) and  rest cut t := cut(rest t) . Note that what is called extend here is called lift in [12]. Remark 9. In the definition of the extend operation in Example 8 the auxiliary operation cut plays a central rôle. It should be remarked here that, using functoriality of Tri, one can define another operation of the same type, namely the family of maps Tri(pr2 (E, A)) : Tri(E × A) → TriA for all sets A. Had we used this operation rather than the corecursively defined cut of Example 8, then the third comonad law would have failed; we refer to [13] for details. The notion of relative comonad captures many properties of Tri and its redecoration operation, in particular the interplay of redecoration with the destructor top via the first two comonad laws. Much of the rest of the present work is concerned with finding a suitable categorical notion that captures the interplay of redecoration with the other destructor, rest. Once we have found such a categorical notion, we can use it to give a definition of what a “coalgebra” for the signature of infinite triangular matrices is, where the codata type Tri is supposed to constitute the terminal object. A first attempt might be to characterize rest as a morphism of comonads: Definition 10 (Morphism of relative comonads). Let T and S be comonads relative to a functor F : C → D. A morphism of relative comonads τ : T → S is given by a family of morphisms τA : D(T A, SA) such that for any A : C0 , counitTA = counitSA ◦ τA and for any A, B : C0 and f : D(SA, F B), τB ◦ cobindT (f ◦ τA ) = cobindS (f ) ◦ τA . Relative comonads over a fixed functor F and their morphisms form a category RComonad(F ). Remark 11. A morphism τ : T → S of relative comonads over a functor F : C → D is natural with respect to the functorial action of Definition 7. At first glance, one might hope for rest to constitute the carrier of a morphism of relative comonads, i.e. one might guess that the axioms of comonad morphism capture the interplay of redecoration with the destructors top and rest. Indeed, it is possible to equip the target Tri(E × _) of rest with the structure of comonad—we show one such possibility in Definition 12. However, we have not found any way to endow Tri and Tri(E × _) with a comonad structure in a way that turns rest into a morphism of comonads. Definition 12. Let T be a comonad relative to a strong monoidal functor F : C → D between cartesian monoidal categories, and let E : C0 be a fixed object of C. The map A 7→ T (E × A) inherits the structure of a comonad relative to F from T : the counit is defined as counitA := F pr2 (E, A) ◦ counitTE×A

6

BENEDIKT AHRENS AND RÉGIS SPADOTTI

and the cobind operation as   cobindA,B : D T (E × A), F B → D T (E × A), T (E × B) f 7→ cobindT (extend0 f ) with extend0 defined as   extend0 : D T (E × A), F B → D T (E × A), F (E × B) , F f 7→ αE,B

−1

◦ hcounitTE ◦ T (pr1 ), f i .

Using such a comonad as the target comonad for rest, one might now try to characterize the destructor rest of the triangular matrices of Example 8 as a morphism of relative comonads. However, this fails already for the law involving counit: we would have to prove top = pr2 ◦ top ◦ rest, which fails each time the first two entries on the diagonal are not equal. We leave it up to the reader to find counterexamples for the comonad law involving cobind. For heterogeneous inductive types, Hirschowitz and Maggesi [10] solve an analogue question by considering the notion of module over a monad. We dualize their approach by introducing comodules over (relative) comonads. 4. Comodules over relative comonads In this section we dualize the notion of module over a relative monad as developed in [5]. This dualization is straightforward. Definition 13 (Comodule over relative comonad). Let T be a relative comonad over the functor F : C → D, and let E be a category. A comodule over T towards E consists of • a map : C0 → E0 on the objects of the categories involved; • an operation mcobind : ∀A, B : C0 , D(T A, F B) → E(M A, M B) such that • ∀A : C0 , mcobind(counitA ) = idA ; • ∀A, B, C : C0 , ∀f : D(T A, F B), ∀g : D(T B, F C), mcobind(g) ◦ mcobind(f ) = mcobind(g ◦ cobind(f )) . Every relative comonad comes with a canonical comodule over it, the tautological comodule: Definition 14 (Tautological comodule). Given a comonad T relative to F : C → D, the assignment A 7→ T A yields a comodule over T with target category D, the tautological comodule of T , also called T . The comodule operation is given by mcobindT (f ) := cobindT (f ) . A morphism of comodules is given by a family of morphisms that is compatible with the comodule operation: Definition 15 (Morphism of comodules). Let M, N : C → E be comodules over the comonad T relative to F : C → D. A morphism of comodules from M to N is given by a family of morphisms αA : E(M A, N A) such that for any A, B : C0 and f : D(T A, F B) one has αB ◦ mcobindM (f ) = mcobindN (f ) ◦ αA .

COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

7

Composition and identity of comodule morphisms happens pointwise. We thus obtain a category of comodules over a fixed comonad, towards a fixed target category: Remark 16 (Category of comodules). Fix a comonad T relative to F : C → D and a category E. Comodules over T towards E and their morphisms form a category RComod(T, E). Similarly to relative comonads, comodules over these are functorial: Definition 17 (Functoriality for comodules). Let M : RComod(T, E) be a comodule over T towards some category E. For f : C(A, B) we define mliftS (f ) := mcobind(F f ◦ counitA ). Remark 18. The family of morphisms constituting a comodule morphism is actually natural with respect to the functoriality defined in Definition 17. Given a morphism of comonads, we can “transport” comodules over the source comonad to comodules over the target comonad: Definition 19 (Pushforward comodule). Let τ : T → S be a morphism of comonads relative to a functor F : C → D, and let furthermore M be a comodule over T towards a category E. We define the pushforward comodule τ∗ M to be the comodule over S given by τ∗ M (A) := M A and, for f : D(SA, F B), mcobindτ∗ M (f ) := mcobindM (f ◦ τA ) . Pushforward is functorial: if M and N are comodules over T with codomain category E, and α : M → N is a morphism of comodules, then we define τ∗ α : τ∗ M → τ∗ N as the family of morphisms (τ∗ α)A := αA . It is easy to check that this is a morphism of comodules (over S) between τ∗ M and τ∗ N . Pushforward thus yields a functor τ∗ : RComod(T, E) → RComod(S, E). Definition 20 (Morphism of comonads induces morphism of comodules). Let τ : T → S be a morphism of comonads relative to a functor F : C → D. Then τ gives rise to a morphism of comodules over S from the pushforward of the tautological comodule of T along τ to the tautological comodule over S, hτ i : τ∗ T → S ,

hτ iA := τA .

5. Coalgebras for infinite triangular matrices Our goal is to define coalgebras for the signature of infinite triangular matrices such that the codata type Tri defined in Example 8 constitutes the terminal such coalgebra. Since Tri forms a comonad relative to the functor eq : Set → Setoid, it seems reasonable to define an arbitrary coalgebra as such a relative comonad as well, equiped with some extra structure which remains to be defined. A morphism of coalgebras would then be defined as a morphism of relative comonads that is compatible with that extra structure.

8

BENEDIKT AHRENS AND RÉGIS SPADOTTI

The purpose of this extra structure is to characterize the destructor rest, that is, its type as well as its behaviour with respect to the redecoration operation. Dualizing the approach of Hirschowitz and Maggesi [10], we hope to characterize rest as a morphism of comodules of specified type, which would allow us to define the extra structure of an arbitrary coalgebra as a suitable comodule morphism as well. In order to turn rest into a morphism of comodules, the crucial task is to suitably endow the source and target functors Tri and Tri(E × _), respectively, with a comodule structure over the comonad Tri. For this, we now take a closer look at how the extend operation for Tri is defined. Its purpose is to modify the function f used for redecoration, when redecorating rest t : Tri(E × A) rather than t : TriA. First we note an important difference between the extend operation and its analogue in the inductive case: Remark 21 (shift operation for the lambda calculus). The heterogeneous data type of lambda terms comes with a term constructor absA : LC(A∗ ) → LCA, which is the constructor responsible for the heterogeneity of that data type. We compare the extend operation to the one with analogue purpose in the case of the lambda calculus, the shift operation, of type  (5.1) shiftA,B : Set(A, LCB) → Set A∗ , LC(B ∗ ) . Here LCA denotes the set of lambda terms in context A, and A∗ := 1 + A denotes the context A extended by a distinguished variable—the variable bound by the abstraction constructor. The shift operation is used in the definition of the monadic substitution operation substA,B : Set(A, LCB) → Set(LCA, LCB) of the lambda calculus, when recursively substituting under an abstraction:  subst f (abs t) := abs subst (shift f ) t . The reader is encouraged to compare this equation to Equation (3.1). The shift operation can be defined in monadic terms, for any monad T on the category of sets such that, when T is instantiated by the monad LC, it becomes the specific shift operation of Equation (5.1). However, we were not able to express the extend operation of Tri entirely in terms of categorical and comonadic operations. Indeed, if one dualizes the categorical definition of shift, one obtains an operation that has the same type as extend, but for which, when used to define the redecoration, some comonadic property of redecoration fails. More specifically, it is the auxiliary cut operation as defined in Example 8 which does not seem to be definable in categorical terms. There is an operation of the same type which is categorical—see Remark 24—but which, when used for the definition of the extend operation, yields a “redecoration” operation for which the third comonad law fails. Details are given in an article by Matthes and Strecker [13]. The failure to describe the extend operation—more precisely, the auxiliary cut—in categorical terms leads us to considering comonads that come with a specified cut operation for some fixed object E in the source category: Definition 22 (Relative comonad with cut). Let C and D be categories with binary products and F : C → D a strong monoidal functor. Let E : C0 be a fixed object of

COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

9

C. We define a comonad relative to F with cut relative to E to be a comonad T relative to F together with a cut operation cut : ∀ A : C0 , T (E × A) → T A satisfying the following axioms: • ∀ A : C0 , counitA ◦ cutA = F (pr2 (E, A)) ◦ counitE×A ; • ∀ A B : C0 , ∀ f : D(T A, F B), cobind(f ) ◦ cutA = cutB ◦ cobind(extend f ),  where, for f : D(T A, F B), we define extend(f ) : D T (E × A), F (E × B) as F extend(f ) := αE,B

−1

◦ (counitE × f ) ◦ hT (pr1 ), cuti .

Morphisms of comonads with cut are morphisms of comonads that are compatible with the respective cut operations: Definition 23 (Morphism of comonads with cut). Let (T, cutT ) and (S, cutS ) be two comonads relative to a functor F with cut relative to E as in Definition 22. A morphism of comonads with cut is a comonad morphism τ between the underlying comonads as in Definition 10 that commutes suitably with the respective cut operations, i.e. for any A : C0 , cutSA ◦ τE×A = τA ◦ cutTA . Comonads with cut relative to a fixed functor F : C → D and E : C0 form a category RComonadwCut(F, E). There is the obvious forgetful functor from RComonadwCut(F, E) to RComonad(F ). Conversely, any comonad T relative to a suitable functor can be equiped with a cut operation, using functoriality of T : Remark 24 (Canonical cut operation). Any comonad T relative to a functor F as in Definition 22 can be equiped with a cut operation by setting  cutA := liftT pr2 (E, A) . It follows from the axioms of comonad morphism that a comonad morphism τ : T → S satisfies the equation of Definition 23 for the thus defined operations cutT and cutS , hence constitutes a morphism of comonads with cut from (T, cutT ) to (S, cutS ). The functor given by the canonical cut operation, followed by the forgetful functor, yields the identity. We can thus view relative comonads with cut as a generalization of relative comonads. Our prime example of relative comonad comes with a cut operation that is not the canonical one: Example 25 (cut for Tri). The relative comonad Tri from Example 8, together with the cut operation defined there, is a comonad with cut as in Definition 22. Given a comodule M over a relative comonad with cut, we define a comodule over the same comonad obtained by precomposition of M with “product with a fixed object E”: Definition 26 (Precomposition with product). Suppose F : C → D is a strong monoidal functor, and T is a comonad relative to F with a cut operation relative to E : C0 as in Definition 22. Given a comodule M over T , precomposition with “product with E” gives a comodule M (E × _) over T , M (E × _) : A 7→ M (E × A) .

10

BENEDIKT AHRENS AND RÉGIS SPADOTTI

The comodule operation is deduced from that of M by M (E×_)

mcobindA,B

 : D(T A, F B) → E M (E × A), M (E × B) , f 7→ mcobindM E×A,E×B (extend(f )) ,

where the extend operation is the one defined in Definition 22. Furthermore, given two comodules M and N over T with target category E, and a comodule morphism α : M → N , then we can define a comodule morphism α(E × _) : M (E × _) → N (E × _) by setting α(E × _)A := αE×A . We thus obtain an endofunctor on the category of comodules over T towards E, M 7→ M (E × _) : RComod(T, E) → RComod(T, E) . Before we give an example—really, the reason we did all this work—using such a comodule, we state some more facts about the construction just described: Remark 27 (Pushforward commutes with product in context). Note that the constructions of Definition 26 and Definition 19 commute in the sense that we have an isomorphism of comodules ∼ (τ∗ M )(E × _) τ∗ (M (E × _)) = given pointwise by identity morphisms. It directly follows from the definition that the cut operation of any comonad T with cut constitutes a comodule morphism cut : T (E × _) → T . We can thus restate the definiton of a morphism of comonads with cut as in Definition 23 by asking the following diagram of comodule morphisms (in the category RComod(S, D)) to commute (where in the upper left corner we silently add an isomorphism as in Remark 27): τ∗ T (E × _)

τ∗ (cutT )

hτ i(E×_)

 S(E × _)

/ τ∗ T hτ i

cutS

 /S .

The construction of Definition 26 yields a categorical characterization of the rest deconstructor—more precisely, of Equation (3.1), via the notion of comodule morphism: Example 28 (rest is a comodule morphism). Consider the comonad Tri, equiped with the cut operation of Example 25. The destructor rest of Example 8 is a morphism of comodules over the comonad Tri from the tautological comodule Tri to Tri(E × _). We now define a category in which the codata type of triangular matrices, equiped with the bisimulation relation of Example 8 and the comonadic redecoration, constitutes the terminal object. We call an object of this category a coalgebra of triangular matrices, even though we do not define the category as a category of coalgebras of a given endofunctor.

COINITIAL SEMANTICS FOR REDECORATION OF TRIANGULAR MATRICES

11

The definition of such a coalgebra is obtained by collecting all the information we have gathered so far about the comonad Tri, and abstracting from this particular comonad: Definition 29 (Coalgebras of infinite triangualar matrices). Let E : Set0 be a set. Let T = TE be the category where an object consists of • a comonad T over the functor eq : Set → Setoid with cut relative to E and • a morphism rest of comodules over T of type T → T (E × _) such that for any set A, restA ◦ cutA = cutE×A ◦ restE×A . The last equation can be stated as an equality of comodule morphisms as  rest ◦ cut = cut(E × _) ◦ rest(E × _) = (cut ◦ rest)(E × _) . A morphism between two such objects (T, restT ) and (S, restS ) is given by a morphism of relative comonads with cut τ : T → S such that the following diagram of comodule morphisms in the category RComod(S, E) commutes, τ∗ T

τ∗ (restT )

hτ i(E×_)

hτ i

 S

/ τ∗ T (E × _)

restS

 / S(E × _) .

Here in the upper right corner we silently insert an isomorphism as in Remark 27. We now have all the ingredients to state (and prove) our main theorem: Theorem 30 (Coinitial semantics for triangular matrices with redecoration). The pair (Tri, rest) consisting of the relative comonad with cut Tri of Example 25 together with the morphism of comodules rest of Example 28, constitutes the terminal— “coinitial”—coalgebra of triangular matrices. Proof sketch. For a given coalgebra (T, restT ), the (terminal) morphism = T : T → Tri is defined via the corecursive equations  (5.2) top t := counitT t and  (5.3) rest t := (restT t) . By coinduction we show that the map is compatible with cobind and cut operations of the source and target coalgebras. We omit these calculations, which can be consulted in the Coq source files. Note that there is actually no choice in this definition: Equation (5.2) is forced upon us since we want to constitute a morphism of comonads—the equation directly corresponds to one of the axioms. Equation (5.3) is forced upon us by the diagram a morphism of coalgebras has to make commute. The same argument is used to show, again by coinduction, that any two morphisms of coalgebras τ, ρ : (T, restT ) → (Tri, rest) are equal (i.e. extensionally bisimilar), thus concluding the proof. 

12

BENEDIKT AHRENS AND RÉGIS SPADOTTI

6. Formalization in Coq All our definitions and theorems are mechanized in the proof assistant Coq [7]. The formalization of infinite triangular matrices is taken from the work by Matthes and Picard [12], and only slightly adapted to compile with the version of Coq we use. In the following we explain some of our design choices for this mechanization and point out differences between the pen-and-paper definitions and the mechanized ones. 6.1. Implementation choices. We explain two choices we made in the course of the formalization in Coq. The first choice concerns the formalization of categories, more precisely, how to formalize equality of morphisms. The second choice concerns the formalization of algebraic structures. 6.1.1. Setoids for hom-sets. We formalize categories to be given by a type of objects and a dependent type—indexed by pairs of objects—of morphisms, equiped with suitable composition and identity operations satisfying appropriate axioms. More precisely, the family of morphisms is given by a family of setoids, where the setoidal equivalence relation on each type of morphisms denotes the equality relation on these morphisms. This approach was first used by Aczel [3] in the proof assistant LEGO, and also by Huet and Saïbi [11] in their library of category theory in Coq. Alternatively, we could have chosen to consider morphisms modulo propositional equality. Indeed, the morphisms we consider—morphisms of comonads and comodules—are given by structures bundling a lot of data and properties; in order to consider two such morphisms as equal, we usually only compare one field of the corresponding records. Furthermore, this field usually consists of a (dependent) function. It would be rather cumbersome to reduce equality of two such records to extensional equality of one of their fields, necessitating the use of the axioms of propositional and functional extensionality. Using setoids for morphisms instead seems to come with less overhead and conceptually cleaner. 6.1.2. Records vs. classes. Two approaches to the formalization of mathematical structures have been used extensively in Coq: on the one hand, packaging structures in record types in combination with use of canonical structures, is used with success, e.g., in the formalisation of algebraic structure in the context of the proof of the Feit-Thompson theorem [9]. On the other hand, Spitters and Weegen [15] suggest the use of type classes, in particular when multiple inheritance is an issue. In the present formalization, we decide to use records rather than classes, since the strongest argument for type classes—multiple inheritance—does not occur. We make use of canonical structures in order for Coq to deduce instance of categories when we mention objects of a category; in particular, this is used to allow for overloading of the notation for morphisms of a category. We can thus conveniently use the same arrow symbol to denote the type of morphisms between two comonads, between two comodules and so on. 6.2. Formal vs. informal definitions. The only noteworthy discrepancy between formal and informal definition arises in the definition of the codata type of infinite triangular matrices: in Coq, coinductive types are specified through constructors rather than destructors. However, one can easily define inverse functions and work

REFERENCES

13

in terms of destructors afterwards. Work on the the declaration of coinductive types via destructors is done in Agda, cf. [2]. Other than that, the informal definitions correspond closely to the formalized ones—which might also stem from the fact that the formalization was used as a research tool, playing an essential rôle in the development of this work. 7. Conclusions and future work We have given a category-theoretic characterisation, via a universal property, of infinite triangular matrices equiped with a comonadic redecoration operation. While a significant part of our work—surmounting the non-categoricity of the cut operation—seems to be specific to this particular codata type, we believe that our work proves the suitability of the notion of relative (co)monads and (co)modules thereover for a categorical treatment of coinductive data types. We plan to pursue two obvious lines of work: Firstly, we will work on a suitable notion of signature for the specification of coinductive data types. Secondly, we hope to integrate equations into the notion of signature, equations which will allow, e.g, considering branching trees modulo permutation of subtrees. Acknowledgments. We thank Ralph Matthes and Paige North for many helpful discussions, and André Hirschowitz for providing feedback on a draft of this paper. References [1] Andreas Abel, Ralph Matthes, and Tarmo Uustalu. “Iteration and coiteration schemes for higher-order and nested datatypes”. In: Theor. Comput. Sci. 333.1-2 (2005), pp. 3–66 (cit. on p. 2). [2] Andreas Abel, Brigitte Pientka, David Thibodeau, and Anton Setzer. “Copatterns: programming infinite structures by observations”. In: POPL. Ed. by Roberto Giacobazzi and Radhia Cousot. ACM, 2013, pp. 27–38 (cit. on p. 13). [3] Peter Aczel. Galois: A Theory Development Project. Technical Report for the 1993 Turin meeting on the Representation of Mathematics in Logical Frameworks. 1993. url: http://www.cs.man.ac.uk/~petera/papers.html (cit. on p. 12). [4] Peter Aczel. Non-Well-Founded Sets. Vol. 14. CSLI Lecture Notes. Center for the Study of Languages and Information, 1988 (cit. on p. 1). [5] Benedikt Ahrens. Modules over relative monads for syntax and semantics. Accepted for publication in Math. Struct. in Comp. Science. arXiv:1107.5252 (cit. on pp. 2, 6). [6] Thorsten Altenkirch, James Chapman, and Tarmo Uustalu. “Monads Need Not Be Endofunctors”. In: FOSSACS. Ed. by C.-H. Luke Ong. Vol. 6014. Lecture Notes in Computer Science. Springer, 2010, pp. 297–311 (cit. on pp. 2, 3). [7] Coq development team. The Coq Proof Assistant, v8.4pl3. 2013. url: http: //coq.inria.fr/distrib/V8.4pl3/ (cit. on pp. 2, 12). [8] Marcelo Fiore, Gordon Plotkin, and Daniele Turi. “Abstract Syntax and Variable Binding”. In: Proceedings of the 14th Annual IEEE Symposium on Logic in Computer Science. LICS ’99. Washington, DC, USA: IEEE Computer Society, 1999, pp. 193–202 (cit. on p. 1).

14

REFERENCES

[9] François Garillot, Georges Gonthier, Assia Mahboubi, and Laurence Rideau. “Packaging Mathematical Structures”. In: TPHOLs. Ed. by Stefan Berghofer, Tobias Nipkow, Christian Urban, and Makarius Wenzel. Vol. 5674. Lecture Notes in Computer Science. Springer, 2009, pp. 327–342 (cit. on p. 12). [10] André Hirschowitz and Marco Maggesi. “Modules over monads and initial semantics”. In: Inf. Comput. 208.5 (2010), pp. 545–564 (cit. on pp. 1, 2, 6, 8). [11] Gérard Huet and Amokrane Saïbi. “Constructive Category Theory”. In: In Proceedings of the Joint CLICS-TYPES Workshop on Categories and Type Theory, Goteborg. MIT Press, 1998 (cit. on p. 12). [12] Ralph Matthes and Celia Picard. “Verification of redecoration for infinite triangular matrices using coinduction”. In: TYPES. Ed. by Nils Anders Danielsson and Bengt Nordström. Vol. 19. LIPIcs. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, 2011, pp. 55–69. doi: 10.4230/LIPIcs.TYPES.2011.55 (cit. on pp. 1, 4, 5, 12). [13] Ralph Matthes and Martin Strecker. “Verification of the Redecoration Algorithm for Triangular Matrices”. In: TYPES. Ed. by Marino Miculan, Ivan Scagnetto, and Furio Honsell. Vol. 4941. Lecture Notes in Computer Science. Springer, 2007, pp. 125–141 (cit. on pp. 5, 8). [14] Ieke Moerdijk and Erik Palmgren. “Wellfounded trees in categories”. In: Ann. Pure Appl. Logic 104.1-3 (2000), pp. 189–218 (cit. on p. 1). [15] Bas Spitters and Eelis van der Weegen. “Type classes for mathematics in type theory”. In: Mathematical Structures in Computer Science 21.4 (2011), pp. 795–825 (cit. on p. 12). [16] Tarmo Uustalu and Varmo Vene. “The Dual of Substitution is Redecoration”. In: Scottish Functional Programming Workshop. Ed. by Kevin Hammond and Sharon Curtis. Vol. 3. Trends in Functional Programming. Intellect, 2001, pp. 99–110 (cit. on p. 2). Appendix A. Correspondance of informal and formal definitions Informal Category Functor Relative comonad Triangular matrices as comonad Comodule over comonad Tautological comodule Pushforward comodule Induced comodule morphism Relative comonad with cut Precomposition with product rest is comodule morphism Coalgebras of triangular matrices Triangular matrices are terminal

Reference

Def. 6 Ex. 8 Def. 13 Def. 14 Def. 19 Def. 20 Def. 22 Def. 26 Ex. 28 Def. 29 Thm. 30

Formal Category Functor RelativeComonad Tri Comodule tcomod pushforward induced_morphism RelativeComonadWithCut precomposition_with_product Rest TriMat Coinitiality