Monads and E Ects
Total Page:16
File Type:pdf, Size:1020Kb
Monads and Eects Nick Benton John Hughes and Eugenio Moggi Microsoft Research Chalmers Univ DISI Univ di Genova Genova Italy moggidisiunigeit Abstract A tension in language design has b een b etween simple se mantics on the one hand and rich p ossibilities for sideeects exception handling and so on on the other The intro duction of monads has made a large step towards reconciling these alternatives First prop osed by Moggi as a way of structuring semantic descriptions they were adopted by Wadler to structure Haskell programs and now oer a general tech nique for delimiting the scop e of eects thus reconciling referential trans parency and imp erative op erations within one programming language Monads have b een used to solve longstanding problems such as adding p ointers and assignment interlanguage working and exception handling to Haskell without compromising its purely functional semantics The course will intro duce monads eects and related notions and exemplify their applications in programming Haskell and in compilation MLj The course will present typ ed metalanguages for monads and related categorical notions and describ e how they can b e further rened by in tro ducing eects Research partially supp orted by MURST and ESPRIT WG APPSEM Monads and Computational Typ es Monads sometimes called triples have b een considered in Category Theory CT relatively late only in the late fties Monads and comonads the dual of monads are closely related to adjunctions probably the most p ervasive no tion in CT The connection b etween monads and adjunctions was established indep endently by Kleisli and Eilenb ergMo ore in the sixties Monads like ad junctions arise in many contexts eg in algebraic theories Go o d CT references for monads are ManBWBor It is not surprising that monads arise also in applications of CT to Computer Science CS We will use monads for giving denotational semantics to programming languages and more sp ecically as a way of mo deling computational types Mog to interpret a programming language in a category C we distinguish the ob ject A of values of typ e A from the ob ject TA of computations of typ e A and take as denotations of programs of typ e A the elements of TA In particular we identify the typ e A with the ob ject of values of typ e A and obtain the ob ject of computations of typ e A by applying an unary typ econstructor T to A We call T a notion of computation since it abstracts away from the typ e of values computations may pro duce Example We give few notions of computation in the category of sets partiality TA A ie A fg where is the diverging computation nondeterminism TA P A ie the set of nite subsets of A f in S L sideeects TA AS where S is a set of states eg a set U of stores or a set of inputoutput sequences U exceptions TA A E where E is the set of exceptions A R continuations TA R where R is the set of results U interactive input TA X A X where U is the set of characters More explicitly TA is the set of U branching trees with only nite paths and Alab elled leaves interactive output TA X A U X ie U A up to iso Further examples in the category of cp os could b e given based on the denota tional semantics for various programming languages Remark Many of the examples ab ove are instances of the following one given a single sorted algebraic theory T h let TA jT Aj ie the carrier of the T h free T halgebra T A over A One could consider combinations of the examples T h ab ove eg S S TA A E S and TA AS E capture imp erative programs with exceptions TA X P A ActX captures parallel programs interacting via a f in set Act of actions in fact TA is the set of nite synchronization trees up to strong bisimulation S TA X P A X S captures parallel imp erative programs with f in shared memory Wad advo cates a similar idea to mimic impure programs in a pure functional language Indeed the Haskell community has gone a long way in exploiting this approach to reconcile the advantages of pure functional programming with the exibility of imp erative or other styles of programming The analogies of com putational typ es with eect systems GL have b een observed by Wad but formal relations b etween the two have b een established only recently eg see Wad In the denotational semantics of programming languages there are other in formal notions mo deled using monads for instance col lection types in database languages BNTW or col lection classes in ob jectoriented languages Man It is imp ortant to distinguish the mathematical notion of monad or its rene ments from informal notions such as computational and collection typ es which are dened by examples In fact these informal notions can b e mo deled with a b etter degree of approximation by considering monads with additional prop er ties or additional structures When considering these renements it is often the case that what seems a natural requirement for mo deling computational typ es is not appropriate for mo deling collection typ es for instance It seems natural that programming languages can express divergent compu tations and more generally they should supp ort recursive denitions of pro grams therefore computational typ es should come equipp ed with a constant TA for the divergent computation and a least xp oint combinator Y TA TA TA While it seems natural that a collection should have only a nite numb er of elements and there should b e an empty collection TA and a way of merging two collections using a binary op eration TA TA TA There are several equivalent denitions of monad the same happ ens with adjunctions Man gives three denitions of monadtriple called in monoid form the one usually adopted in CT b o oks in extension form the most in tuitive one for us and in clone form which takes comp osition in the Kleisli category as basic We consider only triples in monoid and extension form Notation We assume know ledge of basic notions from category theory such as category functor and natural transformation In some cases familiarity with universal constructions products sums exponentials and adjunction is as sumed We use the fol lowing notation given a category C we write jC j for the setclass of its objects C A B for the homset of morphisms from A to B g f and f g for the composition A B C f g id for the identity on A A F C D means that F is a functor from C to D and F G means that is a natural transformation from F to G G D means that G is right adjoint to F F is left adjoint to G C F Denition Kleisli tripletriple in extension form A Kleisli triple over a category C is a triple T where T jC j jC j A TA for A A jC j f TA TB for f A TB and the fol lowing equations hold id TA A f f for f A TB A f g f g for f A TB and g B TC Kleisli triples have an intuitive justication in terms of computational typ es is the inclusion of values into computations A A a A a TA f is the extension of a function f from values to computations to a function from computations to computations which rst evaluates a computation and then applies f to the resulting value f a A f a TB f c TA let ac in f a TB In order to justify the axioms for a Kleisli triple we have rst to intro duce a category C whose morphisms corresp ond to programs We pro ceed by analogy T with the categorical semantics for terms where typ es are interpreted by ob jects and terms of typ e B with a parameter free variable of typ e A are interpreted by morphisms from A to B Since the denotation of programs of typ e B are supp osed to b e elements of TB programs of typ e B with a parameter of typ e A ought to b e interpreted by morphisms with co domain TB but for their domain there are two alternatives either A or TA dep ending on whether parameters of typ e A are identied with values or computations of typ e A We cho ose the rst alternative b ecause it entails the second Indeed computations of typ e A are the same as values of typ e TA So we take C A B to b e C A T B It remains to T dene comp osition and identities in C and show that they satisfy the unit and T asso ciativity axioms for categories over C the Denition Kleisli category Given a Kleisli triple T Kleisli category C is dened as fol lows T the objects of C are those of C T the set C A B of morphisms from A to B in C is C A T B T T the identity on A in C is A TA T A f C A B fol lowed by g C B C in C is f g A TC T T T It is natural to take as the identity on A in the category C since it maps a A T parameter x to x ie to x viewed as a computation Similarly comp osition in C has a simple explanation in terms of the intuitive meaning of f in fact T f g x A f x TB y B g y TC f g x A let y f x in g y TC ie f followed by g in C with parameter x is the program which rst evaluates T the program f x and then feed the resulting value as parameter to g At this p oint we can give also a simple justication for the three axioms of Kleisli triples namely they are equivalent to the following unit and asso ciativity axioms which say that C is a category T f for f A TB f B f f for f A TB A f g h f g h for f A TB g B TC and h C TD Example We go through the examples of computational typ es given in Ex ample and show that they are indeed part of suitable Kleisli triples partiality TA A A fg is the inclusion of A into A A if f A TB then f and f a f a when a A nondeterminism TA P A f in is the singleton map a fag A if f A TB and c TA then