Proposal: add 'state' to the MonadState class

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 or as a stand alone function like modify. I personally prefer to add it to the class, since that allows for a more efficient implementation. In particular: instance Monad m => MonadState s (StateT s m) where .... state = StateT (return . f) Discussion period: 2 weeks (ends October 10th) Twan

On Mon, Sep 26, 2011 at 9:25 AM, Twan van Laarhoven
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
or as a stand alone function like modify. I personally prefer to add it to the class, since that allows for a more efficient implementation. In particular:
instance Monad m => MonadState s (StateT s m) where .... state = StateT (return . f)
I think that "mtl" has a real maintainer now - unless something changed from the thread back in June I think Edward K. is doing the duty, so I don't know that the libraries process strictly applies. But that doesn't mean we can't have a discussion in public! Would we make the corresponding change to the "monads-tf" package? I think it is worth keeping their public API in sync to the extent practical, but I don't know if the maintainers of the two packages have a policy on that. Antoine

On Mon, Sep 26, 2011 at 9:41 AM, Antoine Latter
I think that "mtl" has a real maintainer now - unless something changed from the thread back in June I think Edward K. is doing the duty, so I don't know that the libraries process strictly applies.
But that doesn't mean we can't have a discussion in public!
Okay, next time I'll read the process before citing it! The new process is to email a proposal to the list, and CC the package maintainer. http://www.haskell.org/haskellwiki/Library_submissions Antoine

On Mon, Sep 26, 2011 at 10:41 AM, Antoine Latter
On Mon, Sep 26, 2011 at 9:25 AM, Twan van Laarhoven
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.

On Mon, Sep 26, 2011 at 9:50 AM, Edward Kmett
On Mon, Sep 26, 2011 at 10:41 AM, Antoine Latter
wrote: On Mon, Sep 26, 2011 at 9:25 AM, Twan van Laarhoven
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

Yes.
On Mon, Sep 26, 2011 at 11:58 AM, Antoine Latter
On Mon, Sep 26, 2011 at 10:41 AM, Antoine Latter
wrote: On Mon, Sep 26, 2011 at 9:25 AM, Twan van Laarhoven
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
On Mon, Sep 26, 2011 at 9:50 AM, Edward Kmett
wrote: 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

On 26 Sep 2011, at 15:50, Edward Kmett wrote:
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.
(+1)

On 9/26/11 10:25 AM, Twan van Laarhoven wrote:
But this function makes sense for all MonadState instances. I therefore propose to either add it to the MonadState class: [...] or as a stand alone function like modify. I personally prefer to add it to the class, since that allows for a more efficient implementation. In particular:
I'm all for either of the two proposals. If there are efficiency or correctness reasons for wanting it in the class rather than externally defined, then of course that is preferable. -- Live well, ~wren

On 26 September 2011 16:25, Twan van Laarhoven
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
or as a stand alone function like modify. I personally prefer to add it to the class, since that allows for a more efficient implementation. In particular:
instance Monad m => MonadState s (StateT s m) where .... state = StateT (return . f)
Discussion period: 2 weeks (ends October 10th)
Twan
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
+1 What happend to this proposal? Edward, could you also list yourself as maintainer of mtl and list the repository in the cabal file[1]? Cheers, Bas [1] http://www.haskell.org/cabal/users-guide/#source-repositories

On Sat, Jan 21, 2012 at 4:13 PM, Bas van Dijk
What happend to this proposal?
This was my mistake. For some reason I had thought part of this change had belonged in transformers, so I let it get backed up on that, but upon looking back at it, it is entirely an mtl concern, and doesn't require anything out of Ross.
Edward, could you also list yourself as maintainer of mtl and list the repository in the cabal file[1]?
Sure thing. -Edward

On 21 January 2012 22:39, Edward Kmett
On Sat, Jan 21, 2012 at 4:13 PM, Bas van Dijk
wrote: What happend to this proposal?
This was my mistake. For some reason I had thought part of this change had belonged in transformers, so I let it get backed up on that, but upon looking back at it, it is entirely an mtl concern, and doesn't require anything out of Ross.
Edward, could you also list yourself as maintainer of mtl and list the repository in the cabal file[1]?
Sure thing.
-Edward
Great! Bas
participants (6)
-
Antoine Latter
-
Bas van Dijk
-
Edward Kmett
-
Julian Bean
-
Twan van Laarhoven
-
wren ng thornton