Simple pipe program

I have the following program that does not produce output when run from runhaskell Toy.hs , but instead hangs endlessly. In my opinion, the program should print "hello" and then exit. I would appreciate an answer and / or advice on how to debug such a problem. I am using Pipes 4.0.0 from github ( github.com/Gabriel439/Haskell-Pipes-Library ).

 module Toy where import Pipes import Control.Monad.State type Request = String type Response = String serveChoice :: Request -> Server Request Response IO () serveChoice = forever go where go req = do lift $ putStrLn req respond req run :: Monad m => () -> Client Request Response (StateT Int m) () run () = do request "hi" return () main :: IO () main = evalStateT (runEffect $ hoist lift . serveChoice >-> run $ ()) 0 
+7
source share
1 answer

You need to use foreverK instead of forever , for example:

 module Toy where import Pipes import Pipes.Prelude (foreverK) import Control.Monad.State type Request = String type Response = String serveChoice :: Request -> Server Request Response IO () serveChoice = foreverK go where go req = do lift $ putStrLn req respond req run :: Monad m => () -> Client Request Response (StateT Int m) () run () = do request "hi" return () main :: IO () main = evalStateT (runEffect $ hoist lift . serveChoice >-> run $ ()) 0 

The reason your original version freezes is because you used forever in the Reader monad (i.e. in the monad ((->) a) ), and not in the monad. Inside this monad, forever equivalent to :

 -- ie mb -> mc forever :: (a -> b) -> (a -> c) forever m = m >> forever m = m >>= \_ -> forever m = \a -> (\_ -> forever m) (ma) a = \a -> forever ma = forever m 

foreverK is probably what you wanted as it is the same idiom for Server presented in the pipes-3.3.0 tutorial.

This change fixes a program that has now completed normally:

 >>> main hi >>> 
+9
source

All Articles