2 <title>Using GHCi</title>
3 <indexterm><primary>GHCi</primary></indexterm>
4 <indexterm><primary>interpreter</primary><see>GHCi</see></indexterm>
5 <indexterm><primary>interactive</primary><see>GHCi</see></indexterm>
8 <para>The ‘i’ stands for “Interactive”</para>
10 is GHC's interactive environment, in which Haskell expressions can
11 be interactively evaluated and programs can be interpreted. If
12 you're famililar with <ulink url="http://www.haskell.org/hugs/">Hugs</ulink><indexterm><primary>Hugs</primary>
13 </indexterm>, then you'll be right at home with GHCi. However, GHCi
14 also has support for interactively loading compiled code, as well as
15 supporting all<footnote><para>except <literal>foreign export</literal>, at the moment</para>
16 </footnote> the language extensions that GHC provides.</para>
17 <indexterm><primary>FFI</primary><secondary>GHCi support</secondary></indexterm>
18 <indexterm><primary>Foreign Function Interface</primary><secondary>GHCi support</secondary></indexterm>
21 <title>Introduction to GHCi</title>
23 <para>Let's start with an example GHCi session. You can fire up
24 GHCi with the command <literal>ghci</literal>:</para>
30 / /_\// /_/ / / | | GHC Interactive, version 5.04, for Haskell 98.
31 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
32 \____/\/ /_/\____/|_| Type :? for help.
34 Loading package base ... linking ... done.
35 Loading package haskell98 ... linking ... done.
39 <para>There may be a short pause while GHCi loads the prelude and
40 standard libraries, after which the prompt is shown. If we follow
41 the instructions and type <literal>:?</literal> for help, we
45 Commands available from the prompt:
47 %lt;stmt> evaluate/run %lt;stmt>
48 :add %lt;filename> ... add module(s) to the current target set
49 :browse [*]%lt;module> display the names defined by %lt;module>
50 :cd %lt;dir> change directory to %lt;dir>
51 :def %lt;cmd> %lt;expr> define a command :%lt;cmd>
52 :help, :? display this list of commands
53 :info [%lt;name> ...] display information about the given names
54 :load %lt;filename> ... load module(s) and their dependents
55 :module [+/-] [*]%lt;mod> ... set the context for expression evaluation
56 :reload reload the current module set
58 :set %lt;option> ... set options
59 :set args %lt;arg> ... set the arguments returned by System.getArgs
60 :set prog %lt;progname> set the value returned by System.getProgName
62 :show modules show the currently loaded modules
63 :show bindings show the current bindings made at the prompt
65 :type %lt;expr> show the type of %lt;expr>
66 :undef %lt;cmd> undefine user-defined command :%lt;cmd>
67 :unset %lt;option> ... unset options
69 :!%lt;command> run the shell command %lt;command>
71 Options for `:set' and `:unset':
73 +r revert top-level expressions after each evaluation
74 +s print timing/memory stats after each evaluation
75 +t print type after evaluation
76 -%lt;flags> most GHC command line flags can also be set here
77 (eg. -v2, -fglasgow-exts, etc.)
80 <para>We'll explain most of these commands as we go along. For
81 Hugs users: many things work the same as in Hugs, so you should be
82 able to get going straight away.</para>
84 <para>Haskell expressions can be typed at the prompt:</para>
85 <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
91 Prelude> let x = 42 in x / 9
96 <para>GHCi interprets the whole line as an expression to evaluate.
97 The expression may not span several lines - as soon as you press
98 enter, GHCi will attempt to evaluate it.</para>
102 <title>Loading source files</title>
104 <para>Suppose we have the following Haskell source code, which we
105 place in a file <filename>Main.hs</filename> in the current
109 main = print (fac 20)
112 fac n = n * fac (n-1)
115 <para>To load a Haskell source file into GHCi, use the
116 <literal>:load</literal> command:</para>
117 <indexterm><primary><literal>:load</literal></primary></indexterm>
121 Compiling Main ( Main.hs, interpreted )
122 Ok, modules loaded: Main.
126 <para>GHCi has loaded the <literal>Main</literal> module, and the
127 prompt has changed to “<literal>*Main></literal>” to
128 indicate that the current context for expressions typed at the
129 prompt is the <literal>Main</literal> module we just loaded (we'll
130 explain what the <literal>*</literal> means later in <xref
131 linkend="ghci-scope">). So we can now type expressions involving
132 the functions from <filename>Main.hs</filename>:</para>
139 <para>Loading a multi-module program is just as straightforward;
140 just give the name of the “topmost” module to the
141 <literal>:load</literal> command (hint: <literal>:load</literal>
142 can be abbreviated to <literal>:l</literal>). The topmost module
143 will normally be <literal>Main</literal>, but it doesn't have to
144 be. GHCi will discover which modules are required, directly or
145 indirectly, by the topmost module, and load them all in dependency
149 <title>Modules vs. filenames</title>
150 <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
151 <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
153 <para>Question: How does GHC find the filename which contains
154 module <replaceable>M</replaceable>? Answer: it looks for the
155 file <literal><replaceable>M</replaceable>.hs</literal>, or
156 <literal><replaceable>M</replaceable>.lhs</literal>. This means
157 that for most modules, the module name must match the filename.
158 If it doesn't, GHCi won't be able to find it.</para>
160 <para>There is one exception to this general rule: when you load
161 a program with <literal>:load</literal>, or specify it when you
162 invoke <literal>ghci</literal>, you can give a filename rather
163 than a module name. This filename is loaded if it exists, and
164 it may contain any module you like. This is particularly
165 convenient if you have several <literal>Main</literal> modules
166 in the same directory and you can't call them all
167 <filename>Main.hs</filename>.</para>
169 <para>The search path for finding source files is specified with
170 the <option>-i</option> option on the GHCi command line, like
172 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
174 <para>or it can be set using the <literal>:set</literal> command
175 from within GHCi (see <xref
176 linkend="ghci-cmd-line-options">)<footnote><para>Note that in
177 GHCi, and <option>––make</option> mode, the <option>-i</option>
178 option is used to specify the search path for
179 <emphasis>source</emphasis> files, whereas in standard
180 batch-compilation mode the <option>-i</option> option is used to
181 specify the search path for interface files, see <xref
182 linkend="options-finding-imports">.</para> </footnote></para>
184 <para>One consequence of the way that GHCi follows dependencies
185 to find modules to load is that every module must have a source
186 file. The only exception to the rule is modules that come from
187 a package, including the <literal>Prelude</literal> and standard
188 libraries such as <literal>IO</literal> and
189 <literal>Complex</literal>. If you attempt to load a module for
190 which GHCi can't find a source file, even if there are object
191 and interface files for the module, you'll get an error
194 <para>One final note: if you load a module called Main, it must
195 contain a <literal>main</literal> function, just like in
200 <title>Making changes and recompilation</title>
201 <indexterm><primary><literal>:reload</literal></primary></indexterm>
203 <para>If you make some changes to the source code and want GHCi
204 to recompile the program, give the <literal>:reload</literal>
205 command. The program will be recompiled as necessary, with GHCi
206 doing its best to avoid actually recompiling modules if their
207 external dependencies haven't changed. This is the same
208 mechanism we use to avoid re-compiling modules in the batch
209 compilation setting (see <xref linkend="recomp">).</para>
213 <sect1 id="ghci-compiled">
214 <title>Loading compiled code</title>
215 <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
217 <para>When you load a Haskell source module into GHCi, it is
218 normally converted to byte-code and run using the interpreter.
219 However, interpreted code can also run alongside compiled code in
220 GHCi; indeed, normally when GHCi starts, it loads up a compiled
221 copy of the <literal>base</literal> package, which contains the
222 <literal>Prelude</literal>.</para>
224 <para>Why should we want to run compiled code? Well, compiled
225 code is roughly 10x faster than interpreted code, but takes about
226 2x longer to produce (perhaps longer if optimisation is on). So
227 it pays to compile the parts of a program that aren't changing
228 very often, and use the interpreter for the code being actively
231 <para>When loading up source files with <literal>:load</literal>,
232 GHCi looks for any corresponding compiled object files, and will
233 use one in preference to interpreting the source if possible. For
234 example, suppose we have a 4-module program consisting of modules
235 A, B, C, and D. Modules B and C both import D only,
236 and A imports both B & C:</para>
244 <para>We can compile D, then load the whole program, like this:</para>
246 Prelude> :! ghc -c D.hs
248 Skipping D ( D.hs, D.o )
249 Compiling C ( C.hs, interpreted )
250 Compiling B ( B.hs, interpreted )
251 Compiling A ( A.hs, interpreted )
252 Ok, modules loaded: A, B, C, D.
256 <para>In the messages from the compiler, we see that it skipped D,
257 and used the object file <filename>D.o</filename>. The message
258 <literal>Skipping</literal> <replaceable>module</replaceable>
259 indicates that compilation for <replaceable>module</replaceable>
260 isn't necessary, because the source and everything it depends on
261 is unchanged since the last compilation.</para>
263 <para>At any time you can use the command
264 <literal>:show modules</literal>
265 to get a list of the modules currently loaded
271 C ( C.hs, interpreted )
272 B ( B.hs, interpreted )
273 A ( A.hs, interpreted )
276 <para>If we now modify the source of D (or pretend to: using Unix
277 command <literal>touch</literal> on the source file is handy for
278 this), the compiler will no longer be able to use the object file,
279 because it might be out of date:</para>
284 Compiling D ( D.hs, interpreted )
285 Skipping C ( C.hs, interpreted )
286 Skipping B ( B.hs, interpreted )
287 Skipping A ( A.hs, interpreted )
288 Ok, modules loaded: A, B, C, D.
292 <para>Note that module D was compiled, but in this instance
293 because its source hadn't really changed, its interface remained
294 the same, and the recompilation checker determined that A, B and C
295 didn't need to be recompiled.</para>
297 <para>So let's try compiling one of the other modules:</para>
300 *Main> :! ghc -c C.hs
302 Compiling D ( D.hs, interpreted )
303 Compiling C ( C.hs, interpreted )
304 Compiling B ( B.hs, interpreted )
305 Compiling A ( A.hs, interpreted )
306 Ok, modules loaded: A, B, C, D.
309 <para>We didn't get the compiled version of C! What happened?
310 Well, in GHCi a compiled module may only depend on other compiled
311 modules, and in this case C depends on D, which doesn't have an
312 object file, so GHCi also rejected C's object file. Ok, so let's
313 also compile D:</para>
316 *Main> :! ghc -c D.hs
318 Ok, modules loaded: A, B, C, D.
321 <para>Nothing happened! Here's another lesson: newly compiled
322 modules aren't picked up by <literal>:reload</literal>, only
323 <literal>:load</literal>:</para>
327 Skipping D ( D.hs, D.o )
328 Skipping C ( C.hs, C.o )
329 Compiling B ( B.hs, interpreted )
330 Compiling A ( A.hs, interpreted )
331 Ok, modules loaded: A, B, C, D.
334 <para>HINT: since GHCi will only use a compiled object file if it
335 can sure that the compiled version is up-to-date, a good technique
336 when working on a large program is to occasionally run
337 <literal>ghc ––make</literal> to compile the whole project (say
338 before you go for lunch :-), then continue working in the
339 interpreter. As you modify code, the new modules will be
340 interpreted, but the rest of the project will remain
346 <title>Interactive evaluation at the prompt</title>
348 <para>When you type an expression at the prompt, GHCi immediately
349 evaluates and prints the result. But that's not the whole story:
350 if you type something of type <literal>IO a</literal> for some
351 <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
352 as an IO-computation, and doesn't attempt to print the
358 Prelude> putStrLn "hello"
362 <para>What actually happens is that GHCi typechecks the
363 expression, and if it doesn't have an <literal>IO</literal> type,
364 then it transforms it as follows: an expression
365 <replaceable>e</replaceable> turns into
367 let it = <replaceable>e</replaceable>;
370 which is then run as an IO-action.</para>
372 <para>Hence, the original expression must have a type which is an
373 instance of the <literal>Show</literal> class, or GHCi will
378 No instance for `Show (a -> a)'
379 arising from use of `print'
380 in a `do' expression pattern binding: print it
383 <para>The error message contains some clues as to the
384 transformation happening internally.</para>
386 <sect2 id="ghci-scope">
387 <title>What's really in scope at the prompt?</title>
389 <para>When you type an expression at the prompt, what
390 identifiers and types are in scope? GHCi provides a flexible
391 way to control exactly how the context for an expression is
392 constructed. Let's start with the simple cases; when you start
393 GHCi the prompt looks like this:</para>
395 <screen>Prelude></screen>
397 <para>Which indicates that everything from the module
398 <literal>Prelude</literal> is currently in scope. If we now
399 load a file into GHCi, the prompt will change:</para>
402 Prelude> :load Main.hs
403 Compiling Main ( Main.hs, interpreted )
407 <para>The new prompt is <literal>*Main</literal>, which
408 indicates that we are typing expressions in the context of the
409 top-level of the <literal>Main</literal> module. Everything
410 that is in scope at the top-level in the module
411 <literal>Main</literal> we just loaded is also in scope at the
412 prompt (probably including <literal>Prelude</literal>, as long
413 as <literal>Main</literal> doesn't explicitly hide it).</para>
416 <literal>*<replaceable>module</replaceable></literal> indicates
417 that it is the full top-level scope of
418 <replaceable>module</replaceable> that is contributing to the
419 scope for expressions typed at the prompt. Without the
420 <literal>*</literal>, just the exports of the module are
423 <para>We're not limited to a single module: GHCi can combine
424 scopes from multiple modules, in any mixture of
425 <literal>*</literal> and non-<literal>*</literal> forms. GHCi
426 combines the scopes from all of these modules to form the scope
427 that is in effect at the prompt. For technical reasons, GHCi
428 can only support the <literal>*</literal>-form for modules which
429 are interpreted, so compiled modules and package modules can
430 only contribute their exports to the current scope.</para>
432 <para>The scope is manipulated using the
433 <literal>:module</literal> command. For example, if the current
434 scope is <literal>Prelude</literal>, then we can bring into
435 scope the exports from the module <literal>IO</literal> like
440 Prelude,IO> hPutStrLn stdout "hello\n"
445 <para>(Note: <literal>:module</literal> can be shortened to
446 <literal>:m</literal>). The full syntax of the
447 <literal>:module</literal> command is:</para>
450 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
453 <para>Using the <literal>+</literal> form of the
454 <literal>module</literal> commands adds modules to the current
455 scope, and <literal>-</literal> removes them. Without either
456 <literal>+</literal> or <literal>-</literal>, the current scope
457 is replaced by the set of modules specified. Note that if you
458 use this form and leave out <literal>Prelude</literal>, GHCi
459 will assume that you really wanted the
460 <literal>Prelude</literal> and add it in for you (if you don't
461 want the <literal>Prelude</literal>, then ask to remove it with
462 <literal>:m -Prelude</literal>).</para>
464 <para>The scope is automatically set after a
465 <literal>:load</literal> command, to the most recently loaded
466 "target" module, in a <literal>*</literal>-form if possible.
467 For example, if you say <literal>:load foo.hs bar.hs</literal>
468 and <filename>bar.hs</filename> contains module
469 <literal>Bar</literal>, then the scope will be set to
470 <literal>*Bar</literal> if <literal>Bar</literal> is
471 interpreted, or if <literal>Bar</literal> is compiled it will be
472 set to <literal>Prelude,Bar</literal> (GHCi automatically adds
473 <literal>Prelude</literal> if it isn't present and there aren't
474 any <literal>*</literal>-form modules).</para>
476 <para>With multiple modules in scope, especially multiple
477 <literal>*</literal>-form modules, it is likely that name
478 clashes will occur. Haskell specifies that name clashes are
479 only reported when an ambiguous identifier is used, and GHCi
480 behaves in the same way for expressions typed at the
484 <title>Qualified names</title>
486 <para>To make life slightly easier, the GHCi prompt also
487 behaves as if there is an implicit <literal>import
488 qualified</literal> declaration for every module in every
489 package, and every module currently loaded into GHCi.</para>
494 <title>Using <literal>do-</literal>notation at the prompt</title>
495 <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
496 <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
498 <para>GHCi actually accepts <firstterm>statements</firstterm>
499 rather than just expressions at the prompt. This means you can
500 bind values and functions to names, and use them in future
501 expressions or statements.</para>
503 <para>The syntax of a statement accepted at the GHCi prompt is
504 exactly the same as the syntax of a statement in a Haskell
505 <literal>do</literal> expression. However, there's no monad
506 overloading here: statements typed at the prompt must be in the
507 <literal>IO</literal> monad.</para>
509 <para>Here's an example:</para>
511 Prelude> x <- return 42
516 <para>The statement <literal>x <- return 42</literal> means
517 “execute <literal>return 42</literal> in the
518 <literal>IO</literal> monad, and bind the result to
519 <literal>x</literal>”. We can then use
520 <literal>x</literal> in future statements, for example to print
521 it as we did above.</para>
523 <para>Of course, you can also bind normal non-IO expressions
524 using the <literal>let</literal>-statement:</para>
531 <para>An important difference between the two types of binding
532 is that the monadic bind (<literal>p <- e</literal>) is
533 <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
534 whereas with the <literal>let</literal> form, the expression
535 isn't evaluated immediately:</para>
537 Prelude> let x = error "help!"
542 <para>Any exceptions raised during the evaluation or execution
543 of the statement are caught and printed by the GHCi command line
544 interface (for more information on exceptions, see the module
545 <literal>Control.Exception</literal> in the libraries
546 documentation).</para>
548 <para>Every new binding shadows any existing bindings of the
549 same name, including entities that are in scope in the current
550 module context.</para>
552 <para>WARNING: temporary bindings introduced at the prompt only
553 last until the next <literal>:load</literal> or
554 <literal>:reload</literal> command, at which time they will be
555 simply lost. However, they do survive a change of context with
556 <literal>:module</literal>: the temporary bindings just move to
557 the new location.</para>
559 <para>HINT: To get a list of the bindings currently in scope, use the
560 <literal>:show bindings</literal> command:</para>
563 Prelude> :show bindings
567 <para>HINT: if you turn on the <literal>+t</literal> option,
568 GHCi will show the type of each variable bound by a statement.
570 <indexterm><primary><literal>+t</literal></primary></indexterm>
573 Prelude> let (x:xs) = [1..]
581 <title>The <literal>it</literal> variable</title>
582 <indexterm><primary><literal>it</literal></primary>
585 <para>Whenever an expression (or a non-binding statement, to be
586 precise) is typed at the prompt, GHCi implicitly binds its value
587 to the variable <literal>it</literal>. For example:</para>
595 <para>This is a result of the translation mentioned earlier,
596 namely that an expression <replaceable>e</replaceable> is
599 let it = <replaceable>e</replaceable>;
602 before execution, resulting in a binding for
603 <literal>it</literal>.</para>
605 <para>If the expression was of type <literal>IO a</literal> for
606 some <literal>a</literal>, then <literal>it</literal> will be
607 bound to the result of the <literal>IO</literal> computation,
608 which is of type <literal>a</literal>. eg.:</para>
610 Prelude> Time.getClockTime
612 Wed Mar 14 12:23:13 GMT 2001
615 <para>The corresponding translation for an IO-typed
616 <replaceable>e</replaceable> is
618 it <- <replaceable>e</replaceable>
622 <para>Note that <literal>it</literal> is shadowed by the new
623 value each time you evaluate a new expression, and the old value
624 of <literal>it</literal> is lost.</para>
629 <sect1 id="ghci-invokation">
630 <title>Invoking GHCi</title>
631 <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
632 <indexterm><primary><option>––interactive</option></primary></indexterm>
634 <para>GHCi is invoked with the command <literal>ghci</literal> or
635 <literal>ghc ––interactive</literal>. One or more modules or
636 filenames can also be specified on the command line; this
637 instructs GHCi to load the specified modules or filenames (and all
638 the modules they depend on), just as if you had said
639 <literal>:load <replaceable>modules</replaceable></literal> at the
640 GHCi prompt (see <xref linkend="ghci-commands">). For example, to
641 start GHCi and load the program whose topmost module is in the
642 file <literal>Main.hs</literal>, we could say:</para>
648 <para>Most of the command-line options accepted by GHC (see <xref
649 linkend="using-ghc">) also make sense in interactive mode. The ones
650 that don't make sense are mostly obvious; for example, GHCi
651 doesn't generate interface files, so options related to interface
652 file generation won't have any effect.</para>
655 <title>Packages</title>
656 <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
658 <para>GHCi can make use of all the packages that come with GHC,
659 For example, to start up GHCi with the <literal>network</literal>
660 package loaded:</para>
663 $ ghci -package network
666 / /_\// /_/ / / | | GHC Interactive, version 5.04, for Haskell 98.
667 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
668 \____/\/ /_/\____/|_| Type :? for help.
670 Loading package base ... linking ... done.
671 Loading package haskell98 ... linking ... done.
672 Loading package network ... linking ... done.
676 <para>Note that GHCi will also automatically load any packages
677 on which the requested package depends.</para>
679 <para>The following command works to load new packages into a
683 Prelude> :set -package <replaceable>name</replaceable>
686 <para>But note that doing this will cause all currently loaded
687 modules to be unloaded, and you'll be dumped back into the
692 <title>Extra libraries</title>
693 <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
695 <para>Extra libraries may be specified on the command line using
696 the normal <literal>-l<replaceable>lib</replaceable></literal>
697 option. For example, to load the “m” library:</para>
703 <para>On systems with <literal>.so</literal>-style shared
704 libraries, the actual library loaded will the
705 <filename>lib<replaceable>lib</replaceable>.so</filename>. GHCi
706 searches the following places for libraries, in this order:</para>
710 <para>Paths specified using the
711 <literal>-L<replaceable>path</replaceable></literal>
712 command-line option,</para>
715 <para>the standard library search path for your system,
716 which on some systems may be overriden by setting the
717 <literal>LD_LIBRARY_PATH</literal> environment
722 <para>On systems with <literal>.dll</literal>-style shared
723 libraries, the actual library loaded will be
724 <filename><replaceable>lib</replaceable>.dll</filename>. Again,
725 GHCi will signal an error if it can't find the library.</para>
727 <para>GHCi can also load plain object files
728 (<literal>.o</literal> or <literal>.obj</literal> depending on
729 your platform) from the command-line. Just add the name the
730 object file to the command line.</para>
735 <sect1 id="ghci-commands">
736 <title>GHCi commands</title>
738 <para>GHCi commands all begin with
739 ‘<literal>:</literal>’ and consist of a single command
740 name followed by zero or more parameters. The command name may be
741 abbreviated, as long as the abbreviation is not ambiguous. All of
742 the builtin commands, with the exception of
743 <literal>:unset</literal> and <literal>:undef</literal>, may be
744 abbreviated to a single letter.</para>
748 <term><literal>:add</literal>
749 <replaceable>module</replaceable> ...</term>
750 <indexterm><primary><literal>:add</literal></primary></indexterm>
752 <para>Add <replaceable>module</replaceable>(s) to the
753 current <firstterm>target set</firstterm>, and perform a
759 <term><literal>:browse</literal>
760 <optional><literal>*</literal></optional><replaceable>module</replaceable>
762 <indexterm><primary><literal>:browse</literal></primary>
765 <para>Displays the identifiers defined by the module
766 <replaceable>module</replaceable>, which must be either
767 loaded into GHCi or be a member of a package. If the
768 <literal>*</literal> symbol is placed before the module
769 name, then <emphasis>all</emphasis> the identifiers defined
770 in <replaceable>module</replaceable> are shown; otherwise
771 the list is limited to the exports of
772 <replaceable>module</replaceable>. The
773 <literal>*</literal>-form is only available for modules
774 which are interpreted; for compiled modules (including
775 modules from packages) only the non-<literal>*</literal>
776 form of <literal>:browse</literal> is available.</para>
781 <term><literal>:cd</literal> <replaceable>dir</replaceable></term>
782 <indexterm><primary><literal>:cd</literal></primary></indexterm>
784 <para>Changes the current working directory to
785 <replaceable>dir</replaceable>. A
786 ‘<literal>˜</literal>’ symbol at the
787 beginning of <replaceable>dir</replaceable> will be replaced
788 by the contents of the environment variable
789 <literal>HOME</literal>.</para>
794 <term><literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable></term>
795 <indexterm><primary><literal>:def</literal></primary></indexterm>
797 <para>The command <literal>:def</literal>
798 <replaceable>name</replaceable>
799 <replaceable>expr</replaceable> defines a new GHCi command
800 <literal>:<replaceable>name</replaceable></literal>,
801 implemented by the Haskell expression
802 <replaceable>expr</replaceable>, which must have type
803 <literal>String -> IO String</literal>. When
804 <literal>:<replaceable>name</replaceable>
805 <replaceable>args</replaceable></literal> is typed at the
806 prompt, GHCi will run the expression
807 <literal>(<replaceable>name</replaceable>
808 <replaceable>args</replaceable>)</literal>, take the
809 resulting <literal>String</literal>, and feed it back into
810 GHCi as a new sequence of commands. Separate commands in
811 the result must be separated by
812 ‘<literal>\n</literal>’.</para>
814 <para>That's all a little confusing, so here's a few
815 examples. To start with, here's a new GHCi command which
816 doesn't take any arguments or produce any results, it just
817 outputs the current date & time:</para>
820 Prelude> let date _ = Time.getClockTime >>= print >> return ""
821 Prelude> :def date date
823 Fri Mar 23 15:16:40 GMT 2001
826 <para>Here's an example of a command that takes an argument.
827 It's a re-implementation of <literal>:cd</literal>:</para>
830 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
831 Prelude> :def mycd mycd
835 <para>Or I could define a simple way to invoke
836 “<literal>ghc ––make Main</literal>” in the
837 current directory:</para>
840 Prelude> :def make (\_ -> return ":! ghc ––make Main")
847 <term><literal>:help</literal></term>
848 <indexterm><primary><literal>:help</literal></primary></indexterm>
849 <term><literal>:?</literal></term>
850 <indexterm><primary><literal>:?</literal></primary></indexterm>
852 <para>Displays a list of the available commands.</para>
857 <term><literal>:info</literal> <replaceable>name</replaceable>
859 <indexterm><primary><literal>:info</literal></primary>
862 <para>Displays information about the given name(s). For
863 example, if <replaceable>name</replaceable> is a class, then
864 the class methods and their types will be printed; if
865 <replaceable>name</replaceable> is a type constructor, then
866 its definition will be printed; if
867 <replaceable>name</replaceable> is a function, then its type
868 will be printed. If <replaceable>name</replaceable> has
869 been loaded from a source file, then GHCi will also display
870 the location of its definition in the source.</para>
875 <term><literal>:load</literal>
876 <replaceable>module</replaceable> ...</term>
877 <indexterm><primary><literal>:load</literal></primary></indexterm>
879 <para>Recursively loads the specified
880 <replaceable>module</replaceable>s, and all the modules they
881 depend on. Here, each <replaceable>module</replaceable>
882 must be a module name or filename, but may not be the name
883 of a module in a package.</para>
885 <para>All previously loaded modules, except package modules,
886 are forgotten. The new set of modules is known as the
887 <firstterm>target set</firstterm>. Note that
888 <literal>:load</literal> can be used without any arguments
889 to unload all the currently loaded modules and
892 <para>After a <literal>:load</literal> command, the current
893 context is set to:</para>
897 <para><replaceable>module</replaceable>, if it was loaded
898 successfully, or</para>
901 <para>the most recently successfully loaded module, if
902 any other modules were loaded as a result of the current
903 <literal>:load</literal>, or</para>
906 <para><literal>Prelude</literal> otherwise.</para>
913 <term><literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal></term>
914 <indexterm><primary><literal>:module</literal></primary></indexterm>
916 <para>Sets or modifies the current context for statements
917 typed at the prompt. See <xref linkend="ghci-scope"> for
923 <term><literal>:quit</literal></term>
924 <indexterm><primary><literal>:quit</literal></primary></indexterm>
926 <para>Quits GHCi. You can also quit by typing a control-D
927 at the prompt.</para>
932 <term><literal>:reload</literal></term>
933 <indexterm><primary><literal>:reload</literal></primary></indexterm>
935 <para>Attempts to reload the current target set (see
936 <literal>:load</literal>) if any of the modules in the set,
937 or any dependent module, has changed. Note that this may
938 entail loading new modules, or dropping modules which are no
939 longer indirectly required by the target.</para>
944 <term><literal>:set</literal> <optional><replaceable>option</replaceable>...</optional></term>
945 <indexterm><primary><literal>:set</literal></primary></indexterm>
947 <para>Sets various options. See <xref linkend="ghci-set">
948 for a list of available options. The
949 <literal>:set</literal> command by itself shows which
950 options are currently set.</para>
955 <term><literal>:set</literal> <literal>args</literal>
956 <replaceable>arg</replaceable> ...</term>
957 <indexterm><primary><literal>:set</literal></primary></indexterm>
959 <para>Sets the list of arguments which are returned when the
960 program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
966 <term><literal>:set</literal> <literal>prog</literal>
967 <replaceable>prog</replaceable></term>
968 <indexterm><primary><literal>:set</literal></primary></indexterm>
970 <para>Sets the string to be returned when the program calls
971 <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
977 <term><literal>:show bindings</literal></term>
978 <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
980 <para>Show the bindings made at the prompt and their
986 <term><literal>:show modules</literal></term>
987 <indexterm><primary><literal>:show modules</literal></primary></indexterm>
989 <para>Show the list of modules currently load.</para>
994 <term><literal>:type</literal> <replaceable>expression</replaceable></term>
995 <indexterm><primary><literal>:type</literal></primary></indexterm>
997 <para>Infers and prints the type of
998 <replaceable>expression</replaceable>, including explicit
999 forall quantifiers for polymorphic types. The monomorphism
1000 restriction is <emphasis>not</emphasis> applied to the
1001 expression during type inference.</para>
1006 <term><literal>:undef</literal> <replaceable>name</replaceable></term>
1007 <indexterm><primary><literal>:undef</literal></primary></indexterm>
1009 <para>Undefines the user-defined command
1010 <replaceable>name</replaceable> (see <literal>:def</literal>
1016 <term><literal>:unset</literal> <replaceable>option</replaceable>...</term>
1017 <indexterm><primary><literal>:unset</literal></primary></indexterm>
1019 <para>Unsets certain options. See <xref linkend="ghci-set">
1020 for a list of available options.</para>
1025 <term><literal>:!</literal> <replaceable>command</replaceable>...</term>
1026 <indexterm><primary><literal>:!</literal></primary></indexterm>
1027 <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
1029 <para>Executes the shell command
1030 <replaceable>command</replaceable>.</para>
1037 <sect1 id="ghci-set">
1038 <title>The <literal>:set</literal> command</title>
1039 <indexterm><primary><literal>:set</literal></primary></indexterm>
1041 <para>The <literal>:set</literal> command sets two types of
1042 options: GHCi options, which begin with
1043 ‘<literal>+</literal>” and “command-line”
1044 options, which begin with ‘-’. </para>
1046 <para>NOTE: at the moment, the <literal>:set</literal> command
1047 doesn't support any kind of quoting in its arguments: quotes will
1048 not be removed and cannot be used to group words together. For
1049 example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
1053 <title>GHCi options</title>
1054 <indexterm><primary>options</primary><secondary>GHCi</secondary>
1057 <para>GHCi options may be set using <literal>:set</literal> and
1058 unset using <literal>:unset</literal>.</para>
1060 <para>The available GHCi options are:</para>
1064 <term><literal>+r</literal></term>
1065 <indexterm><primary><literal>+r</literal></primary></indexterm>
1066 <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
1067 <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
1069 <para>Normally, any evaluation of top-level expressions
1070 (otherwise known as CAFs or Constant Applicative Forms) in
1071 loaded modules is retained between evaluations. Turning
1072 on <literal>+r</literal> causes all evaluation of
1073 top-level expressions to be discarded after each
1074 evaluation (they are still retained
1075 <emphasis>during</emphasis> a single evaluation).</para>
1077 <para>This option may help if the evaluated top-level
1078 expressions are consuming large amounts of space, or if
1079 you need repeatable performance measurements.</para>
1084 <term><literal>+s</literal></term>
1085 <indexterm><primary><literal>+s</literal></primary></indexterm>
1087 <para>Display some stats after evaluating each expression,
1088 including the elapsed time and number of bytes allocated.
1089 NOTE: the allocation figure is only accurate to the size
1090 of the storage manager's allocation area, because it is
1091 calculated at every GC. Hence, you might see values of
1092 zero if no GC has occurred.</para>
1097 <term><literal>+t</literal></term>
1098 <indexterm><primary><literal>+t</literal></primary></indexterm>
1100 <para>Display the type of each variable bound after a
1101 statement is entered at the prompt. If the statement is a
1102 single expression, then the only variable binding will be
1104 ‘<literal>it</literal>’.</para>
1110 <sect2 id="ghci-cmd-line-options">
1111 <title>Setting GHC command-line options in GHCi</title>
1113 <para>Normal GHC command-line options may also be set using
1114 <literal>:set</literal>. For example, to turn on
1115 <option>-fglasgow-exts</option>, you would say:</para>
1118 Prelude> :set -fglasgow-exts
1121 <para>Any GHC command-line option that is designated as
1122 <firstterm>dynamic</firstterm> (see the table in <xref
1123 linkend="flag-reference">), may be set using
1124 <literal>:set</literal>. To unset an option, you can set the
1125 reverse option:</para>
1126 <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
1129 Prelude> :set -fno-glasgow-exts
1132 <para><xref linkend="flag-reference"> lists the reverse for each
1133 option where applicable.</para>
1135 <para>Certain static options (<option>-package</option>,
1136 <option>-I</option>, <option>-i</option>, and
1137 <option>-l</option> in particular) will also work, but some may
1138 not take effect until the next reload.</para>
1139 <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
1143 <sect1 id="ghci-dot-files">
1144 <title>The <filename>.ghci</filename> file</title>
1145 <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
1147 <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
1150 <para>When it starts, GHCi always reads and executes commands from
1151 <filename>$HOME/.ghci</filename>, followed by
1152 <filename>./.ghci</filename>.</para>
1154 <para>The <filename>.ghci</filename> in your home directory is
1155 most useful for turning on favourite options (eg. <literal>:set
1156 +s</literal>), and defining useful macros. Placing a
1157 <filename>.ghci</filename> file in a directory with a Haskell
1158 project is a useful way to set certain project-wide options so you
1159 don't have to type them everytime you start GHCi: eg. if your
1160 project uses GHC extensions and CPP, and has source files in three
1161 subdirectories A B and C, you might put the following lines in
1162 <filename>.ghci</filename>:</para>
1165 :set -fglasgow-exts -cpp
1169 <para>(Note that strictly speaking the <option>-i</option> flag is
1170 a static one, but in fact it works to set it using
1171 <literal>:set</literal> like this. The changes won't take effect
1172 until the next <literal>:load</literal>, though.)</para>
1174 <para>Two command-line options control whether the
1175 <filename>.ghci</filename> files are read:</para>
1179 <term><option>-ignore-dot-ghci</option></term>
1180 <indexterm><primary><option>-ignore-dot-ghci</option></primary>
1183 <para>Don't read either <filename>./.ghci</filename> or
1184 <filename>$HOME/.ghci</filename> when starting up.</para>
1188 <term><option>-read-dot-ghci</option></term>
1189 <indexterm><primary><option>-read-dot-ghci</option></primary>
1192 <para>Read <filename>.ghci</filename> and
1193 <filename>$HOME/.ghci</filename>. This is normally the
1194 default, but the <option>-read-dot-ghci</option> option may
1195 be used to override a previous
1196 <option>-ignore-dot-ghci</option> option.</para>
1204 <title>FAQ and Things To Watch Out For</title>
1208 <term>GHCi complains about <function>main</function> not being
1209 in scope when I load a module.</term>
1210 <indexterm><primary><function>main</function></primary><secondary>with GHCi</secondary>
1213 <para>You probably omitted the <literal>module</literal>
1214 declaration at the top of the module, which causes the
1215 module name to default to <literal>Main</literal>. In
1216 Haskell, the <literal>Main</literal> module must define a
1217 function called <function>main</function>. Admittedly this
1218 doesn't make a great deal of sense for an interpreter, but
1219 the rule was kept for compatibility with GHC.</para>
1224 <term>The interpreter can't load modules with foreign export
1225 declarations!</term>
1227 <para>Unfortunately not. We haven't implemented it yet.
1228 Please compile any offending modules by hand before loading
1229 them into GHCi.</para>
1234 <term><literal>-O</literal> doesn't work with GHCi!</term>
1235 <indexterm><primary><option>-O</option></primary>
1238 <para>For technical reasons, the bytecode compiler doesn't
1239 interact well with one of the optimisation passes, so we
1240 have disabled optimisation when using the interpreter. This
1241 isn't a great loss: you'll get a much bigger win by
1242 compiling the bits of your code that need to go fast, rather
1243 than interpreting them with optimisation turned on.</para>
1248 <term>Unboxed tuples don't work with GHCi</term>
1250 <para>That's right. You can always compile a module that
1251 uses unboxed tuples and load it into GHCi, however.
1252 (Incidentally the previous point, namely that
1253 <literal>-O</literal> is incompatible with GHCi, is because
1254 the bytecode compiler can't deal with unboxed
1260 <term>Concurrent threads don't carry on running when GHCi is
1261 waiting for input.</term>
1263 <para>No, they don't. This is because the Haskell binding
1264 to the GNU readline library doesn't support reading from the
1265 terminal in a non-blocking way, which is required to work
1266 properly with GHC's concurrency model.</para>
1271 <term>After using <literal>getContents</literal>, I can't use
1272 <literal>stdin</literal> again until I do
1273 <literal>:load</literal> or <literal>:reload</literal>.</term>
1276 <para>This is the defined behaviour of
1277 <literal>getContents</literal>: it puts the stdin Handle in
1278 a state known as <firstterm>semi-closed</firstterm>, wherein
1279 any further I/O operations on it are forbidden. Because I/O
1280 state is retained between computations, the semi-closed
1281 state persists until the next <literal>:load</literal> or
1282 <literal>:reload</literal> command.</para>
1284 <para>You can make <literal>stdin</literal> reset itself
1285 after every evaluation by giving GHCi the command
1286 <literal>:set +r</literal>. This works because
1287 <literal>stdin</literal> is just a top-level expression that
1288 can be reverted to its unevaluated state in the same way as
1289 any other top-level expression (CAF).</para>
1299 ;;; Local Variables: ***
1301 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***