newbieQ: LYH13.1 tell?

7 views
Skip to first unread message

TJ

unread,
Jan 13, 2012, 1:05:40 AM1/13/12
to pdxfunc
Using GHCi, version 7.0.4, a sample code below from "Learn You a
Haskell.." Chap 13 fails:

============ code ====================
import Data.Monoid
import qualified Control.Monad.Writer

newtype Writer w a = Writer { runWriter :: (a, w) }

instance (Monoid w) => Monad (Writer w) where
return x = Writer (x, mempty)
(Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v
`mappend` v')

logNumber :: Int -> Writer [String] Int
logNumber x = Writer (x, ["Got number: " ++ show x])

multWithLog :: Writer [String] Int
multWithLog = do
a <- logNumber 3
b <- logNumber 5
tell ["Gonna multiply these two"]
return (a*b)
============ code ====================

> :l LYH13
[1 of 1] Compiling Main ( LYH13.hs, interpreted )
Failed, modules loaded: none.

LYH13.hs:17:5: Not in scope: `tell'
>
> :m +Control.Monad.Writer
> :info tell
class (Monoid w, Monad m) => MonadWriter w m | m -> w where
tell :: w -> m ()
...
-- Defined in Control.Monad.Writer.Class
>


Why?

TJ


Shachaf Ben-Kiki

unread,
Jan 13, 2012, 1:58:50 AM1/13/12
to pdx...@googlegroups.com
* You should make up your mind on what you're doing: Are you using
Control.Monad.Writer's Writer, or defining your own?
* In the former case, you should either not import it qualified or
refer to the qualified name;
* In the latter case, you're going to have to define your own
"tell". It doesn't look like LYAH defines it, so it'll make a good
exercise.
* At any rate, you can figure out the problem pretty easily from
the error: GHC tells you that "tell" isn't in scope, so you have to
figure out where it should come from. In this case the answer is
"nowhere".
* #haskell on freenode is a good place to ask these sorts of questions
if you want a more interactive response. :-)

Shachaf

> --
> You received this message because you are subscribed to the Google Groups "pdxfunc" group.
> To post to this group, send email to pdx...@googlegroups.com.
> To unsubscribe from this group, send email to pdxfunc+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pdxfunc?hl=en.
>

TJ

unread,
Jan 13, 2012, 3:51:53 AM1/13/12
to pdxfunc
Shachaf, thank you for your reply.

I assume LYAH intends to use Control.Monad.Writer's Writer.
However, if we don't use "qualified" as in the book,
then we have three errors below:

LYH.hs:6:31:
Ambiguous occurrence `Writer'
It could refer to either `Main.Writer', defined at a.hs:6:9
or `Control.Monad.Writer.Writer',
imported from Control.Monad.Writer at
LHY.hs:1:1-27
...
[two more errors that are similar to this]

I thought that's why "qualified" was necessary.
Isn't "tell" of Control.Monad.Writer qualified?
I am still puzzled.

BTW, sorry if I'm not suppose to ask this kind of question here.
If it isn't appropriate to continue, let me know. I'll move out.

Regards,
TJ

Shachaf Ben-Kiki

unread,
Jan 13, 2012, 3:57:38 AM1/13/12
to pdx...@googlegroups.com
On Fri, Jan 13, 2012 at 00:51, TJ <tj.t...@gmail.com> wrote:
> Shachaf, thank you for your reply.
>
> I assume LYAH intends to use Control.Monad.Writer's Writer.
> However, if we don't use "qualified" as in the book,
> then we have three errors below:
>
> LYH.hs:6:31:
>    Ambiguous occurrence `Writer'
>    It could refer to either `Main.Writer', defined at a.hs:6:9
>                          or `Control.Monad.Writer.Writer',
>                             imported from Control.Monad.Writer at
> LHY.hs:1:1-27
> ...
> [two more errors that are similar to this]

I haven't looked into LYAH's text very deeply, but I don't think
you're supposed to take its code verbatim; it looked to me like it was
first demonstrating how Control.Monad.Writer worked and then giving
part of its implementaion.

> I thought that's why "qualified" was necessary.
> Isn't "tell" of Control.Monad.Writer qualified?
> I am still puzzled.

I'm not sure what you mean by "qualified" here. The word "qualified"
is also never mentioned in that chapter, so I assume you added it. You
should probably read more about what it does and how Haskell imports
work.

> BTW, sorry if I'm not suppose to ask this kind of question here.
> If it isn't appropriate to continue, let me know. I'll move out.

I have no idea what you're supposed to do or not supposed to do here
-- I've never even been to PDXFunc (though I was planning on going
this one time!). I was more proposing asking in #haskell for your own
benefit.

Shachaf

Reply all
Reply to author
Forward
0 new messages