lazyio (empty) → 0.0.1
raw patch · 6 files changed
+229/−0 lines, 6 filesdep +basedep +mtldep +special-functorssetup-changed
Dependencies added: base, mtl, special-functors
Files
- LICENSE +31/−0
- Setup.lhs +3/−0
- lazyio.cabal +44/−0
- src/Data/ApplicativeChain.hs +17/−0
- src/System/IO/Lazy.hs +103/−0
- src/System/IO/Lazy/Applicative.hs +31/−0
+ LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2009, Henning Thielemann++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.++ * The names of contributors may not 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.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#! /usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ lazyio.cabal view
@@ -0,0 +1,44 @@+Name: lazyio+Version: 0.0.1+License: BSD3+License-File: LICENSE+Author: Henning Thielemann <haskell@henning-thielemann.de>+Maintainer: Henning Thielemann <haskell@henning-thielemann.de>+Homepage: http://www.haskell.org/haskellwiki/Lazy_IO+Category: Monads, Control+Synopsis: Run IO actions lazily while respecting their order+Description:+ Run IO actions lazily while respecting their order.+ Running a value of the LazyIO monad in the IO monad is like starting a thread+ which is however driven by its output.+ That is, the LazyIO action is only executed as far as necessary+ in order to provide the required data.+Tested-With: GHC==6.8.2+Cabal-Version: >=1.6+Build-Type: Simple+Source-Repository head+ type: darcs+ location: http://code.haskell.org/~thielema/lazyio/++Source-Repository this+ type: darcs+ location: http://code.haskell.org/~thielema/lazyio/+ tag: 0.0.1++Flag splitBase+ description: Choose the new smaller, split-up base package.++Library+ Build-Depends: mtl >=1.1 && <1.2+ If flag(splitBase)+ Build-Depends: base >= 2+ Else+ Build-Depends: base >= 1.0 && < 2, special-functors >=1.0 && <1.1++ GHC-Options: -Wall+ Hs-Source-Dirs: src+ Exposed-Modules:+ System.IO.Lazy+ Other-Modules:+ Data.ApplicativeChain+ System.IO.Lazy.Applicative
+ src/Data/ApplicativeChain.hs view
@@ -0,0 +1,17 @@+module Data.ApplicativeChain where++import Control.Applicative (Applicative(pure, (<*>)), )+++data T a = Cons {runAll :: RunAll, result :: a}++data RunAll = RunAll+ deriving Show++instance Functor T where+ fmap f ~(Cons as a) = Cons as $ f a++instance Applicative T where+ pure = Cons RunAll+ Cons fs f <*> a =+ fmap f $ case fs of RunAll -> a
+ src/System/IO/Lazy.hs view
@@ -0,0 +1,103 @@+{- |+Caution:++- Although this module calls 'unsafeInterleaveIO' for you,+ it cannot take the responsibility from you.+ Using this module is still as unsafe as calling 'unsafeInterleaveIO' manually.+ Thus we recommend to wrap the lazy I/O monad+ into a custom @newtype@ with a restricted set of operations+ which is considered safe for interleaving I/O actions.++- Operations like 'System.IO.hClose' are usually not safe within this monad,+ since they will only executed, if their result is consumed.+ Since this result is often @()@ this is quite unusual.+ It will also often be the case, that not the complete output is read,+ and thus the closing action is never reached.+ It is certainly best to call a closing action after you wrote+ the complete result of the lazy I/O monad somewhere++- @return a :: LazyIO a@ is very different from @liftIO (return a) :: LazyIO a@.+ The first one does not trigger previous IO actions,+ whereas the second one does.++Use it like++> import qualified System.IO.Lazy as LazyIO+>+> LazyIO.run $+> do liftIO $ putStr "enter first line:"+> x <- liftIO getLine+> liftIO $ putStr "enter second line:"+> y <- liftIO getLine+> return x++Because only the first line is needed,+only the first prompt and the first 'getLine' is executed.++-}+module System.IO.Lazy (+ T,+ run,+ ) where++import Control.Monad.Trans (MonadIO(liftIO), )+import Control.Monad.State (StateT(StateT), mapStateT, evalStateT, {- runStateT, -} )+import Control.Monad (ap, {- liftM2, -} )+import Control.Applicative (Applicative(pure, (<*>)), )+import System.IO.Unsafe (unsafeInterleaveIO, )+++newtype T a = Cons {decons :: StateT RunAll IO a}++data RunAll = RunAll+ deriving Show++instance Monad T where+ return x = Cons $ return x+ x >>= f = Cons $+ mapStateT unsafeInterleaveIO . decons . f =<<+ mapStateT unsafeInterleaveIO (decons x)++instance Functor T where+ fmap f = Cons . fmap f . decons++instance Applicative T where+ pure = return+ (<*>) = ap++instance MonadIO T where+ liftIO m = Cons $ StateT $ \RunAll -> fmap (\x->(x,RunAll)) m++run :: T a -> IO a+run =+ flip evalStateT RunAll . decons++{-+correct:+run $ do x <- liftIO getLine; y <- liftIO getLine; a <- return (x,y); return (fst a)++*LazyIO> run (Control.Monad.replicateM 5 (liftIO getChar)) >>= putStrLn+0011223344++*LazyIO> run (liftIO (putStrLn "bla") >> liftIO getLine) >>= print+"bla+1+1"++*LazyIO> run $ Monad.liftM (\ ((a,b),(c,d))->b) $ liftM2 (,) (liftM2 (,) (liftIO getLine) (liftIO getLine)) (liftM2 (,) (liftIO getLine) (liftIO getLine))+"1+2+2"+-}++{-+testLazy, testStrict :: IO String+testLazy = run $ liftM2 (const) (liftIO getLine) (liftIO getLine)+testStrict = run $ liftM2 (flip const) (liftIO getLine) (liftIO getLine)++test :: IO (String, RunAll)+test = flip runStateT RunAll $ decons $+ liftIO getLine >>= \x ->+ liftIO getLine >>+ return x+-}
+ src/System/IO/Lazy/Applicative.hs view
@@ -0,0 +1,31 @@+module System.IO.Lazy.Applicative where++import qualified Data.ApplicativeChain as Chain+import Control.Applicative (Applicative(pure, (<*>)), )+-- import Control.Monad.Trans (MonadIO(liftIO), )+import Control.Monad (liftM2, )+import System.IO.Unsafe (unsafeInterleaveIO, )+++newtype T a = Cons {decons :: IO (Chain.T a)}++data RunAll = RunAll+ deriving Show++instance Functor T where+ fmap f = Cons . fmap (fmap f) . decons++instance Applicative T where+ pure = Cons . return . Chain.Cons Chain.RunAll+ Cons f <*> Cons x = Cons $ liftM2 (<*>) f x++-- instance MonadIO T where+liftIO :: IO a -> T a+liftIO = Cons . unsafeInterleaveIO . fmap (Chain.Cons Chain.RunAll)++run :: T a -> IO a+run = fmap Chain.result . decons++{-+run $ liftA (\ ~( ~(a,b), ~(c,d)) -> a) $ liftA2 (,) (liftA2 (,) (liftIO getLine) (liftIO getLine)) (liftA2 (,) (liftIO getLine) (liftIO getLine))+-}