[Haskell-beginners] different function implementations for different values of typevar - is it possible?

Stephen Tetley stephen.tetley at gmail.com
Sat Jan 23 17:42:38 EST 2010

Hi Sergey


The writer monad is accompanied by a function 'tell' that is called
explicitly to produce output.

'tell' is the 'non-proper morphism' of the writer monad (well the
standard Monad Transformer Library - MTL version has a couple of other
functions but that's an implementation detail). It characterizes the
writer monad in the same way that 'ask' characterises the reader monad
and the combination of get & put characterise the state monad. In
Haskell, you have to represent a Monad with a data type or newtype -
non-proper morphisms need to 'unpeel' the constructor to access the
internals of the monad. Here is the code from MTL for Writers tell:

instance (Monoid w) => MonadWriter w (Writer w) where
    tell   w = Writer ((), w)
    ... (listen and pass - the other non-proper morphisms).

Non-proper morphisms contrast with general monadic functions that are
usable with any monad. Take liftM as an example - liftM needs only
(>>=) and 'return' it doesn't have to look inside a monad's

-- | Promote a function to a monad.
liftM   :: (Monad m) => (a1 -> r) -> m a1 -> m r
liftM f m1              = do { x1 <- m1; return (f x1) }

-- aka. liftM f m1 = m1 >>= \x1 -> return (f x1)

You would want to add a 'tell' function to your module. You're not
obliged to call it 'tell' of course, the MonadLib library, which is an
alternative to the standard MTL library, calls the function 'put'.

Best wishes


More information about the Beginners mailing list