File tree Expand file tree Collapse file tree 8 files changed +213
-7
lines changed
Expand file tree Collapse file tree 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+
14module Control.Alternative where
25
36import Control.Alt
@@ -14,9 +17,18 @@ import Control.Plus
1417-- | - Annihilation: `empty <*> f = empty`
1518class (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.
1724some :: forall f a . (Alternative f , Lazy1 f ) => f a -> f [a ]
1825some 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.
2032many :: forall f a . (Alternative f , Lazy1 f ) => f a -> f [a ]
2133many v = some v <|> pure []
2234
Original file line number Diff line number Diff line change 1+ -- | This module defines helper functions for working with `Bind` instances.
2+
13module 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
Original file line number Diff line number Diff 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-- |
Original file line number Diff line number Diff 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`.
3641duplicate :: forall a w . (Extend w ) => w a -> w (w a )
3742duplicate 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+
14module 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.
311class Lazy l where
412 defer :: (Unit -> l ) -> l
513
14+ -- | A version of `Lazy` for type constructors of one type argument
615class 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
919class 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.
1225fix :: forall l a . (Lazy l ) => (l -> l ) -> l
1326fix f = defer (\_ -> f (fix f))
1427
28+ -- | A version of `fix` for type constructors of one type argument
1529fix1 :: forall l a . (Lazy1 l ) => (l a -> l a ) -> l a
1630fix1 f = defer1 (\_ -> f (fix1 f))
1731
32+ -- | A version of `fix` for type constructors of two type arguments
1833fix2 :: forall l a b . (Lazy2 l ) => (l a b -> l a b ) -> l a b
1934fix2 f = defer2 (\_ -> f (fix2 f))
Original file line number Diff line number Diff line change 1+ -- | This module defines the `MonadPlus` type class.
2+
13module Control.MonadPlus where
24
35import Control.Alternative
@@ -13,6 +15,21 @@ import Control.Plus
1315-- | - Annihilation: `empty >>= f = empty`
1416class (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+ -- | ```
1633guard :: forall m . (MonadPlus m ) => Boolean -> m Unit
1734guard true = return unit
1835guard false = empty
Original file line number Diff line number Diff line change 1+ -- | This module defines the `Plus` type class.
2+
13module Control.Plus where
24
35import Control.Alt
You can’t perform that action at this time.
0 commit comments