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