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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With