How to (install and) use package in haskell - haskell

I'm trying to use this xml package in ghci.
What I already did in cmd:
cabal install cabal-install
cabal install xml-1.3.13.tar.gz
And the installation was succesful.
But how do I use this package inside my scripts and GHCi? I'm about to throw my pc out of the window atm...

To explicitly load the xml package in ghci, use:
ghci -package xml
A single package contains modules, which are the things you import from source code.
In this case we can see a list of them from the hackage page you linked to under the "Modules" heading. The highest-level module listed there is Text.XML.Light so I'd suggest starting from that:
import Text.XML.Light
and then look at the documentation for that module to see what you can do with it.

Related

Hidden packages (after upgrade to Fedora 33?)

I just upgraded to Fedora 33, and at least for Data.Vector and System.Random, I am getting stuff like:
Prelude> import Data.Vector
<no location info>: error:
Could not load module ‘Data.Vector’
It is a member of the hidden package ‘vector-0.12.1.2’.
You can run ‘:set -package vector’ to expose it.
(Note: this unloads all the modules in the current scope.)
Any ideas?
EDIT: I upgraded another system, and both modules are fine. Maybe some local cabal misconfiguration?
EDIT 2: ghc-pkg list shows both packages as non-hidden (ie they are not in parenthesis). What is overriding this?
EDIT 3: Removing .ghc/x86_64-linux-8.8.4/environments/default solves the issue. By looking at it, not all system packages are listed. Do I need that? How was it generated?
EDIT 4: The problem seems to be that cabal install --lib creates a default environment which does not include all the system packages. I'll file a bug...
I don't know the root cause but I would warn against using bare ghci on a modern Haskell install. Instead I would suggest cabal v2-repl --build-depends vector to ensure a compatible vector package is loaded.

How do I use the gloss-examples package?

I've installed the Gloss graphics library with cabal. I also installed the gloss examples package.
Now I want to test the examples. This may seem like a silly question, but how do I actually use the package gloss-examples? I understand I can now import the gloss library to a haskell module but how do I test the gloss-examples I installed i.e. how do I use the gloss-example package?
Building gloss-examples produces a handful of executables, as specified by the Executable entries in the .cabal file. (Another telltale sign of executable-only packages is the lack of entries for modules at the front page of the Hackage docs.) In Linux, the default destination of such executables is ~/.cabal/bin. According to the answers to this question, in OS X the default is ~/Library/Haskell/bin/cabal, unless you are using the Homebrew package manager, which changes it to ~/.cabal/bin. In Windows, it is %APPDATA%\cabal\bin. See also: the Installation paths section in Cabal's user guide.
The gloss-examples package does not install any modules and you do not work with it as a library. Instead, it builds executables (see here) under the names gloss-*. Just run those executables which are wherever you have cabal installing the binaries (typically $HOME/.cabal/bin).

Cabal - Expose all modules while building library

Is it possible to tell Cabal to expose all modules while building a library?
Right now I have to provide very long list of modules in the exposed-modules cabal configurtion file section.
The modern answer is stack + hpack instead of using explicit cabal config. It could automatically expose package modules and provides many other enhancements.
You have to list all modules in the cabal configuration file. In your case, you just put the list of modules after exposed-modules:. There is no simpler way to write a list of modules.
Cabal cannot automatically find the files that are part of an executable or library, so it relies on the list of modules in the configuration file. Unlike GHC, cabal cannot find modules based on import statements in the source code. If you don't list every module, then you may be able to build the project (because GHC can find source files), but other commands such as cabal sdist will not access the source files that aren't listed.

How are Hackage package names mapped to 'cabal install' names?

I'm using cabal to download Haskell packages.
The following works:
> cabal install JSON
It gets Text.JSON
However, this fails:
> cabal install Data.List.Key
cabal: "Data.List.Key" is not valid syntax for a package name or package
dependency.
What is the syntax problem here? How do I make cabal get Data.List.Key? In general, for a package of name X.Y, what name does cabal install need in order to find the package? (I'm confused why cabal install JSON gets Text.JSON, and not Foobarbaz.JSON)
The cabal install command uses package names. Package names are different from module names. If you look on the hackage page for the text package, you'll see that the package name is "text", but it exports a module called Data.Text (amongst others). Packages can export any number of modules and there does not have to be any relationship between the name of the package and the name of the modules it exports.
If you know a package you want, but you don't know the exported modules, look on the hackage page for that package. To do this, I go to "http://hackage.haskell.org/package/" in my browser. I've gone there so many times, it auto-completes very quickly, then I add the package name to the end of that url. If I don't know the exact package name, then I just go to that page and search the package list for what I want.
The converse situation where you know what module you want but don't know what package provides it is a little more difficult. In this case, I rely on the wonderful Haskell search engine Hoogle.(Another one Hayoo, has been offline for a while.)
Packages can include more than one module. There is no rule about how module names map to package names.
If you know the module name and want to find the package it is in, browse it's hackage documentation.
The url of the module description contains the package name after the package part, e.g. the url of Linear.Quaterion is
http://hackage.haskell.org/package/ linear-1.21.1 /docs/Linear-Quaternion.html
On that page the package name is also shown in the top left end.
If you know the package name, you can query which modules are included with
cabal info <package name>.

Which Haskell package contains given module

I know a Haskell module name, but I can't figure out in what package it is defined. This is bad because I can't compile without a package exposing this module.
Specificaly it is Text.Regex that I can't locate, but I would like to know how to solve that problem in general.
http://www.haskell.org/ghc/docs/latest/html/users_guide/packages.html
ghc-pkg find-module Text.Regex
But that only works for (a) recent GHCs, and (b) packages installed on your system.
You can also grep through the package file (e.g. /usr/lib/ghc-6.8.2/package.conf) to see what's installed.
You can also use either the haskell API search engines hoogle or the hackage search engine hayoo.
Text.Regex is in the package regex-base, and a few others built on top of it.
If you're using Cabal and you have the package installed, you can just try to compile it with cabal build, and Cabal will inform you of which package you forgot to add to your dependencies:
Main.hs:1:8:
Could not find module `Text.Regex':
It is a member of the hidden package `regex-compat-0.93.1'.
Perhaps you need to add `regex-compat' to the build-depends in your .cabal file.
Use -v to see a list of the files searched for.
The best tools are:
hoogle; or
hayoo.
Both are search engines for Haskell modules and functions.
If you are using Debian and the Debian-provided packages, there is a global documentation index at /usr/share/doc/ghc-doc/html/libraries/index.html which lists the package in the last column.

Resources