IOã¢ãããåããªã
è²ã
å
¥éæ¸ãèªãã§ã¿ãã®ã ããIOã¢ããã¨ãããã®ãã©ããããã®ãªã®ãåãããªãã
使ãæ¹ã¯åãããã ãããªãã§ãããªã£ã¦ãã®ã¨ããã©ããã風ã«å®è£
ããã¦ãã®ãã¨ããã£ã±ãç解ã®å¤ã§ããã
ã¨ãããã¨ã§ãã¾ããNiseIOã¢ãããèªåã§ä½ã£ã¦è¦ã¦ããç¨åº¦èªåã§èãã¦ããå®è£ ãè¦ãªãã¨æããæ°åã«ãªãã®ã§ãèãã¦ã¿ãã
ããããå
¥éæ¸ãããIOã¢ããã¯å¯ä½ç¨å®è¡åã®ä¸çã¨å¯ä½ç¨å®è¡å¾ã®ä¸çï¼å¯ä½ç¨ã®çµæãèãããã¨ã§ç´ç²ã«ãªã£ã¦ããããã
ã ããã¾ãæ®éã«èãã¦ããããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯
data Env = Env {input::String, output::String } data NiseIOResult a = NiseIOResult a Env getChar_1 :: Env -> NiseIOResult Char getChar_1 (Env (x:xs) y) = NiseIOResult x (Env xs y) putChar_1 :: Char -> Env -> NiseIOResult () putChar_1 c (Env x y) = NiseIOResult () (Env x (c:y)) example_echo env = let NiseIOResult c env' = getChar_1 env in let NiseIOResult () env'' = putChar_1 c env' in example_echo env''
ã®ããã«ãå¯ä½ç¨ãç°å¢ãåãåã£ã¦ãæ°ããç°å¢ãè¿ãããã«ãªã£ã¦ããã®ã ã¨æãã
ã¨ããããå¯ä½ç¨ã®ããè¨èªã®æå³è«ã¯ããããå½¢ã«å¤æãã¦ããèãããã®ã ã£ãã¨æãã
ã¾ããããªé¢¨ã«ç°å¢ãé½ã«è¨è¿°ãããããªããæ¹ã ã¨ãæããã«ç°å¢ãæ£ãããªã³ã¯ãã¦ããã¨ãä¿éã§ããªãã®ã§è«å¤ãªããã§ã
ãããæ£ãããªã³ã¯ãããããã«å¶éããã¤ã³ã¿ã¼ãã§ã¼ã¹ãIOã¢ããã ã¨æã£ã¦ãã¾ã£ã¦ããã
ãããå ã«ãã¦NiseIOã¢ãããä½ã£ã¦ã¿ãã¨
type NiseIO a = Env -> NiseIOResult a instance Monad NiseIO where x >>= f = \env -> let (NiseIOResult v env') = x env in f v env' return x = \env -> NiseIOResult env x getChar_2 :: NiseIO Char getChar_2 = getChar_1 putChar_2 :: Char -> NiseIO () putChar_2 c = putChar_1 c execute :: Env -> NiseIO a -> IO (NiseIOResult a) execute env f = return (f env)
ã£ã¦ãªã£ããã ãã©ãããã¯ãªãã ããããããexecuteå´ãgetCharã¨ããå®è¡ãã¦ã¨ãã¥ã¬ã¼ãããªãã¨ãããªãã®ã«ãããå ´æããªãã
ã ãããããStateã¢ããã®å£åçãããããªããä½ããããã®ã¨éãã
ä½ãããã®ã¯
niseGetChar :: NiseIO Char nisePutChar :: Char -> NiseIO () niseMain :: NiseIO () niseMain = do c <- niseGetChar nisePutChar c niseMain executeNiseIO :: NiseIO a -> IO a main = executeNiseIO niseMain
ãªã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãå½ã¢ã¯ã·ã§ã³ã¯ç¡éãªã¹ãã¿ããã«ãªã£ã¦ã¦
executeNiseIO å½ã¢ã¯ã·ã§ã³
executeNiseIO (æåã®å½ã¢ã¯ã·ã§ã³:æ®ãã®å½ã¢ã¯ã·ã§ã³)
ããã§æåã®å½ã¢ã¯ã·ã§ã³ãå®è¡ãã
executeNiseIO (æåã®å½ã¢ã¯ã·ã§ã³:äºã¤ç®ã®å½ã¢ã¯ã·ã§ã³:æ®ãã®å½ã¢ã¯ã·ã§ã³)
ããã§äºã¤ç®ã®å½ã¢ã¯ã·ã§ã³ãå®è¡ãã
ã¿ããã«ãªã£ã¦ãªãã¨ãHaskellã®å¦çç³»ã¯å®è£ ã§ããªãã¨æãã®ã§ããã
ãããã£ã¦èãæãã¦ã§ããã®ããã
ç¶ããèªã