14

I'd like to try out the Writer monad in ghci. As advised here, I tried to use only stack to manage GHC and packages, and avoid a global installation.

From a fresh Ubuntu 15.04 install, after installing stack:

stack setup
mkdir lyah && cd lyah
stack new
stack install mtl
stack ghci
ghci> import Control.Monad.Writer
Could not find module ‘Control.Monad.Writer’
It is a member of the hidden package ‘mtl-2.1.3.1’.

I understand that pre-stack ghc-pkg was used to show/hide packages, but I'm not sure how to proceed here to 'unhide' the mtl package.

jazmit
  • 5,170
  • 1
  • 29
  • 36

4 Answers4

18

Edit the .cabal file stack new created and add mtl to the build-depends section. That part of the file should look like this:

build-depends:       base >= 4.7 && < 5
                   , mtl

Then, do a stack build before stack ghci.

By the way, do not use stack install to install libraries - it is merely a shortcut to copy binaries. E.g. stack install hlint will first build the package and then copy the resulting binary to ~/.local/bin/. Instead, always add the packages to the .cabal file, as shown above, and use stack build so that they get installed.

duplode
  • 33,731
  • 7
  • 79
  • 150
2

Just to complement @Jazmit answer. Be aware that your .cabal file will have two build-depends sections. One under library: and the other under executable my-project-exec. In this case, you'd need to put the module under the executable section.

Example:

my-project.cabal

library: 
  build-depends:
  ...

executable my-project-exe:
  build-depends:
    base >= 4.7 && < 5
    , mtl

For further info about library and executable check the docs: https://cabal.readthedocs.io/en/latest/developing-packages.html#editing-the-cabal-file

Dan
  • 458
  • 1
  • 6
  • 11
2

If you are using stack, try adding your packages definition to dependencies field inpackage.yaml for expose it into you project.

dependencies:
- mtl

Then, stack run

Example of package.yaml

Stelf
  • 88
  • 1
  • 5
1

Since you're working in GHCi, you can just change the command line passed to the underlying GHC. For example, I did this recently:

Prelude> import qualified GI.Gtk as Gtk

<no location info>: error:
    Could not load module ‘GI.Gtk’
    It is a member of the hidden package ‘gi-gtk-3.0.31’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
    It is a member of the hidden package ‘gi-gtk-3.0.27’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
Prelude> :set -package gi-gtk-3.0.27
package flags have changed, resetting and loading new packages...
Prelude> import qualified GI.Gtk as Gtk
Prelude Gtk> 
jpaugh
  • 6,634
  • 4
  • 38
  • 90