
I'm trying to learn to use the Writer Monad, but I'm having trouble with the following very simple program: import Control.Monad.Writer foo :: Writer String Int foo = tell "hello" Basically, I was trying to figure out the 'tell' function, so I want foo to return ((), "hello"). But I get this error: Couldn't match `Int' against `()' Expected type: Writer String Int Inferred type: Writer String () In the application `tell "hello"' In the definition of `foo': foo = tell "hello" What am I supposed to do? Write my own version of tell for Writer String Int? How exactly do I do this? -- View this message in context: http://www.nabble.com/writer-monad-help-tf3777133.html#a10680445 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

iskaldur wrote:
I'm trying to learn to use the Writer Monad, but I'm having trouble with the following very simple program:
import Control.Monad.Writer foo :: Writer String Int foo = tell "hello"
Basically, I was trying to figure out the 'tell' function, so I want foo to return ((), "hello").
You want foo to return ((),"hello")? Well, then, it has type Writer String (), not Writer String Int :-)
But I get this error: Couldn't match `Int' against `()' Expected type: Writer String Int Inferred type: Writer String () In the application `tell "hello"' In the definition of `foo': foo = tell "hello"
What am I supposed to do? Write my own version of tell for Writer String Int? How exactly do I do this?
tell doesn't return anything. So tell "hello" has type Writer String (). There isn't anything sensible for it to return, is there? If you want foo to have the type Writer String Int you might try: foo = tell "hello" >> return (5*5) a.k.a. foo = do tell "hello" return (5*5) Remember the 'return type' of a monad isn't somehow 'fixed' so you're stuck for it. Each statement can return a different type (and all the values are ignore unless you bind them with a "x <-"). The 'return type' of the entire expression is simply whatever is returned by the last statement. Hope that helps, Jules

On May 18, 2007, at 7:32 , iskaldur wrote:
import Control.Monad.Writer foo :: Writer String Int foo = tell "hello"
foo = tell "hello" >> return 1 -- or whatever You declared it to return Int, you should probably produce an Int somewhere.... -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

if by "figure out" you meant to "do what tell does without using it", you
should have written:
foo = Writer ((),"hello")
that would have had the right type, using return you are doing something
different.
in the Writer monad return :: Monoid w => a -> Writer w a
so return ((),"hello") :: Monoid w => Writer w ((),String)
in fact in this case return a = Writer (a,mempty)
so return ((),"hello") == Writer (((),"hello"),mempty)
in general you can't "mess" with the internals of a monad using return, it's
only meant to lift a pure value in that monad.
That's also a reason because tell is a typeclass method, you need to know
how your specific monad data type work to implement it.
full source:
http://darcs.haskell.org/packages/mtl/Control/Monad/Writer/Lazy.hs
2007/5/18, iskaldur
I'm trying to learn to use the Writer Monad, but I'm having trouble with the following very simple program:
import Control.Monad.Writer foo :: Writer String Int foo = tell "hello"
Basically, I was trying to figure out the 'tell' function, so I want foo to return ((), "hello").
But I get this error: Couldn't match `Int' against `()' Expected type: Writer String Int Inferred type: Writer String () In the application `tell "hello"' In the definition of `foo': foo = tell "hello"
What am I supposed to do? Write my own version of tell for Writer String Int? How exactly do I do this? -- View this message in context: http://www.nabble.com/writer-monad-help-tf3777133.html#a10680445 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Andrea Vezzosi
-
Brandon S. Allbery KF8NH
-
iskaldur
-
Jules Bean