:browse [*]<module> display the names defined by <module>
:cd <dir> change directory to <dir>
:def <cmd> <expr> define a command :<cmd>
+ :edit <file> edit file
+ :edit edit last module
:help, :? display this list of commands
:info [<name> ...] display information about the given names
:load <filename> ... load module(s) and their dependents
:set args <arg> ... set the arguments returned by System.getArgs
:set prog <progname> set the value returned by System.getProgName
:set prompt <prompt> set the prompt used in GHCi
+ :set editor <cmd> set the command used for :edit
:show modules show the currently loaded modules
:show bindings show the current bindings made at the prompt
:ctags [<file>] create tags file for Vi (default: "tags")
- :etags [<file>] create tags file for Emacs (defauilt: "TAGS")
+ :etags [<file>] create tags file for Emacs (default: "TAGS")
:type <expr> show the type of <expr>
:kind <type> show the kind of <type>
:undef <cmd> undefine user-defined command :<cmd>
</screen>
</para></sect2>
- <sect2>
+ <sect2 id="ghci-stmts">
<title>Using <literal>do-</literal>notation at the prompt</title>
<indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
<indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
<literal>Show</literal></para>
</listitem>
</itemizedlist>
- </para>
+ The automatic printing of binding results can be supressed with
+ <option>:set -fno-print-bind-result</option> (this does not
+ supress printing the result of non-binding statements).
+ <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
+ You might want to do this to prevent the result of binding
+ statements from being fully evaluated by the act of printing
+ them, for example.</para>
<para>Of course, you can also bind normal non-IO expressions
using the <literal>let</literal>-statement:</para>
behaves in the same way for expressions typed at the
prompt.</para>
+ <para>
+ Hint: GHCi will tab-complete names that are in scope; for
+ example, if you run GHCi and type <literal>J<tab></literal>
+ then GHCi will expand it to <literal>Just </literal>.
+ </para>
+
<sect3>
<title>Qualified names</title>
package be loaded by using the <literal>-package</literal> flag:</para>
<screen>
-$ ghci -package data
+$ ghci -package readline
___ ___ _
/ _ \ /\ /\/ __(_)
- / /_\// /_/ / / | | GHC Interactive, version 5.05, for Haskell 98.
+ / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98.
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/
\____/\/ /_/\____/|_| Type :? for help.
Loading package base ... linking ... done.
-Loading package haskell98 ... linking ... done.
-Loading package lang ... linking ... done.
-Loading package concurrent ... linking ... done.
-Loading package readline ... linking ... done.
-Loading package unix ... linking ... done.
-Loading package posix ... linking ... done.
-Loading package util ... linking ... done.
-Loading package data ... linking ... done.
+Loading package readline-1.0 ... linking ... done.
Prelude>
</screen>
<para>GHCi commands all begin with
‘<literal>:</literal>’ 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
- <literal>:unset</literal> and <literal>:undef</literal>, may be
- abbreviated to a single letter.</para>
+ abbreviated, with ambiguities being resolved in favour of the more
+ commonly used commands.</para>
<variablelist>
<varlistentry>
<varlistentry>
<term>
+ <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
+ <indexterm><primary><literal>:edit</literal></primary></indexterm>
+ </term>
+ <listitem>
+ <para>Opens an editor to edit the file
+ <replaceable>file</replaceable>, or the most recently loaded
+ module if <replaceable>file</replaceable> is omitted. The
+ editor to invoke is taken from the <literal>EDITOR</literal>
+ environment variable, or a default editor on your system if
+ <literal>EDITOR</literal> is not set. You can change the
+ editor using <literal>:set editor</literal>.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
<literal>:help</literal>
<indexterm><primary><literal>:help</literal></primary></indexterm>
</term>
<varlistentry>
<term>
+ <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
+ <indexterm><primary><literal>:main</literal></primary></indexterm>
+ </term>
+ <listitem>
+ <para>
+ When a program is compiled and executed, it can use the
+ <literal>getArgs</literal> function to access the
+ command-line arguments.
+ However, we cannot simply pass the arguments to the
+ <literal>main</literal> function while we are testing in ghci,
+ as the <literal>main</literal> function doesn't take its
+ directly.
+ </para>
+
+ <para>
+ Instead, we can use the <literal>:main</literal> command.
+ This runs whatever <literal>main</literal> is in scope, with
+ any arguments being treated the same as command-line arguments,
+ e.g.:
+ </para>
+
+<screen>
+Prelude> let main = System.Environment.getArgs >>= print
+Prelude> :main foo bar
+["foo","bar"]
+</screen>
+
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
<literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
<indexterm><primary><literal>:module</literal></primary></indexterm>
</term>
<varlistentry>
<term>
+ <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
+ </term>
+ <listitem>
+ <para>Sets the command used by <literal>:edit</literal> to
+ <replaceable>cmd</replaceable>.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
<literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
<indexterm><primary><literal>:set prog</literal></primary></indexterm>
</term>
</sect1>
- <sect1>
+ <sect1 id="ghci-faq">
<title>FAQ and Things To Watch Out For</title>
<variablelist>
<term>Concurrent threads don't carry on running when GHCi is
waiting for input.</term>
<listitem>
- <para>No, they don't. This is because the Haskell binding
- to the GNU readline library doesn't support reading from the
- terminal in a non-blocking way, which is required to work
- properly with GHC's concurrency model.</para>
+ <para>This should work, as long as your GHCi was built with
+ the <option>-threaded</option> switch, which is the default.
+ Consult whoever supplied your GHCi installation.</para>
</listitem>
</varlistentry>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term>I can't use Control-C to interrupt computations in
+ GHCi on Windows.</term>
+ <listitem>
+ <para>See <xref linkend="ghci-windows">.</xref></para>
+ </listitem>
+ </varlistentry>
</variablelist>
</sect1>