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