Using the Glasgow Haskell Compiler (GHC)

Downloading and Installation

The easiest way to download and install GHC is as part of the Haskell Platform. The Haskell Platform contains the compiler together with appropriate versions of many Haskell libraries. We recommend the use of Haskell Platform for Mac and Windows users. You can find installers for Mac and Windows on that site; make sure to use the latest release (as of writing, 2014.2.0.0).

Mac: The platform installer for Mac requires the Apple XCode tools to be installed first. Should you have troubles with Platform, e.g. GHC not finding QuickCheck, please use GHC for Mac OS X instead.

Linux: We recommend installing GHC and Cabal using the standard package manager of your system. You can easily install additional required packages using Cabal. Make sure to install GHC >= 7.6.2. You can also choose to install the Haskell Platform with your package manager, but it'll likely be outdated.

Ubuntu Linux: In order to use GHC 7.6, you need to run at least Ubuntu 13.04 (Linux Mint: 13). We recommend running 14.04 LTS (Linux Mint: 17), though. You can get all required packages by installing ghc, libghc-quickcheck2-dev, libghc-network-dev and cabal-install. (Run the command apt-get install ghc ....)

Debian GNU/Linux: The GHC version in Wheezy is severely outdated. Use at least Jessie. Install the packages ghc, libghc-quickcheck2-dev, libghc-network-dev and cabal-install. (Run the command apt-get install ghc ....)

Virtual Machine: If you don't want or having troubles to install GHC on your system, you can use the VM provided by one of our tutors: VM with Haskell preinstalled. You need VirtualBox to run this VM.

GHCi

GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load.

Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

GHCi provides a read-eval-print loop: You type a Haskell expression at the prompt, and GHCi evaluates it and prints the result.

user@local:~$ ghci -W
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> 5 + 5
10
Prelude> let f x = 2 * x + 3 in f 10
23

GHCi also supports command line history: Press the up/down arrows to recall previously-entered lines.

Type :q or :quit to quit GHCi.

Prelude> :q
Leaving GHCi.

Source Files

GHCi is best used with a Haskell source file. For example, create a file Factorial.hs like this:

module Factorial where

factorial :: Integer -> Integer
factorial x = if x > 0 then x * factorial (x - 1) else 1

Note: The line "module Factorial where" allows this file to be imported by other Haskell files; it is optional if you only use one Haskell file at a time.

Load this file in GHCi with "ghci -W Factorial.hs". (If GHCi is already running, you can just type ":l Factorial.hs" in GHCi.)

user@local:~$ ghci -W Factorial.hs
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Factorial        ( Factorial.hs, interpreted )
Ok, modules loaded: Factorial.
*Factorial> 

You may now enter expressions referring to values from the loaded source file.

*Factorial> factorial 7
5040

The ":browse" command gives a useful summary of all functions in the source file.

*Factorial> :browse
factorial :: Integer -> Integer

If you modify your Haskell source file while GHCi is still running, use the command ":r" or ":reload" to make GHCi reload it.

Command Summary

Summary of useful GHCi commands:

:r or :reload Reload source files. Use after updating and saving your .hs files in your editor.
:l <file> or :load <file> Load Haskell source file and its dependents.
:t <expr> or :type <expr> Print the type of the expression <expr>.
:i <name> or :info <name> Display information about the given name(s). Works with function names, type names, etc.
:browse List all functions in scope, with type signatures.
let <pattern> = <expr> Introduce temporary value or function definition. (These disappear after reloading any files.)
:? or :help Print complete list of GHCi commands.
:q or :quit Quit.

See the GHCi Chapter of the GHC User's Guide for more information about GHCi.

More Documentation

The central homepage for GHC documentation is at www.haskell.org/haskellwiki/GHC.

Links include the GHC User's Guide and the Haskell Standard Libraries documentation, which gives type signatures and descriptions for all library functions. (Documentation for the Prelude is particularly relevant.)