Why can't the ghc (Haskell) compiler find the module Data.Vector which I have imported? I get the error: Could not find module 'Data.Vector' - haskell

vector module has been installed by "cabal install vector"
bash-3.2$ ghc-pkg list -f $HOME/.cabal/store/ghc-9.2.1/package.db
/Users/gcolpitts/.cabal/store/ghc-9.2.1/package.db
primitive-0.7.3.0
vector-0.12.3.1
but the compiler can't find it:
$ ghc prob214ff.hs
[1 of 1] Compiling Main ( prob214ff.hs, prob214ff.o )
prob214ff.hs:14:1: error:
Could not find module ‘Data.Vector’
Perhaps you meant Data.Functor (from base-4.16.0.0)
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
14 | import Data.Vector
Thx #n. 1.8e9-where's-my-share !
ghc -package-db $HOME/.cabal/store/ghc-9.2.1/package.db prob214ff.hs
solves my problem although it is a regression from previous versions of ghc which didn't require the use of the -package-db option.
I'd love to use GHC_PACKAGE_PATH but I can't see how to make that work. Sec 5.9.5 of the ghc users guide is confusing. It states "A package database is where the details about installed packages are stored. It is a directory, usually called package.conf.d." i.e. not the package.db file that I gave with -package-db on the command line. But where is the package.conf.db directory with info on the vector package that I installed with cabal?
"ghc-pkg list" references /usr/local/lib/ghc-9.2.1/lib/package.conf.d but the output of the command doesn't list the vector package I installed.
"ghc-pkg list -f $HOME/.cabal/store/ghc-9.2.1/package.db" does list the vector package in it's output but doesn't tell me a package.conf.d directory that knows about the vector package.

This is a bit fiddly with cabal version 2. Installing packages no longer really makes sense (despite the cabal install still existing...). Instead you will probably find it easiest to create a cabal package instead, for example:
$ mkdir my-package
$ cd my-package
$ cabal init
<Edit my-package.cabal and add vector to the build-depends line>
<Put the contents of prob214ff.hs in app/Main.hs instead>
$ cabal run

This will be fixed in 9.2.2, see https://gitlab.haskell.org/ghc/ghc/-/issues/20660#note_391573

Related

With HaskellStack install packages to use with GHC without stack

