I do the following in GHCI:
:m + Data.Map
let map = fromList [(1, 2)]
lookup 1 map
GHCI knows that map is a (Map Integer Integer). So why does it claim an ambiguity between Prelude.lookup and Data.Map.lookup when the type is clear and can I avoid?
<interactive>:1:0:
Ambiguous occurrence `lookup'
It could refer to either `Prelude.lookup', imported from Prelude
or `Data.Map.lookup', imported from Data.Map
> :t map
map :: Map Integer Integer
> :t Prelude.lookup
Prelude.lookup :: (Eq a) => a -> [(a, b)] -> Maybe b
> :t Data.Map.lookup
Data.Map.lookup :: (Ord k) => k -> Map k a -> Maybe a
The types are clearly different but Haskell doesn’t allow ad-hoc overloading of names, so you can only choose one
lookupto be used without a prefix.The typical solution is to import
Data.Mapqualified:Then you can say
Usually, Haskell libraries either avoid re-using names from the Prelude, or else re-use a whole bunch of them.
Data.Mapis one of the second, and the authors expect you to import it qualified.[Edit to include ephemient’s comment]
If you want to use
Data.Map.lookupwithout the prefix, you have to hidePrelude.lookupsince it’s implicitly imported otherwise:This is a bit weird but might be useful if you use
Data.Map.lookupa whole bunch and your data structures are all maps, never alists.