on master help sign in
Merge pull request #2959 from ziman/monad-flatten

Add flatten to Monad.

commit3963984a962d8fd77a759202c817a7c85dd72117 authorEdwin Brady <edwin.brady@gmail.com> dateSun 14 Feb 2016, 22:30 (11 years ago) parent9f202dcbdeaed6
2 files changed+12−3
libs/prelude/Prelude/Monad.idr+11−3 libs/prelude/Prelude/Providers.idr+1
libs/prelude/Prelude/Monad.idr+11−3
@@ -13,17 +13,25 @@ import IO
1313infixl 5 >>=
1414
1515interface Applicative m => Monad (m : Type -> Type) where
16+ ||| Also called `bind`.
1617 (>>=) : m a -> ((result : a) -> m b) -> m b
1718
18−||| Also called `join` or mu
19−flatten : Monad m => m (m a) -> m a
20−flatten a = a >>= id
19+ ||| Also called `flatten` or mu
20+ join : m (m a) -> m a
21+
22+ -- default implementations
23+ (>>=) x f = join (f <$> x)
24+ join x = x >>= id
2125
2226||| For compatibility with Haskell. Note that monads are **not** free to
2327||| define `return` and `pure` differently!
2428return : Monad m => a -> m a
2529return = pure
2630
31+flatten : Monad m => m (m a) -> m a
32+flatten = join
33+%deprecate flatten "Please use `join`, which is the standard name."
34+
2735-- Annoyingly, these need to be here, so that we can use them in other
2836-- Prelude modules other than the top level.
2937
libs/prelude/Prelude/Providers.idr+1
@@ -1,5 +1,6 @@
11module Prelude.Providers
22
3+import Prelude.Basics
34import Prelude.Functor
45import Prelude.Applicative
56import Prelude.Monad