I install GHC on Windows10 using the recommended Haskell Stack. I want to us GHC without all the Stack overhead for Advent of Code. This was working fine until I tried to get the extra package.
I can install it with Stack, but I don't seem to have a way to get it in the global package database. Haskell Stack apparently does not install the cabal executable and seems to have it locked out of their package database.
How do I install the extra package for use with vanilla GHC?
John Miller#DESKTOP-NENAGQH MSYS /d/dev/AdventOfCode2020
$ stack ghc -- AoC/Utils.hs
[1 of 1] Compiling AoC.Utils ( AoC\Utils.hs, AoC\Utils.o )
John Miller#DESKTOP-NENAGQH MSYS /d/dev/AdventOfCode2020
$ ghc AoC/Utils.hs
[1 of 1] Compiling AoC.Utils ( AoC\Utils.hs, AoC\Utils.o ) [Data.List.Extra changed]
AoC\Utils.hs:3:1: error:
Could not find module `Data.List.Extra'
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
3 | import Data.List.Extra
| ^^^^^^^^^^^^^^^^^^^^^^
I don't know whether stack supports installing to the global package DB. cabal does though:
$ cabal install --lib extra
Resolving dependencies...
Up to date
$ ghci
GHCi, version 8.10.2: https://www.haskell.org/ghc/ :? for help
Loaded package environment from /home/simon/.ghc/x86_64-linux-8.10.2/environments/default
Prelude> import Data.List.Extra
Prelude Data.List.Extra>
Ok, so stack can install the cabal executable if you beat at it long enough. The package is called cabal-install and it is not in any resolver, but is on Hackage.
stack install cabal-install
Because it is not in the resolver there is a pretty good chance that the version of Cabal, the library for manipulating cabal packages in Haskell, is not compatible. First, ask stack where it keeps its global config
stack path --config-location
Edit that file to allow for the needed dependencies under extra-deps: Stack will helpfully tell you what they are. It may also be helpful to change the resolver to a newer version in that file while your at it.
Now try
stack install cabal-install
again and if these instructions have not fallen out of date since December 2020 you will get the cabal executable somewhere potentially useful.
Before using cabal you will have to run a cabal update to get the package list.
At this point cabal should manipulate your global package database and stack can install GCH and all its libraries over and over and over again if you want to use it for a project instead. They should just keep out of each other's way.

Haskell - could not find module 'Test.QuickCheck'

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

How to work together with cabal-3 and ghc (ghc-pkg, too)?

With the release of cabal-3, the packages from Hackage are installed in a new location that the compiler ghc and ghc-pkg know nothing about.
In other words, packages are installed but not registered for ghc. Ghci, ghc, ghc-pkg cannot work.
For example,
cabal install safe --lib
Create file t1.hs
import Safe
t1 = tailMay [1,2,3]
Let's try:
> ghci t1.hs
GHCi, version 8.10.2: https://www.haskell.org/ghc/:? for help
[1 of 1] Compiling Main (t1.hs, interpreted)
t1.hs: 1: 1: error:
Could not find module `Safe '
Use -v (or `: set -v` in ghci) to see a list of the files searched for.
|
1 | import Safe
| ^^^^^^^^^^^
Failed, no modules loaded.
This bug is described here
https://github.com/haskell/cabal/issues/6262
and here
https://gitlab.haskell.org/ghc/ghc/-/issues/17341
I use as a temporary solution setting a system variable
GHC_PACKAGE_PATH=C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db;
(Windwos 10, haskell-dev by chocolatey)
via
On Windows, packages installed with cabal seem to be unavailable in ghc/ghci
but with updates I will have to manually change this system variable.
Are there any more elegant solutions to this problem?
P.S. Unfortunately, this solution (via GHC's environment variable GHC_PACKAGE_PATH) is incompatible with Cabal :(
https://github.com/haskell/cabal/issues/1944
One way to achieve this is to use the --env flag to make the libraries available to GHC whenever you are in the current directory:
~ $ mkdir /tmp/foo
~ $ cd /tmp/foo
/tmp/foo $ cabal install safe --lib --env .
Resolving dependencies...
Build profile: -w ghc-8.8.3 -O1
In order, the following will be built (use -v for more details):
- safe-0.3.19 (lib) (requires build)
Configuring library for safe-0.3.19..
Preprocessing library for safe-0.3.19..
Building library for safe-0.3.19..
…
> Installing library in /home/jojo/.cabal/store/ghc-8.8.3/incoming/new-4056/home/jojo/.cabal/store/ghc-8.8.3/safe-0.3.19-92fbaef88124b4508ce447f6245bc793f7a1748247ae68d10e449150df1069af/lib
t1.hs
/tmp/foo $ cat > t1.hs
import Safe
t1 = tailMay [1,2,3]
/tmp/foo $ ls -a
. .. .ghc.environment.x86_64-linux-8.8.3 t1.hs
/tmp/foo $ ghci t1.hs
GHCi, version 8.8.3: https://www.haskell.org/ghc/ :? for help
Loaded package environment from /tmp/foo/.ghc.environment.x86_64-linux-8.8.3
[1 of 1] Compiling Main ( t1.hs, interpreted )
Ok, one module loaded.
*Main>
Note that you probably shouldn’t do this in a directory where you actually have a foo.cabal file. See the documentation of cabal v2-install for details.
Working with GHC_ENVIRONMENT is better:
setx GHC_ENVIRONMENT C:\Users\me\.ghc\x86_64-mingw32-8.10.2\environments\default
it helps for ghc and ghci.
After, in C:\Users\me\AppData\Roaming\cabal\config we should add
package-db: C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db
it helps for cabal.
Unfortunately, ghc-pkg still has problem and works with such flag:
ghc-pkg list --user-package-db="C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db"
For Linux the steps are similar.

Haskell there are files missing in the QuickCheck-2.11.3 package

I tried running my program which uses Haskell QuickCheck via ghc MyProgramm.hs , but received the following error:
$ ghc Ex2.hs
[1 of 1] Compiling Ex2 ( Ex2.hs, Ex2.o )
Ex2.hs:21:1: error:
Could not find module ‘Test.QuickCheck’
There are files missing in the ‘QuickCheck-2.11.3’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
21 | import Test.QuickCheck (
| ^^^^^^^^^^^^^^^^^^^^^^^^...
I installed stack, ran stack update and stack install QuickCheck without issue but the error persisted. Then, I ran cabal install QuickCheck and got the following errors:
$ cabal install QuickCheck
Resolving dependencies...
Configuring QuickCheck-2.12.4...
Building QuickCheck-2.12.4...
Failed to install QuickCheck-2.12.4
Build log ( /home/username/.cabal/logs/ghc-8.4.3/QuickCheck-2.12.4-3d2YDDqfPBn4BfmTJbpJXK.log ):
cabal: Entering directory '/tmp/cabal-tmp-9133/QuickCheck-2.12.4'
Configuring QuickCheck-2.12.4...
Preprocessing library for QuickCheck-2.12.4..
Building library for QuickCheck-2.12.4..
[ 1 of 16] Compiling Test.QuickCheck.Exception ( Test/QuickCheck/Exception.hs, dist/build/Test/QuickCheck/Exception.o )
[ 2 of 16] Compiling Test.QuickCheck.Random ( Test/QuickCheck/Random.hs, dist/build/Test/QuickCheck/Random.o )
Test/QuickCheck/Random.hs:10:1: error:
Could not find module ‘System.Random’
There are files missing in the ‘random-1.1’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
10 | import System.Random
| ^^^^^^^^^^^^^^^^^^^^
Test/QuickCheck/Random.hs:11:1: error:
Could not find module ‘System.Random.TF’
There are files missing in the ‘tf-random-0.5’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
11 | import System.Random.TF
| ^^^^^^^^^^^^^^^^^^^^^^^
Test/QuickCheck/Random.hs:12:1: error:
Could not find module ‘System.Random.TF.Gen’
There are files missing in the ‘tf-random-0.5’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
12 | import System.Random.TF.Gen(splitn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cabal: Leaving directory '/tmp/cabal-tmp-9133/QuickCheck-2.12.4'
cabal: Error: some packages failed to install:
QuickCheck-2.12.4-3d2YDDqfPBn4BfmTJbpJXK failed during the building phase. The
exception was:
ExitFailure 1
However, I already have the arch packages haskell-random, haskell-tf-random and haskell-mwc-random installed. Does anybody know how to fix this?
Edit: I also ran cabal install random --reinstall.
Problem
On Archlinux as of 2022-09-17, pacman -S ghc cabal-install will install
system packages that provide only dynamic files (.so, .dyn_hi) in
installed packages inside /usr/lib/ghc-*; static files (.a, .hi) are (for
the most part) missing. However, the default cabal configuration enables static file
building. Unfortunately, upstream cabal-install doesn't track whether or not
static files are available inside installed packages. It just assumes they
are, and when they are gone, it fails with errors such as you have found:
[1 of 1] Compiling Main ( Main.hs, ../setup.dist/work/depender/dist/build/depender/depender-tmp/Main.o )
Main.hs:3:1: error:
Could not find module `Dynamic'
There are files missing in the `dynamic-1.0' package,
try running 'ghc-pkg check'.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
| import qualified Dynamic (number)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Workaround
A quick workaround just to get up and running quickly is to disable static file
building, which is by default enabled. (Note that trying to pass package-local
flags to disable static file with e.g. --enable-shared --enable-executable-dynamic --disable-library-vanilla (which is how most if
not all current Archlinux Haskell packages seem to build packages, e.g. with
https://github.com/archlinux/svntogit-community/blob/master/haskell-scientific/trunk/PKGBUILD)
building using cabal-install may be ignored due to another, related bug; but
~/.cabal/config is a safe bet.) You may do so by adding 3 lines to
~/.cabal/config in the appropriate location:
library-vanilla: False
shared: True
executable-dynamic: True
(Alternatively, consider using alternative tools like stack.)
More complete solution
For a more long-term solution, one option involves 2 pieces: 1) one or more
system packages that provide all types of build artifacts, static and dynamic,
for the base, foundational packages (from GHC and cabal-install), at least as an option besides dynamic-only packages (secondary Haskell packages are optional,
since cabal-install can rebuild these with needed build artifacts (static or
dynamic)), and 2) patching cabal-install (and ghc-pkg, which can handle .conf
files recording information about installed packages) to track whether static
files are available, and to be aware of these when resolving dependencies so
that cabal-install knows when to prefer rebuild a source package with needed
build artifact configuration over an already installed package that doesn't
provid required build artifacts.
There is a merge request (I submitted) that provides such a patchset, called
fix-dynamic-deps, at https://github.com/haskell/cabal/pull/8461. For users
running into exactly the problem that you described (myself included), I also
created an AUR package that provides both pieces based on GHC 9.4.2 with Cabal
3.9.0.0 that includes my patchset (there is a mirror at
https://github.com/bairyn/ghc-cabal-arts.) It provides ghc and
cabal-install but includes both of these pieces.
Further reading
Here are a few more resources I wrote on or are related to this bug:
https://wiki.archlinux.org/index.php?title=Haskell&diff=745459&oldid=738269#Troubleshooting
https://github.com/haskell/cabal/pull/8461
https://github.com/bairyn/ghc-cabal-arts
https://github.com/bairyn/cabal/tree/fix-dynamic-builds
Even though I have a main = do and couldn't get it working that way, I was able to run my QuickCheck test as follows:
To run a quickCheck, first load your program with:
$ ghci MyProg.hs
Then to run the quickCheck, find the test you want to run, then run it with:
$ quickCheck my_quick_check

Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’

I have been trying to run a simple Haskell program using TDFA. The program is as follows:
import Control.Monad
import Data.Array
import qualified Data.Text as T
import Text.Regex
import Text.Regex.TDFA
import Text.Regex.Base
str = "abbbbaab" :: String
regex = "(a+)(b+)" :: String
main = do
if str (=~) :: regex then putStrLn "matched" else putStrLn "no matches"
when I try to run the above program using the command like: ghc test.hs -o test and then I am getting the following error (test.hs is the Haskell program which contains the above code):
Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’
Note that I have the latest version of TDFA installed on my pc. I did it by following ways:
ghc --make -o setup Setup.hs
Also, I did the following:
user#user-VirtualBox:~/regex-tdfa-master$ sudo cabal install regex-tdfa
[sudo] password for user:
Resolving dependencies...
All the requested packages are already installed:
regex-tdfa-1.2.2
Use --reinstall if you want to reinstall anyway.
I even tried it with the Makefile which I got from TDFA's github repository:
user#user-VirtualBox:~/regex-tdfa-master$ make
ghc -o setup --make ./Setup.hs
./setup clean
cleaning...
./setup configure --prefix=/Users/user/local/devel/trl --enable-library-profiling --user
Configuring regex-tdfa-1.2.2...
./setup build
Building regex-tdfa-1.2.2...
Preprocessing` library regex-tdfa-1.2.2...
[ 1 of 23] Compiling Text.Regex.TDFA.NewDFA.Uncons ( Text/Regex/TDFA/NewDFA/Uncons.hs, dist/build/Text/Regex/TDFA/NewDFA/Uncons.o )
...........(Skipped to make the things short here)..............................
[23 of 23] Compiling Text.Regex.TDFA ( Text/Regex/TDFA.hs, dist/build/Text/Regex/TDFA.o )
Text/Regex/TDFA.hs:60:8:
Could not find module ‘Text.Regex.Base’
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/ByteString.hs:24:8:
Could not find module ‘Text.Regex.Base.Impl’
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/CorePattern.hs:37:8:
Could not find module ‘Control.Monad.RWS’
Perhaps you haven't installed the profiling libraries for package ‘mtl-2.2.1#mtl_Aue4leSeVkpKLsfHIV51E8’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/ReadRegex.hs:13:8:
`Could not find module ‘Text.ParserCombinators.Parsec’`
Perhaps you haven't installed the profiling libraries for package ‘parsec-3.1.9#parse_EE5NO1mlYLh4J8mgDEshNv’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/String.hs:23:8:
Could not find module ‘Text.Regex.Base.RegexLike’`
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/TDFA.hs:10:8:`
Could not find module ‘Control.Monad.State’
Perhaps you haven't installed the profiling libraries for package ‘mtl-2.2.1#mtl_Aue4leSeVkpKLsfHIV51E8’?
Use -v to see a list of the files searched for.
Makefile:16: recipe for target 'build' failed
make: *** [build] Error 1
user#user-VirtualBox:~/regex-tdfa-master$
However, nothing worked. Hence, any help would be so appreciated...
Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’
On an initial note, this error points to a module of the regex-tdfa package, so you should have gotten it when building the package, rather than when building your test.hs, unless you were trying to build test.hs after putting it within the package source tree. In any case, line 13 of the mentioned file is:
import GHC.Prim(MutableByteArray#,RealWorld,Int#,sizeofMutableByteArray#,unsafeCoerce#)
The # names require the MagicHash GHC extension to be enabled. As there is no {-# LANGUAGE MagicHash #-} pragma at the beginning of the file, one should assume the extension is enabled through the .cabal file of the package, and that is indeed the case. That being so, your problem seems to be that you are attempting use the package modules straight from the source tree, without using an appropriate build tool such as Cabal. (Note that if you did successfully run cabal install regex-tdfa there should be no need of dealing with the source tree: ghc --make -o test test.hs should be enough.)
P.S.: There is an error in your test.hs. The last line should be...
if str =~ regex then putStrLn "matched" else putStrLn "no matches"
(I did manage to run it after this correction.)
It looks like you are compiling your program from within Regex-TDFA source code. I am able to reproduce your problem if I do cabal unpack regex-tdfa && cd regex-tdfa-1.2.2 && ghc --make test.hs.
The error happens because when GHC finds Regex-TDFA source files in the current directory, it just picks them and tries to compile, as if they were ordinary source files belonging to your project. However, Regex-TDFA cannot be built with simple ghc --make: in order to build it, you have to run cabal first. Cabal will read regex-tdfa.cabal file that contains the list of the necessary GHC extensions.
In particular, parse error happens because GHC extension MagicHash is missing (that's where the hash # comes from).
The "fix" is to move your test.hs to some other location: then GHC will use system package for Regex-TDFA.

Resources