Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword or section
Like this
2Activity
×
P. 1
All About Monads

All About Monads

Ratings: (0)|Views: 1,077|Likes:
Published by shabunc

More info:

Published by: shabunc on Jun 16, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See More
See less

01/21/2014

pdf

text

original

 
All About Monads
Next:Introduction
All About Monads
 A comprehensive guide to the theory and practice of monadic programming in Haskell
Version 1.1.0This tutorial aims to explain the concept of a monad and its application to functional programming in a way that is easy to understand and usefulto beginning and intermediate Haskell programmers. Familiarity with the Haskell language is assumed, but no prior experience with monads isrequired. The tutorial covers a lot of material and the later sections require a thorough understanding of the earlier material. Many code examplesare provided along the way to demonstrate monadic programming. It is not advisable to attempt to absorb all of the material in a single reading.The tutorial is arranged in three parts. The first part provides a basic understanding of the role of monads in functional programming, howmonads operate, and how they are declared and used in Haskell. The second part covers each standard monad in Haskell, giving the definition of the monad and discussing the use of the monad. The third part covers advanced material relating to monad transformers and real-world issuesencountered when programming with monads.You can also download the complete tutorial as acompressed tar archiveor as azip archive. The best way to really understand monads is to experiment with monadic code. This tutorial provides manycode exampleswhich range fromsimple to moderately complex, and you are encouraged to study them, play with them, modify them, etc.
Unfortunately, it is necessary that theearly examples must be contrived to avoid issues with combining multiple monads. If you find yourself frustrated by this restriction whileexperimenting with the example code, look ahead at Part III to see how it is overcome.
Please send any comments, corrections, or suggestions to Jeff Newbern at jnewbern@yahoo.com
Contents
Part I - Understanding Monads
IntroductionWhat is a monad?Why should I make the effort to understand monads?Meet the MonadsType constructorsMaybe a monadAn exampleA list is also a monadSummaryDoing it with classHaskell type classesThe Monad classExample continuedDo notationSummaryThe monad lawsThe three fundamental lawsFailure IS an optionNo way outZero and PlusSummaryExercisesMonad support in HaskellIn the standard preludeIn the Monad moduleSummary
Part II - A Catalog of Standard Monads
IntroductionThe Identity monadOverviewMotivationDefinitionExampleThe Maybe monadOverviewMotivationDefinitionExampleThe Error monadOverview
 
All About Monads
MotivationDefinitionExampleThe List monadOverviewMotivationDefinitionExampleThe IO monadOverviewMotivationDefinitionExampleThe State monadOverviewMotivationDefinitionExampleThe Reader monadOverviewMotivationDefinitionExampleThe Writer monadOverviewMotivationDefinitionExampleThe Continuation monadOverviewMotivationDefinitionExample
Part III - Monads in the Real World
IntroductionCombining monads the hard wayNested MonadsCombined MonadsMonad transformersTransformer type constructorsLiftingStandard monad transformersThe MonadTrans and MonadIO classesTransformer versions of standard monadsAnatomy of a monad transformerCombined monad definitionDefining the lifting functionFunctorsMore examples with monad transformersWriterT with IOReaderT with IOStateT with ListManaging the transformer stack Selecting the correct orderAn example with multiple transformersHeavy liftingContinuing Exploration
Appendices
Appendix I - A physical analogy for monadsAppendix II - Haskell code examplesNext:Introduction
 
Introduction
Prev:Table of ContentsTOC:ContentsNext:Meet the Monads
Introduction
What is a monad?Why should I make the effort to understand monads?
What is a monad?
A monad is a way to structure computations in terms of values and sequences of computations using those values. Monads allow the programmerto build up computations using sequential building blocks, which can themselves be sequences of computations. The monad determines howcombined computations form a new computation and frees the programmer from having to code the combination manually each time it isrequired.
 It is useful to think of a monad as a strategy for combining computations into more complex computations.
For example, you should be familiarwith the
Maybe
type in Haskell:
data Maybe a = Nothing | Just a
which represents the type of computations which may fail to return a result. The
Maybe
type suggests a strategy for combining computationswhich return
Maybe
values: if a combined computation consists of one computation
B
that depends on the result of another computation
A
, thenthe combined computation should yield
Nothing
whenever either
A
or
B
yield
Nothing
and the combined computation should yield the result of 
B
applied to the result of 
A
when both computations succeed.Other monads exist for building computations that perform I/O, have state, may return multiple results, etc. There are as many different type of monads as there are strategies for combining computations, but there are certain monads that are especially useful and are common enough thatthey are part of the standardHaskell 98 libraries. These monads are each described inPart II.
Why should I make the effort to understand monads?
The sheer number of differentmonad tutorialson the internet is a good indication of the difficulty many people have understanding the concept.This is due to the abstract nature of monads and to the fact that they are used in several different capacities, which can confuse the picture of exactly what a monad is and what it is good for.In Haskell, monads play a central role in the I/O system. It is not essential to understand monads to do I/O in Haskell, but understanding the I/Omonad will improve your code and extend your capabilities.For the programmer, monads are useful tools for structuring functional programs. They have three properties that make them especially useful:Modularity - They allow computations to be composed from simpler computations and separate the combination strategy from the actualcomputations being performed.1.Flexibility - They allow functional programs to be much more adaptable than equivalent programs written without monads. This is becausethe monad distills the computational strategy into a single place instead of requiring it be distributed throughout the entire program.2.Isolation - They can be used to create imperative-style computational structures which remain safely isolated from the main body of thefunctional program. This is useful for incorporating side-effects (such as I/O) and state (which violates referential transparency) into a purefunctional language like Haskell.3.Each of these features will be revisited later in the tutorial in the context of specific monads.Prev:Table of ContentsTOC:ContentsNext:Meet the Monads

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->