contstuff 0.3.1 → 0.4.0
raw patch · 2 files changed
+74/−3 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Control.ContStuff: instance (Alternative m, Monad m) => Alternative (ContT r m)
+ Control.ContStuff: choice :: [a] -> ChoiceT r i m a
+ Control.ContStuff: instance (Alternative m) => Alternative (ContT r m)
+ Control.ContStuff: instance (Alternative m) => MonadPlus (ContT r m)
+ Control.ContStuff: instance (Alternative m) => MonadPlus (EitherT r e m)
+ Control.ContStuff: instance (Alternative m) => MonadPlus (StateT r s m)
+ Control.ContStuff: instance (Alternative m, Monad m) => MonadPlus (IdT m)
+ Control.ContStuff: instance MonadPlus (ChoiceT r i m)
+ Control.ContStuff: listChoice :: Choice [a] [a] a -> [a]
+ Control.ContStuff: maybeChoice :: Choice (Maybe a) (Maybe a) a -> Maybe a
+ Control.ContStuff: maybeChoiceT :: (Applicative m) => ChoiceT (Maybe a) (Maybe a) m a -> m (Maybe a)
+ Control.ContStuff: type Choice r i a = ChoiceT r i Id a
Files
- Control/ContStuff.hs +72/−2
- contstuff.cabal +2/−1
Control/ContStuff.hs view
@@ -20,7 +20,8 @@ -- ** ContT ContT(..), runContT, evalContT, modifyContT, -- ** Choice/nondeterminism- ChoiceT(..), runChoiceT, findFirst, findAll, listChoiceT, listA,+ ChoiceT(..), runChoiceT, choice, findFirst, findAll,+ listChoiceT, listA, maybeChoiceT, -- ** Exceptions EitherT(..), runEitherT, evalEitherT, modifyEitherT, -- ** State@@ -32,6 +33,8 @@ -- * Monads -- ** Identity monad Id(..),+ -- ** Choice+ Choice, listChoice, maybeChoice, -- ** Cont Cont, runCont, evalCont, modifyCont, -- ** State@@ -141,6 +144,10 @@ ChoiceT $ \fold z k -> c (\x y kc -> getChoiceT (f y) fold x kc) z k +instance MonadPlus (ChoiceT r i m) where+ mzero = empty+ mplus = (<|>)+ instance Transformer (ChoiceT r i) where lift c = ChoiceT $ \fold z k -> c >>= \x -> fold z x k @@ -156,6 +163,15 @@ runChoiceT fold z k (ChoiceT c) = c fold z k +-- | Turn a list into a 'ChoiceT' computation efficiently.++choice :: [a] -> ChoiceT r i m a+choice xs = ChoiceT (choice' xs)+ where+ choice' [] = \_ z k -> k z+ choice' (x:xs) = \fold z k -> fold z x (\y -> choice' xs fold y k)++ -- | Find the first solution. findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)@@ -181,6 +197,29 @@ listA = foldr (<|>) empty . map pure +-- | Get one solution (faster than 'findFirst').++maybeChoiceT :: Applicative m => ChoiceT (Maybe a) (Maybe a) m a -> m (Maybe a)+maybeChoiceT = runChoiceT (\_ y _ -> pure (Just y)) Nothing pure+++-- | The choice monad. Derived from 'ChoiceT'.++type Choice r i a = ChoiceT r i Id a+++-- | Get list of solutions.++listChoice :: Choice [a] [a] a -> [a]+listChoice = getId . listChoiceT+++-- | Get one solution.++maybeChoice :: Choice (Maybe a) (Maybe a) a -> Maybe a+maybeChoice = getId . maybeChoiceT++ ----------- -- ContT -- -----------@@ -195,7 +234,7 @@ type Result (ContT r m) = r abort = ContT . const . pure -instance (Alternative m, Monad m) => Alternative (ContT r m) where+instance Alternative m => Alternative (ContT r m) where empty = ContT $ const empty ContT c <|> ContT d = ContT $ \k -> c k <|> d k @@ -215,6 +254,10 @@ ContT c >>= f = ContT $ \k -> c (\x -> getContT (f x) k) +instance Alternative m => MonadPlus (ContT r m) where+ mzero = empty+ mplus = (<|>)+ instance Runnable (ContT r) r m a where type Argument (ContT r) r m a = a -> m r runT k (ContT c) = c k@@ -314,6 +357,10 @@ EitherT $ \k expk -> c (\x -> getEitherT (f x) k expk) expk +instance Alternative m => MonadPlus (EitherT r e m) where+ mzero = empty+ mplus = (<|>)+ instance Runnable (EitherT r e) r m a where type Argument (EitherT r e) r m a = (a -> m r, e -> m r) runT (k, expk) (EitherT c) = c k expk@@ -372,6 +419,10 @@ return = IdT . return IdT c >>= f = IdT $ c >>= getIdT . f +instance (Alternative m, Monad m) => MonadPlus (IdT m) where+ mzero = empty+ mplus = (<|>)+ instance MonadFix m => MonadFix (IdT m) where mfix f = IdT $ mfix (getIdT . f) @@ -470,6 +521,10 @@ StateT c >>= f = StateT $ \s0 k -> c s0 (\s1 x -> getStateT (f x) s1 k) +instance Alternative m => MonadPlus (StateT r s m) where+ mzero = empty+ mplus = (<|>)+ instance Runnable (StateT r s) r m a where type Argument (StateT r s) r m a = (s, s -> a -> m r) runT (s0, k) (StateT c) = c s0 k@@ -554,7 +609,10 @@ -- | Monads supporting abortion. class Abortable m where+ -- | End result of the computation. type Result m++ -- | Ignore current continuation and abort. abort :: Result m -> m a @@ -664,7 +722,10 @@ -- | Monads, which support lifting base monad computations. class LiftBase m a where+ -- | Base monad of @m@. type Base m a++ -- | Promote a base monad computation. base :: Base m a -> m a instance LiftBase IO a where type Base IO a = IO a; base = id@@ -696,19 +757,28 @@ -- @m a@ can be an instance of this class. class Runnable t r m a where+ -- | Arguments needed to run. type Argument t r m a++ -- | Run the transformer. runT :: Argument t r m a -> t m a -> m r -- | Stateful monads.+--+-- Minimal complete definition: 'StateOf', 'get' and 'putLazy'. class Stateful m where+ -- | State type of @m@. type StateOf m+ -- | Get the current state. get :: m (StateOf m)+ -- | Set the current state and force it. put :: StateOf m -> m () put x = x `seq` putLazy x+ -- | Set the current state, but don't force it. putLazy :: StateOf m -> m ()
contstuff.cabal view
@@ -1,10 +1,11 @@ Name: contstuff-Version: 0.3.1+Version: 0.4.0 Category: Control, Monads Synopsis: Easy to use CPS-based monads Maintainer: Ertugrul Söylemez <es@ertes.de> Author: Ertugrul Söylemez <es@ertes.de> Copyright: (c) 2010 Ertugrul Söylemez+Homepage: http://haskell.org/haskellwiki/Contstuff License: BSD3 License-file: LICENSE Build-type: Simple