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

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

Related

How to (install and) use package in 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.

cabal build-depends: how to find?

How can I find out what needs to go into the module.cabal build-depends? I mean, some modules may already be part of the Haskell platform whilst others may not? How to I find out/know what I must write here so that the module I offer will install with cabal on the majority of Haskell installations "out there"?
My situation is that I have it working on my systems, but cannot remember for what import I actually had to install an other module and what was part of the Haskell Platform that I use. How do I now best find the way from my situation to a cabal installable package?
If you use Cabal to build your project, it will only look at the modules listed in the .cabal file, even if you have other modules installed locally.
So all you have to do is run it as is (with nothing in the depends declaration) and it will give you an error for each module you need to specify. I think the error even tells you the name of the package.

How to include hackage packages into Leksah

I wanted to start playing with hExpat for Xml I/O with Haskell.
However I didn't manage to find where to express to Leksah that I want to import that package into my current module.
Could you tell me how to achieve this ?
EDIT: still searching. There is some uselful info with this Q&A but it is only about Data.Time Data.Directory.
In my case, it is a downloaded, unzipped, Hackage package.
The first thing is, that you have to install the package. Just run cabal and type:
cabal install YOUR PACKAGE NAME HERE
Than, open Leksah, open the project's dependencies (package -> edit package -> dependencies) and enter your new package. Hit "save" afterwards. Now you have to reconfigure the package and afterwards it should work.

Could not find module `Control.Monad.Reader'

Today when I tried to compile my code in Geany I got this error
Could not find module `Control.Monad.Reader':
it was found in multiple packages: monads-fd-0.1.0.1 mtl-1.1.0.2
Compilation failed.
I get the same for the Writer monad; I thought I should remove one of those packages, but I do not want to break other packages, so now what should I do, yesterday everything worked without any problem.
It looks like you have recently installed monads-fd, perhaps as a dependency of something else you installed. Both monads-fd and mtl packages contain the module Control.Monad.Reader, so GHC doesn't know which one to use when you compile some code that imports it. You need to resolve the ambiguity somehow:
If you are using GHC or GHCi directly
either use a -hide-package <package> flag on the command line to hide one of the packages, or
hide the package by default using ghc-pkg hide <package>. You may need to use ghc-pkg --user hide <package> if the package was installed in your home directory (the default on some platforms).
You can use Cabal, and say exactly which one of the conflicting packages you depend on using the build-depends field in your .cabal file
I encountered a similar problem recently, and it was suggested that I run ghc-pkg hide {x} where '{x}' is the name of one of those packages. It worked in my situation.

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