© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_26

26. Cabal

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

Haskell includes a standard package system called Cabal, which is used to configure, build, install, and (re)distribute Haskell software. In this chapter, you will learn how to use Cabal to increase your productivity.

Cabal is that part of Haskell that helps you to manage packages. It draws the packages from Hackage, the archive of Haskell that contains a large number of libraries in the Cabal package format.

Packages help developers to distribute and (re)use software. A package system is an important component because it centralizes reusable software that can be shared by many developers.

The components of Cabal are called Cabal packages and can be independent, but they can also depend on one another. Cabal knows this, and when a package is installed, it also installs all the dependencies of that package. Note that packages are not part of the Haskell language (because they are not included by default in the Haskell installation), but you can think of them as features resulting from a combination of Cabal and GHC.

The command that is used to build and install a package is cabal . Let’s see a simple example. Open a terminal (if you work on Windows OS, right-click the terminal and choose Run as Administrator). Type cabal --help . This command will provide a list of options for cabal, as shown in Figure 26-1.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig1_HTML.jpg
Figure 26-1.

The result of the cabal --help command

A Cabal package contains the following items:
  • The software, i.e., libraries, executable, tests

  • The .cabal file, which is a metadata file with information about that package

  • The Setup.hs file, which is a standard interface, based on which the package is built

Many developers use cabal to install a package. To install a package, all you need to do is to check its page at http://hackage.haskell.org and then type at the terminal cabal install, followed by the package name. For example, let’s install the errors package (this package helps to handle errors), as shown in Figure 26-2.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig2_HTML.jpg
Figure 26-2.

Installing the errors package

If you already have installed a desired package, you will receive the message shown in Figure 26-3.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig3_HTML.jpg
Figure 26-3.

Message for an already installed package

If you feel everything is OK, you don’t need to reinstall the package.

Let’s see some warnings. If you received a warning message when you tried to install the pipes package in Chapter 19, you are in the right place. Let’s try to install the pipes package ; you will receive the message shown in Figure 26-4.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig4_HTML.jpg
Figure 26-4.

A warning message

This means your package archive is out-of-date, but don’t worry—all you need to do is to follow the steps in the message. So, update the archive, as shown in Figure 26-5.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig5_HTML.jpg
Figure 26-5.

Updating the archive of packages

This may take a few minutes. Next, type the installation command, as shown in Figure 26-6.
../images/475690_1_En_26_Chapter/475690_1_En_26_Fig6_HTML.jpg
Figure 26-6.

Successful installation of a package

That’s it, you just installed the pipes package .

Note that in Figure 26-6 that we built and installed all the packages that pipes needs to work properly.

In addition, these steps work for all packages for which you receive a similar warning message, not just for pipes. Now that you know what happens in the terminal, we will show you just the commands.

You can also install a package from a local source (the archive for a package usually ends with .tar.gz), as shown here:
$ cabal install pipes-4.3.9.tar.gz
Resolving dependencies...
In order, the following will be installed:
pipes-4.3.9 (reinstall)
Warning: Note that reinstalls are always dangerous. Continuing anyway...
Configuring pipes-4.3.9...
Building pipes-4.3.9...
Installed pipes-4.3.9

Note

When you install a package from a local source, the current directory from the terminal needs to be the same as the local path where the package is located.

Cabal is not used just to install packages. It can do a lot more, such as the following:
  • Build a software system

  • Configure a system

  • Package a system for distribution

  • Run tests

  • Generate documentation

  • Automatically manage the packages

  • Archive online and local packages in Cabal form

For example, here are the commands used to build and install a system package:
$ runhaskell Setup.hs configure --ghc
$ runhaskell Setup.hs build
$ runhaskell Setup.hs install

On the first line, the system is prepared to build the software using GHC, on the second line it is actually built, and on the third line it installs the package by copying the build results to a permanent place and registering the package with GHC.

There are lot things that can be done with Cabal that require more advanced programming skills than shown here. You can find a complete guide to using Cabal on the official page.1

Summary

In this chapter, you learned the following:
  • What Cabal is and what it can do

  • What the structure of a package is

  • How to install packages and resolve some error or warning messages

References

  1. 1.

    I. Jones, “The Haskell Cabal: A Common Architecture for Building Applications and Libraries” (2005)

     
  2. 2.

    P. Hudak, “A History of Haskell: Being Lazy with Class” in proceedings of the Third ACM SIGPLAN Conference on History of Programming Languages (ACM, 2007)

     
  3. 3.
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.13.219