Haskell

From ArchWiki
(Redirected from Stack (Haskell))
Jump to navigation Jump to search

From Wikipedia:

Haskell is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Developed to be suitable for teaching, research and industrial application, Haskell has pioneered a number of advanced programming language features such as type classes, which enable type-safe operator overloading. Haskell's main implementation is the Glasgow Haskell Compiler (GHC). It is named after logician Haskell Curry.

Installation

Note: There are several choices for Haskell installation, one is supported by Arch Linux, while others are officially supported by Haskell for any Linux distributions. This article describes Arch way of installing and using Haskell. See #Alternate installations for more information about other installation methods.

Haskell generates machine code that can be run natively on Linux. There is nothing special required to run a binary (already compiled) software, like xmonad or pandoc provided in the official repositories. On the other side, building AUR packages or developing software require a compiler and build tools to be installed.

To install the latest version of Haskell, install the following packages from the official repositories:

  • ghc (GHC) — A Haskell compiler. There are several implementations available, but the one used most (which is now de facto the reference) is the GHC (Glasgow Haskell Compiler).
  • cabal-install (Cabal) — A build tool focused on dependency resolution and sources packages from Hackage. Hackage is the Haskell community's central package archive of open source software.
  • stack (Stack) — A build tool focused on curated snapshots and sources packages from Stackage. Stackage is a stable subset of Hackage that provides curated sets (snapshots) of packages known to work well with each other. Alternatively, Stack can be installed through stack-staticAUR package. It provides statically linked binaries, thereby avoiding dozens of haskell-* dependencies.

You can install and configure either Cabal or Stack or both of them. Most of the popular Haskell packages support both Cabal and Stack.

Configuration

Since version 8.0.2-1, the Arch ghc package and all haskell-* packages in community provide only dynamically linked libraries. Therefore, to link successfully one must configure GHC, Cabal and Stack for dynamic linking, as the default is to use static linking.

Using dynamic linking typically results in faster builds and smaller disk and RAM usage (by sharing pages between multiple running Haskell programs), and will free you from troubleshooting cross-GHC mixing errors. But it has its own disadvantage: all tools you install from source will break on every update of ghc, ghc-libs or haskell-* packages since libraries compiled with GHC do not provide a stable ABI. When running such broken binary, you will see the usual message error while loading shared libraries: libHS...so: cannot open shared object file: No such file or directory. To fix this, just rebuild and reinstall the broken tool in order to relink it to newer libraries.

On the other hand, static linking is generally easier to maintain and does not force you to rebuild all tools from source after every update of their dependencies. For these reasons, static linking is often the preferred option for local development outside of the package system. If you prefer static linking, see #Static linking or #Alternate installations for details.

Note: For a detailed explanation of why Arch moved to dynamic linking for Haskell packages, see this maintainer's response.

Invoking GHC directly

In order to link successfully one must pass the -dynamic flag to GHC. You can try it with the following file:

Main.hs
main = putStrLn "Hello, World"

Compile and run it with:

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

Configuring Cabal for dynamic linking

First, run the following command to download the latest list of packages from Hackage and create global configuration file ~/.cabal/config (or the file $CABAL_CONFIG points to):

$ cabal update
Tip: It is advisable to periodically run cabal update to synchronize your local list of packages and dependencies with the newest version on Hackage.

To configure Cabal for dynamic linking, uncomment and edit the following options in ~/.cabal/config:

~/.cabal/config
library-vanilla: False
shared: True
executable-dynamic: True
program-default-options
  ghc-options: -dynamic
  • library-vanilla: False suppresses the creation of static libraries (if your project contains a library).
  • shared: True enables the creation of shared libraries (if your project contains a library).
  • executable-dynamic: True causes dynamic linking to be used for executables (if your project contains executables).
  • ghc-options: -dynamic adds the -dynamic flag to every invocation of GHC (e.g. if a package has a non-trivial Setup.hs).

Configuring Stack for dynamic linking

You can use stack setup command to initialize Stack and create global configuration file ~/.stack/config.yaml. By default Stack will automatically download its own version of GHC to an isolated location upon first invocation. To force Stack to use system GHC installation instead, run stack setup with --system-ghc and --resolver flags:

