Proposal: add 'state' to the MonadState class

Edward Kmett ekmett at gmail.com
Mon Sep 26 19:57:34 CEST 2011


Yes.

On Mon, Sep 26, 2011 at 11:58 AM, Antoine Latter <aslatter at gmail.com> wrote:

> On Mon, Sep 26, 2011 at 9:50 AM, Edward Kmett <ekmett at gmail.com> wrote:
> > On Mon, Sep 26, 2011 at 10:41 AM, Antoine Latter <aslatter at gmail.com>
> wrote:
> >>
> >> On Mon, Sep 26, 2011 at 9:25 AM, Twan van Laarhoven <twanvl at gmail.com>
> >> wrote:
> >> > Hello list,
> >> >
> >> >
> >> > Currently the 'state' function is only defined for the State type (aka
> >> > StateT Identity):
> >> >
> >> >    state :: (s -> (a, s)) -> State s a
> >> >    state f = StateT (Identity . f)
> >> >
> >> > But this function makes sense for all MonadState instances. I
> therefore
> >> > propose to either add it to the MonadState class:
> >> >
> >> >    class MonadState s m | m -> s where
> >> >        ...
> >> >        state :: (s -> (a,s)) -> m a
> >> >        state f = do s <- get
> >> >                     let (a,s') = f s
> >> >                     set s'
> >> >                     return a
> >> >
> >
> > I am 100% behind this change.
> > It has been on the short list of changes that I want to make to mtl.
> > The main reason that I like it aside from the fact that it witnesses the
> > canonical monad homomorphism from State is that it yields more efficient
> > definitions for functions like modify, and the often-needed but
> needlessly
> > expensive operations where you bump a counter _and_ want the result for
> > something like a fresh variable supply.
> > I would like to encourage further public discussion.
>
> If 'modify' is re-written to use this proposed class method, does that
> mean I can finally make "modify" safe to use in "BackwardStateT"?
>
> Not that I actually use "BawkardStateT", but it is a fun demo.
>
> Antoine
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20110926/72b6e8bf/attachment-0001.htm>


More information about the Libraries mailing list