On 16 March 2015 at 19:51, Maurizio Vitale <mrz.vtl@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@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