Haskell

From ArchWiki
Jump to: navigation, search

Haskell is a general purpose, purely functional, programming language.

Installation

Haskell generates machine code that can be run natively on Linux. There is nothing special required to run a binary (already compiled) software, like the ones provided in the official repositories or by the ArchHaskell group. On the other side, AUR packages or source codes requires a compiler to build the software.

Installing the compiler alone permits to build Haskell source code. A few additional tools are needed for development work.

Compiler

To build a Haskell source code into native code, a compiler must be installed. There are several implementations available, but the one used most (which is now de facto the reference) is the GHC (Glasgow Haskell Compiler). It is available in the official repositories as ghc.

You can try it with the following file:

Main.hs
main = putStrLn "Hello, World"

and by running:

$ ghc -dynamic Main.hs
$ ./Main 
Hello, World

Problems with linking

Since version 8.0.2-1, the Arch ghc package no longer installs static versions of the GHC boot libraries by default, nor do any of the haskell-* packages in community. Therefore, to link successfully one must pass the -dynamic flag to GHC, as the default is to use static linking. For Cabal, this amounts to the following configuration:

cabal configure --disable-library-vanilla --enable-shared --enable-executable-dynamic
  • --disable-library-vanilla suppresses the creation of static libraries (if your project contains a library).
  • --enable-shared enables the creation of shared libraries (if your project contains a library).
  • --enable-executable-dynamic causes dynamic linking to be used for executables (if your project contains executables).

You can also set these flags in ~/.cabal/config so that it applies to all projects by default:

~/.cabal/config
library-vanilla: False
shared: True
executable-dynamic: True

Dynamic linking is used for most Haskell modules packaged through pacman and some packages in the AUR. Since GHC provides no ABI compatibility between compiler releases, static linking is often the preferred option for local development outside of the package system.

Note: In the context of this article, static linking does not mean generating completely static ELF binaries. Only Haskell code will be linked statically into a single ELF binary, which may be dynamically linked to other system libraries such as glibc.

Static linking

To use static linking, one must, at minimum, install the static boot libraries through the ghc-static package. This would allow you to build projects that depend exclusively on the boot libraries as well as any library that is not installed through the haskell-* packages.

Unfortunately, if your project depends on any of the haskell-* packages that you have installed, Cabal does not take the absence of static libraries into account during dependency resolution. As a result, it will try to use the existing haskell-* package and then fail with linker errors when it realizes the static libraries are missing. Unlike ghc-static, there are no “haskell-*-static” packages to fix this problem.

To work around this problem, you can install ghc-pristineAUR, which wraps over an existing GHC installation to create a separate GHC distribution in /usr/share/ghc-pristine, with a package database that contains only boot libraries. Then, to build software with static linking, one simply needs to invoke the wrapped compiler /usr/share/ghc-pristine/bin/ghc. For Cabal, this amounts to the following configuration:

cabal configure --with-compiler=/usr/share/ghc-pristine

This can be made permanent by editing ~/.cabal/config. Be aware that you still need ghc-static.

Alternatives to ghc-pristine:

  • One could manually run cabal install --force-reinstalls to shadow the corresponding haskell-* packages. This can be tedious as you must explicitly enumerate all transitive dependencies that coincide with an existing haskell-* package.
  • Use a completely separate GHC distribution: download the official Linux binaries for GHC and cabal-install and unpack them somewhere else. This is in effect what ghc-pristine does, but ghc-pristine uses less disk space.

Building statically linked packages with Cabal (without using shared libraries)

This section explains how to install cabal-install but from Hackage instead of the official cabal-install package from the repositories. This cabal-install will build Haskell packages without using shared libraries unlike the official cabal-install which requires you to link dynamically.

