# New monads

### From HaskellWiki

(Move over many pages from old wiki and add LazyWriterT) |
(Link to wiki page on monadLib instead of old galios.com pages) |
||

(23 intermediate revisions by 8 users not shown) | |||

Line 1: | Line 1: | ||

__TOC__ |
__TOC__ |
||

+ | |||

+ | Remember to add a [ [ Category:Code ] ] tag to any new sub-pages. |
||

== MonadBase == |
== MonadBase == |
||

Line 5: | Line 7: | ||

It seems that the liftIO function from MonadIO can be generalized to access whatever the base of a transformer stack happens to be. So there is no need for a liftSTM, liftST, etc. |
It seems that the liftIO function from MonadIO can be generalized to access whatever the base of a transformer stack happens to be. So there is no need for a liftSTM, liftST, etc. |
||

− | View [[NewMonads/MonadBase]]. |
+ | View [[New monads/MonadBase]]. |

== MonadLib == |
== MonadLib == |
||

− | This is by Iavor S. Diatchki and can be found at http://www.cse.ogi.edu/~diatchki/monadLib/ |
+ | [[MonadLib]] is written by Iavor S. Diatchki. |

− | It is a new version of the mtl package with transformers: ReaderT WriterT StateT ExceptT SearchT ContT |
+ | It is a new version of the mtl package with base monads: Id, and Lift, and transformers ReaderT, WriterT, StateT, ExceptionT, ChoiceT, and ContT. |

It also defines BaseM which is like MonadBase above. |
It also defines BaseM which is like MonadBase above. |
||

− | |||

− | == LazyWriterT == |
||

− | |||

− | This came up on the mailing list: Why is WriterT never lazy? The answer is it does not use lazy patterns with "~". So here is a more useful [[NewMonads/LazyWriterT]] that add two "~" to the definition of (>>=) and renames WriterT to LazyWriterT. |
||

== MonadRandom == |
== MonadRandom == |
||

Line 23: | Line 21: | ||

A simple monad transformer to allow computations in the transformed monad to generate random values. |
A simple monad transformer to allow computations in the transformed monad to generate random values. |
||

− | View [[NewMonads/MonadRandom]]. |
+ | View [[New monads/MonadRandom]]. |

+ | |||

+ | ===MonadRandomSplittable=== |
||

+ | A refinement of MonadRandom to integrate RandomGen's split function. |
||

+ | |||

+ | View at [[New monads/MonadRandomSplittable]] |
||

+ | |||

+ | == MaybeT == |
||

+ | |||

+ | The Maybe monad deserves a transformer, just like the other classic monads. |
||

+ | |||

+ | View [[New monads/MaybeT]]. |
||

== MonadSupply == |
== MonadSupply == |
||

Line 29: | Line 27: | ||

Here is a simple monad/monad transformer for computations which consume values from a (finite or infinite) supply. Note that due to pattern matching, running out of supply in a non-MonadZero monad will cause an error. |
Here is a simple monad/monad transformer for computations which consume values from a (finite or infinite) supply. Note that due to pattern matching, running out of supply in a non-MonadZero monad will cause an error. |
||

− | View [[NewMonads/MonadSupply]]. |
+ | View [[New monads/MonadSupply]]. |

== MonadUndo == |
== MonadUndo == |
||

Line 35: | Line 33: | ||

Here is a modified state monad transformer for keeping track of undo/redo states automatically. |
Here is a modified state monad transformer for keeping track of undo/redo states automatically. |
||

− | View [[NewMonads/MonadUndo]]. |
+ | View [[New monads/MonadUndo]]. |

== MonadUnique == |
== MonadUnique == |
||

Line 41: | Line 39: | ||

This is a simple (trivial) monad transformer for supplying unique integer values to an algorithm. |
This is a simple (trivial) monad transformer for supplying unique integer values to an algorithm. |
||

− | View [[NewMonads/MonadUnique]]. |
+ | View [[New monads/MonadUnique]]. |

+ | |||

+ | == MonadSTO == |
||

+ | |||

+ | Here's an extension of the ST monad in which the references are ordered and showable (they list their creation index). |
||

+ | |||

+ | View [[New monads/MonadSTO]]. |
||

+ | |||

+ | == MonadNondet == |
||

+ | |||

