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