In theory with any cabal-install you could choose between both methods, static and dynamic, for linking your Haskell code. In practice because in Arch some basic Haskell libraries (haskell-* packages) are provided as shared objects (.so files) and those libraries are globally registered in Cabal, it has trouble using the same libraries for static linking. To avoid linking errors, it's also especially important to not to mix statically and dynamically Haskell packages installed on the same system, as Cabal doesn't fetch a required package once it has been globally registered (check them with the command ghc-pkg list) in one of its forms and not the other (independently of the linking type of the package that it's needed).

For these reasons, you have to make sure that the only two related Haskell packages you have installed are ghc, the compiler, and ghc-static, the boot libraries on its static form, (not stack, cabal-helper, cabal-install and none of the haskell-* dynamic libraries available in the official repositories).

You can also use Stack as an alternative build tool for Haskell packages, which will link statically by default. But if you still want to use Cabal to build using static linking, follow the next steps of this section to install your own compiled cabal-install from Hackage. For this purpose we are going to use the tool Stack that will help us fetching all the dependencies required and building your own cabal-install.

First you have to install stack-staticAUR because you'll use it to bootstrap the compilation of your own Cabal and you don't want to pull as dependencies those packages from the official Arch repositories containing the haskell-* dynamic libraries.

Then prepare Stack downloading the package index. Stack will be used with the only purpose of bootstrapping the compilation of your own cabal-install but using the already installed ghc compiler:

$ stack setup --system-ghc

Now install your own cabal-install:

$ stack install --system-ghc cabal-install

This newly installed cabal-install has been compiled without shared libraries and won't use them when building packages by default. Also, this cabal-install will use the installed ghc compiler.

Haskell development tools

To start developing in Haskell easily, one option is the haskell-platform bundle which is described as:

The easiest way to get started with programming Haskell. It comes with all you need to get up and running. Think of it as "Haskell: batteries included".

Although an AUR package exists (haskell-platformAUR[broken link: archived in aur-mirror]), the Haskell Platform can be advantageously replaced by installing the following packages from the official repositories:

Alternatively, you can use stack to manage your Haskell environment by following the Arch Linux install instructions.

Managing Haskell packages

Many Haskell libraries and executables are grouped in packages. They are all available on Hackage, and a subset of them is curated on Stackage. To install and manage these packages, several methods are available:

cblrepo is a tool used for maintaining Haskell packages for Linux distributions. A wrapper around this, cabal2pkgbuild-gitAUR[broken link: archived in aur-mirror], can create PKGBUILD files from Hackage packages. See Haskell package guidelines for more information on creating new Haskell packages.

Pros/Cons of the different methods

The following table documents the advantages and disadvantages of different package management styles.

Method Pros Cons
Official repositories Provided by ArchLinux developers, consistent versions of packages, already compiled Only a few packages available, only dynamic libraries available
ArchHaskell repository Provided by ArchHaskell group, consistent versions of packages, already compiled Need manual intervention to get started with
cabal-install All packages available, root not required Installed in home directory, failures in dependency resolution, difficult to remove specific packages
stack All packages available (favors Stackage), root not required Installed in home directory, versions are pinned to snapshot, difficult to remove specific packages
Arch User Repository Simple to get started Risk of unmaintained or orphaned packages, incompatible versions of packages possible

ArchHaskell repository

See ArchHaskell for details.

cabal-install

cabal-install is available from the official repositories.

Note: cabal-install is the package that contains the "cabal" command-line utility, whereas Cabal is the package that provides the "Cabal" library, used by both cabal-install and stack.

Preparation and $PATH

To run user-wide executables installed by cabal-install, ~/.cabal/bin must be added to the $PATH variable. That can be done by putting the following line in your shell configuration file, for instance ~/.bashrc for bash or ~/.zshrc for zsh:

PATH=$PATH:~/.cabal/bin

Installing packages user-wide

$ cabal update

$ # Dynamic linking
$ cabal install --disable-library-vanilla --enable-shared --enable-executable-dynamic <pkg>

$ # Static linking (requires ghc-static and ghc-pristine)
$ cabal install --with-compiler=/usr/share/ghc-pristine/bin/ghc <pkg>

It is possible to install a package system-wide with the --global flag, but this is strongly discouraged. With the user-wide install, all files are kept in ~/.cabal and libraries are registered to ~/.ghc, offering the possibility to do a clean-up easily by simply removing these folders. With system-wide install, the files will be dispersed in the file system and difficult to manage.

Sandboxes

Cabal sandboxes provide a consistent local package database and environment (similar to virtual-env in Python or rvm in Ruby). To create a sandbox in the current directory, run:

cabal sandbox init
It can be later removed using
cabal sandbox delete

By default, if the current directory contains a sandbox, cabal will take advantage of it for installation, so you can follow the same steps as #Installing packages user-wide. If you want to use a sandbox elsewhere, you can – while the current directory contains the sandbox – run cabal exec "$SHELL" to start a sandbox-aware shell. Then you can change the directory to wherever you want, and cabal will still use the sandbox. Alternatively, you can pass the --sandbox-config-file=/somewhere/cabal.sandbox.config flag to cabal.

To run executables within a cabal sandbox, you must also set
PATH=$PATH:$PWD/.cabal-sandbox/bin
or start a local shell with cabal exec "$SHELL".

Removing packages

There is no easy way to do it. Cabal does not have removing process.

Consider installing to a sandbox instead, which can be deleted without affecting other sandboxes or your user-wide installation.

One thing to make your life easier is use zsh auto completion to find all the haskell packages.

If you want/can fix/reinstall whole user-wide Haskell package system - remove ~/.cabal and ~/.ghc and start from scratch. This is often necessary when GHC is upgraded.

stack

Stack is a build tool that focuses on automatically curated, consistent package sets rather than dependency resolution. This means it's easy to install a set of packages without concern of version conflicts as long as they co-exist within a given Stackage snapshot. It can be installed through either stack or stack-staticAUR. The latter provides statically linked binaries, thereby avoiding dozens of haskell-* dependencies. More information can be found at § Install/upgrade # Arch Linux.

See also