Search code examples
haskellhappstack

Haskell Happstack


trying to use happstack, got it to install correctly on windows but now it's chucking out some errors when I compile my test class any input would be appreciated.

module Main where

import Happstack.Server
import           Text.Blaze ((!))
import qualified Text.Blaze.Html4.Strict as H
import qualified Text.Blaze.Html4.Strict.Attributes as A

appTemplate :: String -> [H.Html] -> H.Html -> H.Html
appTemplate title headers body =
H.html $ do
  H.head $ do
    H.title (H.toHtml title)
    H.meta ! A.httpEquiv "Content-Type"
           ! A.content "text/html;charset=utf-8"
    sequence_ headers
  H.body $ do
    body

helloBlaze :: ServerPart Response
helloBlaze =
ok $ toResponse $
appTemplate "Hello, Blaze!"
            [H.meta ! A.name "keywords"
                    ! A.content "happstack, blaze, html"
            ]
            (H.p $ do "Hello, "
                      H.b "blaze-html!")

main :: IO ()
main = simpleHTTP nullConf $ helloBlaze

I created a happstack folder and used cabal to install the required files to this folder, but when I compile the code I get the errors below.

Main.hs:13:30:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.httpEquiv’, namely ‘"Content-Type"’
In the second argument of ‘(!)’, namely
  ‘A.httpEquiv "Content-Type"’
In the first argument of ‘(!)’, namely
  ‘H.meta ! A.httpEquiv "Content-Type"’

Main.hs:14:28:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.content’, namely
  ‘"text/html;charset=utf-8"’
In the second argument of ‘(!)’, namely
  ‘A.content "text/html;charset=utf-8"’
In a stmt of a 'do' block:
  H.meta ! A.httpEquiv "Content-Type"
  ! A.content "text/html;charset=utf-8"

Main.hs:23:34:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.name’, namely ‘"keywords"’
In the second argument of ‘(!)’, namely ‘A.name "keywords"’
In the first argument of ‘(!)’, namely ‘H.meta ! A.name "keywords"’

Main.hs:24:37:
Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
In the first argument of ‘A.content’, namely
  ‘"happstack, blaze, html"’
In the second argument of ‘(!)’, namely
  ‘A.content "happstack, blaze, html"’
In the expression:
  H.meta ! A.name "keywords" ! A.content "happstack, blaze, html"

Main.hs:26:27:
 Couldn't match type ‘[]’ with ‘Text.Blaze.Internal.MarkupM’

 Expected type: Text.Blaze.Internal.MarkupM Char
   Actual type: [Char]
 In a stmt of a 'do' block: "Hello, "
 In the second argument of ‘($)’, namely
   ‘do { "Hello, ";
         H.b "blaze-html!" }’
 In the third argument of ‘appTemplate’, namely
   ‘(H.p
     $ do { "Hello, ";
            H.b "blaze-html!" })’

Main.hs:27:31:
Couldn't match type ‘[Char]’ with ‘Text.Blaze.Internal.MarkupM ()’
Expected type: H.Html
  Actual type: [Char]
In the first argument of ‘H.b’, namely ‘"blaze-html!"’
In a stmt of a 'do' block: H.b "blaze-html!"
In the second argument of ‘($)’, namely
  ‘do { "Hello, ";
        H.b "blaze-html!" }’

Solution

  • In the lines like this:

    Couldn't match expected type ‘H.AttributeValue’
            with actual type ‘[Char]’
    

    compiler complains that it expected to receive H.AttributeValue but got [Char].

    AttributeValue is defined here. As you can see, it is an instance of IsString, that means that you can make use of OverloadedStrings which is a language extension. To enable it put

    {-# LANGUAGE OverloadedStrings #-}
    

    at the top of your file.

    This will rewrite fragments like this:

    A.httpEquiv "Content-Type"
    

    to something like this:

    A.httpEquiv (fromString "Content-Type")
    

    and fromString comes from IsString class which is defined here, as:

    class IsString a where
        fromString :: String -> a