1

I have a little problem to understand an error message in haskell.

For instance:

import qualified Data.Map as M

test = M.empty

This code runs as it should do without getting any error message.

The output looks like:

*Main> test

fromList []

But if I try something like that

import qualified Data.Map as M

test = do print M.empty

I get an error message like this

Ambiguous type variable `k0' in the constraint:
      (Show k0) arising from a use of `print'
    Probable fix: add a type signature that fixes these type variable(s)
    In a stmt of a 'do' block: print M.empty
    In the expression: do { print M.empty }
    In an equation for `test': test = do { print M.empty }

So I think it has something to do with the print statement.

But if I try it in the console (ghci)

Prelude Data.Map> print empty
fromList []

everything works fine.

So I hope someone can explain me where the problem is.

Thanks in advance.

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
jimmyt
  • 491
  • 4
  • 10

2 Answers2

7

This code runs as it should do without getting any error message.

In a source file, it shouldn't.

import qualified Data.Map as M

test = M.empty

The inferred type of test is Ord k => Map k a, a polymorphic type with a constrained type variable. Since test is not a function and has no type signature, by the monomorphism restriction, its type must be made monomorphic by resolving the constrained type variables to a default type. Since the only constraint here is Ord, the defaulting rules forbid that type variable to be defaulted (there must be at least one numeric constraint for defaulting to be allowed).

Thus, compilation is required to fail by the language standard.

In ghci, however, there are extended defaulting rules that allow to default the type. If you want to print test, a further Show constraint is introduced on both type variables, and ghci defaults the type of test to Map () () when asked to print it.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
0

This is because Data.Map.empty has the type Map k a. A map of keys of type k to values, type a.

print on the other hand has the type print :: Show a => a -> IO (), which means that it can only display types that are instances of Show, while M.empty has type Map k a, has no such constraint. It can be any type k or a -- it is not required to be able to show them.

So, basically, print doesn't know what type it's being asked to display.

As for why it works in ghci; I'm not entirely sure. Maybe one of the resident Haskell wizards can shed some light on that.

identity
  • 949
  • 6
  • 14
  • For me it is totally strange because for instance this code `import qualified Data.Map as M test :: IO () test = do let l1 = [(1,2), (2,3), (3,4)] t1 = M.fromList l1 t2 = M.empty print $ t1 print $ M.fromList[(1,2)]` – jimmyt Jul 24 '12 at 15:54
  • Sorry, I pressed the return key too quickly. I just wanted to say that this code runs without errors. But if I add a line `print $ t2` to the code I get again an error. ;) – jimmyt Jul 24 '12 at 15:57