Using GHCi

The Glasgow Haskell Compiler User's Guide, Version 5.04
Prev |  | Next

# Chapter 3. Using GHCi

GHCi[1] is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted. If you're famililar with Hugs, then you'll be right at home with GHCi. However, GHCi also has support for interactively loading compiled code, as well as supporting all[2] the language extensions that GHC provides.

# 3.1. Introduction to GHCi

Let's start with an example GHCi session. You can fire up GHCi with the command `ghci`:

    $ ghci
       ___         ___ _
      / _ \ /\  /\/ __(_)
     / /_\// /_/ / /  | |      GHC Interactive, version 5.04, for Haskell 98.
    / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
    \____/\/ /_/\____/|_|      Type :? for help.

    Loading package base ... linking ... done.
    Loading package haskell98 ... linking ... done.
    Prelude>

There may be a short pause while GHCi loads the prelude and standard libraries, after which the prompt is shown. If we follow the instructions and type `:?` for help, we get:

     Commands available from the prompt:

       <stmt>                     evaluate/run <stmt>
       :add <filename> ...        add module(s) to the current target set
       :browse [*]<module>        display the names defined by <module>
       :cd <dir>                  change directory to <dir>
       :def <cmd> <expr>          define a command :<cmd>
       :help, :?                  display this list of commands
       :info [<name> ...]         display information about the given names
       :load <filename> ...       load module(s) and their dependents
       :module [+/-] [*]<mod> ... set the context for expression evaluation
       :reload                    reload the current module set

       :set <option> ...          set options
       :set args <arg> ...        set the arguments returned by System.getArgs
       :set prog <progname>       set the value returned by System.getProgName

       :show modules              show the currently loaded modules
       :show bindings             show the current bindings made at the prompt

       :type <expr>               show the type of <expr>
       :undef <cmd>               undefine user-defined command :<cmd>
       :unset <option> ...        unset options
       :quit                      exit GHCi
       :!<command>                run the shell command <command>

     Options for `:set' and `:unset':

        +r                 revert top-level expressions after each evaluation
        +s                 print timing/memory stats after each evaluation
        +t                 print type after evaluation
        -<flags>           most GHC command line flags can also be set here
                             (eg. -v2, -fglasgow-exts, etc.)

We'll explain most of these commands as we go along. For Hugs users: many things work the same as in Hugs, so you should be able to get going straight away.

Haskell expressions can be typed at the prompt:

    Prelude> 1+2
    3
    Prelude> let x = 42 in x / 9
    4.666666666666667
    Prelude>

GHCi interprets the whole line as an expression to evaluate. The expression may not span several lines - as soon as you press enter, GHCi will attempt to evaluate it.

### Notes

[1] | The ‘i’ stands for “Interactive”
[2] | except `foreign export`, at the moment

Prev | Home | Next
The layout of installed files |  | Loading source files