<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Given</div><div><br></div><div>StateT mf <*> StateT mx </div><div><br></div><div>mf :: s -> m (a -> b, s)</div><div><br></div><div>mx :: s -> m (a, s)</div><div><br></div><div>We have (s :: s) in scope. We can apply mf to s no problem. Now what do we do with mx? We also need to give it (_ :: s). We could give it the same s we gave mf, but that would be wrong. It would ignore any state changes in mf. </div><div><br></div><div>So the correct option is to use the s value returned by mf. But there's no way to get it out of the monad. You could use the functor properties of m to get</div><div><br></div><div>fmap (\(s, f) -> fmap (\(s, a) -> (s, f a)) (mx s)) (mf s)</div><div><br></div><div>Which has type</div><div><br></div><div>m (m (s, b))</div><div><br></div><div>So you still need a monadic join to get the desired result. </div><div><br></div><div>The applicative instance doesn't really help as far as I can tell because it doesn't let you do anything to things of the form </div><div><br></div><div>x -> m y</div><div><br></div><div>Which is what StateT is made of. </div><div><br></div><div>Will</div><div><br></div><div><br>On Feb 15, 2017, at 11:01 AM, Laurent Christophe <<a href="mailto:lachrist@vub.ac.be">lachrist@vub.ac.be</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div class="">Hi guys, the way `StateT` are implemented as `Applicative` have been buggling my mind for some time.</div><div class=""><a href="https://hackage.haskell.org/package/transformers-0.5.2.0/docs/src/Control.Monad.Trans.State.Lazy.html#line-201" class="">https://hackage.haskell.org/package/transformers-0.5.2.0/docs/src/Control.Monad.Trans.State.Lazy.html#line-201</a></div><div class=""><br class=""></div><div class=""><font face="Courier" class="">instance (Functor m, Monad m) => Applicative (StateT s m) where</font></div><div class=""><font face="Courier" class="">    pure a = StateT $ \ s -> return (a, s)</font></div><div class=""><font face="Courier" class="">    StateT mf <*> StateT mx = StateT $ \ s -> do</font></div><div class=""><font face="Courier" class="">        (f, s') <- mf s</font></div><div class=""><font face="Courier" class="">        (x, s'') <- mx s'</font></div><div class=""><font face="Courier" class="">        return (f x, s'')</font></div><div class=""><br class=""></div><div class="">Using dependant monadic computations, this implementation cannot be expressed in term of applicative.</div><div class="">This explains why we cannot have `instance (Applicative m) => Applicative (State s m)`.</div><div class="">However using real monadic style computations for implementing `<*>` buggles my mind.</div><div class="">Moreover `liftA2 (<*>)` can be used to generically compose applicative functors so why monads are needed?</div><div class=""><a href="https://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers" class="">https://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers</a></div><div class=""><br class=""></div><div class="">Any inputs would be greatly appreciated!</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Laurent</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Haskell-Cafe mailing list</span><br><span>To (un)subscribe, modify options or view archives go to:</span><br><span><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a></span><br><span>Only members subscribed via the mailman list are allowed to post.</span></div></blockquote></body></html>