Dylan McDermott (speaker)
University of Cambridge

Given a programming language, can we give a monadic semantics that is stable under language extension? This problem was posed independently in Cartwright and Felleisen [3] and Wadler [10]. It is yet to be resolved. In this talk, we describe a solution to it based on factorizations of monad morphisms.

## I The conjecture

First we will give a more detailed description of the problem. Consider a programming language with effects. A model for this language consists of a category and a monad over , and each operation has a corresponding Kleisli arrow . A simple example is global state with the monad and operations and . In general, the monad may need to combine many different effects, such as nondeterminism, I/O, and local state.

Models such as these are unstable under the addition of language features: if we decide to add a new effect to the language, such as exceptions, we may need a new monad, even to model the parts of the language that do not use the new effect. All expressions will be given different denotations than previously. Even expressions that do not use the new effects receive more complicated denotations. For example, if we started with a read-only state and then add the ability to write, we switch from the reader monad to the global state monad. This is undesirable. To use a slogan: simpler programs should retain their simpler semantics.

Cartwright and Felleisen [3] noticed this problem. The (non-monadic) semantics they describe addresses it.

We can rephrase the problem using effect systems, in which simpler fragments are restricted to a subsignature of operations. Wadler [10] proposed using monads over and a Kleisli arrow for each . A model would have one monad per set . Now if we add new effects to the language, expressions that only use the original effects will be given the same denotations, since we can interpret them using the original monad. Models for parts of the language can be simpler than the model for the entire language, because they do not need to support all of the operations in the language. If the language has global state, the part that can only write (corresponding to the subsignature ) can be modelled using the monad , and this will still be true even if we add other effects to the language. Choosing the correct also gives us an easier way to reason about the part of the language corresponding to . Wadler [10] conjectures that there should be some method for constructing models of this form using .

The problem that we consider in this talk is whether we can construct models of the second kind from models of the first. Given the monad we want, for every subsignature , a monad that allows us to interpret the operations in . The resulting model would automatically be stable and should satisfy two additional properties:

• Correctness: it should agree with in the sense that it should identify the same programs as the original model.

• Modularity: each should support only the operations in . In other words, the monads should not be more complicated than necessary.

The construction should also be as general as possible. By solving this problem, we answer the conjecture made by Wadler, and address the problems with monadic semantics discussed by Cartwright and Felleisen. The solution is useful because it allows us to, for example, validate effect-dependent program transformations [6, 2]. This is because we can reason about the simpler monads instead of .

There have already been partial solutions to this problem. Katsumata [8] describes a method of constructing graded monads. When the grading is by sets of operations and the monad is free the construction creates a monad for each subsignature. It is not clear that the same construction can be used for non-free monads, and hence it is not as general as we would like.

Kammar’s thesis [5] describes a construction that does not require any additional data, based on factorizations of morphisms of Lawvere theories. Apart from its increased mathematical sophistication, it does not apply to all of the cases we would like to consider, since it requires the semantics to be algebraic. Continuations, for example, are excluded. The construction we describe generalizes it by considering factorizations of monad morphisms.

## Ii Constructing stable models

To describe the construction, we use the standard notion of factorization system [1]. A factorization system for a category consists of two classes of morphisms and such that each morphism can be factored into , for some and , and some additional properties are satisfied.

There are many examples of factorization systems. For , we can take as the class of surjective functions and as the class of injective functions. Every function factorizes into a surjection followed by an inclusion. For presheaves we take pointwise surjections and injections. For we take as the class of dense epimorphisms (continuous functions where the codomain is the closure of the image) and as the class of full monomorphisms (continuous functions satisfying ). We will assume from now on that the category has a given factorization system .

The main theorem allows us to factor monad morphisms. It observes that if we have a monad and is closed under products and (i.e. if then and ), then monad morphisms can be factored pointwise. We do not make any additional assumptions about .

###### Theorem 1.

Let be a strong monad morphism, and factorize pointwise:

 \definecolorpgfstrokecolorrgb0,0,0\pgfsys@color@rgb@stroke000\pgfsys@color@rgb@fill000SX \definecolorpgfstrokecolorrgb0,0,0\pgfsys@color@rgb@stroke000\pgfsys@color@rgb@fill000TX \definecolorpgfstrokecolorrgb0,0,0\pgfsys@color@rgb@stroke000\pgfsys@color@rgb@fill000RX