+ | There is a [[Sudoku#Monadic_Non-Deterministic_Solver | MonadNondet]] that when compiled with optimizations outperforms List. |
||

+ | |||

+ | == Stateful nondeterminism == |
||

+ | |||

+ | There is a [[Stateful nondeterminism]] monad for if you want to do nondeterministic computation with local states for each of your threads and a global state shared by all your threads. |
||

+ | |||

+ | == MonadAdvSTM == |
||

+ | |||

+ | Here is an extension of STM to easy interaction with IO after committing or retrying. Inspired by Simon P-J. |
||

+ | |||

+ | View [[New monads/MonadAdvSTM]]. |
||

+ | |||

+ | == TimedStateT == |
||

+ | |||

+ | A monad transformer which combines State, Reader, and Error functionality to give the effect of a StateT monad which checks clock-time and stops the current computation if a period is exceeded. |
||

+ | |||

+ | darcs get http://www.mapcar.org/haskell/TimedStateT/ |
||

+ | |||

+ | Haddocks: http://www.mapcar.org/haskell/TimedStateT/dist/doc/html/ |
||

+ | |||

+ | == MonadExit == |
||

+ | |||

+ | The Exit monad provides [[short-circuiting]] for complex program flow logic. |
||

+ | |||

+ | If you are using CPS or MonadCont only for this purpose, the Exit monad will likely simplify your program considerably. |
||

+ | |||

+ | View [[New monads/MonadExit|MonadExit]]. |
||

+ | |||

+ | == MonadSplit == |
||

+ | |||

+ | Represents the class of monads such that |
||

+ | |||

+ | <haskell>l == (msplit l >>= \(x,xs) -> return x `mplus` xs)</haskell> |
||

+ | |||

+ | In English, msplit is a counterpart to "mplus". |
||

+ | |||

+ | Using this, you can redefine many of the functions which previously depended on lists: foldM, scanM, inits, tails, and some derived functions. |
||

+ | |||

+ | Note: A more general form of this monad, |
||

+ | [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html Data.Foldable], is now part of the |
||

+ | [http://haskell.org/ghc/docs/latest/html/libraries/ standard libraries]. |
||

+ | |||

+ | View [[New monads/MonadSplit]]. |
||

+ | |||

+ | == Lazy and Strict variants == |
||

+ | |||

+ | This section contains monads that have interesting Strict or Lazy properties. |
||

+ | |||

+ | === LazyWriterT === |
||

+ | |||

+ | This came up on the mailing list: Why is WriterT never lazy? The answer is it does not use lazy patterns with "~". So here is a more useful [[New monads/LazyWriterT]] that add two "~" to the definition of (>>=) and renames WriterT to LazyWriterT. |
||

+ | |||

+ | === Strict RWS === |
||

+ | |||

+ | This was contribute by John Meacham on on the haskell-cafe mailing list. [[New monads/UnboxedRWS]] is an strict variant of RWS. |
||

+ | |||

+ | [[Category:Idioms]] |
||

+ | [[Category:Monad]] |
||

+ | [[Category:Proposals]] |

## Latest revision as of 23:10, 7 September 2010

## Contents |

Remember to add a [ [ Category:Code ] ] tag to any new sub-pages.

## [edit] 1 MonadBase

It seems that the liftIO function from MonadIO can be generalized to access whatever the base of a transformer stack happens to be. So there is no need for a liftSTM, liftST, etc.

View New monads/MonadBase.

## [edit] 2 MonadLib

MonadLib is written by Iavor S. Diatchki.

It is a new version of the mtl package with base monads: Id, and Lift, and transformers ReaderT, WriterT, StateT, ExceptionT, ChoiceT, and ContT.

It also defines BaseM which is like MonadBase above.

## [edit] 3 MonadRandom

A simple monad transformer to allow computations in the transformed monad to generate random values.

View New monads/MonadRandom.

### [edit] 3.1 MonadRandomSplittable

A refinement of MonadRandom to integrate RandomGen's split function.

View at New monads/MonadRandomSplittable

## [edit] 4 MaybeT

The Maybe monad deserves a transformer, just like the other classic monads.

View New monads/MaybeT.

## [edit] 5 MonadSupply

Here is a simple monad/monad transformer for computations which consume values from a (finite or infinite) supply. Note that due to pattern matching, running out of supply in a non-MonadZero monad will cause an error.

View New monads/MonadSupply.

## [edit] 6 MonadUndo

Here is a modified state monad transformer for keeping track of undo/redo states automatically.

View New monads/MonadUndo.

## [edit] 7 MonadUnique

This is a simple (trivial) monad transformer for supplying unique integer values to an algorithm.

View New monads/MonadUnique.

## [edit] 8 MonadSTO

Here's an extension of the ST monad in which the references are ordered and showable (they list their creation index).

View New monads/MonadSTO.

## [edit] 9 MonadNondet

There is a MonadNondet that when compiled with optimizations outperforms List.

## [edit] 10 Stateful nondeterminism

There is a Stateful nondeterminism monad for if you want to do nondeterministic computation with local states for each of your threads and a global state shared by all your threads.

## [edit] 11 MonadAdvSTM

Here is an extension of STM to easy interaction with IO after committing or retrying. Inspired by Simon P-J.

View New monads/MonadAdvSTM.

## [edit] 12 TimedStateT

A monad transformer which combines State, Reader, and Error functionality to give the effect of a StateT monad which checks clock-time and stops the current computation if a period is exceeded.

darcs get http://www.mapcar.org/haskell/TimedStateT/

Haddocks: http://www.mapcar.org/haskell/TimedStateT/dist/doc/html/

## [edit] 13 MonadExit

The Exit monad provides short-circuiting for complex program flow logic.

If you are using CPS or MonadCont only for this purpose, the Exit monad will likely simplify your program considerably.

View MonadExit.

## [edit] 14 MonadSplit

Represents the class of monads such that

l == (msplit l >>= \(x,xs) -> return x `mplus` xs)

In English, msplit is a counterpart to "mplus".

Using this, you can redefine many of the functions which previously depended on lists: foldM, scanM, inits, tails, and some derived functions.

Note: A more general form of this monad, Data.Foldable, is now part of the standard libraries.

View New monads/MonadSplit.

## [edit] 15 Lazy and Strict variants

This section contains monads that have interesting Strict or Lazy properties.

### [edit] 15.1 LazyWriterT

This came up on the mailing list: Why is WriterT never lazy? The answer is it does not use lazy patterns with "~". So here is a more useful New monads/LazyWriterT that add two "~" to the definition of (>>=) and renames WriterT to LazyWriterT.

### [edit] 15.2 Strict RWS

This was contribute by John Meacham on on the haskell-cafe mailing list. New monads/UnboxedRWS is an strict variant of RWS.