Skip to content

Commit 285f934

Browse files
Add quick overview of do notation in Monad docs
1 parent 8157482 commit 285f934

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/Control/Monad.purs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,50 @@ import Data.Unit (Unit)
2828
-- | - Left Identity: `pure x >>= f = f x`
2929
-- | - Right Identity: `x >>= pure = x`
3030
-- | - Applicative Superclass: `apply = ap`
31+
-- |
32+
-- | ## Do Notation
33+
-- |
34+
-- | When using a type that has an instance for `Monad`, one can use
35+
-- | "do notation." In short, this code...
36+
-- | ```
37+
-- | foo =
38+
-- | bind boxedA (\a ->
39+
-- | let b = a + 4
40+
-- | in bind boxedC (\c ->
41+
-- | bind boxedUnit (\_ ->
42+
-- | pure (a * b - c)
43+
-- | )
44+
-- | )
45+
-- | )
46+
-- | ```
47+
-- |
48+
-- | ... can be converted into this code...
49+
-- | ```
50+
-- | foo =
51+
-- | boxedA >>= (\a ->
52+
-- | let b = a + 4
53+
-- | in boxedC >>= (\c ->
54+
-- | boxedUnit >>= (\_ ->
55+
-- | pure (a * b - c)
56+
-- | )
57+
-- | )
58+
-- | )
59+
-- | ```
60+
-- |
61+
-- | ...which can be converted once more into "do notation:"
62+
-- | ```
63+
-- | foo = do
64+
-- | a <- boxedA
65+
-- | let b = a + 4
66+
-- | c <- boxedC
67+
-- | boxedUnit
68+
-- | pure (a * b - c)
69+
-- | ```
70+
-- |
71+
-- | Note: if one wants to use "do notation" but redefine what the in-scope
72+
-- | definitions are for `bind` and `pure` in a given context, one can use
73+
-- | "qualified do notation." This is an intermediate/advanced language feature
74+
-- | not explained here.
3175
class (Applicative m, Bind m) <= Monad m
3276

3377
instance monadFn :: Monad ((->) r)

0 commit comments

Comments
 (0)