Funktionale Programmierung und Verifikation (IN0003), WS 2019/20

Installing the Glasgow Haskell Compiler (GHC)

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 you to install the Haskell Platform for an easy setup. You can find installers for Linux, Mac, and Windows on linked site. Make sure you install GHC 8.4.4 or newer.
You also need to install QuickCheck. If you installed the Haskell Platform, you can run

    cabal install QuickCheck
    

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 8.4.4: http://www.haskell.org/ghc/  :? for help
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 8.4.4: http://www.haskell.org/ghc/  :? for help
[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.

Commands 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.

Running Tests

We use QuickCheck to run tests. For example, create a file Reverse.hs like this:

module Reverse where

prop_rev :: [Int] -> Bool
prop_rev xs = (reverse $ reverse xs) == xs

Load this file in GHCi and enter the following commands to import QuickCheck and test the defined proposition.

user@local:~$ ghci -W Reverse.hs
GHCi, version 8.4.4: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Reverse        ( Reverse.hs, interpreted )
Ok, modules loaded: Reverse.
*Reverse> import Test.QuickCheck
*Reverse Test.QuickCheck> quickCheck prop_rev
+++ OK, passed 100 tests.

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 Prelude is particularly relevant.