Haskell Ambiguous Occurrences -- how to avoid?
dictionary, haskell
Solution
The types are clearly different but Haskell doesn't allow ad-hoc overloading of names, so you can only choose one `lookup` to be used without a prefix.
The typical solution is to import `Data.Map` qualified:
> import qualified Data.Map as Map
Then you can say
> lookup 1 [(1,2), (3,4)]
Just 2
> Map.lookup 1 Map.empty
Nothing
Usually, Haskell libraries either avoid re-using names from the Prelude, or else re-use a whole bunch of them. `Data.Map` is 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.lookup` without the prefix, you have to hide `Prelude.lookup` since it's implicitly imported otherwise:
import Prelude hiding (lookup)
import Data.Map (lookup)
This is a bit weird but might be useful if you use `Data.Map.lookup` a whole bunch and your data structures are all maps, never alists.
Problem
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 ```