Skip to content

Weaken Monad constraints of {eval,exec}StateT to Functor. #95

@sjshuck

Description

@sjshuck

[original issue 94 by @Icelandjack]

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&#39;
  where
    f&#39; :: a -> StateT Int f b
    f&#39; y = StateT \i -> (\z -> (z, i+1)) <$> f i y

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions