I recently started playing with haskell, and I ran into a problem when using HashMap, which can be illustrated by the example of this toy:
import Data.HashMap as HashMap foo = HashMap.insert 42 53 HashMap.empty
Here is the error I get when I upload a file to the interpreter or compile it:
Prelude List HashMap> :load TypeError.hs [1 of 1] Compiling Main ( TypeError.hs, interpreted ) TypeError.hs:3:22: Ambiguous type variable `k0' in the constraints: (Num k0) arising from the literal `42' at TypeError.hs:3:22-23 (Ord k0) arising from a use of `insert' at TypeError.hs:3:7-20 (Data.Hashable.Hashable k0) arising from a use of `insert' at TypeError.hs:3:7-20 Possible cause: the monomorphism restriction applied to the following: foo :: Map k0 Integer (bound at TypeError.hs:3:1) Probable fix: give these definition(s) an explicit type signature or use -XNoMonomorphismRestriction In the first argument of `insert', namely `42' In the expression: insert 42 53 empty In an equation for `foo': foo = insert 42 53 empty Failed, modules loaded: none. Prelude List HashMap>
However, if I define the same function directly in the interpreter, I get no error:
Prelude List HashMap> let foo = HashMap.insert 42 53 HashMap.empty Prelude List HashMap>
Does anyone know about this?
Thanks.
source share