$ stack setup --system-ghc --resolver resolver

Note that you need to specify a resolver which is compatible with your system GHC. Otherwise Stack will happily ignore --system-ghc flag and download its own copy of GHC. You can determine the version of system GHC using ghc --version command:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.10.2

Then visit Stackage website and pick a suitable Long Term Support (LTS) or nightly snapshot matching your system GHC version. Use the selected snapshot for --resolver flag on the command line, e.g. --resolver lts-16.15 or --resolver nightly-2020-09-01.

Stackage typically lags behind new GHC releases. It may happen that no Stackage snapshot for your system GHC has yet been released. In this case you might want to choose a snapshot for some earlier minor version of GHC or temporarily downgrade your Haskell installation and wait until support for newer GHC releases finally lands on Stackage.

To configure Stack for dynamic linking, add the following snippet to ~/.stack/config.yaml:

~/.stack/config.yaml
# Stop downloading GHCs into isolated locations under ~/.stack.
install-ghc: false

# Allow Stack to pick the system GHC (false by default).
system-ghc: true

# Allow to use, say, Stackage snapshot for GHC 8.8.2 with system GHC 8.8.3.
compiler-check: newer-minor

# Add the -dynamic flag to every invocation of GHC.
ghc-options:
  "$everything": -dynamic

Package management

Most of Haskell libraries and executables are distributed in units of source packages available from Hackage and Stackage.

As is common in other compiled languages, a number of popular Haskell packages are available from official Arch repositories in pre-built form. Some additional packages can be installed from AUR.

