Could not load module 'System.Random' - haskell

I could not add System.Random module to use it my source haskell file.
import System.Random
This is the error produced by stack ghc:
/Users/admin1/Haskell/PROJECTS/L4/src/Lib4.hs:32:1: error:
Could not load module ‘System.Random’
It is a member of the hidden package ‘random-1.1’.
You can run ‘:set -package random’ to expose it.
(Note: this unloads all the modules in the current scope.)
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
32 | import System.Random
| ^^^^^^^^^^^^^^^^^^^^
Failed, five modules loaded.
Thank you very much in advance.
P.S. I am using Stack and GHC versions: Version 2.3.1, Git revision x86_64 hpack-0.33.0, ghc-8.8.3 on Mac OSX

As the error says:
It is a member of the hidden package ‘random-1.1’.
This likely means that you did not list it in the build-depends, and thus it is not exposed to your modules.
You can alter the .cabal file, and add it, for example:
-- project.cabal file
-- …
executable …
-- …
build-depends:
base >= 4.7 && < 5
, random >= 1.1
-- , …

Related

how do I install `Test.QuickCheck` GHCi can't find it, import fails

I'm on macOS running stack/GHCi version 8.4.3:
import Test.QuickCheck fails at both the GHCi prompt and in my .hs files. Either way I'm told it's not found.
GHCi Prelude>
<no location info>: error:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
in .hs file >
"Could not find module ‘Test.QuickCheck’ '
The source code is on this page but I'm not sure how to install a new package into stack manually. From my brief reading when I googled "install Haskell package" it suggests installing a cabal package universally is a bad idea. Not sure this is a cabal package, and in any case it would be good to be able to import it for any project I think in my case.
The modern way to do this for a quick ghci session is to use cabal repl and add a dependency on QuickCheck:
% cabal repl --build-depends QuickCheck
> import Test.QuickCheck
Test.QuickCheck> -- ^_^
For longer-term programming (i.e. not just quick tests of stuff in ghci), the modern way is to create a cabal package and add QuickCheck to the build-depends section in the *.cabal produced:
% mkdir fancy-package
% cd fancy-package
% cabal init
<follow prompts>
% $EDITOR fancy-package.cabal
<find build-depends: and add, for example, QuickCheck ^>= 2.14 to the list>
% cabal repl
*Main> import Test.QuickCheck
*Main Test.QuickCheck> -- ^_^

Which package to install to run this code successfully

I am trying following simple code from here :
import Data.GI.Base
import qualified GI.Gtk as Gtk
main :: IO()
main = do
Gtk.init Nothing
Gtk.main
However, I am getting following error:
Could not find module `Data.GI.Base'
Use -v to see a list of the files searched for.
|
1 | import Data.GI.Base
| ^^^^^^^^^^^^^^^^^^^
simplewin.hs:2:1: error:
Could not find module `GI.Gtk'
Use -v to see a list of the files searched for.
|
2 | import qualified GI.Gtk as Gtk
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Following attempts to install packages do not find any matches:
>cabal list Data.GI.Base
No matches found.
>cabal list GI.Gtk
No matches found.
Which packages do I install with cabal to correct above errors?
You can search Stackage (the FPComplete package repository, the de-facto standard for Haskell) for the module, type, or function name.
For example, when I type in Data.GI.Base, I get its documentation page, which tells me (at the very top) that the module is available in the package haskell-gi-base.
Not every package is available on Stackage though. If you can't find something, try Hackage next. It's less regulated and controlled, but has more stuff because of it.

Which library is the simpleHTTP function in?

I have installed using cabal install network, HTTP, http-conduit, and http-client. When I run ghc it still throws this error:
testRun.hs:1:1: error:
Could not find module `Network.HTTP.Simple'
Perhaps you meant
Network.HTTP.Base (from HTTP-4000.3.12)
Network.HTTP.Cookie (from HTTP-4000.3.12)
Network.HTTP.Stream (from HTTP-4000.3.12)
Use -v to see a list of the files searched for.
|
1 | import Network.HTTP.Simple
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
or this error if I use Conduit:
testRun.hs:1:1: error:
Could not find module `Network.HTTP.Conduit'
Perhaps you meant Network.HTTP.Cookie (from HTTP-4000.3.12)
Use -v to see a list of the files searched for.
|
1 | import Network.HTTP.Conduit (simpleHttp)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What do in need to install?
It's using a different approach (Stack as a script interpreter), but most likely the following command will run the script with the necessary libraries available:
stack --resolver lts-12.9 script testRun.hs

