transformers-bifunctors (empty) → 0.1
raw patch · 6 files changed
+217/−0 lines, 6 filesdep +basedep +mmorphdep +transformerssetup-changed
Dependencies added: base, mmorph, transformers
Files
- CHANGELOG.md +3/−0
- LICENSE +30/−0
- README.md +9/−0
- Setup.hs +2/−0
- src/Control/Monad/Trans/Bifunctor.hs +128/−0
- transformers-bifunctors.cabal +45/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+## Version 0.1 (2017-07-16)++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017, Jacob Stanley++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 Jacob Stanley 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.
+ README.md view
@@ -0,0 +1,9 @@+transformers-bifunctors [![Hackage][hackage-shield]][hackage]+=======================++Bifunctors over monad transformers.++[hackage]:+ http://hackage.haskell.org/package/transformers-bifunctors+[hackage-shield]:+ https://img.shields.io/hackage/v/transformers-bifunctors.svg
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Monad/Trans/Bifunctor.hs view
@@ -0,0 +1,128 @@+{-# LANGUAGE LambdaCase #-}+module Control.Monad.Trans.Bifunctor (+ BifunctorTrans(..)+ , bimapX+ , firstX+ , secondX+ ) where++import Control.Monad.Morph (MMonad, squash, hoist)+import Control.Monad.Trans.Except (ExceptT(..), runExceptT)+import qualified Control.Monad.Trans.Writer.Lazy as Lazy+import qualified Control.Monad.Trans.Writer.Strict as Strict+++-- | Class of monad transformers which are bifunctors.+--+-- You can implement a 'BifunctorTrans' by either defining 'bimapT' or by+-- defining both 'firstT' and 'secondT'.+--+-- If you supply 'bimapT', you should ensure that:+--+-- @'bimapT' 'id' 'id' ≡ 'id'@+--+-- If you supply 'first' and 'second', ensure:+--+-- @+-- 'firstT' 'id' ≡ 'id'+-- 'secondT' 'id' ≡ 'id'+-- @+--+-- If you supply both, you should also ensure:+--+-- @'bimapT' f g ≡ 'firstT' f '.' 'secondT' g@+--+-- These ensure by parametricity:+--+-- @+-- 'bimapT' (f '.' g) (h '.' i) ≡ 'bimapT' f h '.' 'bimapT' g i+-- 'firstT' (f '.' g) ≡ 'firstT' f '.' 'firstT' g+-- 'secondT' (f '.' g) ≡ 'secondT' f '.' 'secondT' g+-- @+--+class BifunctorTrans t where+ {-# MINIMAL bimapT | firstT, secondT #-}++ -- | Map over both arguments at the same time.+ --+ -- @'bimap' f g ≡ 'first' f '.' 'second' g@+ --+ bimapT :: Functor f => (x -> y) -> (a -> b) -> t x f a -> t y f b+ bimapT f g =+ firstT f . secondT g+ {-# INLINE bimapT #-}++ -- | Map covariantly over the first argument.+ --+ -- @'firstT' f ≡ 'bimapT' f 'id'@+ --+ firstT :: Functor f => (x -> y) -> t x f a -> t y f a+ firstT f =+ bimapT f id+ {-# INLINE firstT #-}++ -- | Map covariantly over the second argument.+ --+ -- @'second' ≡ 'bimap' 'id'@+ --+ secondT :: Functor f => (a -> b) -> t x f a -> t x f b+ secondT =+ bimapT id+ {-# INLINE secondT #-}++instance BifunctorTrans ExceptT where+ bimapT f g =+ let+ h = \case+ Left x ->+ Left (f x)+ Right a ->+ Right (g a)+ {-# INLINE h #-}+ in+ ExceptT . fmap h . runExceptT+ {-# INLINE bimapT #-}++instance BifunctorTrans Lazy.WriterT where+ bimapT f g =+ let+ h (a, x) =+ (g a, f x)+ {-# INLINE h #-}+ in+ Lazy.WriterT . fmap h . Lazy.runWriterT+ {-# INLINE bimapT #-}++instance BifunctorTrans Strict.WriterT where+ bimapT f g =+ let+ h (a, x) =+ (g a, f x)+ {-# INLINE h #-}+ in+ Strict.WriterT . fmap h . Strict.runWriterT+ {-# INLINE bimapT #-}++-- | Map over two stacked transformer bifunctors, unifying their type and+-- squashing the result.+--+bimapX :: (BifunctorTrans t, MMonad (t z), Monad (t y m), Monad m) => (x -> z) -> (y -> z) -> t x (t y m) a -> t z m a+bimapX f g =+ squash . hoist (firstT g) . firstT f+{-# INLINE bimapX #-}++-- | Map over the first of two stacked transformer bifunctors, unifying their+-- type and squashing the result.+--+firstX :: (BifunctorTrans t, MMonad (t y), Functor (t y m), Monad m) => (x -> y) -> t x (t y m) a -> t y m a+firstX f =+ squash . firstT f+{-# INLINE firstX #-}++-- | Map over the second of two stacked transformer bifunctors, unifying their+-- type and squashing the result.+--+secondX :: (BifunctorTrans t, MMonad (t x), Monad (t y m), Monad m) => (y -> x) -> t x (t y m) a -> t x m a+secondX f =+ squash . hoist (firstT f)+{-# INLINE secondX #-}
+ transformers-bifunctors.cabal view
@@ -0,0 +1,45 @@+version: 0.1++name:+ transformers-bifunctors+author:+ Jacob Stanley+maintainer:+ Jacob Stanley <jacob@stanley.io>+homepage:+ https://github.com/jystic/transformers-bifunctors+bug-reports:+ https://github.com/jystic/transformers-bifunctors/issues+synopsis:+ Bifunctors over monad transformers.+description:+ Bifunctors over monad transformers.+category:+ Control, Functors+license:+ BSD3+license-file:+ LICENSE+cabal-version:+ >= 1.8+build-type:+ Simple+extra-source-files:+ README.md+ CHANGELOG.md++source-repository head+ type: git+ location: git://github.com/jystic/transformers-bifunctors.git++library+ build-depends:+ base >= 3 && < 5+ , mmorph >= 1.0 && < 1.2+ , transformers >= 0.3 && < 0.6++ hs-source-dirs:+ src++ exposed-modules: + Control.Monad.Trans.Bifunctor