Unifying graded and parameterised monads

01/28/2020 ∙ by Dominic Orchard, et al. ∙ 0

Monads are a useful tool for structuring effectful features of computation such as state, non-determinism, and continuations. In the last decade, several generalisations of monads have been suggested which provide a more fine-grained model of effects by replacing the single type constructor of a monad with an indexed family of constructors. Most notably, graded monads, indexed by a monoid, model effect systems and parameterised monads, indexed by pairs of pre- and post-conditions, model program logics. This paper studies the relationship between these two generalisations of monads via a third generalisation. This third generalisation, which we call category-graded monads, arises by generalising a view of monads as a particular special case of lax functors. A category-graded monad provides a family of functors T f indexed by morphisms f of some other category. This allows certain compositions of effects to be ruled out (in the style of a program logic) as well as an abstract description of effects (in the style of an effect system). Using this as a basis, we show how graded and parameterised monads can be unified, studying their similarities and differences along the way.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Ever since moggi1989monads, moggi1991monads, monads have become an important structure in programming and semantics, particularly for capturing computational effects. In this approach, an effectful computation yielding a value of type is modelled by the type where is an endofunctor with the structure of a monad, i.e., with two operations (and some axioms): unit () mapping values into pure computations (with no effects) and multiplication () composing effects of two computations (one nested in the other):

Various generalisations of monads replace the endofunctor with a family of endofunctors indexed by effect information. For example, wadler2003marriage proposed a generalisation of monads to model effect systems using a family of monads indexed over a bounded semilattice . Later, graded monads were proposed (DBLP:journals/corr/OrchardPM14, katsumata2014parametric), generalising this idea to a family of endofunctors indexed by the elements of an ordered monoid with monad-like unit and multiplication operations (and axioms) mediated by the monoid structure:

A family of approximation maps is derived from the ordering.

Another indexed generalisation is provided by parameterised monads, which comprise a family of endofunctors indexed by pairs of objects drawn from a category which provides information akin to pre- and post-conditions (wadler1994monads, atkey2009parameterised). Parameterised monads have unit and multiplication operations (satisfying analogous axioms to monads):

A family of maps (for all ) provides a notion of approximation. For pure computations via , the pre-condition is preserved as the post-condition. For composition via , the post-condition of the first computation must agree with the pre-condition of the second, yielding a pair of the pre-condition of the first and post-condition of the second.

Graded and parameterised monads are used for various kinds of fine-grained effectful semantics, reasoning, and programming. For example, graded monads model effect systems (katsumata2014parametric, mycroft2016effect), trace semantics (miliusgeneric), and session types (orchard2016effects). Parameterised monads are used to refine effectful semantics with Floyd-Hoare triples (atkey2009parameterised), information flow tracking (stefan2011flexible), and session types (Pucell2008, imai2011session). In GHC/Haskell, graded and parameterised monads are provided by the effect-monad package,111https://hackage.haskell.org/package/effect-monad leveraging GHC’s advanced type system features particularly in the case of graded monads.

Both structures appear to follow a similar pattern, generalising monads into some indexed family of type constructors with monad-like operations mediated by structure on the indices. Furthermore, there are applications for which both graded and parameterised monads have been used, notably session types.222orchard2017session provide a survey of different structuring techniques for session types in the context of Haskell. Thus, one might naturally wonder how graded and parameterised monads are related. We show that they can be related by a structure we call category-graded monads, based on a specialised class of lax functors.

Whilst graded monads are indexed by elements of a monoid and parameterised monads by pairs of indices, category-graded monads are indexed by the morphisms of a category, and have operations:

for , , . Indexing by morphisms provides a way to restrict composition of effectful computations and a model that captures various kinds of indexing. The rightmost operation is provided by 2-category-graded monads (generalising grading to 2-categories) where lifts a 2-morphism (morphism between morphisms), providing a notion of approximation akin to graded monads.

