[Haskell-cafe] Implementing `MonadBaseControl IO` for application type

Li-yao Xia lysxia at gmail.com
Sat Feb 2 13:36:40 UTC 2019


Hi Magnus,

You can use GeneralizedNewtypeDeriving:

     deriving (..., MonadBase IO, MonadBaseControl IO)

To do it by hand, since AppM is a newtype around ReaderT, use the fact 
that is already is an instance of MonadBaseControl.

     liftBaseWith f = AppM (liftBaseWith (\run -> f (run . unAppM)))

To discover that implementation, you can start with the idea that 
liftBaseWith should basically be the same as the one as for ReaderT, 
using TypeApplications to make explicit the instance we want to use:

     liftBaseWith = liftBaseWith @IO @(ReaderT Env IO)

And then fix the type errors by inserting AppM and unAppM in the right 
places, possibly after eta-expanding some things.

     liftBaseWith f = liftBaseWith @IO @(ReaderT Env IO) f
     liftBaseWith f = liftBaseWith @IO @(ReaderT Env IO) (\run -> f run)
     -- The type errors now point exactly to the two locations that need 
changing.

The rest of your implementation looks good.

Li-yao

On 2/2/19 6:28 AM, Magnus Therning wrote:
> Hi,
> 
> I'm getting stuck on implementing `MonadBaseControl IO` for my 
> application context type. What I have so far (in a minimized example) is
> 
> ```
> data Env = Env {envBalance :: !(TVar Int)}
> 
> newtype AppM a = AppM { unAppM :: ReaderT Env IO a }
>    deriving (Functor, Applicative, Monad, MonadIO, MonadReader Env)
> 
> instance MonadBase IO AppM where
>    liftBase = liftIO
> 
> instance MonadBaseControl IO AppM where
>    type StM AppM a = a
>    liftBaseWith f = undefined
>    restoreM = return
> ```
> 
> I'm so utterly stuck on the definition of `liftBaseWith`... but I'm not 
> 100% sure on the other bits either, though it "feels right".
> 
> Any tips on how I should go about it?
> 
> /M
> 
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
> 


More information about the Haskell-Cafe mailing list