How to install an library in Haskell?

Viewed 195

I try to use Control.Monad.Extra.andM

import Control.Monad.Extra (andM)

but has an error:

Could not find module ‘Control.Monad.Extra’
Perhaps you meant
  Control.Monad.Catch (needs flag -package-key exceptions-0.10.4)
  Control.Monad.Error (needs flag -package-key mtl-2.2.2)
  Control.Monad.Except (needs flag -package-key mtl-2.2.2)not found

This error does not make sense.

According to

https://cabal.readthedocs.io/en/3.6/installing-packages.html#installing-packages-from-hackage

3.2.1. Installing packages from Hackage The cabal tool also can download, configure, build and install a Hackage package and all of its dependencies in a single step. To do this, run:

$ cabal install [PACKAGE...]

To browse the list of available packages, visit the Hackage web site.

Which says "in a single step", but in my experience this is too complicated and actually I have no idea how to install Control.Monad.Extra .

Usually, when a Haskeller want to install a specific library/package like this, how do you do this? There's no adequate documentations, it seems.

2 Answers

How to install an library in Haskell?

You don't. You should just depend on them, and then let Cabal worry about any installations that may need to be done. I.e., as you wrote

  1. Go to the Hackage page and study which exact library to be used.

  2. *.cabal file -> build-depends: extra >=1.7.10

    This is the crucial step. Your own Cabal file is the way to both specify what libraries are needed right now, as well as ensuring that everything will still work in the future. This file needs to specify both the packages you require, as well as the modules you're defining yourself.

    Probably you don't actually need >=1.7.10, but it can't hurt much to be specific there. Standard practice is to add both lower and upper bounds on the x.y level, i.e. you'd use extra >=1.7 && <1.8, and then push the upper boundary as new versions come out. Arguably this is a bit overcautious; if you only use some simple tools from a package that is unlikely to have breaking changes in the future then it may be less trouble to just leave the upper boundary out.

  3. $ cabal install extra

    You've already specified that extra is needed for your project, no need to state that again. Instead, simply build your own project now.

    $ cabal build
    or, to get a GHCi prompt with your modules,
    $ cabal repl
    or, if you have an executable with a main that you wish to execute
    $ cabal run
    or if you want to install that executable (and only then)
    $ cabal install
    Cabal will then automatically figure out that extra-1.7.10 should be installed first.

    (In old Cabal it would still have been necessary to run cabal install --dependencies-only first.)

Ok Self-Answered. Comment me if the information is wrong.

1. Go to the Hackage page and study which exact library to be used.

enter image description here

In this case, it seems extra-1.7.10.

2. *.cabal file -> build-depends: extra >=1.7.10 (among other libs)

3. $ cabal install extra

Error:

cabal: Cannot build the executables in the package extra because it does not contain any executables. Check the .cabal file for the package and make sure that it properly declares the components that you expect.

Well, I did for sure.

4. cabal install extra --lib

Finally works, but the documentation never said this.

Am I correct? ` Is this the only method? or any other smarter way?

Related