Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
El agua es una riqueza
El agua es una riqueza
Loading in …3
×
1 of 16

ああLazy io

0

Share

Download to read offline

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

ああ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/O main :: 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/O type a :-> b = OI a -> b mainR :: a :-> () runI :: (a :-> b) -> IO b main = runI mainR
  16. 16. oi package 実装についてはオイオイ to be continued

×