[Haskell-cafe] [Haskell-beginners] testing IO code

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Mon Mar 16 14:40:35 UTC 2015


On 16 March 2015 at 19:51, Maurizio Vitale <mrz.vtl at gmail.com> wrote:

> suppose I have a restricted IO monad, RIO that only exposes readFile.
> and then I have a monad SIO that will eventually provide a virtual file
> system from a map path->content, also with a readFile function returning
> SIO(String).
>
> What is the way to write a function parseFile that can operate in both
> monads so that I can use SIO for testing? should I define a third monad
> CompileMonad that has instances for both RIO and SIO and then having
> parseFile :: CompileMonad ast?
>

You might be able to do something like,

    class MonadIO m => ProvidesReadFile m where
        readFile :: FilePath -> m String

    instance ProvidesReadFile RIO where
        readFile = readFileRIO    -- the RIO specific readFile

    instance ProvidesReadFile SIO where
        readFile = readFileSIO    -- the SIO specific readFile

    parseFile :: ProvidesReadFile m => FilePath -> m ast
    parseFile = do
        f <- readFile
        let ast = parse f    -- the pure parser
        return ast           -- works for both monads


> Thanks,
>
>   Maurizio
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
This is more suitable for the haskell-cafe. I am posting it there so that
more people might comment on it.
HTH.

-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150316/9545df39/attachment.html>


More information about the Haskell-Cafe mailing list