ああLazy io

498 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ああLazy io

  1. 1. Non-Imperative Functional Programming 序章: 要求駆動I/O 2012-04-22 山下伸夫 nobsun@sampou.org
  2. 2. IO は命令書だmain :: IO ()main = do input <- openFile "i" ReadMode contents <- hGetContents input hClose input output <- openFile "o" WriteMode hPutStr output contents hClose output return ()
  3. 3. すぐやらなくてもいいLazyな命令 書いちゃだめでしょ!main :: IO ()main = do input <- openFile "i" ReadMode contents <- hGetContents input hClose input output <- openFile "o" WriteMode hPutStr output contents hClose output return ()
  4. 4. 命令書と実行結果main :: IO ()main = do line1 <- getLine line2 <- getLine putStrLn line2 putStrLn line1 ghci> :main world!↓ Hello,↓ Hello, world!
  5. 5. Lazyな命令があると?main :: IO ()main = do line1 <- lazy getLine line2 <- lazy getLine putStrLn line2 putStrLn line1 return () ghci> :main ?
  6. 6. Eagerな文脈にLazyな要素main :: IO ()main = do line1 <- lazy getLine line2 <- lazy getLine putStrLn line2 putStrLn line1 return () ghci> :main world!↓ world! Hello,↓ Hello,
  7. 7. Eagerな文脈にLazyな要素 !main :: IO ()main = do line1 <- lazy getLine 険 line2 <- lazy getLine putStrLn line2 危 な putStrLn line1 る return () ghci> :main world!↓ ぜ world! Hello,↓ Hello, ま
  8. 8. 全部Lazyにしてしまえ!main :: IO ()main = do line1 <- lazy getLine line2 <- lazy getLine lazy (putStrLn line2) lazy (putStrLn line1) return () ghci> :main ?
  9. 9. Lazy = 要求駆動main :: IO ()main = do line1 <- lazy getLine line2 <- lazy getLine lazy (putStrLn line2) ね lazy (putStrLn line1) return () だ ghci> :main 然 ghci> 当 。 ま
  10. 10. 要求駆動I/Omain :: IO ()main = do line1 <- lazy getLine line2 <- lazy getLine prn2 <- lazy (putStrLn line2) prn1 <- lazy (putStrLn line1) return $! sequencing [force line1 ,force line2 ,prn2 ,prn1]
  11. 11. 要求駆動I/O ス ー ! ォ えforce :: a -> ()force !x = () フ 使 をsequencing :: [()] -> ()sequencing sq = case dropWhile (()==) sq of [] -> ()
  12. 12. IO は命令書だmain :: IO ()main = do input <- lazy $ openFile "i" ReadMode icloseR <- lazy $ hClose input output <- lazy $ openFile "o" WriteMode ocloseR <- lazy $ hClose output contents <- hGetContents input printR <- lazy $ hPutStr output contents return $! sequencing $ [printR,icloseR,ocloseR]
  13. 13. ああ IO ()main :: IO ()main = do input <- lazy $ openFile "i" ReadMode icloseR <- lazy $ hClose input output <- lazy $ openFile "o" WriteMode ocloseR <- lazy $ hClose output contents <- hGetContents input printR <- lazy $ hPutStr output contents return $! sequencing $ [printR,icloseR,ocloseR]
  14. 14. ああ IO ()型はプログラムの仕様じゃなかった
  15. 15. 対話:: a :-> b としてのI/Otype a :-> b = OI a -> bmainR :: a :-> ()runI :: (a :-> b) -> IO bmain = runI mainR
  16. 16. oi package実装についてはオイオイ to be continued

×