Skip to content
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

Prepare for 2.0 release #77

Merged
merged 10 commits into from
Oct 10, 2016
18 changes: 2 additions & 16 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,12 @@
"package.json"
],
"dependencies": {
"purescript-arrays": "^1.1.0",
"purescript-arrays": "^3.0.0",
"purescript-lazy": "^2.0.0",
"purescript-tailrec": "^2.0.0",
"purescript-unfoldable": "^1.1.0",
"purescript-distributive": "bump",
"purescript-distributive": "^2.0.0",
"purescript-tuples": "^3.0.0"
},
"devDependencies": {
"purescript-console": "^2.0.0"
},
"resolutions": {
"purescript-tuples": "^3.0.0",
"purescript-monoid": "^2.0.0",
"purescript-foldable-traversable": "^2.0.0",
"purescript-st": "^2.0.0",
"purescript-eff": "^2.0.0",
"purescript-control": "^2.0.0",
"purescript-bifunctors": "^2.0.0",
"purescript-invariant": "^2.0.0",
"purescript-maybe": "^2.0.0",
"purescript-prelude": "^2.1.0"
}
}
30 changes: 19 additions & 11 deletions src/Control/Comonad/Env/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import Control.Comonad.Env.Trans (EnvT(..))

import Data.Tuple (Tuple(..), fst)

-- | The `ComonadEnv` type class represents those monads which support a global environment via
-- | `ask` and `local`.
-- | The `ComonadEnv` type class represents those comonads which support a
-- | global environment that can be provided via the `ask` function.
-- |
-- | - `ask` reads the current environment from the context.
-- | - `local` changes the value of the global environment.
-- | An implementation is provided for `EnvT`.
class Comonad w <= ComonadAsk e w | w -> e where
ask :: forall a. w a -> e

-- | Get a value which depends on the environment.
asks :: forall e1 e2 w. ComonadEnv e1 w => (e1 -> e2) -> w e1 -> e2
asks f x = f (ask x)

-- | The `ComonadEnv` type class extends `ComonadAsk` with a function
-- | `local f x` that allows the value of the local context to be modified for
-- | the duration of the execution of action `x`.
-- |
-- | An implementation is provided for `EnvT`.
-- |
Expand All @@ -20,19 +29,18 @@ import Data.Tuple (Tuple(..), fst)
-- | - `ask (local f x) = f (ask x)`
-- | - `extract (local _ x) = extract a`
-- | - `extend g (local f x) = extend (g <<< local f) x`
class Comonad w <= ComonadEnv e w | w -> e where
ask :: forall a. w a -> e
class ComonadAsk e w <= ComonadEnv e w | w -> e where
local :: forall a. (e -> e) -> w a -> w a

-- | Get a value which depends on the environment.
asks :: forall e1 e2 w. ComonadEnv e1 w => (e1 -> e2) -> w e1 -> e2
asks f x = f (ask x)
instance comonadAskTuple :: ComonadAsk e (Tuple e) where
ask = fst

instance comonadEnvTuple :: ComonadEnv e (Tuple e) where
ask = fst
local f (Tuple x y) = Tuple (f x) y

instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w) where
instance comonadAskEnvT :: Comonad w => ComonadAsk e (EnvT e w) where
ask (EnvT x) = fst x

instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w) where
local f (EnvT x) = EnvT case x of
Tuple x y -> Tuple (f x) y
5 changes: 4 additions & 1 deletion src/Control/Comonad/Env/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module Control.Comonad.Env.Trans where
import Prelude

import Control.Comonad (class Comonad, extract)
import Control.Comonad.Trans (class ComonadTrans)
import Control.Comonad.Trans.Class (class ComonadTrans)
import Control.Extend (class Extend, (<<=))

import Data.Tuple (Tuple(..))
import Data.Newtype (class Newtype)

-- | The environment comonad transformer.
-- |
Expand All @@ -30,6 +31,8 @@ withEnvT f (EnvT (Tuple e x)) = EnvT $ Tuple (f e) x
mapEnvT :: forall e w1 w2 a b. (w1 a -> w2 b) -> EnvT e w1 a -> EnvT e w2 b
mapEnvT f (EnvT (Tuple e x)) = EnvT $ Tuple e (f x)

derive instance newtypeEnvT :: Newtype (EnvT e w a) _

instance functorEnvT :: Functor w => Functor (EnvT e w) where
map f (EnvT (Tuple e x)) = EnvT $ Tuple e (f <$> x)

Expand Down
5 changes: 4 additions & 1 deletion src/Control/Comonad/Store/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module Control.Comonad.Store.Trans where
import Prelude

