$ ghci
___ ___ _
/ _ \ /\ /\/ __(_)
- / /_\// /_/ / / | | GHC Interactive, version 5.04, 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.
Prelude>
</screen>
<screen>
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
- :set prompt <prompt> set the prompt used in GHCi
-
- :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 (default: "TAGS")
- :type <expr> show the type of <expr>
- :kind <type> show the kind of <type>
- :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
+ <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
+ :main [<arguments> ...] run the main function with the given arguments
+ :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
+ :set prompt <prompt> set the prompt used in GHCi
+
+ :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")
+ :type <expr> show the type of <expr>
+ :kind <type> show the kind of <type>
+ :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.)
</screen>
</screen>
<para>HINT: since GHCi will only use a compiled object file if it
- can sure that the compiled version is up-to-date, a good technique
+ can be sure that the compiled version is up-to-date, a good technique
when working on a large program is to occasionally run
<literal>ghc ––make</literal> to compile the whole project (say
before you go for lunch :-), then continue working in the
<screen>
Prelude> :module +IO
-Prelude,IO> hPutStrLn stdout "hello\n"
+Prelude IO> hPutStrLn stdout "hello\n"
hello
-Prelude,IO>
+Prelude IO>
</screen>
<para>(Note: <literal>:module</literal> can be shortened to
<literal>Bar</literal>, then the scope will be set to
<literal>*Bar</literal> if <literal>Bar</literal> is
interpreted, or if <literal>Bar</literal> is compiled it will be
- set to <literal>Prelude,Bar</literal> (GHCi automatically adds
+ set to <literal>Prelude Bar</literal> (GHCi automatically adds
<literal>Prelude</literal> if it isn't present and there aren't
any <literal>*</literal>-form modules).</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>
qualified</literal> declaration for every module in every
package, and every module currently loaded into GHCi.</para>
</sect3>
+
+ <sect3>
+ <title>The <literal>:main</literal> command</title>
+
+ <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>
+
+ </sect3>
</sect2>
expression, and if it doesn't have an <literal>IO</literal> type,
then it transforms it as follows: an expression
<replaceable>e</replaceable> turns into
-<screen>
- let it = <replaceable>e</replaceable>;
- print it
+<screen>
+let it = <replaceable>e</replaceable>;
+print it
</screen>
which is then run as an IO-action.</para>
complain:</para>
<screen>
-Prelude> id
-No instance for `Show (a -> a)'
-arising from use of `print'
-in a `do' expression pattern binding: print it
+Prelude> id
+
+<interactive>:1:0:
+ No instance for (Show (a -> a))
+ arising from use of `print' at <interactive>:1:0-1
+ Possible fix: add an instance declaration for (Show (a -> a))
+ In the expression: print it
+ In a 'do' expression: print it
</screen>
<para>The error message contains some clues as to the
which is of type <literal>a</literal>. eg.:</para>
<screen>
Prelude> Time.getClockTime
+Wed Mar 14 12:23:13 GMT 2001
Prelude> print it
Wed Mar 14 12:23:13 GMT 2001
</screen>
<para>The corresponding translation for an IO-typed
<replaceable>e</replaceable> is
-<screen>
- it <- <replaceable>e</replaceable>
+<screen>
+it <- <replaceable>e</replaceable>
</screen>
</para>
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>: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>