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