The structures we introduce along with their relationships are summarised by the diagram below, where the source of an arrow is at a more specific structure, and the target is a more general structure. Highlighted in bold are the new structures in this paper, centrally category-graded monads and its generalisations 2-category-graded monads and additional structure of generalised units. Throughout, we state the results of the diagram below as propositions of the form “every A is a B” whenever there is an arrow from to above. By this we mean there is an injective map from structures into structures.

Section 3 introduces category-graded monads as a particular way of generalising monads via lax functors. Section LABEL:sec:graded-monads considers graded monads and 2-category-graded monads. Section LABEL:sec:lax-nats considers the notion of lax natural transformations used in Section LABEL:sec:loose which considers parameterised monads and generalised units.

We show that the subclasses of unordered graded monads and discrete parameterised monads are both subsumed by category-graded monads. However, graded monads may have an ordering which provides approximation, requiring 2-category-graded monads, and full (non-discrete) parameterised monads also have a kind of approximation which can be captured by a generalised unit for a category-graded monad. Section LABEL:sec:combined-example shows that the apex of 2-category-graded monads plus generalised units capture analyses and semantics that have both graded and parameterised monad components, using an example of a Hoare logic for probabilistic computations.

2 Background

We first recall some standard, basic categorical facts relating monoids to categories which will be used throughout. We start by fixing some notation.

Notation 1 (2-Category).

For a 2-category , we denote the class of objects as , 1-morphisms as and 2-morphisms as . We write 2-morphism arrows as unless they are natural transformations (2-morphisms in ) which are instead written as . Appendix LABEL:app:additional-background provides a definition of 2-categories.

Notation 2 (Homset).

The homset of (1-)morphisms between any two objects is written .

There are two standard ways to view monoids in categorical terms: Recall a set-theoretic presentation of a monoid with set , operation and unit . This is identical to a category , which has a single object , morphisms as the elements of , i.e., , composition via the operation and identity morphism . Associativity and unit properties of categories and monoids align. Hence:

Proposition 3.

Monoids are one-object categories.

An equivalent view of monoids is as a monoidal category, where is a category with objects , bifunctor and unit object . This monoidal category is discrete, meaning the only morphisms are the identities.

Proposition 4.

Monoids are discrete monoidal categories.

In this paper, we study graded monads which are typically defined in the literature as being indexed by a pre-ordered monoid, or pomonoid. We therefore recall how the above two results generalise to pomonoids. A monoid with a preorder (and monotonic wrt. ) is a 2-category following Proposition 3 but with added 2-morphisms between every pair of morphisms whenever .

Proposition 5.

Pre-ordered monoids are one-object 2-categories.

Pre-orders are categories, with a morphism for every pair of ordered elements. Thus we can replay Proposition 4, but we now have morphisms between some elements representing the ordering and thus the category is no longer discrete. However this monoidal category is strict, meaning that associativity and unit axioms are equalities rather than isomorphisms. (Note discrete categories are automatically strict).

Proposition 6.

Pre-ordered monoids are strict monoidal categories.

A further categorical view on monoids is that they can be identified as some distinguished object in a monoidal category:

Definition 7.

A monoid in a monoidal category is a distinguished object equipped with a pair of morphisms for the unit and , with associativity and unit axioms.

The monoidal category of endofunctors on some category is particularly important in this paper:

Definition 8.

The category of endofunctors on , denoted , has endofunctors as objects and natural transformations as morphisms. This is a strict monoidal category with , i.e., with functor composition as the bifunctor and the identity endofunctor as the unit element.

The classic aphorism that monads are just a monoid in the category of endofunctors thus applies Definition 7 in the context of the monoidal category of endofunctors , pointing out that a monad on endofunctor

is a particular single object. Since the tensor product of the monoidal category is

then monad multiplication is the binary operator and the monad unit operation identifies the unit element . Thus:

Proposition 9.

Monads are monoids in the category of endofunctors.

