packages feed

monad-control-identity (empty) → 0.1.0.0

raw patch · 5 files changed

+185/−0 lines, 5 filesdep +basedep +monad-controldep +transformers

Dependencies added: base, monad-control, transformers, transformers-base

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for monad-control-identity++## 0.1.0.0 -- 2020-07-17++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2020, Felix Springer++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 Felix Springer 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.
+ monad-control-identity.cabal view
@@ -0,0 +1,25 @@+name:                monad-control-identity+version:             0.1.0.0+synopsis:            Stronger classes than monad-control+-- description:+license:             BSD3+license-file:        LICENSE+author:              Felix Springer+maintainer:          felixspringer149@gmail.com+homepage:            https://github.com/jumper149/monad-control-identity+bug-reports:         https://github.com/jumper149/monad-control-identity/issues+category:            Control+build-type:          Simple+extra-source-files:  CHANGELOG.md+cabal-version:       >=1.10++library+  exposed-modules:     Control.Monad.Trans.Control.Functor+                       Control.Monad.Trans.Control.Identity+  build-depends:       base              >= 4.14    && < 4.15+                     , monad-control     >= 1.0.2.3 && < 1.1+                     , transformers      >= 0.5.6.2 && < 0.6+                     , transformers-base >= 0.4.5.2 && < 0.5+  hs-source-dirs:      src+  default-language:    Haskell2010+  ghc-options:         -Wall
+ src/Control/Monad/Trans/Control/Functor.hs view
@@ -0,0 +1,28 @@+module Control.Monad.Trans.Control.Functor (+  MonadTransFunctor (..)+, hoistTrans+) where++import Control.Monad.Base+import Control.Monad.Trans.Control+import Control.Monad.Trans.Control.Identity+import Control.Monad.Trans.Identity+import Control.Monad.Trans.Reader++{- | This type class is generalization of functions like 'mapReaderT'+   and 'mapIdentityT'.+-}+class MonadTransControlIdentity t => MonadTransFunctor t where+  liftMap :: (m a -> n b) -> t m a -> t n b++instance MonadTransFunctor IdentityT where+  liftMap f = IdentityT . f . runIdentityT++instance MonadTransFunctor (ReaderT r) where+  liftMap f m = ReaderT $ f . runReaderT m++hoistTrans :: (MonadBaseControl b m, MonadBaseControl b (t m), MonadTransFunctor t)+           => t b a+           -> t m a+hoistTrans a = (=<<) restoreM $ liftBaseWith $ \ runInBase ->+                 runInBase $ liftMap liftBase $ a
+ src/Control/Monad/Trans/Control/Identity.hs view
@@ -0,0 +1,97 @@+{-# LANGUAGE FlexibleInstances, FunctionalDependencies, TypeFamilies, Rank2Types, UndecidableInstances #-}++module Control.Monad.Trans.Control.Identity (++-- * MonadTransControlIdentity+  MonadTransControlIdentity (..)+{- | 'MonadTransControlIdentity' instances can easily be created for+  monad transformers, because of the superclass 'MonadTransControl':++@+newtype ExampleT = ...+  deriving ('Monad', 'MonadTrans')++instance 'MonadTransControl' ExampleT where+  ...++instance 'MonadTransControlIdentity' ExampleT where+  'liftWithIdentity' = 'defaultLiftWithIdentity'+@+-}+, defaultLiftWithIdentity++-- * MonadBaseControlIdentity+-- | Regarding the 'IO' base monad this can be seen as an alternative+-- way to implement 'MonadUnliftIO'.+, MonadBaseControlIdentity (..)+{- | Just like 'MonadTransControlIdentity', 'MonadBaseControl' instances+   can easily be created for monad transformers:++@+instance 'MonadTransControlIdentity' ExampleT where+  'liftBaseWithIdentity' = 'defaultLiftBaseWithIdentity'+@+  -}+, defaultLiftBaseWithIdentity++) where++import Control.Monad.Trans.Control+import Control.Monad.Trans.Identity+import Control.Monad.Trans.Reader++{- | The 'MonadTransControlIdentity' type class is a stronger version of+  'MonadTransControl':++  'MonadTransControl' instances are aware of the monadic state of the+  transformer and allow to save and restore this state.+  'MonadTransControlIdentity' instances on the other hand exist only for+  exactly those transformers, that don't have any monadic state.++  So for any instance of this class this should hold:++  @forall a. 'StT' t a ~ a@++  This can't be given as a constraint to the class due to limitations+  regarding the @TypeFamilies@ extension.+-}+class MonadTransControl t => MonadTransControlIdentity t where+  liftWithIdentity :: Monad m => ((forall x. t m x -> m x) -> m a) -> t m a++defaultLiftWithIdentity :: (Monad m, MonadTransControl t)+                        => ((forall x. StT t x ~ x => t m x -> m x) -> m a)+                        -> t m a+defaultLiftWithIdentity = liftWith++instance MonadTransControlIdentity IdentityT where+  liftWithIdentity = defaultLiftWithIdentity++instance MonadTransControlIdentity (ReaderT r) where+  liftWithIdentity = defaultLiftWithIdentity++{- | The 'MonadBaseControlIdentity' type class is a stronger version of+  'MonadBaseControl'.++  Just like 'MonadTransControlIdentity' instances of+  'MonadBaseControlIdentity' hold no monadic state:++  @forall a. 'StM' m a ~ a@+-}+class MonadBaseControl b m => MonadBaseControlIdentity b m | m -> b where+  liftBaseWithIdentity :: ((forall x. m x -> b x) -> b a) -> m a++defaultLiftBaseWithIdentity :: (MonadBaseControlIdentity b m, MonadTransControlIdentity t)+                            => ((forall x. t m x -> b x) -> b a)+                            -> t m a+defaultLiftBaseWithIdentity inner = liftWithIdentity $ \ runId ->+  liftBaseWithIdentity $ \ runIdInBase ->+    inner $ runIdInBase . runId++instance MonadBaseControl b b => MonadBaseControlIdentity b b where+  liftBaseWithIdentity inner = inner id++instance MonadBaseControlIdentity b m => MonadBaseControlIdentity b (IdentityT m) where+  liftBaseWithIdentity = defaultLiftBaseWithIdentity++instance MonadBaseControlIdentity b m => MonadBaseControlIdentity b (ReaderT r m) where+  liftBaseWithIdentity = defaultLiftBaseWithIdentity