Although it is recommended to use pacman to install GHC, libraries and tools from official repositories — at some point you may want to install Haskell packages directly from Hackage/Stackage or compile your own (or somebody else's) packages from source. You will need Cabal or Stack for doing that.

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

Method Pros Cons
Official repositories Provided by Arch Linux developers, consistent versions of packages, already compiled Not all packages available, only dynamic libraries available
Cabal 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
AUR Additional packages available Risk of unmaintained or orphaned packages, incompatible versions of packages possible

Cabal

Note: In Haskell, the term Cabal can refer to either:
  • Cabal file format that describes Haskell packages and their dependencies;
  • Cabal library that works with Cabal file format;
  • cabal command-line tool (provided by cabal-install package) that uses Cabal library to build Haskell packages.
In the context of this article, Cabal usually means cabal command-line tool unless specified otherwise.

Cabal is "the original" build system for Haskell. Most of libraries and tools you can find on Hackage can be installed via Cabal.

Installing packages

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

export PATH="$HOME/.cabal/bin:$PATH"

Run the following command to install a Hackage package and all of its dependencies in a single step:

$ cabal install --ghc-options=-dynamic package
Note: As of October 2020 Cabal ignores ghc-options from ~/.cabal/config while building packages with build-type: Custom. Therefore, it is necessary to specify --ghc-options=-dynamic flag on the command line otherwise you might experience build errors in setup.hs like Could not find module ‘Prelude’ There are files missing in the ‘base-...’ package.

You can also build and install a Haskell package from source. To do this, run the following command from the package directory:

$ cabal install --ghc-options=-dynamic

Each Cabal package should specify a list of its dependencies and their version constraints in the .cabal file according to the Package Versioning Policy (PVP). During the package installation, Cabal tries to find a set of dependencies that satisfies all the constraints. This process is called dependency resolution.

There are reasons why Stack exists; Cabal is known to generate a lot of friction with beginners. Most of the time dependency resolution works well but sometimes it fails. In this case you will need to figure out the cause of the problem and give Cabal some hints about how to resolve offending dependencies. For example, sometimes it is necessary to say cabal install --allow-newer --ghc-options=-dynamic package to allow Cabal to ignore package's PVP-dictated upper bounds on dependency versions, effectively installing package with newer dependencies than the package author has permitted. It gets hairier for less-well maintained packages; for another example, see this thread about installing Idris (another programming language, written in Haskell), where one had to use both --allow-newer and --constraint='haskeline < 0.8.0.0' command-line flags to get a successful compile.

Removing packages

There is no easy way to do it. Cabal does not have support for this functionality but there are external tools like cabal-store-gc.

If you want to reinstall the entire user-wide Haskell package system, remove ~/.cabal and ~/.ghc and start from scratch. This is often necessary when GHC is upgraded.

Stack

Stack is another tool to manage Haskell packages. It pursuits slightly different goals than Cabal, with a slightly different philosophy. It uses Cabal library under the hood and integrates with Hackage — but maintains its own repositories of packages (snapshots) on Stackage with the promise that snapshots are curated and include packages which work well together.

Installing packages

In its default configuration, Stack installs compiled executables to ~/.local/bin. Add this directory to the $PATH environment variable in your shell configuration file, for instance ~/.bashrc for bash or ~/.zshrc for zsh:

export PATH="$HOME/.local/bin:$PATH"

Run the following command to download, build and install a Stackage package:

stack install package
Note: As of October 2020 Stack ignores ghc-options from ~/.stack/config.yaml while building Setup.hs. If you experience build errors in Setup.hs like Could not find module ‘Prelude’ There are files missing in the ‘base-...’ package, try to install ghc-static package as a workaround.

You can also build and install a Haskell package from source by running the following command from the package directory:

stack install --resolver resolver

Note that you should specify the same resolver as one used for stack setup command.

Removing packages

Stack does not support the "uninstall" operation.

If you want to reinstall the entire user-wide Haskell package system, remove ~/.stack directory and start from scratch. This is often necessary when GHC is upgraded.

Alternate installations

Note: This section describes alternative ways of installing Haskell on Arch without using packages from the official repositories. If you previously installed GHC, Cabal, Stack or any other Haskell packages, make sure to uninstall them first. Remove ~/.cabal and ~/.stack directories if they exist.

There are two officially recommended methods of installing Haskell on any generic Linux distribution: ghcup and Stack. Both methods install statically linked GHC, tools and libraries in your home directory.

The advantage of using ghcup or Stack instead of Haskell packages from the official repositories is the ability to install and manage multiple versions of GHC side by side. Cabal and Stack installed this way typically work right out of the box without any additional configuration, which might be easier for beginners.

A completely different way of installing Haskell is Nix package manager. Nix has a steeper learning curve but offers greater flexibility in managing both Haskell and non-Haskell packages in a reliable and reproducible fashion.

ghcup

ghcup is a command line tool that allows to easily install multiple versions of GHC and switch between them. It is similar in scope to rustup, pyenv and jenv.

Install ghcup-hs-binAUR package. Alternatively, you may follow official installation instructions or manually download ghcup binary and place it somewhere into your $PATH.

By default, ghcup will install GHC executables into ~/.ghcup/bin. You need to add this directory to the $PATH environment variable in your shell configuration file, for instance ~/.bashrc for bash or ~/.zshrc for zsh. If you want to run executables installed by Cabal, add ~/.cabal/bin to $PATH as well:

export PATH="$HOME/.cabal/bin:$HOME/.ghcup/bin:$PATH"
Tip: If you want to use XDG style directories, define GHCUP_USE_XDG_DIRS environment variable (https://gitlab.haskell.org/haskell/ghcup-hs/#xdg-support).

ghcup provides a convenient TUI which supports most of its functionality:

$ ghcup tui

Alternatively, you can use the following CLI commands:

List available versions of GHC and Cabal:

$ ghcup list

Install the recommended version of GHC:

$ ghcup install ghc

You can also install a specific version of GHC, for example:

$ ghcup install ghc 8.10.2

The commands above do not automatically make GHC available on the $PATH. You need to select which GHC version to use by default:

$ ghcup set ghc 8.10.2

Install the recommended version of Cabal:

$ ghcup install cabal

You can now use Cabal to build and install statically linked Haskell executables without any special configuration or command line flags:

$ cabal update
$ cabal install executable

To start a new Cabal project:

$ cd /path/to/my-project
$ cabal init
$ cabal build
$ cabal run
Up to date
Hello, Haskell!

For more information, refer to official ghcup and Cabal documentation.

Stack

Install stack-staticAUR package. Alternatively, you may follow official installation instructions or manually download Stack binary and place it somewhere into your $PATH.

If you want to run executables installed by Stack, add ~/.local/bin directory to the $PATH environment variable in your shell configuration file, for instance ~/.bashrc for bash or ~/.zshrc for zsh:

export PATH="$HOME/.local/bin:$PATH"

Run stack setup to automatically install GHC from the latest Stackage LTS snapshot:

$ stack setup

You can now use Stack to build and install statically linked Haskell packages without any special configuration or command line flags:

$ stack install package

For more information, refer to official Stack documentation.

Nix

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: I cannot offer a good enough overview, due to no experience with it (Discuss in Talk:Haskell#)

IDE support

Tools

ghcid

ghcid provides a simple and robust way to display compiler messages on source code change.

Linters

hlint is a linter, suggesting possible improvements to Haskell code.

stan is another linter, complementary to hlint. It is in its early stages as of January 2021.

haskell-language-server

haskell-language-server is a Language Server Protocol (LSP) implementation for Haskell. It provides IDE-like features like type-on-hover or autocompletions for any editor integrating with the LSP.

There are pre-built binaries which can be installed via haskell-language-server-binAUR, #ghcup or by the Haskell extension for Visual Studio Code.

Formatters

Formatter Notes
brittany
floskell
ormolu not configurable by design
fourmolu fork of ormolu, adding configurability
stylish-haskell

Editors

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: I don't have experience with other editors (Discuss in Talk:Haskell#)

Emacs

Basic Emacs support for Haskell is provided by the official haskell-mode. For more advanced features, also use lsp-haskell with #haskell-language-server.

Visual Studio Code

Visual Studio Code support is provided by the official Haskell plugin.

Tips and tricks

Static linking

Note: This section explains how to build statically linked Haskell packages on Arch, but still use GHC installed from the official repositories. Before you proceed, make sure to remove ~/.cabal and ~/.stack directories if they exist.
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.

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 on any other libraries that are not installed through the haskell-* packages from the official repositories.

Unfortunately, if your project depends on any of dynamically linked 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.

There are multiple ways to work around this issue. One involves compilation of Haskell packages in a separate build environment provided by ghc-pristineAUR package, while others use statically linked cabal-staticAUR or stack-staticAUR packages instead of their dynamically linked counterparts from the official repositories.

ghc-pristine

Install ghc-pristineAUR package, 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. This effectively creates a semi-isolated environment without dynamically linked haskell-* packages, but still makes use of the GHC compiler from the official repositories. 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 in ~/.cabal/config:

~/.cabal/config
with-compiler: /usr/share/ghc-pristine/bin/ghc

You can also specify the path to the compiler on a per-project basis by running the following command from the project directory:

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

cabal-static

Another way to gain back static linking on Arch is to install cabal-staticAUR package. Unlike the official cabal-install, this one does not pull dynamically linked haskell-* dependencies from the official repositories and avoids mixing static and shared Haskell libraries installed on the same system. Then you can use Cabal as usual with the following limitation: you have to make sure that the only other Haskell packages you have installed are ghc, ghc-libs and ghc-static (not cabal-install, stack and none of the haskell-* packages available in the official repositories).

stack-static

Install stack-staticAUR package. Similarly to cabal-static method, make sure that the only other Haskell packages you have installed from the official repositories are ghc, ghc-libs and ghc-static. Then setup Stack to use system GHC as explained in #Configuring Stack for dynamic linking:

$ stack setup --system-ghc --resolver resolver

To make these options permanent, paste the following snippet to ~/.stack/config.yaml:

~/.stack/config.yaml
# Stop downloading GHCs into isolated locations under ~/.stack.
install-ghc: false

# Allow Stack to pick the system GHC (false by default).
system-ghc: true

# Allow to use, say, Stackage snapshot for GHC 8.8.2 with system GHC 8.8.3.
compiler-check: newer-minor

This configuration will allow you to build statically linked packages as you would normally do, but using system GHC installation instead of GHC provided by Stack.

hpack-static-bin

hpack-static-binAUR provides a statically linked (meaning no haskell-* dependencies) alternative to haskell-hpack. It is precompiled, so no make dependencies are needed.

See also