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