Category Archives: Cabal

Getting Started with Cabal

Cabal is the Haskell package and build tool.

The following are a few of the “getting started” commands.

What version do you have of Cabal?

Run the following

cabal --version

Check for updates to Cabal

You can check

cabal new-install Cabal cabal-install

Check for package updates

This command will pull the latest package list from hackage.haskell.org

cabal update

Creating a new project

We can create a new Haskell project using

cabal init

This will create a Main.hs, Setup.hs and a .cabal file for your configurations. We can then edit the .cabal file or we can run the interactive version and supply details using

cabal init -i

Building our project

We can build our project by simply using

cabal build

Running our project

To run (or build and run)

cabal run

Cleaning our project

cabal clean

Running the ghci repl

You can run ghci by simply typing

ghci

Or you can run the ghci that’s available via cabal using

cabal repl

Adding a library

We can download and install package from The Haskell Package Repository using the command

cabal install --lib QuickCheck HUnit

This installs the packages QuickCheck and HUnit from the package repository. The –lib is used for packages which do not contain an executable (i.e. a library package). We can also install .gz packages by specifying a file location or URL.

Packages are then stored on Windows in

%USERPROFILE%\AppData\Roaming\cabal\store\ghc-8.10.1

References

Welcome to the Cabal User Guide

The Haskell repl – basic commands

Haskell’s repl can be run use ghci or cabal repl. The repl using : to prefix commands, so for example :? will list the help.

I’m not going to go through all commands, but just the one’s I’ve been using regularly, check out Cabal for the full list of commands and features.

Quitting the repl

Let’s start by looking at how to quitting the repl (gracefully)

Prelude> :q

Writing code

Obviously we want to be able to execute code in the repl, in such cases we write code such as

x = 1 + 2
-- press enter
x
-- press enter

The output from this will obviously be 3.

To handle multi line input we create a block using :{ and :}, for example

:{
data Expr = Lit Integer |
            Div Expr Expr
:}

Don’t forget indention is required!

Loading existing code

In some cases we might want to load existing code into the repl, such a data definitions etc. Let’s say we have a Main.hs then run

:load Main

Display information

Assuming we added the Expr data type, to the repl, we might want to show the definition at some point, just use

:i Expr

Showing the type of an expression

Let’s assume we have something like the following in a module

eval :: Expr -> Integer

We can use the following to return the type (obviously in this instance we should see the above, but this command can ofcourse be executed against types where we want to find the type annotations/decalaractions

:t Expr

Now try

:t print

and you’ll see the following

print :: Show a => a -> IO ()

References

Using GHCi
Cabal User Guide