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.
The Haskell Platform website (www.haskell.org/platform/) provides installers for Windows, Mac, and Linux; the Haskell Platform is also available through the standard package managers in most Linux systems. (Make sure that the GHC version is >= 7.) Note that the Mac installer requires the Apple XCode tools to be installed first.
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.)