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