import Control.Comonad (class Comonad, extract)
import Control.Comonad.Trans (class ComonadTrans)
import Control.Comonad.Trans.Class (class ComonadTrans)
import Control.Extend (class Extend, (<<=))

import Data.Tuple (Tuple(..))
import Data.Newtype (class Newtype)

-- | The store comonad transformer.
-- |
Expand All @@ -22,6 +23,8 @@ newtype StoreT s w a = StoreT (Tuple (w (s -> a)) s)
runStoreT :: forall s w a. StoreT s w a -> Tuple (w (s -> a)) s
runStoreT (StoreT s) = s

derive instance newtypeStoreT :: Newtype (StoreT s w a) _

instance functorStoreT :: Functor w => Functor (StoreT s w) where
map f (StoreT (Tuple w s)) = StoreT $ Tuple ((\h -> h >>> f) <$> w) s

Expand Down
5 changes: 4 additions & 1 deletion src/Control/Comonad/Traced/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module Control.Comonad.Traced.Trans where
import Prelude

import Control.Comonad (class Comonad, extract)
import Control.Comonad.Trans (class ComonadTrans)
import Control.Comonad.Trans.Class (class ComonadTrans)
import Control.Extend (class Extend, (<<=))

import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype)

-- | The cowriter comonad transformer.
-- |
Expand All @@ -22,6 +23,8 @@ newtype TracedT t w a = TracedT (w (t -> a))
runTracedT :: forall w a t. TracedT t w a -> w (t -> a)
runTracedT (TracedT w) = w

derive instance newtypeTracedT :: Newtype (TracedT t w a) _

instance functorTracedT :: Functor w => Functor (TracedT t w) where
map f (TracedT w) = TracedT ((\g t -> f $ g t) <$> w)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- | This module defines the `ComonadTrans` type class of _comonad transformers_.

module Control.Comonad.Trans where
module Control.Comonad.Trans.Class where

import Control.Comonad (class Comonad)

Expand Down
14 changes: 10 additions & 4 deletions src/Control/Monad/Cont/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

module Control.Monad.Cont.Trans
( ContT(..), runContT, mapContT, withContT
, module Control.Monad.Trans
, module Control.Monad.Trans.Class
, module Control.Monad.Cont.Class
) where

import Prelude

import Control.Monad.Cont.Class (class MonadCont, callCC)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Reader.Class (class MonadReader, ask, local)
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
import Control.Monad.State.Class (class MonadState, state)
import Control.Monad.Trans (class MonadTrans, lift)
import Control.Monad.Trans.Class (class MonadTrans, lift)

import Data.Newtype (class Newtype)

-- | The CPS monad transformer.
-- |
Expand All @@ -31,6 +33,8 @@ mapContT f (ContT m) = ContT (\k -> f (m k))
withContT :: forall r m a b. ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b
withContT f (ContT m) = ContT (\k -> m (f k))

derive instance newtypeContT :: Newtype (ContT r m a) _

instance monadContContT :: Monad m => MonadCont (ContT r m) where
callCC f = ContT (\k -> case f (\a -> ContT (\_ -> k a)) of ContT f' -> f' k)

Expand All @@ -54,8 +58,10 @@ instance monadTransContT :: MonadTrans (ContT r) where
instance monadEffContT :: MonadEff eff m => MonadEff eff (ContT r m) where
liftEff = lift <<< liftEff

instance monadReaderContT :: MonadReader r1 m => MonadReader r1 (ContT r m) where
instance monadAskContT :: MonadAsk r1 m => MonadAsk r1 (ContT r m) where
ask = lift ask

instance monadReaderContT :: MonadReader r1 m => MonadReader r1 (ContT r m) where
local f (ContT c) = ContT \k -> do
r <- ask
local f (c (local (const (r :: r1)) <<< k))
Expand Down
31 changes: 16 additions & 15 deletions src/Control/Monad/Except/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Control.Monad.Except.Trans
( ExceptT(..), runExceptT, withExceptT, mapExceptT, except
, module Control.Monad.Trans
, module Control.Monad.Trans.Class
, module Control.Monad.Error.Class
) where

Expand All @@ -13,18 +13,18 @@ import Control.Alternative (class Alternative)
import Control.Monad.Cont.Class (class MonadCont, callCC)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Error.Class (class MonadError, throwError, catchError)
import Control.Monad.Reader.Class (class MonadReader, local, ask)
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
import Control.Monad.RWS.Class (class MonadRWS)
import Control.Monad.State.Class (class MonadState, state)
import Control.Monad.Trans (class MonadTrans, lift)
import Control.Monad.Writer.Class (class MonadWriter, pass, listen, writer)
import Control.Monad.Trans.Class (class MonadTrans, lift)
import Control.Monad.Writer.Class (class MonadWriter, class MonadTell, pass, listen, tell)
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)

