4.8.  Packages

A package is a library of Haskell modules known to the compiler. GHC comes with several packages: see the accompanying library documentation.

Using a package couldn't be simpler: if you're using --make or GHCi, then most of the installed packages will be automatically available to your program without any further options. The exceptions to this rule are covered below in Section 4.8.1, “Using Packages ”.

Building your own packages is also quite straightforward: we provide the Cabal infrastructure which automates the process of configuring, building, installing and distributing a package. All you need to do is write a simple configuration file, put a few files in the right places, and you have a package. See the Cabal documentation for details, and also the Cabal libraries (Distribution.Simple, for example).

4.8.1. Using Packages

To see which packages are installed, use the ghc-pkg command:

$ ghc-pkg list
/usr/lib/ghc-6.4/package.conf:
    base-1.0, haskell98-1.0, template-haskell-1.0, mtl-1.0, unix-1.0,
    Cabal-1.0, haskell-src-1.0, parsec-1.0, network-1.0,
    QuickCheck-1.0, HUnit-1.1, fgl-1.0, X11-1.1, HGL-3.1, OpenGL-2.0,
    GLUT-2.0, stm-1.0, readline-1.0, (lang-1.0), (concurrent-1.0),
    (posix-1.0), (util-1.0), (data-1.0), (text-1.0), (net-1.0),
    (hssource-1.0), rts-1.0
      

Packages are either exposed or hidden. Only modules from exposed packages may be imported by your Haskell code; if you try to import a module from a hidden package, GHC will emit an error message.

Each package has an exposed flag, which says whether it is exposed by default or not. Packages hidden by default are listed in parentheses (eg. (lang-1.0)) in the output from ghc-pkg list. To expose a package which is hidden by default, use the -package flag (see below).

To see which modules are exposed by a package:

$ ghc-pkg field network exposed-modules
exposed-modules: Network.BSD,
                 Network.CGI,
                 Network.Socket,
                 Network.URI,
                 Network

In general, packages containing hierarchical modules are usually exposed by default. However, it is possible for two packages to contain the same module: in this case, only one of the packages should be exposed. It is an error to import a module that belongs to more than one exposed package.

The GHC command line options that control packages are:

-package P

This option causes package P to be exposed. The package P can be specified in full with its version number (e.g. network-1.0) or the version number can be omitted if there is only one version of the package installed.

If there are multiple versions of P installed, then all other versions will become hidden.

The -package P option also causes package P to be linked into the resulting executable. In ––make mode and GHCi, the compiler normally determines which packages are required by the current Haskell modules, and links only those. In batch mode however, the dependency information isn't available, and explicit -package options must be given when linking.

For example, to link a program consisting of objects Foo.o and Main.o, where we made use of the network package, we need to give GHC the -package flag thus:

$ ghc -o myprog Foo.o Main.o -package network

The same flag is necessary even if we compiled the modules from source, because GHC still reckons it's in batch mode:

$ ghc -o myprog Foo.hs Main.hs -package network

In --make and --interactive modes (Section 4.4, “Modes of operation”), however, GHC figures out the packages required for linking without further assistance.

The one other time you might need to use -package to force linking a package is when the package does not contain any Haskell modules (it might contain a C library only, for example). In that case, GHC will never discover a dependency on it, so it has to be mentioned explicitly.

-hide-all-packages

Ignore the exposed flag on installed packages, and hide them all by default. If you use this flag, then any packages you require (including base) need to be explicitly exposed using -package options.

This is a good way to insulate your program from differences in the globally exposed packages, and being explicit about package dependencies is a Good Thing.

-hide-package P

This option does the opposite of -package: it causes the specified package to be hidden, which means that none of its modules will be available for import by Haskell import directives.

Note that the package might still end up being linked into the final program, if it is a dependency (direct or indirect) of another exposed package.

-ignore-package P

Causes the compiler to behave as if package P, and any packages that depend on P, are not installed at all.

Saying -ignore-package P is the same as giving -hide-package flags for P and all the packages that depend on P. Sometimes we don't know ahead of time which packages will be installed that depend on P, which is when the -ignore-package flag can be useful.

4.8.2. The module overlap restriction

The module names in a Haskell program must be distinct. This doesn't sound like a severe restriction, but in a Haskell program using multiple packages with interdependencies, difficulties can start to arise. You should be aware of what the module overlap restriction means, and how to avoid it.

