Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
All About Monads
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Transformer versions of standard monads === The standard monads of the monad template library all have transformer versions which are defined consistently with their non-transformer versions. However, it is not the case the all monad transformers apply the same transformation. We have seen that the <code>ContT</code> transformer turns continuations of the form <code>(a->r)->r</code> into continuations of the form <code>(a->m r)->m r</code>. The <code>StateT</code> transformer is different. It turns state transformer functions of the form <code>s->(a,s)</code> into state transformer functions of the form <code>s->m (a,s)</code>. In general, there is no magic formula to create a transformer version of a monad β the form of each transformer depends on what makes sense in the context of its non-transformer type. <table> <tr class="header"> <th align="left">Standard Monad</th> <th align="left">Transformer Version</th> <th align="left">Original Type</th> <th align="left">Combined Type</th> </tr> <tr class="odd"> <td align="left">[[errormonad.html|Error]]</td> <td align="left">[https://www.haskell.org/ghc/docs/latest/html/base/Control.Monad.Error.html#ErrorT ErrorT]</td> <td align="left"><code>Either e a</code></td> <td align="left"><code>m (Either e a)</code></td> </tr> <tr class="even"> <td align="left">[[statemonad.html|State]]</td> <td align="left">[https://www.haskell.org/ghc/docs/latest/html/base/Control.Monad.State.html#StateT StateT]</td> <td align="left"><code>s -> (a,s)</code></td> <td align="left"><code>s -> m (a,s)</code></td> </tr> <tr class="odd"> <td align="left">[[readermonad.html|Reader]]</td> <td align="left">[https://www.stackage.org/haddock/lts-16.22/mtl-2.2.2/Control-Monad-Reader.html#g:3 ReaderT]</td> <td align="left"><code>r -> a</code></td> <td align="left"><code>r -> m a</code></td> </tr> <tr class="even"> <td align="left">[[writermonad.html|Writer]]</td> <td align="left">[https://www.haskell.org/ghc/docs/latest/html/base/Control.Monad.Writer.html#WriterT WriterT]</td> <td align="left"><code>(a,w)</code></td> <td align="left"><code>m (a,w)</code></td> </tr> <tr class="odd"> <td align="left">[[contmonad.html|Cont]]</td> <td align="left">[https://www.haskell.org/ghc/docs/latest/html/base/Control.Monad.Cont.html#ContT ContT]</td> <td align="left"><code>(a -> r) -> r</code></td> <td align="left"><code>(a -> m r) -> m r</code></td> </tr> </table> [[Image:info.png]] Order is important when combining monads. <code>StateT s (Error e)</code> is different than <code>ErrorT e (State s)</code>. The first produces a combined type of <code>s -> Error e (a,s)</code>, in which the computation can either return a new state or generate an error. The second combination produces a combined type of <code>s -> (Error e a,s)</code>, in which the computation always returns a new state, and the value can be an error or a normal value.<br />
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width