oi 0.0.4 → 0.0.5
raw patch · 4 files changed
+67/−46 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Data.OI: (>|->|) :: (a -> b :-> c) -> (a' -> b' :-> c') -> ((a, a') -> (b, b') :-> (c, c'))
+ Data.OI: (>|>|) :: (a -> b :-> c) -> (c -> d :-> e) -> (a -> (b, d) :-> e)
+ Data.OI: (|-|) :: (a :-> b) -> (a' :-> b') -> (a, a') :-> (b, b')
+ Data.OI: (|<|) :: (b -> c :-> d) -> (a :-> b) -> (a, c) :-> d
+ Data.OI: (|<|<) :: (c -> d :-> e) -> (a -> b :-> c) -> (a -> (b, d) :-> e)
+ Data.OI: (|>) :: (a :-> b) -> (b -> c) -> (a :-> c)
+ Data.OI: (|><|) :: (q -> a :-> (p, c)) -> (p -> b :-> (q, d)) -> ((a, b) :-> (c, d))
+ Data.OI: (|>|) :: (a :-> b) -> (b -> c :-> d) -> (a, c) :-> d
- Data.OI: (<|) :: (b -> c :-> d) -> (a :-> b) -> (a, c) :-> d
+ Data.OI: (<|) :: (b -> c) -> (a :-> b) -> (a :-> c)
- Data.OI: deTuple :: (a, b) :-> (OI a, OI b)
+ Data.OI: deTuple :: OI (a, b) -> (OI a, OI b)
Files
- oi.cabal +1/−1
- sample/echo.hs +1/−1
- sample/talk.hs +18/−36
- src/Data/OI.hs +47/−8
oi.cabal view
@@ -1,5 +1,5 @@ Name: oi-Version: 0.0.4+Version: 0.0.5 Category: Data Synopsis: Purely Functional Lazy Interaction with the outer world Description: This package implements a data structure and operations on it
sample/echo.hs view
@@ -8,7 +8,7 @@ import System.IO main :: IO ()-main = do { [] <- run $ puts <| gets; return () }+main = do { [] <- run $ puts |<| gets; return () } eof :: Int eof = -1
sample/talk.hs view
@@ -4,50 +4,32 @@ import Data.OI import Control.Parallel- import System.Environment main :: IO () main = do { kbd1:scr1:kbd2:scr2:_ <- getArgs- ; run (pmain kbd1 scr1 kbd2 scr2)+ ; run $ pmain (kbd1,scr1) (kbd2,scr2) } -pmain- :: FilePath- -> FilePath- -> FilePath- -> FilePath- -> ((String, [String], ()), (String, [String], ()))- :-> ()-pmain kbd1 scr1 kbd2 scr2 r- = let- p1 = talk "Alice" kbd1 scr1- p2 = talk "Bob" kbd2 scr2- (is1,is2) = (p1 |><| p2) r- in is1 `par` is2--(|><|) :: (q -> a :-> (p,c))- -> (p -> b :-> (q,d))- -> ((a,b) :-> (c,d))-(f |><| g) rasbs = case deTuple rasbs of- (ras,rbs) -> (cs,ds) where { (xs,cs) = f ys ras; (ys,ds) = g xs rbs }+pmain :: (FilePath,FilePath)+ -> (FilePath,FilePath)+ -> ((String, [String], ()), (String, [String], ()))+ :-> ()+pmain (kbd1,scr1) (kbd2,scr2)+ = uncurry par . (talk "Alice" (kbd1,scr1) |><| talk "Bob" (kbd2,scr2)) -talk :: String -- Talker name- -> FilePath- -> FilePath - -> [String] -- Messages from the other end- -> OI ( String -- Messages typed by the keyboard- , [String] -- Oracles for merging messages- , ()) -- Result of the process- -> ([String] -- Messages to the other end- ,()) -- Results of the process-talk name kbd scr msg r = (ins,showscreen scr (mergeOI ins msg os) us)+talk :: String -- Talker's name+ -> (FilePath,FilePath) -- (Talker's keybord, Talker's screen)+ -> [String] -- Messages from the other end+ -> OI (String,[String],()) -- (Talker's input,Merged messages, Process result)+ -> ([String],()) -- (Messages to the other end, Process result)+talk name (kbd,scr) msg r = (ins,showscr scr (mergeOI ins msg os) us) where (is,os,us) = deTriple r- ins = map ((name ++ ": ")++) $ lines $ readkeyboard kbd is+ ins = map ((name ++ ": ")++) $ lines $ readkbd kbd is -readkeyboard :: FilePath -> String :-> String-readkeyboard = iooi . readFile+readkbd :: FilePath -> String :-> String+readkbd = iooi . readFile -showscreen :: FilePath -> [String] -> () :-> ()-showscreen s = iooi . writeFile s . unlines+showscr :: FilePath -> [String] -> () :-> ()+showscr s = iooi . writeFile s . unlines
src/Data/OI.hs view
@@ -45,7 +45,15 @@ ,deList -- * Interaction combinators+ ,(>|>|)+ ,(|<|<)+ ,(>|->|)+ ,(|-|)+ ,(|<|)+ ,(|>|) ,(<|)+ ,(|>)+ ,(|><|) ,mapOI ,mapOI' ,zipWithOI@@ -63,7 +71,7 @@ import System.IO.Unsafe data OI a = OI { variable :: LeftValueOf (IO a), value :: a }-type a :-> b = OI a -> b+type a :-> b = OI a -> b -- | Binding operator (=:) :: a -> a :-> a@@ -81,15 +89,15 @@ -- instance Functor OI where- fmap f x = (#) (f (x?))+ fmap f = (#) . f . (?) instance Monad OI where return = (#) x >>= f = f (x?) instance Applicative OI where- pure = (#)- f <*> x = (#)((f?)(x?))+ pure = (#)+ (<*>) = (. (?)) . ((#) .) . (?) instance Extend OI where duplicate = (#)@@ -114,7 +122,7 @@ firstA f ac = case deTuple ac of (x,z) -> (f x, (z?)) -- | Embed interactions into tuple-deTuple :: (a,b) :-> (OI a,OI b)+deTuple :: OI (a,b) -> (OI a,OI b) deTuple (OI vxy ~(x,y)) = assign io vxy `pseq` (OI vx x, OI vy y) where vx = new0 ()@@ -145,10 +153,41 @@ io = (:) <$> (unsafeInterleaveIO (dereference vx)) <*> (unsafeInterleaveIO (dereference vxs)) -- | Connect two interactions into an interaction-(<|) :: (b -> c :-> d) -> (a :-> b) -> (a,c) :-> d-(f <| g) ac = case deTuple ac of (a,c) -> f (g a) c -infixr 1 <|+(|<|<) :: (c -> d :-> e) -> (a -> b :-> c) -> (a -> (b,d) :-> e)+(f |<|< g) a bd = case deTuple bd of (b,d) -> f (g a b) d++(>|>|) :: (a -> b :-> c) -> (c -> d :-> e) -> (a -> (b,d) :-> e)+(>|>|) = flip (|<|<)++(>|->|) :: (a -> b :-> c) -> (a' -> b' :-> c') -> ((a,a') -> (b,b') :-> (c,c'))+(f >|->| g) (a,a') bb' = case deTuple bb' of (b,b') -> (f a b, g a' b')++(|-|) :: (a :-> b) -> (a' :-> b') -> (a,a') :-> (b,b')+(f |-| g) aa' = case deTuple aa' of (a,a') -> (f a, g a')++(|<|) :: (b -> c :-> d) -> (a :-> b) -> (a,c) :-> d+(f |<| g) ac = case deTuple ac of (a,c) -> f (g a) c++(|>|) :: (a :-> b) -> (b -> c :-> d) -> (a,c) :-> d+(|>|) = flip (|<|)++infixr 1 |<|+infixl 1 |>|++(|>) :: (a :-> b) -> (b -> c) -> (a :-> c)+(|>) = flip (.)++(<|) :: (b -> c) -> (a :-> b) -> (a :-> c)+(<|) = (.)++(|><|) :: (q -> a :-> (p,c)) -> (p -> b :-> (q,d)) -> ((a,b) :-> (c,d))+(f |><| g) rab = case deTuple rab of+ (ra,rb) -> (c,d)+ where + (x,c) = f y ra+ (y,d) = g x rb+ -- | Map interaction function on an interaction list mapOI :: (a :-> b) -> [a] :-> [b]