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