monadLib-compose (empty) → 0.1
raw patch · 5 files changed
+194/−0 lines, 5 filesdep +basedep +monadLibsetup-changed
Dependencies added: base, monadLib
Files
- LICENSE +30/−0
- MonadLib/Compose.hs +101/−0
- README +1/−0
- Setup.hs +2/−0
- monadLib-compose.cabal +60/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2010, Aristid Breitkreuz++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 Aristid Breitkreuz nor the names of other+ 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+OWNER 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.
+ MonadLib/Compose.hs view
@@ -0,0 +1,101 @@+{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}++{- |+Module : MonadLib.Compose+Copyright : 2010 Aristid Breitkreuz+License : BSD3+Stability : experimental+Portability : portable++This module provides Arrow-like monad composition for monadLib. To be more precise, it is "Category-like",+i.e. the parallels are to 'Control.Category.Category'.++'Control.Category.Category' generalises '.' and 'id' to arrows and categories. One such arrow is 'Kleisli',+which represents functions returning monadic values. Incidentally, that's equivalent to 'ReaderT'! So it+turns out that it is possible to generalise '.' and 'id' to 'ReaderT' ('id' is just 'ask'), as well as to+many monad transformer stacks that embed a 'ReaderT' inside.++The motivation to create this module was a nagging feeling when reading the documentation for @hxt@ and @HaXml@:+composing filters is very nice, but the abundance of constant arrows, and the lack of access to the very extensive+set of monad combinators, leads to duplicated effort and unwieldy code (in my humble opinion). I think it is+possible to gain similar functionality with a stack of monad transformers including 'ReaderT', and 'ComposeM',+presented here.+-}++module MonadLib.Compose+(+ mid+, ComposeM(..)+, (<<<)+, (>>>)+)+where+ +import Data.Monoid+import MonadLib+import MonadLib.Derive+import MonadLib.Monads++-- | Alias for 'ask'. Compare with 'Control.Category.id'.+mid :: ReaderM m s => m s+mid = ask++-- | Composable monads. Compare with 'Control.Category.Category'.+-- Note that there are two different monad types involved in each instance.+class (Monad m, Monad n) => ComposeM m n s t | m -> s, n -> t, n s -> m where+ -- | Compose two monadic values from right to left. @mcompose f g@ is + -- comparable to @f . g@ but for monadic values. Compare with 'Control.Category..'.+ mcompose :: m a -> n s -> n a++-- | Compose two monadic values from right to left. Compare with 'Control.Category.<<<'.+-- @f <<< g@ is equivalent to @mcompose f g@.+(<<<) :: ComposeM m n s t => m a -> n s -> n a+(<<<) = mcompose+infixr 1 <<<++-- | Compose two monadic values from left to right. Compare with 'Control.Category.>>>'.+-- @g >>> f@ is equivalent to @mcompose f g@.+(>>>) :: ComposeM m n s t => n s -> m a -> n a+(>>>) = flip mcompose+infixl 1 >>>++instance ComposeM ((->) s) ((->) t) s t where+ mcompose = (.)++instance Monad m => ComposeM (ReaderT s m) (ReaderT t m) s t where+ mcompose m n = do+ s <- n+ lift (runReaderT s m)++instance ComposeM (Reader s) (Reader t) s t where+ mcompose m n = asks $ flip runReader m . flip runReader n++derive_mcompose close open m n = do+ s <- n+ u <- lift $ mcompose (open m) (return s)+ close u++instance ComposeM m n s t => ComposeM (IdT m) (IdT n) s t where+ mcompose = derive_mcompose return runIdT++instance (ComposeM m n s t, Monoid w) => ComposeM (WriterT w m) (WriterT w n) s t where+ mcompose = derive_mcompose puts runWriterT++instance ComposeM m n s t => ComposeM (ExceptionT e m) (ExceptionT e n) s t where+ mcompose = derive_mcompose raises runExceptionT++instance ComposeM m n s t => ComposeM (StateT i m) (StateT i n) s t where+ mcompose m n = do+ s <- n+ i <- get+ (u, i') <- lift $ mcompose (runStateT i m) (return s)+ set i'+ return u++instance ComposeM m n s t => ComposeM (ChoiceT m) (ChoiceT n) s t where+ mcompose m n = do+ s <- n+ u <- lift $ mcompose (runChoiceT m) (return s)+ case u of+ Nothing -> mzero+ Just (a, m') -> return a `mplus` (mcompose m' (return s))
+ README view
@@ -0,0 +1,1 @@+Arrow-like monad composition for monadLib.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ monadLib-compose.cabal view
@@ -0,0 +1,60 @@+-- monadLib-compose.cabal auto-generated by cabal init. For additional+-- options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: monadLib-compose++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.1++-- A short (one-line) description of the package.+Synopsis: Arrow-like monad composition for monadLib.++-- A longer description of the package.+Description: Arrow-like monad composition for monadLib.++-- The license under which the package is released.+License: BSD3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Aristid Breitkreuz++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: aristidb@googlemail.com++-- A copyright notice.+Copyright: Copyright (C) 2010 Aristid Breitkreuz++Category: Monads, Control++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+Extra-source-files: README++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.8+++Library+ -- Modules exported by the library.+ Exposed-modules: MonadLib.Compose+ + -- Packages needed in order to build this package.+ Build-depends:+ base >=4 && <5,+ monadLib ==3.6.*+ + -- Modules not exported by this package.+ -- Other-modules: + + -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools: +