<div dir="ltr">On Wed, Feb 15, 2017 at 12:01 PM, Laurent Christophe <span dir="ltr"><<a href="mailto:lachrist@vub.ac.be" target="_blank">lachrist@vub.ac.be</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi guys, the way `StateT` are implemented as `Applicative` have been buggling my mind for some time.</div><div><a href="https://hackage.haskell.org/package/transformers-0.5.2.0/docs/src/Control.Monad.Trans.State.Lazy.html#line-201" target="_blank">https://hackage.haskell.org/<wbr>package/transformers-0.5.2.0/<wbr>docs/src/Control.Monad.Trans.<wbr>State.Lazy.html#line-201</a></div><div><br></div><div><font face="Courier">instance (Functor m, Monad m) => Applicative (StateT s m) where</font></div><div><font face="Courier"> pure a = StateT $ \ s -> return (a, s)</font></div><div><font face="Courier"> StateT mf <*> StateT mx = StateT $ \ s -> do</font></div><div><font face="Courier"> (f, s') <- mf s</font></div><div><font face="Courier"> (x, s'') <- mx s'</font></div><div><font face="Courier"> return (f x, s'')</font></div><div><br></div><div>Using dependant monadic computations, this implementation cannot be expressed in term of applicative.</div><div>This explains why we cannot have `instance (Applicative m) => Applicative (State s m)`.</div><div>However using real monadic style computations for implementing `<*>` buggles my mind.</div><div>Moreover `liftA2 (<*>)` can be used to generically compose applicative functors so why monads are needed?</div><div><a href="https://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers" target="_blank">https://www.haskell.org/<wbr>haskellwiki/Applicative_<wbr>functor#Applicative_<wbr>transfomers</a></div></div></blockquote><div><br></div><div>StateT s m is not a composition of applicative functors. It allows two-way communication between the state and the underlying monad m.</div><div><br></div><div>Like Compose m (State s), effects in m can affect how the state evolves. Like Compose (State s) m, the state can influence what effects occur in m.</div><div><br></div><div>For example, StateT s Maybe will discard changes to the state if a subcomputation returns Nothing and permits subcomputations to choose whether to return Nothing based on the state. No applicative composition of State s and Maybe can do both.</div><div><br></div><div>These limitations are implied by the underlying types:</div><div><br></div><div><div>Compose m (State s) a = m (s -> (a, s))</div><div>Compose (State s) m a = s -> (m a, s)</div><div>StateT s m a = s -> m (a, s)</div></div><div><br></div></div><div><br></div>-- <br><div class="gmail_signature">Dave Menendez <<a href="mailto:dave@zednenem.com" target="_blank">dave@zednenem.com</a>><br><<a href="http://www.eyrie.org/~zednenem/" target="_blank">http://www.eyrie.org/~zednenem/</a>></div>
</div></div>