packages feed

compose-trans (empty) → 0.0

raw patch · 7 files changed

+165/−0 lines, 7 filesdep +basedep +mtlsetup-changed

Dependencies added: base, mtl

Files

+ LICENSE view
@@ -0,0 +1,10 @@+Copyright (c) 2009, Mikhail Mitrofanov+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+    * 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 Mikhail Mitrofanov nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ compose-trans.cabal view
@@ -0,0 +1,28 @@+Author:		Miguel Mitrofanov+Build-Type:	Simple+Cabal-Version:	>=1.2.3+Category:	Monads+Description:	A version of monad transformers that (a) allows one to convince the type checker that transformer application is a monad, and (b) doesn't need lots of boilerplate to add a new transformer. It's supposed to play nicely with @Control.Monad.Trans@.+		.+		In order to make a new transformer (say, @T@) an instance of @TransM@ (@TransP@, @TransF@) class, all you have to do is:+		.+		* define @instance MonadTrans T@ &#x2014; which you've probably done already+		.+		* define @instance Monad m => Monad (T m)@ &#x2014; also something quite common (for @TransP@ and @TransF@ you'd need another instance for @MonadPlus@ and @MonadFix@, respectively).+		.+		* write @instance Trans T where transMInst = instM@ &#x2014; which is exactly one line of boilerplate. Sorry for that.+		.+		After that, you can use your new and shiny transformer in compositions, like @ReaderT Char :. T :. WriterT String@ &#x2014; and such a composition would automagically become a monad transmormer.+License:	BSD3+License-file:	LICENSE+Maintainer:	miguelimo38@yandex.ru+Name:		compose-trans+Synopsis:	Composable monad transformers+Version:	0.0++Library+  Build-Depends:	base >= 2 && <= 4, mtl+  Exposed-Modules:	Control.Monad.Trans.Category, Control.Monad.Trans.Monad, Control.Monad.Trans.MonadFix, Control.Monad.Trans.MonadPlus+  Extensions:		GeneralizedNewtypeDeriving, RankNTypes, StandaloneDeriving, TypeOperators+  GHC-Options:		-Wall -fno-warn-orphans+  Hs-Source-Dirs:	src
+ src/Control/Monad/Trans/Category.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE KindSignatures #-}+{- |+Basic definitions in the category @(* -> *)@.+-}+module Control.Monad.Trans.Category ((:->), (:$)(..), (:.)(..), Inst) where+-- | @m :-> n@ is the set of morphisms (from @m@ to @n@, naturally) in our category.+type m :-> n = forall a. m a -> n a+-- | If @m@ is an algebra over an endofunctor @t@, then it's structure morphism has the type @Inst t m@.+type Inst c m = c m :-> m+infixr 0 :$+-- | If @t@ is an endofunctor in our category, then @t :$ m@ is basically the same as @t m@.+newtype (t :$ (m :: * -> *)) a = ApplyF {runApplyF :: t m a}+infixr 9 :.+-- | If @t1@ and @t2@ are endofunctorsm then @t2 :. t1@ is their composition (which is also an endofunctor)+newtype (t2 :. t1) m a = ComposeF {runComposeF :: (t2 :$ (t1 :$ m)) a}
+ src/Control/Monad/Trans/Monad.hs view
@@ -0,0 +1,39 @@+{- |+@Monad@ transformers. There are also @MonadPlus@ and @MonadFix@ transformes, see the corresponding modules.+-}+module Control.Monad.Trans.Monad (MonadM, TransM(..), instM, return', bind') where+import Control.Monad.Cont+import Control.Monad.List+import Control.Monad.Reader+import Control.Monad.State+import Control.Monad.Trans.Category+import Control.Monad.Writer+-- | @MonadM m@ is actually a free monad generated by @m@. @MonadM@ is a monad itself (on the @(* -> *)@ category), as usually happens with free structures.+newtype MonadM m x = MonadM {bindM :: forall n. Monad n => (m :-> n) -> n x}+-- | A monad is nothing but an algebra over the @MonadM@ monad. @instM@ provides it's structure map.+instM :: Monad m => Inst MonadM m+instM mmx = bindM mmx id+-- | Sometimes we need an @instance Monad T@, while everything we've got is @Inst MonadP T@. In this case, @return'@ serves as a @return@ substitution.+return' :: Inst MonadM m -> x -> m x+return' i x = i $ MonadM {bindM = \_ -> return x}+-- | Sometimes we need an @instance Monad T@, while everything we've got is @Inst MonadP T@. In this case, @bind'@ serves as a @>>=@ substitution.+bind' :: Inst MonadM m -> m x -> (x -> m y) -> m y+bind' i mx f = i $ MonadM {bindM = \mor -> mor mx >>= mor . f}+-- | A composable monad transformer.+--+-- You shoudn't (and probably can't) use *anything* except for @'instM'@, defined in this very module, as @transMInst@.+--+-- If you define @instance TransM T where transMInst = instM@, then you would also need to define @instance Monad m => Monad (T m)@ somewhere in your code.+class MonadTrans t => TransM t where transMInst :: Monad m => Inst MonadM (t m)+instance (Monad m, TransM t) => Monad (t :$ m) where+    return x = ApplyF {runApplyF = return' transMInst x}+    tmx >>= f = ApplyF {runApplyF = runApplyF tmx >>>= (runApplyF . f)} where (>>>=) = bind' transMInst+deriving instance MonadTrans t => MonadTrans ((:$) t)+deriving instance (Monad m, TransM t1, TransM t2) => Monad ((t2 :. t1) m)+instance (TransM t1, MonadTrans t2) => MonadTrans (t2 :. t1) where lift mx = ComposeF {runComposeF = lift $ lift mx}+instance (TransM t1, TransM t2) => TransM (t2 :. t1) where transMInst = instM+instance TransM (ContT c) where transMInst = instM+instance TransM ListT where transMInst = instM+instance TransM (ReaderT r) where transMInst = instM+instance TransM (StateT s) where transMInst = instM+instance Monoid w => TransM (WriterT w) where transMInst = instM
+ src/Control/Monad/Trans/MonadFix.hs view
@@ -0,0 +1,33 @@+{- |+@MonadFix@ transformers. There are also @Monad@ and @MonadPlus@ transformes, see the corresponding modules.++Note that each @MonadFix@ transformer is also a @Monad@ transformer.+-}+module Control.Monad.Trans.MonadFix (MonadF, TransF(..), instF, mfix') where+import Control.Monad.Reader+import Control.Monad.State+import Control.Monad.Trans.Category+import Control.Monad.Trans.Monad+import Control.Monad.Writer+import Data.Monoid+-- | @MonadF m@ is actually a free @MonadFix@ generated by @m@. @MonadF@ is a monad itself (on the @(* -> *)@ category), as usually happens with free structures.+newtype MonadF m x = MonadF {bindF :: forall n. MonadFix n => (m :-> n) -> n x}+-- | A @MonadFix@ is nothing but an algebra over the @MonadF@ monad. @instF@ provides it's structure map.+instF :: MonadFix m => Inst MonadF m+instF mmx = bindF mmx id+-- | Sometimes we need an @instance MonadFix T@, while everything we've got is @InstP MonadF T@. In this case, @mfix'@ serves as a @mfix@ substitution.+mfix' :: Inst MonadF m -> (x -> m x) -> m x+mfix' i f = i $ MonadF {bindF = \mor -> mfix $ mor . f}+-- | A composable @MonadFix@ transformer.+--+-- You shoudn't (and probably can't) use *anything* except for @'instF'@, defined in this very module, as @transFInst@.+--+-- If you define @instance TransF T where transFInst = instF@, then you would also need to define @instance MonadFix m => MonadFix (T m)@ somewhere in your code.+class TransM t => TransF t where transFInst :: MonadFix m => Inst MonadF (t m)+instance (MonadFix m, TransF t) => MonadFix (t :$ m) where+    mfix f = ApplyF {runApplyF = mfix' transFInst $ runApplyF . f}+deriving instance (MonadFix m, TransF t1, TransF t2) => MonadFix ((t2 :. t1) m)+instance (TransF t1, TransF t2) => TransF (t2 :. t1) where transFInst = instF+instance TransF (ReaderT r) where transFInst = instF+instance TransF (StateT s) where transFInst = instF+instance Monoid w => TransF (WriterT w) where transFInst = instF
+ src/Control/Monad/Trans/MonadPlus.hs view
@@ -0,0 +1,38 @@+{- |+@MonadPlus@ transformers. There are also @Monad@ and @MonadFix@ transformes, see the corresponding modules.++Note that each @MonadPlus@ transformer is also a @Monad@ transformer.+-}+module Control.Monad.Trans.MonadPlus (MonadP, TransP(..), instP, mzero', mplus') where+import Control.Monad.List+import Control.Monad.Reader+import Control.Monad.State+import Control.Monad.Trans.Category+import Control.Monad.Trans.Monad+import Control.Monad.Writer+-- | @MonadP m@ is actually a free @MonadPlus@ generated by @m@. @MonadP@ is a monad itself (on the @(* -> *)@ category), as usually happens with free structures.+newtype MonadP m x = MonadP {bindP :: forall n. MonadPlus n => (m :-> n) -> n x}+-- | A @MonadPlus@ is nothing but an algebra over the @MonadP@ monad. @instP@ provides it's structure map.+instP :: MonadPlus m => Inst MonadP m+instP mmx = bindP mmx id+-- | Sometimes we need an @instance MonadPlus T@, while everything we've got is @InstP MonadP T@. In this case, @mzero'@ serves as a @mzero@ substitution.+mzero' :: Inst MonadP m -> m x+mzero' i = i $ MonadP {bindP = \_ -> mzero}+-- | Sometimes we need an @instance Monad T@, while everything we've got is @Inst MonadP T@. In this case, @mplus'@ serves as a @mplus@ substitution.+mplus' :: Inst MonadP m -> m x -> m x -> m x+mplus' i mx1 mx2 = i $ MonadP {bindP = \mor -> mor mx1 `mplus` mor mx2}+-- | A composable @MonadPlus@ transformer.+--+-- You shoudn't (and probably can't) use *anything* except for @'instP'@, defined in this very module, as @transPInst@.+--+-- If you define @instance TransP T where transPInst = instP@, then you would also need to define @instance MonadPlus m => MonadPlus (T m)@ somewhere in your code.+class TransM t => TransP t where transPInst :: MonadPlus m => Inst MonadP (t m)+instance (MonadPlus m, TransP t) => MonadPlus (t :$ m) where+    mzero = ApplyF {runApplyF = mzero' transPInst}+    tm1 `mplus` tm2 = ApplyF {runApplyF = runApplyF tm1 `mplus1` runApplyF tm2} where mplus1 = mplus' transPInst+deriving instance (MonadPlus m, TransP t1, TransP t2) => MonadPlus ((t2 :. t1) m)+instance (TransP t1, TransP t2) => TransP (t2 :. t1) where transPInst = instP+instance TransP ListT where transPInst = instP+instance TransP (ReaderT r) where transPInst = instP+instance TransP (StateT s) where transPInst = instP+instance Monoid w => TransP (WriterT w) where transPInst = instP