import Data.Either (Either(..), either)
import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype)
import Data.Tuple (Tuple(..))

-- | A monad transformer which adds exceptions to other monads, in the same way
Expand Down Expand Up @@ -52,16 +52,15 @@ mapExceptT f (ExceptT m) = ExceptT (f m)
except :: forall e m a. Applicative m => Either e a -> ExceptT e m a
except = ExceptT <<< pure

derive instance newtypeExceptT :: Newtype (ExceptT e m a) _

instance functorExceptT :: Functor m => Functor (ExceptT e m) where
map f = mapExceptT (map (map f))

instance applyExceptT :: Apply m => Apply (ExceptT e m) where
apply (ExceptT f) (ExceptT x) =
let f' = apply <$> f
x' = f' <*> x
in ExceptT x'
instance applyExceptT :: Monad m => Apply (ExceptT e m) where
apply = ap

instance applicativeExceptT :: Applicative m => Applicative (ExceptT e m) where
instance applicativeExceptT :: Monad m => Applicative (ExceptT e m) where
pure = ExceptT <<< pure <<< Right

instance bindExceptT :: Monad m => Bind (ExceptT e m) where
Expand Down Expand Up @@ -116,15 +115,19 @@ instance monadErrorExceptT :: Monad m => MonadError e (ExceptT e m) where
catchError (ExceptT m) k =
ExceptT (m >>= either (\a -> case k a of ExceptT b -> b) (pure <<< Right))

instance monadReaderExceptT :: MonadReader r m => MonadReader r (ExceptT e m) where
instance monadAskExceptT :: MonadAsk r m => MonadAsk r (ExceptT e m) where
ask = lift ask

instance monadReaderExceptT :: MonadReader r m => MonadReader r (ExceptT e m) where
local f = mapExceptT (local f)

instance monadStateExceptT :: MonadState s m => MonadState s (ExceptT e m) where
state f = lift (state f)

instance monadTellExceptT :: MonadTell w m => MonadTell w (ExceptT e m) where
tell = lift <<< tell

instance monadWriterExceptT :: MonadWriter w m => MonadWriter w (ExceptT e m) where
writer wd = lift (writer wd)
listen = mapExceptT \m -> do
Tuple a w <- listen m
pure $ (\r -> Tuple r w) <$> a
Expand All @@ -133,5 +136,3 @@ instance monadWriterExceptT :: MonadWriter w m => MonadWriter w (ExceptT e m) wh
pure case a of
Left e -> Tuple (Left e) id
Right (Tuple r f) -> Tuple (Right r) f

instance monadRWSExceptT :: MonadRWS r w s m => MonadRWS r w s (ExceptT e m)
11 changes: 7 additions & 4 deletions src/Control/Monad/List/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ module Control.Monad.List.Trans
, wrapLazy
, zipWith
, zipWith'
, module Control.Monad.Trans
, module Control.Monad.Trans.Class
) where

import Prelude

import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Trans (class MonadTrans, lift)
import Control.Monad.Trans.Class (class MonadTrans, lift)
import Control.MonadPlus (class MonadPlus)
import Control.MonadZero (class MonadZero)
import Control.Plus (class Plus)

import Data.Lazy (Lazy, defer, force)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Monoid (class Monoid)
import Data.Newtype (class Newtype)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Unfoldable (class Unfoldable)

Expand All @@ -52,7 +53,7 @@ import Data.Unfoldable (class Unfoldable)
-- | This monad transformer extends the base monad with _non-determinism_.
-- | That is, the transformed monad supports the same effects as the base monad
-- | but with multiple return values.
data ListT f a = ListT (f (Step a (ListT f a)))
newtype ListT f a = ListT (f (Step a (ListT f a)))

-- | The result of a single step in a `ListT` computation. Either:
-- |
Expand Down Expand Up @@ -215,7 +216,7 @@ scanl f b l = unfold g (Tuple b l)
where
g (Tuple b' (ListT l')) = h <$> l'
where
h (Yield a s) = let b'' = f b a in Just $ Tuple (Tuple b'' (force s)) b''
h (Yield a s) = let b'' = f b' a in Just $ Tuple (Tuple b'' (force s)) b'
h (Skip s) = Just $ Tuple (Tuple b' (force s)) b'
h Done = Nothing

Expand All @@ -237,6 +238,8 @@ zipWith f = zipWith' g
where
g a b = pure $ f a b

derive instance newtypeListT :: Newtype (ListT f a) _

instance semigroupListT :: Applicative f => Semigroup (ListT f a) where
append = concat

Expand Down
Loading