GHC knows which packages are in use by your program: a package is in use if you imported something from it, or if it is a dependency of some other package in use. There must be no conflicts between the packages in use; a conflict is when two packages contain a module with the same name. If GHC detects a conflict, it will issue a message stating which packages are in conflict, and which modules are overlapping.

For example, a conflict might arise if you use two packages, say P and Q, which respectively depend on two different versions of another package, say R-1.0 and R-2.0. The two versions of R are likely to contain at least some of the same modules, so this situation would be a conflict.

4.8.3. Package Databases

A package database is a file, normally called package.conf which contains descriptions of installed packages. GHC usually knows about two package databases:

  • The global package database, which comes with your GHC installation.

  • A package database private to each user. On Unix systems this will be $HOME/.ghc/arch-os-version/package.conf, and on Windows it will be something like C:\Documents And Settings\user\ghc. The ghc-pkg tool knows where this file should be located, and will create it if it doesn't exist (see Section 4.8.5, “Package management (the ghc-pkg command)”).

When GHC starts up, it reads the contents of these two package databases, and builds up a list of the packages it knows about. You can see GHC's package table by running GHC with the -v flag.

Package databases may overlap: for example, packages in the user database will override those of the same name in the global database.

You can control the loading of package databses using the following GHC options:

-package-conf file

Read in the package configuration file file in addition to the system default file and the user's local file. Packages in additional files read this way will override those in the global and user databases.

-no-user-package-conf

Prevent loading of the user's local package database.

To create a new package database, just create a new file and put the string “[]” in it. Packages can be added to the file using the ghc-pkg tool, described in Section 4.8.5, “Package management (the ghc-pkg command)”.

4.8.4. Building a package from Haskell source

We don't recommend building packages the hard way. Instead, use the Cabal infrastructure if possible. If your package is particularly complicated or requires a lot of configuration, then you might have to fall back to the low-level mechanisms, so a few hints for those brave souls follow.

  • You need to build an "installed package info" file for passing to ghc-pkg when installing your package. The contents of this file are described in Section 4.8.6, “ InstalledPackageInfo: a package specification ”.

  • The Haskell code in a package may be built into one or more archive libraries (e.g. libHSfoo.a), or a single DLL on Windows (e.g. HSfoo.dll). The restriction to a single DLL on Windows is because the package system is used to tell the compiler when it should make an inter-DLL call rather than an intra-DLL call (inter-DLL calls require an extra indirection). Building packages as DLLs doesn't work at the moment; see Section 11.5.1, “Creating a DLL” for the gory details.

    Building a static library is done by using the ar tool, like so:

    ar cqs libHSfoo.a A.o B.o C.o ...

    where A.o, B.o and so on are the compiled Haskell modules, and libHSfoo.a is the library you wish to create. The syntax may differ slightly on your system, so check the documentation if you run into difficulties.

    Versions of the Haskell libraries for use with GHCi may also be included: GHCi cannot load .a files directly, instead it will look for an object file called HSfoo.o and load that. On some systems, the ghc-pkg tool can automatically build the GHCi version of each library, see Section 4.8.5, “Package management (the ghc-pkg command)”. To build these libraries by hand from the .a archive, it is possible to use GNU ld as follows:

    ld -r ––whole-archive -o HSfoo.o libHSfoo.a

    (replace ––--whole-archive with –all_load on MacOS X)

    GHC does not maintain detailed cross-package dependency information. It does remember which modules in other packages the current module depends on, but not which things within those imported things.

It is worth noting that on Windows, when each package is built as a DLL, since a reference to a DLL costs an extra indirection, intra-package references are cheaper than inter-package references. Of course, this applies to the Main package as well.

4.8.5. Package management (the ghc-pkg command)

The ghc-pkg tool allows packages to be added or removed from a package database. By default, the system-wide package database is modified, but alternatively the user's local package database or another specified file can be used.

Commands which only inspect the database (list, describe, field) will take into account the user's local package database too, unless the --global option is given. This matches the behaviour of GHC, which automatically reads the user's local database if it is available.

The ghc-pkg program may be run in the ways listed below. Where a package name is required, the package can be named in full including the version number (e.g. network-1.0), or without the version number. Naming a package without the version number matches all versions of the package; the specified action will be applied to all the matching packages. A package specifier that matches all version of the package can also be written pkg-*, to make it clearer that multiple packages are being matched.

ghc-pkg register file

Reads a package specification from file (which may be “-” to indicate standard input), and adds it to the database of installed packages. The syntax of file is given in Section 4.8.6, “ InstalledPackageInfo: a package specification ”.