Finally, we leverage the (standard) equivalence between strict monoidal categories and 2-categories with one object, which “transposes” monoidal and 1-categorical composition into 1-categorical and 2-categorical composition respectively. That is, for a strict monoidal category we can construct a one-object 2-category, call it , with and and with horizontal composition and identity morphism , and vertical composition and 2-identities by identities of . Conversely, given a one-object 2-category, , we can construct a strict monoidal category where , , , is 1-morphism identity, composition and identity is the 2-identity. The same result applies for discrete monoidal categories and one-object categories where we can elide the 2-categorical part. Hence:

Proposition 10.

Strict monoidal categories are equivalent to one-object 2-categories, and discrete monoidal categories are equivalent to one-object categories.

Corollary 11.

By Def. 8 and Prop. 10, the monoidal category of endofunctors is equivalent to a one-object 2-category with single object , morphisms as endofunctors and 2-morphisms are natural transformations. For clarity, we denote this 2-category as .

3 Generalising monads via lax functors to category-graded monads

Recall that every monoid corresponds to a single-object category. (Proposition 3). In a single-object category, all morphisms compose just as all elements of a monoid multiply. Categories therefore generalise monoids: morphisms and compose to only when the source of agrees with the target of . Similarly, groupoids generalise the notion of groups to categories, where elements of the group are morphisms and every morphism has an inverse.

This process of generalising some notion to a category is known as horizontal categorification or oidification (echoing the relationship of groups to groupoids) (nlab:horizontal_categorification, bertozzini2008non, bertozzini2008horizontal). The general approach is to realise some concept as a kind of category comprising a single object which can then be generalised to many objects; categories are the “oidification” of monoids going from a single object to many (nlab:horizontal_categorification)—though the term category is preferred to monoidoid!

Our approach can be summarised as the horizontal categorification of monads. It turns out that this yields structures that unify graded and parameterised monads, but with some subtleties as we shall see.

First we need to understand in what way the concept of a monad can be seen as single-object entity such that it can be subjected to oidification. The view of monads as a monoid in category of endofunctors (Proposition 9) highlights that a monoid comprises a single object in but oidification cannot readily be applied to this perspective. Instead, we take Bénabou’s view of monads as lax functors which is more amenable to generalisation. We first recall the definitions of lax functors for completeness.

Definition 12.

A lax functor (where is a 2-category) comprises an object mapping and a morphism mapping, however the usual functor axioms and are replaced by families of 2-morphisms in which “laxly” preserve units and composition:

We have chosen the names of these families to be suggestive of our endpoint here.

Whilst the axioms of a category are preserved automatically by non-lax functors, this is not the case here. For example, if is a functor then , but not if is a lax functor. Instead a lax functor has additional axioms for associativity of and unitality of . We postpone the description of these properties until Corollary 15.

We can thus see monads as lax functors between the terminal category and the one-object 2-category of endofunctors on :

Proposition 13 (benabou1967introduction).

For a category , a monad on is a functor where is the single-object category with and a single morphism . Then and identifies an endofunctor on . Laxness means the functor axioms for are 2-morphisms, which are the unit and multiplication operations of the monad on the endofunctor :

where is the identity endofunctor Id on . The monad axioms are exactly the unit and associativity axioms of the lax functor.

This proposition recasts the aphorism that monads are monoids in the category of endofunctors. It equivalently views monads as lax homomorphisms (lax functors) between the singleton monoid and the monoid of endofunctors. Since the source category has but one object and morphism, identifies a particular endofunctor on and the lax operators and provide the usual monad operations for .

We can now “oidify” this definition in two ways: (1) generalising the singleton source to a category, or (2) generalising the singleton target category to . In this paper, we pursue the first choice, though we discuss the second in Section LABEL:subsec:oidification-alt. We thus oidify Bénabou’s monad definition by replacing the singleton category with an arbitrary category . We might have chosen the name monadoid for this structure, but since there are two ways in which the oidification can be applied, we settle on the name category-graded monads, the terminology for which will be explained once we recall graded monads in the next section. We also found that people do not like the name monadoid.

Definition 14.

