[Haskell-beginners] Haskeline and forkIO

Peter Jones mlists at pmade.com
Thu Aug 28 14:11:15 UTC 2014


"Jeff C. Britton" <jcb at iteris.com> writes:
> loop :: InputT IO ()
> loop = do
>     maybeLine <- getInputLine "Enter a file to compress> "
>     case maybeLine of
>       Nothing -> return ()      -- user entered EOF
>       Just "" -> return ()      -- treat no name as "want to quit" 
>       Just path -> do
>             return (runWorker path)
>             loop

The other issue you're having is because `runWorker path` is an `IO ()`
value but at the point where you use it in the code the type system
wants an `InputT IO ()`.  To try to satisfy the type system you used
`return` to build a `InputT IO (IO ())` value, but that doesn't actually
work (as you've noticed).  Since `InputT` is a transformer you have an
extra layer to work through and so need to *lift* your `IO ()` value
into the `InputT IO` layer.  Try this:

    -- Add this import
    import Control.Monad.IO.Class
    
    loop :: InputT IO ()
    loop = do
        maybeLine <- getInputLine "Enter a file to compress> "
        case maybeLine of
          Nothing -> return ()      -- user entered EOF
          Just "" -> return ()      -- treat no name as "want to quit" 
          Just path -> do
                liftIO (runWorker path)
                loop
    

You can think of `liftIO` as having this signature (in this context):

    liftIO :: IO () -> InputT IO ()
    
-- 
Peter Jones, Founder, Devalot.com
Defending the honor of good code



More information about the Beginners mailing list