resource-pool-monad (empty) → 0.1.0.0
raw patch · 5 files changed
+120/−0 lines, 5 filesdep +basedep +freedep +kan-extensionssetup-changed
Dependencies added: base, free, kan-extensions, monad-control, resource-pool, transformers
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- resource-pool-monad.cabal +31/−0
- src/Control/Monad/Pool.hs +19/−0
- src/Control/Monad/Trans/Pool.hs +48/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Sam Rijs++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ resource-pool-monad.cabal view
@@ -0,0 +1,31 @@+-- Initial resource-pool-monad.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: resource-pool-monad+version: 0.1.0.0+synopsis: A monadic interface for resource-pool+-- description: +homepage: https://github.com/srijs/haskell-resource-pool-monad+license: MIT+license-file: LICENSE+author: Sam Rijs+maintainer: srijs@airpost.net+-- copyright: +category: Control+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Control.Monad.Pool+ Control.Monad.Trans.Pool+ -- other-modules: + -- other-extensions: + build-depends: base >=4.8 && <5.0,+ free >=4.12,+ kan-extensions >=4.2,+ monad-control >=1.0,+ resource-pool >=0.2,+ transformers >=0.4+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Control/Monad/Pool.hs view
@@ -0,0 +1,19 @@+module Control.Monad.Pool (WithResource, withResource, tryWithResource, runPooled, runDedicated) where++import qualified Control.Monad.Trans.Pool as T++import Data.Pool (Pool)++newtype WithResource r a = WithResource (T.WithResource r a)++runPooled :: WithResource r a -> Pool r -> IO a+runPooled (WithResource m) = T.runPooled m++runDedicated :: WithResource r a -> r -> IO a+runDedicated (WithResource m) = T.runDedicated m++withResource :: (r -> IO a) -> WithResource r a+withResource = WithResource . T.withResource++tryWithResource :: (r -> IO a) -> WithResource r (Maybe a)+tryWithResource = WithResource . T.tryWithResource
+ src/Control/Monad/Trans/Pool.hs view
@@ -0,0 +1,48 @@+{-# LANGUAGE GADTs #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Control.Monad.Trans.Pool (WithResourceT, WithResource, withResource, tryWithResource, runPooled, runDedicated) where++import Control.Arrow (Kleisli(..))+import Control.Monad.Trans.Class (MonadTrans(lift))+import Control.Monad.Trans.Control (MonadBaseControl)+import Control.Monad.Trans.Free.Church (FT, toFT, iterT, liftF)++import Data.Functor.Coyoneda (Coyoneda(..), liftCoyoneda)+import Data.Functor.Kan.Lan (Lan(..), glan)+import Data.Pool (Pool)+import qualified Data.Pool as Pool (tryWithResource, withResource)++data WithResourceF r m a = WithResource (Coyoneda (Kleisli m r) a)+ | TryWithResource (Lan Maybe (Kleisli m r) a)+ deriving Functor++type WithResource r a = WithResourceT r IO a++newtype WithResourceT r m a = WithResourceT (FT (WithResourceF r m) m a)+ deriving (Functor, Applicative, Monad)++instance MonadTrans (WithResourceT r) where+ lift = WithResourceT . lift++runPooledF :: MonadBaseControl IO m => Pool r -> WithResourceF r m (m a) -> m a+runPooledF pool (WithResource (Coyoneda next k)) = Pool.withResource pool (runKleisli k) >>= next+runPooledF pool (TryWithResource (Lan next k)) = Pool.tryWithResource pool (runKleisli k) >>= next++runPooled :: MonadBaseControl IO m => WithResourceT r m a -> Pool r -> m a+runPooled (WithResourceT m) pool = iterT (runPooledF pool) m++runDedicatedF :: Monad m => r -> WithResourceF r m (m a) -> m a+runDedicatedF r (WithResource (Coyoneda next k)) = runKleisli k r >>= next+runDedicatedF r (TryWithResource (Lan next k)) = runKleisli k r >>= next . Just++runDedicated :: Monad m => WithResourceT r m a -> r -> m a+runDedicated (WithResourceT m) r = iterT (runDedicatedF r) m++withResource :: Monad m => (r -> m a) -> WithResourceT r m a+withResource = WithResourceT . liftF . WithResource . liftCoyoneda . Kleisli++tryWithResource :: Monad m => (r -> m a) -> WithResourceT r m (Maybe a)+tryWithResource = WithResourceT . liftF . TryWithResource . glan . Kleisli