Could not find module 'Data.Set'

I have the following RPM packages installed on my Fedora 28 system:
ghc-ghc-8.2.2-66
ghc-containers-0.5.10.2-66
According to hackage the set module should be included in the given RPMs. However trying to import Data.Set results in
<no location info>: error:
Could not find module ‘Data.Set’
Perhaps you meant Data.Int (from base-4.10.1.0)
Did I miss something to install? How can I check which modules are available?
Edit:
$ ghc-pkg list
/usr/lib64/ghc-8.2.2/package.conf.d
base-4.10.1.0
ghc-prim-0.5.1.1
integer-gmp-1.0.1.0
rts-1.0
How do I register a module?
I would skip the operating system packages and go with stack:
$ wget -o get-stack.sh https://get.haskellstack.org/
$ chmod +x get-stack.sh
$ ./get-stack.sh -d ~/.local/bin
$ echo 'export PATH="~/.local/bin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc
$ stack --version
Version 1.7.1, Git revision ...
Then use stack ghc to run GHC; the first time it will install this:
$ stack ghc
Writing implicit global project config file to: ...
Note: You can change the snapshot via the resolver field there.
Using latest snapshot resolver: lts-12.9
Downloaded lts-12.9 build plan.
Preparing to install GHC to an isolated location.
For anyone new like me who finds this, you need to add containers to your package.yaml dependencies in order to import Data.Set. My package.yaml dependencies looked like this to get a Data.Set import to work:
dependencies:
- base >= 4.7 && < 5
- containers > 0.6
Then you can import Data.Set in your file like
import Data.Set (Set)
import qualified Data.Set as Set
ghc-containers contains only the shared library (.so) for compiled programs that are linked to it. If you wish to use the library in development, install ghc-containers-devel:
$ dnf install -y ghc-containers-devel
$ ghci
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help
Prelude> import Data.Set
Prelude Data.Set>

GHCI can't load module if both package-conf and source path contains it

I encounter a strange situation in GHCI, don't know if anyone observed similar case. For some module, when I have it in the search path both by -package-conf, and also by -i, GHCI fails when I try to import the module with 'module is not loaded: FooModule'.
:module loads it fine however
or I can do :load FooModule, :m to clear the import list, and then import FooModule
or I can remove the path from -i and then it imports fine
Tracked this to be the otherwise -> modNotLoadedError m loc case in GHC, where otherwise ~ modulePackageId = this_pkg (the meaning of which I don't know).
This is not entirely systematic, there are some module which are both in package and source path, but can be imported.
GHC only knows about packages that are installed. To see which packages are installed, use the ghc-pkg list command:
$ ghc-pkg list
/usr/lib/ghc-6.12.1/package.conf.d:
Cabal-1.7.4
array-0.2.0.1
base-3.0.3.0
base-4.2.0.0
bin-package-db-0.0.0.0
binary-0.5.0.1
bytestring-0.9.1.4
containers-0.2.0.1
directory-1.0.0.2
(dph-base-0.4.0)
(dph-par-0.4.0)
(dph-prim-interface-0.4.0)
(dph-prim-par-0.4.0)
(dph-prim-seq-0.4.0)
(dph-seq-0.4.0)
extensible-exceptions-0.1.1.0
ffi-1.0
filepath-1.1.0.1
(ghc-6.12.1)
ghc-prim-0.1.0.0
haskeline-0.6.2
haskell98-1.0.1.0
hpc-0.5.0.2
integer-gmp-0.1.0.0
mtl-1.1.0.2
old-locale-1.0.0.1
old-time-1.0.0.1
pretty-1.0.1.0
process-1.0.1.1
random-1.0.0.1
rts-1.0
syb-0.1.0.0
template-haskell-2.4.0.0
terminfo-0.3.1
time-1.1.4
unix-2.3.1.0
utf8-string-0.3.4

Resources