The package specification must be a package that isn't already installed.

ghc-pkg update file

The same as register, except that if a package of the same name is already installed, it is replaced by the new one.

ghc-pkg unregister P

Remove the specified package from the database.

ghc-pkg expose P

Sets the exposed flag for package P to True.

ghc-pkg hide P

Sets the exposed flag for package P to False.

ghc-pkg list [P] [--simple-output]

This option displays the currently installed packages, for each of the databases known to ghc-pkg. That includes the global database, the user's local database (if --user is given), and any further files specified using the -f option on the command line.

Hidden packages (those for which the exposed flag is False) are shown in parentheses in the list of packages.

If an optional package identifier P is given, then only packages matching that identifier are shown.

If the option --simple-output is given, then the packages are listed on a single line separated by spaces, and the database names are not included. This is intended to make it easier to parse the output of ghc-pkg list using a script.

ghc-pkg latest P

Prints the latest available version of package P.

ghc-pkg describe P

Emit the full description of the specified package. The description is in the form of an InstalledPackageInfo, the same as the input file format for ghc-pkg register. See Section 4.8.6, “ InstalledPackageInfo: a package specification ” for details.

ghc-pkg field P field

Show just a single field of the installed package description for P.

Additionally, the following flags are accepted by ghc-pkg:

––auto-ghci-libs

Automatically generate the GHCi .o version of each .a Haskell library, using GNU ld (if that is available). Without this option, ghc-pkg will warn if GHCi versions of any Haskell libraries in the package don't exist.

GHCi .o libraries don't necessarily have to live in the same directory as the corresponding .a library. However, this option will cause the GHCi library to be created in the same directory as the .a library.

-f file , -package-conf file

Operate on the package database in file.

When multiple -f options are given, or -f is used in conjunction with --user or --global, the last one on the command-line is the one that will be modified.

––force

Causes ghc-pkg to ignore missing dependencies, directories and libraries when registering a package, and just go ahead and add it anyway. This might be useful if your package installation system needs to add the package to GHC before building and installing the files.

––global

Operate on the global package database (this is the default). This flag affects the register, update, unregister, expose, and hide commands.

––help , -?

Outputs the command-line syntax.

––user

Operate on the current user's local package database. This flag affects the register, update, unregister, expose, and hide commands.

-V , ––version

Output the ghc-pkg version number.

When modifying the package database file, a copy of the original file is saved in file.old, so in an emergency you can always restore the old settings by copying the old file back again.

4.8.6.  InstalledPackageInfo: a package specification

A package specification is a Haskell record; in particular, it is the record InstalledPackageInfo in the module Distribution.InstalledPackageInfo, which is part of the Cabal package distributed with GHC.

An InstalledPackageInfo has a human readable/writable syntax. The functions parseInstalledPackageInfo and showInstalledPackageInfo read and write this syntax respectively. Here's an example of the InstalledPackageInfo for the unix package:

$ ghc-pkg describe unix
name: unix
version: 1.0
license: BSD3
copyright:
maintainer: libraries@haskell.org
stability:
homepage:
package-url:
description:
category:
author:
exposed: True
exposed-modules: System.Posix,
                 System.Posix.DynamicLinker.Module,
                 System.Posix.DynamicLinker.Prim,
                 System.Posix.Directory,
                 System.Posix.DynamicLinker,
                 System.Posix.Env,
                 System.Posix.Error,
                 System.Posix.Files,
                 System.Posix.IO,
                 System.Posix.Process,
                 System.Posix.Resource,
                 System.Posix.Temp,
                 System.Posix.Terminal,
                 System.Posix.Time,
                 System.Posix.Unistd,
                 System.Posix.User,
                 System.Posix.Signals.Exts
import-dirs: /usr/lib/ghc-6.4/libraries/unix
library-dirs: /usr/lib/ghc-6.4/libraries/unix
hs-libraries: HSunix
extra-libs: HSunix_cbits, dl
include-dirs: /usr/lib/ghc-6.4/libraries/unix/include
includes: HsUnix.h
depends: base-1.0

The full Cabal documentation is still in preparation (at time of writing), so in the meantime here is a brief description of the syntax of this file:

A package description consists of a number of field/value pairs. A field starts with the field name in the left-hand column followed by a “:”, and the value continues until the next line that begins in the left-hand column, or the end of file.

The syntax of the value depends on the field. The various field types are:

freeform

Any arbitrary string, no interpretation or parsing is done.

