Stack can't find a downloaded module (Data.MultiSet)

不羁的心 提交于 2020-05-09 08:01:17

问题


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:


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).



来源:https://stackoverflow.com/questions/57157070/stack-cant-find-a-downloaded-module-data-multiset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!