[Haskell-beginners] class (Monad m) => MonadState s m | m -> s where - What does m | m -> s construct do?

Daniel Fischer daniel.is.fischer at googlemail.com
Mon Apr 11 17:44:46 CEST 2011

On Monday 11 April 2011 17:18:44, Amitava Shee wrote:
> I can across the following construct in ghc source
> class (Monad m) => MonadState s m | m -> s where
>     get :: m s
>     put :: s -> m ()
> I am not aware of the syntactic construct  | m -> s just before the
> where? Can someone please explain it?

It introduces a "functional dependency" (look up FunctionalDependencies in 
the GHC users' guide, section 7.??).

The '|' separates the instance head (MonadState s m) from the functional 
dependenc(y|ies). Here there's one FunDep, m -> s, saying the class 
parameter m (the monad) determines the class parameter s (the state).
So for each monad m, there can be at most one state type s such that m is a 
'stateful monad' with state s.

If you're already familiar with type families, the remark that this 
functional dependency is roughly equivalent to

class (Monad m) => MonadState m where
    type MState m :: *
    get :: m (MState m)
    put :: MState m -> m ()

might be helpful.

> Thanks & Regards,
> Amitava Shee

More information about the Beginners mailing list