-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,43 @@ | ||
-- | This module defines helper functions for working with `Bind` instances. | ||
|
||
module Control.Bind where | ||
|
||
infixr 1 =<< | ||
infixr 1 >=> | ||
infixr 1 <=< | ||
|
||
-- | A version of `(>>=)` with its arguments reversed | ||
(=<<) :: forall a b m. (Bind m) => (a -> m b) -> m a -> m b | ||
(=<<) f m = m >>= f | ||
|
||
-- | Forwards Kleisli composition | ||
-- | | ||
-- | For example: | ||
-- | | ||
-- | ```purescript | ||
-- | import Data.Array (head, tail) | ||
-- | | ||
-- | third = tail >=> tail >=> head | ||
-- | ``` | ||
(>=>) :: forall a b c m. (Bind m) => (a -> m b) -> (b -> m c) -> a -> m c | ||
(>=>) f g a = f a >>= g | ||
|
||
-- | Backwards Kleisli composition | ||
(<=<) :: forall a b c m. (Bind m) => (b -> m c) -> (a -> m b) -> a -> m c | ||
(<=<) f g a = f =<< g a | ||
|
||
-- | Collapse two applications of a monadic type constructor into one. | ||
join :: forall a m. (Bind m) => m (m a) -> m a | ||
join m = m >>= id | ||
|
||
-- | Execute a monadic action if a condition holds. | ||
-- | | ||
-- | For example: | ||
-- | | ||
-- | ```purescript | ||
-- | main = ifM ((< 0.5) <$> random) | ||
-- | (trace "Heads") | ||
-- | (trace "Tails") | ||
-- | ``` | ||
ifM :: forall a m. (Bind m) => m Boolean -> m a -> m a -> m a | ||
ifM cond t f = cond >>= \cond' -> if cond' then t else f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
-- | This module defines the `Lazy` type class and associated | ||
-- | helper functions. | ||
|
||
module Control.Lazy where | ||
|
||
-- | The `Lazy` class represents types which allow evaluation of values | ||
-- | to be _deferred_. | ||
-- | | ||
-- | Usually, this means that a type contains a function arrow which can | ||
-- | be used to delay evaluation. | ||
class Lazy l where | ||
defer :: (Unit -> l) -> l | ||
|
||
-- | A version of `Lazy` for type constructors of one type argument | ||
class Lazy1 l where | ||
defer1 :: forall a. (Unit -> l a) -> l a | ||
|
||
-- | A version of `Lazy` for type constructors of two type arguments | ||
class Lazy2 l where | ||
defer2 :: forall a b. (Unit -> l a b) -> l a b | ||
|
||
-- | `fix` defines a value as the fixed point of a function. | ||
-- | | ||
-- | The `Lazy` instance allows us to generate the result lazily. | ||
fix :: forall l a. (Lazy l) => (l -> l) -> l | ||
fix f = defer (\_ -> f (fix f)) | ||
|
||
-- | A version of `fix` for type constructors of one type argument | ||
fix1 :: forall l a. (Lazy1 l) => (l a -> l a) -> l a | ||
fix1 f = defer1 (\_ -> f (fix1 f)) | ||
|
||
-- | A version of `fix` for type constructors of two type arguments | ||
fix2 :: forall l a b. (Lazy2 l) => (l a b -> l a b) -> l a b | ||
fix2 f = defer2 (\_ -> f (fix2 f)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
-- | This module defines the `Plus` type class. | ||
|
||
module Control.Plus where | ||
|
||
import Control.Alt | ||
|