Search code examples
haskellscotty

Ambiguous type variable ‘a0’ arising from a use of ‘param’ prevents the constraint ‘(Parsable a0)’ from being solved


I'm having trouble understanding this error message, not too sure what I should investigate next.

I've got the following imports:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Web.Scotty

import Control.Applicative
import Control.Monad.IO.Class
import Database.SQLite.Simple
import Database.SQLite.Simple.FromRow
import qualified Data.Text.Lazy as L

The code causing the error:

routes :: ScottyM ()
routes = do
  post "/create" $ do
    f <- param ("fa" :: L.Text)
    file "create.html"

And the error:

    • Ambiguous type variable ‘a0’ arising from a use of ‘param’
  prevents the constraint ‘(Parsable a0)’ from being solved.
  Probable fix: use a type annotation to specify what ‘a0’ should be.
  These potential instances exist:
    instance Parsable Integer
      -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’
    instance Parsable L.Text
      -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’
    instance Parsable ()
      -- Defined in ‘scotty-0.11.0:Web.Scotty.Action’
    ...plus 7 others
    ...plus 12 instances involving out-of-scope types
    (use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: f <- param ("f" :: L.Text)
  In the second argument of ‘($)’, namely
    ‘do { f <- param ("f" :: L.Text);
          file "create.html" }’
  In a stmt of a 'do' block:
    post "/create"
    $ do { f <- param ("f" :: L.Text);
           file "create.html" }

Solution

  • param has type Parseable a => Text -> ActionM a, which means f would have type a. However, you never do anything with f, so there is no way to infer what type a should be. Assuming you don't want to just comment the line out until you do use f, you need to provide an explicit type:

    routes :: ScottyM ()
    routes = do
      post "/create" $ do
        f <- param ("fa" :: L.Text) :: ActionM L.Text
        file "create.html"
    

    You could probably pick any type that has a Parseable instance, but L.Text seems like the type it should be. Once you actually use f, you can probably remove the explicit annotation.