File tree 8 files changed +213
-7
lines changed 8 files changed +213
-7
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ -- | This module defines the `Alternative` type class and associated
2
+ -- | helper functions.
3
+
1
4
module Control.Alternative where
2
5
3
6
import Control.Alt
@@ -14,9 +17,18 @@ import Control.Plus
14
17
-- | - Annihilation: `empty <*> f = empty`
15
18
class (Applicative f , Plus f ) <= Alternative f
16
19
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.
17
24
some :: forall f a . (Alternative f , Lazy1 f ) => f a -> f [a ]
18
25
some v = (:) <$> v <*> defer1 (\_ -> many v)
19
26
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.
20
32
many :: forall f a . (Alternative f , Lazy1 f ) => f a -> f [a ]
21
33
many v = some v <|> pure []
22
34
Original file line number Diff line number Diff line change
1
+ -- | This module defines helper functions for working with `Bind` instances.
2
+
1
3
module Control.Bind where
2
4
3
5
infixr 1 =<<
4
6
infixr 1 >=>
5
7
infixr 1 <=<
6
8
9
+ -- | A version of `(>>=)` with its arguments reversed
7
10
(=<<) :: forall a b m . (Bind m ) => (a -> m b ) -> m a -> m b
8
11
(=<<) f m = m >>= f
9
12
13
+ -- | Forwards Kleisli composition
14
+ -- |
15
+ -- | For example:
16
+ -- |
17
+ -- | ```purescript
18
+ -- | import Data.Array (head, tail)
19
+ -- |
20
+ -- | third = tail >=> tail >=> head
21
+ -- | ```
10
22
(>=>) :: forall a b c m . (Bind m ) => (a -> m b ) -> (b -> m c ) -> a -> m c
11
23
(>=>) f g a = f a >>= g
12
24
25
+ -- | Backwards Kleisli composition
13
26
(<=<) :: forall a b c m . (Bind m ) => (b -> m c ) -> (a -> m b ) -> a -> m c
14
27
(<=<) f g a = f =<< g a
15
28
29
+ -- | Collapse two applications of a monadic type constructor into one.
16
30
join :: forall a m . (Bind m ) => m (m a ) -> m a
17
31
join m = m >>= id
18
32
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
+ -- | ```
19
42
ifM :: forall a m . (Bind m ) => m Boolean -> m a -> m a -> m a
20
43
ifM cond t f = cond >>= \cond' -> if cond' then t else f
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ import Control.Extend
6
6
7
7
-- | `Comonad` extends the `Extend` class with the `extract` function
8
8
-- | 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`.
9
12
-- |
10
13
-- | Laws:
11
14
-- |
Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ infixr 1 =<=
11
11
-- | which extends a local context-dependent computation to
12
12
-- | a global computation.
13
13
-- |
14
+ -- | `Extend` is the dual of `Bind`, and `(<<=)` is the dual of
15
+ -- | `(>>=)`.
16
+ -- |
14
17
-- | Laws:
15
18
-- |
16
19
-- | - Associativity: `extend f <<< extend g = extend (f <<< extend g)`
@@ -33,5 +36,7 @@ instance extendArr :: (Semigroup w) => Extend ((->) w) where
33
36
(=<=) f g w = f (g <<= w)
34
37
35
38
-- | Duplicate a comonadic context
39
+ -- |
40
+ -- | `duplicate` is dual to `join`.
36
41
duplicate :: forall a w . (Extend w ) => w a -> w (w a )
37
42
duplicate w = id <<= w
Original file line number Diff line number Diff line change
1
+ -- | This module defines the `Lazy` type class and associated
2
+ -- | helper functions.
3
+
1
4
module Control.Lazy where
2
5
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.
3
11
class Lazy l where
4
12
defer :: (Unit -> l ) -> l
5
13
14
+ -- | A version of `Lazy` for type constructors of one type argument
6
15
class Lazy1 l where
7
16
defer1 :: forall a . (Unit -> l a ) -> l a
8
17
18
+ -- | A version of `Lazy` for type constructors of two type arguments
9
19
class Lazy2 l where
10
20
defer2 :: forall a b . (Unit -> l a b ) -> l a b
11
21
22
+ -- | `fix` defines a value as the fixed point of a function.
23
+ -- |
24
+ -- | The `Lazy` instance allows us to generate the result lazily.
12
25
fix :: forall l a . (Lazy l ) => (l -> l ) -> l
13
26
fix f = defer (\_ -> f (fix f))
14
27
28
+ -- | A version of `fix` for type constructors of one type argument
15
29
fix1 :: forall l a . (Lazy1 l ) => (l a -> l a ) -> l a
16
30
fix1 f = defer1 (\_ -> f (fix1 f))
17
31
32
+ -- | A version of `fix` for type constructors of two type arguments
18
33
fix2 :: forall l a b . (Lazy2 l ) => (l a b -> l a b ) -> l a b
19
34
fix2 f = defer2 (\_ -> f (fix2 f))
Original file line number Diff line number Diff line change
1
+ -- | This module defines the `MonadPlus` type class.
2
+
1
3
module Control.MonadPlus where
2
4
3
5
import Control.Alternative
@@ -13,6 +15,21 @@ import Control.Plus
13
15
-- | - Annihilation: `empty >>= f = empty`
14
16
class (Monad m , Alternative m ) <= MonadPlus m
15
17
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
+ -- | ```
16
33
guard :: forall m . (MonadPlus m ) => Boolean -> m Unit
17
34
guard true = return unit
18
35
guard false = empty
Original file line number Diff line number Diff line change
1
+ -- | This module defines the `Plus` type class.
2
+
1
3
module Control.Plus where
2
4
3
5
import Control.Alt
You can’t perform that action at this time.
0 commit comments