Ambiguous occurrence in 'map'

Hello so I’m running a simple function in my ghci map sum $ transpose [[0,3,5,9],[10,0,0,9],[8,5,1,-1]]

and this is what the ghci returns,

<interactive>:12:2: error:
    Ambiguous occurrence ‘map’
    It could refer to
       either ‘Prelude.map’,
              imported from ‘Prelude’ at modules.hs:1:1
              (and originally defined in ‘GHC.Base’)
           or ‘Data.Map.map’,
              imported from ‘Data.Map’ at modules.hs:5:1-15
              (and originally defined in ‘Data.Map.Internal’)

I tried using qualified imports, here’s my code,

import Data.List (nub, sort)  --you can selectively import functions
{-import Data.List hiding (nub)  you're importing all functions accept the nub-}
-- import qualified Data.Map as M  
-- Now, to reference Data.Map's filter function, we just use M.filter
import Data.Map
import qualified Data.Set
  
numUniques :: (Eq a) => [a] -> Int  
numUniques = length . nub  

Any suggestions?

Before answering I would like to know more about your thought process in order to perhaps think of ways we can improve this error message. Are you aware that the Prelude module is imported implicitly? Do you see that the error message mentions that the map function can be found in the Prelude module and in the Data.Map module? Are you aware that you are importing Data.Map unqualified?

Now for the solution: you have an option commented out in your code:

-- import qualified Data.Map as M

Use that and remove the line with the unqualified import: import Data.Map.

3 Likes

well now I am, I’m new to Haskell. I was just following examples from the book, Learn You a Haskell, chapter 7. I just qualified map imports. The error message did indeed improve. Now transpose is not in the scope.

<interactive>:14:12: error:
    Variable not in scope: transpose :: [[a0]] -> [t0 b]

Someone on my team helped me fix it,

import Data.List (nub, sort, transpose)  --you can selectively import functions
{-import Data.List hiding (nub)  you're importing all functions accept the nub-}
-- import qualified Data.Map as M  
-- Now, to reference Data.Map's filter function, we just use M.filter
import qualified Data.Map as M
import qualified Data.Set
  
numUniques :: (Eq a) => [a] -> Int  
numUniques = length . nub  
1 Like

All the functions, types and typeclasses that we’ve dealt with so far were part of the Prelude module, which is imported by default.

LYAH Chp. 7 :stuck_out_tongue:

Are you sure you are not reading the subsection Data.List in Chapter 7? You should import it in similar fashion

import Data.List (nub, sort, transpose)

I used to think that beginners who were running into problems with Haskell just needed to spend some more time learning. Now, I’ve come to believe that the learning resources and the ecosystem make learning Haskell more difficult than it has to be and being a beginner doesn’t have to be this difficult.

So, if you have anything to share about your experience learning Haskell, please do post it somewhere. You can do it here, in @tomjaguarpaw’s tilapia repo, or in the error-messages repo if it is about error messages. I think that is the best way to improve this situation.

In this case, I would point at the GHC error message which doesn’t give a suggestion for how to resolve the problem and at the LYAH book which doesn’t actually show any examples of error messages. That are two points which could be improved in my opinion. I will probably open an issue in the error-messages repo about the first point and maybe one in the tilapia repo about the second point.

4 Likes

I reported this at https://github.com/haskell/error-messages/issues/11

2 Likes