Skip to content

Commit 78c1d02

Browse files
committed
Docs
1 parent 95ae38f commit 78c1d02

File tree

8 files changed

+213
-7
lines changed

8 files changed

+213
-7
lines changed

README.md

Lines changed: 136 additions & 7 deletions
Large diffs are not rendered by default.

src/Control/Alternative.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- | This module defines the `Alternative` type class and associated
2+
-- | helper functions.
3+
14
module Control.Alternative where
25

36
import Control.Alt
@@ -14,9 +17,18 @@ import Control.Plus
1417
-- | - Annihilation: `empty <*> f = empty`
1518
class (Applicative f, Plus f) <= Alternative f
1619

20+
-- | Attempt a computation multiple times, requiring at least one success.
21+
-- |
22+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
23+
-- | termination.
1724
some :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
1825
some v = (:) <$> v <*> defer1 (\_ -> many v)
1926

27+
-- | Attempt a computation multiple times, returning as many successful results
28+
-- | as possible (possibly zero).
29+
-- |
30+
-- | The `Lazy` constraint is used to generate the result lazily, to ensure
31+
-- | termination.
2032
many :: forall f a. (Alternative f, Lazy1 f) => f a -> f [a]
2133
many v = some v <|> pure []
2234

src/Control/Bind.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1+
-- | This module defines helper functions for working with `Bind` instances.
2+
13
module Control.Bind where
24

35
infixr 1 =<<
46
infixr 1 >=>
57
infixr 1 <=<
68

9+
-- | A version of `(>>=)` with its arguments reversed
710
(=<<) :: forall a b m. (Bind m) => (a -> m b) -> m a -> m b
811
(=<<) f m = m >>= f
912

13+
-- | Forwards Kleisli composition
14+
-- |
15+
-- | For example:
16+
-- |
17+
-- | ```purescript
18+
-- | import Data.Array (head, tail)
19+
-- |
20+
-- | third = tail >=> tail >=> head
21+
-- | ```
1022
(>=>) :: forall a b c m. (Bind m) => (a -> m b) -> (b -> m c) -> a -> m c
1123
(>=>) f g a = f a >>= g
1224

25+
-- | Backwards Kleisli composition
1326
(<=<) :: forall a b c m. (Bind m) => (b -> m c) -> (a -> m b) -> a -> m c
1427
(<=<) f g a = f =<< g a
1528

29+
-- | Collapse two applications of a monadic type constructor into one.
1630
join :: forall a m. (Bind m) => m (m a) -> m a
1731
join m = m >>= id
1832

33+
-- | Execute a monadic action if a condition holds.
34+
-- |
35+
-- | For example:
36+
-- |
37+
-- | ```purescript
38+
-- | main = ifM ((< 0.5) <$> random)
39+
-- | (trace "Heads")
40+
-- | (trace "Tails")
41+
-- | ```
1942
ifM :: forall a m. (Bind m) => m Boolean -> m a -> m a -> m a
2043
ifM cond t f = cond >>= \cond' -> if cond' then t else f

src/Control/Comonad.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Control.Extend
66

77
-- | `Comonad` extends the `Extend` class with the `extract` function
88
-- | which extracts a value, discarding the comonadic context.
9+
-- |
10+
-- | `Comonad` is the dual of `Monad`, and `extract` is the dual of
11+
-- | `pure` or `return`.
912
-- |
1013
-- | Laws:
1114
-- |

src/Control/Extend.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ infixr 1 =<=
1111
-- | which extends a local context-dependent computation to
1212
-- | a global computation.
1313
-- |
14+
-- | `Extend` is the dual of `Bind`, and `(<<=)` is the dual of
15+
-- | `(>>=)`.
16+
-- |
1417
-- | Laws:
1518
-- |
1619
-- | - Associativity: `extend f <<< extend g = extend (f <<< extend g)`
@@ -33,5 +36,7 @@ instance extendArr :: (Semigroup w) => Extend ((->) w) where
3336
(=<=) f g w = f (g <<= w)
3437

3538
-- | Duplicate a comonadic context
39+
-- |
40+
-- | `duplicate` is dual to `join`.
3641
duplicate :: forall a w. (Extend w) => w a -> w (w a)
3742
duplicate w = id <<= w

src/Control/Lazy.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
-- | This module defines the `Lazy` type class and associated
2+
-- | helper functions.
3+
14
module Control.Lazy where
25

6+
-- | The `Lazy` class represents types which allow evaluation of values
7+
-- | to be _deferred_.
8+
-- |
9+
-- | Usually, this means that a type contains a function arrow which can
10+
-- | be used to delay evaluation.
311
class Lazy l where
412
defer :: (Unit -> l) -> l
513

14+
-- | A version of `Lazy` for type constructors of one type argument
615
class Lazy1 l where
716
defer1 :: forall a. (Unit -> l a) -> l a
817

18+
-- | A version of `Lazy` for type constructors of two type arguments
919
class Lazy2 l where
1020
defer2 :: forall a b. (Unit -> l a b) -> l a b
1121

22+
-- | `fix` defines a value as the fixed point of a function.
23+
-- |
24+
-- | The `Lazy` instance allows us to generate the result lazily.
1225
fix :: forall l a. (Lazy l) => (l -> l) -> l
1326
fix f = defer (\_ -> f (fix f))
1427

28+
-- | A version of `fix` for type constructors of one type argument
1529
fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a
1630
fix1 f = defer1 (\_ -> f (fix1 f))
1731

32+
-- | A version of `fix` for type constructors of two type arguments
1833
fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b
1934
fix2 f = defer2 (\_ -> f (fix2 f))

src/Control/MonadPlus.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- | This module defines the `MonadPlus` type class.
2+
13
module Control.MonadPlus where
24

35
import Control.Alternative
@@ -13,6 +15,21 @@ import Control.Plus
1315
-- | - Annihilation: `empty >>= f = empty`
1416
class (Monad m, Alternative m) <= MonadPlus m
1517

18+
-- | Fail using `Plus` if a condition does not hold, or
19+
-- | succeed using `Monad` if it does.
20+
-- |
21+
-- | For example:
22+
-- |
23+
-- | ```purescript
24+
-- | import Data.Array
25+
-- |
26+
-- | factors :: Number -> [Number]
27+
-- | factors n = do
28+
-- | a <- 1 .. n
29+
-- | b <- 1 .. a
30+
-- | guard $ a * b == n
31+
-- | return a
32+
-- | ```
1633
guard :: forall m. (MonadPlus m) => Boolean -> m Unit
1734
guard true = return unit
1835
guard false = empty

src/Control/Plus.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- | This module defines the `Plus` type class.
2+
13
module Control.Plus where
24

35
import Control.Alt

0 commit comments

Comments
 (0)