
Supposing I have the following code:
module Main(main) where main = putStr (show [])
I will get these errors from GHC and Hugs respectively:
Main.hs:2:16: Ambiguous type variable `a' in the constraint: `Show a' arising from a use of `show' at Main.hs:2:16-22 Probable fix: add a type signature that fixes these type variable(s)
ERROR "src/Main.hs":2 - Unresolved top-level overloading *** Binding : main *** Outstanding context : Show b
But if I change my code to the following, it will compile.
module Main(main) where main = putStr (show [1])
I have no problems typing in "putStr (show [])" in Hugs... runs fine. So what's wrong? I've really tried hard to think of an explanation, but with no luck.. Thanks, Benjamin

"Benjamin" == Benjamin Bach
writes:
Benjamin> Supposing I have the following code: >> module Main(main) where main = putStr (show []) Benjamin> I will get these errors from GHC and Hugs respectively: >> Main.hs:2:16: Ambiguous type variable `a' in the constraint: >> `Show a' arising from a use of `show' at Main.hs:2:16-22 >> Probable fix: add a type signature that fixes these type >> variable(s) >> ERROR "src/Main.hs":2 - Unresolved top-level overloading *** >> Binding : main *** Outstanding context : Show b Benjamin> But if I change my code to the following, it will Benjamin> compile. >> module Main(main) where main = putStr (show [1]) Benjamin> I have no problems typing in "putStr (show [])" in Benjamin> Hugs... runs fine. So what's wrong? I've really tried Benjamin> hard to think of an explanation, but with no luck.. I would say that it is complaining that it doesn't know what type of empty list you want it to show. of course, they will all display the same at run time, but it's compile time that is the problem. -- Colin Adams Preston Lancashire

On 2 Jan 2009, at 19:57, Benjamin Bach wrote:
Supposing I have the following code:
module Main(main) where main = putStr (show [])
What type is your "[]" here? main :: IO () putStr :: String -> IO () show [] :: String show :: Show a => a -> String Now, how is Hugs or GHCi supposed to know the type of "[]"? The only information it has is that it's type belongs to the class "Show". You may think it's irrelevant, since empty lists are showed the same; but they are not: for example, ([] :: [Char]) would be shown as '""' (empty string).
I will get these errors from GHC and Hugs respectively:
Main.hs:2:16: Ambiguous type variable `a' in the constraint: `Show a' arising from a use of `show' at Main.hs:2:16-22 Probable fix: add a type signature that fixes these type variable(s)
ERROR "src/Main.hs":2 - Unresolved top-level overloading *** Binding : main *** Outstanding context : Show b
But if I change my code to the following, it will compile.
module Main(main) where main = putStr (show [1])
I have no problems typing in "putStr (show [])" in Hugs... runs fine. So what's wrong? I've really tried hard to think of an explanation, but with no luck..
Thanks, Benjamin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

module Main(main) where main = putStr (show [])
What type is your "[]" here? (...) You may think it's irrelevant, since empty lists are showed the same; but they are not: for example, ([] :: [Char]) would be shown as '""' (empty string).
Of course you're right. Didn't know how to type an empty list. "main = putStr (show ([] :: [Char]))" seems so obvious now. Thanks, Benjamin
participants (3)
-
Benjamin Bach
-
Colin Paul Adams
-
Miguel Mitrofanov