0

I've been following this Happstack.Lite tutorial: http://www.happstack.com/page/view-page-slug/9/happstack-lite-tutorial with much success but I can't seem to get past this compile error:

$ ghc crashcourse.hs -o crashcourse
[1 of 1] Compiling Main             ( crashcourse.hs, crashcourse.o )

crashcourse.hs:52:21:
    Illegal type signature: `String'
      Perhaps you intended to use -XScopedTypeVariables
    In a pattern type-signature

The code in question:

echo :: ServerPart Response
echo =
    path $ \(msg :: String) ->
        ok $ template "echo" $ do
          p $ "echo says: " >> toHtml msg
          p "Change the url to echo something else."

The code in context: (The {-# LANGUAGE OverloadedStrings #-} pragma has no effect on this problem, it was added to solve something else)

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Applicative ((<$>), optional)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text.Lazy (unpack)
import Happstack.Lite
import Text.Blaze.Html5 (Html, (!), a, form, input, p, toHtml, label)
import Text.Blaze.Html5.Attributes (action, enctype, href, name, size, type_, value)
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A

main :: IO ()
main = serve Nothing myApp

myApp :: ServerPart Response
myApp = msum
  [ dir "echo"    $ echo
   ,dir "query"   $ queryParams
--  , dir "form"    $ formPage
--  , dir "fortune" $ fortune
--  , dir "files"   $ fileServing
--  , dir "upload"  $ upload
  , homePage
  ]

template :: Text -> Html -> Response
template title body = toResponse $
  H.html $ do
    H.head $ do
      H.title (toHtml title)
    H.body $ do
      body
      p $ a ! href "/" $ "back home"

homePage :: ServerPart Response
homePage =
    ok $ template "home page" $ do
           H.h1 "Hello!"
           H.p "Writing applications with happstack-lite is fast and simple!"
           H.p "Check out these killer apps."
           H.p $ a ! href "/echo/secret%20message" $ "echo"
           H.p $ a ! href "/query?foo=bar" $ "query parameters"
           H.p $ a ! href "/form" $ "form processing"
           H.p $ a ! href "/fortune" $ "(fortune) cookies"
           H.p $ a ! href "/files" $ "file serving"
           H.p $ a ! href "/upload" $ "file uploads"

echo :: ServerPart Response
echo =
    path $ \(msg :: String) ->
        ok $ template "echo" $ do
          p $ "echo says: " >> toHtml msg
          p "Change the url to echo something else."

queryParams :: ServerPart Response
queryParams =
    do mFoo <- optional $ lookText "foo"
       ok $ template "query params" $ do
         p $ "foo is set to: " >> toHtml (show mFoo)
         p $ "change the url to set it to something else."

I have succeeded in executing this from here:

module Main where

import Control.Monad    (msum)
import Happstack.Server (nullConf, simpleHTTP, ok, dir, path, seeOther)

main :: IO ()
main = simpleHTTP nullConf $
  msum [ dir "hello" $ path $ \s -> ok $ "Hello, " ++ s
       , seeOther "/hello/Haskell" "/hello/Haskell"
       ]

Your help would be greatly appreciated!

Kevin Pluck
  • 641
  • 5
  • 14

1 Answers1

2

As mentioned in the happstack-lite tutorial you need the following pragmas:

{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}

If I add ScopedTypeVariablesit works for me.

Joel Hermanns
  • 355
  • 1
  • 4
  • 11
  • Cheers! As you can possibly tell very much a n00b here. Will see if I can get the tutorial to reformat the pragmas bit as I completely missed the statement. I'm not thinking that Happstack.Lite is particularly light if I have to add all these pragmas and includes. – Kevin Pluck Aug 24 '14 at 11:30
  • `OverloadedStrings` is pretty commonly used for everything to do with text. All web frameworks use it. Happstack-Lite is just a subset of the most commonly used Happstack features. It's not really more light weight. As a beginner myself, I'm also trying out web frameworks. The reason why I'm using Happstack instead of scotty or spock it the available documentation. – orkoden Feb 18 '15 at 12:14