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