Let and be categories. A category-graded monad is a lax functor where for all . Thus the morphism mapping of can be thought of as family of endofunctors indexed by morphisms, i.e., for all in then is an endofunctor.

We refer to as the indexing category and write as . For brevity we sometimes refer to category-graded monads as cat-graded monads or -graded monads if the category is in scope.

The definition of category-graded monads is compact, but the requirement that is a lax functor comes with a lot of structure which are akin to monads and graded monads.

Corollary 15 (Category-graded monad axioms).

Suppose is a category-graded monad. Then, following from the lax functor definition for there are natural transformations (which we call unit and multiplication respectively):

Following from the lax functor, these satisfy associativity and unitality axioms (postponed from Def. 12):

2 Background

We first recall some standard, basic categorical facts relating monoids to categories which will be used throughout. We start by fixing some notation.

Notation 1 (2-Category).

For a 2-category , we denote the class of objects as , 1-morphisms as and 2-morphisms as . We write 2-morphism arrows as unless they are natural transformations (2-morphisms in ) which are instead written as . Appendix LABEL:app:additional-background provides a definition of 2-categories.

Notation 2 (Homset).

The homset of (1-)morphisms between any two objects is written .

There are two standard ways to view monoids in categorical terms: Recall a set-theoretic presentation of a monoid with set , operation and unit . This is identical to a category , which has a single object , morphisms as the elements of , i.e., , composition via the operation and identity morphism . Associativity and unit properties of categories and monoids align. Hence:

Proposition 3.

Monoids are one-object categories.

An equivalent view of monoids is as a monoidal category, where is a category with objects , bifunctor and unit object . This monoidal category is discrete, meaning the only morphisms are the identities.

Proposition 4.

Monoids are discrete monoidal categories.

In this paper, we study graded monads which are typically defined in the literature as being indexed by a pre-ordered monoid, or pomonoid. We therefore recall how the above two results generalise to pomonoids. A monoid with a preorder (and monotonic wrt. ) is a 2-category following Proposition 3 but with added 2-morphisms between every pair of morphisms whenever .

Proposition 5.

Pre-ordered monoids are one-object 2-categories.

Pre-orders are categories, with a morphism for every pair of ordered elements. Thus we can replay Proposition 4, but we now have morphisms between some elements representing the ordering and thus the category is no longer discrete. However this monoidal category is strict, meaning that associativity and unit axioms are equalities rather than isomorphisms. (Note discrete categories are automatically strict).

Proposition 6.

Pre-ordered monoids are strict monoidal categories.

A further categorical view on monoids is that they can be identified as some distinguished object in a monoidal category:

Definition 7.

A monoid in a monoidal category is a distinguished object equipped with a pair of morphisms for the unit and , with associativity and unit axioms.

The monoidal category of endofunctors on some category is particularly important in this paper:

Definition 8.

The category of endofunctors on , denoted , has endofunctors as objects and natural transformations as morphisms. This is a strict monoidal category with , i.e., with functor composition as the bifunctor and the identity endofunctor as the unit element.

The classic aphorism that monads are just a monoid in the category of endofunctors thus applies Definition 7 in the context of the monoidal category of endofunctors , pointing out that a monad on endofunctor

is a particular single object. Since the tensor product of the monoidal category is

then monad multiplication is the binary operator and the monad unit operation identifies the unit element . Thus:

Proposition 9.

Monads are monoids in the category of endofunctors.

Finally, we leverage the (standard) equivalence between strict monoidal categories and 2-categories with one object, which “transposes” monoidal and 1-categorical composition into 1-categorical and 2-categorical composition respectively. That is, for a strict monoidal category we can construct a one-object 2-category, call it , with and and with horizontal composition and identity morphism , and vertical composition and 2-identities by identities of . Conversely, given a one-object 2-category, , we can construct a strict monoidal category where , , , is 1-morphism identity, composition and identity is the 2-identity. The same result applies for discrete monoidal categories and one-object categories where we can elide the 2-categorical part. Hence:

Proposition 10.

Strict monoidal categories are equivalent to one-object 2-categories, and discrete monoidal categories are equivalent to one-object categories.

