module MyIO where import MyState import MyStream newtype MyIO s a = MyIO (WithIO s -> (a, WithIO s)) unMyIO :: MyIO s a -> WithIO s -> (a, WithIO s) unMyIO (MyIO m) = m instance Monad (MyIO s) where -- 基本的には ST と同じ return a = MyIO (\ s -> (a, s)) (MyIO m) >>= k = MyIO (\ w -> let (a,w1) = m w in unMyIO (k a) w1) instance Functor (MyIO s) where fmap f x = x >>= \ a -> return (f a) instance Applicative (MyIO s) where pure = return f <*> x = f >>= \ g -> x >>= \ a -> return (g a) instance MyState MyIO where set p v = MyIO (\ (s,i,o) -> ((), (snd (p s) v,i,o))) get p = MyIO (\ (s,i,o) -> (fst (p s), (s,i,o))) instance MyStream (MyIO s) where readChar = MyIO (\ (s,c:cs,o) -> (c,(s,cs,o))) eof = MyIO (\ (s,i,o) -> (null i,(s,i,o))) writeStr v = MyIO (\ (s,i,o) -> ((),(s,i,o ++ v))) fst3 (x,_,_) = x snd3 (_,y,_) = y thd3 (_,_,z) = z atoi :: String -> Integer atoi = read atof :: String -> Double atof = read