If is closed under and products then is a strong monad and are strong monad morphisms.

The intuition behind the monad is that computations do not satisfy more equations than since is in (and elements of act like injections). They also do not support more operations than because is in (and elements of act like surjections). Many monads satisfy the precondition given in the theorem, and therefore this provides a general method of constructing simpler monads from more complex ones.

We are particularly interested in factorizations yielding the simplest possible . We choose to be the free monad with operations in and to be the induced . It behaves the same as (in the sense that it satisfies the same equations), and it is not more complex than necessary (the only operations it supports are those in ). The Kleisli arrows are given by the composition of and the interpretation of using the free monad. The following lemma allows us to use the free monad when it is constructed as a colimit (see [9]).

###### Lemma 2.

Let be a regular cardinal and be an endofunctor. If has -directed colimits and preserves them then is closed under the free monad for .

The free monad for the subsignature is given by taking to be

 F=∑(op:A→B)∈εA×(−)B

This functor often preserves -directed colimits in practice.

In summary, the construction works as follows. We assume that we have a factorization system for which is closed under products. First we construct the functor from the subsignature . should preserve -directed colimits (so that it can be used in Lemma 2). We then construct the free monad for the functor. Finally, we factorize the unique monad morphism as in Theorem 1 to get . We can interpret each of the operations in using as described above.

In ongoing work we apply the construction to languages with a range of different effects, such as state, names, probability and continuations. We also plan to show that this construction does indeed generalize the construction given by Kammar [5]. The construction is sufficiently general to apply to a wide range of languages.

## Iii Correctness

We have described a method of constructing simpler monads from more complex ones. We have yet to show that the results of this construction satisfy the correctness and modularity properties from Section I. We now briefly describe some preliminary work for showing that the construction is correct.

We need to show that behaves in the same way as . To do this we will use a logical relations proof. Katsumata [7] introduces a notion of fibration for logical relations. This provides a notion of predicate that can be used to construct suitable logical relations.

Factorization systems also provide a notion of predicate for a category: a morphism in can be seen as a predicate that is defined on and true on . Since we already assume that the category has a factorization system, we would like to use it as the fibration for logical relations.

Hughes and Jacobs [4] determine the precise relationship between factorization systems and fibrations. We extend this correspondence to determine the relationship between factorization systems and fibrations for logical relations. In particular, we show that factorization systems with certain additional properties induce fibrations for logical relations. These additional properties are satisfied by all of the factorization systems we are interested in. Hence we get a way to prove the correctness of the construction for free from the factorization system.

## References

• [1] Jiří Adámek, Horst Herrlich, and George Strecker. Abstract and Concrete Categories. Wiley-Interscience, New York, NY, USA, 1990.
• [2] Nick Benton, Andrew Kennedy, Lennart Beringer, and Martin Hofmann. Relational semantics for effect-based program transformations with dynamic allocation. PPDP ’07, pages 87–96. ACM, 2007.
• [3] Robert Cartwright and Matthias Felleisen. Extensible denotational language specifications. TACS ’94, pages 244–272. Springer-Verlag, 1994.
• [4] Jesse Hughes and Bart Jacobs. Factorization systems and fibrations. ENTCS, 69:156 – 182, 2003.
• [5] Ohad Kammar. Algebraic theory of type-and-effect systems. PhD thesis, University of Edinburgh, 2014.
• [6] Ohad Kammar and Gordon D. Plotkin. Algebraic foundations for effect-dependent optimisations. POPL ’12, pages 349–360. ACM, 2012.
• [7] Shin-ya Katsumata. Relating computational effects by -lifting. Inf. Comput., 222:228–246, January 2013.
• [8] Shin-ya Katsumata. Parametric effect monads and semantics of effect systems. POPL ’14, pages 633–645. ACM, 2014.
• [9] G.M. Kelly. A unified treatment of transfinite constructions for free algebras, free monoids, colimits, associated sheaves, and so on. Bulletin of the Australian Mathematical Society, 22(1):1–83, 1980.
• [10] Philip Wadler. The marriage of effects and monads. ICFP ’98, pages 63–74. ACM, 1998.
You are adding the first comment!
How to quickly get a good reply:
• Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
• Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
• Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters