I'm writing a program that involves the use of an interaction
Monad. However it seems to be insufficiently lazy to provide
any interaction. The following code is derived from my original
but highly simplified. Why is "works" lazy, but "main" is not?
Thanks for any help
Sengan
> module Main(main) where
> import IO
--------------------------------------------------------------------------------
> works
> = hGetContents stdin >>= \input ->
> foldr (>>) (return ()) $ map putStr $ lines input
--------------------------------------------------------------------------------
> main
> = hGetContents stdin >>= \input ->
> runI $ foldr (>>) (return "") $ map output $ lines input
> data IState = IState { io :: IO () }
> data I a = I (IState -> (a, IState))
> instance Monad I where
> return k = I $ \s -> (k,s)
> (I c1) >>= fc2 = I $ \s0 -> let (r,s1) = c1 s0
> I c2 = fc2 r in
> c2 s1
> runI (I c) = let (result, state) = c (IState $ return ()) in
> io state >> return ()
> output string
> = I $ \s -> (string, s { io = (io s) >> putStr (string ++ "\n") })