29

First, I created a new workspace:

stack new xxxx
stack init
stack build

then

cd xxx\app
stack ghci
import Data.Map

I can import other modules like Data.Char and Data.List, but I can't import Data.Map. GHCi told me:

Could not find module 'Data.Map'
 It is a member of the hidden package 'containers-0.5.7.1@containers-0.5.7.1'.`
duplode
  • 33,731
  • 7
  • 79
  • 150
AurevoirXavier
  • 2,543
  • 2
  • 17
  • 25

3 Answers3

46

These general steps were helpful for me to resolve similar issues:

  1. Use Hoogle or Stackage to find the package where the module resides

    Note that Hoogle and Stackage are case-sensitive. Looking up Data.Map in Hoogle yields a list similar to the one below. Stackage has a slightly different style, but the basics are the same (mostly because it also uses Hoogle for lookup).

The lines in green under the result headings show the name(s) of the containing

(1) package(s) (in small caps) and

(2) module(s) (capitalized).

Hoogle result for Data.Map

  1. Open project-name.cabal in project root and add required package under build-depends:

    library
      hs-source-dirs:
          src
    
      build-depends:
          base >= 4.7 && < 5
        , containers
    
      exposed-modules:
          Lib
    
  2. Issue stack build to download and build dependencies

    (or stack ghci if you plan to use it in the REPL)

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
toraritte
  • 6,300
  • 3
  • 46
  • 67
  • 7
    Thanks for the explanation! (the previous answer "gave me a fish" instead of "showing me how to fish") – agam Mar 22 '18 at 05:11
  • 1
    @dfeuer Thanks a lot for the edit! I just realized how inaccessible it was with the image... – toraritte Nov 16 '18 at 17:07
  • 1
    @toraritte, you're welcome. Hopefully we'll be able to make some progress towards accessibility. – dfeuer Nov 16 '18 at 17:12
20

The reason you can import Data.Char and Data.List is that they are part of the package base, which is included with GHC and is always loaded with GHCi. By contrast, Data.Map is in the external library containers. One way to load it with stack ghci is to add a cabal file with a build-depends on containers. This will install it in the stack environment for xxxx, so it will then be accessible.

crockeea
  • 21,651
  • 10
  • 48
  • 101
  • I'm not sure what you're asking. Create a cabal file `xxxx.cabal`, add a library/executable section, and add `containers` in the `build-depends` section. See [this answer](http://stackoverflow.com/a/12305972/925978). – crockeea Sep 25 '16 at 18:14
3

I my case I had to add containers to package.yaml instead of adding the dependency directly to the .cabal file:

dependencies:
- base >= 4.7 && < 5
- containers
Ava
  • 818
  • 10
  • 18