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