3

I have 2 functions with "intersecting" interfaces:

type Config = (param1, param2)

runExe :: [String] -> ReaderT Config IO ExitCode
someAction :: [String] -> ReaderT Config (MaybeT IO) ()

Since both runExe and someAction carry the immutable Config, I want to pass it implicitly without doing "asking" explicitly. Something like the following:

 someAction params = monadConvertor runExe ["some" , "params"]

How do I implement monadConvertor?

As far as I understand monadConvertor needs to extract IO ExitCode then lift guard (rc == ExitSuccess). But i don't know if it possible.

0

1 Answer 1

4

Your monadConverter should have the following type:

monadConverter :: ReaderT Config IO ExitCode -> ReaderT Config (MaybeT IO) ()

As you already wrote in your question, you only need to "extract" the ExitCode, and then "lift" guard (rc == ExitCode), which you can do with the lift function. The problem is now that you cannot use the ReaderT Config IO ExitCode inside a ReaderT Config (MaybeT IO) (). You can fix this by using mapReaderT:

mapReaderT :: mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b

Using that function, monadReader can be implemented as follows:

monadConverter :: ReaderT Config IO ExitCode -> ReaderT Config (MaybeT IO) ()
monadConverter action = do
  exitCode <- mapReaderT lift action
  lift $ guard (exitCode == ExitSuccess)

Here, mapReaderT lift has the type ReaderT Config IO ExitCode -> ReaderT Config (MaybeT IO) ExitCode.

1
  • Yes, it works great). But let me to note that there is no need to lift guard explicitly in this case.
    – kubivan
    May 17, 2014 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.