[Haskell-beginners] no instance for monad

Emmanuel Touzery etouzery at gmail.com
Sat Oct 20 20:24:29 CEST 2012


Hello,

 in my program i have a function which can be simplified like this:

 writeProgramInfo :: Handle -> ProgramInfo -> IO ()
 writeProgramInfo handle program = do
        hPrintf handle "hello"

This is using the hPrintf from Text.Printf.

Unfortunately that hPrintf works on String parameters which is not good
enough because I need unicode character support (if I use unpack it works
on linux but complains about invalid characters on windows, I guess because
my locale on linux is utf-8 but on windows it's a local codepage). So I
found the Text.Format library:
http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html

 Which appears to solve exactly that problem (out of the box Text support).
However I can't just install the library, add the import statements, and
replace hPrintf by TF.hprint and change the format string, that doesn't
work; if I change this function to:

import qualified Data.Text.Format as TF

writeProgramInfo :: Handle -> ProgramInfo -> IO ()
writeProgramInfo handle program = do
        TF.hprint handle "hello"

I get this error message at build time:

Couldn't match expected type `IO ()'
                with actual type `ps0 -> m0 ()'
    In the return type of a call of `TF.hprint'
    In the expression: TF.hprint handle "hello"
    In the expression: do { TF.hprint handle "hello" }

Now this clearly results from the type of hprint, which is:
hprint :: (MonadIO<http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Control-Monad-IO-Class.html#t:MonadIO>m,
Params<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format-Params.html#t:Params>ps)
=>
Handle<http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/GHC-IO-Handle.html#t:Handle>->
Format<http://hackage.haskell.org/packages/archive/text-format/0.3.0.7/doc/html/Data-Text-Format.html#t:Format>->
ps -> m
()<http://hackage.haskell.org/packages/archive/ghc-prim/0.2.0.0/doc/html/GHC-Unit.html#t:-40--41->

 while the type of hPrintf is:
hPrintf<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#v%3AhPrintf>::
HPrintfType<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Text-Printf.html#t%3AHPrintfType>r
=>
Handle<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-Handle.html#t%3AHandle>->
String<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Data-Char.html#t%3AString>->
r

 with the precision: The return type is restricted to
(IO<http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/System-IO.html#t%3AIO>a)
.

 Well I didn't completely 'get' the monad thing yet. I think I'm pretty
close.. but not there yet. I'm pretty sure understanding this specific
example will take me one step closer... What am I missing here?

 Thank you!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121020/d2617405/attachment.htm>


More information about the Beginners mailing list