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

Paul Brauner polux2001 at gmail.com
Mon Mar 16 15:11:57 UTC 2015


You might want to have a look at https://hackage.haskell.org/package/IOSpec
which I think does something very similar to what you're trying to achieve.

On Mon, Mar 16, 2015 at 3:47 PM Sumit Sahrawat, Maths & Computing, IIT
(BHU) <sumit.sahrawat.apm13 at iitbhu.ac.in> wrote:

> 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
>  _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150316/a8b0bffc/attachment.html>


More information about the Haskell-Cafe mailing list