File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,50 @@ import Data.Unit (Unit)
28
28
-- | - Left Identity: `pure x >>= f = f x`
29
29
-- | - Right Identity: `x >>= pure = x`
30
30
-- | - 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.
31
75
class (Applicative m , Bind m ) <= Monad m
32
76
33
77
instance monadFn :: Monad ((-> ) r )
You can’t perform that action at this time.
0 commit comments