yi huang | 22 Aug 09:16 2012
Picon

How to implement instance of MonadBaseControl IO

I have a `newtype Yun a = Yun { unYun :: ReaderT YunEnv (ResourceT IO) a }` , and i need to define an instance of `MonadBaseControl IO` for it.

Newtype instance deriving don't work here. I guess the answer is simple, i just can't figure it out, hope anybody can lightening me.

Best regards.
Yihuang.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Chris Wong | 22 Aug 10:17 2012
Picon

Re: How to implement instance of MonadBaseControl IO

On Wed, Aug 22, 2012 at 7:16 PM, yi huang <yi.codeplayer <at> gmail.com> wrote:
> I have a `newtype Yun a = Yun { unYun :: ReaderT YunEnv (ResourceT IO) a }`
> , and i need to define an instance of `MonadBaseControl IO` for it.
> Newtype instance deriving don't work here. I guess the answer is simple, i
> just can't figure it out, hope anybody can lightening me.

I had the same problem some time ago. In my case it's StateT instead
of ReaderT, but it's the same idea. The tough part is getting around
the crazy CPS -- it's supposed to help with performance, but at the
cost of usability.

Anyway, here's my implementation:
https://github.com/lfairy/haskol/blob/master/Web/KoL/Core.hs#L58

Michael Snoyman has written a tutorial as well:
http://www.yesodweb.com/book/monad-control
I'd recommend printing it out and going over it slowly -- it can get
pretty dense at times.

Chris

> Best regards.
> Yihuang.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Erik Hesselink | 24 Aug 21:25 2012
Picon

Re: How to implement instance of MonadBaseControl IO

I'm not sure if you already have something working, but we have
several in our codebase, all following a similar pattern. For example:

newtype GeoServer a = GeoServer { unGeoServer :: ReaderT
GeoServerState (ServerPartT IO) a }

instance MonadBaseControl IO GeoServer where
  newtype StM GeoServer a = StMGeoServer { unStMGeoServer :: StM
(ReaderT GeoServerState (ServerPartT IO)) a }
  liftBaseWith f = GeoServer (liftBaseWith (\run -> f (liftM
StMGeoServer . run . unGeoServer)))
  restoreM = GeoServer . restoreM . unStMGeoServer

Erik

On Wed, Aug 22, 2012 at 9:16 AM, yi huang <yi.codeplayer <at> gmail.com> wrote:
> I have a `newtype Yun a = Yun { unYun :: ReaderT YunEnv (ResourceT IO) a }`
> , and i need to define an instance of `MonadBaseControl IO` for it.
> Newtype instance deriving don't work here. I guess the answer is simple, i
> just can't figure it out, hope anybody can lightening me.
>
> Best regards.
> Yihuang.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
yi huang | 25 Aug 04:08 2012
Picon

Re: How to implement instance of MonadBaseControl IO

On Sat, Aug 25, 2012 at 3:25 AM, Erik Hesselink <hesselink <at> gmail.com> wrote:
I'm not sure if you already have something working, but we have
several in our codebase, all following a similar pattern. For example:

newtype GeoServer a = GeoServer { unGeoServer :: ReaderT
GeoServerState (ServerPartT IO) a }

instance MonadBaseControl IO GeoServer where
  newtype StM GeoServer a = StMGeoServer { unStMGeoServer :: StM
(ReaderT GeoServerState (ServerPartT IO)) a }
  liftBaseWith f = GeoServer (liftBaseWith (\run -> f (liftM
StMGeoServer . run . unGeoServer)))
  restoreM = GeoServer . restoreM . unStMGeoServer

Thank you all, i've adapted Chris's code which is similar to yours, and it works now. 
 

Erik

On Wed, Aug 22, 2012 at 9:16 AM, yi huang <yi.codeplayer <at> gmail.com> wrote:
> I have a `newtype Yun a = Yun { unYun :: ReaderT YunEnv (ResourceT IO) a }`
> , and i need to define an instance of `MonadBaseControl IO` for it.
> Newtype instance deriving don't work here. I guess the answer is simple, i
> just can't figure it out, hope anybody can lightening me.
>
> Best regards.
> Yihuang.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



--
http://yi-programmer.com/
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane