Make printing of binding results optional in GHCi, and document it
[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.</para>
18   <indexterm><primary>FFI</primary><secondary>GHCi support</secondary></indexterm>
19   <indexterm><primary>Foreign Function Interface</primary><secondary>GHCi support</secondary></indexterm>
20
21   <sect1>
22     <title>Introduction to GHCi</title>
23
24     <para>Let's start with an example GHCi session.  You can fire up
25     GHCi with the command <literal>ghci</literal>:</para>
26
27 <screen>
28 $ ghci
29    ___         ___ _
30   / _ \ /\  /\/ __(_)
31  / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
32 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
33 \____/\/ /_/\____/|_|      Type :? for help.
34
35 Loading package base ... 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.  If we follow
41     the instructions and type <literal>:?</literal> for help, we
42     get:</para>
43
44 <screen>
45  Commands available from the prompt:
46
47    &lt;stmt&gt;                      evaluate/run &lt;stmt&gt;
48    :add &lt;filename&gt; ...         add module(s) to the current target set
49    :browse [*]&lt;module&gt;         display the names defined by &lt;module&gt;
50    :cd &lt;dir&gt;                   change directory to &lt;dir&gt;
51    :def &lt;cmd&gt; &lt;expr&gt;           define a command :&lt;cmd&gt;
52    :help, :?                   display this list of commands
53    :info [&lt;name&gt; ...]          display information about the given names
54    :load &lt;filename&gt; ...        load module(s) and their dependents
55    :module [+/-] [*]&lt;mod&gt; ...  set the context for expression evaluation
56    :main [&lt;arguments&gt; ...]     run the main function with the given arguments
57    :reload                     reload the current module set
58
59    :set &lt;option&gt; ...           set options
60    :set args &lt;arg&gt; ...         set the arguments returned by System.getArgs
61    :set prog &lt;progname&gt;        set the value returned by System.getProgName
62    :set prompt &lt;prompt&gt;        set the prompt used in GHCi
63
64    :show modules               show the currently loaded modules
65    :show bindings              show the current bindings made at the prompt
66
67    :ctags [&lt;file&gt;]             create tags file for Vi (default: "tags")
68    :etags [&lt;file&gt;]             create tags file for Emacs (defauilt: "TAGS")
69    :type &lt;expr&gt;                show the type of &lt;expr&gt;
70    :kind &lt;type&gt;                show the kind of &lt;type&gt;
71    :undef &lt;cmd&gt;                undefine user-defined command :&lt;cmd&gt;
72    :unset &lt;option&gt; ...         unset options
73    :quit                       exit GHCi
74    :!&lt;command&gt;                 run the shell command &lt;command&gt;
75
76  Options for ':set' and ':unset':
77
78     +r            revert top-level expressions after each evaluation
79     +s            print timing/memory stats after each evaluation
80     +t            print type after evaluation
81     -&lt;flags&gt;      most GHC command line flags can also be set here
82                          (eg. -v2, -fglasgow-exts, etc.)
83 </screen>
84
85     <para>We'll explain most of these commands as we go along.  For
86     Hugs users: many things work the same as in Hugs, so you should be
87     able to get going straight away.</para>
88
89     <para>Haskell expressions can be typed at the prompt:</para>
90     <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
91   </indexterm>
92
93 <screen>
94 Prelude> 1+2
95 3
96 Prelude> let x = 42 in x / 9
97 4.666666666666667
98 Prelude> 
99 </screen>
100
101     <para>GHCi interprets the whole line as an expression to evaluate.
102     The expression may not span several lines - as soon as you press
103     enter, GHCi will attempt to evaluate it.</para>
104   </sect1>
105
106   <sect1>
107     <title>Loading source files</title>
108
109     <para>Suppose we have the following Haskell source code, which we
110     place in a file <filename>Main.hs</filename>:</para>
111
112 <programlisting>
113 main = print (fac 20)
114
115 fac 0 = 1
116 fac n = n * fac (n-1)
117 </programlisting>
118
119     <para>You can save <filename>Main.hs</filename> anywhere you like,
120     but if you save it somewhere other than the current
121     directory<footnote><para>If you started up GHCi from the command
122     line then GHCi's current directory is the same as the current
123     directory of the shell from which it was started.  If you started
124     GHCi from the &ldquo;Start&rdquo; menu in Windows, then the
125     current directory is probably something like
126     <filename>C:\Documents and Settings\<replaceable>user
127     name</replaceable></filename>.</para> </footnote> then we will
128     need to change to the right directory in GHCi:</para>
129
130 <screen>
131 Prelude> :cd <replaceable>dir</replaceable>
132 </screen>
133
134     <para>where <replaceable>dir</replaceable> is the directory (or
135     folder) in which you saved <filename>Main.hs</filename>.</para>
136
137     <para>To load a Haskell source file into GHCi, use the
138     <literal>:load</literal> command:</para>
139     <indexterm><primary><literal>:load</literal></primary></indexterm>
140
141 <screen>
142 Prelude> :load Main
143 Compiling Main             ( Main.hs, interpreted )
144 Ok, modules loaded: Main.
145 *Main>
146 </screen>
147
148     <para>GHCi has loaded the <literal>Main</literal> module, and the
149     prompt has changed to &ldquo;<literal>*Main></literal>&rdquo; to
150     indicate that the current context for expressions typed at the
151     prompt is the <literal>Main</literal> module we just loaded (we'll
152     explain what the <literal>*</literal> means later in <xref
153     linkend="ghci-scope"/>).  So we can now type expressions involving
154     the functions from <filename>Main.hs</filename>:</para>
155
156 <screen>
157 *Main> fac 17
158 355687428096000
159 </screen>
160
161     <para>Loading a multi-module program is just as straightforward;
162     just give the name of the &ldquo;topmost&rdquo; module to the
163     <literal>:load</literal> command (hint: <literal>:load</literal>
164     can be abbreviated to <literal>:l</literal>).  The topmost module
165     will normally be <literal>Main</literal>, but it doesn't have to
166     be.  GHCi will discover which modules are required, directly or
167     indirectly, by the topmost module, and load them all in dependency
168     order.</para>
169
170     <sect2 id="ghci-modules-filenames">
171       <title>Modules vs. filenames</title>
172       <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
173       <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
174       
175       <para>Question: How does GHC find the filename which contains
176       module <replaceable>M</replaceable>?  Answer: it looks for the
177       file <literal><replaceable>M</replaceable>.hs</literal>, or
178       <literal><replaceable>M</replaceable>.lhs</literal>.  This means
179       that for most modules, the module name must match the filename.
180       If it doesn't, GHCi won't be able to find it.</para>
181
182       <para>There is one exception to this general rule: when you load
183       a program with <literal>:load</literal>, or specify it when you
184       invoke <literal>ghci</literal>, you can give a filename rather
185       than a module name.  This filename is loaded if it exists, and
186       it may contain any module you like.  This is particularly
187       convenient if you have several <literal>Main</literal> modules
188       in the same directory and you can't call them all
189       <filename>Main.hs</filename>.</para>
190
191       <para>The search path for finding source files is specified with
192       the <option>-i</option> option on the GHCi command line, like
193       so:</para>
194 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
195
196       <para>or it can be set using the <literal>:set</literal> command
197       from within GHCi (see <xref
198       linkend="ghci-cmd-line-options"/>)<footnote><para>Note that in
199       GHCi, and <option>&ndash;&ndash;make</option> mode, the <option>-i</option>
200       option is used to specify the search path for
201       <emphasis>source</emphasis> files, whereas in standard
202       batch-compilation mode the <option>-i</option> option is used to
203       specify the search path for interface files, see <xref
204       linkend="search-path"/>.</para> </footnote></para>
205
206       <para>One consequence of the way that GHCi follows dependencies
207       to find modules to load is that every module must have a source
208       file.  The only exception to the rule is modules that come from
209       a package, including the <literal>Prelude</literal> and standard
210       libraries such as <literal>IO</literal> and
211       <literal>Complex</literal>.  If you attempt to load a module for
212       which GHCi can't find a source file, even if there are object
213       and interface files for the module, you'll get an error
214       message.</para>
215     </sect2>
216
217     <sect2>
218       <title>Making changes and recompilation</title>
219       <indexterm><primary><literal>:reload</literal></primary></indexterm>
220
221       <para>If you make some changes to the source code and want GHCi
222       to recompile the program, give the <literal>:reload</literal>
223       command.  The program will be recompiled as necessary, with GHCi
224       doing its best to avoid actually recompiling modules if their
225       external dependencies haven't changed.  This is the same
226       mechanism we use to avoid re-compiling modules in the batch
227       compilation setting (see <xref linkend="recomp"/>).</para>
228     </sect2>
229   </sect1>
230
231   <sect1 id="ghci-compiled">
232     <title>Loading compiled code</title>
233     <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
234
235     <para>When you load a Haskell source module into GHCi, it is
236     normally converted to byte-code and run using the interpreter.
237     However, interpreted code can also run alongside compiled code in
238     GHCi; indeed, normally when GHCi starts, it loads up a compiled
239     copy of the <literal>base</literal> package, which contains the
240     <literal>Prelude</literal>.</para>
241
242     <para>Why should we want to run compiled code?  Well, compiled
243     code is roughly 10x faster than interpreted code, but takes about
244     2x longer to produce (perhaps longer if optimisation is on).  So
245     it pays to compile the parts of a program that aren't changing
246     very often, and use the interpreter for the code being actively
247     developed.</para>
248
249     <para>When loading up source files with <literal>:load</literal>,
250     GHCi looks for any corresponding compiled object files, and will
251     use one in preference to interpreting the source if possible.  For
252     example, suppose we have a 4-module program consisting of modules
253     A, B, C, and D.  Modules B and C both import D only,
254     and A imports both B &amp; C:</para>
255 <screen>
256       A
257      / \
258     B   C
259      \ /
260       D
261 </screen>
262     <para>We can compile D, then load the whole program, like this:</para>
263 <screen>
264 Prelude> :! ghc -c D.hs
265 Prelude> :load A
266 Skipping  D                ( D.hs, D.o )
267 Compiling C                ( C.hs, interpreted )
268 Compiling B                ( B.hs, interpreted )
269 Compiling A                ( A.hs, interpreted )
270 Ok, modules loaded: A, B, C, D.
271 *Main>
272 </screen>
273
274     <para>In the messages from the compiler, we see that it skipped D,
275     and used the object file <filename>D.o</filename>.  The message
276     <literal>Skipping</literal> <replaceable>module</replaceable>
277     indicates that compilation for <replaceable>module</replaceable>
278     isn't necessary, because the source and everything it depends on
279     is unchanged since the last compilation.</para>
280
281     <para>At any time you can use the command 
282     <literal>:show modules</literal>
283     to get a list of the modules currently loaded
284     into GHCi:</para>
285
286 <screen>
287 *Main> :show modules
288 D                ( D.hs, D.o )
289 C                ( C.hs, interpreted )
290 B                ( B.hs, interpreted )
291 A                ( A.hs, interpreted )
292 *Main></screen>
293
294     <para>If we now modify the source of D (or pretend to: using Unix
295     command <literal>touch</literal> on the source file is handy for
296     this), the compiler will no longer be able to use the object file,
297     because it might be out of date:</para>
298
299 <screen>
300 *Main> :! touch D.hs
301 *Main> :reload
302 Compiling D                ( D.hs, interpreted )
303 Skipping  C                ( C.hs, interpreted )
304 Skipping  B                ( B.hs, interpreted )
305 Skipping  A                ( A.hs, interpreted )
306 Ok, modules loaded: A, B, C, D.
307 *Main> 
308 </screen>
309
310     <para>Note that module D was compiled, but in this instance
311     because its source hadn't really changed, its interface remained
312     the same, and the recompilation checker determined that A, B and C
313     didn't need to be recompiled.</para>
314
315     <para>So let's try compiling one of the other modules:</para>
316
317 <screen>
318 *Main> :! ghc -c C.hs
319 *Main> :load A
320 Compiling D                ( D.hs, interpreted )
321 Compiling C                ( C.hs, interpreted )
322 Compiling B                ( B.hs, interpreted )
323 Compiling A                ( A.hs, interpreted )
324 Ok, modules loaded: A, B, C, D.
325 </screen>
326
327     <para>We didn't get the compiled version of C!  What happened?
328     Well, in GHCi a compiled module may only depend on other compiled
329     modules, and in this case C depends on D, which doesn't have an
330     object file, so GHCi also rejected C's object file.  Ok, so let's
331     also compile D:</para>
332
333 <screen>
334 *Main> :! ghc -c D.hs
335 *Main> :reload
336 Ok, modules loaded: A, B, C, D.
337 </screen>
338
339     <para>Nothing happened!  Here's another lesson: newly compiled
340     modules aren't picked up by <literal>:reload</literal>, only
341     <literal>:load</literal>:</para>
342
343 <screen>
344 *Main> :load A
345 Skipping  D                ( D.hs, D.o )
346 Skipping  C                ( C.hs, C.o )
347 Compiling B                ( B.hs, interpreted )
348 Compiling A                ( A.hs, interpreted )
349 Ok, modules loaded: A, B, C, D.
350 </screen>
351
352     <para>HINT: since GHCi will only use a compiled object file if it
353     can be sure that the compiled version is up-to-date, a good technique
354     when working on a large program is to occasionally run
355     <literal>ghc &ndash;&ndash;make</literal> to compile the whole project (say
356     before you go for lunch :-), then continue working in the
357     interpreter.  As you modify code, the new modules will be
358     interpreted, but the rest of the project will remain
359     compiled.</para>
360
361   </sect1>
362
363   <sect1>
364     <title>Interactive evaluation at the prompt</title>
365
366     <para>When you type an expression at the prompt, GHCi immediately
367     evaluates and prints the result:
368 <screen>
369 Prelude> reverse "hello"
370 "olleh"
371 Prelude> 5+5
372 10
373 </screen>
374 </para>
375
376 <sect2><title>I/O actions at the prompt</title>
377
378 <para>GHCi does more than simple expression evaluation at the prompt.
379 If you type something of type <literal>IO a</literal> for some
380     <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
381     as an IO-computation.
382 <screen>
383 Prelude> "hello"
384 "hello"
385 Prelude> putStrLn "hello"
386 hello
387 </screen>
388 Furthermore, GHCi will print the result of the I/O action if (and only
389 if):
390 <itemizedlist>
391   <listitem><para>The result type is an instance of <literal>Show</literal>.</para></listitem>
392   <listitem><para>The result type is not
393   <literal>()</literal>.</para></listitem>
394 </itemizedlist>
395 For example, remembering that <literal>putStrLn :: String -> IO ()</literal>:
396 <screen>
397 Prelude> putStrLn "hello"
398 hello
399 Prelude> do { putStrLn "hello"; return "yes" }
400 hello
401 "yes"
402 </screen>
403 </para></sect2>
404
405     <sect2 id="ghci-stmts">
406       <title>Using <literal>do-</literal>notation at the prompt</title>
407       <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
408       <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
409       
410       <para>GHCi actually accepts <firstterm>statements</firstterm>
411       rather than just expressions at the prompt.  This means you can
412       bind values and functions to names, and use them in future
413       expressions or statements.</para>
414
415       <para>The syntax of a statement accepted at the GHCi prompt is
416       exactly the same as the syntax of a statement in a Haskell
417       <literal>do</literal> expression.  However, there's no monad
418       overloading here: statements typed at the prompt must be in the
419       <literal>IO</literal> monad.
420 <screen>
421 Prelude> x &lt;- return 42
422 42
423 Prelude> print x
424 42
425 Prelude>
426 </screen>
427       The statement <literal>x &lt;- return 42</literal> means
428       &ldquo;execute <literal>return 42</literal> in the
429       <literal>IO</literal> monad, and bind the result to
430       <literal>x</literal>&rdquo;.  We can then use
431       <literal>x</literal> in future statements, for example to print
432       it as we did above.</para>
433
434       <para>GHCi will print the result of a statement if and only if: 
435         <itemizedlist>
436           <listitem>
437             <para>The statement is not a binding, or it is a monadic binding 
438               (<literal>p &lt;- e</literal>) that binds exactly one
439               variable.</para>
440           </listitem>
441           <listitem>
442             <para>The variable's type is not polymorphic, is not
443               <literal>()</literal>, and is an instance of
444               <literal>Show</literal></para>
445           </listitem>
446         </itemizedlist>
447       The automatic printing of binding results can be supressed with
448       <option>:set -fno-print-bind-result</option> (this does not
449       supress printing the result of non-binding statements).
450       <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
451       You might want to do this to prevent the result of binding
452       statements from being fully evaluated by the act of printing
453       them, for example.</para>
454
455       <para>Of course, you can also bind normal non-IO expressions
456       using the <literal>let</literal>-statement:</para>
457 <screen>
458 Prelude> let x = 42
459 Prelude> x
460 42
461 Prelude>
462 </screen>
463       <para>Another important difference between the two types of binding
464       is that the monadic bind (<literal>p &lt;- e</literal>) is
465       <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
466       whereas with the <literal>let</literal> form, the expression
467       isn't evaluated immediately:</para>
468 <screen>
469 Prelude> let x = error "help!"
470 Prelude> print x
471 *** Exception: help!
472 Prelude>
473 </screen>
474
475       <para>Note that <literal>let</literal> bindings do not automatically
476         print the value bound, unlike monadic bindings.</para>
477
478       <para>Any exceptions raised during the evaluation or execution
479       of the statement are caught and printed by the GHCi command line
480       interface (for more information on exceptions, see the module
481       <literal>Control.Exception</literal> in the libraries
482       documentation).</para>
483
484       <para>Every new binding shadows any existing bindings of the
485       same name, including entities that are in scope in the current
486       module context.</para>
487
488       <para>WARNING: temporary bindings introduced at the prompt only
489       last until the next <literal>:load</literal> or
490       <literal>:reload</literal> command, at which time they will be
491       simply lost.  However, they do survive a change of context with
492       <literal>:module</literal>: the temporary bindings just move to
493       the new location.</para>
494
495       <para>HINT: To get a list of the bindings currently in scope, use the
496       <literal>:show bindings</literal> command:</para>
497
498 <screen>
499 Prelude> :show bindings
500 x :: Int
501 Prelude></screen>
502
503       <para>HINT: if you turn on the <literal>+t</literal> option,
504       GHCi will show the type of each variable bound by a statement.
505       For example:</para>
506       <indexterm><primary><literal>+t</literal></primary></indexterm>
507 <screen>
508 Prelude> :set +t
509 Prelude> let (x:xs) = [1..]
510 x :: Integer
511 xs :: [Integer]
512 </screen>
513
514     </sect2>
515
516     <sect2 id="ghci-scope">
517       <title>What's really in scope at the prompt?</title> 
518
519       <para>When you type an expression at the prompt, what
520       identifiers and types are in scope?  GHCi provides a flexible
521       way to control exactly how the context for an expression is
522       constructed.  Let's start with the simple cases; when you start
523       GHCi the prompt looks like this:</para>
524
525 <screen>Prelude></screen>
526
527       <para>Which indicates that everything from the module
528       <literal>Prelude</literal> is currently in scope.  If we now
529       load a file into GHCi, the prompt will change:</para>
530
531 <screen>
532 Prelude> :load Main.hs
533 Compiling Main             ( Main.hs, interpreted )
534 *Main>
535 </screen>
536
537       <para>The new prompt is <literal>*Main</literal>, which
538       indicates that we are typing expressions in the context of the
539       top-level of the <literal>Main</literal> module.  Everything
540       that is in scope at the top-level in the module
541       <literal>Main</literal> we just loaded is also in scope at the
542       prompt (probably including <literal>Prelude</literal>, as long
543       as <literal>Main</literal> doesn't explicitly hide it).</para>
544
545       <para>The syntax
546       <literal>*<replaceable>module</replaceable></literal> indicates
547       that it is the full top-level scope of
548       <replaceable>module</replaceable> that is contributing to the
549       scope for expressions typed at the prompt.  Without the
550       <literal>*</literal>, just the exports of the module are
551       visible.</para>
552
553       <para>We're not limited to a single module: GHCi can combine
554       scopes from multiple modules, in any mixture of
555       <literal>*</literal> and non-<literal>*</literal> forms.  GHCi
556       combines the scopes from all of these modules to form the scope
557       that is in effect at the prompt.  For technical reasons, GHCi
558       can only support the <literal>*</literal>-form for modules which
559       are interpreted, so compiled modules and package modules can
560       only contribute their exports to the current scope.</para>
561
562       <para>The scope is manipulated using the
563       <literal>:module</literal> command.  For example, if the current
564       scope is <literal>Prelude</literal>, then we can bring into
565       scope the exports from the module <literal>IO</literal> like
566       so:</para>
567
568 <screen>
569 Prelude> :module +IO
570 Prelude IO> hPutStrLn stdout "hello\n"
571 hello
572 Prelude IO>
573 </screen>
574
575       <para>(Note: <literal>:module</literal> can be shortened to
576       <literal>:m</literal>). The full syntax of the
577       <literal>:module</literal> command is:</para>
578
579 <screen>
580 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
581 </screen>
582
583       <para>Using the <literal>+</literal> form of the
584       <literal>module</literal> commands adds modules to the current
585       scope, and <literal>-</literal> removes them.  Without either
586       <literal>+</literal> or <literal>-</literal>, the current scope
587       is replaced by the set of modules specified.  Note that if you
588       use this form and leave out <literal>Prelude</literal>, GHCi
589       will assume that you really wanted the
590       <literal>Prelude</literal> and add it in for you (if you don't
591       want the <literal>Prelude</literal>, then ask to remove it with
592       <literal>:m -Prelude</literal>).</para>
593
594       <para>The scope is automatically set after a
595       <literal>:load</literal> command, to the most recently loaded
596       "target" module, in a <literal>*</literal>-form if possible.
597       For example, if you say <literal>:load foo.hs bar.hs</literal>
598       and <filename>bar.hs</filename> contains module
599       <literal>Bar</literal>, then the scope will be set to
600       <literal>*Bar</literal> if <literal>Bar</literal> is
601       interpreted, or if <literal>Bar</literal> is compiled it will be
602       set to <literal>Prelude Bar</literal> (GHCi automatically adds
603       <literal>Prelude</literal> if it isn't present and there aren't
604       any <literal>*</literal>-form modules).</para>
605
606       <para>With multiple modules in scope, especially multiple
607       <literal>*</literal>-form modules, it is likely that name
608       clashes will occur.  Haskell specifies that name clashes are
609       only reported when an ambiguous identifier is used, and GHCi
610       behaves in the same way for expressions typed at the
611       prompt.</para>
612
613       <para>
614         Hint: GHCi will tab-complete names that are in scope; for
615         example, if you run GHCi and type <literal>J&lt;tab&gt;</literal>
616         then GHCi will expand it to <literal>Just </literal>.
617       </para>
618
619       <sect3>
620         <title>Qualified names</title>
621
622         <para>To make life slightly easier, the GHCi prompt also
623         behaves as if there is an implicit <literal>import
624         qualified</literal> declaration for every module in every
625         package, and every module currently loaded into GHCi.</para>
626       </sect3>
627
628       <sect3>
629         <title>The <literal>:main</literal> command</title>
630
631         <para>
632           When a program is compiled and executed, it can use the
633           <literal>getArgs</literal> function to access the
634           command-line arguments.
635           However, we cannot simply pass the arguments to the
636           <literal>main</literal> function while we are testing in ghci,
637           as the <literal>main</literal> function doesn't take its
638           directly.
639         </para>
640
641         <para>
642           Instead, we can use the <literal>:main</literal> command.
643           This runs whatever <literal>main</literal> is in scope, with
644           any arguments being treated the same as command-line arguments,
645           e.g.:
646         </para>
647
648 <screen>
649 Prelude> let main = System.Environment.getArgs >>= print
650 Prelude> :main foo bar
651 ["foo","bar"]
652 </screen>
653
654       </sect3>
655     </sect2>
656   
657
658     <sect2>
659       <title>The <literal>it</literal> variable</title>
660       <indexterm><primary><literal>it</literal></primary>
661       </indexterm>
662       
663       <para>Whenever an expression (or a non-binding statement, to be
664       precise) is typed at the prompt, GHCi implicitly binds its value
665       to the variable <literal>it</literal>.  For example:</para>
666 <screen>
667 Prelude> 1+2
668 3
669 Prelude> it * 2
670 6
671 </screen>
672     <para>What actually happens is that GHCi typechecks the
673     expression, and if it doesn't have an <literal>IO</literal> type,
674     then it transforms it as follows: an expression
675     <replaceable>e</replaceable> turns into 
676 <screen>
677 let it = <replaceable>e</replaceable>;
678 print it
679 </screen>
680     which is then run as an IO-action.</para>
681
682     <para>Hence, the original expression must have a type which is an
683     instance of the <literal>Show</literal> class, or GHCi will
684     complain:</para>
685
686 <screen>
687 Prelude&gt; id
688
689 &lt;interactive&gt;:1:0:
690     No instance for (Show (a -&gt; a))
691       arising from use of `print' at &lt;interactive&gt;:1:0-1
692     Possible fix: add an instance declaration for (Show (a -> a))
693     In the expression: print it
694     In a 'do' expression: print it
695 </screen>
696
697     <para>The error message contains some clues as to the
698     transformation happening internally.</para>
699
700       <para>If the expression was instead of type <literal>IO a</literal> for
701       some <literal>a</literal>, then <literal>it</literal> will be
702       bound to the result of the <literal>IO</literal> computation,
703       which is of type <literal>a</literal>.  eg.:</para>
704 <screen>
705 Prelude> Time.getClockTime
706 Wed Mar 14 12:23:13 GMT 2001
707 Prelude> print it
708 Wed Mar 14 12:23:13 GMT 2001
709 </screen>
710
711       <para>The corresponding translation for an IO-typed
712       <replaceable>e</replaceable> is
713 <screen>
714 it &lt;- <replaceable>e</replaceable>
715 </screen>
716       </para>
717
718       <para>Note that <literal>it</literal> is shadowed by the new
719       value each time you evaluate a new expression, and the old value
720       of <literal>it</literal> is lost.</para>
721
722     </sect2>
723
724     <sect2 id="extended-default-rules">
725       <title>Type defaulting in GHCi</title>
726     <indexterm><primary>Type default</primary></indexterm>
727     <indexterm><primary><literal>Show</literal> class</primary></indexterm>
728       <para>
729       Consider this GHCi session:
730 <programlisting>
731   ghci> reverse []
732 </programlisting>
733       What should GHCi do?  Strictly speaking, the program is ambiguous.  <literal>show (reverse [])</literal>
734       (which is what GHCi computes here) has type <literal>Show a => a</literal> and how that displays depends 
735       on the type <literal>a</literal>.  For example:
736 <programlisting>
737   ghci> (reverse []) :: String
738   ""
739   ghci> (reverse []) :: [Int]
740   []
741 </programlisting>
742     However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting
743     rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows.  The
744     standard rules take each group of constraints <literal>(C1 a, C2 a, ..., Cn
745     a)</literal> for each type variable <literal>a</literal>, and defaults the
746     type variable if 
747         <itemizedlist>
748             <listitem><para> The type variable <literal>a</literal>
749         appears in no other constraints </para></listitem>
750             <listitem><para> All the classes <literal>Ci</literal> are standard.</para></listitem>
751             <listitem><para> At least one of the classes <literal>Ci</literal> is
752             numeric.</para></listitem>
753       </itemizedlist>
754    At the GHCi prompt, the second and third rules are relaxed as follows
755    (differences italicised):
756         <itemizedlist>
757             <listitem><para> <emphasis>All</emphasis> of the classes
758             <literal>Ci</literal> are single-parameter type classes.</para></listitem>
759             <listitem><para> At least one of the classes <literal>Ci</literal> is
760             numeric, <emphasis>or is <literal>Show</literal>, 
761                 <literal>Eq</literal>, or <literal>Ord</literal></emphasis>.</para></listitem>
762       </itemizedlist>
763    The same type-default behaviour can be enabled in an ordinary Haskell
764    module, using the flag <literal>-fextended-default-rules</literal>.
765    </para>
766     </sect2>
767   </sect1>
768
769   <sect1 id="ghci-invocation">
770     <title>Invoking GHCi</title>
771     <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
772     <indexterm><primary><option>&ndash;&ndash;interactive</option></primary></indexterm>
773
774     <para>GHCi is invoked with the command <literal>ghci</literal> or
775     <literal>ghc &ndash;&ndash;interactive</literal>.  One or more modules or
776     filenames can also be specified on the command line; this
777     instructs GHCi to load the specified modules or filenames (and all
778     the modules they depend on), just as if you had said
779     <literal>:load <replaceable>modules</replaceable></literal> at the
780     GHCi prompt (see <xref linkend="ghci-commands"/>).  For example, to
781     start GHCi and load the program whose topmost module is in the
782     file <literal>Main.hs</literal>, we could say:</para>
783
784 <screen>
785 $ ghci Main.hs
786 </screen>
787
788     <para>Most of the command-line options accepted by GHC (see <xref
789     linkend="using-ghc"/>) also make sense in interactive mode.  The ones
790     that don't make sense are mostly obvious; for example, GHCi
791     doesn't generate interface files, so options related to interface
792     file generation won't have any effect.</para>
793
794     <sect2>
795       <title>Packages</title>
796       <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
797
798       <para>Most packages (see <xref linkend="using-packages"/>) are
799       available without needing to specify any extra flags at all:
800       they will be automatically loaded the first time they are
801       needed.</para>
802
803       <para>For non-auto packages, however, you need to request the
804       package be loaded by using the <literal>-package</literal> flag:</para>
805
806 <screen>
807 $ ghci -package readline
808    ___         ___ _
809   / _ \ /\  /\/ __(_)
810  / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
811 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
812 \____/\/ /_/\____/|_|      Type :? for help.
813
814 Loading package base ... linking ... done.
815 Loading package readline-1.0 ... linking ... done.
816 Prelude> 
817 </screen>
818
819       <para>The following command works to load new packages into a
820       running GHCi:</para>
821
822 <screen>
823 Prelude> :set -package <replaceable>name</replaceable>
824 </screen>
825
826       <para>But note that doing this will cause all currently loaded
827       modules to be unloaded, and you'll be dumped back into the
828       <literal>Prelude</literal>.</para>
829     </sect2>
830
831     <sect2>
832       <title>Extra libraries</title>
833       <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
834       
835       <para>Extra libraries may be specified on the command line using
836       the normal <literal>-l<replaceable>lib</replaceable></literal>
837       option.  (The term <emphasis>library</emphasis> here refers to
838       libraries of foreign object code; for using libraries of Haskell
839       source code, see <xref linkend="ghci-modules-filenames"/>.) For
840       example, to load the &ldquo;m&rdquo; library:</para>
841
842 <screen>
843 $ ghci -lm
844 </screen>
845
846       <para>On systems with <literal>.so</literal>-style shared
847       libraries, the actual library loaded will the
848       <filename>lib<replaceable>lib</replaceable>.so</filename>.  GHCi
849       searches the following places for libraries, in this order:</para>
850
851       <itemizedlist>
852         <listitem>
853           <para>Paths specified using the
854           <literal>-L<replaceable>path</replaceable></literal>
855           command-line option,</para>
856         </listitem>
857         <listitem>
858           <para>the standard library search path for your system,
859           which on some systems may be overridden by setting the
860           <literal>LD_LIBRARY_PATH</literal> environment
861           variable.</para>
862         </listitem>
863       </itemizedlist>
864
865       <para>On systems with <literal>.dll</literal>-style shared
866       libraries, the actual library loaded will be
867       <filename><replaceable>lib</replaceable>.dll</filename>.  Again,
868       GHCi will signal an error if it can't find the library.</para>
869
870       <para>GHCi can also load plain object files
871       (<literal>.o</literal> or <literal>.obj</literal> depending on
872       your platform) from the command-line.  Just add the name the
873       object file to the command line.</para>
874
875       <para>Ordering of <option>-l</option> options matters: a library
876       should be mentioned <emphasis>before</emphasis> the libraries it
877       depends on (see <xref linkend="options-linker"/>).</para>
878     </sect2>
879
880   </sect1>
881
882   <sect1 id="ghci-commands">
883     <title>GHCi commands</title>
884
885     <para>GHCi commands all begin with
886     &lsquo;<literal>:</literal>&rsquo; and consist of a single command
887     name followed by zero or more parameters.  The command name may be
888     abbreviated, with ambiguities being resolved in favour of the more
889     commonly used commands.</para>
890
891     <variablelist>
892       <varlistentry>
893         <term>
894           <literal>:add</literal> <replaceable>module</replaceable> ...
895           <indexterm><primary><literal>:add</literal></primary></indexterm>
896         </term>
897         <listitem>
898           <para>Add <replaceable>module</replaceable>(s) to the
899           current <firstterm>target set</firstterm>, and perform a
900           reload.</para>
901         </listitem>
902       </varlistentry>
903
904       <varlistentry>
905         <term>
906           <literal>:browse</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
907           <indexterm><primary><literal>:browse</literal></primary></indexterm>
908         </term>
909         <listitem>
910           <para>Displays the identifiers defined by the module
911           <replaceable>module</replaceable>, which must be either
912           loaded into GHCi or be a member of a package.  If the
913           <literal>*</literal> symbol is placed before the module
914           name, then <emphasis>all</emphasis> the identifiers defined
915           in <replaceable>module</replaceable> are shown; otherwise
916           the list is limited to the exports of
917           <replaceable>module</replaceable>.  The
918           <literal>*</literal>-form is only available for modules
919           which are interpreted; for compiled modules (including
920           modules from packages) only the non-<literal>*</literal>
921           form of <literal>:browse</literal> is available.</para>
922         </listitem>
923       </varlistentry>
924
925       <varlistentry>
926         <term>
927           <literal>:cd</literal> <replaceable>dir</replaceable>
928           <indexterm><primary><literal>:cd</literal></primary></indexterm>
929         </term>
930         <listitem>
931           <para>Changes the current working directory to
932           <replaceable>dir</replaceable>.  A
933           &lsquo;<literal>&tilde;</literal>&rsquo; symbol at the
934           beginning of <replaceable>dir</replaceable> will be replaced
935           by the contents of the environment variable
936           <literal>HOME</literal>.</para>
937
938           <para>NOTE: changing directories causes all currently loaded
939           modules to be unloaded.  This is because the search path is
940           usually expressed using relative directories, and changing
941           the search path in the middle of a session is not
942           supported.</para>
943         </listitem>
944       </varlistentry>
945
946       <varlistentry>
947         <term>
948           <literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable>
949           <indexterm><primary><literal>:def</literal></primary></indexterm>
950         </term>
951         <listitem>
952           <para>The command <literal>:def</literal>
953           <replaceable>name</replaceable>
954           <replaceable>expr</replaceable> defines a new GHCi command
955           <literal>:<replaceable>name</replaceable></literal>,
956           implemented by the Haskell expression
957           <replaceable>expr</replaceable>, which must have type
958           <literal>String -> IO String</literal>.  When
959           <literal>:<replaceable>name</replaceable>
960           <replaceable>args</replaceable></literal> is typed at the
961           prompt, GHCi will run the expression
962           <literal>(<replaceable>name</replaceable>
963           <replaceable>args</replaceable>)</literal>, take the
964           resulting <literal>String</literal>, and feed it back into
965           GHCi as a new sequence of commands.  Separate commands in
966           the result must be separated by
967           &lsquo;<literal>\n</literal>&rsquo;.</para>
968
969           <para>That's all a little confusing, so here's a few
970           examples.  To start with, here's a new GHCi command which
971           doesn't take any arguments or produce any results, it just
972           outputs the current date &amp; time:</para>
973
974 <screen>
975 Prelude> let date _ = Time.getClockTime >>= print >> return ""
976 Prelude> :def date date
977 Prelude> :date
978 Fri Mar 23 15:16:40 GMT 2001
979 </screen>
980
981           <para>Here's an example of a command that takes an argument.
982           It's a re-implementation of <literal>:cd</literal>:</para>
983
984 <screen>
985 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
986 Prelude> :def mycd mycd
987 Prelude> :mycd ..
988 </screen>
989
990           <para>Or I could define a simple way to invoke
991           &ldquo;<literal>ghc &ndash;&ndash;make Main</literal>&rdquo; in the
992           current directory:</para>
993
994 <screen>
995 Prelude> :def make (\_ -> return ":! ghc &ndash;&ndash;make Main")
996 </screen>
997
998           <para>We can define a command that reads GHCi input from a
999           file.  This might be useful for creating a set of bindings
1000           that we want to repeatedly load into the GHCi session:</para>
1001
1002 <screen>
1003 Prelude> :def . readFile
1004 Prelude> :. cmds.ghci
1005 </screen>
1006
1007           <para>Notice that we named the command
1008           <literal>:.</literal>, by analogy with the
1009           &lsquo;<literal>.</literal>&rsquo; Unix shell command that
1010           does the same thing.</para>
1011         </listitem>
1012       </varlistentry>
1013
1014       <varlistentry>
1015         <term>
1016           <literal>:help</literal>
1017           <indexterm><primary><literal>:help</literal></primary></indexterm>
1018         </term>
1019         <term>
1020           <literal>:?</literal>
1021           <indexterm><primary><literal>:?</literal></primary></indexterm>
1022         </term>
1023         <listitem>
1024           <para>Displays a list of the available commands.</para>
1025         </listitem>
1026       </varlistentry>
1027
1028       <varlistentry>
1029         <term>
1030           <literal>:info</literal> <replaceable>name</replaceable> ...
1031           <indexterm><primary><literal>:info</literal></primary></indexterm>
1032         </term>
1033         <listitem>
1034           <para>Displays information about the given name(s).  For
1035           example, if <replaceable>name</replaceable> is a class, then
1036           the class methods and their types will be printed;  if
1037           <replaceable>name</replaceable> is a type constructor, then
1038           its definition will be printed;  if
1039           <replaceable>name</replaceable> is a function, then its type
1040           will be printed.  If <replaceable>name</replaceable> has
1041           been loaded from a source file, then GHCi will also display
1042           the location of its definition in the source.</para>
1043         </listitem>
1044       </varlistentry>
1045
1046       <varlistentry>
1047         <term>
1048           <literal>:load</literal> <replaceable>module</replaceable> ...
1049           <indexterm><primary><literal>:load</literal></primary></indexterm>
1050         </term>
1051         <listitem>
1052           <para>Recursively loads the specified
1053           <replaceable>module</replaceable>s, and all the modules they
1054           depend on.  Here, each <replaceable>module</replaceable>
1055           must be a module name or filename, but may not be the name
1056           of a module in a package.</para>
1057
1058           <para>All previously loaded modules, except package modules,
1059           are forgotten.  The new set of modules is known as the
1060           <firstterm>target set</firstterm>.  Note that
1061           <literal>:load</literal> can be used without any arguments
1062           to unload all the currently loaded modules and
1063           bindings.</para>
1064
1065           <para>After a <literal>:load</literal> command, the current
1066           context is set to:</para>
1067
1068           <itemizedlist>
1069             <listitem>
1070               <para><replaceable>module</replaceable>, if it was loaded
1071               successfully, or</para>
1072             </listitem>
1073             <listitem>
1074               <para>the most recently successfully loaded module, if
1075               any other modules were loaded as a result of the current
1076               <literal>:load</literal>, or</para>
1077             </listitem>
1078             <listitem>
1079               <para><literal>Prelude</literal> otherwise.</para>
1080             </listitem>
1081           </itemizedlist>
1082         </listitem>
1083       </varlistentry>
1084
1085       <varlistentry>
1086         <term>
1087           <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
1088           <indexterm><primary><literal>:main</literal></primary></indexterm>
1089         </term>
1090         <listitem>
1091           <para>
1092             When a program is compiled and executed, it can use the
1093             <literal>getArgs</literal> function to access the
1094             command-line arguments.
1095             However, we cannot simply pass the arguments to the
1096             <literal>main</literal> function while we are testing in ghci,
1097             as the <literal>main</literal> function doesn't take its
1098             directly.
1099           </para>
1100
1101           <para>
1102             Instead, we can use the <literal>:main</literal> command.
1103             This runs whatever <literal>main</literal> is in scope, with
1104             any arguments being treated the same as command-line arguments,
1105             e.g.:
1106           </para>
1107
1108 <screen>
1109 Prelude> let main = System.Environment.getArgs >>= print
1110 Prelude> :main foo bar
1111 ["foo","bar"]
1112 </screen>
1113
1114         </listitem>
1115       </varlistentry>
1116
1117       <varlistentry>
1118         <term>
1119           <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
1120           <indexterm><primary><literal>:module</literal></primary></indexterm>
1121         </term>
1122         <listitem>
1123           <para>Sets or modifies the current context for statements
1124           typed at the prompt.  See <xref linkend="ghci-scope"/> for
1125           more details.</para>
1126         </listitem>
1127       </varlistentry>
1128
1129       <varlistentry>
1130         <term>
1131           <literal>:quit</literal>
1132           <indexterm><primary><literal>:quit</literal></primary></indexterm>
1133         </term>
1134         <listitem>
1135           <para>Quits GHCi.  You can also quit by typing a control-D
1136           at the prompt.</para>
1137         </listitem>
1138       </varlistentry>
1139
1140       <varlistentry>
1141         <term>
1142           <literal>:reload</literal>
1143           <indexterm><primary><literal>:reload</literal></primary></indexterm>
1144         </term>
1145         <listitem>
1146           <para>Attempts to reload the current target set (see
1147           <literal>:load</literal>) if any of the modules in the set,
1148           or any dependent module, has changed.  Note that this may
1149           entail loading new modules, or dropping modules which are no
1150           longer indirectly required by the target.</para>
1151         </listitem>
1152       </varlistentry>
1153
1154       <varlistentry>
1155         <term>
1156           <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
1157           <indexterm><primary><literal>:set</literal></primary></indexterm>
1158         </term>
1159         <listitem>
1160           <para>Sets various options.  See <xref linkend="ghci-set"/>
1161           for a list of available options.  The
1162           <literal>:set</literal> command by itself shows which
1163           options are currently set.</para>
1164         </listitem>
1165       </varlistentry>
1166
1167       <varlistentry>
1168         <term>
1169           <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
1170           <indexterm><primary><literal>:set args</literal></primary></indexterm>
1171         </term>
1172         <listitem>
1173           <para>Sets the list of arguments which are returned when the
1174           program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
1175             </indexterm>.</para>
1176         </listitem>
1177       </varlistentry>
1178
1179       <varlistentry>
1180         <term>
1181            <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
1182            <indexterm><primary><literal>:set prog</literal></primary></indexterm>
1183         </term>
1184         <listitem>
1185           <para>Sets the string to be returned when the program calls
1186           <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
1187             </indexterm>.</para>
1188         </listitem>
1189       </varlistentry>
1190
1191       <varlistentry>
1192         <term>
1193            <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
1194         </term>
1195         <listitem>
1196           <para>Sets the string to be used as the prompt in GHCi.
1197           Inside <replaceable>prompt</replaceable>, the sequence
1198           <literal>%s</literal> is replaced by the names of the
1199           modules currently in scope, and <literal>%%</literal> is
1200           replaced by <literal>%</literal>.</para>
1201         </listitem>
1202       </varlistentry>
1203
1204       <varlistentry>
1205         <term>
1206           <literal>:show bindings</literal>
1207           <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
1208         </term>
1209         <listitem>
1210           <para>Show the bindings made at the prompt and their
1211           types.</para>
1212         </listitem>
1213       </varlistentry>
1214
1215       <varlistentry>
1216         <term>
1217           <literal>:show modules</literal>
1218           <indexterm><primary><literal>:show modules</literal></primary></indexterm>
1219         </term>
1220         <listitem>
1221           <para>Show the list of modules currently load.</para>
1222         </listitem>
1223       </varlistentry>
1224
1225       <varlistentry>
1226         <term>
1227           <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
1228           <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
1229           <indexterm><primary><literal>:etags</literal></primary>
1230           </indexterm>
1231           <indexterm><primary><literal>:etags</literal></primary>
1232           </indexterm>
1233         </term>
1234         <listitem>
1235           <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
1236             (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>).  If
1237             no filename is specified, the defaulit <filename>tags</filename> or
1238             <filename>TAGS</filename> is
1239             used, respectively.  Tags for all the functions, constructors and
1240             types in the currently loaded modules are created.  All modules must
1241             be interpreted for these commands to work.</para>
1242           <para>See also <xref linkend="hasktags" />.</para>
1243         </listitem>
1244       </varlistentry>
1245
1246       <varlistentry>
1247         <term>
1248          <literal>:type</literal> <replaceable>expression</replaceable>
1249          <indexterm><primary><literal>:type</literal></primary></indexterm>
1250         </term>
1251         <listitem>
1252           <para>Infers and prints the type of
1253           <replaceable>expression</replaceable>, including explicit
1254           forall quantifiers for polymorphic types.  The monomorphism
1255           restriction is <emphasis>not</emphasis> applied to the
1256           expression during type inference.</para>
1257         </listitem>
1258       </varlistentry>
1259
1260       <varlistentry>
1261         <term>
1262           <literal>:kind</literal> <replaceable>type</replaceable>
1263           <indexterm><primary><literal>:kind</literal></primary></indexterm>
1264         </term>
1265         <listitem>
1266           <para>Infers and prints the kind of
1267           <replaceable>type</replaceable>. The latter can be an arbitrary
1268             type expression, including a partial application of a type constructor,
1269             such as <literal>Either Int</literal>.</para>
1270         </listitem>
1271       </varlistentry>
1272
1273       <varlistentry>
1274         <term>
1275           <literal>:undef</literal> <replaceable>name</replaceable>
1276           <indexterm><primary><literal>:undef</literal></primary></indexterm>
1277         </term>
1278         <listitem>
1279           <para>Undefines the user-defined command
1280           <replaceable>name</replaceable> (see <literal>:def</literal>
1281           above).</para>
1282         </listitem>
1283       </varlistentry>
1284
1285       <varlistentry>
1286         <term>
1287           <literal>:unset</literal> <replaceable>option</replaceable>...
1288           <indexterm><primary><literal>:unset</literal></primary></indexterm>
1289         </term>
1290         <listitem>
1291           <para>Unsets certain options.  See <xref linkend="ghci-set"/>
1292           for a list of available options.</para>
1293         </listitem>
1294       </varlistentry>
1295
1296       <varlistentry>
1297         <term>
1298           <literal>:!</literal> <replaceable>command</replaceable>...
1299           <indexterm><primary><literal>:!</literal></primary></indexterm>
1300           <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
1301         </term>
1302         <listitem>
1303           <para>Executes the shell command
1304           <replaceable>command</replaceable>.</para>
1305         </listitem>
1306       </varlistentry>
1307
1308     </variablelist>
1309   </sect1>
1310
1311   <sect1 id="ghci-set">
1312     <title>The <literal>:set</literal> command</title>
1313     <indexterm><primary><literal>:set</literal></primary></indexterm>
1314
1315     <para>The <literal>:set</literal> command sets two types of
1316     options: GHCi options, which begin with
1317     &lsquo;<literal>+</literal>&rdquo; and &ldquo;command-line&rdquo;
1318     options, which begin with &lsquo;-&rsquo;.  </para>
1319
1320     <para>NOTE: at the moment, the <literal>:set</literal> command
1321     doesn't support any kind of quoting in its arguments: quotes will
1322     not be removed and cannot be used to group words together.  For
1323     example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
1324     you expect.</para>
1325
1326     <sect2>
1327       <title>GHCi options</title>
1328       <indexterm><primary>options</primary><secondary>GHCi</secondary>
1329       </indexterm>
1330
1331       <para>GHCi options may be set using <literal>:set</literal> and
1332       unset using <literal>:unset</literal>.</para>
1333
1334       <para>The available GHCi options are:</para>
1335
1336       <variablelist>
1337         <varlistentry>
1338           <term>
1339             <literal>+r</literal>
1340             <indexterm><primary><literal>+r</literal></primary></indexterm>
1341             <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
1342             <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
1343           </term>
1344           <listitem>
1345             <para>Normally, any evaluation of top-level expressions
1346             (otherwise known as CAFs or Constant Applicative Forms) in
1347             loaded modules is retained between evaluations.  Turning
1348             on <literal>+r</literal> causes all evaluation of
1349             top-level expressions to be discarded after each
1350             evaluation (they are still retained
1351             <emphasis>during</emphasis> a single evaluation).</para>
1352           
1353             <para>This option may help if the evaluated top-level
1354             expressions are consuming large amounts of space, or if
1355             you need repeatable performance measurements.</para>
1356           </listitem>
1357         </varlistentry>
1358
1359         <varlistentry>
1360           <term>
1361             <literal>+s</literal>
1362             <indexterm><primary><literal>+s</literal></primary></indexterm>
1363           </term>
1364           <listitem>
1365             <para>Display some stats after evaluating each expression,
1366             including the elapsed time and number of bytes allocated.
1367             NOTE: the allocation figure is only accurate to the size
1368             of the storage manager's allocation area, because it is
1369             calculated at every GC.  Hence, you might see values of
1370             zero if no GC has occurred.</para>
1371           </listitem>
1372         </varlistentry>
1373
1374         <varlistentry>
1375           <term>
1376             <literal>+t</literal>
1377             <indexterm><primary><literal>+t</literal></primary></indexterm>
1378           </term>
1379           <listitem>
1380             <para>Display the type of each variable bound after a
1381             statement is entered at the prompt.  If the statement is a
1382             single expression, then the only variable binding will be
1383             for the variable
1384             &lsquo;<literal>it</literal>&rsquo;.</para>
1385           </listitem>
1386         </varlistentry>
1387       </variablelist>
1388     </sect2>
1389
1390     <sect2 id="ghci-cmd-line-options">
1391       <title>Setting GHC command-line options in GHCi</title>
1392
1393       <para>Normal GHC command-line options may also be set using
1394       <literal>:set</literal>.  For example, to turn on
1395       <option>-fglasgow-exts</option>, you would say:</para>
1396
1397 <screen>
1398 Prelude> :set -fglasgow-exts
1399 </screen>
1400       
1401       <para>Any GHC command-line option that is designated as
1402       <firstterm>dynamic</firstterm> (see the table in <xref
1403       linkend="flag-reference"/>), may be set using
1404       <literal>:set</literal>.  To unset an option, you can set the
1405       reverse option:</para>
1406       <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
1407
1408 <screen>
1409 Prelude> :set -fno-glasgow-exts
1410 </screen>
1411
1412       <para><xref linkend="flag-reference"/> lists the reverse for each
1413       option where applicable.</para>
1414
1415       <para>Certain static options (<option>-package</option>,
1416       <option>-I</option>, <option>-i</option>, and
1417       <option>-l</option> in particular) will also work, but some may
1418       not take effect until the next reload.</para>
1419       <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
1420     </sect2>
1421   </sect1>
1422
1423   <sect1 id="ghci-dot-files">
1424     <title>The <filename>.ghci</filename> file</title>
1425     <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
1426     </indexterm>
1427     <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
1428     </indexterm>
1429
1430     <para>When it starts, GHCi always reads and executes commands from
1431     <filename>$HOME/.ghci</filename>, followed by
1432     <filename>./.ghci</filename>.</para>
1433
1434     <para>The <filename>.ghci</filename> in your home directory is
1435     most useful for turning on favourite options (eg. <literal>:set
1436     +s</literal>), and defining useful macros.  Placing a
1437     <filename>.ghci</filename> file in a directory with a Haskell
1438     project is a useful way to set certain project-wide options so you
1439     don't have to type them everytime you start GHCi: eg. if your
1440     project uses GHC extensions and CPP, and has source files in three
1441     subdirectories A B and C, you might put the following lines in
1442     <filename>.ghci</filename>:</para>
1443
1444 <screen>
1445 :set -fglasgow-exts -cpp
1446 :set -iA:B:C
1447 </screen>
1448
1449     <para>(Note that strictly speaking the <option>-i</option> flag is
1450     a static one, but in fact it works to set it using
1451     <literal>:set</literal> like this.  The changes won't take effect
1452     until the next <literal>:load</literal>, though.)</para>
1453
1454     <para>Two command-line options control whether the
1455     <filename>.ghci</filename> files are read:</para>
1456
1457     <variablelist>
1458       <varlistentry>
1459         <term>
1460           <option>-ignore-dot-ghci</option>
1461           <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
1462         </term>
1463         <listitem>
1464           <para>Don't read either <filename>./.ghci</filename> or
1465           <filename>$HOME/.ghci</filename> when starting up.</para>
1466         </listitem>
1467       </varlistentry>
1468       <varlistentry>
1469         <term>
1470           <option>-read-dot-ghci</option>
1471           <indexterm><primary><option>-read-dot-ghci</option></primary></indexterm>
1472         </term>
1473         <listitem>
1474           <para>Read <filename>.ghci</filename> and
1475           <filename>$HOME/.ghci</filename>.  This is normally the
1476           default, but the <option>-read-dot-ghci</option> option may
1477           be used to override a previous
1478           <option>-ignore-dot-ghci</option> option.</para>
1479         </listitem>
1480       </varlistentry>
1481     </variablelist>
1482
1483   </sect1>
1484
1485   <sect1>
1486     <title>FAQ and Things To Watch Out For</title>
1487     
1488     <variablelist>
1489       <varlistentry>
1490         <term>The interpreter can't load modules with foreign export
1491         declarations!</term>
1492         <listitem>
1493           <para>Unfortunately not.  We haven't implemented it yet.
1494           Please compile any offending modules by hand before loading
1495           them into GHCi.</para>
1496         </listitem>
1497       </varlistentry>
1498
1499       <varlistentry>
1500         <term>
1501           <literal>-O</literal> doesn't work with GHCi!
1502           <indexterm><primary><option>-O</option></primary></indexterm>
1503          </term>
1504         <listitem>
1505           <para>For technical reasons, the bytecode compiler doesn't
1506           interact well with one of the optimisation passes, so we
1507           have disabled optimisation when using the interpreter.  This
1508           isn't a great loss: you'll get a much bigger win by
1509           compiling the bits of your code that need to go fast, rather
1510           than interpreting them with optimisation turned on.</para>
1511         </listitem>
1512       </varlistentry>
1513
1514       <varlistentry>
1515         <term>Unboxed tuples don't work with GHCi</term>
1516         <listitem>
1517           <para>That's right.  You can always compile a module that
1518           uses unboxed tuples and load it into GHCi, however.
1519           (Incidentally the previous point, namely that
1520           <literal>-O</literal> is incompatible with GHCi, is because
1521           the bytecode compiler can't deal with unboxed
1522           tuples).</para>
1523         </listitem>
1524       </varlistentry>
1525
1526       <varlistentry>
1527         <term>Concurrent threads don't carry on running when GHCi is
1528         waiting for input.</term>
1529         <listitem>
1530           <para>No, they don't.  This is because the Haskell binding
1531           to the GNU readline library doesn't support reading from the
1532           terminal in a non-blocking way, which is required to work
1533           properly with GHC's concurrency model.</para>
1534         </listitem>
1535       </varlistentry>
1536
1537       <varlistentry>
1538         <term>After using <literal>getContents</literal>, I can't use
1539         <literal>stdin</literal> again until I do
1540         <literal>:load</literal> or <literal>:reload</literal>.</term>
1541
1542         <listitem>
1543           <para>This is the defined behaviour of
1544           <literal>getContents</literal>: it puts the stdin Handle in
1545           a state known as <firstterm>semi-closed</firstterm>, wherein
1546           any further I/O operations on it are forbidden.  Because I/O
1547           state is retained between computations, the semi-closed
1548           state persists until the next <literal>:load</literal> or
1549           <literal>:reload</literal> command.</para>
1550
1551           <para>You can make <literal>stdin</literal> reset itself
1552           after every evaluation by giving GHCi the command
1553           <literal>:set +r</literal>.  This works because
1554           <literal>stdin</literal> is just a top-level expression that
1555           can be reverted to its unevaluated state in the same way as
1556           any other top-level expression (CAF).</para>
1557         </listitem>
1558       </varlistentry>
1559
1560     </variablelist>
1561   </sect1>
1562
1563 </chapter>
1564
1565 <!-- Emacs stuff:
1566      ;;; Local Variables: ***
1567      ;;; mode: xml ***
1568      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
1569      ;;; End: ***
1570  -->