Haskell - could not find module 'Test.QuickCheck'
Asked Answered
U

2

4

I'm getting an error that says the module doesn't exist when I try to runhaskell. It's odd because I try to install it first and says its up to date. Any idea how to fix this? enter image description here

Unmeriting answered 10/9, 2020 at 20:57 Comment(7)
You probably need to add a dependency in your cabal file.Carport
i.imgur.com/nNaeODy.png I added this to my cabal file and ran cabal configure, cabal build, and cabal install. I got this error at cabal install: i.imgur.com/6zATtvu.pngUnmeriting
runhaskell calls GHC which doesn't expose modules by default. Why are you using runhaskell if you have a cabal file? Can you not cabal run?Hesperidin
The license error just says you need a license file touch LICENSE to make a blank one. I dislike this cabal behavior but am sure there is a long argument on the issue tracker.Hesperidin
How can I make ghc expose modules? I'd rather use runhaskellUnmeriting
You can use the -package flag such as runhaskell -package QuickCheck Ex1.hs.Hesperidin
Hm, another error. i.imgur.com/l6kasN1.png Any idea how to fix this one?Unmeriting
E
9

You could try creating the package environment in the local directory that holds your project, like this:

c:\Users\...\ex1haskell> cabal install --lib --package-env . QuickCheck

This should create a file of the form .ghc.environment.xxx in ex1haskell, which hopefully should be picked up by runhaskell/ghci/ghc invocations.

In ghci sessions, a sign that the environment is being picked up is the following message while starting:

Loaded package environment from ...

When the --package-env location is not given explicitly, a default location is used. According to the docs:

By default, it is writing to the global environment in ~/.ghc/$ARCH-$OS-$GHCVER/environments/default. v2-install provides the --package-env flag to control which of these environments is modified.

But it seems that runhaskell is having problems to find the environment file in that default location.

Note. When creating a package environment, it's possible to specify multiple packages simultaneously, like this:

cabal install --lib --package-env . QuickCheck random aeson transformers

Also, package environments are just text files, so local environments can be deleted and recreated at will. The actual package binaries reside elsewhere and can potentially be reused by cabal.

Etherege answered 11/9, 2020 at 6:14 Comment(2)
The problem with not finding the default global environment on Windows is caused by a bug: gitlab.haskell.org/ghc/ghc/-/issues/19286Etherege
This solution helped a lot !. I am on ubuntu, and was looking to use the "text" package. Created the local environment as above using the "--package-env" command, Note: a small adjustment I made . in ghci "import Text.CSV" asked me to ":set -package csv" to expose the member of the text package. After that "import Text.CSV" worked like a charm.Indene
H
2

A Common Environment

It is hard to debug if/when the actual tooling differs so let's first get a unified setup. I'll use docker to get GHC 8 and Cabal 3.x:

docker run --rm -it haskell bash

Understand that this isn't arbitrary or even preemptive. What you have shown - cabal install --lib ... and runhaskell ... does work for sane tool installations. You might have a bad installation or an old version of a tool that has different behavior.

Running a single file with runhaskell

Now we need a project:

root@8a934c302dba:/# mkdir Ex1
root@8a934c302dba:/# cd Ex1
root@8a934c302dba:/Ex1# cat <<EOF >Main.hs
> import Test.QuickCheck
>
> main :: IO ()
> main = print =<< (generate arbitrary :: IO Int)
> EOF

And test failure:

root@8a934c302dba:/Ex1# runhaskell Main.hs

Main.hs:1:1: error:
    Could not find module `Test.QuickCheck'
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import Test.QuickCheck

And install the library:

root@8a934c302dba:/Ex1# cabal update && cabal install --lib QuickCheck

And successful run:

root@8a934c302dba:/Ex1# runhaskell Main.hs
15

So my comment above was wrong - we don't need to explicitly list the package as it is already exposed after installation.

Hesperidin answered 10/9, 2020 at 21:37 Comment(3)
I tried doing cabal update, then cabal install, then runhaskell as I don't think I've tried that sequence of steps yet, but still doesn't work. I'll try creating a new working directory like you.Unmeriting
Doesn't work for me - import QuickCheck fails.; cabal info QuickCheck says "not installed". Have to use v1-install, then it works.Begin
I was able to execute exactly my above sequence successfully. Are you sure you ran the "And install the library" part as shown? Can you paste-bin your full set of steps?Hesperidin

© 2022 - 2024 — McMap. All rights reserved.