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

Magnus Therning magnus at therning.org
Sat Feb 2 13:52:03 UTC 2019


Li-yao Xia <lysxia at gmail.com> writes:

> Hi Magnus,
>
> You can use GeneralizedNewtypeDeriving:
>
>     deriving (..., MonadBase IO, MonadBaseControl IO)

I did start out with that, but got this from the compiler:

    Main.hs    18  26 error           error:
        • The type family application ‘StM (ReaderT Env IO) a’
            is no smaller than the instance head ‘StM AppM a’
          (Use UndecidableInstances to permit this)
        • In the instance declaration for ‘MonadBaseControl IO 
        AppM’

Adding `UndecidableInstances` is something I'm not quite 
comfortable with,
since I don't understand what it means.

> 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.

Thanks. That's excellent advise, I'll need to add that extension,
`TypeApplications`, to my toolbox right away.

> The rest of your implementation looks good.

Thanks for your help!

/M

--
Magnus Therning              OpenPGP: 0x927912051716CE39
email: magnus at therning.org
twitter: magthe              http://magnus.therning.org/

Reality is that which, when you stop believing in it, doesn't go 
away.
     — Philip K. Dick
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190202/850ff432/attachment.sig>


More information about the Haskell-Cafe mailing list