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