packages feed

monadio-unwrappable (empty) → 0.1

raw patch · 5 files changed

+113/−0 lines, 5 filesdep +basedep +monads-tfdep +transformerssetup-changed

Dependencies added: base, monads-tf, transformers

Files

+ Control/Monad/IO/MonadIOException.hs view
@@ -0,0 +1,11 @@+module Control.Monad.IO.MonadIOException where++import Control.Monad.IO.Unwrappable+import Control.Monad.IO.Class+import Control.Exception++bracketIO :: MonadIOUnwrappable m d e => IO a -> (a -> IO b) -> (a -> m c) -> m c+bracketIO init cleanup action = do+  s <- unwrapState+  r <- liftIO $ bracket init cleanup (\x -> unwrapMonadIO s (action x))+  rewrapMonadIO s r
+ Control/Monad/IO/Unwrappable.hs view
@@ -0,0 +1,55 @@+{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}+-- | Code in this module is not directly related to CellML, but it is rather convenience code used by the solver.+module Control.Monad.IO.Unwrappable (MonadIOUnwrappable, unwrapState, unwrapMonadIO, rewrapMonadIO)+where       ++import Control.Monad.Trans.Class+import Control.Monad+import Control.Monad.IO.Class+import Control.Monad.Error+import Control.Monad.Reader+import Control.Monad.Reader.Class+import Control.Monad.State+import Control.Monad.State.Class+import Data.IORef++data Single a = Single a++-- | Represents a MonadIO where any change further up the monad stack can be+-- | represented lower down in the stack.+class MonadIO m => MonadIOUnwrappable m c s | m -> c, m -> s where+  -- | Sets up state (e.g. an IORef) to be used to simulate the monad from the+  -- | IO monad.+  unwrapState :: m s+  +  -- | Maps the monad to only use IO level constructs and the state set up +  -- | using unwrapState.+  unwrapMonadIO :: s -> m a -> IO (c a)+  +  -- | Reverses a previous unwrapMonadIO operation.+  rewrapMonadIO :: s -> c a -> m a++instance MonadIOUnwrappable IO Single () where+  unwrapState = return ()+  unwrapMonadIO _ asIO = liftM Single asIO+  rewrapMonadIO _ (Single x) = return x++newtype EitherChain a b c = EitherChain (a (Either b c))+instance (Error e, MonadIO m, MonadIOUnwrappable m c s) => MonadIOUnwrappable (ErrorT e m) (EitherChain c e) s where+  unwrapState = lift (unwrapState)+  unwrapMonadIO s m = liftM EitherChain $ unwrapMonadIO s (runErrorT m)+  rewrapMonadIO s (EitherChain v) = ErrorT (rewrapMonadIO s v)++instance (MonadIO m, MonadIOUnwrappable m c s) => MonadIOUnwrappable (ReaderT r m) c (r, s) where+  unwrapState = liftM2 (,) ask (lift unwrapState)+  unwrapMonadIO (r, s) m = unwrapMonadIO s (runReaderT m r)+  rewrapMonadIO (_, s) v = ReaderT (\_ -> rewrapMonadIO s v)++instance (MonadIO m, MonadIOUnwrappable m c s) => MonadIOUnwrappable (StateT r m) c (IORef r, s) where+  unwrapState = liftM2 (,) (get >>= (liftIO . newIORef)) (lift unwrapState)+  unwrapMonadIO (r, s) m = unwrapMonadIO s $ do+    s0 <- liftIO (readIORef r)+    (a, v') <- runStateT m s0+    liftIO (writeIORef r v')+    return a+  rewrapMonadIO (r, s) a = StateT (\_ -> liftM2 (,) (rewrapMonadIO s a) (liftIO . readIORef $ r))
+ LICENSE view
@@ -0,0 +1,23 @@+Copyright (c) 2012, The University of Auckland+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice,+   this list of conditions and the following disclaimer.+2. 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.++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ monadio-unwrappable.cabal view
@@ -0,0 +1,22 @@+Name: monadio-unwrappable+Version: 0.1+Cabal-Version: >= 1.6+Build-Type: Simple+License: BSD3+License-File: LICENSE+Author: Andrew Miller+Copyright: (C) 2012 The University of Auckland+Maintainer: ak.miller@auckland.ac.nz+Stability: experimental+Synopsis: Reversibly allow monad transformer stacks to run in IO+Description: Provides MonadIOUnwrappable instances for monad transformer stacks that can be run in the IO monad, with the result converted back to the monadic type. This package also includes some utilities built using MonadIOUnwrappable, such as a bracket implementation that works with MonadIO even on stacks containing one or more ErrorT transformers+Category: Control+Source-Repository head+    Type:     git+    Location: https://github.com/A1kmm/monadio-unwrappable++Library+    Exposed-Modules: Control.Monad.IO.Unwrappable+                     Control.Monad.IO.MonadIOException+    Build-Depends: base >= 3 && < 5,+                   monads-tf, transformers