Skip to content

Implement ap in terms of Bind #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Control/Bind.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Control.Bind
, composeKleisli, (>=>)
, composeKleisliFlipped, (<=<)
, ifM
, ap
, module Data.Functor
, module Control.Apply
, module Control.Applicative
Expand All @@ -32,10 +33,11 @@ import Data.Unit (Unit)
-- |
-- | where the function argument of `f` is given the name `y`.
-- |
-- | Instances must satisfy the following law in addition to the `Apply`
-- | Instances must satisfy the following laws in addition to the `Apply`
-- | laws:
-- |
-- | - Associativity: `(x >>= f) >>= g = x >>= (\k -> f k >>= g)`
-- | - Apply Superclass: `apply = ap`
-- |
-- | Associativity tells us that we can regroup operations which use `do`
-- | notation so that we can unambiguously write, for example:
Expand Down Expand Up @@ -134,3 +136,18 @@ infixr 1 composeKleisliFlipped as <=<
-- | ```
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


-- | `ap` provides a default implementation of `(<*>)` for any `Bind`, without
-- | using `(<*>)` as provided by the `Apply`-`Bind` superclass relationship.
-- |
-- | `ap` can therefore be used to write `Apply` instances as follows:
-- |
-- | ```purescript
-- | instance applyF :: Apply F where
-- | apply = ap
-- | ```
ap :: forall m a b. Bind m => m (a -> b) -> m a -> m b
ap f a = do
f' <- f
map f' a
21 changes: 1 addition & 20 deletions src/Control/Monad.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Control.Monad
( class Monad
, liftM1
, ap
, whenM
, unlessM
, module Data.Functor
Expand All @@ -12,7 +11,7 @@ module Control.Monad

import Control.Applicative (class Applicative, liftA1, pure, unless, when)
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))
import Control.Bind (class Bind, bind, ap, ifM, join, (<=<), (=<<), (>=>), (>>=))

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit)
Expand All @@ -27,7 +26,6 @@ import Data.Unit (Unit)
-- |
-- | - Left Identity: `pure x >>= f = f x`
-- | - Right Identity: `x >>= pure = x`
-- | - Applicative Superclass: `apply = ap`
class (Applicative m, Bind m) <= Monad m

instance monadFn :: Monad ((->) r)
Expand All @@ -50,23 +48,6 @@ liftM1 f a = do
a' <- a
pure (f a')

-- | `ap` provides a default implementation of `(<*>)` for any
-- | [`Monad`](#monad), without using `(<*>)` as provided by the
-- | [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
-- |
-- | `ap` can therefore be used to write [`Apply`](#apply) instances as
-- | follows:
-- |
-- | ```purescript
-- | instance applyF :: Apply F where
-- | apply = ap
-- | ```
ap :: forall m a b. Monad m => m (a -> b) -> m a -> m b
ap f a = do
f' <- f
a' <- a
pure (f' a')

-- | Perform a monadic action when a condition is true, where the conditional
-- | value is also in a monadic context.
whenM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
Expand Down
4 changes: 2 additions & 2 deletions src/Prelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module Prelude

import Control.Applicative (class Applicative, pure, liftA1, unless, when)
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Bind (class Bind, bind, class Discard, discard, ifM, join, (<=<), (=<<), (>=>), (>>=))
import Control.Bind (class Bind, bind, class Discard, discard, ifM, ap, join, (<=<), (=<<), (>=>), (>>=))
import Control.Category (class Category, identity)
import Control.Monad (class Monad, ap, liftM1, unlessM, whenM)
import Control.Monad (class Monad, liftM1, unlessM, whenM)
import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))

import Data.Boolean (otherwise)
Expand Down