Skip to content

Fix loop #233

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 9 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"devDependencies": {
"eslint": "^4.19.1",
"purescript-psa": "^0.6.0",
"purescript-psa": "^0.8.0",
"pulp": "^15.0.0",
"rimraf": "^2.6.2"
}
Expand Down
17 changes: 1 addition & 16 deletions src/Control/Bind.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Control.Bind
, composeKleisli, (>=>)
, composeKleisliFlipped, (<=<)
, ifM
, ap
, module Data.Functor
, module Control.Apply
, module Control.Applicative
Expand Down Expand Up @@ -38,7 +37,7 @@ import Type.Proxy (Proxy(..), Proxy2, Proxy3)
-- | laws:
-- |
-- | - Associativity: `(x >>= f) >>= g = x >>= (\k -> f k >>= g)`
-- | - Apply Superclass: `apply = ap`
-- | - Apply Superclass: `apply f x = f >>= \f’ -> map f’ x`
-- |
-- | Associativity tells us that we can regroup operations which use `do`
-- | notation so that we can unambiguously write, for example:
Expand Down Expand Up @@ -149,17 +148,3 @@ 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
22 changes: 21 additions & 1 deletion src/Control/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Control.Monad
, liftM1
, whenM
, unlessM
, ap
, module Data.Functor
, module Control.Apply
, module Control.Applicative
Expand All @@ -11,7 +12,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, ap, ifM, join, (<=<), (=<<), (>=>), (>>=))
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit)
Expand Down Expand Up @@ -64,3 +65,22 @@ unlessM :: forall m. Monad m => m Boolean -> m Unit -> m Unit
unlessM mb m = do
b <- mb
unless b m

-- | `ap` provides a default implementation of `(<*>)` for any `Monad`, without
-- | using `(<*>)` as provided by the `Apply`-`Monad` superclass relationship.
-- |
-- | `ap` can therefore be used to write `Apply` instances as follows:
-- |
-- | ```purescript
-- | instance applyF :: Apply F where
-- | apply = ap
-- | ```
-- Note: Only a `Bind` constraint is needed, but this can
-- produce loops when used with other default implementations
-- (i.e. `liftA1`).
-- See https://github.com/purescript/purescript-prelude/issues/232
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')
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, ap, join, (<=<), (=<<), (>=>), (>>=))
import Control.Bind (class Bind, bind, class Discard, discard, ifM, join, (<=<), (=<<), (>=>), (>>=))
import Control.Category (class Category, identity)
import Control.Monad (class Monad, liftM1, unlessM, whenM)
import Control.Monad (class Monad, liftM1, unlessM, whenM, ap)
import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))

import Data.Boolean (otherwise)
Expand Down