Corollary 11.

By Def. 8 and Prop. 10, the monoidal category of endofunctors is equivalent to a one-object 2-category with single object , morphisms as endofunctors and 2-morphisms are natural transformations. For clarity, we denote this 2-category as .

3 Generalising monads via lax functors to category-graded monads

Recall that every monoid corresponds to a single-object category. (Proposition 3). In a single-object category, all morphisms compose just as all elements of a monoid multiply. Categories therefore generalise monoids: morphisms and compose to only when the source of agrees with the target of . Similarly, groupoids generalise the notion of groups to categories, where elements of the group are morphisms and every morphism has an inverse.

This process of generalising some notion to a category is known as horizontal categorification or oidification (echoing the relationship of groups to groupoids) (nlab:horizontal_categorification, bertozzini2008non, bertozzini2008horizontal). The general approach is to realise some concept as a kind of category comprising a single object which can then be generalised to many objects; categories are the “oidification” of monoids going from a single object to many (nlab:horizontal_categorification)—though the term category is preferred to monoidoid!

Our approach can be summarised as the horizontal categorification of monads. It turns out that this yields structures that unify graded and parameterised monads, but with some subtleties as we shall see.

First we need to understand in what way the concept of a monad can be seen as single-object entity such that it can be subjected to oidification. The view of monads as a monoid in category of endofunctors (Proposition 9) highlights that a monoid comprises a single object in but oidification cannot readily be applied to this perspective. Instead, we take Bénabou’s view of monads as lax functors which is more amenable to generalisation. We first recall the definitions of lax functors for completeness.

Definition 12.

A lax functor (where is a 2-category) comprises an object mapping and a morphism mapping, however the usual functor axioms and are replaced by families of 2-morphisms in which “laxly” preserve units and composition:

We have chosen the names of these families to be suggestive of our endpoint here.

Whilst the axioms of a category are preserved automatically by non-lax functors, this is not the case here. For example, if is a functor then , but not if is a lax functor. Instead a lax functor has additional axioms for associativity of and unitality of . We postpone the description of these properties until Corollary 15.

We can thus see monads as lax functors between the terminal category and the one-object 2-category of endofunctors on :

Proposition 13 (benabou1967introduction).

For a category , a monad on is a functor where is the single-object category with and a single morphism . Then and identifies an endofunctor on . Laxness means the functor axioms for are 2-morphisms, which are the unit and multiplication operations of the monad on the endofunctor :

where is the identity endofunctor Id on . The monad axioms are exactly the unit and associativity axioms of the lax functor.

This proposition recasts the aphorism that monads are monoids in the category of endofunctors. It equivalently views monads as lax homomorphisms (lax functors) between the singleton monoid and the monoid of endofunctors. Since the source category has but one object and morphism, identifies a particular endofunctor on and the lax operators and provide the usual monad operations for .

We can now “oidify” this definition in two ways: (1) generalising the singleton source to a category, or (2) generalising the singleton target category to . In this paper, we pursue the first choice, though we discuss the second in Section LABEL:subsec:oidification-alt. We thus oidify Bénabou’s monad definition by replacing the singleton category with an arbitrary category . We might have chosen the name monadoid for this structure, but since there are two ways in which the oidification can be applied, we settle on the name category-graded monads, the terminology for which will be explained once we recall graded monads in the next section. We also found that people do not like the name monadoid.

Definition 14.

Let and be categories. A category-graded monad is a lax functor where for all . Thus the morphism mapping of can be thought of as family of endofunctors indexed by morphisms, i.e., for all in then is an endofunctor.

We refer to as the indexing category and write as . For brevity we sometimes refer to category-graded monads as cat-graded monads or -graded monads if the category is in scope.

The definition of category-graded monads is compact, but the requirement that is a lax functor comes with a lot of structure which are akin to monads and graded monads.

Corollary 15 (Category-graded monad axioms).

Suppose is a category-graded monad. Then, following from the lax functor definition for there are natural transformations (which we call unit and multiplication respectively):

