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