1
votes

I've been using stack (in my linux computer) to compile my haskell code and today I installed for the first time an external module. I installed Data.MultiSet by running the following command:

sudo stack install multiset

Supposedly the module was succesfully installed but I can't load it, stack throws the following message when I try to compile my code:

..error: Could not find module ‘Data.MultiSet’...

I checked the files contained in my .stack directory and there are many files with the name of this package but I am not very familiar with this directory tree. I also tried writing the name of the module in the dependency field of the package.yaml of my project but it didn't work either. Any clues of what's happening? Thanks!

1
Try adding a --package multiset option to your command e.g. stack ghc --package multiset myfile.hs. Not putting this as an answer as I don’t know exactly which command you’re using for compilation. (Also, stack install is discouraged, especially for libraries.)bradrn
@bradrn I think that comment could be expanded into a great answer.leftaroundabout
Thanks @leftaroundabout! Which bits do you think could be expanded?bradrn
It should quote the important parts from the article you've linked to.leftaroundabout
@leftaroundabout But that isn’t really related to the question — it’s just tangential information which I thought was important enough to warrant a mention.bradrn

1 Answers

2
votes

Stack doesn't work like that. The idea behind Stack is this: you have a project, you know the dependencies of this project, and Stack ensures you can always build that project – now or in the future. Stack is not about tweaking your system in some way that'll give you access to some packages here and now.

So, if you're going to use Stack for development (which I personally don't, but I also don't say it's a bad idea; lots Haskellers do this) then you should first set up the file you're working on as part of a project. This can be as simple as making your file a Stack snippet: add the following to the top of your file (I'll assume it's called script.hs)

#!/usr/bin/env stack
{- stack --resolver lts-13.0 --install-ghc
      runghc --package multiset
  -}

(You could also pass those flags on the command line for stack, but that gets tedious quickly. env really does nothing else, but consistently.)

Then make your file executable – on Linux or OSX this can be done with chmod +x script.hs – and all you need to do to compile & run that script will be ./script.hs. (Not stack script.hs or ghc script.hs.) The great thing about this is that you get “instant continuous integration”: if Stack is able to build run your script on your computer now, you can be pretty sure that it'll also work on any other computer in the future, without you needing to remember what packages to install etc..

More info on that technique: https://www.fpcomplete.com/blog/2016/08/bitrot-free-scripts.

If it's more than a simple script you're writing, you should make a proper Cabal/Stack configuration for it. This can be created easily with cabal init (regardless of whether you will use Cabal or Stack).