on master help sign in
an Applicative instance for StateT s f where f is a Monad.

I'm not sure whether it's possible to do it where f is just an Applicative, but I'd love to be proven wrong.

commit455b962ae954e1f5a0ffdf8d40ed307409c6f00b authorstartling <tdixon51793@gmail.com> dateMon 19 Nov 2012, 13:17 (14 years ago) parent015200a
1 file changed+7
lib/Control/Monad/State.idr+7
lib/Control/Monad/State.idr+7
@@ -19,6 +19,13 @@ instance Functor f => Functor (StateT s f) where
1919 mapFst : (a -> x) -> (a, b) -> (x, b)
2020 mapFst fn (a, b) = (fn a, b)
2121
22+instance Monad f => Applicative (StateT s f) where
23+ pure x = ST (\st => pure (x, st))
24+
25+ (ST f) <$> (ST a) = ST (\st => do (g, r) <- f st
26+ (b, t) <- a r
27+ return (g b, t))
28+
2229instance Monad m => Monad (StateT s m) where
2330 return x = ST (\st => return (x, st))
2431