Make the User's Guide DocBook XML 4.2 conformant again
[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    :edit &lt;file&gt;                edit file
53    :edit                       edit last module
54    :help, :?                   display this list of commands
55    :info [&lt;name&gt; ...]          display information about the given names
56    :load &lt;filename&gt; ...        load module(s) and their dependents
57    :module [+/-] [*]&lt;mod&gt; ...  set the context for expression evaluation
58    :main [&lt;arguments&gt; ...]     run the main function with the given arguments
59    :reload                     reload the current module set
60
61    :set &lt;option&gt; ...           set options
62    :set args &lt;arg&gt; ...         set the arguments returned by System.getArgs
63    :set prog &lt;progname&gt;        set the value returned by System.getProgName
64    :set prompt &lt;prompt&gt;        set the prompt used in GHCi
65    :set editor &lt;cmd&gt;        set the command used for :edit
66
67    :show modules               show the currently loaded modules
68    :show bindings              show the current bindings made at the prompt
69
70    :ctags [&lt;file&gt;]             create tags file for Vi (default: "tags")
71    :etags [&lt;file&gt;]             create tags file for Emacs (default: "TAGS")
72    :type &lt;expr&gt;                show the type of &lt;expr&gt;
73    :kind &lt;type&gt;                show the kind of &lt;type&gt;
74    :undef &lt;cmd&gt;                undefine user-defined command :&lt;cmd&gt;
75    :unset &lt;option&gt; ...         unset options
76    :quit                       exit GHCi
77    :!&lt;command&gt;                 run the shell command &lt;command&gt;
78
79  Options for ':set' and ':unset':
80
81     +r            revert top-level expressions after each evaluation
82     +s            print timing/memory stats after each evaluation
83     +t            print type after evaluation
84     -&lt;flags&gt;      most GHC command line flags can also be set here
85                          (eg. -v2, -fglasgow-exts, etc.)
86 </screen>
87
88     <para>We'll explain most of these commands as we go along.  For
89     Hugs users: many things work the same as in Hugs, so you should be
90     able to get going straight away.</para>
91
92     <para>Haskell expressions can be typed at the prompt:</para>
93     <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
94   </indexterm>
95
96 <screen>
97 Prelude> 1+2
98 3
99 Prelude> let x = 42 in x / 9
100 4.666666666666667
101 Prelude> 
102 </screen>
103
104     <para>GHCi interprets the whole line as an expression to evaluate.
105     The expression may not span several lines - as soon as you press
106     enter, GHCi will attempt to evaluate it.</para>
107   </sect1>
108
109   <sect1>
110     <title>Loading source files</title>
111
112     <para>Suppose we have the following Haskell source code, which we
113     place in a file <filename>Main.hs</filename>:</para>
114
115 <programlisting>
116 main = print (fac 20)
117
118 fac 0 = 1
119 fac n = n * fac (n-1)
120 </programlisting>
121
122     <para>You can save <filename>Main.hs</filename> anywhere you like,
123     but if you save it somewhere other than the current
124     directory<footnote><para>If you started up GHCi from the command
125     line then GHCi's current directory is the same as the current
126     directory of the shell from which it was started.  If you started
127     GHCi from the &ldquo;Start&rdquo; menu in Windows, then the
128     current directory is probably something like
129     <filename>C:\Documents and Settings\<replaceable>user
130     name</replaceable></filename>.</para> </footnote> then we will
131     need to change to the right directory in GHCi:</para>
132
133 <screen>
134 Prelude> :cd <replaceable>dir</replaceable>
135 </screen>
136
137     <para>where <replaceable>dir</replaceable> is the directory (or
138     folder) in which you saved <filename>Main.hs</filename>.</para>
139
140     <para>To load a Haskell source file into GHCi, use the
141     <literal>:load</literal> command:</para>
142     <indexterm><primary><literal>:load</literal></primary></indexterm>
143
144 <screen>
145 Prelude> :load Main
146 Compiling Main             ( Main.hs, interpreted )
147 Ok, modules loaded: Main.
148 *Main>
149 </screen>
150
151     <para>GHCi has loaded the <literal>Main</literal> module, and the
152     prompt has changed to &ldquo;<literal>*Main></literal>&rdquo; to
153     indicate that the current context for expressions typed at the
154     prompt is the <literal>Main</literal> module we just loaded (we'll
155     explain what the <literal>*</literal> means later in <xref
156     linkend="ghci-scope"/>).  So we can now type expressions involving
157     the functions from <filename>Main.hs</filename>:</para>
158
159 <screen>
160 *Main> fac 17
161 355687428096000
162 </screen>
163
164     <para>Loading a multi-module program is just as straightforward;
165     just give the name of the &ldquo;topmost&rdquo; module to the
166     <literal>:load</literal> command (hint: <literal>:load</literal>
167     can be abbreviated to <literal>:l</literal>).  The topmost module
168     will normally be <literal>Main</literal>, but it doesn't have to
169     be.  GHCi will discover which modules are required, directly or
170     indirectly, by the topmost module, and load them all in dependency
171     order.</para>
172
173     <sect2 id="ghci-modules-filenames">
174       <title>Modules vs. filenames</title>
175       <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
176       <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
177       
178       <para>Question: How does GHC find the filename which contains
179       module <replaceable>M</replaceable>?  Answer: it looks for the
180       file <literal><replaceable>M</replaceable>.hs</literal>, or
181       <literal><replaceable>M</replaceable>.lhs</literal>.  This means
182       that for most modules, the module name must match the filename.
183       If it doesn't, GHCi won't be able to find it.</para>
184
185       <para>There is one exception to this general rule: when you load
186       a program with <literal>:load</literal>, or specify it when you
187       invoke <literal>ghci</literal>, you can give a filename rather
188       than a module name.  This filename is loaded if it exists, and
189       it may contain any module you like.  This is particularly
190       convenient if you have several <literal>Main</literal> modules
191       in the same directory and you can't call them all
192       <filename>Main.hs</filename>.</para>
193
194       <para>The search path for finding source files is specified with
195       the <option>-i</option> option on the GHCi command line, like
196       so:</para>
197 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
198
199       <para>or it can be set using the <literal>:set</literal> command
200       from within GHCi (see <xref
201       linkend="ghci-cmd-line-options"/>)<footnote><para>Note that in
202       GHCi, and <option>&ndash;&ndash;make</option> mode, the <option>-i</option>
203       option is used to specify the search path for
204       <emphasis>source</emphasis> files, whereas in standard
205       batch-compilation mode the <option>-i</option> option is used to
206       specify the search path for interface files, see <xref
207       linkend="search-path"/>.</para> </footnote></para>
208
209       <para>One consequence of the way that GHCi follows dependencies
210       to find modules to load is that every module must have a source
211       file.  The only exception to the rule is modules that come from
212       a package, including the <literal>Prelude</literal> and standard
213       libraries such as <literal>IO</literal> and
214       <literal>Complex</literal>.  If you attempt to load a module for
215       which GHCi can't find a source file, even if there are object
216       and interface files for the module, you'll get an error
217       message.</para>
218     </sect2>
219
220     <sect2>
221       <title>Making changes and recompilation</title>
222       <indexterm><primary><literal>:reload</literal></primary></indexterm>
223
224       <para>If you make some changes to the source code and want GHCi
225       to recompile the program, give the <literal>:reload</literal>
226       command.  The program will be recompiled as necessary, with GHCi
227       doing its best to avoid actually recompiling modules if their
228       external dependencies haven't changed.  This is the same
229       mechanism we use to avoid re-compiling modules in the batch
230       compilation setting (see <xref linkend="recomp"/>).</para>
231     </sect2>
232   </sect1>
233
234   <sect1 id="ghci-compiled">
235     <title>Loading compiled code</title>
236     <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
237
238     <para>When you load a Haskell source module into GHCi, it is
239     normally converted to byte-code and run using the interpreter.
240     However, interpreted code can also run alongside compiled code in
241     GHCi; indeed, normally when GHCi starts, it loads up a compiled
242     copy of the <literal>base</literal> package, which contains the
243     <literal>Prelude</literal>.</para>
244
245     <para>Why should we want to run compiled code?  Well, compiled
246     code is roughly 10x faster than interpreted code, but takes about
247     2x longer to produce (perhaps longer if optimisation is on).  So
248     it pays to compile the parts of a program that aren't changing
249     very often, and use the interpreter for the code being actively
250     developed.</para>
251
252     <para>When loading up source files with <literal>:load</literal>,
253     GHCi looks for any corresponding compiled object files, and will
254     use one in preference to interpreting the source if possible.  For
255     example, suppose we have a 4-module program consisting of modules
256     A, B, C, and D.  Modules B and C both import D only,
257     and A imports both B &amp; C:</para>
258 <screen>
259       A
260      / \
261     B   C
262      \ /
263       D
264 </screen>
265     <para>We can compile D, then load the whole program, like this:</para>
266 <screen>
267 Prelude> :! ghc -c D.hs
268 Prelude> :load A
269 Skipping  D                ( D.hs, D.o )
270 Compiling C                ( C.hs, interpreted )
271 Compiling B                ( B.hs, interpreted )
272 Compiling A                ( A.hs, interpreted )
273 Ok, modules loaded: A, B, C, D.
274 *Main>
275 </screen>
276
277     <para>In the messages from the compiler, we see that it skipped D,
278     and used the object file <filename>D.o</filename>.  The message
279     <literal>Skipping</literal> <replaceable>module</replaceable>
280     indicates that compilation for <replaceable>module</replaceable>
281     isn't necessary, because the source and everything it depends on
282     is unchanged since the last compilation.</para>
283
284     <para>At any time you can use the command 
285     <literal>:show modules</literal>
286     to get a list of the modules currently loaded
287     into GHCi:</para>
288
289 <screen>
290 *Main> :show modules
291 D                ( D.hs, D.o )
292 C                ( C.hs, interpreted )
293 B                ( B.hs, interpreted )
294 A                ( A.hs, interpreted )
295 *Main></screen>
296
297     <para>If we now modify the source of D (or pretend to: using Unix
298     command <literal>touch</literal> on the source file is handy for
299     this), the compiler will no longer be able to use the object file,
300     because it might be out of date:</para>
301
302 <screen>
303 *Main> :! touch D.hs
304 *Main> :reload
305 Compiling D                ( D.hs, interpreted )
306 Skipping  C                ( C.hs, interpreted )
307 Skipping  B                ( B.hs, interpreted )
308 Skipping  A                ( A.hs, interpreted )
309 Ok, modules loaded: A, B, C, D.
310 *Main> 
311 </screen>
312
313     <para>Note that module D was compiled, but in this instance
314     because its source hadn't really changed, its interface remained
315     the same, and the recompilation checker determined that A, B and C
316     didn't need to be recompiled.</para>
317
318     <para>So let's try compiling one of the other modules:</para>
319
320 <screen>
321 *Main> :! ghc -c C.hs
322 *Main> :load A
323 Compiling D                ( D.hs, interpreted )
324 Compiling C                ( C.hs, interpreted )
325 Compiling B                ( B.hs, interpreted )
326 Compiling A                ( A.hs, interpreted )
327 Ok, modules loaded: A, B, C, D.
328 </screen>
329
330     <para>We didn't get the compiled version of C!  What happened?
331     Well, in GHCi a compiled module may only depend on other compiled
332     modules, and in this case C depends on D, which doesn't have an
333     object file, so GHCi also rejected C's object file.  Ok, so let's
334     also compile D:</para>
335
336 <screen>
337 *Main> :! ghc -c D.hs
338 *Main> :reload
339 Ok, modules loaded: A, B, C, D.
340 </screen>
341
342     <para>Nothing happened!  Here's another lesson: newly compiled
343     modules aren't picked up by <literal>:reload</literal>, only
344     <literal>:load</literal>:</para>
345
346 <screen>
347 *Main> :load A
348 Skipping  D                ( D.hs, D.o )
349 Skipping  C                ( C.hs, C.o )
350 Compiling B                ( B.hs, interpreted )
351 Compiling A                ( A.hs, interpreted )
352 Ok, modules loaded: A, B, C, D.
353 </screen>
354
355     <para>HINT: since GHCi will only use a compiled object file if it
356     can be sure that the compiled version is up-to-date, a good technique
357     when working on a large program is to occasionally run
358     <literal>ghc &ndash;&ndash;make</literal> to compile the whole project (say
359     before you go for lunch :-), then continue working in the
360     interpreter.  As you modify code, the new modules will be
361     interpreted, but the rest of the project will remain
362     compiled.</para>
363
364   </sect1>
365
366   <sect1>
367     <title>Interactive evaluation at the prompt</title>
368
369     <para>When you type an expression at the prompt, GHCi immediately
370     evaluates and prints the result:
371 <screen>
372 Prelude> reverse "hello"
373 "olleh"
374 Prelude> 5+5
375 10
376 </screen>
377 </para>
378
379 <sect2><title>I/O actions at the prompt</title>
380
381 <para>GHCi does more than simple expression evaluation at the prompt.
382 If you type something of type <literal>IO a</literal> for some
383     <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
384     as an IO-computation.
385 <screen>
386 Prelude> "hello"
387 "hello"
388 Prelude> putStrLn "hello"
389 hello
390 </screen>
391 Furthermore, GHCi will print the result of the I/O action if (and only
392 if):
393 <itemizedlist>
394   <listitem><para>The result type is an instance of <literal>Show</literal>.</para></listitem>
395   <listitem><para>The result type is not
396   <literal>()</literal>.</para></listitem>
397 </itemizedlist>
398 For example, remembering that <literal>putStrLn :: String -> IO ()</literal>:
399 <screen>
400 Prelude> putStrLn "hello"
401 hello
402 Prelude> do { putStrLn "hello"; return "yes" }
403 hello
404 "yes"
405 </screen>
406 </para></sect2>
407
408     <sect2 id="ghci-stmts">
409       <title>Using <literal>do-</literal>notation at the prompt</title>
410       <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
411       <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
412       
413       <para>GHCi actually accepts <firstterm>statements</firstterm>
414       rather than just expressions at the prompt.  This means you can
415       bind values and functions to names, and use them in future
416       expressions or statements.</para>
417
418       <para>The syntax of a statement accepted at the GHCi prompt is
419       exactly the same as the syntax of a statement in a Haskell
420       <literal>do</literal> expression.  However, there's no monad
421       overloading here: statements typed at the prompt must be in the
422       <literal>IO</literal> monad.
423 <screen>
424 Prelude> x &lt;- return 42
425 42
426 Prelude> print x
427 42
428 Prelude>
429 </screen>
430       The statement <literal>x &lt;- return 42</literal> means
431       &ldquo;execute <literal>return 42</literal> in the
432       <literal>IO</literal> monad, and bind the result to
433       <literal>x</literal>&rdquo;.  We can then use
434       <literal>x</literal> in future statements, for example to print
435       it as we did above.</para>
436
437       <para>GHCi will print the result of a statement if and only if: 
438         <itemizedlist>
439           <listitem>
440             <para>The statement is not a binding, or it is a monadic binding 
441               (<literal>p &lt;- e</literal>) that binds exactly one
442               variable.</para>
443           </listitem>
444           <listitem>
445             <para>The variable's type is not polymorphic, is not
446               <literal>()</literal>, and is an instance of
447               <literal>Show</literal></para>
448           </listitem>
449         </itemizedlist>
450       The automatic printing of binding results can be supressed with
451       <option>:set -fno-print-bind-result</option> (this does not
452       supress printing the result of non-binding statements).
453       <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
454       You might want to do this to prevent the result of binding
455       statements from being fully evaluated by the act of printing
456       them, for example.</para>
457
458       <para>Of course, you can also bind normal non-IO expressions
459       using the <literal>let</literal>-statement:</para>
460 <screen>
461 Prelude> let x = 42
462 Prelude> x
463 42
464 Prelude>
465 </screen>
466       <para>Another important difference between the two types of binding
467       is that the monadic bind (<literal>p &lt;- e</literal>) is
468       <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
469       whereas with the <literal>let</literal> form, the expression
470       isn't evaluated immediately:</para>
471 <screen>
472 Prelude> let x = error "help!"
473 Prelude> print x
474 *** Exception: help!
475 Prelude>
476 </screen>
477
478       <para>Note that <literal>let</literal> bindings do not automatically
479         print the value bound, unlike monadic bindings.</para>
480
481       <para>Any exceptions raised during the evaluation or execution
482       of the statement are caught and printed by the GHCi command line
483       interface (for more information on exceptions, see the module
484       <literal>Control.Exception</literal> in the libraries
485       documentation).</para>
486
487       <para>Every new binding shadows any existing bindings of the
488       same name, including entities that are in scope in the current
489       module context.</para>
490
491       <para>WARNING: temporary bindings introduced at the prompt only
492       last until the next <literal>:load</literal> or
493       <literal>:reload</literal> command, at which time they will be
494       simply lost.  However, they do survive a change of context with
495       <literal>:module</literal>: the temporary bindings just move to
496       the new location.</para>
497
498       <para>HINT: To get a list of the bindings currently in scope, use the
499       <literal>:show bindings</literal> command:</para>
500
501 <screen>
502 Prelude> :show bindings
503 x :: Int
504 Prelude></screen>
505
506       <para>HINT: if you turn on the <literal>+t</literal> option,
507       GHCi will show the type of each variable bound by a statement.
508       For example:</para>
509       <indexterm><primary><literal>+t</literal></primary></indexterm>
510 <screen>
511 Prelude> :set +t
512 Prelude> let (x:xs) = [1..]
513 x :: Integer
514 xs :: [Integer]
515 </screen>
516
517     </sect2>
518
519     <sect2 id="ghci-scope">
520       <title>What's really in scope at the prompt?</title> 
521
522       <para>When you type an expression at the prompt, what
523       identifiers and types are in scope?  GHCi provides a flexible
524       way to control exactly how the context for an expression is
525       constructed.  Let's start with the simple cases; when you start
526       GHCi the prompt looks like this:</para>
527
528 <screen>Prelude></screen>
529
530       <para>Which indicates that everything from the module
531       <literal>Prelude</literal> is currently in scope.  If we now
532       load a file into GHCi, the prompt will change:</para>
533
534 <screen>
535 Prelude> :load Main.hs
536 Compiling Main             ( Main.hs, interpreted )
537 *Main>
538 </screen>
539
540       <para>The new prompt is <literal>*Main</literal>, which
541       indicates that we are typing expressions in the context of the
542       top-level of the <literal>Main</literal> module.  Everything
543       that is in scope at the top-level in the module
544       <literal>Main</literal> we just loaded is also in scope at the
545       prompt (probably including <literal>Prelude</literal>, as long
546       as <literal>Main</literal> doesn't explicitly hide it).</para>
547
548       <para>The syntax
549       <literal>*<replaceable>module</replaceable></literal> indicates
550       that it is the full top-level scope of
551       <replaceable>module</replaceable> that is contributing to the
552       scope for expressions typed at the prompt.  Without the
553       <literal>*</literal>, just the exports of the module are
554       visible.</para>
555
556       <para>We're not limited to a single module: GHCi can combine
557       scopes from multiple modules, in any mixture of
558       <literal>*</literal> and non-<literal>*</literal> forms.  GHCi
559       combines the scopes from all of these modules to form the scope
560       that is in effect at the prompt.  For technical reasons, GHCi
561       can only support the <literal>*</literal>-form for modules which
562       are interpreted, so compiled modules and package modules can
563       only contribute their exports to the current scope.</para>
564
565       <para>The scope is manipulated using the
566       <literal>:module</literal> command.  For example, if the current
567       scope is <literal>Prelude</literal>, then we can bring into
568       scope the exports from the module <literal>IO</literal> like
569       so:</para>
570
571 <screen>
572 Prelude> :module +IO
573 Prelude IO> hPutStrLn stdout "hello\n"
574 hello
575 Prelude IO>
576 </screen>
577
578       <para>(Note: <literal>:module</literal> can be shortened to
579       <literal>:m</literal>). The full syntax of the
580       <literal>:module</literal> command is:</para>
581
582 <screen>
583 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
584 </screen>
585
586       <para>Using the <literal>+</literal> form of the
587       <literal>module</literal> commands adds modules to the current
588       scope, and <literal>-</literal> removes them.  Without either
589       <literal>+</literal> or <literal>-</literal>, the current scope
590       is replaced by the set of modules specified.  Note that if you
591       use this form and leave out <literal>Prelude</literal>, GHCi
592       will assume that you really wanted the
593       <literal>Prelude</literal> and add it in for you (if you don't
594       want the <literal>Prelude</literal>, then ask to remove it with
595       <literal>:m -Prelude</literal>).</para>
596
597       <para>The scope is automatically set after a
598       <literal>:load</literal> command, to the most recently loaded
599       "target" module, in a <literal>*</literal>-form if possible.
600       For example, if you say <literal>:load foo.hs bar.hs</literal>
601       and <filename>bar.hs</filename> contains module
602       <literal>Bar</literal>, then the scope will be set to
603       <literal>*Bar</literal> if <literal>Bar</literal> is
604       interpreted, or if <literal>Bar</literal> is compiled it will be
605       set to <literal>Prelude Bar</literal> (GHCi automatically adds
606       <literal>Prelude</literal> if it isn't present and there aren't
607       any <literal>*</literal>-form modules).</para>
608
609       <para>With multiple modules in scope, especially multiple
610       <literal>*</literal>-form modules, it is likely that name
611       clashes will occur.  Haskell specifies that name clashes are
612       only reported when an ambiguous identifier is used, and GHCi
613       behaves in the same way for expressions typed at the
614       prompt.</para>
615
616       <para>
617         Hint: GHCi will tab-complete names that are in scope; for
618         example, if you run GHCi and type <literal>J&lt;tab&gt;</literal>
619         then GHCi will expand it to <literal>Just </literal>.
620       </para>
621
622       <sect3>
623         <title>Qualified names</title>
624
625         <para>To make life slightly easier, the GHCi prompt also
626         behaves as if there is an implicit <literal>import
627         qualified</literal> declaration for every module in every
628         package, and every module currently loaded into GHCi.</para>
629       </sect3>
630
631       <sect3>
632         <title>The <literal>:main</literal> command</title>
633
634         <para>
635           When a program is compiled and executed, it can use the
636           <literal>getArgs</literal> function to access the
637           command-line arguments.
638           However, we cannot simply pass the arguments to the
639           <literal>main</literal> function while we are testing in ghci,
640           as the <literal>main</literal> function doesn't take its
641           directly.
642         </para>
643
644         <para>
645           Instead, we can use the <literal>:main</literal> command.
646           This runs whatever <literal>main</literal> is in scope, with
647           any arguments being treated the same as command-line arguments,
648           e.g.:
649         </para>
650
651 <screen>
652 Prelude> let main = System.Environment.getArgs >>= print
653 Prelude> :main foo bar
654 ["foo","bar"]
655 </screen>
656
657       </sect3>
658     </sect2>
659   
660
661     <sect2>
662       <title>The <literal>it</literal> variable</title>
663       <indexterm><primary><literal>it</literal></primary>
664       </indexterm>
665       
666       <para>Whenever an expression (or a non-binding statement, to be
667       precise) is typed at the prompt, GHCi implicitly binds its value
668       to the variable <literal>it</literal>.  For example:</para>
669 <screen>
670 Prelude> 1+2
671 3
672 Prelude> it * 2
673 6
674 </screen>
675     <para>What actually happens is that GHCi typechecks the
676     expression, and if it doesn't have an <literal>IO</literal> type,
677     then it transforms it as follows: an expression
678     <replaceable>e</replaceable> turns into 
679 <screen>
680 let it = <replaceable>e</replaceable>;
681 print it
682 </screen>
683     which is then run as an IO-action.</para>
684
685     <para>Hence, the original expression must have a type which is an
686     instance of the <literal>Show</literal> class, or GHCi will
687     complain:</para>
688
689 <screen>
690 Prelude&gt; id
691
692 &lt;interactive&gt;:1:0:
693     No instance for (Show (a -&gt; a))
694       arising from use of `print' at &lt;interactive&gt;:1:0-1
695     Possible fix: add an instance declaration for (Show (a -> a))
696     In the expression: print it
697     In a 'do' expression: print it
698 </screen>
699
700     <para>The error message contains some clues as to the
701     transformation happening internally.</para>
702
703       <para>If the expression was instead of type <literal>IO a</literal> for
704       some <literal>a</literal>, then <literal>it</literal> will be
705       bound to the result of the <literal>IO</literal> computation,
706       which is of type <literal>a</literal>.  eg.:</para>
707 <screen>
708 Prelude> Time.getClockTime
709 Wed Mar 14 12:23:13 GMT 2001
710 Prelude> print it
711 Wed Mar 14 12:23:13 GMT 2001
712 </screen>
713
714       <para>The corresponding translation for an IO-typed
715       <replaceable>e</replaceable> is
716 <screen>
717 it &lt;- <replaceable>e</replaceable>
718 </screen>
719       </para>
720
721       <para>Note that <literal>it</literal> is shadowed by the new
722       value each time you evaluate a new expression, and the old value
723       of <literal>it</literal> is lost.</para>
724
725     </sect2>
726
727     <sect2 id="extended-default-rules">
728       <title>Type defaulting in GHCi</title>
729     <indexterm><primary>Type default</primary></indexterm>
730     <indexterm><primary><literal>Show</literal> class</primary></indexterm>
731       <para>
732       Consider this GHCi session:
733 <programlisting>
734   ghci> reverse []
735 </programlisting>
736       What should GHCi do?  Strictly speaking, the program is ambiguous.  <literal>show (reverse [])</literal>
737       (which is what GHCi computes here) has type <literal>Show a => a</literal> and how that displays depends 
738       on the type <literal>a</literal>.  For example:
739 <programlisting>
740   ghci> (reverse []) :: String
741   ""
742   ghci> (reverse []) :: [Int]
743   []
744 </programlisting>
745     However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting
746     rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows.  The
747     standard rules take each group of constraints <literal>(C1 a, C2 a, ..., Cn
748     a)</literal> for each type variable <literal>a</literal>, and defaults the
749     type variable if 
750         <itemizedlist>
751             <listitem><para> The type variable <literal>a</literal>
752         appears in no other constraints </para></listitem>
753             <listitem><para> All the classes <literal>Ci</literal> are standard.</para></listitem>
754             <listitem><para> At least one of the classes <literal>Ci</literal> is
755             numeric.</para></listitem>
756       </itemizedlist>
757    At the GHCi prompt, the second and third rules are relaxed as follows
758    (differences italicised):
759         <itemizedlist>
760             <listitem><para> <emphasis>All</emphasis> of the classes
761             <literal>Ci</literal> are single-parameter type classes.</para></listitem>
762             <listitem><para> At least one of the classes <literal>Ci</literal> is
763             numeric, <emphasis>or is <literal>Show</literal>, 
764                 <literal>Eq</literal>, or <literal>Ord</literal></emphasis>.</para></listitem>
765       </itemizedlist>
766    The same type-default behaviour can be enabled in an ordinary Haskell
767    module, using the flag <literal>-fextended-default-rules</literal>. 
768    </para>
769     </sect2>
770   </sect1>
771
772   <sect1 id="ghci-invocation">
773     <title>Invoking GHCi</title>
774     <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
775     <indexterm><primary><option>&ndash;&ndash;interactive</option></primary></indexterm>
776
777     <para>GHCi is invoked with the command <literal>ghci</literal> or
778     <literal>ghc &ndash;&ndash;interactive</literal>.  One or more modules or
779     filenames can also be specified on the command line; this
780     instructs GHCi to load the specified modules or filenames (and all
781     the modules they depend on), just as if you had said
782     <literal>:load <replaceable>modules</replaceable></literal> at the
783     GHCi prompt (see <xref linkend="ghci-commands"/>).  For example, to
784     start GHCi and load the program whose topmost module is in the
785     file <literal>Main.hs</literal>, we could say:</para>
786
787 <screen>
788 $ ghci Main.hs
789 </screen>
790
791     <para>Most of the command-line options accepted by GHC (see <xref
792     linkend="using-ghc"/>) also make sense in interactive mode.  The ones
793     that don't make sense are mostly obvious; for example, GHCi
794     doesn't generate interface files, so options related to interface
795     file generation won't have any effect.</para>
796
797     <sect2>
798       <title>Packages</title>
799       <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
800
801       <para>Most packages (see <xref linkend="using-packages"/>) are
802       available without needing to specify any extra flags at all:
803       they will be automatically loaded the first time they are
804       needed.</para>
805
806       <para>For non-auto packages, however, you need to request the
807       package be loaded by using the <literal>-package</literal> flag:</para>
808
809 <screen>
810 $ ghci -package readline
811    ___         ___ _
812   / _ \ /\  /\/ __(_)
813  / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
814 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
815 \____/\/ /_/\____/|_|      Type :? for help.
816
817 Loading package base ... linking ... done.
818 Loading package readline-1.0 ... linking ... done.
819 Prelude> 
820 </screen>
821
822       <para>The following command works to load new packages into a
823       running GHCi:</para>
824
825 <screen>
826 Prelude> :set -package <replaceable>name</replaceable>
827 </screen>
828
829       <para>But note that doing this will cause all currently loaded
830       modules to be unloaded, and you'll be dumped back into the
831       <literal>Prelude</literal>.</para>
832     </sect2>
833
834     <sect2>
835       <title>Extra libraries</title>
836       <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
837       
838       <para>Extra libraries may be specified on the command line using
839       the normal <literal>-l<replaceable>lib</replaceable></literal>
840       option.  (The term <emphasis>library</emphasis> here refers to
841       libraries of foreign object code; for using libraries of Haskell
842       source code, see <xref linkend="ghci-modules-filenames"/>.) For
843       example, to load the &ldquo;m&rdquo; library:</para>
844
845 <screen>
846 $ ghci -lm
847 </screen>
848
849       <para>On systems with <literal>.so</literal>-style shared
850       libraries, the actual library loaded will the
851       <filename>lib<replaceable>lib</replaceable>.so</filename>.  GHCi
852       searches the following places for libraries, in this order:</para>
853
854       <itemizedlist>
855         <listitem>
856           <para>Paths specified using the
857           <literal>-L<replaceable>path</replaceable></literal>
858           command-line option,</para>
859         </listitem>
860         <listitem>
861           <para>the standard library search path for your system,
862           which on some systems may be overridden by setting the
863           <literal>LD_LIBRARY_PATH</literal> environment
864           variable.</para>
865         </listitem>
866       </itemizedlist>
867
868       <para>On systems with <literal>.dll</literal>-style shared
869       libraries, the actual library loaded will be
870       <filename><replaceable>lib</replaceable>.dll</filename>.  Again,
871       GHCi will signal an error if it can't find the library.</para>
872
873       <para>GHCi can also load plain object files
874       (<literal>.o</literal> or <literal>.obj</literal> depending on
875       your platform) from the command-line.  Just add the name the
876       object file to the command line.</para>
877
878       <para>Ordering of <option>-l</option> options matters: a library
879       should be mentioned <emphasis>before</emphasis> the libraries it
880       depends on (see <xref linkend="options-linker"/>).</para>
881     </sect2>
882
883   </sect1>
884
885   <sect1 id="ghci-commands">
886     <title>GHCi commands</title>
887
888     <para>GHCi commands all begin with
889     &lsquo;<literal>:</literal>&rsquo; and consist of a single command
890     name followed by zero or more parameters.  The command name may be
891     abbreviated, with ambiguities being resolved in favour of the more
892     commonly used commands.</para>
893
894     <variablelist>
895       <varlistentry>
896         <term>
897           <literal>:add</literal> <replaceable>module</replaceable> ...
898           <indexterm><primary><literal>:add</literal></primary></indexterm>
899         </term>
900         <listitem>
901           <para>Add <replaceable>module</replaceable>(s) to the
902           current <firstterm>target set</firstterm>, and perform a
903           reload.</para>
904         </listitem>
905       </varlistentry>
906
907       <varlistentry>
908         <term>
909           <literal>:breakpoint</literal> <replaceable>list|add|continue|del|stop|step</replaceable> ...
910           <indexterm><primary><literal>:breakpoint</literal></primary></indexterm>
911         </term>
912         <listitem>
913           <para>Permits to add, delete or list the breakpoints in a debugging session.
914           In order to make this command available, the 
915           <literal>-fdebugging</literal> flag must be active. The easiest way is to launch
916           GHCi with the <literal>-fdebugging</literal> option. For more
917           details on how the debugger works, see <xref linkend="ghci-debugger"/>.
918           </para>
919         </listitem>
920       </varlistentry>
921
922       <varlistentry>
923         <term>
924           <literal>:browse</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
925           <indexterm><primary><literal>:browse</literal></primary></indexterm>
926         </term>
927         <listitem>
928           <para>Displays the identifiers defined by the module
929           <replaceable>module</replaceable>, which must be either
930           loaded into GHCi or be a member of a package.  If the
931           <literal>*</literal> symbol is placed before the module
932           name, then <emphasis>all</emphasis> the identifiers defined
933           in <replaceable>module</replaceable> are shown; otherwise
934           the list is limited to the exports of
935           <replaceable>module</replaceable>.  The
936           <literal>*</literal>-form is only available for modules
937           which are interpreted; for compiled modules (including
938           modules from packages) only the non-<literal>*</literal>
939           form of <literal>:browse</literal> is available.</para>
940         </listitem>
941       </varlistentry>
942
943       <varlistentry>
944         <term>
945           <literal>:cd</literal> <replaceable>dir</replaceable>
946           <indexterm><primary><literal>:cd</literal></primary></indexterm>
947         </term>
948         <listitem>
949           <para>Changes the current working directory to
950           <replaceable>dir</replaceable>.  A
951           &lsquo;<literal>&tilde;</literal>&rsquo; symbol at the
952           beginning of <replaceable>dir</replaceable> will be replaced
953           by the contents of the environment variable
954           <literal>HOME</literal>.</para>
955
956           <para>NOTE: changing directories causes all currently loaded
957           modules to be unloaded.  This is because the search path is
958           usually expressed using relative directories, and changing
959           the search path in the middle of a session is not
960           supported.</para>
961         </listitem>
962       </varlistentry>
963
964       <varlistentry>
965         <term>
966           <literal>:continue</literal> 
967           <indexterm><primary><literal>:continue</literal></primary></indexterm>
968         </term>
969         <listitem><para>Shortcut to <literal>:breakpoint continue</literal></para>
970         </listitem>
971       </varlistentry>
972
973       <varlistentry>
974         <term>
975           <literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable>
976           <indexterm><primary><literal>:def</literal></primary></indexterm>
977         </term>
978         <listitem>
979           <para>The command <literal>:def</literal>
980           <replaceable>name</replaceable>
981           <replaceable>expr</replaceable> defines a new GHCi command
982           <literal>:<replaceable>name</replaceable></literal>,
983           implemented by the Haskell expression
984           <replaceable>expr</replaceable>, which must have type
985           <literal>String -> IO String</literal>.  When
986           <literal>:<replaceable>name</replaceable>
987           <replaceable>args</replaceable></literal> is typed at the
988           prompt, GHCi will run the expression
989           <literal>(<replaceable>name</replaceable>
990           <replaceable>args</replaceable>)</literal>, take the
991           resulting <literal>String</literal>, and feed it back into
992           GHCi as a new sequence of commands.  Separate commands in
993           the result must be separated by
994           &lsquo;<literal>\n</literal>&rsquo;.</para>
995
996           <para>That's all a little confusing, so here's a few
997           examples.  To start with, here's a new GHCi command which
998           doesn't take any arguments or produce any results, it just
999           outputs the current date &amp; time:</para>
1000
1001 <screen>
1002 Prelude> let date _ = Time.getClockTime >>= print >> return ""
1003 Prelude> :def date date
1004 Prelude> :date
1005 Fri Mar 23 15:16:40 GMT 2001
1006 </screen>
1007
1008           <para>Here's an example of a command that takes an argument.
1009           It's a re-implementation of <literal>:cd</literal>:</para>
1010
1011 <screen>
1012 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
1013 Prelude> :def mycd mycd
1014 Prelude> :mycd ..
1015 </screen>
1016
1017           <para>Or I could define a simple way to invoke
1018           &ldquo;<literal>ghc &ndash;&ndash;make Main</literal>&rdquo; in the
1019           current directory:</para>
1020
1021 <screen>
1022 Prelude> :def make (\_ -> return ":! ghc &ndash;&ndash;make Main")
1023 </screen>
1024
1025           <para>We can define a command that reads GHCi input from a
1026           file.  This might be useful for creating a set of bindings
1027           that we want to repeatedly load into the GHCi session:</para>
1028
1029 <screen>
1030 Prelude> :def . readFile
1031 Prelude> :. cmds.ghci
1032 </screen>
1033
1034           <para>Notice that we named the command
1035           <literal>:.</literal>, by analogy with the
1036           &lsquo;<literal>.</literal>&rsquo; Unix shell command that
1037           does the same thing.</para>
1038         </listitem>
1039       </varlistentry>
1040
1041       <varlistentry>
1042         <term>
1043           <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
1044           <indexterm><primary><literal>:edit</literal></primary></indexterm>
1045         </term>
1046         <listitem>
1047           <para>Opens an editor to edit the file
1048           <replaceable>file</replaceable>, or the most recently loaded
1049           module if <replaceable>file</replaceable> is omitted.  The
1050           editor to invoke is taken from the <literal>EDITOR</literal>
1051           environment variable, or a default editor on your system if
1052           <literal>EDITOR</literal> is not set.  You can change the
1053           editor using <literal>:set editor</literal>.</para>
1054         </listitem>
1055       </varlistentry>
1056
1057       <varlistentry>
1058         <term>
1059           <literal>:help</literal>
1060           <indexterm><primary><literal>:help</literal></primary></indexterm>
1061         </term>
1062         <term>
1063           <literal>:?</literal>
1064           <indexterm><primary><literal>:?</literal></primary></indexterm>
1065         </term>
1066         <listitem>
1067           <para>Displays a list of the available commands.</para>
1068         </listitem>
1069       </varlistentry>
1070
1071       <varlistentry>
1072         <term>
1073           <literal>:info</literal> <replaceable>name</replaceable> ...
1074           <indexterm><primary><literal>:info</literal></primary></indexterm>
1075         </term>
1076         <listitem>
1077           <para>Displays information about the given name(s).  For
1078           example, if <replaceable>name</replaceable> is a class, then
1079           the class methods and their types will be printed;  if
1080           <replaceable>name</replaceable> is a type constructor, then
1081           its definition will be printed;  if
1082           <replaceable>name</replaceable> is a function, then its type
1083           will be printed.  If <replaceable>name</replaceable> has
1084           been loaded from a source file, then GHCi will also display
1085           the location of its definition in the source.</para>
1086         </listitem>
1087       </varlistentry>
1088
1089       <varlistentry>
1090         <term>
1091           <literal>:load</literal> <replaceable>module</replaceable> ...
1092           <indexterm><primary><literal>:load</literal></primary></indexterm>
1093         </term>
1094         <listitem>
1095           <para>Recursively loads the specified
1096           <replaceable>module</replaceable>s, and all the modules they
1097           depend on.  Here, each <replaceable>module</replaceable>
1098           must be a module name or filename, but may not be the name
1099           of a module in a package.</para>
1100
1101           <para>All previously loaded modules, except package modules,
1102           are forgotten.  The new set of modules is known as the
1103           <firstterm>target set</firstterm>.  Note that
1104           <literal>:load</literal> can be used without any arguments
1105           to unload all the currently loaded modules and
1106           bindings.</para>
1107
1108           <para>After a <literal>:load</literal> command, the current
1109           context is set to:</para>
1110
1111           <itemizedlist>
1112             <listitem>
1113               <para><replaceable>module</replaceable>, if it was loaded
1114               successfully, or</para>
1115             </listitem>
1116             <listitem>
1117               <para>the most recently successfully loaded module, if
1118               any other modules were loaded as a result of the current
1119               <literal>:load</literal>, or</para>
1120             </listitem>
1121             <listitem>
1122               <para><literal>Prelude</literal> otherwise.</para>
1123             </listitem>
1124           </itemizedlist>
1125         </listitem>
1126       </varlistentry>
1127
1128       <varlistentry>
1129         <term>
1130           <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
1131           <indexterm><primary><literal>:main</literal></primary></indexterm>
1132         </term>
1133         <listitem>
1134           <para>
1135             When a program is compiled and executed, it can use the
1136             <literal>getArgs</literal> function to access the
1137             command-line arguments.
1138             However, we cannot simply pass the arguments to the
1139             <literal>main</literal> function while we are testing in ghci,
1140             as the <literal>main</literal> function doesn't take its
1141             directly.
1142           </para>
1143
1144           <para>
1145             Instead, we can use the <literal>:main</literal> command.
1146             This runs whatever <literal>main</literal> is in scope, with
1147             any arguments being treated the same as command-line arguments,
1148             e.g.:
1149           </para>
1150
1151 <screen>
1152 Prelude> let main = System.Environment.getArgs >>= print
1153 Prelude> :main foo bar
1154 ["foo","bar"]
1155 </screen>
1156
1157         </listitem>
1158       </varlistentry>
1159
1160       <varlistentry>
1161         <term>
1162           <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
1163           <indexterm><primary><literal>:module</literal></primary></indexterm>
1164         </term>
1165         <listitem>
1166           <para>Sets or modifies the current context for statements
1167           typed at the prompt.  See <xref linkend="ghci-scope"/> for
1168           more details.</para>
1169         </listitem>
1170       </varlistentry>
1171
1172       <varlistentry>
1173         <term>
1174           <literal>:print </literal> <replaceable>names</replaceable> ...
1175           <indexterm><primary><literal>:print</literal></primary></indexterm>
1176         </term>
1177         <listitem>
1178           <para> Prints a semievaluated value without forcing its evaluation. 
1179           <literal>:print </literal> works just like <literal>:sprint</literal> but additionally, 
1180            <literal>:print</literal> binds the unevaluated parts -called 
1181            <quote>suspensions</quote>-
1182           to names which you can play with. For example:
1183 <screen>
1184 Prelude> let li = map Just [1..5]
1185 Prelude> :sp li
1186 li - _
1187 Prelude> :p li
1188 li - (_t1::[Maybe Integer])
1189 Prelude> head li
1190 Just 1
1191 Prelude> :sp li
1192 li - [Just 1 | _]
1193 Prelude> :p li
1194 li - [Just 1 | (_t2::[Maybe Integer])]
1195 Prelude> last li
1196 Just 5
1197 Prelude> :sp li
1198 li - [Just 1,_,_,_,Just 5]
1199 Prelude> :p li
1200 li - [Just 1,(_t3::Maybe Integer),(_t4::Maybe Integer),(_t5::Maybe Integer),Just 4]
1201 Prelude> _t4
1202 Just 3
1203 Prelude> :p li
1204 li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4]
1205 </screen>
1206          The example uses <literal>:print</literal> and  <literal>:sprint</literal> 
1207          to help us observe how the <literal>li</literal> variable is evaluated progressively as we operate
1208          with it. Note for instance how <quote>last</quote> traverses all the elements of
1209          the list to compute its result, but without evaluating the individual elements.</para>
1210            <para>Finally note that the Prolog convention of [head | tail] is used by 
1211          <literal>:sprint</literal> to display unevaluated lists.
1212           </para>
1213         </listitem>
1214       </varlistentry>
1215
1216       <varlistentry>
1217         <term>
1218           <literal>:quit</literal>
1219           <indexterm><primary><literal>:quit</literal></primary></indexterm>
1220         </term>
1221         <listitem>
1222           <para>Quits GHCi.  You can also quit by typing a control-D
1223           at the prompt.</para>
1224         </listitem>
1225       </varlistentry>
1226
1227       <varlistentry>
1228         <term>
1229           <literal>:reload</literal>
1230           <indexterm><primary><literal>:reload</literal></primary></indexterm>
1231         </term>
1232         <listitem>
1233           <para>Attempts to reload the current target set (see
1234           <literal>:load</literal>) if any of the modules in the set,
1235           or any dependent module, has changed.  Note that this may
1236           entail loading new modules, or dropping modules which are no
1237           longer indirectly required by the target.</para>
1238         </listitem>
1239       </varlistentry>
1240
1241       <varlistentry>
1242         <term>
1243           <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
1244           <indexterm><primary><literal>:set</literal></primary></indexterm>
1245         </term>
1246         <listitem>
1247           <para>Sets various options.  See <xref linkend="ghci-set"/>
1248           for a list of available options.  The
1249           <literal>:set</literal> command by itself shows which
1250           options are currently set.</para>
1251         </listitem>
1252       </varlistentry>
1253
1254       <varlistentry>
1255         <term>
1256           <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
1257           <indexterm><primary><literal>:set args</literal></primary></indexterm>
1258         </term>
1259         <listitem>
1260           <para>Sets the list of arguments which are returned when the
1261           program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
1262             </indexterm>.</para>
1263         </listitem>
1264       </varlistentry>
1265
1266       <varlistentry>
1267         <term>
1268            <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
1269         </term>
1270         <listitem>
1271           <para>Sets the command used by <literal>:edit</literal> to
1272           <replaceable>cmd</replaceable>.</para>
1273         </listitem>
1274       </varlistentry>
1275
1276       <varlistentry>
1277         <term>
1278            <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
1279            <indexterm><primary><literal>:set prog</literal></primary></indexterm>
1280         </term>
1281         <listitem>
1282           <para>Sets the string to be returned when the program calls
1283           <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
1284             </indexterm>.</para>
1285         </listitem>
1286       </varlistentry>
1287
1288       <varlistentry>
1289         <term>
1290            <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
1291         </term>
1292         <listitem>
1293           <para>Sets the string to be used as the prompt in GHCi.
1294           Inside <replaceable>prompt</replaceable>, the sequence
1295           <literal>%s</literal> is replaced by the names of the
1296           modules currently in scope, and <literal>%%</literal> is
1297           replaced by <literal>%</literal>.</para>
1298         </listitem>
1299       </varlistentry>
1300
1301       <varlistentry>
1302         <term>
1303           <literal>:show bindings</literal>
1304           <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
1305         </term>
1306         <listitem>
1307           <para>Show the bindings made at the prompt and their
1308           types.</para>
1309         </listitem>
1310       </varlistentry>
1311
1312       <varlistentry>
1313         <term>
1314           <literal>:show modules</literal>
1315           <indexterm><primary><literal>:show modules</literal></primary></indexterm>
1316         </term>
1317         <listitem>
1318           <para>Show the list of modules currently load.</para>
1319         </listitem>
1320       </varlistentry>
1321       <varlistentry>
1322         <term>
1323           <literal>:sprint</literal>
1324           <indexterm><primary><literal>:sprint</literal></primary></indexterm>
1325         </term>
1326         <listitem>
1327           <para>Prints a semievaluated value without forcing its evaluation. 
1328           <literal>:sprint</literal> and its sibling <literal>:print</literal> 
1329           are very useful to observe how lazy evaluation works in your code. For example:
1330 <screen>
1331 Prelude> let li = map Just [1..5]
1332 Prelude> :sp li
1333 li - _
1334 Prelude> head li
1335 Just 1
1336 Prelude> :sp li
1337 li - [Just 1 | _]
1338 Prelude> last li
1339 Just 5
1340 Prelude> :sp li
1341 li - [Just 1,_,_,_,Just 5]
1342 </screen>
1343          The example uses <literal>:sprint</literal> to help us observe how the <literal>li</literal> variable is evaluated progressively as we operate
1344          with it. Note for instance how <quote>last</quote> traverses all the elements of
1345          the list to compute its result, but without evaluating the individual elements.</para>
1346            <para>Finally note that the Prolog convention of [head | tail] is used by 
1347          <literal>:sprint</literal> to display unevaluated lists.
1348           </para>
1349         </listitem>
1350       </varlistentry>
1351       <varlistentry>
1352         <term>
1353           <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
1354           <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
1355           <indexterm><primary><literal>:etags</literal></primary>
1356           </indexterm>
1357           <indexterm><primary><literal>:etags</literal></primary>
1358           </indexterm>
1359         </term>
1360         <listitem>
1361           <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
1362             (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>).  If
1363             no filename is specified, the defaulit <filename>tags</filename> or
1364             <filename>TAGS</filename> is
1365             used, respectively.  Tags for all the functions, constructors and
1366             types in the currently loaded modules are created.  All modules must
1367             be interpreted for these commands to work.</para>
1368           <para>See also <xref linkend="hasktags" />.</para>
1369         </listitem>
1370       </varlistentry>
1371
1372       <varlistentry>
1373         <term>
1374          <literal>:type</literal> <replaceable>expression</replaceable>
1375          <indexterm><primary><literal>:type</literal></primary></indexterm>
1376         </term>
1377         <listitem>
1378           <para>Infers and prints the type of
1379           <replaceable>expression</replaceable>, including explicit
1380           forall quantifiers for polymorphic types.  The monomorphism
1381           restriction is <emphasis>not</emphasis> applied to the
1382           expression during type inference.</para>
1383         </listitem>
1384       </varlistentry>
1385
1386       <varlistentry>
1387         <term>
1388           <literal>:kind</literal> <replaceable>type</replaceable>
1389           <indexterm><primary><literal>:kind</literal></primary></indexterm>
1390         </term>
1391         <listitem>
1392           <para>Infers and prints the kind of
1393           <replaceable>type</replaceable>. The latter can be an arbitrary
1394             type expression, including a partial application of a type constructor,
1395             such as <literal>Either Int</literal>.</para>
1396         </listitem>
1397       </varlistentry>
1398
1399       <varlistentry>
1400         <term>
1401           <literal>:undef</literal> <replaceable>name</replaceable>
1402           <indexterm><primary><literal>:undef</literal></primary></indexterm>
1403         </term>
1404         <listitem>
1405           <para>Undefines the user-defined command
1406           <replaceable>name</replaceable> (see <literal>:def</literal>
1407           above).</para>
1408         </listitem>
1409       </varlistentry>
1410
1411       <varlistentry>
1412         <term>
1413           <literal>:unset</literal> <replaceable>option</replaceable>...
1414           <indexterm><primary><literal>:unset</literal></primary></indexterm>
1415         </term>
1416         <listitem>
1417           <para>Unsets certain options.  See <xref linkend="ghci-set"/>
1418           for a list of available options.</para>
1419         </listitem>
1420       </varlistentry>
1421
1422       <varlistentry>
1423         <term>
1424           <literal>:!</literal> <replaceable>command</replaceable>...
1425           <indexterm><primary><literal>:!</literal></primary></indexterm>
1426           <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
1427         </term>
1428         <listitem>
1429           <para>Executes the shell command
1430           <replaceable>command</replaceable>.</para>
1431         </listitem>
1432       </varlistentry>
1433
1434     </variablelist>
1435   </sect1>
1436
1437   <sect1 id="ghci-set">
1438     <title>The <literal>:set</literal> command</title>
1439     <indexterm><primary><literal>:set</literal></primary></indexterm>
1440
1441     <para>The <literal>:set</literal> command sets two types of
1442     options: GHCi options, which begin with
1443     &lsquo;<literal>+</literal>&rdquo; and &ldquo;command-line&rdquo;
1444     options, which begin with &lsquo;-&rsquo;.  </para>
1445
1446     <para>NOTE: at the moment, the <literal>:set</literal> command
1447     doesn't support any kind of quoting in its arguments: quotes will
1448     not be removed and cannot be used to group words together.  For
1449     example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
1450     you expect.</para>
1451
1452     <sect2>
1453       <title>GHCi options</title>
1454       <indexterm><primary>options</primary><secondary>GHCi</secondary>
1455       </indexterm>
1456
1457       <para>GHCi options may be set using <literal>:set</literal> and
1458       unset using <literal>:unset</literal>.</para>
1459
1460       <para>The available GHCi options are:</para>
1461
1462       <variablelist>
1463         <varlistentry>
1464           <term>
1465             <literal>+r</literal>
1466             <indexterm><primary><literal>+r</literal></primary></indexterm>
1467             <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
1468             <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
1469           </term>
1470           <listitem>
1471             <para>Normally, any evaluation of top-level expressions
1472             (otherwise known as CAFs or Constant Applicative Forms) in
1473             loaded modules is retained between evaluations.  Turning
1474             on <literal>+r</literal> causes all evaluation of
1475             top-level expressions to be discarded after each
1476             evaluation (they are still retained
1477             <emphasis>during</emphasis> a single evaluation).</para>
1478           
1479             <para>This option may help if the evaluated top-level
1480             expressions are consuming large amounts of space, or if
1481             you need repeatable performance measurements.</para>
1482           </listitem>
1483         </varlistentry>
1484
1485         <varlistentry>
1486           <term>
1487             <literal>+s</literal>
1488             <indexterm><primary><literal>+s</literal></primary></indexterm>
1489           </term>
1490           <listitem>
1491             <para>Display some stats after evaluating each expression,
1492             including the elapsed time and number of bytes allocated.
1493             NOTE: the allocation figure is only accurate to the size
1494             of the storage manager's allocation area, because it is
1495             calculated at every GC.  Hence, you might see values of
1496             zero if no GC has occurred.</para>
1497           </listitem>
1498         </varlistentry>
1499
1500         <varlistentry>
1501           <term>
1502             <literal>+t</literal>
1503             <indexterm><primary><literal>+t</literal></primary></indexterm>
1504           </term>
1505           <listitem>
1506             <para>Display the type of each variable bound after a
1507             statement is entered at the prompt.  If the statement is a
1508             single expression, then the only variable binding will be
1509             for the variable
1510             &lsquo;<literal>it</literal>&rsquo;.</para>
1511           </listitem>
1512         </varlistentry>
1513       </variablelist>
1514     </sect2>
1515
1516     <sect2 id="ghci-cmd-line-options">
1517       <title>Setting GHC command-line options in GHCi</title>
1518
1519       <para>Normal GHC command-line options may also be set using
1520       <literal>:set</literal>.  For example, to turn on
1521       <option>-fglasgow-exts</option>, you would say:</para>
1522
1523 <screen>
1524 Prelude> :set -fglasgow-exts
1525 </screen>
1526       
1527       <para>Any GHC command-line option that is designated as
1528       <firstterm>dynamic</firstterm> (see the table in <xref
1529       linkend="flag-reference"/>), may be set using
1530       <literal>:set</literal>.  To unset an option, you can set the
1531       reverse option:</para>
1532       <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
1533
1534 <screen>
1535 Prelude> :set -fno-glasgow-exts
1536 </screen>
1537
1538       <para><xref linkend="flag-reference"/> lists the reverse for each
1539       option where applicable.</para>
1540
1541       <para>Certain static options (<option>-package</option>,
1542       <option>-I</option>, <option>-i</option>, and
1543       <option>-l</option> in particular) will also work, but some may
1544       not take effect until the next reload.</para>
1545       <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
1546     </sect2>
1547   </sect1>
1548   <sect1 id="ghci-debugger">
1549     <title>The GHCi debugger</title>
1550     <indexterm><primary>debugger</primary></indexterm>
1551     <para>GHCi embeds an utility debugger with a very basic set of operations. The debugger
1552           is always available in ghci, you do not need to do anything to activate it. </para>
1553     <para>The following conditions must hold before a module can be debugged in GHCi:
1554       <itemizedlist>
1555          <listitem>
1556            <para>The module must have been loaded interpreted, i.e. not loaded from an <filename>.o</filename> file compiled by ghc </para>
1557          </listitem>
1558          <listitem>
1559            <para>The module must have been loaded with the <literal>-fdebugging</literal> flag
1560            </para></listitem>
1561        </itemizedlist></para>
1562     <sect2><title>Using the debugger</title>
1563     <para>The debugger allows the insertion of breakpoints at specific locations in the source code. These locations are governed by event sites, and not by line as in traditional debuggers such as gdb. </para> <para>
1564       Once a breakpointed event is hit, the debugger stops the execution and you can examine the local variables in scope
1565       in the context of the event, as well as evaluate arbitrary Haskell expressions in
1566       a special interactive prompt. </para><para>
1567       
1568      When you are done you issue the <literal>:continue</literal> 
1569       command to leave the breakpoint and let the execution go on. 
1570      Note that not all the GHCi commands are supported in a breakpoint. 
1571
1572     </para>
1573     <sidebar><title>Events</title><?dbfo float-type="left"?>
1574     <para> Events are the places in source code where you can set a breakpoint.
1575 <programlisting>
1576 qsort [] = <co id="name-binding-co"/> []
1577 qsort (x:xs) = 
1578    <coref linkend="name-binding-co"/> let left  = <coref linkend="name-binding-co"/> filter (\y -> <co id="lambda-co"/> y &lt; x) xs
1579            right = <coref linkend="name-binding-co"/> case filter (\y -> <coref linkend="lambda-co"/> y &gt; x) xs of 
1580                               right_val -> <co id="case-co"/> right_val
1581     in <co id="let-co"/> qsort left ++ [x] ++ qsort right
1582 main = <coref linkend="name-binding-co"/> do { 
1583    arg &lt;- <coref linkend="name-binding-co"/> getLine ;
1584    let num = <coref linkend="name-binding-co"/> read arg :: [Int] ;
1585  <co id="stmts-co"/> print (qsort num) ;
1586  <coref linkend="stmts-co"/> putStrLn "GoodBye!" }
1587 </programlisting>
1588      The GHCi debugger recognizes the following event types:
1589     <calloutlist>
1590       <callout arearefs="name-binding-co" id="name-binding">
1591         <para>Function definition and local bindings in let/where</para>
1592     </callout>
1593     <callout arearefs="lambda-co" id="lambda">
1594         <para>Lambda expression entry point</para>
1595     </callout>
1596     <callout arearefs="let-co" id="let">
1597       <para>Let expression body</para>
1598     </callout>
1599     <callout arearefs="case-co" id="case">
1600       <para>Case alternative body</para>
1601     </callout>
1602     <callout arearefs="stmts-co" id="stmts">
1603       <para>do notation statements</para>
1604     </callout>
1605     </calloutlist></para>
1606     <para>In reality however, ghci eliminates some redundant event sites. 
1607     For instance, sites with two co-located breakpoint events are coalesced into a single one,
1608     and sites with no bindings in scope are assumed to be uninteresting and no breakpoint can be set in them.</para>
1609     </sidebar>
1610
1611 <para>
1612       You don't need to do anything special in order to start the debugging session.
1613       Simply use ghci to evaluate your Haskell expressions and whenever a breakpoint
1614       is hit, the debugger will enter the stage:
1615 <programlisting>
1616 *main:Main> :break add Main 2
1617 Breakpoint set at (2,15)
1618
1619 *main:Main> qsort [10,9..1]
1620 Local bindings in scope:
1621   x :: a, xs :: [a], left :: [a], right :: [a]
1622
1623 qsort2.hs:2:15-46>   
1624 </programlisting>
1625       What is happening here is that GHCi has interrupted the evaluation of 
1626       <literal>qsort</literal> at the breakpoint set in line 2, as the prompt indicates.
1627       At this point you can freely explore the contents of the bindings in scope,
1628       but with two catches. </para><para>
1629       First, take into account that due to the lazy nature of Haskell, some of
1630       these bindings may be unevaluated, and that exploring their contents may 
1631       trigger a computation. </para><para>
1632       Second: look at the types of the things in scope.
1633       GHCi has left its types parameterised by a variable!
1634       Look at the type of <literal>qsort</literal>, which is 
1635       polymorphic on the type of its argument. It does not 
1636       tell us really what the types of <literal>x</literal> and <literal>xs</literal> can be. 
1637       In general, polymorphic programs deal with polymorphic values,
1638       and this means that some of the bindings available in a breakpoint site
1639       will be parametrically typed.
1640       </para><para>
1641       So, what can we do with a value without concrete type? Very few interesting
1642       things. The <literal>:print</literal> command in ghci allows you to 
1643       explore its contents and see if it is evaluated or not. 
1644       This is useful because you cannot just type <literal>x</literal> in the 
1645       prompt and expect GHCi to return you its value. Perhaps you know for 
1646       sure that 
1647       <literal>x</literal> is of type <literal>Int</literal>, which is an instance of 
1648       <literal>Show</literal>, but GHCi does not have this information. 
1649       <literal>:print</literal> however is fine, because it does not need to know the 
1650       type to do its work. </para>
1651       <para> Let's go on with the debugging session of the <literal>qsort</literal>
1652       example:
1653 <example id="debuggingEx"><title>A short debugging session</title>
1654 <programlisting>
1655 qsort2.hs:2:15-46> x
1656 This is an untyped, unevaluated computation. You can use seq to 
1657 force its evaluation and then :print to recover its type <co id="seq1"/>
1658 qsort2.hs:2:15-46> seq x ()  <co id="seq2"/>
1659 () 
1660 qsort2.hs:2:15-46> x <co id="seq3"/>
1661 This is an untyped, unevaluated computation. You can use seq to 
1662 force its evaluation and then :print to recover its type
1663
1664 qsort2.hs:2:15-46> :t x
1665 x :: GHC.Base.Unknown
1666 qsort2.hs:2:15-46> :p x <co id="seq4"/>
1667 x - 10
1668 qsort2.hs:2:15-46> :t x <co id="seq5"/>
1669 x :: Int
1670 </programlisting>
1671 </example>
1672       <calloutlist>
1673         <callout arearefs="seq1">
1674           <para>GHCi reminds us that this value is untyped, and instructs us to force its evaluation </para>
1675         </callout>
1676         <callout arearefs="seq2">
1677           <para>This line forces the evaluation of <literal>x</literal> </para>
1678         </callout>
1679         <callout arearefs="seq3">
1680           <para>Even though x has been evaluated, we cannot simply use its name to see its value! 
1681           This is a bit counterintuitive, but currently in GHCi the type of a binding
1682           cannot be a type variable <literal>a</literal>. 
1683           Thus, the binding <literal>x</literal> gets assigned the concrete type Unknown.</para>
1684         </callout>
1685         <callout arearefs="seq4">
1686           <para>We can explore <literal>x</literal> using the <literal>:print</literal> 
1687           command, which does find out that <literal>x</literal> is of type Int and prints
1688           its value accordingly.</para>
1689         </callout>
1690         <callout arearefs="seq5">
1691            <para><literal>:print</literal> also updates the type of <literal>x</literal> with
1692            the most concrete type information available.</para>
1693         </callout>
1694       </calloutlist>
1695       The example shows the standard way to proceeed with polymorphic values in a breakpoint. 
1696       </para>
1697     </sect2>
1698     <sect2><title>Commands</title>
1699     <para>Breakpoints can be set in several ways using the <literal>:breakpoint</literal> command. Note that you can take advantage of the command abbreviation feature of GHCi and use simply <literal>:bre</literal> to save quite a few keystrokes.
1700 <variablelist>
1701 <varlistentry>
1702   <term>
1703     <literal>:breakpoint add <replaceable>module</replaceable> <replaceable>line</replaceable></literal>
1704   </term>
1705   <listitem><para>
1706     Adds a breakpoint at the first event found at line <literal>line</literal> in <literal>module</literal>, if any.
1707   </para></listitem>
1708 </varlistentry>
1709 <varlistentry>
1710   <term>
1711     <literal>:breakpoint add <replaceable>module</replaceable> <replaceable>line</replaceable> <replaceable>column</replaceable></literal>
1712   </term>
1713   <listitem><para>
1714     Adds a breakpoint at the first event found after column <literal>column</literal>
1715     at line <literal>line</literal> in <literal>module</literal>, if any.
1716   </para></listitem>
1717 </varlistentry>
1718
1719 <varlistentry>
1720   <term>
1721     <literal>:breakpoint continue</literal>
1722   </term>
1723   <listitem><para>
1724    When at a breakpoint, continue execution up to the next breakpoint
1725    or end of evaluation.
1726   </para></listitem>
1727 </varlistentry>
1728
1729 <varlistentry>
1730   <term>
1731     <literal>:continue</literal>
1732   </term>
1733   <listitem><para>
1734       Shortcut for <literal>:breakpoint continue</literal>
1735   </para></listitem>
1736 </varlistentry>
1737
1738 <varlistentry>
1739   <term>
1740     <literal>:breakpoint list</literal>
1741   </term>
1742   <listitem><para>
1743     Lists the currently set up breakpoints.
1744   </para></listitem>
1745 </varlistentry>
1746 <varlistentry>
1747   <term>
1748     <literal>:breakpoint del <replaceable>num</replaceable></literal>
1749   </term>
1750   <listitem><para>
1751     Deletes the breakpoint at position <literal>num</literal> in the list of
1752     breakpoints shown by <literal>:breakpoint list</literal>.
1753   </para></listitem>
1754 </varlistentry>
1755 <varlistentry>
1756   <term>
1757     <literal>:breakpoint del <replaceable>module</replaceable> <replaceable>line</replaceable></literal>
1758   </term>
1759   <listitem><para>
1760   Dels the breakpoint at line <literal>line</literal> in <literal>module</literal>, if any.
1761   </para></listitem>
1762 </varlistentry>
1763 <varlistentry>
1764   <term>
1765     <literal>:breakpoint del <replaceable>module</replaceable> <replaceable>line</replaceable><replaceable>col</replaceable> </literal>
1766   </term>
1767   <listitem><para>
1768     Deletes the first breakpoint found after column <literal>column</literal>
1769     at line <literal>line</literal> in <literal>module</literal>, if any.
1770   </para></listitem>
1771 </varlistentry>
1772 <varlistentry>
1773   <term>
1774     <literal>:breakpoint stop </literal>
1775   </term>
1776   <listitem><para>
1777     Stop the program being executed. This interrupts a debugging session
1778     and returns to the top level.
1779   </para></listitem>
1780 </varlistentry>
1781 </variablelist></para>
1782     </sect2>
1783     <sect2><title>Limitations</title>
1784      <para>
1785       <itemizedlist>
1786         <listitem><para>
1787           Implicit parameters (see <xref linkend="implicit-parameters"/>) are only available 
1788           at the scope of a breakpoint if there is a explicit type signature.
1789         </para></listitem>
1790       </itemizedlist>
1791       <itemizedlist>
1792         <listitem><para>
1793           Modules compiled by GHCi under the <literal>-fdebugging
1794         </literal> flag  will perform slower: the debugging mode introduces some overhead.
1795       Modules compiled to object code by ghc are not affected.
1796         </para></listitem>
1797       </itemizedlist>      
1798      </para>
1799     </sect2>
1800     <sect2><title>Tips</title>
1801       <variablelist>
1802         <varlistentry><term>* Use PRAGMAs to fine tune which modules are loaded under debugging mode</term>
1803           <listitem>
1804             <programlisting>{-# OPTIONS_GHC -fdebugging #-}</programlisting>
1805           </listitem>
1806         </varlistentry>
1807         <varlistentry> <term>* Repeated use of <literal>seq</literal> and 
1808             <literal>:print</literal> may be necessary to observe unevaluated
1809             untyped bindings</term> 
1810           <listitem><para>see <xref linkend="debuggingEx"/> 
1811           </para></listitem>
1812         </varlistentry>
1813         <varlistentry> <term> * <literal>GHC.Exts.unsafeCoerce</literal> can help if you are positive about the type of a binding</term> 
1814           <listitem><para><programlisting>
1815 type MyLongType a = [Maybe [Maybe a]]
1816
1817 main:Main> :m +GHC.Exts
1818 main:Main> main
1819 Local bindings in scope:
1820   x :: a
1821 Main.hs:15> let x' = unsafeCoerce x :: MyLongType Bool
1822 Main.hs:15> x'
1823 [Just [Just False, Just True]]
1824           </programlisting>
1825            Note that a wrong coercion will likely result in your debugging session being interrupted by a segmentation fault 
1826           </para></listitem>
1827         </varlistentry>
1828         <varlistentry> <term> * The undocumented (and unsupported) &colon;force command </term>
1829           <listitem><para> 
1830               equivalent to <literal> :print</literal> with automatic 
1831               <literal>seq</literal> forcing, 
1832               may prove useful to replace sequences of <literal>seq</literal> and 
1833               <literal>&colon;print</literal> in some situations. 
1834           </para></listitem>
1835         </varlistentry> 
1836     </variablelist>
1837     </sect2></sect1>
1838   <sect1 id="ghci-dot-files">
1839     <title>The <filename>.ghci</filename> file</title>
1840     <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
1841     </indexterm>
1842     <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
1843     </indexterm>
1844
1845     <para>When it starts, GHCi always reads and executes commands from
1846     <filename>$HOME/.ghci</filename>, followed by
1847     <filename>./.ghci</filename>.</para>
1848
1849     <para>The <filename>.ghci</filename> in your home directory is
1850     most useful for turning on favourite options (eg. <literal>:set
1851     +s</literal>), and defining useful macros.  Placing a
1852     <filename>.ghci</filename> file in a directory with a Haskell
1853     project is a useful way to set certain project-wide options so you
1854     don't have to type them everytime you start GHCi: eg. if your
1855     project uses GHC extensions and CPP, and has source files in three
1856     subdirectories A B and C, you might put the following lines in
1857     <filename>.ghci</filename>:</para>
1858
1859 <screen>
1860 :set -fglasgow-exts -cpp
1861 :set -iA:B:C
1862 </screen>
1863
1864     <para>(Note that strictly speaking the <option>-i</option> flag is
1865     a static one, but in fact it works to set it using
1866     <literal>:set</literal> like this.  The changes won't take effect
1867     until the next <literal>:load</literal>, though.)</para>
1868
1869     <para>Two command-line options control whether the
1870     <filename>.ghci</filename> files are read:</para>
1871
1872     <variablelist>
1873       <varlistentry>
1874         <term>
1875           <option>-ignore-dot-ghci</option>
1876           <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
1877         </term>
1878         <listitem>
1879           <para>Don't read either <filename>./.ghci</filename> or
1880           <filename>$HOME/.ghci</filename> when starting up.</para>
1881         </listitem>
1882       </varlistentry>
1883       <varlistentry>
1884         <term>
1885           <option>-read-dot-ghci</option>
1886           <indexterm><primary><option>-read-dot-ghci</option></primary></indexterm>
1887         </term>
1888         <listitem>
1889           <para>Read <filename>.ghci</filename> and
1890           <filename>$HOME/.ghci</filename>.  This is normally the
1891           default, but the <option>-read-dot-ghci</option> option may
1892           be used to override a previous
1893           <option>-ignore-dot-ghci</option> option.</para>
1894         </listitem>
1895       </varlistentry>
1896     </variablelist>
1897
1898   </sect1>
1899
1900   <sect1 id="ghci-faq">
1901     <title>FAQ and Things To Watch Out For</title>
1902     
1903     <variablelist>
1904       <varlistentry>
1905         <term>The interpreter can't load modules with foreign export
1906         declarations!</term>
1907         <listitem>
1908           <para>Unfortunately not.  We haven't implemented it yet.
1909           Please compile any offending modules by hand before loading
1910           them into GHCi.</para>
1911         </listitem>
1912       </varlistentry>
1913
1914       <varlistentry>
1915         <term>
1916           <literal>-O</literal> doesn't work with GHCi!
1917           <indexterm><primary><option>-O</option></primary></indexterm>
1918          </term>
1919         <listitem>
1920           <para>For technical reasons, the bytecode compiler doesn't
1921           interact well with one of the optimisation passes, so we
1922           have disabled optimisation when using the interpreter.  This
1923           isn't a great loss: you'll get a much bigger win by
1924           compiling the bits of your code that need to go fast, rather
1925           than interpreting them with optimisation turned on.</para>
1926         </listitem>
1927       </varlistentry>
1928
1929       <varlistentry>
1930         <term>Unboxed tuples don't work with GHCi</term>
1931         <listitem>
1932           <para>That's right.  You can always compile a module that
1933           uses unboxed tuples and load it into GHCi, however.
1934           (Incidentally the previous point, namely that
1935           <literal>-O</literal> is incompatible with GHCi, is because
1936           the bytecode compiler can't deal with unboxed
1937           tuples).</para>
1938         </listitem>
1939       </varlistentry>
1940
1941       <varlistentry>
1942         <term>Concurrent threads don't carry on running when GHCi is
1943         waiting for input.</term>
1944         <listitem>
1945           <para>This should work, as long as your GHCi was built with
1946           the <option>-threaded</option> switch, which is the default.
1947           Consult whoever supplied your GHCi installation.</para>
1948         </listitem>
1949       </varlistentry>
1950
1951       <varlistentry>
1952         <term>After using <literal>getContents</literal>, I can't use
1953         <literal>stdin</literal> again until I do
1954         <literal>:load</literal> or <literal>:reload</literal>.</term>
1955
1956         <listitem>
1957           <para>This is the defined behaviour of
1958           <literal>getContents</literal>: it puts the stdin Handle in
1959           a state known as <firstterm>semi-closed</firstterm>, wherein
1960           any further I/O operations on it are forbidden.  Because I/O
1961           state is retained between computations, the semi-closed
1962           state persists until the next <literal>:load</literal> or
1963           <literal>:reload</literal> command.</para>
1964
1965           <para>You can make <literal>stdin</literal> reset itself
1966           after every evaluation by giving GHCi the command
1967           <literal>:set +r</literal>.  This works because
1968           <literal>stdin</literal> is just a top-level expression that
1969           can be reverted to its unevaluated state in the same way as
1970           any other top-level expression (CAF).</para>
1971         </listitem>
1972       </varlistentry>
1973
1974       <varlistentry>
1975         <term>I can't use Control-C to interrupt computations in
1976           GHCi on Windows.</term>
1977         <listitem>
1978           <para>See <xref linkend="ghci-windows"/></para>
1979         </listitem>
1980       </varlistentry>
1981     </variablelist>
1982   </sect1>
1983
1984 </chapter>
1985
1986 <!-- Emacs stuff:
1987      ;;; Local Variables: ***
1988      ;;; mode: xml ***
1989      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
1990      ;;; End: ***
1991  -->