concrete functor and monad transformers
#94Weaken Monad constraints of {eval,exec}StateT to Functor.
Is it possible to weaken the constraints of evalStateT
and execStateT
from Monad
to Functor
?
evalStateT :: Functor f => StateT s f a -> s -> f a
evalStateT m s = fst <$> runStateT m s
execStateT :: Functor f => StateT s f a -> s -> f s
execStateT m s = snd <$> runStateT m s
This came up translating withIndex
(https://github.com/mstksg/inCode/blob/e1f80a3dfd83eaa2b817dc922fd7f331cd1ece8a/app-purescript/Entry.purs#L279) to Haskell
withIndex
:: forall s t a b f. Functor f -- Monad f
=> ((a -> StateT Int f b) -> (s -> StateT Int f t))
-> (Int -> a -> f b) -> (s -> f t)
withIndex lens f = (`myEvalStateT` 0) . lens f'
where
f' :: a -> StateT Int f b
f' y = StateT \i -> (\z -> (z, i+1)) <$> f i y