oi 0.1.0 → 0.1.1
raw patch · 10 files changed
+139/−57 lines, 10 files
Files
- oi.cabal +5/−3
- sample/Makefile +12/−4
- sample/cats.hs +0/−31
- sample/catsIO.hs +17/−0
- sample/catsLIO.hs +33/−0
- sample/catsOI.hs +24/−0
- sample/catsSIO.hs +23/−0
- sample/echo.hs +2/−2
- src/Data/OI/Base.hs +2/−3
- src/Data/OI/Combinator.hs +21/−14
oi.cabal view
@@ -1,10 +1,12 @@ Name: oi-Version: 0.1.0+Version: 0.1.1 Category: Data Synopsis: Purely Functional Lazy Interaction with the outer world Description: This package implements a data structure and operations on it for making interactive program without using explicitly IO monads.- Version 0.1.0: APIs are changed from before.+ + Version 0.1.1: APIs are changed!+ Stability: Experimental License: BSD3 License-File: LICENSE@@ -14,7 +16,7 @@ Cabal-Version: >= 1.14 Data-dir: sample-Data-files: Makefile cats.hs echo.hs recdircs.hs talk.hs+Data-files: Makefile catsSIO.hs catsLIO.hs catsIO.hs catsOI.hs echo.hs recdircs.hs talk.hs Library Default-Language: Haskell2010
sample/Makefile view
@@ -1,4 +1,4 @@-EXECS = echo recdircs talk cats+EXECS = echo recdircs talk catsSIO catsIO catsLIO catsOI all : $(EXECS) @@ -11,9 +11,17 @@ talk : talk.hs ghc --make -O0 -threaded $< -o $@ -cats : cats.hs+catsIO : catsIO.hs ghc --make -O0 -threaded $< -o $@ -clean :- -rm $(EXECS) *.hi *.o *~+catsSIO : catsSIO.hs+ ghc --make -O0 -threaded $< -o $@ +catsLIO : catsLIO.hs+ ghc --make -O0 -threaded $< -o $@++catsOI : catsOI.hs+ ghc --make -O0 -threaded $< -o $@++clean :+ -rm $(EXECS) *.hi *.o *~ core
− sample/cats.hs
@@ -1,31 +0,0 @@-{-# LANGUAGE BangPatterns- ,TypeOperators- #-}-module Main where--import Data.List-import Data.OI-import Prelude as P--main :: IO ()-main = run pmain'--pmain :: (IOResult (Resource String), ([IOResult (Resource String)], [()])) :-> ()-pmain = forces . (mapR id . inFileResource "files" |/| head3s)--head3s :: [FilePath] -> ([IOResult (Resource String)], [()]) :-> [()]-head3s xs = map (unlines . takeR (3::Int)) . zipWithOI' inFileResource xs |/| zipWithOI' (iooi . P.putStrLn)--pmain' :: (IOResult (Resource String), ([IOResult (Resource String)], IOResult (Resource ()))) :-> ()-pmain' = mapR id . inFileResource "files" |/| head3s'--head3s' :: [FilePath] -> ([IOResult (Resource String)], IOResult (Resource ())) :-> ()-head3s' xs = forceResult . (map (unlines . ("==========":) . takeR (3::Int)) . zipWithOI' inFileResource xs |/| outFileResource "/dev/pts/3")--forceResult :: IOResult (Resource a) -> ()-forceResult (Success s) = forces $ map force $ stream s-forceResult (Failure e) = error e--main' :: IO ()-main' = readFile "files"- >>= mapM readFile . lines >>= mapM_ putStr . intersperse "==========" . map (unlines . take 3 . lines)
+ sample/catsIO.hs view
@@ -0,0 +1,17 @@+module Main where++import System.Environment++main :: IO ()+main = do+ args <- getArgs+ contents <- if not $ null args + then do+ mapM readFile args+ else do + files <- readFile "files"+ mapM readFile (lines files)+ writeFile "output.txt" (concatMap textproc contents)++textproc :: String -> String+textproc = unlines . take 1 . lines
+ sample/catsLIO.hs view
@@ -0,0 +1,33 @@+module Main where++import System.Environment+import System.IO+import System.IO.Unsafe+import Prelude++main :: IO ()+main = do+ args <- getArgs+ contents <- if not $ null args + then do+ sequenceLz $ map readFile args+ else do + files <- readFile "files"+ sequenceLz $ map readFile (lines files)+ writeFile "output.txt" (concatMap textproc contents)++textproc :: String -> String+textproc = id -- unlines . take 1 . lines++lazy :: IO a -> IO a+lazy = unsafeInterleaveIO++mapLzM :: (a -> IO b) -> [a] -> IO [b]+mapLzM = (sequenceLz .) . map++sequenceLz :: [IO a] -> IO [a]+sequenceLz [] = lazy $ return []+sequenceLz (i:is) = lazy $ do+ r <- lazy i+ rs <- sequenceLz is+ return (r:rs)
+ sample/catsOI.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE TypeOperators #-}+module Main where++import Data.OI++main :: IO ()+main = run pmain++pmain :: (([String], Either (String, [String]) [String]), ()) :-> ()+pmain = args + |/| choiceOIOn (const $ lines . readFile' "files" |/| zipWithOI readFile') + (zipWithOI readFile')+ null+ |/| writeFile' "output.txt" . concatMap textproc++readFile' :: FilePath -> OI String -> String+readFile' = iooi . readFile++writeFile' :: FilePath -> String -> OI () -> ()+writeFile' = (iooi .) . writeFile++textproc :: String -> String+textproc = id -- unlines . take 1 . lines+
+ sample/catsSIO.hs view
@@ -0,0 +1,23 @@+module Main where++import System.Environment+import System.IO.Strict+import Prelude hiding (readFile, writeFile)++main :: IO ()+main = do+ args <- getArgs+ main' args++main' :: [String] -> IO ()+main' args = run $ do+ contents <- if not $ null args + then do+ mapM readFile args+ else do + files <- readFile "files"+ mapM readFile (lines files)+ writeFile "output.txt" (concatMap textproc contents)++textproc :: String -> String+textproc = unlines . take 1 . lines
sample/echo.hs view
@@ -29,13 +29,13 @@ gets = map chr . takeWhile (eof /=) . mapOI getc puts :: String -> [()] :-> [()]-puts = zipWithOI' putc+puts = zipWithOI putc getchar :: IO Int getchar = choice (return (-1)) (return . ord =<< getChar) =<< isEOF puts' :: String -> OI [()] -> ()-puts' = seqsOI' . map putc+puts' = seqsOI . map putc echo = getc |/| putc' echos = gets |/| puts
src/Data/OI/Base.hs view
@@ -234,8 +234,8 @@ -- | Forcing utilities -forces :: [()] -> ()-forces = force . dropWhile (()==)+forces :: [a] -> ()+forces = force . dropWhile (()==) . map force force :: a -> () force x = x `pseq` ()@@ -273,4 +273,3 @@ { s <- tryPutMVar v x ; if s then return x else readMVar v }-
src/Data/OI/Combinator.hs view
@@ -25,6 +25,7 @@ -- * Conditional Choice ,ifOI ,choiceOI+ ,choiceOIOn -- * Sequencing ,seqsOI ,seqsOI'@@ -47,12 +48,12 @@ (|:|) :: (a :-> c) -> (b :-> d) -> ((a,b) :-> (c,d)) (f |:| g) o = case dePair o of (a,b) -> (f a, g b) -(|>|) :: (a :-> (p,c)) -> (b :-> (p -> d)) -> ((a,b) :-> (c,d))-(f |>| g) o = case dePair o of (a,b) -> (c, g b p) where (p,c) = f a- (|/|) :: (a :-> c) -> (c -> (b :-> d)) -> ((a,b) :-> d) (f |/| g) o = case dePair o of (a,b) -> g (f a) b +(|>|) :: (a :-> (p,c)) -> (b :-> (p -> d)) -> ((a,b) :-> (c,d))+(f |>| g) o = case dePair o of (a,b) -> (c, g b p) where (p,c) = f a+ (|><|) :: (a :-> (p -> (q,c))) -> (b :-> (q -> (p,d))) -> ((a,b) :-> (c,d))@@ -66,13 +67,13 @@ Just (x,xs) -> f x : mapOI f xs _ -> [] -zipWithOI :: (a :-> (b -> c)) -> ([a] :-> ([b] -> [c]))-zipWithOI _ _ [] = []-zipWithOI f os (b:bs) = case deList os of- Just (x,xs) -> f x b : zipWithOI f xs bs- _ -> []+zipWithOI :: (a -> (b :-> c)) -> ([a] -> ([b] :-> [c]))+zipWithOI _ [] _ = []+zipWithOI f (b:bs) os = case deList os of+ Just (x,xs) -> f b x : zipWithOI f bs xs+ _ -> [] -zipWithOI' :: (a -> (b :-> c)) -> ([a] -> ([b] :-> [c]))+zipWithOI' :: (a :-> (b -> c)) -> ([a] :-> ([b] -> [c])) zipWithOI' = flip . zipWithOI . flip -- | Conditional branching@@ -88,11 +89,17 @@ choiceOI :: (a :-> c) -> (b :-> c) -> Bool -> (Either a b :-> c) choiceOI = flip . flip ifOI +choiceOIOn :: (t -> a :-> c) -> (t -> b :-> c) -> (t -> Bool)+ -> t -> Either a b :-> c+choiceOIOn f g p x = choiceOI (f x) (g x) (p x)+ -- | Sequencing-seqsOI :: [a] :-> ([a :-> b] -> ())-seqsOI os is = case dropWhile (()==) $ map force $ zipWithOI (flip id) os is of+seqsOI :: [a :-> b] -> ([a] :-> ())+seqsOI s os = forces $ zipWithOI ($) s os+++seqsOI' :: [a] :-> ([a :-> b] -> ())+seqsOI' os is = case dropWhile (()==) $ map force $ zipWithOI id is os of [] -> ()- _ -> error "seqsOI: Impossible!"+ _ -> error "seqsOI': Impossible!" -seqsOI' :: [a :-> b] -> ([a] :-> ())-seqsOI' = flip seqsOI