Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I make StateP from Control.Proxy an instance of MonadState?

I am changing some code that used to run inside a StateT monad to run inside of StateP from Control.Proxy. However, some of my code (e.g. the %= operator from Control.Lens) requires a MonadState instance. Is it safe/correct for me to just add such an instance? This seems like something that is most correctly handled by a library (in this case, Control.Proxy).

like image 288
ajp Avatar asked Oct 04 '22 18:10

ajp


1 Answers

Yes, this is safe. The instance you want is:

instance (Monad m, Proxy p) => MonadState s (PS.StateP s p a' a b' b m) where
    get = PS.get
    put = PS.put

I just want to briefly note that in pipes-4.0.0 (which is on Github) the proxy transformers are no longer necessary and the same extensions are outsourced to monad transformers in the base monad. This means that instead of:

Consumer (StateP s p) a m r

... you would use:

Consumer a (StateT s m) r

This means that you would then just be able to write:

lift $ myLens %= f

However, I still plan on adding MonadState instances for Proxy anyway, although perhaps in a separate package (I still haven't decided whether to include them in the main library yet). They would look like this:

instance (MonadState s m) => MonadState s (Proxy a' a b' b m r) where
    put s = lift (put s)
    get   = lift get
like image 129
Gabriella Gonzalez Avatar answered Oct 07 '22 18:10

Gabriella Gonzalez