
Comprehending Monads Philip Wadler University of Glasgow Abstract Category theorists invented monads in the s to concisely express certain asp ects of universal algebra Functional programmers invented list comprehensions in the s to concisely express certain programs involving lists This pap er shows how list comprehensions may b e generalised to an arbitrary monad and howthe resulting programming feature can concisely express in a pure functional language some programs that manipulate state handle exceptions parse text or invokecon tinuations A new solution to the old problem of destructive array up date is also presented No knowledge of category theory is assumed Intro duction Is there a waytocombine the indulgences of impurity with the blessings of purity Impure strict functional languages such as Standard ML Mil HMT and Scheme RC supp ort a wide variety of features such as assigning to state handling exceptions and invoking continuations Pure lazy functional languages suchasHaskell HPW or Miranda Tur eschew such features b ecause they are incompatible with the advan tages of lazy evaluation and equational reasoning advantages that have b een describ ed at length elsewhere Hug BW Purity has its regrets and all programmers in pure functional languages will recall some moment when an impure feature has tempted them For instance if a counter is required to generate unique names then an assignable variable seems just the ticket In such cases it is always p ossible to mimic the required impure feature by straightforward or instance a counter can b e simulated by mo difying the relevant though tedious means F functions to accept an additional parameter the counters currentvalue and return an additional result the counters up dated value Miranda is a trademark of ResearchSoftware Limited Authors address Department of Computing Science University of Glasgow G QQ Scotland Elec tronic mail wadlercsglasgowacuk This pap er app eared in Mathematical Structures in Computer Science volume pp copy right Cambridge University Press This version corrects a few small errors in the published version An earlier version app eared in ACM ConferenceonLispandFunctional Programming Nice June This pap er describ es a new metho d for structuring pure programs that mimic impure features This metho d do es not completely eliminate the tension b etween purity and impurity but it do es relax it a little bit It increases the readability of the resulting programs and it eliminates the p ossibility of certain silly errors that might otherwise arise such as accidentally passing the wrong value for the counter parameter The inspiration for this technique comes from the work of Eugenio Moggi Moga Mogb His goal was to provide a way of structuring the semantic description of features such as state exceptions and continuations His discovery was that the notion of a monad from category theory suits this purp ose By dening an interpretation of calculus in an arbitrary monad he provided a framework that could describ e all these features and more It is relatively straightforward to adopt Moggis technique of structuring denotational sp ecications into a technique for structuring functional programs This pap er presents a simplied version of Moggis ideas framed in a way b etter suited to functional program mers than semanticists in particular no knowledge of category theory is assumed The pap er contains two signicant new contributions The rst contribution is a new language feature the monad comprehension This generalises the familiar notion of list comprehension Wad due originally to Burstall and Darlington and found in KRCTur Miranda Haskell and other languages Monad comprehensions are not essential to the structuring technique describ ed here but they do provide a pleasant syntax for expressing programs structured in this way The second contribution is a new solution to the old problem of destructive array een up date The solution consists of two abstract data typ es with ten op erations b etw them Under this approach the usual typing discipline eg HindleyMilner extended with abstract data typ es is sucient to guarantee that arrayupdatemay safely b e implemented byoverwriting Tomyknowledge this solution has never b een prop osed b efore and its discovery comes as a surprise considering the plethora of more elab orate solutions that have b een prop osed these include syntactic restrictions Sch runtime checks Hol abstract interpretation Huda Hudb Blo and exotic typ e systems GH Wad Wad That monads led to the discovery of this solution must countas a p oint in their favour Why has this solution not b een discovered b efore One likely reason is that the data typ es involve higherorder functions in an essential way The usual axiomatisation of arrays involves only rstorder functions index update andnewarray as describ ed in Section and so apparentlyitdidnotoccurtoanyone to search for an abstract data typ e based on higherorder functions Incidentally the higherorder nature of the solution means that it cannot b e applied in rstorder languages such as Prolog or OBJ It also casts doubt on Goguens thesis that rstorder languages are sucient for most purp oses Gog Monads and monad comprehensions help to clarify and unify some previous prop osals for incorp orating various features into functional languages exceptions Wad Spi parsers Wad Fai FL and nondeterminism HO In particular Spiveys work Spi is notable for p ointing out indep endently of Moggi that monads provide a frame work for exception handling There is a translation scheme from calculus into an arbitrary monad Indeed there are twoschemes one yielding callbyvalue semantics and one yielding callbyname These can b e used to systematically transform languages with state exceptions continu ations or other features into a pure functional language Two applications are given One is to derive callbyvalue and callbyname interpretations for a simple nondeterministic language this ts the work of Hughes and ODonnell HO into the more general frame work given here The other is to apply the callbyvalue scheme in the monad of continu ations the result is the familiar continuationpassing style transformation It remains an op en question whether there is a translation scheme that corresp onds to callbyneed as opp osed to callbyname Akey feature of the monad approach is the use of typ es to indicate what parts of a program mayhave what sorts of eects In this it is similar in spirit to Giord and Lucassens eect systems GL The examples in this pap er are based on Haskell HPW though any lazy functional language incorp orating the HindleyMilner typ e system would work as well The remainder of this pap er is organised as follows Section uses list comprehensions to motivate the concept of a monad and intro duces monad comprehensions Section shows that variable binding as in let terms and control of evaluation order can b e mo delled bytwo trivial monads Section explores the use of monads to structure pro grams that manipulate state and presents the new solution to the array up date problem Two examples are considered renaming b ound variables and interpreting a simple im p erative language Section extends monad comprehensions to include lters Section intro duces the concept of monad morphism and gives a simple pro of of the equivalence of two programs Section catalogues three more monads parsers exceptions and continu ations Section giv es the translation schemes for interpreting calculus in an arbitrary monad Two examples are considered giving a semantics to a nondeterministic language and deriving continuationpassing style Comprehensions and monads Lists Let us write Mx for the data typ e of lists with elements of typ e x In Haskell this is usually written x For example MInt and a b cM Char We write map for the higherorder function that applies a function to eachelment of a list map x y Mx My In Haskell typ e variables are written with small letters eg x and y and typ e construc tors are written with capital letters eg M For example if code Char Int maps a character to its ASCI I co de then map code a b c Observethat i map id id ii map g f map g map f Here id is the identity function id x x and g f is function comp osition g f x g fx In category theory the notions of type and function are generalised to object and arrow An op erator M taking each ob ject x into an ob ject Mx combined with an op erator map taking eacharrow f x y into an arrow map f Mx My and satisfying i andii is called a functor Categorists prefer to use the same symb ol for b oth op erators and so would write Mf where we write map f The function unit converts a value into a singleton lists and the function join con catenates a list of lists into a list unit x Mx Mx Mx join M For example unit and join Observe that iii map f unit unit f iv map f join join map map f Laws iii and iv may b e derived by a systematic transformation of the p olymorphic typ es of unit and join The idea of deriving laws from typ es go es by the slogan theorems for free Wad and is a consequence of Reynolds abstraction theorem for p olymorphic lamb da calculus Rey In categorical terms unit and join are natural transformations Rather than treat unit as a single function with a p olymorphic typ e categorists treat it as a family of arrows unit unit f for anyobjects unit x Mx one for each ob ject x satisfying map f x y x x and y and any arrow f x y between them They treat join similarly
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages38 Page
-
File Size-