Following from the lax functor, these satisfy associativity and unitality axioms (postponed from Def. 12):

3 Generalising monads via lax functors to category-graded monads

Recall that every monoid corresponds to a single-object category. (Proposition 3). In a single-object category, all morphisms compose just as all elements of a monoid multiply. Categories therefore generalise monoids: morphisms and compose to only when the source of agrees with the target of . Similarly, groupoids generalise the notion of groups to categories, where elements of the group are morphisms and every morphism has an inverse.

This process of generalising some notion to a category is known as horizontal categorification or oidification (echoing the relationship of groups to groupoids) (nlab:horizontal_categorification, bertozzini2008non, bertozzini2008horizontal). The general approach is to realise some concept as a kind of category comprising a single object which can then be generalised to many objects; categories are the “oidification” of monoids going from a single object to many (nlab:horizontal_categorification)—though the term category is preferred to monoidoid!

Our approach can be summarised as the horizontal categorification of monads. It turns out that this yields structures that unify graded and parameterised monads, but with some subtleties as we shall see.

First we need to understand in what way the concept of a monad can be seen as single-object entity such that it can be subjected to oidification. The view of monads as a monoid in category of endofunctors (Proposition 9) highlights that a monoid comprises a single object in but oidification cannot readily be applied to this perspective. Instead, we take Bénabou’s view of monads as lax functors which is more amenable to generalisation. We first recall the definitions of lax functors for completeness.

Definition 12.

A lax functor (where is a 2-category) comprises an object mapping and a morphism mapping, however the usual functor axioms and are replaced by families of 2-morphisms in which “laxly” preserve units and composition:

We have chosen the names of these families to be suggestive of our endpoint here.

Whilst the axioms of a category are preserved automatically by non-lax functors, this is not the case here. For example, if is a functor then , but not if is a lax functor. Instead a lax functor has additional axioms for associativity of and unitality of . We postpone the description of these properties until Corollary 15.

We can thus see monads as lax functors between the terminal category and the one-object 2-category of endofunctors on :

Proposition 13 (benabou1967introduction).

For a category , a monad on is a functor where is the single-object category with and a single morphism . Then and identifies an endofunctor on . Laxness means the functor axioms for are 2-morphisms, which are the unit and multiplication operations of the monad on the endofunctor :

where is the identity endofunctor Id on . The monad axioms are exactly the unit and associativity axioms of the lax functor.

This proposition recasts the aphorism that monads are monoids in the category of endofunctors. It equivalently views monads as lax homomorphisms (lax functors) between the singleton monoid and the monoid of endofunctors. Since the source category has but one object and morphism, identifies a particular endofunctor on and the lax operators and provide the usual monad operations for .

We can now “oidify” this definition in two ways: (1) generalising the singleton source to a category, or (2) generalising the singleton target category to . In this paper, we pursue the first choice, though we discuss the second in Section LABEL:subsec:oidification-alt. We thus oidify Bénabou’s monad definition by replacing the singleton category with an arbitrary category . We might have chosen the name monadoid for this structure, but since there are two ways in which the oidification can be applied, we settle on the name category-graded monads, the terminology for which will be explained once we recall graded monads in the next section. We also found that people do not like the name monadoid.

Definition 14.

Let and be categories. A category-graded monad is a lax functor where for all . Thus the morphism mapping of can be thought of as family of endofunctors indexed by morphisms, i.e., for all in then is an endofunctor.

We refer to as the indexing category and write as . For brevity we sometimes refer to category-graded monads as cat-graded monads or -graded monads if the category is in scope.

The definition of category-graded monads is compact, but the requirement that is a lax functor comes with a lot of structure which are akin to monads and graded monads.

Corollary 15 (Category-graded monad axioms).

Suppose is a category-graded monad. Then, following from the lax functor definition for there are natural transformations (which we call unit and multiplication respectively):

Following from the lax functor, these satisfy associativity and unitality axioms (postponed from Def. 12):