tags:

views:

119

answers:

2

I'm a Haskell newbie, and I'm trying to get the wai package working (because I'm interested in using Haskell for web applications). I tried to start with the first, simplest example from the wai homepage:

[ 1] {-# LANGUAGE OverloadedStrings #-}
[ 2] import Network.Wai
[ 3] import Network.Wai.Enumerator (fromLBS)
[ 4] import Network.Wai.Handler.SimpleServer (run)
[ 5] 
[ 6] app :: Application
[ 7] app _ = return Response
[ 8]     { status          = status200
[ 9]     , responseHeaders = [("Content-Type", "text/plain")]
[10]     , responseBody    = ResponseLBS "Hello, Web!"
[11]     }
[12] 
[13] main :: IO ()
[14] main = do
[15]     putStrLn $ "http://localhost:8080/"
[16]     run 8080 app

When I run the code above (with runhaskell), I get the following error:

wapp.hs:10:36: No instance for (Data.String.IsString Data.ByteString.Lazy.Internal.ByteString) arising from the literal `"Hello, Web!"' at wapp.hs:10:36-48

Possible fix: add an instance declaration for (Data.String.IsString Data.ByteString.Lazy.Internal.ByteString)

In the first argument of ResponseLBS', namely"Hello, Web!"'

In the `responseBody' field of a record

In the first argument of `return', namely

`Response
 {status = status200,
  responseHeaders = [("Content-Type", "text/plain")],
  responseBody = ResponseLBS "Hello, Web!"}'

Is it something wrong with the example (I don't think so, because it's from the wai homepage - it should be correct!), or is it something wrong with my Haskell system?

A: 

Are you using hugs or ghc? At the bottom of the wai webpage is the item titled `Doing without overloaded strings' that seems to address your problem. Try:

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as LB8

notFound = Response
    { status          = Status404
    , responseHeaders = [("Content-Type", B8.pack "text/plain")]
    , responseBody    = Right $ fromLBS $ LB8.pack "404 - Not Found"
    }
ShiDoiSi
I use ghc. I've tried that, but I get the following error:wapp.hs:12:26: Couldn't match expected type `CIByteString' against inferred type `[Char]' Expected type: ResponseHeader Inferred type: [Char] In the expression: "Content-Type" In the expression: ("Content-Type", B8.pack "text/plain")
TPJ
+4  A: 

You need to import modules that export IsString instances for the types you want to use as overloaded strings. It looks like you're not importing any module that exports an IsString instance for lazy bytestrings. Try adding this import to your code:

import Data.ByteString.Lazy.Char8
Carl
Thanks a lot! After I added the line you suggested, ghc detected ambiguous occurence of putStrLn, so I changed putStrLn in the example to Prelude.putStrLn, and everything started to work :-)
TPJ
You could also change the import to import Data.ByteString.Lazy.Char8(), which would import the instance you need, but none of the conflicting names.
Carl