Don't show the ghci :? output in the user guide
[ghc-hetmet.git] / docs / users_guide / ghci.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="ghci">
3   <title>Using GHCi</title>
4   <indexterm><primary>GHCi</primary></indexterm>
5   <indexterm><primary>interpreter</primary><see>GHCi</see></indexterm>
6   <indexterm><primary>interactive</primary><see>GHCi</see></indexterm>
7   
8   <para>GHCi<footnote>
9       <para>The &lsquo;i&rsquo; stands for &ldquo;Interactive&rdquo;</para>
10     </footnote>
11   is GHC's interactive environment, in which Haskell expressions can
12   be interactively evaluated and programs can be interpreted.  If
13   you're familiar with <ulink url="http://www.haskell.org/hugs/">Hugs</ulink><indexterm><primary>Hugs</primary>
14   </indexterm>, then you'll be right at home with GHCi.  However, GHCi
15   also has support for interactively loading compiled code, as well as
16   supporting all<footnote><para>except <literal>foreign export</literal>, at the moment</para>
17   </footnote> the language extensions that GHC provides.
18   <indexterm><primary>FFI</primary><secondary>GHCi support</secondary></indexterm>
19   <indexterm><primary>Foreign Function
20   Interface</primary><secondary>GHCi support</secondary></indexterm>.
21   GHCi also includes an interactive debugger (see <xref linkend="ghci-debugger"/>).</para>
22
23   <sect1 id="ghci-introduction">
24     <title>Introduction to GHCi</title>
25
26     <para>Let's start with an example GHCi session.  You can fire up
27     GHCi with the command <literal>ghci</literal>:</para>
28
29 <screen>
30 $ ghci
31 GHCi, version 6.8.1: http://www.haskell.org/ghc/  :? for help
32 Loading package base ... linking ... done.
33 Prelude> 
34 </screen>
35
36     <para>There may be a short pause while GHCi loads the prelude and
37     standard libraries, after which the prompt is shown. As the banner
38     says, you can type <literal>:?</literal> to see the list of commands
39     available, and a half line description of each of them.</para>
40
41     <para>We'll explain most of these commands as we go along.  For
42     Hugs users: many things work the same as in Hugs, so you should be
43     able to get going straight away.</para>
44
45     <para>Haskell expressions can be typed at the prompt:</para>
46     <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
47   </indexterm>
48
49 <screen>
50 Prelude> 1+2
51 3
52 Prelude> let x = 42 in x / 9
53 4.666666666666667
54 Prelude> 
55 </screen>
56
57     <para>GHCi interprets the whole line as an expression to evaluate.
58     The expression may not span several lines - as soon as you press
59     enter, GHCi will attempt to evaluate it.</para>
60   </sect1>
61
62   <sect1 id="loading-source-files">
63     <title>Loading source files</title>
64
65     <para>Suppose we have the following Haskell source code, which we
66     place in a file <filename>Main.hs</filename>:</para>
67
68 <programlisting>
69 main = print (fac 20)
70
71 fac 0 = 1
72 fac n = n * fac (n-1)
73 </programlisting>
74
75     <para>You can save <filename>Main.hs</filename> anywhere you like,
76     but if you save it somewhere other than the current
77     directory<footnote><para>If you started up GHCi from the command
78     line then GHCi's current directory is the same as the current
79     directory of the shell from which it was started.  If you started
80     GHCi from the &ldquo;Start&rdquo; menu in Windows, then the
81     current directory is probably something like
82     <filename>C:\Documents and Settings\<replaceable>user
83     name</replaceable></filename>.</para> </footnote> then we will
84     need to change to the right directory in GHCi:</para>
85
86 <screen>
87 Prelude> :cd <replaceable>dir</replaceable>
88 </screen>
89
90     <para>where <replaceable>dir</replaceable> is the directory (or
91     folder) in which you saved <filename>Main.hs</filename>.</para>
92
93     <para>To load a Haskell source file into GHCi, use the
94     <literal>:load</literal> command:</para>
95     <indexterm><primary><literal>:load</literal></primary></indexterm>
96
97 <screen>
98 Prelude> :load Main
99 Compiling Main             ( Main.hs, interpreted )
100 Ok, modules loaded: Main.
101 *Main>
102 </screen>
103
104     <para>GHCi has loaded the <literal>Main</literal> module, and the
105     prompt has changed to &ldquo;<literal>*Main></literal>&rdquo; to
106     indicate that the current context for expressions typed at the
107     prompt is the <literal>Main</literal> module we just loaded (we'll
108     explain what the <literal>*</literal> means later in <xref
109     linkend="ghci-scope"/>).  So we can now type expressions involving
110     the functions from <filename>Main.hs</filename>:</para>
111
112 <screen>
113 *Main> fac 17
114 355687428096000
115 </screen>
116
117     <para>Loading a multi-module program is just as straightforward;
118     just give the name of the &ldquo;topmost&rdquo; module to the
119     <literal>:load</literal> command (hint: <literal>:load</literal>
120     can be abbreviated to <literal>:l</literal>).  The topmost module
121     will normally be <literal>Main</literal>, but it doesn't have to
122     be.  GHCi will discover which modules are required, directly or
123     indirectly, by the topmost module, and load them all in dependency
124     order.</para>
125
126     <sect2 id="ghci-modules-filenames">
127       <title>Modules vs. filenames</title>
128       <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
129       <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
130       
131       <para>Question: How does GHC find the filename which contains
132       module <replaceable>M</replaceable>?  Answer: it looks for the
133       file <literal><replaceable>M</replaceable>.hs</literal>, or
134       <literal><replaceable>M</replaceable>.lhs</literal>.  This means
135       that for most modules, the module name must match the filename.
136       If it doesn't, GHCi won't be able to find it.</para>
137
138       <para>There is one exception to this general rule: when you load
139       a program with <literal>:load</literal>, or specify it when you
140       invoke <literal>ghci</literal>, you can give a filename rather
141       than a module name.  This filename is loaded if it exists, and
142       it may contain any module you like.  This is particularly
143       convenient if you have several <literal>Main</literal> modules
144       in the same directory and you can't call them all
145       <filename>Main.hs</filename>.</para>
146
147       <para>The search path for finding source files is specified with
148       the <option>-i</option> option on the GHCi command line, like
149       so:</para>
150 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
151
152       <para>or it can be set using the <literal>:set</literal> command
153       from within GHCi (see <xref
154       linkend="ghci-cmd-line-options"/>)<footnote><para>Note that in
155       GHCi, and <option>&ndash;&ndash;make</option> mode, the <option>-i</option>
156       option is used to specify the search path for
157       <emphasis>source</emphasis> files, whereas in standard
158       batch-compilation mode the <option>-i</option> option is used to
159       specify the search path for interface files, see <xref
160       linkend="search-path"/>.</para> </footnote></para>
161
162       <para>One consequence of the way that GHCi follows dependencies
163       to find modules to load is that every module must have a source
164       file.  The only exception to the rule is modules that come from
165       a package, including the <literal>Prelude</literal> and standard
166       libraries such as <literal>IO</literal> and
167       <literal>Complex</literal>.  If you attempt to load a module for
168       which GHCi can't find a source file, even if there are object
169       and interface files for the module, you'll get an error
170       message.</para>
171     </sect2>
172
173     <sect2>
174       <title>Making changes and recompilation</title>
175       <indexterm><primary><literal>:reload</literal></primary></indexterm>
176
177       <para>If you make some changes to the source code and want GHCi
178       to recompile the program, give the <literal>:reload</literal>
179       command.  The program will be recompiled as necessary, with GHCi
180       doing its best to avoid actually recompiling modules if their
181       external dependencies haven't changed.  This is the same
182       mechanism we use to avoid re-compiling modules in the batch
183       compilation setting (see <xref linkend="recomp"/>).</para>
184     </sect2>
185   </sect1>
186
187   <sect1 id="ghci-compiled">
188     <title>Loading compiled code</title>
189     <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
190
191     <para>When you load a Haskell source module into GHCi, it is
192     normally converted to byte-code and run using the interpreter.
193     However, interpreted code can also run alongside compiled code in
194     GHCi; indeed, normally when GHCi starts, it loads up a compiled
195     copy of the <literal>base</literal> package, which contains the
196     <literal>Prelude</literal>.</para>
197
198     <para>Why should we want to run compiled code?  Well, compiled
199     code is roughly 10x faster than interpreted code, but takes about
200     2x longer to produce (perhaps longer if optimisation is on).  So
201     it pays to compile the parts of a program that aren't changing
202     very often, and use the interpreter for the code being actively
203     developed.</para>
204
205     <para>When loading up source files with <literal>:load</literal>,
206     GHCi looks for any corresponding compiled object files, and will
207     use one in preference to interpreting the source if possible.  For
208     example, suppose we have a 4-module program consisting of modules
209     A, B, C, and D.  Modules B and C both import D only,
210     and A imports both B &amp; C:</para>
211 <screen>
212       A
213      / \
214     B   C
215      \ /
216       D
217 </screen>
218     <para>We can compile D, then load the whole program, like this:</para>
219 <screen>
220 Prelude> :! ghc -c D.hs
221 Prelude> :load A
222 Skipping  D                ( D.hs, D.o )
223 Compiling C                ( C.hs, interpreted )
224 Compiling B                ( B.hs, interpreted )
225 Compiling A                ( A.hs, interpreted )
226 Ok, modules loaded: A, B, C, D.
227 *Main>
228 </screen>
229
230     <para>In the messages from the compiler, we see that it skipped D,
231     and used the object file <filename>D.o</filename>.  The message
232     <literal>Skipping</literal> <replaceable>module</replaceable>
233     indicates that compilation for <replaceable>module</replaceable>
234     isn't necessary, because the source and everything it depends on
235     is unchanged since the last compilation.</para>
236
237     <para>At any time you can use the command 
238     <literal>:show modules</literal>
239     to get a list of the modules currently loaded
240     into GHCi:</para>
241
242 <screen>
243 *Main> :show modules
244 D                ( D.hs, D.o )
245 C                ( C.hs, interpreted )
246 B                ( B.hs, interpreted )
247 A                ( A.hs, interpreted )
248 *Main></screen>
249
250     <para>If we now modify the source of D (or pretend to: using Unix
251     command <literal>touch</literal> on the source file is handy for
252     this), the compiler will no longer be able to use the object file,
253     because it might be out of date:</para>
254
255 <screen>
256 *Main> :! touch D.hs
257 *Main> :reload
258 Compiling D                ( D.hs, interpreted )
259 Skipping  C                ( C.hs, interpreted )
260 Skipping  B                ( B.hs, interpreted )
261 Skipping  A                ( A.hs, interpreted )
262 Ok, modules loaded: A, B, C, D.
263 *Main> 
264 </screen>
265
266     <para>Note that module D was compiled, but in this instance
267     because its source hadn't really changed, its interface remained
268     the same, and the recompilation checker determined that A, B and C
269     didn't need to be recompiled.</para>
270
271     <para>So let's try compiling one of the other modules:</para>
272
273 <screen>
274 *Main> :! ghc -c C.hs
275 *Main> :load A
276 Compiling D                ( D.hs, interpreted )
277 Compiling C                ( C.hs, interpreted )
278 Compiling B                ( B.hs, interpreted )
279 Compiling A                ( A.hs, interpreted )
280 Ok, modules loaded: A, B, C, D.
281 </screen>
282
283     <para>We didn't get the compiled version of C!  What happened?
284     Well, in GHCi a compiled module may only depend on other compiled
285     modules, and in this case C depends on D, which doesn't have an
286     object file, so GHCi also rejected C's object file.  Ok, so let's
287     also compile D:</para>
288
289 <screen>
290 *Main> :! ghc -c D.hs
291 *Main> :reload
292 Ok, modules loaded: A, B, C, D.
293 </screen>
294
295     <para>Nothing happened!  Here's another lesson: newly compiled
296     modules aren't picked up by <literal>:reload</literal>, only
297     <literal>:load</literal>:</para>
298
299 <screen>
300 *Main> :load A
301 Skipping  D                ( D.hs, D.o )
302 Skipping  C                ( C.hs, C.o )
303 Compiling B                ( B.hs, interpreted )
304 Compiling A                ( A.hs, interpreted )
305 Ok, modules loaded: A, B, C, D.
306 </screen>
307
308     <para>HINT: since GHCi will only use a compiled object file if it
309     can be sure that the compiled version is up-to-date, a good technique
310     when working on a large program is to occasionally run
311     <literal>ghc &ndash;&ndash;make</literal> to compile the whole project (say
312     before you go for lunch :-), then continue working in the
313     interpreter.  As you modify code, the new modules will be
314     interpreted, but the rest of the project will remain
315     compiled.</para>
316
317   </sect1>
318
319   <sect1 id="interactive-evaluation">
320     <title>Interactive evaluation at the prompt</title>
321
322     <para>When you type an expression at the prompt, GHCi immediately
323     evaluates and prints the result:
324 <screen>
325 Prelude> reverse "hello"
326 "olleh"
327 Prelude> 5+5
328 10
329 </screen>
330 </para>
331
332 <sect2><title>I/O actions at the prompt</title>
333
334 <para>GHCi does more than simple expression evaluation at the prompt.
335 If you type something of type <literal>IO a</literal> for some
336     <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
337     as an IO-computation.
338 <screen>
339 Prelude> "hello"
340 "hello"
341 Prelude> putStrLn "hello"
342 hello
343 </screen>
344 Furthermore, GHCi will print the result of the I/O action if (and only
345 if):
346 <itemizedlist>
347   <listitem><para>The result type is an instance of <literal>Show</literal>.</para></listitem>
348   <listitem><para>The result type is not
349   <literal>()</literal>.</para></listitem>
350 </itemizedlist>
351 For example, remembering that <literal>putStrLn :: String -> IO ()</literal>:
352 <screen>
353 Prelude> putStrLn "hello"
354 hello
355 Prelude> do { putStrLn "hello"; return "yes" }
356 hello
357 "yes"
358 </screen>
359 </para></sect2>
360
361     <sect2 id="ghci-stmts">
362       <title>Using <literal>do-</literal>notation at the prompt</title>
363       <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
364       <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
365       
366       <para>GHCi actually accepts <firstterm>statements</firstterm>
367       rather than just expressions at the prompt.  This means you can
368       bind values and functions to names, and use them in future
369       expressions or statements.</para>
370
371       <para>The syntax of a statement accepted at the GHCi prompt is
372       exactly the same as the syntax of a statement in a Haskell
373       <literal>do</literal> expression.  However, there's no monad
374       overloading here: statements typed at the prompt must be in the
375       <literal>IO</literal> monad.
376 <screen>
377 Prelude> x &lt;- return 42
378 42
379 Prelude> print x
380 42
381 Prelude>
382 </screen>
383       The statement <literal>x &lt;- return 42</literal> means
384       &ldquo;execute <literal>return 42</literal> in the
385       <literal>IO</literal> monad, and bind the result to
386       <literal>x</literal>&rdquo;.  We can then use
387       <literal>x</literal> in future statements, for example to print
388       it as we did above.</para>
389
390       <para>GHCi will print the result of a statement if and only if: 
391         <itemizedlist>
392           <listitem>
393             <para>The statement is not a binding, or it is a monadic binding 
394               (<literal>p &lt;- e</literal>) that binds exactly one
395               variable.</para>
396           </listitem>
397           <listitem>
398             <para>The variable's type is not polymorphic, is not
399               <literal>()</literal>, and is an instance of
400               <literal>Show</literal></para>
401           </listitem>
402         </itemizedlist>
403       The automatic printing of binding results can be supressed with
404       <option>:set -fno-print-bind-result</option> (this does not
405       supress printing the result of non-binding statements).
406       <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
407       You might want to do this to prevent the result of binding
408       statements from being fully evaluated by the act of printing
409       them, for example.</para>
410
411       <para>Of course, you can also bind normal non-IO expressions
412       using the <literal>let</literal>-statement:</para>
413 <screen>
414 Prelude> let x = 42
415 Prelude> x
416 42
417 Prelude>
418 </screen>
419       <para>Another important difference between the two types of binding
420       is that the monadic bind (<literal>p &lt;- e</literal>) is
421       <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
422       whereas with the <literal>let</literal> form, the expression
423       isn't evaluated immediately:</para>
424 <screen>
425 Prelude> let x = error "help!"
426 Prelude> print x
427 *** Exception: help!
428 Prelude>
429 </screen>
430
431       <para>Note that <literal>let</literal> bindings do not automatically
432         print the value bound, unlike monadic bindings.</para>
433
434       <para>Any exceptions raised during the evaluation or execution
435       of the statement are caught and printed by the GHCi command line
436       interface (for more information on exceptions, see the module
437       <literal>Control.Exception</literal> in the libraries
438       documentation).</para>
439
440       <para>Every new binding shadows any existing bindings of the
441       same name, including entities that are in scope in the current
442       module context.</para>
443
444       <para>WARNING: temporary bindings introduced at the prompt only
445       last until the next <literal>:load</literal> or
446       <literal>:reload</literal> command, at which time they will be
447       simply lost.  However, they do survive a change of context with
448       <literal>:module</literal>: the temporary bindings just move to
449       the new location.</para>
450
451       <para>HINT: To get a list of the bindings currently in scope, use the
452       <literal>:show bindings</literal> command:</para>
453
454 <screen>
455 Prelude> :show bindings
456 x :: Int
457 Prelude></screen>
458
459       <para>HINT: if you turn on the <literal>+t</literal> option,
460       GHCi will show the type of each variable bound by a statement.
461       For example:</para>
462       <indexterm><primary><literal>+t</literal></primary></indexterm>
463 <screen>
464 Prelude> :set +t
465 Prelude> let (x:xs) = [1..]
466 x :: Integer
467 xs :: [Integer]
468 </screen>
469
470     </sect2>
471
472     <sect2 id="ghci-scope">
473       <title>What's really in scope at the prompt?</title> 
474
475       <para>When you type an expression at the prompt, what
476       identifiers and types are in scope?  GHCi provides a flexible
477       way to control exactly how the context for an expression is
478       constructed.  Let's start with the simple cases; when you start
479       GHCi the prompt looks like this:</para>
480
481 <screen>Prelude></screen>
482
483       <para>Which indicates that everything from the module
484       <literal>Prelude</literal> is currently in scope.  If we now
485       load a file into GHCi, the prompt will change:</para>
486
487 <screen>
488 Prelude> :load Main.hs
489 Compiling Main             ( Main.hs, interpreted )
490 *Main>
491 </screen>
492
493       <para>The new prompt is <literal>*Main</literal>, which
494       indicates that we are typing expressions in the context of the
495       top-level of the <literal>Main</literal> module.  Everything
496       that is in scope at the top-level in the module
497       <literal>Main</literal> we just loaded is also in scope at the
498       prompt (probably including <literal>Prelude</literal>, as long
499       as <literal>Main</literal> doesn't explicitly hide it).</para>
500
501       <para>The syntax
502       <literal>*<replaceable>module</replaceable></literal> indicates
503       that it is the full top-level scope of
504       <replaceable>module</replaceable> that is contributing to the
505       scope for expressions typed at the prompt.  Without the
506       <literal>*</literal>, just the exports of the module are
507       visible.</para>
508
509       <para>We're not limited to a single module: GHCi can combine
510       scopes from multiple modules, in any mixture of
511       <literal>*</literal> and non-<literal>*</literal> forms.  GHCi
512       combines the scopes from all of these modules to form the scope
513       that is in effect at the prompt.  For technical reasons, GHCi
514       can only support the <literal>*</literal>-form for modules which
515       are interpreted, so compiled modules and package modules can
516       only contribute their exports to the current scope.</para>
517
518       <para>The scope is manipulated using the
519       <literal>:module</literal> command.  For example, if the current
520       scope is <literal>Prelude</literal>, then we can bring into
521       scope the exports from the module <literal>IO</literal> like
522       so:</para>
523
524 <screen>
525 Prelude> :module +IO
526 Prelude IO> hPutStrLn stdout "hello\n"
527 hello
528 Prelude IO>
529 </screen>
530
531       <para>(Note: you can use <literal>import M</literal> as an
532       alternative to <literal>:module +M</literal>, and
533       <literal>:module</literal> can also be shortened to 
534       <literal>:m</literal>). The full syntax of the
535       <literal>:module</literal> command is:</para>
536
537 <screen>
538 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
539 </screen>
540
541       <para>Using the <literal>+</literal> form of the
542       <literal>module</literal> commands adds modules to the current
543       scope, and <literal>-</literal> removes them.  Without either
544       <literal>+</literal> or <literal>-</literal>, the current scope
545       is replaced by the set of modules specified.  Note that if you
546       use this form and leave out <literal>Prelude</literal>, GHCi
547       will assume that you really wanted the
548       <literal>Prelude</literal> and add it in for you (if you don't
549       want the <literal>Prelude</literal>, then ask to remove it with
550       <literal>:m -Prelude</literal>).</para>
551
552       <para>The scope is automatically set after a
553       <literal>:load</literal> command, to the most recently loaded
554       "target" module, in a <literal>*</literal>-form if possible.
555       For example, if you say <literal>:load foo.hs bar.hs</literal>
556       and <filename>bar.hs</filename> contains module
557       <literal>Bar</literal>, then the scope will be set to
558       <literal>*Bar</literal> if <literal>Bar</literal> is
559       interpreted, or if <literal>Bar</literal> is compiled it will be
560       set to <literal>Prelude Bar</literal> (GHCi automatically adds
561       <literal>Prelude</literal> if it isn't present and there aren't
562       any <literal>*</literal>-form modules).</para>
563
564       <para>With multiple modules in scope, especially multiple
565       <literal>*</literal>-form modules, it is likely that name
566       clashes will occur.  Haskell specifies that name clashes are
567       only reported when an ambiguous identifier is used, and GHCi
568       behaves in the same way for expressions typed at the
569       prompt.</para>
570
571       <para>
572         Hint: GHCi will tab-complete names that are in scope; for
573         example, if you run GHCi and type <literal>J&lt;tab&gt;</literal>
574         then GHCi will expand it to <literal>Just </literal>.
575       </para>
576
577       <sect3>
578         <title>Qualified names</title>
579
580         <para>To make life slightly easier, the GHCi prompt also
581         behaves as if there is an implicit <literal>import
582         qualified</literal> declaration for every module in every
583         package, and every module currently loaded into GHCi.</para>
584       </sect3>
585
586       <sect3>
587         <title>The <literal>:main</literal> command</title>
588
589         <para>
590           When a program is compiled and executed, it can use the
591           <literal>getArgs</literal> function to access the
592           command-line arguments.
593           However, we cannot simply pass the arguments to the
594           <literal>main</literal> function while we are testing in ghci,
595           as the <literal>main</literal> function doesn't take its
596           directly.
597         </para>
598
599         <para>
600           Instead, we can use the <literal>:main</literal> command.
601           This runs whatever <literal>main</literal> is in scope, with
602           any arguments being treated the same as command-line arguments,
603           e.g.:
604         </para>
605
606 <screen>
607 Prelude> let main = System.Environment.getArgs >>= print
608 Prelude> :main foo bar
609 ["foo","bar"]
610 </screen>
611
612       </sect3>
613     </sect2>
614   
615
616     <sect2>
617       <title>The <literal>it</literal> variable</title>
618       <indexterm><primary><literal>it</literal></primary>
619       </indexterm>
620       
621       <para>Whenever an expression (or a non-binding statement, to be
622       precise) is typed at the prompt, GHCi implicitly binds its value
623       to the variable <literal>it</literal>.  For example:</para>
624 <screen>
625 Prelude> 1+2
626 3
627 Prelude> it * 2
628 6
629 </screen>
630     <para>What actually happens is that GHCi typechecks the
631     expression, and if it doesn't have an <literal>IO</literal> type,
632     then it transforms it as follows: an expression
633     <replaceable>e</replaceable> turns into 
634 <screen>
635 let it = <replaceable>e</replaceable>;
636 print it
637 </screen>
638     which is then run as an IO-action.</para>
639
640     <para>Hence, the original expression must have a type which is an
641     instance of the <literal>Show</literal> class, or GHCi will
642     complain:</para>
643
644 <screen>
645 Prelude&gt; id
646
647 &lt;interactive&gt;:1:0:
648     No instance for (Show (a -&gt; a))
649       arising from use of `print' at &lt;interactive&gt;:1:0-1
650     Possible fix: add an instance declaration for (Show (a -> a))
651     In the expression: print it
652     In a 'do' expression: print it
653 </screen>
654
655     <para>The error message contains some clues as to the
656     transformation happening internally.</para>
657
658       <para>If the expression was instead of type <literal>IO a</literal> for
659       some <literal>a</literal>, then <literal>it</literal> will be
660       bound to the result of the <literal>IO</literal> computation,
661       which is of type <literal>a</literal>.  eg.:</para>
662 <screen>
663 Prelude> Time.getClockTime
664 Wed Mar 14 12:23:13 GMT 2001
665 Prelude> print it
666 Wed Mar 14 12:23:13 GMT 2001
667 </screen>
668
669       <para>The corresponding translation for an IO-typed
670       <replaceable>e</replaceable> is
671 <screen>
672 it &lt;- <replaceable>e</replaceable>
673 </screen>
674       </para>
675
676       <para>Note that <literal>it</literal> is shadowed by the new
677       value each time you evaluate a new expression, and the old value
678       of <literal>it</literal> is lost.</para>
679
680     </sect2>
681
682     <sect2 id="extended-default-rules">
683       <title>Type defaulting in GHCi</title>
684     <indexterm><primary>Type default</primary></indexterm>
685     <indexterm><primary><literal>Show</literal> class</primary></indexterm>
686       <para>
687       Consider this GHCi session:
688 <programlisting>
689   ghci> reverse []
690 </programlisting>
691       What should GHCi do?  Strictly speaking, the program is ambiguous.  <literal>show (reverse [])</literal>
692       (which is what GHCi computes here) has type <literal>Show a => a</literal> and how that displays depends 
693       on the type <literal>a</literal>.  For example:
694 <programlisting>
695   ghci> (reverse []) :: String
696   ""
697   ghci> (reverse []) :: [Int]
698   []
699 </programlisting>
700     However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting
701     rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows.  The
702     standard rules take each group of constraints <literal>(C1 a, C2 a, ..., Cn
703     a)</literal> for each type variable <literal>a</literal>, and defaults the
704     type variable if 
705     <orderedlist>
706         <listitem>
707             <para>
708                 The type variable <literal>a</literal> appears in no
709                 other constraints
710             </para>
711         </listitem>
712         <listitem>
713             <para>
714                 All the classes <literal>Ci</literal> are standard.
715             </para>
716         </listitem>
717         <listitem>
718             <para>
719                 At least one of the classes <literal>Ci</literal> is
720                 numeric.
721             </para>
722         </listitem>
723     </orderedlist>
724     At the GHCi prompt, or with GHC if the
725     <literal>-fextended-default-rules</literal> flag is given,
726     the following additional differences apply:
727     <itemizedlist>
728         <listitem>
729             <para>
730                 Rule 2 above is relaxed thus:
731                 <emphasis>All</emphasis> of the classes
732                 <literal>Ci</literal> are single-parameter type classes.
733             </para>
734         </listitem>
735         <listitem>
736             <para>
737                 Rule 3 above is relaxed this:
738                 At least one of the classes <literal>Ci</literal> is
739                 numeric, <emphasis>or is <literal>Show</literal>,
740                 <literal>Eq</literal>, or
741                 <literal>Ord</literal></emphasis>.
742             </para>
743         </listitem>
744         <listitem>
745             <para>
746                 The unit type <literal>()</literal> is added to the
747                 start of the standard list of types which are tried when
748                 doing type defaulting.
749             </para>
750         </listitem>
751     </itemizedlist>
752     The last point means that, for example, this program:
753 <programlisting>
754 main :: IO ()
755 main = print def
756
757 instance Num ()
758
759 def :: (Num a, Enum a) => a
760 def = toEnum 0
761 </programlisting>
762     prints <literal>()</literal> rather than <literal>0</literal> as the
763     type is defaulted to <literal>()</literal> rather than
764     <literal>Integer</literal>.
765    </para>
766    <para>
767     The motivation for the change is that it means <literal>IO a</literal>
768     actions default to <literal>IO ()</literal>, which in turn means that
769     ghci won't try to print a result when running them. This is
770     particularly important for <literal>printf</literal>, which has an
771     instance that returns <literal>IO a</literal>.
772     However, it is only able to return
773     <literal>undefined</literal>
774     (the reason for the instance having this type is to not require
775     extensions to the class system), so if the type defaults to
776     <literal>Integer</literal> then ghci gives an error when running a
777     printf.
778    </para>
779     </sect2>
780   </sect1>
781
782   <sect1 id="ghci-debugger">
783     <title>The GHCi Debugger</title>
784     <indexterm><primary>debugger</primary><secondary>in GHCi</secondary>
785     </indexterm>
786
787     <para>GHCi contains a simple imperative-style debugger in which you can
788       stop a running computation in order to examine the values of
789       variables.  The debugger is integrated into GHCi, and is turned on by
790       default: no flags are required to enable the debugging facilities.  There
791       is one major restriction: breakpoints and single-stepping are only
792       available in <emphasis>interpreted</emphasis> modules; compiled code is
793       invisible to the debugger.</para>
794
795     <para>The debugger provides the following:
796     <itemizedlist>
797         <listitem>
798           <para>The abilty to set a <firstterm>breakpoint</firstterm> on a
799             function definition or expression in the program.  When the function
800             is called, or the expression evaluated, GHCi suspends 
801             execution and returns to the prompt, where you can inspect the
802             values of local variables before continuing with the
803             execution.</para>
804         </listitem>
805         <listitem>
806           <para>Execution can be <firstterm>single-stepped</firstterm>: the
807             evaluator will suspend execution approximately after every
808             reduction, allowing local variables to be inspected.  This is
809             equivalent to setting a breakpoint at every point in the
810             program.</para>
811         </listitem>
812         <listitem>
813           <para>Execution can take place in <firstterm>tracing
814               mode</firstterm>, in which the evaluator remembers each
815             evaluation step as it happens, but doesn't suspend execution until
816             an actual breakpoint is reached.  When this happens, the history of
817             evaluation steps can be inspected.</para>
818         </listitem>
819         <listitem>
820           <para>Exceptions (e.g. pattern matching failure and
821             <literal>error</literal>) can be treated as breakpoints, to help
822             locate the source of an exception in the program.</para>
823         </listitem>
824       </itemizedlist>
825     </para>
826       
827     <para>There is currently no support for obtaining a &ldquo;stack
828       trace&rdquo;, but the tracing and history features provide a useful
829       second-best, which will often be enough to establish the context of an
830       error.</para>
831       
832     <sect2 id="breakpoints">
833       <title>Breakpoints and inspecting variables</title>
834       
835       <para>Let's use quicksort as a running example.  Here's the code:</para>
836
837 <programlisting>
838 qsort [] = [] 
839 qsort (a:as) = qsort left ++ [a] ++ qsort right
840   where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
841
842 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
843 </programlisting>
844
845       <para>First, load the module into GHCi:</para>
846
847 <screen>
848 Prelude> :l qsort.hs
849 [1 of 1] Compiling Main             ( qsort.hs, interpreted )
850 Ok, modules loaded: Main.
851 *Main>
852       </screen>       
853
854       <para>Now, let's set a breakpoint on the right-hand-side of the second
855         equation of qsort:</para>
856
857 <programlisting>
858 *Main> :break 2
859 Breakpoint 0 activated at qsort.hs:2:15-46
860 *Main>
861 </programlisting>
862       
863       <para>The command <literal>:break 2</literal> sets a breakpoint on line
864         2 of the most recently-loaded module, in this case
865         <literal>qsort.hs</literal>.   Specifically, it picks the
866         leftmost complete subexpression on that line on which to set the
867         breakpoint, which in this case is the expression 
868         <literal>(qsort left ++ [a] ++ qsort right)</literal>.</para>
869
870       <para>Now, we run the program:</para>
871
872 <programlisting>
873 *Main> main
874 Stopped at qsort.hs:2:15-46
875 _result :: [a]
876 a :: a
877 left :: [a]
878 right :: [a]
879 [qsort.hs:2:15-46] *Main>
880 </programlisting>
881
882       <para>Execution has stopped at the breakpoint.  The prompt has changed to
883         indicate that we are currently stopped at a breakpoint, and the location:
884         <literal>[qsort.hs:2:15-46]</literal>.  To further clarify the
885         location, we can use the <literal>:list</literal> command:</para>
886
887 <programlisting>
888 [qsort.hs:2:15-46] *Main> :list 
889 1  qsort [] = [] 
890 2  qsort (a:as) = qsort left ++ [a] ++ qsort right
891 3    where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
892 </programlisting>
893
894       <para>The <literal>:list</literal> command lists the source code around
895         the current breakpoint.  If your output device supports it, then GHCi
896         will highlight the active subexpression in bold.</para>
897
898       <para>GHCi has provided bindings for the free variables<footnote><para>We
899             originally provided bindings for all variables in scope, rather
900             than just
901             the free variables of the expression, but found that this affected
902             performance considerably, hence the current restriction to just the
903             free variables.</para>
904         </footnote> of the expression
905         on which the
906         breakpoint was placed (<literal>a</literal>, <literal>left</literal>,
907         <literal>right</literal>), and additionally a binding for the result of
908         the expression (<literal>_result</literal>).  These variables are just
909         like other variables that you might define in GHCi; you
910         can use them in expressions that you type at the prompt, you can ask
911         for their types with <literal>:type</literal>, and so on.  There is one
912         important difference though: these variables may only have partial
913         types.  For example, if we try to display the value of
914         <literal>left</literal>:</para>
915
916 <screen>
917 [qsort.hs:2:15-46] *Main> left
918
919 &lt;interactive&gt;:1:0:
920     Ambiguous type variable `a' in the constraint:
921       `Show a' arising from a use of `print' at &lt;interactive&gt;:1:0-3
922     Cannot resolve unknown runtime types: a
923     Use :print or :force to determine these types
924 </screen>
925
926       <para>This is because <literal>qsort</literal> is a polymorphic function,
927         and because GHCi does not carry type information at runtime, it cannot
928         determine the runtime types of free variables that involve type
929         variables.  Hence, when you ask to display <literal>left</literal> at
930         the prompt, GHCi can't figure out which instance of
931         <literal>Show</literal> to use, so it emits the type error above.</para>
932
933       <para>Fortunately, the debugger includes a generic printing command,
934         <literal>:print</literal>, which can inspect the actual runtime value of a
935         variable and attempt to reconstruct its type.  If we try it on
936         <literal>left</literal>:</para>
937
938 <screen>
939 [qsort.hs:2:15-46] *Main> :print left
940 left = (_t1::[a])
941 </screen>
942
943       <para>This isn't particularly enlightening.  What happened is that
944         <literal>left</literal> is bound to an unevaluated computation (a
945         suspension, or <firstterm>thunk</firstterm>), and
946         <literal>:print</literal> does not force any evaluation.  The idea is
947         that <literal>:print</literal> can be used to inspect values at a
948         breakpoint without any unfortunate side effects.  It won't force any
949         evaluation, which could cause the program to give a different answer
950         than it would normally, and hence it won't cause any exceptions to be
951         raised, infinite loops, or further breakpoints to be triggered (see
952         <xref linkend="nested-breakpoints" />).
953         Rather than forcing thunks, <literal>:print</literal>
954         binds each thunk to a fresh variable beginning with an
955         underscore, in this case
956         <literal>_t1</literal>.</para>
957
958       <para>If we aren't concerned about preserving the evaluatedness of a
959         variable, we can use <literal>:force</literal> instead of
960         <literal>:print</literal>.  The <literal>:force</literal> command
961         behaves exactly like <literal>:print</literal>, except that it forces
962         the evaluation of any thunks it encounters:</para>
963
964 <screen>
965 [qsort.hs:2:15-46] *Main> :force left
966 left = [4,0,3,1]
967 </screen>
968
969       <para>Now, since <literal>:force</literal> has inspected the runtime
970         value of <literal>left</literal>, it has reconstructed its type.  We
971         can see the results of this type reconstruction:</para>
972
973 <screen>
974 [qsort.hs:2:15-46] *Main> :show bindings
975 _result :: [Integer]
976 a :: Integer
977 left :: [Integer]
978 right :: [Integer]
979 _t1 :: [Integer]
980 </screen>
981
982       <para>Not only do we now know the type of <literal>left</literal>, but
983         all the other partial types have also been resolved.  So we can ask
984         for the value of <literal>a</literal>, for example:</para>
985
986 <screen>
987 [qsort.hs:2:15-46] *Main> a
988 8
989 </screen>
990       
991       <para>You might find it useful to use Haskell's
992         <literal>seq</literal> function to evaluate individual thunks rather
993         than evaluating the whole expression with <literal>:force</literal>.
994         For example:</para>
995
996 <screen>
997 [qsort.hs:2:15-46] *Main> :print right
998 right = (_t1::[Integer])
999 [qsort.hs:2:15-46] *Main> seq _t1 ()
1000 ()
1001 [qsort.hs:2:15-46] *Main> :print right
1002 right = 23 : (_t2::[Integer])
1003 </screen>
1004
1005       <para>We evaluated only the <literal>_t1</literal> thunk, revealing the
1006         head of the list, and the tail is another thunk now bound to
1007         <literal>_t2</literal>.  The <literal>seq</literal> function is a
1008         little inconvenient to use here, so you might want to use
1009         <literal>:def</literal> to make a nicer interface (left as an exercise
1010         for the reader!).</para>
1011
1012       <para>Finally, we can continue the current execution:</para>
1013
1014 <screen>
1015 [qsort.hs:2:15-46] *Main> :continue
1016 Stopped at qsort.hs:2:15-46
1017 _result :: [a]
1018 a :: a
1019 left :: [a]
1020 right :: [a]
1021 [qsort.hs:2:15-46] *Main> 
1022 </screen>
1023
1024       <para>The execution continued at the point it previously stopped, and has
1025         now stopped at the breakpoint for a second time.</para>
1026
1027       <sect3 id="setting-breakpoings">
1028         <title>Setting breakpoints</title>
1029
1030         <para>Breakpoints can be set in various ways.  Perhaps the easiest way to
1031           set a breakpoint is to name a top-level function:</para>
1032
1033 <screen>
1034    :break <replaceable>identifier</replaceable>
1035 </screen>
1036
1037       <para>Where <replaceable>identifier</replaceable> names any top-level
1038         function in an interpreted module currently loaded into GHCi (qualified
1039         names may be used).  The breakpoint will be set on the body of the
1040         function, when it is fully applied but before any pattern matching has
1041         taken place.</para>
1042
1043       <para>Breakpoints can also be set by line (and optionally column)
1044         number:</para>
1045
1046 <screen>
1047    :break <replaceable>line</replaceable>
1048    :break <replaceable>line</replaceable> <replaceable>column</replaceable>
1049    :break <replaceable>module</replaceable> <replaceable>line</replaceable>
1050    :break <replaceable>module</replaceable> <replaceable>line</replaceable> <replaceable>column</replaceable> 
1051 </screen>
1052
1053       <para>When a breakpoint is set on a particular line, GHCi sets the
1054         breakpoint on the
1055         leftmost subexpression that begins and ends on that line.  If two
1056         complete subexpressions start at the same 
1057         column, the longest one is picked.  If there is no complete
1058         subexpression on the line, then the leftmost expression starting on
1059         the line is picked, and failing that the rightmost expression that
1060         partially or completely covers the line.</para>
1061
1062       <para>When a breakpoint is set on a particular line and column, GHCi
1063         picks the smallest subexpression that encloses that location on which
1064         to set the breakpoint.  Note: GHC considers the TAB character to have a
1065         width of 1, wherever it occurs; in other words it counts
1066           characters, rather than columns.  This matches what some editors do,
1067           and doesn't match others.  The best advice is to avoid tab
1068           characters in your source code altogether (see
1069           <option>-fwarn-tabs</option> in <xref linkend="options-sanity"
1070             />).</para> 
1071
1072       <para>If the module is omitted, then the most recently-loaded module is
1073         used.</para>
1074
1075       <para>Not all subexpressions are potential breakpoint locations.  Single
1076         variables are typically not considered to be breakpoint locations
1077         (unless the variable is the right-hand-side of a function definition,
1078         lambda, or case alternative).  The rule of thumb is that all redexes
1079         are breakpoint locations, together with the bodies of functions,
1080         lambdas, case alternatives and binding statements.  There is normally
1081         no breakpoint on a let expression, but there will always be a
1082         breakpoint on its body, because we are usually interested in inspecting
1083         the values of the variables bound by the let.</para>
1084
1085       </sect3>
1086       <sect3>
1087         <title>Listing and deleting breakpoints</title>
1088
1089         <para>The list of breakpoints currently enabled can be displayed using
1090           <literal>:show&nbsp;breaks</literal></para>:
1091 <screen>
1092 *Main> :show breaks
1093 [0] Main qsort.hs:1:11-12
1094 [1] Main qsort.hs:2:15-46
1095 </screen>
1096
1097         <para>To delete a breakpoint, use the <literal>:delete</literal>
1098           command with the number given in the output from <literal>:show&nbsp;breaks</literal>:</para>
1099
1100 <screen>
1101 *Main> :delete 0
1102 *Main> :show breaks
1103 [1] Main qsort.hs:2:15-46
1104 </screen>        
1105
1106         <para>To delete all breakpoints at once, use <literal>:delete *</literal>.</para>
1107
1108     </sect3>
1109     </sect2>
1110
1111     <sect2 id="single-stepping">
1112       <title>Single-stepping</title>
1113
1114       <para>Single-stepping is a great way to visualise the execution of your
1115         program, and it is also a useful tool for identifying the source of a
1116         bug.  The concept is simple: single-stepping enables all the
1117         breakpoints in the program and executes until the next breakpoint is
1118         reached, at which point you can single-step again, or continue
1119         normally.  For example:</para>
1120
1121 <screen>
1122 *Main> :step main
1123 Stopped at qsort.hs:5:7-47
1124 _result :: IO ()
1125 </screen>
1126
1127       <para>The command <literal>:step
1128           <replaceable>expr</replaceable></literal> begins the evaluation of
1129         <replaceable>expr</replaceable> in single-stepping mode.  If
1130         <replaceable>expr</replaceable> is ommitted, then it single-steps from
1131         the current breakpoint.</para>
1132
1133       <para>The <literal>:list</literal> command is particularly useful when
1134         single-stepping, to see where you currently are:</para>
1135
1136 <screen>
1137 [qsort.hs:5:7-47] *Main> :list
1138 4  
1139 5  main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1140 6  
1141 [qsort.hs:5:7-47] *Main>
1142 </screen>
1143
1144       <para>In fact, GHCi provides a way to run a command when a breakpoint is
1145         hit, so we can make it automatically do
1146         <literal>:list</literal>:</para>
1147
1148 <screen>
1149 [qsort.hs:5:7-47] *Main> :set stop :list
1150 [qsort.hs:5:7-47] *Main> :step
1151 Stopped at qsort.hs:5:14-46
1152 _result :: [Integer]
1153 4  
1154 5  main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1155 6  
1156 [qsort.hs:5:14-46] *Main>
1157 </screen>
1158     </sect2>
1159
1160     <sect2 id="nested-breakpoints">
1161       <title>Nested breakpoints</title>
1162       <para>When GHCi is stopped at a breakpoint, and an expression entered at
1163         the prompt triggers a
1164         second breakpoint, the new breakpoint becomes the &ldquo;current&rdquo;
1165       one, and the old one is saved on a stack.  An arbitrary number of
1166         breakpoint contexts can be built up in this way.  For example:</para>
1167
1168 <screen>
1169 [qsort.hs:2:15-46] *Main> :st qsort [1,3]
1170 Stopped at qsort.hs:(1,0)-(3,55)
1171 _result :: [a]
1172 ... [qsort.hs:(1,0)-(3,55)] *Main>
1173 </screen>
1174
1175       <para>While stopped at the breakpoint on line 2 that we set earlier, we
1176         started a new evaluation with <literal>:step qsort [1,3]</literal>.
1177         This new evaluation stopped after one step (at the definition of
1178         <literal>qsort</literal>).  The prompt has changed, now prefixed with
1179         <literal>...</literal>, to indicate that there are saved breakpoints
1180         beyond the current one.  To see the stack of contexts, use
1181         <literal>:show context</literal>:</para>
1182
1183 <screen>
1184 ... [qsort.hs:(1,0)-(3,55)] *Main> :show context
1185 --> main
1186   Stopped at qsort.hs:2:15-46
1187 --> qsort [1,3]
1188   Stopped at qsort.hs:(1,0)-(3,55)
1189 ... [qsort.hs:(1,0)-(3,55)] *Main>
1190 </screen>
1191
1192         <para>To abandon the current evaluation, use
1193         <literal>:abandon</literal>:</para>
1194
1195 <screen>
1196 ... [qsort.hs:(1,0)-(3,55)] *Main> :abandon
1197 [qsort.hs:2:15-46] *Main> :abandon
1198 *Main>
1199 </screen>
1200     </sect2>
1201
1202     <sect2 id="ghci-debugger-result">
1203       <title>The <literal>_result</literal> variable</title>
1204       <para>When stopped at a breakpoint or single-step, GHCi binds the
1205         variable <literal>_result</literal> to the value of the currently
1206         active expression.  The value of <literal>_result</literal> is
1207         presumably not available yet, because we stopped its evaluation, but it
1208         can be forced: if the type is known and showable, then just entering
1209         <literal>_result</literal> at the prompt will show it.  However,
1210         there's one caveat to doing this: evaluating <literal>_result</literal>
1211         will be likely to trigger further breakpoints, starting with the
1212         breakpoint we are currently stopped at (if we stopped at a real
1213         breakpoint, rather than due to <literal>:step</literal>).  So it will
1214         probably be necessary to issue a <literal>:continue</literal>
1215         immediately when evaluating <literal>_result</literal>.  Alternatively,
1216         you can use <literal>:force</literal> which ignores breakpoints.</para>
1217     </sect2>
1218
1219     <sect2 id="tracing">
1220       <title>Tracing and history</title>
1221
1222       <para>A question that we often want to ask when debugging a program is
1223         &ldquo;how did I get here?&rdquo;.  Traditional imperative debuggers
1224         usually provide some kind of stack-tracing feature that lets you see
1225         the stack of active function calls (sometimes called the &ldquo;lexical
1226         call stack&rdquo;), describing a path through the code
1227         to the current location.  Unfortunately this is hard to provide in
1228         Haskell, because execution proceeds on a demand-driven basis, rather
1229         than a depth-first basis as in strict languages.  The
1230         &ldquo;stack&ldquo; in GHC's execution engine bears little
1231         resemblance to the lexical call stack.  Ideally GHCi would maintain a
1232         separate lexical call stack in addition to the dynamic call stack, and
1233         in fact this is exactly
1234         what our profiling system does (<xref linkend="profiling" />), and what
1235         some other Haskell debuggers do.  For the time being, however, GHCi
1236         doesn't maintain a lexical call stack (there are some technical
1237         challenges to be overcome).  Instead, we provide a way to backtrack from a
1238         breakpoint to previous evaluation steps: essentially this is like
1239         single-stepping backwards, and should in many cases provide enough
1240         information to answer the &ldquo;how did I get here?&rdquo;
1241         question.</para>
1242
1243       <para>To use tracing, evaluate an expression with the
1244         <literal>:trace</literal> command.  For example, if we set a breakpoint
1245         on the base case of <literal>qsort</literal>:</para>
1246
1247 <screen>
1248 *Main&gt; :list qsort
1249 1  qsort [] = [] 
1250 2  qsort (a:as) = qsort left ++ [a] ++ qsort right
1251 3    where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
1252 4  
1253 *Main&gt; :b 1
1254 Breakpoint 1 activated at qsort.hs:1:11-12
1255 *Main&gt; 
1256 </screen>
1257
1258       <para>and then run a small <literal>qsort</literal> with
1259         tracing:</para>
1260
1261 <screen>
1262 *Main> :trace qsort [3,2,1]
1263 Stopped at qsort.hs:1:11-12
1264 _result :: [a]
1265 [qsort.hs:1:11-12] *Main>
1266 </screen>
1267
1268       <para>We can now inspect the history of evaluation steps:</para>
1269
1270 <screen>
1271 [qsort.hs:1:11-12] *Main> :hist
1272 -1  : qsort.hs:3:24-38
1273 -2  : qsort.hs:3:23-55
1274 -3  : qsort.hs:(1,0)-(3,55)
1275 -4  : qsort.hs:2:15-24
1276 -5  : qsort.hs:2:15-46
1277 -6  : qsort.hs:3:24-38
1278 -7  : qsort.hs:3:23-55
1279 -8  : qsort.hs:(1,0)-(3,55)
1280 -9  : qsort.hs:2:15-24
1281 -10 : qsort.hs:2:15-46
1282 -11 : qsort.hs:3:24-38
1283 -12 : qsort.hs:3:23-55
1284 -13 : qsort.hs:(1,0)-(3,55)
1285 -14 : qsort.hs:2:15-24
1286 -15 : qsort.hs:2:15-46
1287 -16 : qsort.hs:(1,0)-(3,55)
1288 &lt;end of history&gt;
1289 </screen>
1290
1291       <para>To examine one of the steps in the history, use
1292         <literal>:back</literal>:</para>
1293
1294 <screen>
1295 [qsort.hs:1:11-12] *Main> :back
1296 Logged breakpoint at qsort.hs:3:24-38
1297 _result :: [a]
1298 as :: [a]
1299 a :: a
1300 [-1: qsort.hs:3:24-38] *Main> 
1301 </screen>
1302
1303       <para>Note that the local variables at each step in the history have been
1304         preserved, and can be examined as usual.  Also note that the prompt has
1305         changed to indicate that we're currently examining the first step in
1306         the history: <literal>-1</literal>.  The command
1307         <literal>:forward</literal> can be used to traverse forward in the
1308         history.</para>
1309
1310       <para>The <literal>:trace</literal> command can be used with or without
1311         an expression.  When used without an expression, tracing begins from
1312         the current breakpoint, just like <literal>:step</literal>.</para>
1313
1314       <para>The history is only available when
1315         using <literal>:trace</literal>; the reason for this is we found that
1316         logging each breakpoint in the history cuts performance by a factor of
1317         2 or more.  GHCi remembers the last 50 steps in the history (perhaps in
1318         the future we'll make this configurable).</para>
1319     </sect2>
1320
1321     <sect2 id="ghci-debugger-exceptions">
1322       <title>Debugging exceptions</title>
1323       <para>Another common question that comes up when debugging is
1324         &ldquo;where did this exception come from?&rdquo;.  Exceptions such as
1325         those raised by <literal>error</literal> or <literal>head []</literal>
1326         have no context information attached to them.  Finding which
1327         particular call to <literal>head</literal> in your program resulted in
1328         the error can be a painstaking process, usually involving
1329         <literal>Debug.Trace.trace</literal>, or compiling with
1330         profiling and using <literal>+RTS -xc</literal> (see <xref
1331           linkend="prof-time-options" />).</para>
1332
1333       <para>The GHCi debugger offers a way to hopefully shed some light on
1334         these errors quickly and without modifying or recompiling the source
1335         code.  One way would be to set a breakpoint on the location in the
1336         source code that throws the exception, and then use
1337         <literal>:trace</literal> and <literal>:history</literal> to establish
1338         the context.  However, <literal>head</literal> is in a library and
1339         we can't set a breakpoint on it directly.  For this reason, GHCi
1340         provides the flag <literal>-fbreak-on-exception</literal> which causes
1341         the evaluator to stop when an exception is thrown, just as it does when
1342         a breakpoint is hit.  This is only really useful in conjunction with
1343         <literal>:trace</literal>, in order to log the steps leading up to the
1344         exception.  For example:</para>
1345
1346 <screen>
1347 *Main> :set -fbreak-on-exception
1348 *Main> :trace qsort ("abc" ++ undefined)
1349 "Stopped at &lt;exception thrown&gt;
1350 _exception :: e
1351 [&lt;exception thrown&gt;] *Main&gt; :hist
1352 -1  : qsort.hs:3:24-38
1353 -2  : qsort.hs:3:23-55
1354 -3  : qsort.hs:(1,0)-(3,55)
1355 -4  : qsort.hs:2:15-24
1356 -5  : qsort.hs:2:15-46
1357 -6  : qsort.hs:(1,0)-(3,55)
1358 &lt;end of history&gt;
1359 [&lt;exception thrown&gt;] *Main&gt; :back
1360 Logged breakpoint at qsort.hs:3:24-38
1361 _result :: [a]
1362 as :: [a]
1363 a :: a
1364 [-1: qsort.hs:3:24-38] *Main&gt; :force as
1365 *** Exception: Prelude.undefined
1366 [-1: qsort.hs:3:24-38] *Main&gt; :print as
1367 as = 'b' : 'c' : (_t1::[Char])
1368 </screen>
1369
1370       <para>The exception itself is bound to a new variable,
1371         <literal>_exception</literal>.</para>
1372
1373       <para>Breaking on exceptions is particularly useful for finding out what
1374         your program was doing when it was in an infinite loop.  Just hit
1375         Control-C, and examine the history to find out what was going
1376         on.</para>
1377     </sect2>
1378
1379     <sect2><title>Example: inspecting functions</title>
1380       <para>
1381         It is possible to use the debugger to examine function values. 
1382         When we are at a breakpoint and a function is in scope, the debugger
1383         cannot show 
1384         you the source code for it; however, it is possible to get some 
1385         information by applying it to some arguments and  observing the result. 
1386       </para>
1387
1388       <para>
1389         The process is slightly complicated when the binding is polymorphic. 
1390         We show the process by means of an example.
1391         To keep things simple, we will use the well known <literal>map</literal> function:
1392 <programlisting>
1393 import Prelude hiding (map)
1394
1395 map :: (a->b) -> a -> b
1396 map f [] = []
1397 map f (x:xs) = f x : map f xs
1398 </programlisting>
1399       </para>
1400
1401       <para>
1402         We set a breakpoint on <literal>map</literal>, and call it.
1403 <screen>
1404 *Main> :break 5
1405 Breakpoint 0 activated at  map.hs:5:15-28
1406 *Main> map Just [1..5]
1407 Stopped at map.hs:(4,0)-(5,12)
1408 _result :: [b]
1409 x :: a
1410 f :: a -> b
1411 xs :: [a]
1412 </screen>
1413       GHCi tells us that, among other bindings, <literal>f</literal> is in scope. 
1414       However, its type is not fully known yet,  
1415       and thus it is not possible to apply it to any 
1416       arguments. Nevertheless, observe that the type of its first argument is the
1417       same as the type of <literal>x</literal>, and its result type is shared
1418         with <literal>_result</literal>.
1419       </para>
1420
1421       <para>
1422         As we demonstrated earlier (<xref linkend="breakpoints" />),  the
1423         debugger has some intelligence built-in to update the type of 
1424         <literal>f</literal> whenever the types of <literal>x</literal> or 
1425         <literal>_result</literal> are discovered.  So what we do in this
1426         scenario is
1427         force <literal>x</literal> a bit, in order to recover both its type 
1428       and the argument part of <literal>f</literal>.  
1429 <screen>
1430 *Main> seq x ()
1431 *Main> :print x
1432 x = 1
1433 </screen>
1434       </para>
1435       <para>
1436         We can check now that as expected, the type of <literal>x</literal>
1437         has been reconstructed, and with it the 
1438         type of <literal>f</literal> has been too:</para>
1439 <screen>
1440 *Main> :t x
1441 x :: Integer
1442 *Main> :t f
1443 f :: Integer -> b
1444 </screen>
1445       <para>
1446         From here, we can apply f to any argument of type Integer and observe
1447         the results. 
1448 <screen><![CDATA[
1449 *Main> let b = f 10
1450 *Main> :t b
1451 b :: b
1452 *Main> b
1453 <interactive>:1:0:
1454     Ambiguous type variable `b' in the constraint:
1455       `Show b' arising from a use of `print' at <interactive>:1:0
1456 *Main> :p b
1457 b = (_t2::a)
1458 *Main> seq b ()
1459 ()
1460 *Main> :t b
1461 b :: a
1462 *Main> :p b
1463 b = Just 10
1464 *Main> :t b
1465 b :: Maybe Integer
1466 *Main> :t f
1467 f :: Integer -> Maybe Integer
1468 *Main> f 20
1469 Just 20
1470 *Main> map f [1..5]
1471 [Just 1, Just 2, Just 3, Just 4, Just 5]
1472 ]]></screen>
1473       In the first application of <literal>f</literal>, we had to do 
1474       some more type reconstruction
1475       in order to recover the result type of <literal>f</literal>. 
1476       But after that, we are free to use 
1477       <literal>f</literal> normally.
1478      </para>
1479     </sect2>
1480
1481     <sect2><title>Limitations</title>
1482       <itemizedlist>
1483         <listitem>
1484           <para>When stopped at a breakpoint, if you try to evaluate a variable
1485             that is already under evaluation, the second evaluation will hang.
1486             The reason is
1487             that GHC knows the variable is under evaluation, so the new
1488             evaluation just waits for the result before continuing, but of
1489             course this isn't going to happen because the first evaluation is
1490             stopped at a breakpoint. Control-C can interrupt the hung
1491             evaluation and return to the prompt.</para>
1492           <para>The most common way this can happen is when you're evaluating a
1493             CAF (e.g. main), stop at a breakpoint, and ask for the value of the
1494             CAF at the prompt again.</para>
1495         </listitem>
1496         <listitem><para>
1497           Implicit parameters (see <xref linkend="implicit-parameters"/>) are only available 
1498           at the scope of a breakpoint if there is a explicit type signature.
1499         </para>
1500         </listitem>
1501       </itemizedlist>
1502     </sect2>
1503   </sect1>
1504
1505   <sect1 id="ghci-invocation">
1506     <title>Invoking GHCi</title>
1507     <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
1508     <indexterm><primary><option>&ndash;&ndash;interactive</option></primary></indexterm>
1509
1510     <para>GHCi is invoked with the command <literal>ghci</literal> or
1511     <literal>ghc &ndash;&ndash;interactive</literal>.  One or more modules or
1512     filenames can also be specified on the command line; this
1513     instructs GHCi to load the specified modules or filenames (and all
1514     the modules they depend on), just as if you had said
1515     <literal>:load <replaceable>modules</replaceable></literal> at the
1516     GHCi prompt (see <xref linkend="ghci-commands" />).  For example, to
1517     start GHCi and load the program whose topmost module is in the
1518     file <literal>Main.hs</literal>, we could say:</para>
1519
1520 <screen>
1521 $ ghci Main.hs
1522 </screen>
1523
1524     <para>Most of the command-line options accepted by GHC (see <xref
1525     linkend="using-ghc"/>) also make sense in interactive mode.  The ones
1526     that don't make sense are mostly obvious; for example, GHCi
1527     doesn't generate interface files, so options related to interface
1528     file generation won't have any effect.</para>
1529
1530     <sect2>
1531       <title>Packages</title>
1532       <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
1533
1534       <para>Most packages (see <xref linkend="using-packages"/>) are
1535       available without needing to specify any extra flags at all:
1536       they will be automatically loaded the first time they are
1537       needed.</para>
1538
1539       <para>For hidden packages, however, you need to request the
1540       package be loaded by using the <literal>-package</literal> flag:</para>
1541
1542 <screen>
1543 $ ghci -package readline
1544    ___         ___ _
1545   / _ \ /\  /\/ __(_)
1546  / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
1547 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
1548 \____/\/ /_/\____/|_|      Type :? for help.
1549
1550 Loading package base ... linking ... done.
1551 Loading package readline-1.0 ... linking ... done.
1552 Prelude> 
1553 </screen>
1554
1555       <para>The following command works to load new packages into a
1556       running GHCi:</para>
1557
1558 <screen>
1559 Prelude> :set -package <replaceable>name</replaceable>
1560 </screen>
1561
1562       <para>But note that doing this will cause all currently loaded
1563       modules to be unloaded, and you'll be dumped back into the
1564       <literal>Prelude</literal>.</para>
1565     </sect2>
1566
1567     <sect2>
1568       <title>Extra libraries</title>
1569       <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
1570       
1571       <para>Extra libraries may be specified on the command line using
1572       the normal <literal>-l<replaceable>lib</replaceable></literal>
1573       option.  (The term <emphasis>library</emphasis> here refers to
1574       libraries of foreign object code; for using libraries of Haskell
1575       source code, see <xref linkend="ghci-modules-filenames"/>.) For
1576       example, to load the &ldquo;m&rdquo; library:</para>
1577
1578 <screen>
1579 $ ghci -lm
1580 </screen>
1581
1582       <para>On systems with <literal>.so</literal>-style shared
1583       libraries, the actual library loaded will the
1584       <filename>lib<replaceable>lib</replaceable>.so</filename>.  GHCi
1585       searches the following places for libraries, in this order:</para>
1586
1587       <itemizedlist>
1588         <listitem>
1589           <para>Paths specified using the
1590           <literal>-L<replaceable>path</replaceable></literal>
1591           command-line option,</para>
1592         </listitem>
1593         <listitem>
1594           <para>the standard library search path for your system,
1595           which on some systems may be overridden by setting the
1596           <literal>LD_LIBRARY_PATH</literal> environment
1597           variable.</para>
1598         </listitem>
1599       </itemizedlist>
1600
1601       <para>On systems with <literal>.dll</literal>-style shared
1602       libraries, the actual library loaded will be
1603       <filename><replaceable>lib</replaceable>.dll</filename>.  Again,
1604       GHCi will signal an error if it can't find the library.</para>
1605
1606       <para>GHCi can also load plain object files
1607       (<literal>.o</literal> or <literal>.obj</literal> depending on
1608       your platform) from the command-line.  Just add the name the
1609       object file to the command line.</para>
1610
1611       <para>Ordering of <option>-l</option> options matters: a library
1612       should be mentioned <emphasis>before</emphasis> the libraries it
1613       depends on (see <xref linkend="options-linker"/>).</para>
1614     </sect2>
1615
1616   </sect1>
1617
1618   <sect1 id="ghci-commands">
1619     <title>GHCi commands</title>
1620
1621     <para>GHCi commands all begin with
1622     &lsquo;<literal>:</literal>&rsquo; and consist of a single command
1623     name followed by zero or more parameters.  The command name may be
1624     abbreviated, with ambiguities being resolved in favour of the more
1625     commonly used commands.</para>
1626
1627     <variablelist>
1628       <varlistentry>
1629         <term>
1630           <literal>:abandon</literal>
1631           <indexterm><primary><literal>:abandon</literal></primary></indexterm>
1632         </term>
1633         <listitem>
1634           <para>Abandons the current evaluation (only available when stopped at
1635           a breakpoint).</para>
1636         </listitem>
1637       </varlistentry>
1638
1639       <varlistentry>
1640         <term>
1641           <literal>:add</literal> <replaceable>module</replaceable> ...
1642           <indexterm><primary><literal>:add</literal></primary></indexterm>
1643         </term>
1644         <listitem>
1645           <para>Add <replaceable>module</replaceable>(s) to the
1646           current <firstterm>target set</firstterm>, and perform a
1647           reload.</para>
1648         </listitem>
1649       </varlistentry>
1650
1651       <varlistentry>
1652         <term>
1653           <literal>:back</literal>
1654           <indexterm><primary><literal>:back</literal></primary></indexterm>
1655         </term>
1656         <listitem>
1657           <para>Travel back one step in the history.  See <xref
1658               linkend="tracing" />.  See also:
1659             <literal>:trace</literal>, <literal>:history</literal>,
1660             <literal>:forward</literal>.</para>
1661         </listitem>
1662       </varlistentry>
1663
1664       <varlistentry>
1665         <term>
1666           <literal>:break [<replaceable>identifier</replaceable> |
1667             [<replaceable>module</replaceable>] <replaceable>line</replaceable>
1668             [<replaceable>column</replaceable>]]</literal>
1669         </term>
1670           <indexterm><primary><literal>:break</literal></primary></indexterm>
1671         <listitem>
1672           <para>Set a breakpoint on the specified function or line and
1673               column.  See <xref linkend="setting-breakpoints" />.</para>
1674         </listitem>
1675       </varlistentry>
1676
1677       <varlistentry>
1678         <term>
1679           <literal>:browse</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
1680           <indexterm><primary><literal>:browse</literal></primary></indexterm>
1681         </term>
1682         <listitem>
1683           <para>Displays the identifiers defined by the module
1684           <replaceable>module</replaceable>, which must be either
1685           loaded into GHCi or be a member of a package.  If the
1686           <literal>*</literal> symbol is placed before the module
1687           name, then <emphasis>all</emphasis> the identifiers defined
1688           in <replaceable>module</replaceable> are shown; otherwise
1689           the list is limited to the exports of
1690           <replaceable>module</replaceable>.  The
1691           <literal>*</literal>-form is only available for modules
1692           which are interpreted; for compiled modules (including
1693           modules from packages) only the non-<literal>*</literal>
1694           form of <literal>:browse</literal> is available.</para>
1695         </listitem>
1696       </varlistentry>
1697
1698       <varlistentry>
1699         <term>
1700           <literal>:cd</literal> <replaceable>dir</replaceable>
1701           <indexterm><primary><literal>:cd</literal></primary></indexterm>
1702         </term>
1703         <listitem>
1704           <para>Changes the current working directory to
1705           <replaceable>dir</replaceable>.  A
1706           &lsquo;<literal>&tilde;</literal>&rsquo; symbol at the
1707           beginning of <replaceable>dir</replaceable> will be replaced
1708           by the contents of the environment variable
1709           <literal>HOME</literal>.</para>
1710
1711           <para>NOTE: changing directories causes all currently loaded
1712           modules to be unloaded.  This is because the search path is
1713           usually expressed using relative directories, and changing
1714           the search path in the middle of a session is not
1715           supported.</para>
1716         </listitem>
1717       </varlistentry>
1718
1719       <varlistentry>
1720         <term>
1721           <literal>:continue</literal> 
1722           <indexterm><primary><literal>:continue</literal></primary></indexterm>
1723         </term>
1724         <listitem><para>Continue the current evaluation, when stopped at a
1725             breakpoint.</para>
1726         </listitem>
1727       </varlistentry>
1728
1729       <varlistentry>
1730         <term>
1731           <literal>:cmd</literal> <replaceable>expr</replaceable>
1732           <indexterm><primary><literal>:cmd</literal></primary></indexterm>
1733         </term>
1734         <listitem>
1735           <para>Executes <replaceable>expr</replaceable> as a computation of
1736             type <literal>IO String</literal>, and then executes the resulting
1737             string as a list of GHCi commands.  Multiple commands are separated
1738             by newlines.  The <literal>:cmd</literal> command is useful with
1739             <literal>:def</literal> and <literal>:set stop</literal>.</para>
1740         </listitem>
1741       </varlistentry>
1742
1743       <varlistentry>
1744         <term>
1745           <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
1746           <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
1747           <indexterm><primary><literal>:etags</literal></primary>
1748           </indexterm>
1749           <indexterm><primary><literal>:etags</literal></primary>
1750           </indexterm>
1751         </term>
1752         <listitem>
1753           <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
1754             (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>).  If
1755             no filename is specified, the defaulit <filename>tags</filename> or
1756             <filename>TAGS</filename> is
1757             used, respectively.  Tags for all the functions, constructors and
1758             types in the currently loaded modules are created.  All modules must
1759             be interpreted for these commands to work.</para>
1760           <para>See also <xref linkend="hasktags" />.</para>
1761         </listitem>
1762       </varlistentry>
1763
1764       <varlistentry>
1765         <term>
1766           <literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable>
1767           <indexterm><primary><literal>:def</literal></primary></indexterm>
1768         </term>
1769         <listitem>
1770           <para>The command <literal>:def</literal>
1771           <replaceable>name</replaceable>
1772           <replaceable>expr</replaceable> defines a new GHCi command
1773           <literal>:<replaceable>name</replaceable></literal>,
1774           implemented by the Haskell expression
1775           <replaceable>expr</replaceable>, which must have type
1776           <literal>String -> IO String</literal>.  When
1777           <literal>:<replaceable>name</replaceable>
1778           <replaceable>args</replaceable></literal> is typed at the
1779           prompt, GHCi will run the expression
1780           <literal>(<replaceable>name</replaceable>
1781           <replaceable>args</replaceable>)</literal>, take the
1782           resulting <literal>String</literal>, and feed it back into
1783           GHCi as a new sequence of commands.  Separate commands in
1784           the result must be separated by
1785           &lsquo;<literal>\n</literal>&rsquo;.</para>
1786
1787           <para>That's all a little confusing, so here's a few
1788           examples.  To start with, here's a new GHCi command which
1789           doesn't take any arguments or produce any results, it just
1790           outputs the current date &amp; time:</para>
1791
1792 <screen>
1793 Prelude> let date _ = Time.getClockTime >>= print >> return ""
1794 Prelude> :def date date
1795 Prelude> :date
1796 Fri Mar 23 15:16:40 GMT 2001
1797 </screen>
1798
1799           <para>Here's an example of a command that takes an argument.
1800           It's a re-implementation of <literal>:cd</literal>:</para>
1801
1802 <screen>
1803 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
1804 Prelude> :def mycd mycd
1805 Prelude> :mycd ..
1806 </screen>
1807
1808           <para>Or I could define a simple way to invoke
1809           &ldquo;<literal>ghc &ndash;&ndash;make Main</literal>&rdquo; in the
1810           current directory:</para>
1811
1812 <screen>
1813 Prelude> :def make (\_ -> return ":! ghc &ndash;&ndash;make Main")
1814 </screen>
1815
1816           <para>We can define a command that reads GHCi input from a
1817           file.  This might be useful for creating a set of bindings
1818           that we want to repeatedly load into the GHCi session:</para>
1819
1820 <screen>
1821 Prelude> :def . readFile
1822 Prelude> :. cmds.ghci
1823 </screen>
1824
1825           <para>Notice that we named the command
1826           <literal>:.</literal>, by analogy with the
1827           &lsquo;<literal>.</literal>&rsquo; Unix shell command that
1828           does the same thing.</para>
1829         </listitem>
1830       </varlistentry>
1831
1832       <varlistentry>
1833         <term>
1834           <literal>:delete * | <replaceable>num</replaceable> ...</literal> 
1835           <indexterm><primary><literal>:delete</literal></primary></indexterm>
1836         </term>
1837         <listitem>
1838           <para>Delete one or more breakpoints by number (use <literal>:show
1839               breaks</literal> to see the number of each breakpoint).  The
1840             <literal>*</literal> form deletes all the breakpoints.</para>
1841         </listitem>
1842       </varlistentry>
1843
1844       <varlistentry>
1845         <term>
1846           <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
1847           <indexterm><primary><literal>:edit</literal></primary></indexterm>
1848         </term>
1849         <listitem>
1850           <para>Opens an editor to edit the file
1851           <replaceable>file</replaceable>, or the most recently loaded
1852           module if <replaceable>file</replaceable> is omitted.  The
1853           editor to invoke is taken from the <literal>EDITOR</literal>
1854           environment variable, or a default editor on your system if
1855           <literal>EDITOR</literal> is not set.  You can change the
1856           editor using <literal>:set editor</literal>.</para>
1857         </listitem>
1858       </varlistentry>
1859
1860       <varlistentry>
1861         <term>
1862           <literal>:force <replaceable>identifier</replaceable> ...</literal>
1863           <indexterm><primary><literal>:force</literal></primary></indexterm>
1864         </term>
1865         <listitem>
1866           <para>Prints the value of <replaceable>identifier</replaceable> in
1867             the same way as <literal>:print</literal>.   Unlike
1868             <literal>:print</literal>, <literal>:force</literal> evaluates each
1869             thunk that it encounters while traversing the value.  This may
1870             cause exceptions or infinite loops, or further breakpoints (which
1871             are ignored, but displayed).</para>
1872         </listitem>
1873       </varlistentry>
1874
1875       <varlistentry>
1876         <term>
1877           <literal>:forward</literal>
1878           <indexterm><primary><literal>:forward</literal></primary></indexterm>
1879         </term>
1880         <listitem>
1881           <para>Move forward in the history.   See <xref
1882               linkend="tracing" />.  See also:
1883             <literal>:trace</literal>, <literal>:history</literal>,
1884             <literal>:back</literal>.</para>
1885         </listitem>
1886       </varlistentry>
1887
1888       <varlistentry>
1889         <term>
1890           <literal>:help</literal>
1891           <indexterm><primary><literal>:help</literal></primary></indexterm>
1892         </term>
1893         <term>
1894           <literal>:?</literal>
1895           <indexterm><primary><literal>:?</literal></primary></indexterm>
1896         </term>
1897         <listitem>
1898           <para>Displays a list of the available commands.</para>
1899         </listitem>
1900       </varlistentry>
1901
1902       <varlistentry>
1903         <term>
1904           <literal>:history [<replaceable>num</replaceable>]</literal>
1905           <indexterm><primary><literal>:history</literal></primary></indexterm>
1906         </term>
1907         <listitem>
1908           <para>Display the history of evaluation steps.  With a number,
1909             displays that many steps (default: 20).  For use with
1910             <literal>:trace</literal>; see <xref
1911               linkend="tracing" />.</para>
1912         </listitem>
1913       </varlistentry>
1914
1915       <varlistentry>
1916         <term>
1917           <literal>:info</literal> <replaceable>name</replaceable> ...
1918           <indexterm><primary><literal>:info</literal></primary></indexterm>
1919         </term>
1920         <listitem>
1921           <para>Displays information about the given name(s).  For
1922           example, if <replaceable>name</replaceable> is a class, then
1923           the class methods and their types will be printed;  if
1924           <replaceable>name</replaceable> is a type constructor, then
1925           its definition will be printed;  if
1926           <replaceable>name</replaceable> is a function, then its type
1927           will be printed.  If <replaceable>name</replaceable> has
1928           been loaded from a source file, then GHCi will also display
1929           the location of its definition in the source.</para>
1930         </listitem>
1931       </varlistentry>
1932
1933       <varlistentry>
1934         <term>
1935           <literal>:kind</literal> <replaceable>type</replaceable>
1936           <indexterm><primary><literal>:kind</literal></primary></indexterm>
1937         </term>
1938         <listitem>
1939           <para>Infers and prints the kind of
1940           <replaceable>type</replaceable>. The latter can be an arbitrary
1941             type expression, including a partial application of a type constructor,
1942             such as <literal>Either Int</literal>.</para>
1943         </listitem>
1944       </varlistentry>
1945
1946       <varlistentry>
1947         <term>
1948           <literal>:load</literal> <replaceable>module</replaceable> ...
1949           <indexterm><primary><literal>:load</literal></primary></indexterm>
1950         </term>
1951         <listitem>
1952           <para>Recursively loads the specified
1953           <replaceable>module</replaceable>s, and all the modules they
1954           depend on.  Here, each <replaceable>module</replaceable>
1955           must be a module name or filename, but may not be the name
1956           of a module in a package.</para>
1957
1958           <para>All previously loaded modules, except package modules,
1959           are forgotten.  The new set of modules is known as the
1960           <firstterm>target set</firstterm>.  Note that
1961           <literal>:load</literal> can be used without any arguments
1962           to unload all the currently loaded modules and
1963           bindings.</para>
1964
1965           <para>After a <literal>:load</literal> command, the current
1966           context is set to:</para>
1967
1968           <itemizedlist>
1969             <listitem>
1970               <para><replaceable>module</replaceable>, if it was loaded
1971               successfully, or</para>
1972             </listitem>
1973             <listitem>
1974               <para>the most recently successfully loaded module, if
1975               any other modules were loaded as a result of the current
1976               <literal>:load</literal>, or</para>
1977             </listitem>
1978             <listitem>
1979               <para><literal>Prelude</literal> otherwise.</para>
1980             </listitem>
1981           </itemizedlist>
1982         </listitem>
1983       </varlistentry>
1984
1985       <varlistentry>
1986         <term>
1987           <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
1988           <indexterm><primary><literal>:main</literal></primary></indexterm>
1989         </term>
1990         <listitem>
1991           <para>
1992             When a program is compiled and executed, it can use the
1993             <literal>getArgs</literal> function to access the
1994             command-line arguments.
1995             However, we cannot simply pass the arguments to the
1996             <literal>main</literal> function while we are testing in ghci,
1997             as the <literal>main</literal> function doesn't take its
1998             directly.
1999           </para>
2000
2001           <para>
2002             Instead, we can use the <literal>:main</literal> command.
2003             This runs whatever <literal>main</literal> is in scope, with
2004             any arguments being treated the same as command-line arguments,
2005             e.g.:
2006           </para>
2007
2008 <screen>
2009 Prelude> let main = System.Environment.getArgs >>= print
2010 Prelude> :main foo bar
2011 ["foo","bar"]
2012 </screen>
2013
2014         </listitem>
2015       </varlistentry>
2016
2017       <varlistentry>
2018         <term>
2019           <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
2020           <indexterm><primary><literal>:module</literal></primary></indexterm>
2021         </term>
2022         <term>
2023           <literal>import <replaceable>mod</replaceable></literal>
2024         </term>
2025         <listitem>
2026           <para>Sets or modifies the current context for statements
2027           typed at the prompt.  The form <literal>import
2028           <replaceable>mod</replaceable></literal> is equivalent to
2029           <literal>:module +<replaceable>mod</replaceable></literal>.
2030           See <xref linkend="ghci-scope"/> for
2031           more details.</para>
2032         </listitem>
2033       </varlistentry>
2034
2035       <varlistentry>
2036         <term>
2037           <literal>:print </literal> <replaceable>names</replaceable> ...
2038           <indexterm><primary><literal>:print</literal></primary></indexterm>
2039         </term>
2040         <listitem>
2041           <para>Prints a value without forcing its evaluation.
2042             <literal>:print</literal> may be used on values whose types are
2043             unkonwn or partially known, which might be the case for local
2044             variables with polymorphic types at a breakpoint.  While inspecting
2045             the runtime value, <literal>:print</literal> attempts to
2046             reconstruct the type of the value, and will elaborate the type in
2047             GHCi's environment if possible.  If any unevaluated components
2048             (thunks) are encountered, then <literal>:print</literal> binds
2049             a fresh variable with a name beginning with <literal>_t</literal>
2050             to each thunk.  See <xref linkend="breakpoints" /> for more
2051             information.  See also the <literal>:sprint</literal> command,
2052             which works like <literal>:print</literal> but does not bind new
2053             variables.</para>
2054         </listitem>
2055       </varlistentry>
2056
2057       <varlistentry>
2058         <term>
2059           <literal>:quit</literal>
2060           <indexterm><primary><literal>:quit</literal></primary></indexterm>
2061         </term>
2062         <listitem>
2063           <para>Quits GHCi.  You can also quit by typing a control-D
2064           at the prompt.</para>
2065         </listitem>
2066       </varlistentry>
2067
2068       <varlistentry>
2069         <term>
2070           <literal>:reload</literal>
2071           <indexterm><primary><literal>:reload</literal></primary></indexterm>
2072         </term>
2073         <listitem>
2074           <para>Attempts to reload the current target set (see
2075           <literal>:load</literal>) if any of the modules in the set,
2076           or any dependent module, has changed.  Note that this may
2077           entail loading new modules, or dropping modules which are no
2078           longer indirectly required by the target.</para>
2079         </listitem>
2080       </varlistentry>
2081
2082       <varlistentry>
2083         <term>
2084           <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
2085           <indexterm><primary><literal>:set</literal></primary></indexterm>
2086         </term>
2087         <listitem>
2088           <para>Sets various options.  See <xref linkend="ghci-set"/>
2089           for a list of available options.  The
2090           <literal>:set</literal> command by itself shows which
2091           options are currently set.</para>
2092         </listitem>
2093       </varlistentry>
2094
2095       <varlistentry>
2096         <term>
2097           <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
2098           <indexterm><primary><literal>:set args</literal></primary></indexterm>
2099         </term>
2100         <listitem>
2101           <para>Sets the list of arguments which are returned when the
2102           program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
2103             </indexterm>.</para>
2104         </listitem>
2105       </varlistentry>
2106
2107       <varlistentry>
2108         <term>
2109            <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
2110         </term>
2111         <listitem>
2112           <para>Sets the command used by <literal>:edit</literal> to
2113           <replaceable>cmd</replaceable>.</para>
2114         </listitem>
2115       </varlistentry>
2116
2117       <varlistentry>
2118         <term>
2119            <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
2120            <indexterm><primary><literal>:set prog</literal></primary></indexterm>
2121         </term>
2122         <listitem>
2123           <para>Sets the string to be returned when the program calls
2124           <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
2125             </indexterm>.</para>
2126         </listitem>
2127       </varlistentry>
2128
2129       <varlistentry>
2130         <term>
2131            <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
2132         </term>
2133         <listitem>
2134           <para>Sets the string to be used as the prompt in GHCi.
2135           Inside <replaceable>prompt</replaceable>, the sequence
2136           <literal>%s</literal> is replaced by the names of the
2137           modules currently in scope, and <literal>%%</literal> is
2138           replaced by <literal>%</literal>.</para>
2139         </listitem>
2140       </varlistentry>
2141
2142       <varlistentry>
2143         <term>
2144            <literal>:set</literal> <literal>stop</literal>
2145           [<replaceable>num</replaceable>] <replaceable>cmd</replaceable>
2146         </term>
2147         <listitem>
2148           <para>Set a command to be executed when a breakpoint is hit, or a new
2149           item in the history is selected.  The most common use of
2150             <literal>:set stop</literal> is to display the source code at the
2151             current location, e.g. <literal>:set stop :list</literal>.</para>
2152
2153           <para>If a number is given before the command, then the commands are
2154             run when the specified breakpoint (only) is hit.  This can be quite
2155             useful: for example, <literal>:set stop 1 :continue</literal>
2156             effectively disables breakpoint 1, by running
2157             <literal>:continue</literal> whenever it is hit (although GHCi will
2158             still emit a message to say the breakpoint was hit).  What's more,
2159             with cunning use of <literal>:def</literal> and
2160             <literal>:cmd</literal> you can use <literal>:set stop</literal> to
2161             implement conditional breakpoints:</para>
2162 <screen>
2163 *Main> :def cond \expr -> return (":cmd if (" ++ expr ++ ") then return \"\" else return \":continue\"")
2164 *Main> :set stop 0 :cond (x &lt; 3)
2165 </screen>
2166           <para>Ignoring breakpoints for a specified number of iterations is
2167             also possible using similar techniques.</para>
2168         </listitem>
2169       </varlistentry>
2170
2171       <varlistentry>
2172         <term>
2173           <literal>:show bindings</literal>
2174           <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
2175         </term>
2176         <listitem>
2177           <para>Show the bindings made at the prompt and their
2178           types.</para>
2179         </listitem>
2180       </varlistentry>
2181
2182       <varlistentry>
2183         <term>
2184           <literal>:show breaks</literal>
2185           <indexterm><primary><literal>:show breaks</literal></primary></indexterm>
2186         </term>
2187         <listitem>
2188           <para>List the active breakpoints.</para>
2189         </listitem>
2190       </varlistentry>
2191
2192       <varlistentry>
2193         <term>
2194           <literal>:show context</literal>
2195           <indexterm><primary><literal>:show context</literal></primary></indexterm>
2196         </term>
2197         <listitem>
2198           <para>List the active evaluations that are stopped at breakpoints.</para>
2199         </listitem>
2200       </varlistentry>
2201
2202       <varlistentry>
2203         <term>
2204           <literal>:show modules</literal>
2205           <indexterm><primary><literal>:show modules</literal></primary></indexterm>
2206         </term>
2207         <listitem>
2208           <para>Show the list of modules currently load.</para>
2209         </listitem>
2210       </varlistentry>
2211
2212       <varlistentry>
2213         <term>
2214           <literal>:show [args|prog|prompt|editor|stop]</literal>
2215           <indexterm><primary><literal>:show</literal></primary></indexterm>
2216         </term>
2217         <listitem>
2218           <para>Displays the specified setting (see
2219             <literal>:set</literal>).</para>
2220         </listitem>
2221       </varlistentry>
2222
2223       <varlistentry>
2224         <term>
2225           <literal>:sprint</literal>
2226           <indexterm><primary><literal>:sprint</literal></primary></indexterm>
2227         </term>
2228         <listitem>
2229           <para>Prints a value without forcing its evaluation.
2230             <literal>:sprint</literal> is similar to <literal>:print</literal>,
2231             with the difference that unevaluated subterms are not bound to new
2232             variables, they are simply denoted by &lsquo;_&rsquo;.</para>
2233         </listitem>
2234       </varlistentry>
2235
2236       <varlistentry>
2237         <term>
2238           <literal>:step [<replaceable>expr</replaceable>]</literal> 
2239           <indexterm><primary><literal>:step</literal></primary></indexterm>
2240         </term>
2241         <listitem>
2242           <para>Single-step from the last breakpoint.  With an expression
2243             argument, begins evaluation of the expression with a
2244             single-step.</para>
2245         </listitem>
2246       </varlistentry>
2247
2248       <varlistentry>
2249         <term>
2250           <literal>:trace [<replaceable>expr</replaceable>]</literal>
2251           <indexterm><primary><literal>:trace</literal></primary></indexterm>
2252         </term>
2253         <listitem>
2254           <para>Evaluates the given expression (or from the last breakpoint if
2255             no expression is given), and additionally logs the evaluation
2256             steps for later inspection using <literal>:history</literal>.  See
2257             <xref linkend="tracing" />.</para>
2258         </listitem>
2259       </varlistentry>
2260
2261       <varlistentry>
2262         <term>
2263          <literal>:type</literal> <replaceable>expression</replaceable>
2264          <indexterm><primary><literal>:type</literal></primary></indexterm>
2265         </term>
2266         <listitem>
2267           <para>Infers and prints the type of
2268           <replaceable>expression</replaceable>, including explicit
2269           forall quantifiers for polymorphic types.  The monomorphism
2270           restriction is <emphasis>not</emphasis> applied to the
2271           expression during type inference.</para>
2272         </listitem>
2273       </varlistentry>
2274
2275       <varlistentry>
2276         <term>
2277           <literal>:undef</literal> <replaceable>name</replaceable>
2278           <indexterm><primary><literal>:undef</literal></primary></indexterm>
2279         </term>
2280         <listitem>
2281           <para>Undefines the user-defined command
2282           <replaceable>name</replaceable> (see <literal>:def</literal>
2283           above).</para>
2284         </listitem>
2285       </varlistentry>
2286
2287       <varlistentry>
2288         <term>
2289           <literal>:unset</literal> <replaceable>option</replaceable>...
2290           <indexterm><primary><literal>:unset</literal></primary></indexterm>
2291         </term>
2292         <listitem>
2293           <para>Unsets certain options.  See <xref linkend="ghci-set"/>
2294           for a list of available options.</para>
2295         </listitem>
2296       </varlistentry>
2297
2298       <varlistentry>
2299         <term>
2300           <literal>:!</literal> <replaceable>command</replaceable>...
2301           <indexterm><primary><literal>:!</literal></primary></indexterm>
2302           <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
2303         </term>
2304         <listitem>
2305           <para>Executes the shell command
2306           <replaceable>command</replaceable>.</para>
2307         </listitem>
2308       </varlistentry>
2309
2310     </variablelist>
2311   </sect1>
2312
2313   <sect1 id="ghci-set">
2314     <title>The <literal>:set</literal> command</title>
2315     <indexterm><primary><literal>:set</literal></primary></indexterm>
2316
2317     <para>The <literal>:set</literal> command sets two types of
2318     options: GHCi options, which begin with
2319     &lsquo;<literal>+</literal>&rdquo; and &ldquo;command-line&rdquo;
2320     options, which begin with &lsquo;-&rsquo;.  </para>
2321
2322     <para>NOTE: at the moment, the <literal>:set</literal> command
2323     doesn't support any kind of quoting in its arguments: quotes will
2324     not be removed and cannot be used to group words together.  For
2325     example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
2326     you expect.</para>
2327
2328     <sect2>
2329       <title>GHCi options</title>
2330       <indexterm><primary>options</primary><secondary>GHCi</secondary>
2331       </indexterm>
2332
2333       <para>GHCi options may be set using <literal>:set</literal> and
2334       unset using <literal>:unset</literal>.</para>
2335
2336       <para>The available GHCi options are:</para>
2337
2338       <variablelist>
2339         <varlistentry>
2340           <term>
2341             <literal>+r</literal>
2342             <indexterm><primary><literal>+r</literal></primary></indexterm>
2343             <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
2344             <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
2345           </term>
2346           <listitem>
2347             <para>Normally, any evaluation of top-level expressions
2348             (otherwise known as CAFs or Constant Applicative Forms) in
2349             loaded modules is retained between evaluations.  Turning
2350             on <literal>+r</literal> causes all evaluation of
2351             top-level expressions to be discarded after each
2352             evaluation (they are still retained
2353             <emphasis>during</emphasis> a single evaluation).</para>
2354           
2355             <para>This option may help if the evaluated top-level
2356             expressions are consuming large amounts of space, or if
2357             you need repeatable performance measurements.</para>
2358           </listitem>
2359         </varlistentry>
2360
2361         <varlistentry>
2362           <term>
2363             <literal>+s</literal>
2364             <indexterm><primary><literal>+s</literal></primary></indexterm>
2365           </term>
2366           <listitem>
2367             <para>Display some stats after evaluating each expression,
2368             including the elapsed time and number of bytes allocated.
2369             NOTE: the allocation figure is only accurate to the size
2370             of the storage manager's allocation area, because it is
2371             calculated at every GC.  Hence, you might see values of
2372             zero if no GC has occurred.</para>
2373           </listitem>
2374         </varlistentry>
2375
2376         <varlistentry>
2377           <term>
2378             <literal>+t</literal>
2379             <indexterm><primary><literal>+t</literal></primary></indexterm>
2380           </term>
2381           <listitem>
2382             <para>Display the type of each variable bound after a
2383             statement is entered at the prompt.  If the statement is a
2384             single expression, then the only variable binding will be
2385             for the variable
2386             &lsquo;<literal>it</literal>&rsquo;.</para>
2387           </listitem>
2388         </varlistentry>
2389       </variablelist>
2390     </sect2>
2391
2392     <sect2 id="ghci-cmd-line-options">
2393       <title>Setting GHC command-line options in GHCi</title>
2394
2395       <para>Normal GHC command-line options may also be set using
2396       <literal>:set</literal>.  For example, to turn on
2397       <option>-fglasgow-exts</option>, you would say:</para>
2398
2399 <screen>
2400 Prelude> :set -fglasgow-exts
2401 </screen>
2402       
2403       <para>Any GHC command-line option that is designated as
2404       <firstterm>dynamic</firstterm> (see the table in <xref
2405       linkend="flag-reference"/>), may be set using
2406       <literal>:set</literal>.  To unset an option, you can set the
2407       reverse option:</para>
2408       <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
2409
2410 <screen>
2411 Prelude> :set -fno-glasgow-exts
2412 </screen>
2413
2414       <para><xref linkend="flag-reference"/> lists the reverse for each
2415       option where applicable.</para>
2416
2417       <para>Certain static options (<option>-package</option>,
2418       <option>-I</option>, <option>-i</option>, and
2419       <option>-l</option> in particular) will also work, but some may
2420       not take effect until the next reload.</para>
2421       <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
2422     </sect2>
2423   </sect1>
2424   <sect1 id="ghci-dot-files">
2425     <title>The <filename>.ghci</filename> file</title>
2426     <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
2427     </indexterm>
2428     <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
2429     </indexterm>
2430
2431     <para>When it starts, GHCi always reads and executes commands from
2432     <filename>$HOME/.ghci</filename>, followed by
2433     <filename>./.ghci</filename>.</para>
2434
2435     <para>The <filename>.ghci</filename> in your home directory is
2436     most useful for turning on favourite options (eg. <literal>:set
2437     +s</literal>), and defining useful macros.  Placing a
2438     <filename>.ghci</filename> file in a directory with a Haskell
2439     project is a useful way to set certain project-wide options so you
2440     don't have to type them everytime you start GHCi: eg. if your
2441     project uses GHC extensions and CPP, and has source files in three
2442     subdirectories A B and C, you might put the following lines in
2443     <filename>.ghci</filename>:</para>
2444
2445 <screen>
2446 :set -fglasgow-exts -cpp
2447 :set -iA:B:C
2448 </screen>
2449
2450     <para>(Note that strictly speaking the <option>-i</option> flag is
2451     a static one, but in fact it works to set it using
2452     <literal>:set</literal> like this.  The changes won't take effect
2453     until the next <literal>:load</literal>, though.)</para>
2454
2455     <para>Two command-line options control whether the
2456     <filename>.ghci</filename> files are read:</para>
2457
2458     <variablelist>
2459       <varlistentry>
2460         <term>
2461           <option>-ignore-dot-ghci</option>
2462           <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
2463         </term>
2464         <listitem>
2465           <para>Don't read either <filename>./.ghci</filename> or
2466           <filename>$HOME/.ghci</filename> when starting up.</para>
2467         </listitem>
2468       </varlistentry>
2469       <varlistentry>
2470         <term>
2471           <option>-read-dot-ghci</option>
2472           <indexterm><primary><option>-read-dot-ghci</option></primary></indexterm>
2473         </term>
2474         <listitem>
2475           <para>Read <filename>.ghci</filename> and
2476           <filename>$HOME/.ghci</filename>.  This is normally the
2477           default, but the <option>-read-dot-ghci</option> option may
2478           be used to override a previous
2479           <option>-ignore-dot-ghci</option> option.</para>
2480         </listitem>
2481       </varlistentry>
2482     </variablelist>
2483
2484   </sect1>
2485
2486   <sect1 id="ghci-obj">
2487     <title>Compiling to object code inside GHCi</title>
2488
2489     <para>By default, GHCi compiles Haskell source code into byte-code
2490     that is interpreted by the runtime system.  GHCi can also compile
2491     Haskell code to object code: to turn on this feature, use the
2492     <option>-fobject-code</option> flag either on the command line or
2493     with <literal>:set</literal> (the option
2494     <option>-fbyte-code</option> restores byte-code compilation
2495     again).  Compiling to object code takes longer, but typically the
2496     code will execute 10-20 times faster than byte-code.</para>
2497
2498     <para>Compiling to object code inside GHCi is particularly useful
2499     if you are developing a compiled application, because the
2500     <literal>:reload</literal> command typically runs much faster than
2501     restarting GHC with <option>--make</option> from the command-line,
2502     because all the interface files are already cached in
2503     memory.</para>
2504
2505     <para>There are disadvantages to compiling to object-code: you
2506     can't set breakpoints in object-code modules, for example.  Only
2507     the exports of an object-code module will be visible in GHCi,
2508     rather than all top-level bindings as in interpreted
2509     modules.</para>
2510   </sect1>
2511
2512   <sect1 id="ghci-faq">
2513     <title>FAQ and Things To Watch Out For</title>
2514     
2515     <variablelist>
2516       <varlistentry>
2517         <term>The interpreter can't load modules with foreign export
2518         declarations!</term>
2519         <listitem>
2520           <para>Unfortunately not.  We haven't implemented it yet.
2521           Please compile any offending modules by hand before loading
2522           them into GHCi.</para>
2523         </listitem>
2524       </varlistentry>
2525
2526       <varlistentry>
2527         <term>
2528           <literal>-O</literal> doesn't work with GHCi!
2529           <indexterm><primary><option>-O</option></primary></indexterm>
2530          </term>
2531         <listitem>
2532           <para>For technical reasons, the bytecode compiler doesn't
2533           interact well with one of the optimisation passes, so we
2534           have disabled optimisation when using the interpreter.  This
2535           isn't a great loss: you'll get a much bigger win by
2536           compiling the bits of your code that need to go fast, rather
2537           than interpreting them with optimisation turned on.</para>
2538         </listitem>
2539       </varlistentry>
2540
2541       <varlistentry>
2542         <term>Unboxed tuples don't work with GHCi</term>
2543         <listitem>
2544           <para>That's right.  You can always compile a module that
2545           uses unboxed tuples and load it into GHCi, however.
2546           (Incidentally the previous point, namely that
2547           <literal>-O</literal> is incompatible with GHCi, is because
2548           the bytecode compiler can't deal with unboxed
2549           tuples).</para>
2550         </listitem>
2551       </varlistentry>
2552
2553       <varlistentry>
2554         <term>Concurrent threads don't carry on running when GHCi is
2555         waiting for input.</term>
2556         <listitem>
2557           <para>This should work, as long as your GHCi was built with
2558           the <option>-threaded</option> switch, which is the default.
2559           Consult whoever supplied your GHCi installation.</para>
2560         </listitem>
2561       </varlistentry>
2562
2563       <varlistentry>
2564         <term>After using <literal>getContents</literal>, I can't use
2565         <literal>stdin</literal> again until I do
2566         <literal>:load</literal> or <literal>:reload</literal>.</term>
2567
2568         <listitem>
2569           <para>This is the defined behaviour of
2570           <literal>getContents</literal>: it puts the stdin Handle in
2571           a state known as <firstterm>semi-closed</firstterm>, wherein
2572           any further I/O operations on it are forbidden.  Because I/O
2573           state is retained between computations, the semi-closed
2574           state persists until the next <literal>:load</literal> or
2575           <literal>:reload</literal> command.</para>
2576
2577           <para>You can make <literal>stdin</literal> reset itself
2578           after every evaluation by giving GHCi the command
2579           <literal>:set +r</literal>.  This works because
2580           <literal>stdin</literal> is just a top-level expression that
2581           can be reverted to its unevaluated state in the same way as
2582           any other top-level expression (CAF).</para>
2583         </listitem>
2584       </varlistentry>
2585
2586       <varlistentry>
2587         <term>I can't use Control-C to interrupt computations in
2588           GHCi on Windows.</term>
2589         <listitem>
2590           <para>See <xref linkend="ghci-windows"/></para>
2591         </listitem>
2592       </varlistentry>
2593     </variablelist>
2594   </sect1>
2595
2596 </chapter>
2597
2598 <!-- Emacs stuff:
2599      ;;; Local Variables: ***
2600      ;;; mode: xml ***
2601      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
2602      ;;; End: ***
2603  -->