1

I've been able to use the Data.Tuple.Extra module after installing the extra package by stack build extra. But the same doesn't apply to the Data.List.Unique module (https://hackage.haskell.org/package/Unique-0.4.7.8/docs/Data-List-Unique.html). According to the website, it's included in package Unique. So, I installed it with

$ stack build Unique

The installation seems to have been successful because I didn't see any error messages. But

$ cat try.hs
import Data.List.Unique
main = do putStrLn "hello"
$ stack runghc try.hs

try.hs:1:1: error:
    Could not find module ‘Data.List.Unique’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import Data.List.Unique
  | ^^^^^^^^^^^^^^^^^^^^^^^
$

The output from stack runghc -v try.hs is too big for me to analyze . . .

Note: I've modified the question to focus on the installation and use of modules. Following the answers provided below, I'm able to run the program using an option. Still, it would be nice if one wouldn't need to use a package option.

3
  • 1
    On your edit: If you set up a project for your code, you can specify Unique as a dependency of it only once, in the project configuration file, as opposed to every time you want to run it.
    – duplode
    Commented Mar 13, 2021 at 13:11
  • @duplode I see. All my programs are small and stand-alone, not worthy of the name "project". Writing dependencies for each little program is overkill. That probably means that I should add packages to .stack/global-project/stack.yaml. Then I'm stuck. Do you know which document should I look at? A simple "packages:\n - Unique" doesn't work --- stack tries to find a directory named "Unique".
    – Ryo
    Commented Mar 13, 2021 at 13:38
  • 1
    For small programs I don't feel like setting up a proper project for, what I usually do is putting them all into a "playground" directory, in which I have a project set up only for the purpose of specifying the dependencies. The alternative is specifying the dependencies in each file, using the Stack script format suggested in Fyodor Soikin's answer. (As for the global project, it operates on a different level: it is used to specify defaults for where Stack will fetch dependencies from, but not for which dependencies will be available in your code.)
    – duplode
    Commented Mar 13, 2021 at 13:58

2 Answers 2

2

stack build doesn't "install" the package globally for use with every compilation, and runghc can't pick up Stack packages. It's meant to be a more or less direct execution of GHC.

You can specify packages to use if you run your program with stack try.hs, as described in the docs. In that case, you'll be able to specify packages to use with specially formatted comments, like this:

-- stack script --package Unique
import Data.List.Unique
main = do putStrLn "hello"
2
  • 1
    While runghc can't pick up Stack packages, stack runghc can, also by using the --package option.
    – duplode
    Commented Mar 12, 2021 at 15:03
  • Well, good to know. I couldn't find any docs on the subject (Stack docs just say "it runs runghc"), so I thought I'd go with a safer, more stable option. Commented Mar 12, 2021 at 16:17
2

The Unique dependency can be specified by passing the --package option to runghc:

stack runghc --package Unique try.hs

Alternatively, you can make try.hs a Stack script, as illustrated by Fyodor Soikin's answer.

If you plan to develop try.hs into more than a standalone file, consider setting up a Stack project. See the Hello World example in the Stack User Guide for initial guidance on that.

2
  • Thanks! But, I wonder why I don't need the --package option for Data.Sort. I did need to install the sort package with stack build sort.
    – Ryo
    Commented Mar 13, 2021 at 2:59
  • @Ryo That's pretty odd. In any case, it is generally not necessary to explicitly install libraries with e.g. stack build. You just have to tell Stack you need the library (in this case, through the --package option), and it will take care of making it available. stack build is primarily meant for building your projects, and not its dependencies.
    – duplode
    Commented Mar 13, 2021 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.