Ver Fonte

Initial version

Marcos Dumay de Medeiros há 8 anos atrás
commit
a2219f7b5c

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+dist/
+.cabal-sandbox/
+cabal.sandbox.config
+*~
+**/*~
+
+

+ 30 - 0
LICENSE

@@ -0,0 +1,30 @@
+Copyright (c) 2016, Marcos Dumay de Medeiros
+
+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 Marcos Dumay de Medeiros 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.

+ 2 - 0
Setup.hs

@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain

+ 39 - 0
interruptible.cabal

@@ -0,0 +1,39 @@
+-- Initial interruptible.cabal generated by cabal init.  For further 
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name:                interruptible
+version:             0.1.0.0
+synopsis:            Interruptible monad transformers - transformers that can be run and resumed later, conserving their context.
+description:
+    Given an inner monad and a transformer:
+    > (Monad m, MonadTrans t)
+    If @t@ is an interruptible transformer, it becomes possible to intercalate executions
+    on the @t@ context with executions over the inner monad @m@ by breaking the execution
+    on @t@ and resuming it later.
+    .
+    Interruptible monads implement the @runI@ function so that, given @f :: a -> t m b@ and
+    @g :: b -> t m c@, @runI (f >>= g)@ is equivalent to @\x -> runI f x >>= runI g@.
+    .
+    That makes it possible to intercalate the execution of different monads, and even to
+    return a monadic context for another function to resume it.
+homepage:            https://sealgram.com/git/haskell/interruptible/
+license:             BSD3
+license-file:        LICENSE
+author:              Marcos Dumay de Medeiros
+maintainer:          marcos@marcosdumay.com
+-- copyright:           
+category:            Control
+build-type:          Simple
+-- extra-source-files:  
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     Control.Monad.Trans.Interruptible, Control.Monad.Trans.Interruptible.Class
+  -- other-modules:       
+  other-extensions:    TypeFamilies
+  build-depends:
+      base >=4.7 && <4.9,
+      transformers,
+      either
+  hs-source-dirs:      src
+  default-language:    Haskell2010

+ 28 - 0
src/Control/Monad/Trans/Interruptible.hs

@@ -0,0 +1,28 @@
+{-# LANGUAGE TypeFamilies #-}
+
+module Control.Monad.Trans.Interruptible where
+
+import Control.Monad.Trans.Interruptible.Class
+
+{- |
+Folds the second list with the function applied to the first,
+intercalating the evaluation. That is:
+
+@
+intercalateM f [a00, a10, a20] [b1, b2] = do
+  a01 <- f a00 b1
+  a11 <- f a10 b1
+  a21 <- f a20 b1
+  a02 <- f a11 b2
+  a12 <- f a21 b2
+  a22 <- f a31 b2
+  return [a02, a12, a22]
+@
+
+Usefull for consuming lazy sequences.
+-}
+intercalateM :: (InterruptibleMonadTrans m, Monad n) => (b -> a -> m n a) -> [b] -> [RDt m a] -> n [RDt m a]
+intercalateM _ [] aa = return aa
+intercalateM f (b:bb) aa = do
+  aa' <- mapM (\x -> runT x $ f b) aa
+  intercalateM f bb aa'

+ 14 - 0
src/Control/Monad/Trans/Interruptible/Class.hs

@@ -0,0 +1,14 @@
+{-# LANGUAGE TypeFamilies #-}
+
+module Control.Monad.Trans.Interruptible.Class where
+
+import Control.Monad.Trans.Class
+import Control.Monad.Trans.Either
+
+class MonadTrans m => InterruptibleMonadTrans m where
+  type RDt m :: * -> *
+  runT :: Monad n => RDt m a -> (a -> m n b) -> n (RDt m b)
+
+instance InterruptibleMonadTrans (EitherT e) where
+  type RDt (EitherT e) = Either e
+  runT st f = runEitherT (hoistEither st >>= f)