xref to the docs for +RTS -xc
[ghc.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.
18 <indexterm><primary>FFI</primary><secondary>GHCi support</secondary></indexterm>
19 <indexterm><primary>Foreign Function
20 Interface</primary><secondary>GHCi support</secondary></indexterm>.
21 GHCi also includes an interactive debugger (see <xref linkend="ghci-debugger"/>)..</para>
22
23 <sect1 id="ghci-introduction">
24 <title>Introduction to GHCi</title>
25
26 <para>Let's start with an example GHCi session. You can fire up
27 GHCi with the command <literal>ghci</literal>:</para>
28
29 <screen>
30 $ ghci
31 ___ ___ _
32 / _ \ /\ /\/ __(_)
33 / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98.
34 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
35 \____/\/ /_/\____/|_| Type :? for help.
36
37 Loading package base ... linking ... done.
38 Prelude>
39 </screen>
40
41 <para>There may be a short pause while GHCi loads the prelude and
42 standard libraries, after which the prompt is shown. If we follow
43 the instructions and type <literal>:?</literal> for help, we
44 get:</para>
45
46 <screen>
47 Commands available from the prompt:
48
49 &lt;stmt&gt; evaluate/run &lt;stmt&gt;
50 :add &lt;filename&gt; ... add module(s) to the current target set
51 :browse [*]&lt;module&gt; display the names defined by &lt;module&gt;
52 :cd &lt;dir&gt; change directory to &lt;dir&gt;
53 :def &lt;cmd&gt; &lt;expr&gt; define a command :&lt;cmd&gt;
54 :edit &lt;file&gt; edit file
55 :edit edit last module
56 :help, :? display this list of commands
57 :info [&lt;name&gt; ...] display information about the given names
58 :load &lt;filename&gt; ... load module(s) and their dependents
59 :module [+/-] [*]&lt;mod&gt; ... set the context for expression evaluation
60 :main [&lt;arguments&gt; ...] run the main function with the given arguments
61 :reload reload the current module set
62
63 :set &lt;option&gt; ... set options
64 :set args &lt;arg&gt; ... set the arguments returned by System.getArgs
65 :set prog &lt;progname&gt; set the value returned by System.getProgName
66 :set prompt &lt;prompt&gt; set the prompt used in GHCi
67 :set editor &lt;cmd&gt; set the command used for :edit
68
69 :show modules show the currently loaded modules
70 :show bindings show the current bindings made at the prompt
71
72 :ctags [&lt;file&gt;] create tags file for Vi (default: "tags")
73 :etags [&lt;file&gt;] create tags file for Emacs (default: "TAGS")
74 :type &lt;expr&gt; show the type of &lt;expr&gt;
75 :kind &lt;type&gt; show the kind of &lt;type&gt;
76 :undef &lt;cmd&gt; undefine user-defined command :&lt;cmd&gt;
77 :unset &lt;option&gt; ... unset options
78 :quit exit GHCi
79 :!&lt;command&gt; run the shell command &lt;command&gt;
80
81 Options for ':set' and ':unset':
82
83 +r revert top-level expressions after each evaluation
84 +s print timing/memory stats after each evaluation
85 +t print type after evaluation
86 -&lt;flags&gt; most GHC command line flags can also be set here
87 (eg. -v2, -fglasgow-exts, etc.)
88 </screen>
89
90 <para>We'll explain most of these commands as we go along. For
91 Hugs users: many things work the same as in Hugs, so you should be
92 able to get going straight away.</para>
93
94 <para>Haskell expressions can be typed at the prompt:</para>
95 <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
96 </indexterm>
97
98 <screen>
99 Prelude> 1+2
100 3
101 Prelude> let x = 42 in x / 9
102 4.666666666666667
103 Prelude>
104 </screen>
105
106 <para>GHCi interprets the whole line as an expression to evaluate.
107 The expression may not span several lines - as soon as you press
108 enter, GHCi will attempt to evaluate it.</para>
109 </sect1>
110
111 <sect1 id="loading-source-files">
112 <title>Loading source files</title>
113
114 <para>Suppose we have the following Haskell source code, which we
115 place in a file <filename>Main.hs</filename>:</para>
116
117 <programlisting>
118 main = print (fac 20)
119
120 fac 0 = 1
121 fac n = n * fac (n-1)
122 </programlisting>
123
124 <para>You can save <filename>Main.hs</filename> anywhere you like,
125 but if you save it somewhere other than the current
126 directory<footnote><para>If you started up GHCi from the command
127 line then GHCi's current directory is the same as the current
128 directory of the shell from which it was started. If you started
129 GHCi from the &ldquo;Start&rdquo; menu in Windows, then the
130 current directory is probably something like
131 <filename>C:\Documents and Settings\<replaceable>user
132 name</replaceable></filename>.</para> </footnote> then we will
133 need to change to the right directory in GHCi:</para>
134
135 <screen>
136 Prelude> :cd <replaceable>dir</replaceable>
137 </screen>
138
139 <para>where <replaceable>dir</replaceable> is the directory (or
140 folder) in which you saved <filename>Main.hs</filename>.</para>
141
142 <para>To load a Haskell source file into GHCi, use the
143 <literal>:load</literal> command:</para>
144 <indexterm><primary><literal>:load</literal></primary></indexterm>
145
146 <screen>
147 Prelude> :load Main
148 Compiling Main ( Main.hs, interpreted )
149 Ok, modules loaded: Main.
150 *Main>
151 </screen>
152
153 <para>GHCi has loaded the <literal>Main</literal> module, and the
154 prompt has changed to &ldquo;<literal>*Main></literal>&rdquo; to
155 indicate that the current context for expressions typed at the
156 prompt is the <literal>Main</literal> module we just loaded (we'll
157 explain what the <literal>*</literal> means later in <xref
158 linkend="ghci-scope"/>). So we can now type expressions involving
159 the functions from <filename>Main.hs</filename>:</para>
160
161 <screen>
162 *Main> fac 17
163 355687428096000
164 </screen>
165
166 <para>Loading a multi-module program is just as straightforward;
167 just give the name of the &ldquo;topmost&rdquo; module to the
168 <literal>:load</literal> command (hint: <literal>:load</literal>
169 can be abbreviated to <literal>:l</literal>). The topmost module
170 will normally be <literal>Main</literal>, but it doesn't have to
171 be. GHCi will discover which modules are required, directly or
172 indirectly, by the topmost module, and load them all in dependency
173 order.</para>
174
175 <sect2 id="ghci-modules-filenames">
176 <title>Modules vs. filenames</title>
177 <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
178 <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
179
180 <para>Question: How does GHC find the filename which contains
181 module <replaceable>M</replaceable>? Answer: it looks for the
182 file <literal><replaceable>M</replaceable>.hs</literal>, or
183 <literal><replaceable>M</replaceable>.lhs</literal>. This means
184 that for most modules, the module name must match the filename.
185 If it doesn't, GHCi won't be able to find it.</para>
186
187 <para>There is one exception to this general rule: when you load
188 a program with <literal>:load</literal>, or specify it when you
189 invoke <literal>ghci</literal>, you can give a filename rather
190 than a module name. This filename is loaded if it exists, and
191 it may contain any module you like. This is particularly
192 convenient if you have several <literal>Main</literal> modules
193 in the same directory and you can't call them all
194 <filename>Main.hs</filename>.</para>
195
196 <para>The search path for finding source files is specified with
197 the <option>-i</option> option on the GHCi command line, like
198 so:</para>
199 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
200
201 <para>or it can be set using the <literal>:set</literal> command
202 from within GHCi (see <xref
203 linkend="ghci-cmd-line-options"/>)<footnote><para>Note that in
204 GHCi, and <option>&ndash;&ndash;make</option> mode, the <option>-i</option>
205 option is used to specify the search path for
206 <emphasis>source</emphasis> files, whereas in standard
207 batch-compilation mode the <option>-i</option> option is used to
208 specify the search path for interface files, see <xref
209 linkend="search-path"/>.</para> </footnote></para>
210
211 <para>One consequence of the way that GHCi follows dependencies
212 to find modules to load is that every module must have a source
213 file. The only exception to the rule is modules that come from
214 a package, including the <literal>Prelude</literal> and standard
215 libraries such as <literal>IO</literal> and
216 <literal>Complex</literal>. If you attempt to load a module for
217 which GHCi can't find a source file, even if there are object
218 and interface files for the module, you'll get an error
219 message.</para>
220 </sect2>
221
222 <sect2>
223 <title>Making changes and recompilation</title>
224 <indexterm><primary><literal>:reload</literal></primary></indexterm>
225
226 <para>If you make some changes to the source code and want GHCi
227 to recompile the program, give the <literal>:reload</literal>
228 command. The program will be recompiled as necessary, with GHCi
229 doing its best to avoid actually recompiling modules if their
230 external dependencies haven't changed. This is the same
231 mechanism we use to avoid re-compiling modules in the batch
232 compilation setting (see <xref linkend="recomp"/>).</para>
233 </sect2>
234 </sect1>
235
236 <sect1 id="ghci-compiled">
237 <title>Loading compiled code</title>
238 <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
239
240 <para>When you load a Haskell source module into GHCi, it is
241 normally converted to byte-code and run using the interpreter.
242 However, interpreted code can also run alongside compiled code in
243 GHCi; indeed, normally when GHCi starts, it loads up a compiled
244 copy of the <literal>base</literal> package, which contains the
245 <literal>Prelude</literal>.</para>
246
247 <para>Why should we want to run compiled code? Well, compiled
248 code is roughly 10x faster than interpreted code, but takes about
249 2x longer to produce (perhaps longer if optimisation is on). So
250 it pays to compile the parts of a program that aren't changing
251 very often, and use the interpreter for the code being actively
252 developed.</para>
253
254 <para>When loading up source files with <literal>:load</literal>,
255 GHCi looks for any corresponding compiled object files, and will
256 use one in preference to interpreting the source if possible. For
257 example, suppose we have a 4-module program consisting of modules
258 A, B, C, and D. Modules B and C both import D only,
259 and A imports both B &amp; C:</para>
260 <screen>
261 A
262 / \
263 B C
264 \ /
265 D
266 </screen>
267 <para>We can compile D, then load the whole program, like this:</para>
268 <screen>
269 Prelude> :! ghc -c D.hs
270 Prelude> :load A
271 Skipping D ( D.hs, D.o )
272 Compiling C ( C.hs, interpreted )
273 Compiling B ( B.hs, interpreted )
274 Compiling A ( A.hs, interpreted )
275 Ok, modules loaded: A, B, C, D.
276 *Main>
277 </screen>
278
279 <para>In the messages from the compiler, we see that it skipped D,
280 and used the object file <filename>D.o</filename>. The message
281 <literal>Skipping</literal> <replaceable>module</replaceable>
282 indicates that compilation for <replaceable>module</replaceable>
283 isn't necessary, because the source and everything it depends on
284 is unchanged since the last compilation.</para>
285
286 <para>At any time you can use the command
287 <literal>:show modules</literal>
288 to get a list of the modules currently loaded
289 into GHCi:</para>
290
291 <screen>
292 *Main> :show modules
293 D ( D.hs, D.o )
294 C ( C.hs, interpreted )
295 B ( B.hs, interpreted )
296 A ( A.hs, interpreted )
297 *Main></screen>
298
299 <para>If we now modify the source of D (or pretend to: using Unix
300 command <literal>touch</literal> on the source file is handy for
301 this), the compiler will no longer be able to use the object file,
302 because it might be out of date:</para>
303
304 <screen>
305 *Main> :! touch D.hs
306 *Main> :reload
307 Compiling D ( D.hs, interpreted )
308 Skipping C ( C.hs, interpreted )
309 Skipping B ( B.hs, interpreted )
310 Skipping A ( A.hs, interpreted )
311 Ok, modules loaded: A, B, C, D.
312 *Main>
313 </screen>
314
315 <para>Note that module D was compiled, but in this instance
316 because its source hadn't really changed, its interface remained
317 the same, and the recompilation checker determined that A, B and C
318 didn't need to be recompiled.</para>
319
320 <para>So let's try compiling one of the other modules:</para>
321
322 <screen>
323 *Main> :! ghc -c C.hs
324 *Main> :load A
325 Compiling D ( D.hs, interpreted )
326 Compiling C ( C.hs, interpreted )
327 Compiling B ( B.hs, interpreted )
328 Compiling A ( A.hs, interpreted )
329 Ok, modules loaded: A, B, C, D.
330 </screen>
331
332 <para>We didn't get the compiled version of C! What happened?
333 Well, in GHCi a compiled module may only depend on other compiled
334 modules, and in this case C depends on D, which doesn't have an
335 object file, so GHCi also rejected C's object file. Ok, so let's
336 also compile D:</para>
337
338 <screen>
339 *Main> :! ghc -c D.hs
340 *Main> :reload
341 Ok, modules loaded: A, B, C, D.
342 </screen>
343
344 <para>Nothing happened! Here's another lesson: newly compiled
345 modules aren't picked up by <literal>:reload</literal>, only
346 <literal>:load</literal>:</para>
347
348 <screen>
349 *Main> :load A
350 Skipping D ( D.hs, D.o )
351 Skipping C ( C.hs, C.o )
352 Compiling B ( B.hs, interpreted )
353 Compiling A ( A.hs, interpreted )
354 Ok, modules loaded: A, B, C, D.
355 </screen>
356
357 <para>HINT: since GHCi will only use a compiled object file if it
358 can be sure that the compiled version is up-to-date, a good technique
359 when working on a large program is to occasionally run
360 <literal>ghc &ndash;&ndash;make</literal> to compile the whole project (say
361 before you go for lunch :-), then continue working in the
362 interpreter. As you modify code, the new modules will be
363 interpreted, but the rest of the project will remain
364 compiled.</para>
365
366 </sect1>
367
368 <sect1 id="interactive-evaluation">
369 <title>Interactive evaluation at the prompt</title>
370
371 <para>When you type an expression at the prompt, GHCi immediately
372 evaluates and prints the result:
373 <screen>
374 Prelude> reverse "hello"
375 "olleh"
376 Prelude> 5+5
377 10
378 </screen>
379 </para>
380
381 <sect2><title>I/O actions at the prompt</title>
382
383 <para>GHCi does more than simple expression evaluation at the prompt.
384 If you type something of type <literal>IO a</literal> for some
385 <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
386 as an IO-computation.
387 <screen>
388 Prelude> "hello"
389 "hello"
390 Prelude> putStrLn "hello"
391 hello
392 </screen>
393 Furthermore, GHCi will print the result of the I/O action if (and only
394 if):
395 <itemizedlist>
396 <listitem><para>The result type is an instance of <literal>Show</literal>.</para></listitem>
397 <listitem><para>The result type is not
398 <literal>()</literal>.</para></listitem>
399 </itemizedlist>
400 For example, remembering that <literal>putStrLn :: String -> IO ()</literal>:
401 <screen>
402 Prelude> putStrLn "hello"
403 hello
404 Prelude> do { putStrLn "hello"; return "yes" }
405 hello
406 "yes"
407 </screen>
408 </para></sect2>
409
410 <sect2 id="ghci-stmts">
411 <title>Using <literal>do-</literal>notation at the prompt</title>
412 <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
413 <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
414
415 <para>GHCi actually accepts <firstterm>statements</firstterm>
416 rather than just expressions at the prompt. This means you can
417 bind values and functions to names, and use them in future
418 expressions or statements.</para>
419
420 <para>The syntax of a statement accepted at the GHCi prompt is
421 exactly the same as the syntax of a statement in a Haskell
422 <literal>do</literal> expression. However, there's no monad
423 overloading here: statements typed at the prompt must be in the
424 <literal>IO</literal> monad.
425 <screen>
426 Prelude> x &lt;- return 42
427 42
428 Prelude> print x
429 42
430 Prelude>
431 </screen>
432 The statement <literal>x &lt;- return 42</literal> means
433 &ldquo;execute <literal>return 42</literal> in the
434 <literal>IO</literal> monad, and bind the result to
435 <literal>x</literal>&rdquo;. We can then use
436 <literal>x</literal> in future statements, for example to print
437 it as we did above.</para>
438
439 <para>GHCi will print the result of a statement if and only if:
440 <itemizedlist>
441 <listitem>
442 <para>The statement is not a binding, or it is a monadic binding
443 (<literal>p &lt;- e</literal>) that binds exactly one
444 variable.</para>
445 </listitem>
446 <listitem>
447 <para>The variable's type is not polymorphic, is not
448 <literal>()</literal>, and is an instance of
449 <literal>Show</literal></para>
450 </listitem>
451 </itemizedlist>
452 The automatic printing of binding results can be supressed with
453 <option>:set -fno-print-bind-result</option> (this does not
454 supress printing the result of non-binding statements).
455 <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
456 You might want to do this to prevent the result of binding
457 statements from being fully evaluated by the act of printing
458 them, for example.</para>
459
460 <para>Of course, you can also bind normal non-IO expressions
461 using the <literal>let</literal>-statement:</para>
462 <screen>
463 Prelude> let x = 42
464 Prelude> x
465 42
466 Prelude>
467 </screen>
468 <para>Another important difference between the two types of binding
469 is that the monadic bind (<literal>p &lt;- e</literal>) is
470 <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
471 whereas with the <literal>let</literal> form, the expression
472 isn't evaluated immediately:</para>
473 <screen>
474 Prelude> let x = error "help!"
475 Prelude> print x
476 *** Exception: help!
477 Prelude>
478 </screen>
479
480 <para>Note that <literal>let</literal> bindings do not automatically
481 print the value bound, unlike monadic bindings.</para>
482
483 <para>Any exceptions raised during the evaluation or execution
484 of the statement are caught and printed by the GHCi command line
485 interface (for more information on exceptions, see the module
486 <literal>Control.Exception</literal> in the libraries
487 documentation).</para>
488
489 <para>Every new binding shadows any existing bindings of the
490 same name, including entities that are in scope in the current
491 module context.</para>
492
493 <para>WARNING: temporary bindings introduced at the prompt only
494 last until the next <literal>:load</literal> or
495 <literal>:reload</literal> command, at which time they will be
496 simply lost. However, they do survive a change of context with
497 <literal>:module</literal>: the temporary bindings just move to
498 the new location.</para>
499
500 <para>HINT: To get a list of the bindings currently in scope, use the
501 <literal>:show bindings</literal> command:</para>
502
503 <screen>
504 Prelude> :show bindings
505 x :: Int
506 Prelude></screen>
507
508 <para>HINT: if you turn on the <literal>+t</literal> option,
509 GHCi will show the type of each variable bound by a statement.
510 For example:</para>
511 <indexterm><primary><literal>+t</literal></primary></indexterm>
512 <screen>
513 Prelude> :set +t
514 Prelude> let (x:xs) = [1..]
515 x :: Integer
516 xs :: [Integer]
517 </screen>
518
519 </sect2>
520
521 <sect2 id="ghci-scope">
522 <title>What's really in scope at the prompt?</title>
523
524 <para>When you type an expression at the prompt, what
525 identifiers and types are in scope? GHCi provides a flexible
526 way to control exactly how the context for an expression is
527 constructed. Let's start with the simple cases; when you start
528 GHCi the prompt looks like this:</para>
529
530 <screen>Prelude></screen>
531
532 <para>Which indicates that everything from the module
533 <literal>Prelude</literal> is currently in scope. If we now
534 load a file into GHCi, the prompt will change:</para>
535
536 <screen>
537 Prelude> :load Main.hs
538 Compiling Main ( Main.hs, interpreted )
539 *Main>
540 </screen>
541
542 <para>The new prompt is <literal>*Main</literal>, which
543 indicates that we are typing expressions in the context of the
544 top-level of the <literal>Main</literal> module. Everything
545 that is in scope at the top-level in the module
546 <literal>Main</literal> we just loaded is also in scope at the
547 prompt (probably including <literal>Prelude</literal>, as long
548 as <literal>Main</literal> doesn't explicitly hide it).</para>
549
550 <para>The syntax
551 <literal>*<replaceable>module</replaceable></literal> indicates
552 that it is the full top-level scope of
553 <replaceable>module</replaceable> that is contributing to the
554 scope for expressions typed at the prompt. Without the
555 <literal>*</literal>, just the exports of the module are
556 visible.</para>
557
558 <para>We're not limited to a single module: GHCi can combine
559 scopes from multiple modules, in any mixture of
560 <literal>*</literal> and non-<literal>*</literal> forms. GHCi
561 combines the scopes from all of these modules to form the scope
562 that is in effect at the prompt. For technical reasons, GHCi
563 can only support the <literal>*</literal>-form for modules which
564 are interpreted, so compiled modules and package modules can
565 only contribute their exports to the current scope.</para>
566
567 <para>The scope is manipulated using the
568 <literal>:module</literal> command. For example, if the current
569 scope is <literal>Prelude</literal>, then we can bring into
570 scope the exports from the module <literal>IO</literal> like
571 so:</para>
572
573 <screen>
574 Prelude> :module +IO
575 Prelude IO> hPutStrLn stdout "hello\n"
576 hello
577 Prelude IO>
578 </screen>
579
580 <para>(Note: <literal>:module</literal> can be shortened to
581 <literal>:m</literal>). The full syntax of the
582 <literal>:module</literal> command is:</para>
583
584 <screen>
585 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
586 </screen>
587
588 <para>Using the <literal>+</literal> form of the
589 <literal>module</literal> commands adds modules to the current
590 scope, and <literal>-</literal> removes them. Without either
591 <literal>+</literal> or <literal>-</literal>, the current scope
592 is replaced by the set of modules specified. Note that if you
593 use this form and leave out <literal>Prelude</literal>, GHCi
594 will assume that you really wanted the
595 <literal>Prelude</literal> and add it in for you (if you don't
596 want the <literal>Prelude</literal>, then ask to remove it with
597 <literal>:m -Prelude</literal>).</para>
598
599 <para>The scope is automatically set after a
600 <literal>:load</literal> command, to the most recently loaded
601 "target" module, in a <literal>*</literal>-form if possible.
602 For example, if you say <literal>:load foo.hs bar.hs</literal>
603 and <filename>bar.hs</filename> contains module
604 <literal>Bar</literal>, then the scope will be set to
605 <literal>*Bar</literal> if <literal>Bar</literal> is
606 interpreted, or if <literal>Bar</literal> is compiled it will be
607 set to <literal>Prelude Bar</literal> (GHCi automatically adds
608 <literal>Prelude</literal> if it isn't present and there aren't
609 any <literal>*</literal>-form modules).</para>
610
611 <para>With multiple modules in scope, especially multiple
612 <literal>*</literal>-form modules, it is likely that name
613 clashes will occur. Haskell specifies that name clashes are
614 only reported when an ambiguous identifier is used, and GHCi
615 behaves in the same way for expressions typed at the
616 prompt.</para>
617
618 <para>
619 Hint: GHCi will tab-complete names that are in scope; for
620 example, if you run GHCi and type <literal>J&lt;tab&gt;</literal>
621 then GHCi will expand it to <literal>Just </literal>.
622 </para>
623
624 <sect3>
625 <title>Qualified names</title>
626
627 <para>To make life slightly easier, the GHCi prompt also
628 behaves as if there is an implicit <literal>import
629 qualified</literal> declaration for every module in every
630 package, and every module currently loaded into GHCi.</para>
631 </sect3>
632
633 <sect3>
634 <title>The <literal>:main</literal> command</title>
635
636 <para>
637 When a program is compiled and executed, it can use the
638 <literal>getArgs</literal> function to access the
639 command-line arguments.
640 However, we cannot simply pass the arguments to the
641 <literal>main</literal> function while we are testing in ghci,
642 as the <literal>main</literal> function doesn't take its
643 directly.
644 </para>
645
646 <para>
647 Instead, we can use the <literal>:main</literal> command.
648 This runs whatever <literal>main</literal> is in scope, with
649 any arguments being treated the same as command-line arguments,
650 e.g.:
651 </para>
652
653 <screen>
654 Prelude> let main = System.Environment.getArgs >>= print
655 Prelude> :main foo bar
656 ["foo","bar"]
657 </screen>
658
659 </sect3>
660 </sect2>
661
662
663 <sect2>
664 <title>The <literal>it</literal> variable</title>
665 <indexterm><primary><literal>it</literal></primary>
666 </indexterm>
667
668 <para>Whenever an expression (or a non-binding statement, to be
669 precise) is typed at the prompt, GHCi implicitly binds its value
670 to the variable <literal>it</literal>. For example:</para>
671 <screen>
672 Prelude> 1+2
673 3
674 Prelude> it * 2
675 6
676 </screen>
677 <para>What actually happens is that GHCi typechecks the
678 expression, and if it doesn't have an <literal>IO</literal> type,
679 then it transforms it as follows: an expression
680 <replaceable>e</replaceable> turns into
681 <screen>
682 let it = <replaceable>e</replaceable>;
683 print it
684 </screen>
685 which is then run as an IO-action.</para>
686
687 <para>Hence, the original expression must have a type which is an
688 instance of the <literal>Show</literal> class, or GHCi will
689 complain:</para>
690
691 <screen>
692 Prelude&gt; id
693
694 &lt;interactive&gt;:1:0:
695 No instance for (Show (a -&gt; a))
696 arising from use of `print' at &lt;interactive&gt;:1:0-1
697 Possible fix: add an instance declaration for (Show (a -> a))
698 In the expression: print it
699 In a 'do' expression: print it
700 </screen>
701
702 <para>The error message contains some clues as to the
703 transformation happening internally.</para>
704
705 <para>If the expression was instead of type <literal>IO a</literal> for
706 some <literal>a</literal>, then <literal>it</literal> will be
707 bound to the result of the <literal>IO</literal> computation,
708 which is of type <literal>a</literal>. eg.:</para>
709 <screen>
710 Prelude> Time.getClockTime
711 Wed Mar 14 12:23:13 GMT 2001
712 Prelude> print it
713 Wed Mar 14 12:23:13 GMT 2001
714 </screen>
715
716 <para>The corresponding translation for an IO-typed
717 <replaceable>e</replaceable> is
718 <screen>
719 it &lt;- <replaceable>e</replaceable>
720 </screen>
721 </para>
722
723 <para>Note that <literal>it</literal> is shadowed by the new
724 value each time you evaluate a new expression, and the old value
725 of <literal>it</literal> is lost.</para>
726
727 </sect2>
728
729 <sect2 id="extended-default-rules">
730 <title>Type defaulting in GHCi</title>
731 <indexterm><primary>Type default</primary></indexterm>
732 <indexterm><primary><literal>Show</literal> class</primary></indexterm>
733 <para>
734 Consider this GHCi session:
735 <programlisting>
736 ghci> reverse []
737 </programlisting>
738 What should GHCi do? Strictly speaking, the program is ambiguous. <literal>show (reverse [])</literal>
739 (which is what GHCi computes here) has type <literal>Show a => a</literal> and how that displays depends
740 on the type <literal>a</literal>. For example:
741 <programlisting>
742 ghci> (reverse []) :: String
743 ""
744 ghci> (reverse []) :: [Int]
745 []
746 </programlisting>
747 However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting
748 rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. The
749 standard rules take each group of constraints <literal>(C1 a, C2 a, ..., Cn
750 a)</literal> for each type variable <literal>a</literal>, and defaults the
751 type variable if
752 <orderedlist>
753 <listitem>
754 <para>
755 The type variable <literal>a</literal> appears in no
756 other constraints
757 </para>
758 </listitem>
759 <listitem>
760 <para>
761 All the classes <literal>Ci</literal> are standard.
762 </para>
763 </listitem>
764 <listitem>
765 <para>
766 At least one of the classes <literal>Ci</literal> is
767 numeric.
768 </para>
769 </listitem>
770 </orderedlist>
771 At the GHCi prompt, or with GHC if the
772 <literal>-fextended-default-rules</literal> flag is given,
773 the following additional differences apply:
774 <itemizedlist>
775 <listitem>
776 <para>
777 Rule 2 above is relaxed thus:
778 <emphasis>All</emphasis> of the classes
779 <literal>Ci</literal> are single-parameter type classes.
780 </para>
781 </listitem>
782 <listitem>
783 <para>
784 Rule 3 above is relaxed this:
785 At least one of the classes <literal>Ci</literal> is
786 numeric, <emphasis>or is <literal>Show</literal>,
787 <literal>Eq</literal>, or
788 <literal>Ord</literal></emphasis>.
789 </para>
790 </listitem>
791 <listitem>
792 <para>
793 The unit type <literal>()</literal> is added to the
794 start of the standard list of types which are tried when
795 doing type defaulting.
796 </para>
797 </listitem>
798 </itemizedlist>
799 The last point means that, for example, this program:
800 <programlisting>
801 main :: IO ()
802 main = print def
803
804 instance Num ()
805
806 def :: (Num a, Enum a) => a
807 def = toEnum 0
808 </programlisting>
809 prints <literal>()</literal> rather than <literal>0</literal> as the
810 type is defaulted to <literal>()</literal> rather than
811 <literal>Integer</literal>.
812 </para>
813 <para>
814 The motivation for the change is that it means <literal>IO a</literal>
815 actions default to <literal>IO ()</literal>, which in turn means that
816 ghci won't try to print a result when running them. This is
817 particularly important for <literal>printf</literal>, which has an
818 instance that returns <literal>IO a</literal>.
819 However, it is only able to return
820 <literal>undefined</literal>
821 (the reason for the instance having this type is to not require
822 extensions to the class system), so if the type defaults to
823 <literal>Integer</literal> then ghci gives an error when running a
824 printf.
825 </para>
826 </sect2>
827 </sect1>
828
829 <sect1 id="ghci-debugger">
830 <title>The GHCi Debugger</title>
831 <indexterm><primary>debugger</primary><secondary>in GHCi</secondary>
832 </indexterm>
833
834 <para>GHCi contains a simple imperative-style debugger in which you can
835 stop a running computation in order to examine the values of
836 variables. The debugger is integrated into GHCi, and is turned on by
837 default: no flags are required to enable the debugging facilities. There
838 is one major restriction: breakpoints and single-stepping are only
839 available in <emphasis>interpreted</emphasis> modules; compiled code is
840 invisible to the debugger.</para>
841
842 <para>The debugger provides the following:
843 <itemizedlist>
844 <listitem>
845 <para>The abilty to set a <firstterm>breakpoint</firstterm> on a
846 function definition or expression in the program. When the function
847 is called, or the expression evaluated, GHCi suspends
848 execution and returns to the prompt, where you can inspect the
849 values of local variables before continuing with the
850 execution.</para>
851 </listitem>
852 <listitem>
853 <para>Execution can be <firstterm>single-stepped</firstterm>: the
854 evaluator will suspend execution approximately after every
855 reduction, allowing local variables to be inspected. This is
856 equivalent to setting a breakpoint at every point in the
857 program.</para>
858 </listitem>
859 <listitem>
860 <para>Execution can take place in <firstterm>tracing
861 mode</firstterm>, in which the evaluator remembers each
862 evaluation step as it happens, but doesn't suspend execution until
863 an actual breakpoint is reached. When this happens, the history of
864 evaluation steps can be inspected.</para>
865 </listitem>
866 <listitem>
867 <para>Exceptions (e.g. pattern matching failure and
868 <literal>error</literal>) can be treated as breakpoints, to help
869 locate the source of an exception in the program.</para>
870 </listitem>
871 </itemizedlist>
872 </para>
873
874 <para>There is currently no support for obtaining a &ldquo;stack
875 trace&rdquo;, but the tracing and history features provide a useful
876 second-best, which will often be enough to establish the context of an
877 error.</para>
878
879 <sect2 id="breakpoints">
880 <title>Breakpoints and inspecting variables</title>
881
882 <para>Let's use quicksort as a running example. Here's the code:</para>
883
884 <programlisting>
885 qsort [] = []
886 qsort (a:as) = qsort left ++ [a] ++ qsort right
887 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
888
889 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
890 </programlisting>
891
892 <para>First, load the module into GHCi:</para>
893
894 <screen>
895 Prelude> :l qsort.hs
896 [1 of 1] Compiling Main ( qsort.hs, interpreted )
897 Ok, modules loaded: Main.
898 *Main>
899 </screen>
900
901 <para>Now, let's set a breakpoint on the right-hand-side of the second
902 equation of qsort:</para>
903
904 <programlisting>
905 *Main> :break 2
906 Breakpoint 0 activated at qsort.hs:2:15-46
907 *Main>
908 </programlisting>
909
910 <para>The command <literal>:break 2</literal> sets a breakpoint on line
911 2 of the most recently-loaded module, in this case
912 <literal>qsort.hs</literal>. Specifically, it picks the
913 leftmost complete subexpression on that line on which to set the
914 breakpoint, which in this case is the expression
915 <literal>(qsort left ++ [a] ++ qsort right)</literal>.</para>
916
917 <para>Now, we run the program:</para>
918
919 <programlisting>
920 *Main> main
921 Stopped at qsort.hs:2:15-46
922 _result :: [a]
923 a :: a
924 left :: [a]
925 right :: [a]
926 [qsort.hs:2:15-46] *Main>
927 </programlisting>
928
929 <para>Execution has stopped at the breakpoint. The prompt has changed to
930 indicate that we are currently stopped at a breakpoint, and the location:
931 <literal>[qsort.hs:2:15-46]</literal>. To further clarify the
932 location, we can use the <literal>:list</literal> command:</para>
933
934 <programlisting>
935 [qsort.hs:2:15-46] *Main> :list
936 1 qsort [] = []
937 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
938 3 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
939 </programlisting>
940
941 <para>The <literal>:list</literal> command lists the source code around
942 the current breakpoint. If your output device supports it, then GHCi
943 will highlight the active subexpression in bold.</para>
944
945 <para>GHCi has provided bindings for the free variables<footnote><para>We
946 originally provided bindings for all variables in scope, rather
947 than just
948 the free variables of the expression, but found that this affected
949 performance considerably, hence the current restriction to just the
950 free variables.</para>
951 </footnote> of the expression
952 on which the
953 breakpoint was placed (<literal>a</literal>, <literal>left</literal>,
954 <literal>right</literal>), and additionally a binding for the result of
955 the expression (<literal>_result</literal>). These variables are just
956 like other variables that you might define in GHCi; you
957 can use them in expressions that you type at the prompt, you can ask
958 for their types with <literal>:type</literal>, and so on. There is one
959 important difference though: these variables may only have partial
960 types. For example, if we try to display the value of
961 <literal>left</literal>:</para>
962
963 <screen>
964 [qsort.hs:2:15-46] *Main> left
965
966 &lt;interactive&gt;:1:0:
967 Ambiguous type variable `a' in the constraint:
968 `Show a' arising from a use of `print' at &lt;interactive&gt;:1:0-3
969 Cannot resolve unknown runtime types: a
970 Use :print or :force to determine these types
971 </screen>
972
973 <para>This is because <literal>qsort</literal> is a polymorphic function,
974 and because GHCi does not carry type information at runtime, it cannot
975 determine the runtime types of free variables that involve type
976 variables. Hence, when you ask to display <literal>left</literal> at
977 the prompt, GHCi can't figure out which instance of
978 <literal>Show</literal> to use, so it emits the type error above.</para>
979
980 <para>Fortunately, the debugger includes a generic printing command,
981 <literal>:print</literal>, which can inspect the actual runtime value of a
982 variable and attempt to reconstruct its type. If we try it on
983 <literal>left</literal>:</para>
984
985 <screen>
986 [qsort.hs:2:15-46] *Main> :print left
987 left = (_t1::[a])
988 </screen>
989
990 <para>This isn't particularly enlightening. What happened is that
991 <literal>left</literal> is bound to an unevaluated computation (a
992 suspension, or <firstterm>thunk</firstterm>), and
993 <literal>:print</literal> does not force any evaluation. The idea is
994 that <literal>:print</literal> can be used to inspect values at a
995 breakpoint without any unfortunate side effects. It won't force any
996 evaluation, which could cause the program to give a different answer
997 than it would normally, and hence it won't cause any exceptions to be
998 raised, infinite loops, or further breakpoints to be triggered (see
999 <xref linkend="nested-breakpoints" />).
1000 Rather than forcing thunks, <literal>:print</literal>
1001 binds each thunk to a fresh variable beginning with an
1002 underscore, in this case
1003 <literal>_t1</literal>.</para>
1004
1005 <para>If we aren't concerned about preserving the evaluatedness of a
1006 variable, we can use <literal>:force</literal> instead of
1007 <literal>:print</literal>. The <literal>:force</literal> command
1008 behaves exactly like <literal>:print</literal>, except that it forces
1009 the evaluation of any thunks it encounters:</para>
1010
1011 <screen>
1012 [qsort.hs:2:15-46] *Main> :force left
1013 left = [4,0,3,1]
1014 </screen>
1015
1016 <para>Now, since <literal>:force</literal> has inspected the runtime
1017 value of <literal>left</literal>, it has reconstructed its type. We
1018 can see the results of this type reconstruction:</para>
1019
1020 <screen>
1021 [qsort.hs:2:15-46] *Main> :show bindings
1022 _result :: [Integer]
1023 a :: Integer
1024 left :: [Integer]
1025 right :: [Integer]
1026 _t1 :: [Integer]
1027 </screen>
1028
1029 <para>Not only do we now know the type of <literal>left</literal>, but
1030 all the other partial types have also been resolved. So we can ask
1031 for the value of <literal>a</literal>, for example:</para>
1032
1033 <screen>
1034 [qsort.hs:2:15-46] *Main> a
1035 8
1036 </screen>
1037
1038 <para>You might find it useful to use Haskell's
1039 <literal>seq</literal> function to evaluate individual thunks rather
1040 than evaluating the whole expression with <literal>:force</literal>.
1041 For example:</para>
1042
1043 <screen>
1044 [qsort.hs:2:15-46] *Main> :print right
1045 right = (_t1::[Integer])
1046 [qsort.hs:2:15-46] *Main> seq _t1 ()
1047 ()
1048 [qsort.hs:2:15-46] *Main> :print right
1049 right = 23 : (_t2::[Integer])
1050 </screen>
1051
1052 <para>We evaluated only the <literal>_t1</literal> thunk, revealing the
1053 head of the list, and the tail is another thunk now bound to
1054 <literal>_t2</literal>. The <literal>seq</literal> function is a
1055 little inconvenient to use here, so you might want to use
1056 <literal>:def</literal> to make a nicer interface (left as an exercise
1057 for the reader!).</para>
1058
1059 <para>Finally, we can continue the current execution:</para>
1060
1061 <screen>
1062 [qsort.hs:2:15-46] *Main> :continue
1063 Stopped at qsort.hs:2:15-46
1064 _result :: [a]
1065 a :: a
1066 left :: [a]
1067 right :: [a]
1068 [qsort.hs:2:15-46] *Main>
1069 </screen>
1070
1071 <para>The execution continued at the point it previously stopped, and has
1072 now stopped at the breakpoint for a second time.</para>
1073
1074 <sect3 id="setting-breakpoings">
1075 <title>Setting breakpoints</title>
1076
1077 <para>Breakpoints can be set in various ways. Perhaps the easiest way to
1078 set a breakpoint is to name a top-level function:</para>
1079
1080 <screen>
1081 :break <replaceable>identifier</replaceable>
1082 </screen>
1083
1084 <para>Where <replaceable>identifier</replaceable> names any top-level
1085 function in an interpreted module currently loaded into GHCi (qualified
1086 names may be used). The breakpoint will be set on the body of the
1087 function, when it is fully applied but before any pattern matching has
1088 taken place.</para>
1089
1090 <para>Breakpoints can also be set by line (and optionally column)
1091 number:</para>
1092
1093 <screen>
1094 :break <replaceable>line</replaceable>
1095 :break <replaceable>line</replaceable> <replaceable>column</replaceable>
1096 :break <replaceable>module</replaceable> <replaceable>line</replaceable>
1097 :break <replaceable>module</replaceable> <replaceable>line</replaceable> <replaceable>column</replaceable>
1098 </screen>
1099
1100 <para>When a breakpoint is set on a particular line, GHCi sets the
1101 breakpoint on the
1102 leftmost subexpression that begins and ends on that line. If two
1103 complete subexpressions start at the same
1104 column, the longest one is picked. If there is no complete
1105 subexpression on the line, then the leftmost expression starting on
1106 the line is picked, and failing that the rightmost expression that
1107 partially or completely covers the line.</para>
1108
1109 <para>When a breakpoint is set on a particular line and column, GHCi
1110 picks the smallest subexpression that encloses that location on which
1111 to set the breakpoint. Note: GHC considers the TAB character to have a
1112 width of 1, wherever it occurs; in other words it counts
1113 characters, rather than columns. This matches what some editors do,
1114 and doesn't match others. The best advice is to avoid tab
1115 characters in your source code altogether (see
1116 <option>-fwarn-tabs</option> in <xref linkend="options-sanity"
1117 />).</para>
1118
1119 <para>If the module is omitted, then the most recently-loaded module is
1120 used.</para>
1121
1122 <para>Not all subexpressions are potential breakpoint locations. Single
1123 variables are typically not considered to be breakpoint locations
1124 (unless the variable is the right-hand-side of a function definition,
1125 lambda, or case alternative). The rule of thumb is that all redexes
1126 are breakpoint locations, together with the bodies of functions,
1127 lambdas, case alternatives and binding statements. There is normally
1128 no breakpoint on a let expression, but there will always be a
1129 breakpoint on its body, because we are usually interested in inspecting
1130 the values of the variables bound by the let.</para>
1131
1132 </sect3>
1133 <sect3>
1134 <title>Listing and deleting breakpoints</title>
1135
1136 <para>The list of breakpoints currently enabled can be displayed using
1137 <literal>:show&nbsp;breaks</literal></para>:
1138 <screen>
1139 *Main> :show breaks
1140 [0] Main qsort.hs:1:11-12
1141 [1] Main qsort.hs:2:15-46
1142 </screen>
1143
1144 <para>To delete a breakpoint, use the <literal>:delete</literal>
1145 command with the number given in the output from <literal>:show&nbsp;breaks</literal>:</para>
1146
1147 <screen>
1148 *Main> :delete 0
1149 *Main> :show breaks
1150 [1] Main qsort.hs:2:15-46
1151 </screen>
1152
1153 <para>To delete all breakpoints at once, use <literal>:delete *</literal>.</para>
1154
1155 </sect3>
1156 </sect2>
1157
1158 <sect2 id="single-stepping">
1159 <title>Single-stepping</title>
1160
1161 <para>Single-stepping is a great way to visualise the execution of your
1162 program, and it is also a useful tool for identifying the source of a
1163 bug. The concept is simple: single-stepping enables all the
1164 breakpoints in the program and executes until the next breakpoint is
1165 reached, at which point you can single-step again, or continue
1166 normally. For example:</para>
1167
1168 <screen>
1169 *Main> :step main
1170 Stopped at qsort.hs:5:7-47
1171 _result :: IO ()
1172 </screen>
1173
1174 <para>The command <literal>:step
1175 <replaceable>expr</replaceable></literal> begins the evaluation of
1176 <replaceable>expr</replaceable> in single-stepping mode. If
1177 <replaceable>expr</replaceable> is ommitted, then it single-steps from
1178 the current breakpoint.</para>
1179
1180 <para>The <literal>:list</literal> command is particularly useful when
1181 single-stepping, to see where you currently are:</para>
1182
1183 <screen>
1184 [qsort.hs:5:7-47] *Main> :list
1185 4
1186 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1187 6
1188 [qsort.hs:5:7-47] *Main>
1189 </screen>
1190
1191 <para>In fact, GHCi provides a way to run a command when a breakpoint is
1192 hit, so we can make it automatically do
1193 <literal>:list</literal>:</para>
1194
1195 <screen>
1196 [qsort.hs:5:7-47] *Main> :set stop :list
1197 [qsort.hs:5:7-47] *Main> :step
1198 Stopped at qsort.hs:5:14-46
1199 _result :: [Integer]
1200 4
1201 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1202 6
1203 [qsort.hs:5:14-46] *Main>
1204 </screen>
1205 </sect2>
1206
1207 <sect2 id="nested-breakpoints">
1208 <title>Nested breakpoints</title>
1209 <para>When GHCi is stopped at a breakpoint, and an expression entered at
1210 the prompt triggers a
1211 second breakpoint, the new breakpoint becomes the &ldquo;current&rdquo;
1212 one, and the old one is saved on a stack. An arbitrary number of
1213 breakpoint contexts can be built up in this way. For example:</para>
1214
1215 <screen>
1216 [qsort.hs:2:15-46] *Main> :st qsort [1,3]
1217 Stopped at qsort.hs:(1,0)-(3,55)
1218 _result :: [a]
1219 ... [qsort.hs:(1,0)-(3,55)] *Main>
1220 </screen>
1221
1222 <para>While stopped at the breakpoint on line 2 that we set earlier, we
1223 started a new evaluation with <literal>:step qsort [1,3]</literal>.
1224 This new evaluation stopped after one step (at the definition of
1225 <literal>qsort</literal>). The prompt has changed, now prefixed with
1226 <literal>...</literal>, to indicate that there are saved breakpoints
1227 beyond the current one. To see the stack of contexts, use
1228 <literal>:show context</literal>:</para>
1229
1230 <screen>
1231 ... [qsort.hs:(1,0)-(3,55)] *Main> :show context
1232 --> main
1233 Stopped at qsort.hs:2:15-46
1234 --> qsort [1,3]
1235 Stopped at qsort.hs:(1,0)-(3,55)
1236 ... [qsort.hs:(1,0)-(3,55)] *Main>
1237 </screen>
1238
1239 <para>To abandon the current evaluation, use
1240 <literal>:abandon</literal>:</para>
1241
1242 <screen>
1243 ... [qsort.hs:(1,0)-(3,55)] *Main> :abandon
1244 [qsort.hs:2:15-46] *Main> :abandon
1245 *Main>
1246 </screen>
1247 </sect2>
1248
1249 <sect2 id="ghci-debugger-result">
1250 <title>The <literal>_result</literal> variable</title>
1251 <para>When stopped at a breakpoint or single-step, GHCi binds the
1252 variable <literal>_result</literal> to the value of the currently
1253 active expression. The value of <literal>_result</literal> is
1254 presumably not available yet, because we stopped its evaluation, but it
1255 can be forced: if the type is known and showable, then just entering
1256 <literal>_result</literal> at the prompt will show it. However,
1257 there's one caveat to doing this: evaluating <literal>_result</literal>
1258 will be likely to trigger further breakpoints, starting with the
1259 breakpoint we are currently stopped at (if we stopped at a real
1260 breakpoint, rather than due to <literal>:step</literal>). So it will
1261 probably be necessary to issue a <literal>:continue</literal>
1262 immediately when evaluating <literal>_result</literal>. Alternatively,
1263 you can use <literal>:force</literal> which ignores breakpoints.</para>
1264 </sect2>
1265
1266 <sect2 id="tracing">
1267 <title>Tracing and history</title>
1268
1269 <para>A question that we often want to ask when debugging a program is
1270 &ldquo;how did I get here?&rdquo;. Traditional imperative debuggers
1271 usually provide some kind of stack-tracing feature that lets you see
1272 the stack of active function calls (sometimes called the &ldquo;lexical
1273 call stack&rdquo;), describing a path through the code
1274 to the current location. Unfortunately this is hard to provide in
1275 Haskell, because execution proceeds on a demand-driven basis, rather
1276 than a depth-first basis as in strict languages. The
1277 &ldquo;stack&ldquo; in GHC's execution engine bears little
1278 resemblance to the lexical call stack. Ideally GHCi would maintain a
1279 separate lexical call stack in addition to the dynamic call stack, and
1280 in fact this is exactly
1281 what our profiling system does (<xref linkend="profiling" />), and what
1282 some other Haskell debuggers do. For the time being, however, GHCi
1283 doesn't maintain a lexical call stack (there are some technical
1284 challenges to be overcome). Instead, we provide a way to backtrack from a
1285 breakpoint to previous evaluation steps: essentially this is like
1286 single-stepping backwards, and should in many cases provide enough
1287 information to answer the &ldquo;how did I get here?&rdquo;
1288 question.</para>
1289
1290 <para>To use tracing, evaluate an expression with the
1291 <literal>:trace</literal> command. For example, if we set a breakpoint
1292 on the base case of <literal>qsort</literal>:</para>
1293
1294 <screen>
1295 *Main&gt; :list qsort
1296 1 qsort [] = []
1297 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
1298 3 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
1299 4
1300 *Main&gt; :b 1
1301 Breakpoint 1 activated at qsort.hs:1:11-12
1302 *Main&gt;
1303 </screen>
1304
1305 <para>and then run a small <literal>qsort</literal> with
1306 tracing:</para>
1307
1308 <screen>
1309 *Main> :trace qsort [3,2,1]
1310 Stopped at qsort.hs:1:11-12
1311 _result :: [a]
1312 [qsort.hs:1:11-12] *Main>
1313 </screen>
1314
1315 <para>We can now inspect the history of evaluation steps:</para>
1316
1317 <screen>
1318 [qsort.hs:1:11-12] *Main> :hist
1319 -1 : qsort.hs:3:24-38
1320 -2 : qsort.hs:3:23-55
1321 -3 : qsort.hs:(1,0)-(3,55)
1322 -4 : qsort.hs:2:15-24
1323 -5 : qsort.hs:2:15-46
1324 -6 : qsort.hs:3:24-38
1325 -7 : qsort.hs:3:23-55
1326 -8 : qsort.hs:(1,0)-(3,55)
1327 -9 : qsort.hs:2:15-24
1328 -10 : qsort.hs:2:15-46
1329 -11 : qsort.hs:3:24-38
1330 -12 : qsort.hs:3:23-55
1331 -13 : qsort.hs:(1,0)-(3,55)
1332 -14 : qsort.hs:2:15-24
1333 -15 : qsort.hs:2:15-46
1334 -16 : qsort.hs:(1,0)-(3,55)
1335 &lt;end of history&gt;
1336 </screen>
1337
1338 <para>To examine one of the steps in the history, use
1339 <literal>:back</literal>:</para>
1340
1341 <screen>
1342 [qsort.hs:1:11-12] *Main> :back
1343 Logged breakpoint at qsort.hs:3:24-38
1344 _result :: [a]
1345 as :: [a]
1346 a :: a
1347 [-1: qsort.hs:3:24-38] *Main>
1348 </screen>
1349
1350 <para>Note that the local variables at each step in the history have been
1351 preserved, and can be examined as usual. Also note that the prompt has
1352 changed to indicate that we're currently examining the first step in
1353 the history: <literal>-1</literal>. The command
1354 <literal>:forward</literal> can be used to traverse forward in the
1355 history.</para>
1356
1357 <para>The <literal>:trace</literal> command can be used with or without
1358 an expression. When used without an expression, tracing begins from
1359 the current breakpoint, just like <literal>:step</literal>.</para>
1360
1361 <para>The history is only available when
1362 using <literal>:trace</literal>; the reason for this is we found that
1363 logging each breakpoint in the history cuts performance by a factor of
1364 2 or more. GHCi remembers the last 50 steps in the history (perhaps in
1365 the future we'll make this configurable).</para>
1366 </sect2>
1367
1368 <sect2 id="ghci-debugger-exceptions">
1369 <title>Debugging exceptions</title>
1370 <para>Another common question that comes up when debugging is
1371 &ldquo;where did this exception come from?&rdquo;. Exceptions such as
1372 those raised by <literal>error</literal> or <literal>head []</literal>
1373 have no context information attached to them. Finding which
1374 particular call to <literal>head</literal> in your program resulted in
1375 the error can be a painstaking process, usually involving
1376 <literal>Debug.Trace.trace</literal>, or compiling with
1377 profiling and using <literal>+RTS -xc</literal> (see <xref
1378 linkend="prof-time-options" />).</para>
1379
1380 <para>The GHCi debugger offers a way to hopefully shed some light on
1381 these errors quickly and without modifying or recompiling the source
1382 code. One way would be to set a breakpoint on the location in the
1383 source code that throws the exception, and then use
1384 <literal>:trace</literal> and <literal>:history</literal> to establish
1385 the context. However, <literal>head</literal> is in a library and
1386 we can't set a breakpoint on it directly. For this reason, GHCi
1387 provides the flag <literal>-fbreak-on-exception</literal> which causes
1388 the evaluator to stop when an exception is thrown, just as it does when
1389 a breakpoint is hit. This is only really useful in conjunction with
1390 <literal>:trace</literal>, in order to log the steps leading up to the
1391 exception. For example:</para>
1392
1393 <screen>
1394 *Main> :set -fbreak-on-exception
1395 *Main> :trace qsort ("abc" ++ undefined)
1396 "Stopped at &lt;exception thrown&gt;
1397 _exception :: e
1398 [&lt;exception thrown&gt;] *Main&gt; :hist
1399 -1 : qsort.hs:3:24-38
1400 -2 : qsort.hs:3:23-55
1401 -3 : qsort.hs:(1,0)-(3,55)
1402 -4 : qsort.hs:2:15-24
1403 -5 : qsort.hs:2:15-46
1404 -6 : qsort.hs:(1,0)-(3,55)
1405 &lt;end of history&gt;
1406 [&lt;exception thrown&gt;] *Main&gt; :back
1407 Logged breakpoint at qsort.hs:3:24-38
1408 _result :: [a]
1409 as :: [a]
1410 a :: a
1411 [-1: qsort.hs:3:24-38] *Main&gt; :force as
1412 *** Exception: Prelude.undefined
1413 [-1: qsort.hs:3:24-38] *Main&gt; :print as
1414 as = 'b' : 'c' : (_t1::[Char])
1415 </screen>
1416
1417 <para>The exception itself is bound to a new variable,
1418 <literal>_exception</literal>.</para>
1419
1420 <para>Breaking on exceptions is particularly useful for finding out what
1421 your program was doing when it was in an infinite loop. Just hit
1422 Control-C, and examine the history to find out what was going
1423 on.</para>
1424 </sect2>
1425
1426 <sect2><title>Example: inspecting functions</title>
1427 <para>
1428 It is possible to use the debugger to examine function values.
1429 When we are at a breakpoint and a function is in scope, the debugger
1430 cannot show
1431 you the source code for it; however, it is possible to get some
1432 information by applying it to some arguments and observing the result.
1433 </para>
1434
1435 <para>
1436 The process is slightly complicated when the binding is polymorphic.
1437 We show the process by means of an example.
1438 To keep things simple, we will use the well known <literal>map</literal> function:
1439 <programlisting>
1440 import Prelude hiding (map)
1441
1442 map :: (a->b) -> a -> b
1443 map f [] = []
1444 map f (x:xs) = f x : map f xs
1445 </programlisting>
1446 </para>
1447
1448 <para>
1449 We set a breakpoint on <literal>map</literal>, and call it.
1450 <screen>
1451 *Main> :break 5
1452 Breakpoint 0 activated at map.hs:5:15-28
1453 *Main> map Just [1..5]
1454 Stopped at map.hs:(4,0)-(5,12)
1455 _result :: [b]
1456 x :: a
1457 f :: a -> b
1458 xs :: [a]
1459 </screen>
1460 GHCi tells us that, among other bindings, <literal>f</literal> is in scope.
1461 However, its type is not fully known yet,
1462 and thus it is not possible to apply it to any
1463 arguments. Nevertheless, observe that the type of its first argument is the
1464 same as the type of <literal>x</literal>, and its result type is shared
1465 with <literal>_result</literal>.
1466 </para>
1467
1468 <para>
1469 As we demonstrated earlier (<xref linkend="breakpoints" />), the
1470 debugger has some intelligence built-in to update the type of
1471 <literal>f</literal> whenever the types of <literal>x</literal> or
1472 <literal>_result</literal> are discovered. So what we do in this
1473 scenario is
1474 force <literal>x</literal> a bit, in order to recover both its type
1475 and the argument part of <literal>f</literal>.
1476 <screen>
1477 *Main> seq x ()
1478 *Main> :print x
1479 x = 1
1480 </screen>
1481 </para>
1482 <para>
1483 We can check now that as expected, the type of <literal>x</literal>
1484 has been reconstructed, and with it the
1485 type of <literal>f</literal> has been too:</para>
1486 <screen>
1487 *Main> :t x
1488 x :: Integer
1489 *Main> :t f
1490 f :: Integer -> b
1491 </screen>
1492 <para>
1493 From here, we can apply f to any argument of type Integer and observe
1494 the results.
1495 <screen><![CDATA[
1496 *Main> let b = f 10
1497 *Main> :t b
1498 b :: b
1499 *Main> b
1500 <interactive>:1:0:
1501 Ambiguous type variable `b' in the constraint:
1502 `Show b' arising from a use of `print' at <interactive>:1:0
1503 *Main> :p b
1504 b = (_t2::a)
1505 *Main> seq b ()
1506 ()
1507 *Main> :t b
1508 b :: a
1509 *Main> :p b
1510 b = Just 10
1511 *Main> :t b
1512 b :: Maybe Integer
1513 *Main> :t f
1514 f :: Integer -> Maybe Integer
1515 *Main> f 20
1516 Just 20
1517 *Main> map f [1..5]
1518 [Just 1, Just 2, Just 3, Just 4, Just 5]
1519 ]]></screen>
1520 In the first application of <literal>f</literal>, we had to do
1521 some more type reconstruction
1522 in order to recover the result type of <literal>f</literal>.
1523 But after that, we are free to use
1524 <literal>f</literal> normally.
1525 </para>
1526 </sect2>
1527
1528 <sect2><title>Limitations</title>
1529 <itemizedlist>
1530 <listitem>
1531 <para>When stopped at a breakpoint, if you try to evaluate a variable
1532 that is already under evaluation, the second evaluation will hang.
1533 The reason is
1534 that GHC knows the variable is under evaluation, so the new
1535 evaluation just waits for the result before continuing, but of
1536 course this isn't going to happen because the first evaluation is
1537 stopped at a breakpoint. Control-C can interrupt the hung
1538 evaluation and return to the prompt.</para>
1539 <para>The most common way this can happen is when you're evaluating a
1540 CAF (e.g. main), stop at a breakpoint, and ask for the value of the
1541 CAF at the prompt again.</para>
1542 </listitem>
1543 <listitem><para>
1544 Implicit parameters (see <xref linkend="implicit-parameters"/>) are only available
1545 at the scope of a breakpoint if there is a explicit type signature.
1546 </para>
1547 </listitem>
1548 </itemizedlist>
1549 </sect2>
1550 </sect1>
1551
1552 <sect1 id="ghci-invocation">
1553 <title>Invoking GHCi</title>
1554 <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
1555 <indexterm><primary><option>&ndash;&ndash;interactive</option></primary></indexterm>
1556
1557 <para>GHCi is invoked with the command <literal>ghci</literal> or
1558 <literal>ghc &ndash;&ndash;interactive</literal>. One or more modules or
1559 filenames can also be specified on the command line; this
1560 instructs GHCi to load the specified modules or filenames (and all
1561 the modules they depend on), just as if you had said
1562 <literal>:load <replaceable>modules</replaceable></literal> at the
1563 GHCi prompt (see <xref linkend="ghci-commands" />). For example, to
1564 start GHCi and load the program whose topmost module is in the
1565 file <literal>Main.hs</literal>, we could say:</para>
1566
1567 <screen>
1568 $ ghci Main.hs
1569 </screen>
1570
1571 <para>Most of the command-line options accepted by GHC (see <xref
1572 linkend="using-ghc"/>) also make sense in interactive mode. The ones
1573 that don't make sense are mostly obvious; for example, GHCi
1574 doesn't generate interface files, so options related to interface
1575 file generation won't have any effect.</para>
1576
1577 <sect2>
1578 <title>Packages</title>
1579 <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
1580
1581 <para>Most packages (see <xref linkend="using-packages"/>) are
1582 available without needing to specify any extra flags at all:
1583 they will be automatically loaded the first time they are
1584 needed.</para>
1585
1586 <para>For hidden packages, however, you need to request the
1587 package be loaded by using the <literal>-package</literal> flag:</para>
1588
1589 <screen>
1590 $ ghci -package readline
1591 ___ ___ _
1592 / _ \ /\ /\/ __(_)
1593 / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98.
1594 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
1595 \____/\/ /_/\____/|_| Type :? for help.
1596
1597 Loading package base ... linking ... done.
1598 Loading package readline-1.0 ... linking ... done.
1599 Prelude>
1600 </screen>
1601
1602 <para>The following command works to load new packages into a
1603 running GHCi:</para>
1604
1605 <screen>
1606 Prelude> :set -package <replaceable>name</replaceable>
1607 </screen>
1608
1609 <para>But note that doing this will cause all currently loaded
1610 modules to be unloaded, and you'll be dumped back into the
1611 <literal>Prelude</literal>.</para>
1612 </sect2>
1613
1614 <sect2>
1615 <title>Extra libraries</title>
1616 <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
1617
1618 <para>Extra libraries may be specified on the command line using
1619 the normal <literal>-l<replaceable>lib</replaceable></literal>
1620 option. (The term <emphasis>library</emphasis> here refers to
1621 libraries of foreign object code; for using libraries of Haskell
1622 source code, see <xref linkend="ghci-modules-filenames"/>.) For
1623 example, to load the &ldquo;m&rdquo; library:</para>
1624
1625 <screen>
1626 $ ghci -lm
1627 </screen>
1628
1629 <para>On systems with <literal>.so</literal>-style shared
1630 libraries, the actual library loaded will the
1631 <filename>lib<replaceable>lib</replaceable>.so</filename>. GHCi
1632 searches the following places for libraries, in this order:</para>
1633
1634 <itemizedlist>
1635 <listitem>
1636 <para>Paths specified using the
1637 <literal>-L<replaceable>path</replaceable></literal>
1638 command-line option,</para>
1639 </listitem>
1640 <listitem>
1641 <para>the standard library search path for your system,
1642 which on some systems may be overridden by setting the
1643 <literal>LD_LIBRARY_PATH</literal> environment
1644 variable.</para>
1645 </listitem>
1646 </itemizedlist>
1647
1648 <para>On systems with <literal>.dll</literal>-style shared
1649 libraries, the actual library loaded will be
1650 <filename><replaceable>lib</replaceable>.dll</filename>. Again,
1651 GHCi will signal an error if it can't find the library.</para>
1652
1653 <para>GHCi can also load plain object files
1654 (<literal>.o</literal> or <literal>.obj</literal> depending on
1655 your platform) from the command-line. Just add the name the
1656 object file to the command line.</para>
1657
1658 <para>Ordering of <option>-l</option> options matters: a library
1659 should be mentioned <emphasis>before</emphasis> the libraries it
1660 depends on (see <xref linkend="options-linker"/>).</para>
1661 </sect2>
1662
1663 </sect1>
1664
1665 <sect1 id="ghci-commands">
1666 <title>GHCi commands</title>
1667
1668 <para>GHCi commands all begin with
1669 &lsquo;<literal>:</literal>&rsquo; and consist of a single command
1670 name followed by zero or more parameters. The command name may be
1671 abbreviated, with ambiguities being resolved in favour of the more
1672 commonly used commands.</para>
1673
1674 <variablelist>
1675 <varlistentry>
1676 <term>
1677 <literal>:abandon</literal>
1678 <indexterm><primary><literal>:abandon</literal></primary></indexterm>
1679 </term>
1680 <listitem>
1681 <para>Abandons the current evaluation (only available when stopped at
1682 a breakpoint).</para>
1683 </listitem>
1684 </varlistentry>
1685
1686 <varlistentry>
1687 <term>
1688 <literal>:add</literal> <replaceable>module</replaceable> ...
1689 <indexterm><primary><literal>:add</literal></primary></indexterm>
1690 </term>
1691 <listitem>
1692 <para>Add <replaceable>module</replaceable>(s) to the
1693 current <firstterm>target set</firstterm>, and perform a
1694 reload.</para>
1695 </listitem>
1696 </varlistentry>
1697
1698 <varlistentry>
1699 <term>
1700 <literal>:back</literal>
1701 <indexterm><primary><literal>:back</literal></primary></indexterm>
1702 </term>
1703 <listitem>
1704 <para>Travel back one step in the history. See <xref
1705 linkend="tracing" />. See also:
1706 <literal>:trace</literal>, <literal>:history</literal>,
1707 <literal>:forward</literal>.</para>
1708 </listitem>
1709 </varlistentry>
1710
1711 <varlistentry>
1712 <term>
1713 <literal>:break [<replaceable>identifier</replaceable> |
1714 [<replaceable>module</replaceable>] <replaceable>line</replaceable>
1715 [<replaceable>column</replaceable>]]</literal>
1716 </term>
1717 <indexterm><primary><literal>:break</literal></primary></indexterm>
1718 <listitem>
1719 <para>Set a breakpoint on the specified function or line and
1720 column. See <xref linkend="setting-breakpoints" />.</para>
1721 </listitem>
1722 </varlistentry>
1723
1724 <varlistentry>
1725 <term>
1726 <literal>:browse</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
1727 <indexterm><primary><literal>:browse</literal></primary></indexterm>
1728 </term>
1729 <listitem>
1730 <para>Displays the identifiers defined by the module
1731 <replaceable>module</replaceable>, which must be either
1732 loaded into GHCi or be a member of a package. If the
1733 <literal>*</literal> symbol is placed before the module
1734 name, then <emphasis>all</emphasis> the identifiers defined
1735 in <replaceable>module</replaceable> are shown; otherwise
1736 the list is limited to the exports of
1737 <replaceable>module</replaceable>. The
1738 <literal>*</literal>-form is only available for modules
1739 which are interpreted; for compiled modules (including
1740 modules from packages) only the non-<literal>*</literal>
1741 form of <literal>:browse</literal> is available.</para>
1742 </listitem>
1743 </varlistentry>
1744
1745 <varlistentry>
1746 <term>
1747 <literal>:cd</literal> <replaceable>dir</replaceable>
1748 <indexterm><primary><literal>:cd</literal></primary></indexterm>
1749 </term>
1750 <listitem>
1751 <para>Changes the current working directory to
1752 <replaceable>dir</replaceable>. A
1753 &lsquo;<literal>&tilde;</literal>&rsquo; symbol at the
1754 beginning of <replaceable>dir</replaceable> will be replaced
1755 by the contents of the environment variable
1756 <literal>HOME</literal>.</para>
1757
1758 <para>NOTE: changing directories causes all currently loaded
1759 modules to be unloaded. This is because the search path is
1760 usually expressed using relative directories, and changing
1761 the search path in the middle of a session is not
1762 supported.</para>
1763 </listitem>
1764 </varlistentry>
1765
1766 <varlistentry>
1767 <term>
1768 <literal>:continue</literal>
1769 <indexterm><primary><literal>:continue</literal></primary></indexterm>
1770 </term>
1771 <listitem><para>Continue the current evaluation, when stopped at a
1772 breakpoint.</para>
1773 </listitem>
1774 </varlistentry>
1775
1776 <varlistentry>
1777 <term>
1778 <literal>:cmd</literal> <replaceable>expr</replaceable>
1779 <indexterm><primary><literal>:cmd</literal></primary></indexterm>
1780 </term>
1781 <listitem>
1782 <para>Executes <replaceable>expr</replaceable> as a computation of
1783 type <literal>IO String</literal>, and then executes the resulting
1784 string as a list of GHCi commands. Multiple commands are separated
1785 by newlines. The <literal>:cmd</literal> command is useful with
1786 <literal>:def</literal> and <literal>:set stop</literal>.</para>
1787 </listitem>
1788 </varlistentry>
1789
1790 <varlistentry>
1791 <term>
1792 <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
1793 <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
1794 <indexterm><primary><literal>:etags</literal></primary>
1795 </indexterm>
1796 <indexterm><primary><literal>:etags</literal></primary>
1797 </indexterm>
1798 </term>
1799 <listitem>
1800 <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
1801 (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>). If
1802 no filename is specified, the defaulit <filename>tags</filename> or
1803 <filename>TAGS</filename> is
1804 used, respectively. Tags for all the functions, constructors and
1805 types in the currently loaded modules are created. All modules must
1806 be interpreted for these commands to work.</para>
1807 <para>See also <xref linkend="hasktags" />.</para>
1808 </listitem>
1809 </varlistentry>
1810
1811 <varlistentry>
1812 <term>
1813 <literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable>
1814 <indexterm><primary><literal>:def</literal></primary></indexterm>
1815 </term>
1816 <listitem>
1817 <para>The command <literal>:def</literal>
1818 <replaceable>name</replaceable>
1819 <replaceable>expr</replaceable> defines a new GHCi command
1820 <literal>:<replaceable>name</replaceable></literal>,
1821 implemented by the Haskell expression
1822 <replaceable>expr</replaceable>, which must have type
1823 <literal>String -> IO String</literal>. When
1824 <literal>:<replaceable>name</replaceable>
1825 <replaceable>args</replaceable></literal> is typed at the
1826 prompt, GHCi will run the expression
1827 <literal>(<replaceable>name</replaceable>
1828 <replaceable>args</replaceable>)</literal>, take the
1829 resulting <literal>String</literal>, and feed it back into
1830 GHCi as a new sequence of commands. Separate commands in
1831 the result must be separated by
1832 &lsquo;<literal>\n</literal>&rsquo;.</para>
1833
1834 <para>That's all a little confusing, so here's a few
1835 examples. To start with, here's a new GHCi command which
1836 doesn't take any arguments or produce any results, it just
1837 outputs the current date &amp; time:</para>
1838
1839 <screen>
1840 Prelude> let date _ = Time.getClockTime >>= print >> return ""
1841 Prelude> :def date date
1842 Prelude> :date
1843 Fri Mar 23 15:16:40 GMT 2001
1844 </screen>
1845
1846 <para>Here's an example of a command that takes an argument.
1847 It's a re-implementation of <literal>:cd</literal>:</para>
1848
1849 <screen>
1850 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
1851 Prelude> :def mycd mycd
1852 Prelude> :mycd ..
1853 </screen>
1854
1855 <para>Or I could define a simple way to invoke
1856 &ldquo;<literal>ghc &ndash;&ndash;make Main</literal>&rdquo; in the
1857 current directory:</para>
1858
1859 <screen>
1860 Prelude> :def make (\_ -> return ":! ghc &ndash;&ndash;make Main")
1861 </screen>
1862
1863 <para>We can define a command that reads GHCi input from a
1864 file. This might be useful for creating a set of bindings
1865 that we want to repeatedly load into the GHCi session:</para>
1866
1867 <screen>
1868 Prelude> :def . readFile
1869 Prelude> :. cmds.ghci
1870 </screen>
1871
1872 <para>Notice that we named the command
1873 <literal>:.</literal>, by analogy with the
1874 &lsquo;<literal>.</literal>&rsquo; Unix shell command that
1875 does the same thing.</para>
1876 </listitem>
1877 </varlistentry>
1878
1879 <varlistentry>
1880 <term>
1881 <literal>:delete * | <replaceable>num</replaceable> ...</literal>
1882 <indexterm><primary><literal>:delete</literal></primary></indexterm>
1883 </term>
1884 <listitem>
1885 <para>Delete one or more breakpoints by number (use <literal>:show
1886 breaks</literal> to see the number of each breakpoint). The
1887 <literal>*</literal> form deletes all the breakpoints.</para>
1888 </listitem>
1889 </varlistentry>
1890
1891 <varlistentry>
1892 <term>
1893 <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
1894 <indexterm><primary><literal>:edit</literal></primary></indexterm>
1895 </term>
1896 <listitem>
1897 <para>Opens an editor to edit the file
1898 <replaceable>file</replaceable>, or the most recently loaded
1899 module if <replaceable>file</replaceable> is omitted. The
1900 editor to invoke is taken from the <literal>EDITOR</literal>
1901 environment variable, or a default editor on your system if
1902 <literal>EDITOR</literal> is not set. You can change the
1903 editor using <literal>:set editor</literal>.</para>
1904 </listitem>
1905 </varlistentry>
1906
1907 <varlistentry>
1908 <term>
1909 <literal>:force <replaceable>identifier</replaceable> ...</literal>
1910 <indexterm><primary><literal>:force</literal></primary></indexterm>
1911 </term>
1912 <listitem>
1913 <para>Prints the value of <replaceable>identifier</replaceable> in
1914 the same way as <literal>:print</literal>. Unlike
1915 <literal>:print</literal>, <literal>:force</literal> evaluates each
1916 thunk that it encounters while traversing the value. This may
1917 cause exceptions or infinite loops, or further breakpoints (which
1918 are ignored, but displayed).</para>
1919 </listitem>
1920 </varlistentry>
1921
1922 <varlistentry>
1923 <term>
1924 <literal>:forward</literal>
1925 <indexterm><primary><literal>:forward</literal></primary></indexterm>
1926 </term>
1927 <listitem>
1928 <para>Move forward in the history. See <xref
1929 linkend="tracing" />. See also:
1930 <literal>:trace</literal>, <literal>:history</literal>,
1931 <literal>:back</literal>.</para>
1932 </listitem>
1933 </varlistentry>
1934
1935 <varlistentry>
1936 <term>
1937 <literal>:help</literal>
1938 <indexterm><primary><literal>:help</literal></primary></indexterm>
1939 </term>
1940 <term>
1941 <literal>:?</literal>
1942 <indexterm><primary><literal>:?</literal></primary></indexterm>
1943 </term>
1944 <listitem>
1945 <para>Displays a list of the available commands.</para>
1946 </listitem>
1947 </varlistentry>
1948
1949 <varlistentry>
1950 <term>
1951 <literal>:history [<replaceable>num</replaceable>]</literal>
1952 <indexterm><primary><literal>:history</literal></primary></indexterm>
1953 </term>
1954 <listitem>
1955 <para>Display the history of evaluation steps. With a number,
1956 displays that many steps (default: 20). For use with
1957 <literal>:trace</literal>; see <xref
1958 linkend="tracing" />.</para>
1959 </listitem>
1960 </varlistentry>
1961
1962 <varlistentry>
1963 <term>
1964 <literal>:info</literal> <replaceable>name</replaceable> ...
1965 <indexterm><primary><literal>:info</literal></primary></indexterm>
1966 </term>
1967 <listitem>
1968 <para>Displays information about the given name(s). For
1969 example, if <replaceable>name</replaceable> is a class, then
1970 the class methods and their types will be printed; if
1971 <replaceable>name</replaceable> is a type constructor, then
1972 its definition will be printed; if
1973 <replaceable>name</replaceable> is a function, then its type
1974 will be printed. If <replaceable>name</replaceable> has
1975 been loaded from a source file, then GHCi will also display
1976 the location of its definition in the source.</para>
1977 </listitem>
1978 </varlistentry>
1979
1980 <varlistentry>
1981 <term>
1982 <literal>:kind</literal> <replaceable>type</replaceable>
1983 <indexterm><primary><literal>:kind</literal></primary></indexterm>
1984 </term>
1985 <listitem>
1986 <para>Infers and prints the kind of
1987 <replaceable>type</replaceable>. The latter can be an arbitrary
1988 type expression, including a partial application of a type constructor,
1989 such as <literal>Either Int</literal>.</para>
1990 </listitem>
1991 </varlistentry>
1992
1993 <varlistentry>
1994 <term>
1995 <literal>:load</literal> <replaceable>module</replaceable> ...
1996 <indexterm><primary><literal>:load</literal></primary></indexterm>
1997 </term>
1998 <listitem>
1999 <para>Recursively loads the specified
2000 <replaceable>module</replaceable>s, and all the modules they
2001 depend on. Here, each <replaceable>module</replaceable>
2002 must be a module name or filename, but may not be the name
2003 of a module in a package.</para>
2004
2005 <para>All previously loaded modules, except package modules,
2006 are forgotten. The new set of modules is known as the
2007 <firstterm>target set</firstterm>. Note that
2008 <literal>:load</literal> can be used without any arguments
2009 to unload all the currently loaded modules and
2010 bindings.</para>
2011
2012 <para>After a <literal>:load</literal> command, the current
2013 context is set to:</para>
2014
2015 <itemizedlist>
2016 <listitem>
2017 <para><replaceable>module</replaceable>, if it was loaded
2018 successfully, or</para>
2019 </listitem>
2020 <listitem>
2021 <para>the most recently successfully loaded module, if
2022 any other modules were loaded as a result of the current
2023 <literal>:load</literal>, or</para>
2024 </listitem>
2025 <listitem>
2026 <para><literal>Prelude</literal> otherwise.</para>
2027 </listitem>
2028 </itemizedlist>
2029 </listitem>
2030 </varlistentry>
2031
2032 <varlistentry>
2033 <term>
2034 <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
2035 <indexterm><primary><literal>:main</literal></primary></indexterm>
2036 </term>
2037 <listitem>
2038 <para>
2039 When a program is compiled and executed, it can use the
2040 <literal>getArgs</literal> function to access the
2041 command-line arguments.
2042 However, we cannot simply pass the arguments to the
2043 <literal>main</literal> function while we are testing in ghci,
2044 as the <literal>main</literal> function doesn't take its
2045 directly.
2046 </para>
2047
2048 <para>
2049 Instead, we can use the <literal>:main</literal> command.
2050 This runs whatever <literal>main</literal> is in scope, with
2051 any arguments being treated the same as command-line arguments,
2052 e.g.:
2053 </para>
2054
2055 <screen>
2056 Prelude> let main = System.Environment.getArgs >>= print
2057 Prelude> :main foo bar
2058 ["foo","bar"]
2059 </screen>
2060
2061 </listitem>
2062 </varlistentry>
2063
2064 <varlistentry>
2065 <term>
2066 <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
2067 <indexterm><primary><literal>:module</literal></primary></indexterm>
2068 </term>
2069 <listitem>
2070 <para>Sets or modifies the current context for statements
2071 typed at the prompt. See <xref linkend="ghci-scope"/> for
2072 more details.</para>
2073 </listitem>
2074 </varlistentry>
2075
2076 <varlistentry>
2077 <term>
2078 <literal>:print </literal> <replaceable>names</replaceable> ...
2079 <indexterm><primary><literal>:print</literal></primary></indexterm>
2080 </term>
2081 <listitem>
2082 <para>Prints a value without forcing its evaluation.
2083 <literal>:print</literal> may be used on values whose types are
2084 unkonwn or partially known, which might be the case for local
2085 variables with polymorphic types at a breakpoint. While inspecting
2086 the runtime value, <literal>:print</literal> attempts to
2087 reconstruct the type of the value, and will elaborate the type in
2088 GHCi's environment if possible. If any unevaluated components
2089 (thunks) are encountered, then <literal>:print</literal> binds
2090 a fresh variable with a name beginning with <literal>_t</literal>
2091 to each thunk. See <xref linkend="breakpoints" /> for more
2092 information. See also the <literal>:sprint</literal> command,
2093 which works like <literal>:print</literal> but does not bind new
2094 variables.</para>
2095 </listitem>
2096 </varlistentry>
2097
2098 <varlistentry>
2099 <term>
2100 <literal>:quit</literal>
2101 <indexterm><primary><literal>:quit</literal></primary></indexterm>
2102 </term>
2103 <listitem>
2104 <para>Quits GHCi. You can also quit by typing a control-D
2105 at the prompt.</para>
2106 </listitem>
2107 </varlistentry>
2108
2109 <varlistentry>
2110 <term>
2111 <literal>:reload</literal>
2112 <indexterm><primary><literal>:reload</literal></primary></indexterm>
2113 </term>
2114 <listitem>
2115 <para>Attempts to reload the current target set (see
2116 <literal>:load</literal>) if any of the modules in the set,
2117 or any dependent module, has changed. Note that this may
2118 entail loading new modules, or dropping modules which are no
2119 longer indirectly required by the target.</para>
2120 </listitem>
2121 </varlistentry>
2122
2123 <varlistentry>
2124 <term>
2125 <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
2126 <indexterm><primary><literal>:set</literal></primary></indexterm>
2127 </term>
2128 <listitem>
2129 <para>Sets various options. See <xref linkend="ghci-set"/>
2130 for a list of available options. The
2131 <literal>:set</literal> command by itself shows which
2132 options are currently set.</para>
2133 </listitem>
2134 </varlistentry>
2135
2136 <varlistentry>
2137 <term>
2138 <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
2139 <indexterm><primary><literal>:set args</literal></primary></indexterm>
2140 </term>
2141 <listitem>
2142 <para>Sets the list of arguments which are returned when the
2143 program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
2144 </indexterm>.</para>
2145 </listitem>
2146 </varlistentry>
2147
2148 <varlistentry>
2149 <term>
2150 <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
2151 </term>
2152 <listitem>
2153 <para>Sets the command used by <literal>:edit</literal> to
2154 <replaceable>cmd</replaceable>.</para>
2155 </listitem>
2156 </varlistentry>
2157
2158 <varlistentry>
2159 <term>
2160 <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
2161 <indexterm><primary><literal>:set prog</literal></primary></indexterm>
2162 </term>
2163 <listitem>
2164 <para>Sets the string to be returned when the program calls
2165 <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
2166 </indexterm>.</para>
2167 </listitem>
2168 </varlistentry>
2169
2170 <varlistentry>
2171 <term>
2172 <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
2173 </term>
2174 <listitem>
2175 <para>Sets the string to be used as the prompt in GHCi.
2176 Inside <replaceable>prompt</replaceable>, the sequence
2177 <literal>%s</literal> is replaced by the names of the
2178 modules currently in scope, and <literal>%%</literal> is
2179 replaced by <literal>%</literal>.</para>
2180 </listitem>
2181 </varlistentry>
2182
2183 <varlistentry>
2184 <term>
2185 <literal>:set</literal> <literal>stop</literal>
2186 [<replaceable>num</replaceable>] <replaceable>cmd</replaceable>
2187 </term>
2188 <listitem>
2189 <para>Set a command to be executed when a breakpoint is hit, or a new
2190 item in the history is selected. The most common use of
2191 <literal>:set stop</literal> is to display the source code at the
2192 current location, e.g. <literal>:set stop :list</literal>.</para>
2193
2194 <para>If a number is given before the command, then the commands are
2195 run when the specified breakpoint (only) is hit. This can be quite
2196 useful: for example, <literal>:set stop 1 :continue</literal>
2197 effectively disables breakpoint 1, by running
2198 <literal>:continue</literal> whenever it is hit (although GHCi will
2199 still emit a message to say the breakpoint was hit). What's more,
2200 with cunning use of <literal>:def</literal> and
2201 <literal>:cmd</literal> you can use <literal>:set stop</literal> to
2202 implement conditional breakpoints:</para>
2203 <screen>
2204 *Main> :def cond \expr -> return (":cmd if (" ++ expr ++ ") then return \"\" else return \":continue\"")
2205 *Main> :set stop 0 :cond (x &lt; 3)
2206 </screen>
2207 <para>Ignoring breakpoints for a specified number of iterations is
2208 also possible using similar techniques.</para>
2209 </listitem>
2210 </varlistentry>
2211
2212 <varlistentry>
2213 <term>
2214 <literal>:show bindings</literal>
2215 <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
2216 </term>
2217 <listitem>
2218 <para>Show the bindings made at the prompt and their
2219 types.</para>
2220 </listitem>
2221 </varlistentry>
2222
2223 <varlistentry>
2224 <term>
2225 <literal>:show breaks</literal>
2226 <indexterm><primary><literal>:show breaks</literal></primary></indexterm>
2227 </term>
2228 <listitem>
2229 <para>List the active breakpoints.</para>
2230 </listitem>
2231 </varlistentry>
2232
2233 <varlistentry>
2234 <term>
2235 <literal>:show context</literal>
2236 <indexterm><primary><literal>:show context</literal></primary></indexterm>
2237 </term>
2238 <listitem>
2239 <para>List the active evaluations that are stopped at breakpoints.</para>
2240 </listitem>
2241 </varlistentry>
2242
2243 <varlistentry>
2244 <term>
2245 <literal>:show modules</literal>
2246 <indexterm><primary><literal>:show modules</literal></primary></indexterm>
2247 </term>
2248 <listitem>
2249 <para>Show the list of modules currently load.</para>
2250 </listitem>
2251 </varlistentry>
2252
2253 <varlistentry>
2254 <term>
2255 <literal>:show [args|prog|prompt|editor|stop]</literal>
2256 <indexterm><primary><literal>:show</literal></primary></indexterm>
2257 </term>
2258 <listitem>
2259 <para>Displays the specified setting (see
2260 <literal>:set</literal>).</para>
2261 </listitem>
2262 </varlistentry>
2263
2264 <varlistentry>
2265 <term>
2266 <literal>:sprint</literal>
2267 <indexterm><primary><literal>:sprint</literal></primary></indexterm>
2268 </term>
2269 <listitem>
2270 <para>Prints a value without forcing its evaluation.
2271 <literal>:sprint</literal> is similar to <literal>:print</literal>,
2272 with the difference that unevaluated subterms are not bound to new
2273 variables, they are simply denoted by &lsquo;_&rsquo;.</para>
2274 </listitem>
2275 </varlistentry>
2276
2277 <varlistentry>
2278 <term>
2279 <literal>:step [<replaceable>expr</replaceable>]</literal>
2280 <indexterm><primary><literal>:step</literal></primary></indexterm>
2281 </term>
2282 <listitem>
2283 <para>Single-step from the last breakpoint. With an expression
2284 argument, begins evaluation of the expression with a
2285 single-step.</para>
2286 </listitem>
2287 </varlistentry>
2288
2289 <varlistentry>
2290 <term>
2291 <literal>:trace [<replaceable>expr</replaceable>]</literal>
2292 <indexterm><primary><literal>:trace</literal></primary></indexterm>
2293 </term>
2294 <listitem>
2295 <para>Evaluates the given expression (or from the last breakpoint if
2296 no expression is given), and additionally logs the evaluation
2297 steps for later inspection using <literal>:history</literal>. See
2298 <xref linkend="tracing" />.</para>
2299 </listitem>
2300 </varlistentry>
2301
2302 <varlistentry>
2303 <term>
2304 <literal>:type</literal> <replaceable>expression</replaceable>
2305 <indexterm><primary><literal>:type</literal></primary></indexterm>
2306 </term>
2307 <listitem>
2308 <para>Infers and prints the type of
2309 <replaceable>expression</replaceable>, including explicit
2310 forall quantifiers for polymorphic types. The monomorphism
2311 restriction is <emphasis>not</emphasis> applied to the
2312 expression during type inference.</para>
2313 </listitem>
2314 </varlistentry>
2315
2316 <varlistentry>
2317 <term>
2318 <literal>:undef</literal> <replaceable>name</replaceable>
2319 <indexterm><primary><literal>:undef</literal></primary></indexterm>
2320 </term>
2321 <listitem>
2322 <para>Undefines the user-defined command
2323 <replaceable>name</replaceable> (see <literal>:def</literal>
2324 above).</para>
2325 </listitem>
2326 </varlistentry>
2327
2328 <varlistentry>
2329 <term>
2330 <literal>:unset</literal> <replaceable>option</replaceable>...
2331 <indexterm><primary><literal>:unset</literal></primary></indexterm>
2332 </term>
2333 <listitem>
2334 <para>Unsets certain options. See <xref linkend="ghci-set"/>
2335 for a list of available options.</para>
2336 </listitem>
2337 </varlistentry>
2338
2339 <varlistentry>
2340 <term>
2341 <literal>:!</literal> <replaceable>command</replaceable>...
2342 <indexterm><primary><literal>:!</literal></primary></indexterm>
2343 <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
2344 </term>
2345 <listitem>
2346 <para>Executes the shell command
2347 <replaceable>command</replaceable>.</para>
2348 </listitem>
2349 </varlistentry>
2350
2351 </variablelist>
2352 </sect1>
2353
2354 <sect1 id="ghci-set">
2355 <title>The <literal>:set</literal> command</title>
2356 <indexterm><primary><literal>:set</literal></primary></indexterm>
2357
2358 <para>The <literal>:set</literal> command sets two types of
2359 options: GHCi options, which begin with
2360 &lsquo;<literal>+</literal>&rdquo; and &ldquo;command-line&rdquo;
2361 options, which begin with &lsquo;-&rsquo;. </para>
2362
2363 <para>NOTE: at the moment, the <literal>:set</literal> command
2364 doesn't support any kind of quoting in its arguments: quotes will
2365 not be removed and cannot be used to group words together. For
2366 example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
2367 you expect.</para>
2368
2369 <sect2>
2370 <title>GHCi options</title>
2371 <indexterm><primary>options</primary><secondary>GHCi</secondary>
2372 </indexterm>
2373
2374 <para>GHCi options may be set using <literal>:set</literal> and
2375 unset using <literal>:unset</literal>.</para>
2376
2377 <para>The available GHCi options are:</para>
2378
2379 <variablelist>
2380 <varlistentry>
2381 <term>
2382 <literal>+r</literal>
2383 <indexterm><primary><literal>+r</literal></primary></indexterm>
2384 <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
2385 <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
2386 </term>
2387 <listitem>
2388 <para>Normally, any evaluation of top-level expressions
2389 (otherwise known as CAFs or Constant Applicative Forms) in
2390 loaded modules is retained between evaluations. Turning
2391 on <literal>+r</literal> causes all evaluation of
2392 top-level expressions to be discarded after each
2393 evaluation (they are still retained
2394 <emphasis>during</emphasis> a single evaluation).</para>
2395
2396 <para>This option may help if the evaluated top-level
2397 expressions are consuming large amounts of space, or if
2398 you need repeatable performance measurements.</para>
2399 </listitem>
2400 </varlistentry>
2401
2402 <varlistentry>
2403 <term>
2404 <literal>+s</literal>
2405 <indexterm><primary><literal>+s</literal></primary></indexterm>
2406 </term>
2407 <listitem>
2408 <para>Display some stats after evaluating each expression,
2409 including the elapsed time and number of bytes allocated.
2410 NOTE: the allocation figure is only accurate to the size
2411 of the storage manager's allocation area, because it is
2412 calculated at every GC. Hence, you might see values of
2413 zero if no GC has occurred.</para>
2414 </listitem>
2415 </varlistentry>
2416
2417 <varlistentry>
2418 <term>
2419 <literal>+t</literal>
2420 <indexterm><primary><literal>+t</literal></primary></indexterm>
2421 </term>
2422 <listitem>
2423 <para>Display the type of each variable bound after a
2424 statement is entered at the prompt. If the statement is a
2425 single expression, then the only variable binding will be
2426 for the variable
2427 &lsquo;<literal>it</literal>&rsquo;.</para>
2428 </listitem>
2429 </varlistentry>
2430 </variablelist>
2431 </sect2>
2432
2433 <sect2 id="ghci-cmd-line-options">
2434 <title>Setting GHC command-line options in GHCi</title>
2435
2436 <para>Normal GHC command-line options may also be set using
2437 <literal>:set</literal>. For example, to turn on
2438 <option>-fglasgow-exts</option>, you would say:</para>
2439
2440 <screen>
2441 Prelude> :set -fglasgow-exts
2442 </screen>
2443
2444 <para>Any GHC command-line option that is designated as
2445 <firstterm>dynamic</firstterm> (see the table in <xref
2446 linkend="flag-reference"/>), may be set using
2447 <literal>:set</literal>. To unset an option, you can set the
2448 reverse option:</para>
2449 <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
2450
2451 <screen>
2452 Prelude> :set -fno-glasgow-exts
2453 </screen>
2454
2455 <para><xref linkend="flag-reference"/> lists the reverse for each
2456 option where applicable.</para>
2457
2458 <para>Certain static options (<option>-package</option>,
2459 <option>-I</option>, <option>-i</option>, and
2460 <option>-l</option> in particular) will also work, but some may
2461 not take effect until the next reload.</para>
2462 <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
2463 </sect2>
2464 </sect1>
2465 <sect1 id="ghci-dot-files">
2466 <title>The <filename>.ghci</filename> file</title>
2467 <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
2468 </indexterm>
2469 <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
2470 </indexterm>
2471
2472 <para>When it starts, GHCi always reads and executes commands from
2473 <filename>$HOME/.ghci</filename>, followed by
2474 <filename>./.ghci</filename>.</para>
2475
2476 <para>The <filename>.ghci</filename> in your home directory is
2477 most useful for turning on favourite options (eg. <literal>:set
2478 +s</literal>), and defining useful macros. Placing a
2479 <filename>.ghci</filename> file in a directory with a Haskell
2480 project is a useful way to set certain project-wide options so you
2481 don't have to type them everytime you start GHCi: eg. if your
2482 project uses GHC extensions and CPP, and has source files in three
2483 subdirectories A B and C, you might put the following lines in
2484 <filename>.ghci</filename>:</para>
2485
2486 <screen>
2487 :set -fglasgow-exts -cpp
2488 :set -iA:B:C
2489 </screen>
2490
2491 <para>(Note that strictly speaking the <option>-i</option> flag is
2492 a static one, but in fact it works to set it using
2493 <literal>:set</literal> like this. The changes won't take effect
2494 until the next <literal>:load</literal>, though.)</para>
2495
2496 <para>Two command-line options control whether the
2497 <filename>.ghci</filename> files are read:</para>
2498
2499 <variablelist>
2500 <varlistentry>
2501 <term>
2502 <option>-ignore-dot-ghci</option>
2503 <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
2504 </term>
2505 <listitem>
2506 <para>Don't read either <filename>./.ghci</filename> or
2507 <filename>$HOME/.ghci</filename> when starting up.</para>
2508 </listitem>
2509 </varlistentry>
2510 <varlistentry>
2511 <term>
2512 <option>-read-dot-ghci</option>
2513 <indexterm><primary><option>-read-dot-ghci</option></primary></indexterm>
2514 </term>
2515 <listitem>
2516 <para>Read <filename>.ghci</filename> and
2517 <filename>$HOME/.ghci</filename>. This is normally the
2518 default, but the <option>-read-dot-ghci</option> option may
2519 be used to override a previous
2520 <option>-ignore-dot-ghci</option> option.</para>
2521 </listitem>
2522 </varlistentry>
2523 </variablelist>
2524
2525 </sect1>
2526
2527 <sect1 id="ghci-obj">
2528 <title>Compiling to object code inside GHCi</title>
2529
2530 <para>By default, GHCi compiles Haskell source code into byte-code
2531 that is interpreted by the runtime system. GHCi can also compile
2532 Haskell code to object code: to turn on this feature, use the
2533 <option>-fobject-code</option> flag either on the command line or
2534 with <literal>:set</literal> (the option
2535 <option>-fbyte-code</option> restores byte-code compilation
2536 again). Compiling to object code takes longer, but typically the
2537 code will execute 10-20 times faster than byte-code.</para>
2538
2539 <para>Compiling to object code inside GHCi is particularly useful
2540 if you are developing a compiled application, because the
2541 <literal>:reload</literal> command typically runs much faster than
2542 restarting GHC with <option>--make</option> from the command-line,
2543 because all the interface files are already cached in
2544 memory.</para>
2545
2546 <para>There are disadvantages to compiling to object-code: you
2547 can't set breakpoints in object-code modules, for example. Only
2548 the exports of an object-code module will be visible in GHCi,
2549 rather than all top-level bindings as in interpreted
2550 modules.</para>
2551 </sect1>
2552
2553 <sect1 id="ghci-faq">
2554 <title>FAQ and Things To Watch Out For</title>
2555
2556 <variablelist>
2557 <varlistentry>
2558 <term>The interpreter can't load modules with foreign export
2559 declarations!</term>
2560 <listitem>
2561 <para>Unfortunately not. We haven't implemented it yet.
2562 Please compile any offending modules by hand before loading
2563 them into GHCi.</para>
2564 </listitem>
2565 </varlistentry>
2566
2567 <varlistentry>
2568 <term>
2569 <literal>-O</literal> doesn't work with GHCi!
2570 <indexterm><primary><option>-O</option></primary></indexterm>
2571 </term>
2572 <listitem>
2573 <para>For technical reasons, the bytecode compiler doesn't
2574 interact well with one of the optimisation passes, so we
2575 have disabled optimisation when using the interpreter. This
2576 isn't a great loss: you'll get a much bigger win by
2577 compiling the bits of your code that need to go fast, rather
2578 than interpreting them with optimisation turned on.</para>
2579 </listitem>
2580 </varlistentry>
2581
2582 <varlistentry>
2583 <term>Unboxed tuples don't work with GHCi</term>
2584 <listitem>
2585 <para>That's right. You can always compile a module that
2586 uses unboxed tuples and load it into GHCi, however.
2587 (Incidentally the previous point, namely that
2588 <literal>-O</literal> is incompatible with GHCi, is because
2589 the bytecode compiler can't deal with unboxed
2590 tuples).</para>
2591 </listitem>
2592 </varlistentry>
2593
2594 <varlistentry>
2595 <term>Concurrent threads don't carry on running when GHCi is
2596 waiting for input.</term>
2597 <listitem>
2598 <para>This should work, as long as your GHCi was built with
2599 the <option>-threaded</option> switch, which is the default.
2600 Consult whoever supplied your GHCi installation.</para>
2601 </listitem>
2602 </varlistentry>
2603
2604 <varlistentry>
2605 <term>After using <literal>getContents</literal>, I can't use
2606 <literal>stdin</literal> again until I do
2607 <literal>:load</literal> or <literal>:reload</literal>.</term>
2608
2609 <listitem>
2610 <para>This is the defined behaviour of
2611 <literal>getContents</literal>: it puts the stdin Handle in
2612 a state known as <firstterm>semi-closed</firstterm>, wherein
2613 any further I/O operations on it are forbidden. Because I/O
2614 state is retained between computations, the semi-closed
2615 state persists until the next <literal>:load</literal> or
2616 <literal>:reload</literal> command.</para>
2617
2618 <para>You can make <literal>stdin</literal> reset itself
2619 after every evaluation by giving GHCi the command
2620 <literal>:set +r</literal>. This works because
2621 <literal>stdin</literal> is just a top-level expression that
2622 can be reverted to its unevaluated state in the same way as
2623 any other top-level expression (CAF).</para>
2624 </listitem>
2625 </varlistentry>
2626
2627 <varlistentry>
2628 <term>I can't use Control-C to interrupt computations in
2629 GHCi on Windows.</term>
2630 <listitem>
2631 <para>See <xref linkend="ghci-windows"/></para>
2632 </listitem>
2633 </varlistentry>
2634 </variablelist>
2635 </sect1>
2636
2637 </chapter>
2638
2639 <!-- Emacs stuff:
2640 ;;; Local Variables: ***
2641 ;;; mode: xml ***
2642 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
2643 ;;; End: ***
2644 -->