mmorph 1.0.3 → 1.2.2
raw patch · 5 files changed
Files
- CHANGELOG.md +50/−0
- LICENSE +2/−2
- mmorph.cabal +16/−9
- src/Control/Monad/Morph.hs +45/−41
- src/Control/Monad/Trans/Compose.hs +56/−39
+ CHANGELOG.md view
@@ -0,0 +1,50 @@+1.2.1++- `MFunctor` and `MMonad` instances for `AccumT`++1.2.0++* BREAKING CHANGE: Remove instances for `ErrorT` and `ListT`+ * These types are deprecated++1.1.5++* Fix build failures on GHC 8.4 and 8.6+* Add support for safe Haskell+ * Specifically, this marks the `Control.Monad.Trans.Compose` module as+ `Trustworthy`+ * The change in 1.1.4 to use `GeneralizedNewtypeDeriving` meant that the+ `Control.Monad.Trans.Compose` module was no longer inferred as safe+* Restore `Traversable` instance removed by mistake in 1.1.4++1.1.4 (Blacklisted)++* Unintentional removal of `Traversable` instance for `ComposeT`+ * This missing instance is restored in 1.1.5+ * This is the reason why the 1.1.4 release is blacklisted+* Fix `MonadFail`-related code to work for GHCJS+* The `MonadRWS` instance for `ComposeT` has a more flexible constraint+ * The constraint is now+ `MonadReader r (f (g m)), MonadWriter w (f (g m)), MonadState s (f (g m))`+ instead of `MonadRWS r w s (f g m)`+ * This loosening of the constraint is backwards-compatible++1.1.3++* Add an `MFunctor` instance for `ComposeT` for GHC >= 8.6+* Add GHC 8.8 support++1.1.2++* Conditionally disable `Polykinds` to support older versions of GHC++1.1.1++* Increase upper bound on `transformers-compat`++1.1.0++* BREAKING CHANGE: Enable `PolyKinds`+ * This should in theory be a non-breaking change, but due to a bug in+ GHC 8.0.1 and kind inference ambiguities I'm marking this as a breaking+ change
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2013, Gabriel Gonzalez+Copyright (c) 2013, Gabriella Gonzalez All rights reserved. Redistribution and use in source and binary forms, with or without modification,@@ -9,7 +9,7 @@ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.-* Neither the name of Gabriel Gonzalez nor the names of other contributors may+* Neither the name of Gabriella Gonzalez nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
mmorph.cabal view
@@ -1,25 +1,32 @@ Name: mmorph-Version: 1.0.3-Cabal-Version: >= 1.8.0.2+Version: 1.2.2+Cabal-Version: >= 1.10 Build-Type: Simple License: BSD3 License-File: LICENSE-Copyright: 2013 Gabriel Gonzalez-Author: Gabriel Gonzalez-Maintainer: Gabriel439@gmail.com-Bug-Reports: https://github.com/Gabriel439/Haskell-MMorph-Library/issues+Copyright: 2013 Gabriella Gonzalez+Author: Gabriella Gonzalez+Maintainer: GenuineGabriella@gmail.com+Bug-Reports: https://github.com/Gabriella439/Haskell-MMorph-Library/issues Synopsis: Monad morphisms Description: This library provides monad morphism utilities, most commonly used for manipulating monad transformer stacks. Category: Control+Extra-Source-Files: CHANGELOG.md Source-Repository head Type: git- Location: https://github.com/Gabriel439/Haskell-MMorph-Library+ Location: https://github.com/Gabriella439/Haskell-MMorph-Library Library Hs-Source-Dirs: src Build-Depends:- base >= 4 && < 5 ,- transformers >= 0.2.0.0 && < 0.5+ base >= 4.5 && < 5 ,+ mtl >= 2.1 && < 2.4,+ transformers >= 0.2.0.0 && < 0.7,+ transformers-compat >= 0.6.1 && < 0.8+ if impl(ghc < 8.0)+ Build-Depends:+ fail == 4.9.* Exposed-Modules: Control.Monad.Morph, Control.Monad.Trans.Compose GHC-Options: -O2+ Default-Language: Haskell2010
src/Control/Monad/Morph.hs view
@@ -1,4 +1,9 @@-{-# LANGUAGE CPP, RankNTypes #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE Safe #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE PolyKinds #-}+#endif {-| A monad morphism is a natural transformation: @@ -8,13 +13,13 @@ > morph $ do x <- m = do x <- morph m > f x morph (f x)-> +> > morph (return x) = return x ... which are equivalent to the following two functor laws: > morph . (f >=> g) = morph . f >=> morph . g-> +> > morph . return = return Examples of monad morphisms include:@@ -73,14 +78,14 @@ ) where import Control.Monad.Trans.Class (MonadTrans(lift))-import qualified Control.Monad.Trans.Error as E+import qualified Control.Monad.Trans.Accum as A+import qualified Control.Monad.Trans.Except as Ex import qualified Control.Monad.Trans.Identity as I-import qualified Control.Monad.Trans.List as L import qualified Control.Monad.Trans.Maybe as M import qualified Control.Monad.Trans.Reader as R import qualified Control.Monad.Trans.RWS.Lazy as RWS import qualified Control.Monad.Trans.RWS.Strict as RWS'-import qualified Control.Monad.Trans.State.Lazy as S +import qualified Control.Monad.Trans.State.Lazy as S import qualified Control.Monad.Trans.State.Strict as S' import qualified Control.Monad.Trans.Writer.Lazy as W' import qualified Control.Monad.Trans.Writer.Strict as W@@ -88,10 +93,8 @@ import Data.Functor.Compose (Compose (Compose)) import Data.Functor.Identity (runIdentity) import Data.Functor.Product (Product (Pair))-#if MIN_VERSION_transformers(0,3,0) import Control.Applicative.Backwards (Backwards (Backwards)) import Control.Applicative.Lift (Lift (Pure, Other))-#endif -- For documentation import Control.Exception (try, IOException)@@ -101,24 +104,27 @@ {-| A functor in the category of monads, using 'hoist' as the analog of 'fmap': > hoist (f . g) = hoist f . hoist g-> +> > hoist id = id -} class MFunctor t where {-| Lift a monad morphism from @m@ to @n@ into a monad morphism from @(t m)@ to @(t n)@++ The first argument to `hoist` must be a monad morphism, even though the+ type system does not enforce this -} hoist :: (Monad m) => (forall a . m a -> n a) -> t m b -> t n b -instance MFunctor (E.ErrorT e) where- hoist nat m = E.ErrorT (nat (E.runErrorT m))+instance MFunctor (A.AccumT w) where+ hoist nat m = A.AccumT (nat . A.runAccumT m) +instance MFunctor (Ex.ExceptT e) where+ hoist nat m = Ex.ExceptT (nat (Ex.runExceptT m))+ instance MFunctor I.IdentityT where hoist nat m = I.IdentityT (nat (I.runIdentityT m)) -instance MFunctor L.ListT where- hoist nat m = L.ListT (nat (L.runListT m))- instance MFunctor M.MaybeT where hoist nat m = M.MaybeT (nat (M.runMaybeT m)) @@ -149,14 +155,12 @@ instance MFunctor (Product f) where hoist nat (Pair f g) = Pair f (nat g) -#if MIN_VERSION_transformers(0,3,0) instance MFunctor Backwards where hoist nat (Backwards f) = Backwards (nat f) instance MFunctor Lift where hoist _ (Pure a) = Pure a hoist nat (Other f) = Other (nat f)-#endif -- | A function that @generalize@s the 'Identity' base monad to be any monad. generalize :: Monad m => Identity a -> m a@@ -167,9 +171,9 @@ analog of 'return' and 'embed' as the analog of ('=<<'): > embed lift = id-> +> > embed f (lift m) = f m-> +> > embed g (embed f t) = embed (\m -> embed g (f m)) t -} class (MFunctor t, MonadTrans t) => MMonad t where@@ -230,9 +234,14 @@ t |>= f = embed f t {-# INLINABLE (|>=) #-} -instance (E.Error e) => MMonad (E.ErrorT e) where- embed f m = E.ErrorT (do - x <- E.runErrorT (f (E.runErrorT m))+instance Monoid w => MMonad (A.AccumT w) where+ embed f m = A.AccumT $ \w -> do+ ((b, wInner), wOuter) <- A.runAccumT (f $ A.runAccumT m w) w+ return (b, wInner `mappend` wOuter)++instance MMonad (Ex.ExceptT e) where+ embed f m = Ex.ExceptT (do+ x <- Ex.runExceptT (f (Ex.runExceptT m)) return (case x of Left e -> Left e Right (Left e) -> Left e@@ -241,11 +250,6 @@ instance MMonad I.IdentityT where embed f m = f (I.runIdentityT m) -instance MMonad L.ListT where- embed f m = L.ListT (do- x <- L.runListT (f (L.runListT m))- return (concat x))- instance MMonad M.MaybeT where embed f m = M.MaybeT (do x <- M.runMaybeT (f (M.runMaybeT m))@@ -278,7 +282,7 @@ Imagine that some library provided the following 'S.State' code: > import Control.Monad.Trans.State-> +> > tick :: State Int () > tick = modify (+1) @@ -305,23 +309,23 @@ to be any monad: > import Data.Functor.Identity-> +> > generalize :: (Monad m) => Identity a -> m a > generalize m = return (runIdentity m) ... which we can 'hoist' to change @tick@'s base monad: > hoist :: (Monad m, MFunctor t) => (forall a . m a -> n a) -> t m b -> t n b-> +> > hoist generalize :: (Monad m, MFunctor t) => t Identity b -> t m b-> +> > hoist generalize tick :: (Monad m) => StateT Int m () This lets us mix @tick@ alongside 'IO' using 'lift': > import Control.Monad.Morph > import Control.Monad.Trans.Class-> +> > tock :: StateT Int IO () > tock = do > hoist generalize tick :: (Monad m) => StateT Int m ()@@ -340,29 +344,29 @@ morphism laws: > generalize (return x)-> +> > -- Definition of 'return' for the Identity monad > = generalize (Identity x)-> +> > -- Definition of 'generalize' > = return (runIdentity (Identity x))-> +> > -- runIdentity (Identity x) = x > = return x > generalize $ do x <- m > f x-> +> > -- Definition of (>>=) for the Identity monad > = generalize (f (runIdentity m))-> +> > -- Definition of 'generalize' > = return (runIdentity (f (runIdentity m)))-> +> > -- Monad law: Left identity > = do x <- return (runIdentity m) > return (runIdentity (f x))-> +> > -- Definition of 'generalize' in reverse > = do x <- generalize m > generalize (f x)@@ -376,7 +380,7 @@ For example, we might want to combine the following @save@ function: > import Control.Monad.Trans.Writer-> +> > -- i.e. :: StateT Int (WriterT [Int] Identity) () > save :: StateT Int (Writer [Int]) () > save = do@@ -397,7 +401,7 @@ generalizing @save@'s base monad: > import Control.Monad-> +> > program :: StateT Int (WriterT [Int] IO) () > program = replicateM_ 4 $ do > hoist lift tock@@ -421,7 +425,7 @@ > import Control.Exception > import Control.Monad.Trans.Class > import Control.Monad.Trans.Error-> +> > check :: IO a -> ErrorT IOException IO a > check io = ErrorT (try io)
src/Control/Monad/Trans/Compose.hs view
@@ -1,5 +1,18 @@-{-# LANGUAGE FlexibleContexts, KindSignatures #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE StandaloneKindSignatures #-}+{-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE DeriveTraversable #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE Trustworthy #-} +#if __GLASGOW_HASKELL__ >= 806+{-# LANGUAGE QuantifiedConstraints #-}+#endif+ {-| Composition of monad transformers. A higher-order version of "Data.Functor.Compose". -}@@ -7,61 +20,65 @@ module Control.Monad.Trans.Compose ( -- * ComposeT ComposeT(ComposeT, getComposeT),+ mapComposeT ) where import Control.Applicative ( Applicative(pure, (<*>), (*>), (<*)), Alternative(empty, (<|>)) ) import Control.Monad (MonadPlus(mzero, mplus), liftM)+import Control.Monad.Cont.Class (MonadCont(callCC))+import Control.Monad.Error.Class (MonadError(throwError, catchError))+import Control.Monad.Fail (MonadFail(..)) import Control.Monad.Morph (MFunctor(hoist))+import Control.Monad.RWS.Class (MonadRWS)+import Control.Monad.Reader.Class (MonadReader(ask, local, reader))+import Control.Monad.State.Class (MonadState(get, put, state)) import Control.Monad.Trans.Class (MonadTrans(lift))+import Control.Monad.Writer.Class (MonadWriter(writer, tell, listen, pass)) import Control.Monad.IO.Class (MonadIO(liftIO)) import Data.Foldable (Foldable(fold, foldMap, foldr, foldl, foldr1, foldl1))+import Data.Kind (Type) import Data.Traversable (Traversable(traverse, sequenceA, mapM, sequence)) import Prelude hiding (foldr, foldl, foldr1, foldl1, mapM, sequence) +infixr 9 `ComposeT`+ -- | Composition of monad transformers.-newtype ComposeT (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *) m a+type ComposeT :: ((Type -> Type) -> Type -> Type) -> ((Type -> Type) -> Type -> Type) -> (Type -> Type) -> Type -> Type+newtype ComposeT f g m a = ComposeT { getComposeT :: f (g m) a }+ deriving+ ( Alternative+ , Applicative+ , Eq+ , Foldable+ , Functor+ , Ord+ , Read+ , Show+ , Traversable+ , Monad+ , MonadCont+ , MonadError e+ , MonadFail+ , MonadIO+ , MonadPlus+ , MonadReader r+ , MonadRWS r w s+ , MonadState s+ , MonadWriter w+ ) instance (MFunctor f, MonadTrans f, MonadTrans g) => MonadTrans (ComposeT f g) where lift = ComposeT . hoist lift . lift -instance Functor (f (g m)) => Functor (ComposeT f g m) where- fmap f (ComposeT m) = ComposeT (fmap f m)--instance Applicative (f (g m)) => Applicative (ComposeT f g m) where- pure a = ComposeT (pure a)- ComposeT f <*> ComposeT a = ComposeT (f <*> a)- ComposeT a *> ComposeT b = ComposeT (a *> b)- ComposeT a <* ComposeT b = ComposeT (a <* b)--instance Alternative (f (g m)) => Alternative (ComposeT f g m) where- empty = ComposeT empty- ComposeT a <|> ComposeT b = ComposeT (a <|> b)--instance Monad (f (g m)) => Monad (ComposeT f g m) where- return a = ComposeT (return a)- m >>= f = ComposeT (getComposeT m >>= \x -> getComposeT (f x))- fail e = ComposeT (fail e)--instance MonadPlus (f (g m)) => MonadPlus (ComposeT f g m) where- mzero = ComposeT mzero- ComposeT a `mplus` ComposeT b = ComposeT (a `mplus` b)--instance MonadIO (f (g m)) => MonadIO (ComposeT f g m) where- liftIO m = ComposeT (liftIO m)--instance Foldable (f (g m)) => Foldable (ComposeT f g m) where- fold (ComposeT m) = fold m- foldMap f (ComposeT m) = foldMap f m- foldr f a (ComposeT m) = foldr f a m- foldl f a (ComposeT m) = foldl f a m- foldr1 f (ComposeT m) = foldr1 f m- foldl1 f (ComposeT m) = foldl1 f m+#if defined(__MHS__) || __GLASGOW_HASKELL__ >= 806+instance (MFunctor f, MFunctor g, forall m. Monad m => Monad (g m))+ => MFunctor (ComposeT f g) where+ hoist f (ComposeT m) = ComposeT (hoist (hoist f) m)+#endif -instance Traversable (f (g m)) => Traversable (ComposeT f g m) where- traverse f (ComposeT m) = fmap ComposeT (traverse f m)- sequenceA (ComposeT m) = fmap ComposeT (sequenceA m)- mapM f (ComposeT m) = liftM ComposeT (mapM f m)- sequence (ComposeT m) = liftM ComposeT (sequence m)+-- | Transform the computation inside a 'ComposeT'.+mapComposeT :: (f (g m) a -> p (q n) b) -> ComposeT f g m a -> ComposeT p q n b+mapComposeT f = ComposeT . f . getComposeT