string

A sequence of non-space characters, or a sequence of arbitrary characters surrounded by quotes "....".

string list

A sequence of strings, separated by commas. The sequence may be empty.

In addition, there are some fields with special syntax (e.g. package names, version, dependencies).

The allowed fields, with their types, are:

name

The package's name (without the version).

version

The package's version, usually in the form A.B (any number of components are allowed).

license

(string) The type of license under which this package is distributed. This field is a value of the License type.

license-file

(optional string) The name of a file giving detailed license information for this package.

copyright

(optional freeform) The copyright string.

maintainer

(optinoal freeform) The email address of the package's maintainer.

stability

(optional freeform) A string describing the stability of the package (eg. stable, provisional or experimental).

homepage

(optional freeform) URL of the package's home page.

package-url

(optional freeform) URL of a downloadable distribution for this package. The distribution should be a Cabal package.

description

(optional freeform) Description of the package.

category

(optinoal freeform) Which category the package belongs to. This field is for use in conjunction with a future centralised package distribution framework, tentatively titled Hackage.

author

(optional freeform) Author of the package.

exposed

(bool) Whether the package is exposed or not.

exposed-modules

(string list) modules exposed by this package.

hidden-modules

(string list) modules provided by this package, but not exposed to the programmer. These modules cannot be imported, but they are still subject to the overlapping constraint: no other package in the same program may provide a module of the same name.

import-dirs

(string list) A list of directories containing interface files (.hi files) for this package.

If the package contains profiling libraries, then the interface files for those library modules should have the suffix .p_hi. So the package can contain both normal and profiling versions of the same library without conflict (see also library_dirs below).

library-dirs

(string list) A list of directories containing libraries for this package.

hs-libraries

(string list) A list of libraries containing Haskell code for this package, with the .a or .dll suffix omitted. When packages are built as libraries, the lib prefix is also omitted.

For use with GHCi, each library should have an object file too. The name of the object file does not have a lib prefix, and has the normal object suffix for your platform.

For example, if we specify a Haskell library as HSfoo in the package spec, then the various flavours of library that GHC actually uses will be called:

libHSfoo.a

The name of the library on Unix and Windows (mingw) systems. Note that we don't support building dynamic libraries of Haskell code on Unix systems.

HSfoo.dll

The name of the dynamic library on Windows systems (optional).

HSfoo.o, HSfoo.obj

The object version of the library used by GHCi.

extra-libs

(string list) A list of extra libraries for this package. The difference between hs-libraries and extra-libs is that hs-libraries normally have several versions, to support profiling, parallel and other build options. The various versions are given different suffixes to distinguish them, for example the profiling version of the standard prelude library is named libHSbase_p.a, with the _p indicating that this is a profiling version. The suffix is added automatically by GHC for hs-libraries only, no suffix is added for libraries in extra-libs.

The libraries listed in extra-libs may be any libraries supported by your system's linker, including dynamic libraries (.so on Unix, .DLL on Windows).

Also, extra-libs are placed on the linker command line after the hs-libraries for the same package. If your package has dependencies in the other direction (i.e. extra-libs depends on hs-libraries), and the libraries are static, you might need to make two separate packages.

include-dirs

(string list) A list of directories containing C includes for this package.

includes

(string list) A list of files to include for via-C compilations using this package. Typically the include file(s) will contain function prototypes for any C functions used in the package, in case they end up being called as a result of Haskell functions from the package being inlined.

depends

(package name list) Packages on which this package depends. This field contains packages with explicit versions are required, except that when submitting a package to ghc-pkg register, the versions will be filled in if they are unambiguous.

extra-hugs-opts

(string list) Options to pass to Hugs for this package.

extra-cc-opts

(string list) Extra arguments to be added to the gcc command line when this package is being used (only for via-C compilations).

extra-ld-opts

(string list) Extra arguments to be added to the gcc command line (for linking) when this package is being used.

framework-dirs

(string list) On Darwin/MacOS X, a list of directories containing frameworks for this package. This corresponds to the -framework-path option. It is ignored on all other platforms.

extra-frameworks

(string list) On Darwin/MacOS X, a list of frameworks to link to. This corresponds to the -framework option. Take a look at Apple's developer documentation to find out what frameworks actually are. This entry is ignored on all other platforms.

haddock-interfaces

(string list) A list of filenames containing Haddock interface files (.haddock files) for this package.

haddock-html

(optional string) The directory containing the Haddock-generated HTML for this package.