3.6. GHCi commands

GHCi commands all begin with ‘:’ and consist of a single command name followed by zero or more parameters. The command name may be abbreviated, as long as the abbreviation is not ambiguous. All of the builtin commands, with the exception of :unset and :undef, may be abbreviated to a single letter.

:cd dir

Changes the current working directory to dir. A ‘˜’ symbol at the beginning of dir will be replaced by the contents of the environment variable HOME.

:def name expr

The command :def name expr defines a new GHCi command :name, implemented by the Haskell expression expr, which must have type String -> IO String. When :name args is typed at the prompt, GHCi will run the expression (name args), take the resulting String, and feed it back into GHCi as a new sequence of commands. Separate commands in the result must be separated by ‘\n’.

That's all a little confusing, so here's a few examples. To start with, here's a new GHCi command which doesn't take any arguments or produce any results, it just outputs the current date & time:

Prelude> let date _ = Time.getClockTime >>= print >> return ""
Prelude> :def date date
Prelude> :date
Fri Mar 23 15:16:40 GMT 2001

Here's an example of a command that takes an argument. It's a re-implementation of :cd:

Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
Prelude> :def mycd mycd
Prelude> :mycd ..

Or I could define a simple way to invoke “ghc --make Main” in the current directory:

Prelude> :def make (\_ -> return ":! ghc --make Main")
:help, :?

Displays a list of the available commands.

:load module

Recursively loads module (which may be a module name or filename), and all the modules it depends on. All previously loaded modules are forgotten. The module module is known as the target.

:module module

Sets the current context for statements typed at the prompt to module, which must be a module name which is already loaded or in a package. See Section 3.4.1 for more information on what effect the context has on what entities are in scope at the prompt.

:quit

Quits GHCi. You can also quit by typing a control-D at the prompt.

:reload

Attempts to reload the current target (see :load) if it, or any module it depends on, has changed. Note that this may entail loading new modules, or even dropping modules which are no longer indirectly required by the target.

:set [option...]

Sets various options. See Section 3.7 for a list of available options. The :set command by itself shows which options are currently set.

:type expression

Infers and prints the type of expression, including explicit forall quantifiers for polymorphic types. The monomorphism restriction is not applied to the expression during type inference.

:undef name

Undefines the user-defined command name (see :def above).

:unset option...

Unsets certain options. See Section 3.7 for a list of available options.

:! command...

Executes the shell command command.