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