[Haskell-beginners] How to interpret the definition of MonadState
es at ertes.de
Tue Jun 7 21:55:29 CEST 2011
djp at arqux.com wrote:
> >> class Monad m => MonadState s m | m -> s where
> >> It is the vertical bar | that confuses me. What does that mean? How
> >> would one read this line in "natural language", say English?
> > The vertical bar signifies a functional dependency (cf. the ghc
> > users' guide for details). It says here, that the monad determines
> > the type of the state.
> Does that mean it's ghc specific, i.e. not in Haskell 98 / 2010?
It's a type system extension, which is not in any standard. But
associated types (a special kind of type families) serve essentially the
same purpose in a cleaner way.
class MonadState m where
type StateOf m
get :: m (StateOf m)
put :: StateOf m -> m ()
instance MonadState (State s) where
type StateOf (State s) = s
This is equivalent to the definition with fundeps. You can find
TF-based monad transformer libraries. Their package names usually end
with "-tf" to indicate this.
nightmare = unsafePerformIO (getWrongWife >>= sex)
More information about the Beginners