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 <sect2 id="ghci-interactive-print">
1094 <title>Using a custom interactive printing function</title>
1095 <para>[<emphasis role="bold">New in version 7.6.1</emphasis>]
1096 By default, GHCi prints the result of expressions typed at the prompt
1097 using the function <literal>System.IO.print</literal>. Its type
1098 signature is <literal>Show a => a -> IO ()</literal>, and it works by
1099 converting the value to <literal>String</literal> using
1100 <literal>show</literal>.
1101 </para>
1102 <para>
1103 This is not ideal in certain cases, like when the output is long, or
1104 contains strings with non-ascii characters.
1105 </para>
1106 <para>
1107 The <literal>-interactive-print</literal> flag allows to specify any
1108 function of type <literal>C a => a -> IO ()</literal>, for some
1109 constraint <literal>C</literal>, as the function for printing evaluated
1110 expressions. The function can reside in any loaded module or any
1111 registered package.
1112 </para>
1113 <para>
1114 As an example, suppose we have following special printing module:
1115 <programlisting>
1116 module SpecPrinter where
1117 import System.IO
1118
1119 sprint a = putStrLn $ show a ++ "!"
1120 </programlisting>
1121 The <literal>sprint</literal> function adds an exclamation mark at the
1122 end of any printed value. Running GHCi with the command:
1123 <programlisting>
1124 ghci -interactive-print=SpecPrinter.sprinter SpecPrinter
1125 </programlisting>
1126 will start an interactive session where values with be printed using
1127 <literal>sprint</literal>:
1128 <programlisting>
1129 *SpecPrinter> [1,2,3]
1130 [1,2,3]!
1131 *SpecPrinter> 42
1132 42!
1133 </programlisting>
1134 </para>
1135 <para>
1136 A custom pretty printing function can be used, for example, to format
1137 tree-like and nested structures in a more readable way.
1138 </para>
1139 <para>
1140 The <literal>-interactive-print</literal> flag can also be used when
1141 running GHC in <literal>-e mode</literal>:
1142 <programlisting>
1143 % ghc -e "[1,2,3]" -interactive-print=SpecPrinter.sprint SpecPrinter
1144 [1,2,3]!
1145 </programlisting>
1146 </para>
1147 </sect2>
1148 </sect1>
1149
1150 <sect1 id="ghci-debugger">
1151 <title>The GHCi Debugger</title>
1152 <indexterm><primary>debugger</primary><secondary>in GHCi</secondary>
1153 </indexterm>
1154
1155 <para>GHCi contains a simple imperative-style debugger in which you can
1156 stop a running computation in order to examine the values of
1157 variables. The debugger is integrated into GHCi, and is turned on by
1158 default: no flags are required to enable the debugging
1159 facilities. There is one major restriction: breakpoints and
1160 single-stepping are only available in interpreted modules;
1161 compiled code is invisible to the debugger<footnote><para>Note that packages
1162 only contain compiled code, so debugging a package requires
1163 finding its source and loading that directly.</para></footnote>.</para>
1164
1165 <para>The debugger provides the following:
1166 <itemizedlist>
1167 <listitem>
1168 <para>The ability to set a <firstterm>breakpoint</firstterm> on a
1169 function definition or expression in the program. When the function
1170 is called, or the expression evaluated, GHCi suspends
1171 execution and returns to the prompt, where you can inspect the
1172 values of local variables before continuing with the
1173 execution.</para>
1174 </listitem>
1175 <listitem>
1176 <para>Execution can be <firstterm>single-stepped</firstterm>: the
1177 evaluator will suspend execution approximately after every
1178 reduction, allowing local variables to be inspected. This is
1179 equivalent to setting a breakpoint at every point in the
1180 program.</para>
1181 </listitem>
1182 <listitem>
1183 <para>Execution can take place in <firstterm>tracing
1184 mode</firstterm>, in which the evaluator remembers each
1185 evaluation step as it happens, but doesn't suspend execution until
1186 an actual breakpoint is reached. When this happens, the history of
1187 evaluation steps can be inspected.</para>
1188 </listitem>
1189 <listitem>
1190 <para>Exceptions (e.g. pattern matching failure and
1191 <literal>error</literal>) can be treated as breakpoints, to help
1192 locate the source of an exception in the program.</para>
1193 </listitem>
1194 </itemizedlist>
1195 </para>
1196
1197 <para>There is currently no support for obtaining a &ldquo;stack
1198 trace&rdquo;, but the tracing and history features provide a
1199 useful second-best, which will often be enough to establish the
1200 context of an error. For instance, it is possible to break
1201 automatically when an exception is thrown, even if it is thrown
1202 from within compiled code (see <xref
1203 linkend="ghci-debugger-exceptions" />).</para>
1204
1205 <sect2 id="breakpoints">
1206 <title>Breakpoints and inspecting variables</title>
1207
1208 <para>Let's use quicksort as a running example. Here's the code:</para>
1209
1210 <programlisting>
1211 qsort [] = []
1212 qsort (a:as) = qsort left ++ [a] ++ qsort right
1213 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
1214
1215 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1216 </programlisting>
1217
1218 <para>First, load the module into GHCi:</para>
1219
1220 <screen>
1221 Prelude> :l qsort.hs
1222 [1 of 1] Compiling Main ( qsort.hs, interpreted )
1223 Ok, modules loaded: Main.
1224 *Main>
1225 </screen>
1226
1227 <para>Now, let's set a breakpoint on the right-hand-side of the second
1228 equation of qsort:</para>
1229
1230 <programlisting>
1231 *Main> :break 2
1232 Breakpoint 0 activated at qsort.hs:2:15-46
1233 *Main>
1234 </programlisting>
1235
1236 <para>The command <literal>:break 2</literal> sets a breakpoint on line
1237 2 of the most recently-loaded module, in this case
1238 <literal>qsort.hs</literal>. Specifically, it picks the
1239 leftmost complete subexpression on that line on which to set the
1240 breakpoint, which in this case is the expression
1241 <literal>(qsort left ++ [a] ++ qsort right)</literal>.</para>
1242
1243 <para>Now, we run the program:</para>
1244
1245 <programlisting>
1246 *Main> main
1247 Stopped at qsort.hs:2:15-46
1248 _result :: [a]
1249 a :: a
1250 left :: [a]
1251 right :: [a]
1252 [qsort.hs:2:15-46] *Main>
1253 </programlisting>
1254
1255 <para>Execution has stopped at the breakpoint. The prompt has changed to
1256 indicate that we are currently stopped at a breakpoint, and the location:
1257 <literal>[qsort.hs:2:15-46]</literal>. To further clarify the
1258 location, we can use the <literal>:list</literal> command:</para>
1259
1260 <programlisting>
1261 [qsort.hs:2:15-46] *Main> :list
1262 1 qsort [] = []
1263 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
1264 3 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
1265 </programlisting>
1266
1267 <para>The <literal>:list</literal> command lists the source code around
1268 the current breakpoint. If your output device supports it, then GHCi
1269 will highlight the active subexpression in bold.</para>
1270
1271 <para>GHCi has provided bindings for the free variables<footnote><para>We
1272 originally provided bindings for all variables in scope, rather
1273 than just
1274 the free variables of the expression, but found that this affected
1275 performance considerably, hence the current restriction to just the
1276 free variables.</para>
1277 </footnote> of the expression
1278 on which the
1279 breakpoint was placed (<literal>a</literal>, <literal>left</literal>,
1280 <literal>right</literal>), and additionally a binding for the result of
1281 the expression (<literal>_result</literal>). These variables are just
1282 like other variables that you might define in GHCi; you
1283 can use them in expressions that you type at the prompt, you can ask
1284 for their types with <literal>:type</literal>, and so on. There is one
1285 important difference though: these variables may only have partial
1286 types. For example, if we try to display the value of
1287 <literal>left</literal>:</para>
1288
1289 <screen>
1290 [qsort.hs:2:15-46] *Main> left
1291
1292 &lt;interactive&gt;:1:0:
1293 Ambiguous type variable `a' in the constraint:
1294 `Show a' arising from a use of `print' at &lt;interactive&gt;:1:0-3
1295 Cannot resolve unknown runtime types: a
1296 Use :print or :force to determine these types
1297 </screen>
1298
1299 <para>This is because <literal>qsort</literal> is a polymorphic function,
1300 and because GHCi does not carry type information at runtime, it cannot
1301 determine the runtime types of free variables that involve type
1302 variables. Hence, when you ask to display <literal>left</literal> at
1303 the prompt, GHCi can't figure out which instance of
1304 <literal>Show</literal> to use, so it emits the type error above.</para>
1305
1306 <para>Fortunately, the debugger includes a generic printing command,
1307 <literal>:print</literal>, which can inspect the actual runtime value of a
1308 variable and attempt to reconstruct its type. If we try it on
1309 <literal>left</literal>:</para>
1310
1311 <screen>
1312 [qsort.hs:2:15-46] *Main> :set -fprint-evld-with-show
1313 [qsort.hs:2:15-46] *Main> :print left
1314 left = (_t1::[a])
1315 </screen>
1316
1317 <para>This isn't particularly enlightening. What happened is that
1318 <literal>left</literal> is bound to an unevaluated computation (a
1319 suspension, or <firstterm>thunk</firstterm>), and
1320 <literal>:print</literal> does not force any evaluation. The idea is
1321 that <literal>:print</literal> can be used to inspect values at a
1322 breakpoint without any unfortunate side effects. It won't force any
1323 evaluation, which could cause the program to give a different answer
1324 than it would normally, and hence it won't cause any exceptions to be
1325 raised, infinite loops, or further breakpoints to be triggered (see
1326 <xref linkend="nested-breakpoints" />).
1327 Rather than forcing thunks, <literal>:print</literal>
1328 binds each thunk to a fresh variable beginning with an
1329 underscore, in this case
1330 <literal>_t1</literal>.</para>
1331
1332 <para>The flag <literal>-fprint-evld-with-show</literal> instructs
1333 <literal>:print</literal> to reuse
1334 available <literal>Show</literal> instances when possible. This happens
1335 only when the contents of the variable being inspected
1336 are completely evaluated.</para>
1337
1338
1339 <para>If we aren't concerned about preserving the evaluatedness of a
1340 variable, we can use <literal>:force</literal> instead of
1341 <literal>:print</literal>. The <literal>:force</literal> command
1342 behaves exactly like <literal>:print</literal>, except that it forces
1343 the evaluation of any thunks it encounters:</para>
1344
1345 <screen>
1346 [qsort.hs:2:15-46] *Main> :force left
1347 left = [4,0,3,1]
1348 </screen>
1349
1350 <para>Now, since <literal>:force</literal> has inspected the runtime
1351 value of <literal>left</literal>, it has reconstructed its type. We
1352 can see the results of this type reconstruction:</para>
1353
1354 <screen>
1355 [qsort.hs:2:15-46] *Main> :show bindings
1356 _result :: [Integer]
1357 a :: Integer
1358 left :: [Integer]
1359 right :: [Integer]
1360 _t1 :: [Integer]
1361 </screen>
1362
1363 <para>Not only do we now know the type of <literal>left</literal>, but
1364 all the other partial types have also been resolved. So we can ask
1365 for the value of <literal>a</literal>, for example:</para>
1366
1367 <screen>
1368 [qsort.hs:2:15-46] *Main> a
1369 8
1370 </screen>
1371
1372 <para>You might find it useful to use Haskell's
1373 <literal>seq</literal> function to evaluate individual thunks rather
1374 than evaluating the whole expression with <literal>:force</literal>.
1375 For example:</para>
1376
1377 <screen>
1378 [qsort.hs:2:15-46] *Main> :print right
1379 right = (_t1::[Integer])
1380 [qsort.hs:2:15-46] *Main> seq _t1 ()
1381 ()
1382 [qsort.hs:2:15-46] *Main> :print right
1383 right = 23 : (_t2::[Integer])
1384 </screen>
1385
1386 <para>We evaluated only the <literal>_t1</literal> thunk, revealing the
1387 head of the list, and the tail is another thunk now bound to
1388 <literal>_t2</literal>. The <literal>seq</literal> function is a
1389 little inconvenient to use here, so you might want to use
1390 <literal>:def</literal> to make a nicer interface (left as an exercise
1391 for the reader!).</para>
1392
1393 <para>Finally, we can continue the current execution:</para>
1394
1395 <screen>
1396 [qsort.hs:2:15-46] *Main> :continue
1397 Stopped at qsort.hs:2:15-46
1398 _result :: [a]
1399 a :: a
1400 left :: [a]
1401 right :: [a]
1402 [qsort.hs:2:15-46] *Main>
1403 </screen>
1404
1405 <para>The execution continued at the point it previously stopped, and has
1406 now stopped at the breakpoint for a second time.</para>
1407
1408
1409 <sect3 id="setting-breakpoints">
1410 <title>Setting breakpoints</title>
1411
1412 <para>Breakpoints can be set in various ways. Perhaps the easiest way to
1413 set a breakpoint is to name a top-level function:</para>
1414
1415 <screen>
1416 :break <replaceable>identifier</replaceable>
1417 </screen>
1418
1419 <para>Where <replaceable>identifier</replaceable> names any top-level
1420 function in an interpreted module currently loaded into GHCi (qualified
1421 names may be used). The breakpoint will be set on the body of the
1422 function, when it is fully applied but before any pattern matching has
1423 taken place.</para>
1424
1425 <para>Breakpoints can also be set by line (and optionally column)
1426 number:</para>
1427
1428 <screen>
1429 :break <replaceable>line</replaceable>
1430 :break <replaceable>line</replaceable> <replaceable>column</replaceable>
1431 :break <replaceable>module</replaceable> <replaceable>line</replaceable>
1432 :break <replaceable>module</replaceable> <replaceable>line</replaceable> <replaceable>column</replaceable>
1433 </screen>
1434
1435 <para>When a breakpoint is set on a particular line, GHCi sets the
1436 breakpoint on the
1437 leftmost subexpression that begins and ends on that line. If two
1438 complete subexpressions start at the same
1439 column, the longest one is picked. If there is no complete
1440 subexpression on the line, then the leftmost expression starting on
1441 the line is picked, and failing that the rightmost expression that
1442 partially or completely covers the line.</para>
1443
1444 <para>When a breakpoint is set on a particular line and column, GHCi
1445 picks the smallest subexpression that encloses that location on which
1446 to set the breakpoint. Note: GHC considers the TAB character to have a
1447 width of 1, wherever it occurs; in other words it counts
1448 characters, rather than columns. This matches what some editors do,
1449 and doesn't match others. The best advice is to avoid tab
1450 characters in your source code altogether (see
1451 <option>-fwarn-tabs</option> in <xref linkend="options-sanity"
1452 />).</para>
1453
1454 <para>If the module is omitted, then the most recently-loaded module is
1455 used.</para>
1456
1457 <para>Not all subexpressions are potential breakpoint locations. Single
1458 variables are typically not considered to be breakpoint locations
1459 (unless the variable is the right-hand-side of a function definition,
1460 lambda, or case alternative). The rule of thumb is that all redexes
1461 are breakpoint locations, together with the bodies of functions,
1462 lambdas, case alternatives and binding statements. There is normally
1463 no breakpoint on a let expression, but there will always be a
1464 breakpoint on its body, because we are usually interested in inspecting
1465 the values of the variables bound by the let.</para>
1466
1467 </sect3>
1468 <sect3>
1469 <title>Listing and deleting breakpoints</title>
1470
1471 <para>The list of breakpoints currently enabled can be displayed using
1472 <literal>:show&nbsp;breaks</literal>:</para>
1473 <screen>
1474 *Main> :show breaks
1475 [0] Main qsort.hs:1:11-12
1476 [1] Main qsort.hs:2:15-46
1477 </screen>
1478
1479 <para>To delete a breakpoint, use the <literal>:delete</literal>
1480 command with the number given in the output from <literal>:show&nbsp;breaks</literal>:</para>
1481
1482 <screen>
1483 *Main> :delete 0
1484 *Main> :show breaks
1485 [1] Main qsort.hs:2:15-46
1486 </screen>
1487
1488 <para>To delete all breakpoints at once, use <literal>:delete *</literal>.</para>
1489
1490 </sect3>
1491 </sect2>
1492
1493 <sect2 id="single-stepping">
1494 <title>Single-stepping</title>
1495
1496 <para>Single-stepping is a great way to visualise the execution of your
1497 program, and it is also a useful tool for identifying the source of a
1498 bug. GHCi offers two variants of stepping. Use
1499 <literal>:step</literal> to enable all the
1500 breakpoints in the program, and execute until the next breakpoint is
1501 reached. Use <literal>:steplocal</literal> to limit the set
1502 of enabled breakpoints to those in the current top level function.
1503 Similarly, use <literal>:stepmodule</literal> to single step only on
1504 breakpoints contained in the current module.
1505 For example:</para>
1506
1507 <screen>
1508 *Main> :step main
1509 Stopped at qsort.hs:5:7-47
1510 _result :: IO ()
1511 </screen>
1512
1513 <para>The command <literal>:step
1514 <replaceable>expr</replaceable></literal> begins the evaluation of
1515 <replaceable>expr</replaceable> in single-stepping mode. If
1516 <replaceable>expr</replaceable> is omitted, then it single-steps from
1517 the current breakpoint. <literal>:stepover</literal>
1518 works similarly.</para>
1519
1520 <para>The <literal>:list</literal> command is particularly useful when
1521 single-stepping, to see where you currently are:</para>
1522
1523 <screen>
1524 [qsort.hs:5:7-47] *Main> :list
1525 4
1526 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1527 6
1528 [qsort.hs:5:7-47] *Main>
1529 </screen>
1530
1531 <para>In fact, GHCi provides a way to run a command when a breakpoint is
1532 hit, so we can make it automatically do
1533 <literal>:list</literal>:</para>
1534
1535 <screen>
1536 [qsort.hs:5:7-47] *Main> :set stop :list
1537 [qsort.hs:5:7-47] *Main> :step
1538 Stopped at qsort.hs:5:14-46
1539 _result :: [Integer]
1540 4
1541 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1542 6
1543 [qsort.hs:5:14-46] *Main>
1544 </screen>
1545 </sect2>
1546
1547 <sect2 id="nested-breakpoints">
1548 <title>Nested breakpoints</title>
1549 <para>When GHCi is stopped at a breakpoint, and an expression entered at
1550 the prompt triggers a
1551 second breakpoint, the new breakpoint becomes the &ldquo;current&rdquo;
1552 one, and the old one is saved on a stack. An arbitrary number of
1553 breakpoint contexts can be built up in this way. For example:</para>
1554
1555 <screen>
1556 [qsort.hs:2:15-46] *Main> :st qsort [1,3]
1557 Stopped at qsort.hs:(1,0)-(3,55)
1558 _result :: [a]
1559 ... [qsort.hs:(1,0)-(3,55)] *Main>
1560 </screen>
1561
1562 <para>While stopped at the breakpoint on line 2 that we set earlier, we
1563 started a new evaluation with <literal>:step qsort [1,3]</literal>.
1564 This new evaluation stopped after one step (at the definition of
1565 <literal>qsort</literal>). The prompt has changed, now prefixed with
1566 <literal>...</literal>, to indicate that there are saved breakpoints
1567 beyond the current one. To see the stack of contexts, use
1568 <literal>:show context</literal>:</para>
1569
1570 <screen>
1571 ... [qsort.hs:(1,0)-(3,55)] *Main> :show context
1572 --> main
1573 Stopped at qsort.hs:2:15-46
1574 --> qsort [1,3]
1575 Stopped at qsort.hs:(1,0)-(3,55)
1576 ... [qsort.hs:(1,0)-(3,55)] *Main>
1577 </screen>
1578
1579 <para>To abandon the current evaluation, use
1580 <literal>:abandon</literal>:</para>
1581
1582 <screen>
1583 ... [qsort.hs:(1,0)-(3,55)] *Main> :abandon
1584 [qsort.hs:2:15-46] *Main> :abandon
1585 *Main>
1586 </screen>
1587 </sect2>
1588
1589 <sect2 id="ghci-debugger-result">
1590 <title>The <literal>_result</literal> variable</title>
1591 <para>When stopped at a breakpoint or single-step, GHCi binds the
1592 variable <literal>_result</literal> to the value of the currently
1593 active expression. The value of <literal>_result</literal> is
1594 presumably not available yet, because we stopped its evaluation, but it
1595 can be forced: if the type is known and showable, then just entering
1596 <literal>_result</literal> at the prompt will show it. However,
1597 there's one caveat to doing this: evaluating <literal>_result</literal>
1598 will be likely to trigger further breakpoints, starting with the
1599 breakpoint we are currently stopped at (if we stopped at a real
1600 breakpoint, rather than due to <literal>:step</literal>). So it will
1601 probably be necessary to issue a <literal>:continue</literal>
1602 immediately when evaluating <literal>_result</literal>. Alternatively,
1603 you can use <literal>:force</literal> which ignores breakpoints.</para>
1604 </sect2>
1605
1606 <sect2 id="tracing">
1607 <title>Tracing and history</title>
1608
1609 <para>A question that we often want to ask when debugging a program is
1610 &ldquo;how did I get here?&rdquo;. Traditional imperative debuggers
1611 usually provide some kind of stack-tracing feature that lets you see
1612 the stack of active function calls (sometimes called the &ldquo;lexical
1613 call stack&rdquo;), describing a path through the code
1614 to the current location. Unfortunately this is hard to provide in
1615 Haskell, because execution proceeds on a demand-driven basis, rather
1616 than a depth-first basis as in strict languages. The
1617 &ldquo;stack&ldquo; in GHC's execution engine bears little
1618 resemblance to the lexical call stack. Ideally GHCi would maintain a
1619 separate lexical call stack in addition to the dynamic call stack, and
1620 in fact this is exactly
1621 what our profiling system does (<xref linkend="profiling" />), and what
1622 some other Haskell debuggers do. For the time being, however, GHCi
1623 doesn't maintain a lexical call stack (there are some technical
1624 challenges to be overcome). Instead, we provide a way to backtrack from a
1625 breakpoint to previous evaluation steps: essentially this is like
1626 single-stepping backwards, and should in many cases provide enough
1627 information to answer the &ldquo;how did I get here?&rdquo;
1628 question.</para>
1629
1630 <para>To use tracing, evaluate an expression with the
1631 <literal>:trace</literal> command. For example, if we set a breakpoint
1632 on the base case of <literal>qsort</literal>:</para>
1633
1634 <screen>
1635 *Main&gt; :list qsort
1636 1 qsort [] = []
1637 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
1638 3 where (left,right) = (filter (&lt;=a) as, filter (&gt;a) as)
1639 4
1640 *Main&gt; :b 1
1641 Breakpoint 1 activated at qsort.hs:1:11-12
1642 *Main&gt;
1643 </screen>
1644
1645 <para>and then run a small <literal>qsort</literal> with
1646 tracing:</para>
1647
1648 <screen>
1649 *Main> :trace qsort [3,2,1]
1650 Stopped at qsort.hs:1:11-12
1651 _result :: [a]
1652 [qsort.hs:1:11-12] *Main>
1653 </screen>
1654
1655 <para>We can now inspect the history of evaluation steps:</para>
1656
1657 <screen>
1658 [qsort.hs:1:11-12] *Main> :hist
1659 -1 : qsort.hs:3:24-38
1660 -2 : qsort.hs:3:23-55
1661 -3 : qsort.hs:(1,0)-(3,55)
1662 -4 : qsort.hs:2:15-24
1663 -5 : qsort.hs:2:15-46
1664 -6 : qsort.hs:3:24-38
1665 -7 : qsort.hs:3:23-55
1666 -8 : qsort.hs:(1,0)-(3,55)
1667 -9 : qsort.hs:2:15-24
1668 -10 : qsort.hs:2:15-46
1669 -11 : qsort.hs:3:24-38
1670 -12 : qsort.hs:3:23-55
1671 -13 : qsort.hs:(1,0)-(3,55)
1672 -14 : qsort.hs:2:15-24
1673 -15 : qsort.hs:2:15-46
1674 -16 : qsort.hs:(1,0)-(3,55)
1675 &lt;end of history&gt;
1676 </screen>
1677
1678 <para>To examine one of the steps in the history, use
1679 <literal>:back</literal>:</para>
1680
1681 <screen>
1682 [qsort.hs:1:11-12] *Main> :back
1683 Logged breakpoint at qsort.hs:3:24-38
1684 _result :: [a]
1685 as :: [a]
1686 a :: a
1687 [-1: qsort.hs:3:24-38] *Main>
1688 </screen>
1689
1690 <para>Note that the local variables at each step in the history have been
1691 preserved, and can be examined as usual. Also note that the prompt has
1692 changed to indicate that we're currently examining the first step in
1693 the history: <literal>-1</literal>. The command
1694 <literal>:forward</literal> can be used to traverse forward in the
1695 history.</para>
1696
1697 <para>The <literal>:trace</literal> command can be used with or without
1698 an expression. When used without an expression, tracing begins from
1699 the current breakpoint, just like <literal>:step</literal>.</para>
1700
1701 <para>The history is only available when
1702 using <literal>:trace</literal>; the reason for this is we found that
1703 logging each breakpoint in the history cuts performance by a factor of
1704 2 or more. GHCi remembers the last 50 steps in the history (perhaps in
1705 the future we'll make this configurable).</para>
1706 </sect2>
1707
1708 <sect2 id="ghci-debugger-exceptions">
1709 <title>Debugging exceptions</title>
1710 <para>Another common question that comes up when debugging is
1711 &ldquo;where did this exception come from?&rdquo;. Exceptions such as
1712 those raised by <literal>error</literal> or <literal>head []</literal>
1713 have no context information attached to them. Finding which
1714 particular call to <literal>head</literal> in your program resulted in
1715 the error can be a painstaking process, usually involving
1716 <literal>Debug.Trace.trace</literal>, or compiling with
1717 profiling and using <literal>Debug.Trace.traceStack</literal>
1718 or <literal>+RTS -xc</literal> (see <xref
1719 linkend="prof-time-options" />).</para>
1720
1721 <para>The GHCi debugger offers a way to hopefully shed some light on
1722 these errors quickly and without modifying or recompiling the source
1723 code. One way would be to set a breakpoint on the location in the
1724 source code that throws the exception, and then use
1725 <literal>:trace</literal> and <literal>:history</literal> to establish
1726 the context. However, <literal>head</literal> is in a library and
1727 we can't set a breakpoint on it directly. For this reason, GHCi
1728 provides the flags <literal>-fbreak-on-exception</literal> which causes
1729 the evaluator to stop when an exception is thrown, and <literal>
1730 -fbreak-on-error</literal>, which works similarly but stops only on
1731 uncaught exceptions. When stopping at an exception, GHCi will act
1732 just as it does when a breakpoint is hit, with the deviation that it
1733 will not show you any source code location. Due to this, these
1734 commands are only really useful in conjunction with
1735 <literal>:trace</literal>, in order to log the steps leading up to the
1736 exception. For example:</para>
1737
1738 <screen>
1739 *Main> :set -fbreak-on-exception
1740 *Main> :trace qsort ("abc" ++ undefined)
1741 &ldquo;Stopped at &lt;exception thrown&gt;
1742 _exception :: e
1743 [&lt;exception thrown&gt;] *Main&gt; :hist
1744 -1 : qsort.hs:3:24-38
1745 -2 : qsort.hs:3:23-55
1746 -3 : qsort.hs:(1,0)-(3,55)
1747 -4 : qsort.hs:2:15-24
1748 -5 : qsort.hs:2:15-46
1749 -6 : qsort.hs:(1,0)-(3,55)
1750 &lt;end of history&gt;
1751 [&lt;exception thrown&gt;] *Main&gt; :back
1752 Logged breakpoint at qsort.hs:3:24-38
1753 _result :: [a]
1754 as :: [a]
1755 a :: a
1756 [-1: qsort.hs:3:24-38] *Main&gt; :force as
1757 *** Exception: Prelude.undefined
1758 [-1: qsort.hs:3:24-38] *Main&gt; :print as
1759 as = 'b' : 'c' : (_t1::[Char])
1760 </screen>
1761
1762 <para>The exception itself is bound to a new variable,
1763 <literal>_exception</literal>.</para>
1764
1765 <para>Breaking on exceptions is particularly useful for finding out what
1766 your program was doing when it was in an infinite loop. Just hit
1767 Control-C, and examine the history to find out what was going
1768 on.</para>
1769 </sect2>
1770
1771 <sect2><title>Example: inspecting functions</title>
1772 <para>
1773 It is possible to use the debugger to examine function values.
1774 When we are at a breakpoint and a function is in scope, the debugger
1775 cannot show
1776 you the source code for it; however, it is possible to get some
1777 information by applying it to some arguments and observing the result.
1778 </para>
1779
1780 <para>
1781 The process is slightly complicated when the binding is polymorphic.
1782 We show the process by means of an example.
1783 To keep things simple, we will use the well known <literal>map</literal> function:
1784 <programlisting>
1785 import Prelude hiding (map)
1786
1787 map :: (a->b) -> [a] -> [b]
1788 map f [] = []
1789 map f (x:xs) = f x : map f xs
1790 </programlisting>
1791 </para>
1792
1793 <para>
1794 We set a breakpoint on <literal>map</literal>, and call it.
1795 <screen>
1796 *Main> :break 5
1797 Breakpoint 0 activated at map.hs:5:15-28
1798 *Main> map Just [1..5]
1799 Stopped at map.hs:(4,0)-(5,12)
1800 _result :: [b]
1801 x :: a
1802 f :: a -> b
1803 xs :: [a]
1804 </screen>
1805 GHCi tells us that, among other bindings, <literal>f</literal> is in scope.
1806 However, its type is not fully known yet,
1807 and thus it is not possible to apply it to any
1808 arguments. Nevertheless, observe that the type of its first argument is the
1809 same as the type of <literal>x</literal>, and its result type is shared
1810 with <literal>_result</literal>.
1811 </para>
1812
1813 <para>
1814 As we demonstrated earlier (<xref linkend="breakpoints" />), the
1815 debugger has some intelligence built-in to update the type of
1816 <literal>f</literal> whenever the types of <literal>x</literal> or
1817 <literal>_result</literal> are discovered. So what we do in this
1818 scenario is
1819 force <literal>x</literal> a bit, in order to recover both its type
1820 and the argument part of <literal>f</literal>.
1821 <screen>
1822 *Main> seq x ()
1823 *Main> :print x
1824 x = 1
1825 </screen>
1826 </para>
1827 <para>
1828 We can check now that as expected, the type of <literal>x</literal>
1829 has been reconstructed, and with it the
1830 type of <literal>f</literal> has been too:</para>
1831 <screen>
1832 *Main> :t x
1833 x :: Integer
1834 *Main> :t f
1835 f :: Integer -> b
1836 </screen>
1837 <para>
1838 From here, we can apply f to any argument of type Integer and observe
1839 the results.
1840 <screen><![CDATA[
1841 *Main> let b = f 10
1842 *Main> :t b
1843 b :: b
1844 *Main> b
1845 <interactive>:1:0:
1846 Ambiguous type variable `b' in the constraint:
1847 `Show b' arising from a use of `print' at <interactive>:1:0
1848 *Main> :p b
1849 b = (_t2::a)
1850 *Main> seq b ()
1851 ()
1852 *Main> :t b
1853 b :: a
1854 *Main> :p b
1855 b = Just 10
1856 *Main> :t b
1857 b :: Maybe Integer
1858 *Main> :t f
1859 f :: Integer -> Maybe Integer
1860 *Main> f 20
1861 Just 20
1862 *Main> map f [1..5]
1863 [Just 1, Just 2, Just 3, Just 4, Just 5]
1864 ]]></screen>
1865 In the first application of <literal>f</literal>, we had to do
1866 some more type reconstruction
1867 in order to recover the result type of <literal>f</literal>.
1868 But after that, we are free to use
1869 <literal>f</literal> normally.
1870 </para>
1871 </sect2>
1872
1873 <sect2><title>Limitations</title>
1874 <itemizedlist>
1875 <listitem>
1876 <para>When stopped at a breakpoint, if you try to evaluate a variable
1877 that is already under evaluation, the second evaluation will hang.
1878 The reason is
1879 that GHC knows the variable is under evaluation, so the new
1880 evaluation just waits for the result before continuing, but of
1881 course this isn't going to happen because the first evaluation is
1882 stopped at a breakpoint. Control-C can interrupt the hung
1883 evaluation and return to the prompt.</para>
1884 <para>The most common way this can happen is when you're evaluating a
1885 CAF (e.g. main), stop at a breakpoint, and ask for the value of the
1886 CAF at the prompt again.</para>
1887 </listitem>
1888 <listitem><para>
1889 Implicit parameters (see <xref linkend="implicit-parameters"/>) are only available
1890 at the scope of a breakpoint if there is an explicit type signature.
1891 </para>
1892 </listitem>
1893 </itemizedlist>
1894 </sect2>
1895 </sect1>
1896
1897 <sect1 id="ghci-invocation">
1898 <title>Invoking GHCi</title>
1899 <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
1900 <indexterm><primary><option>&ndash;&ndash;interactive</option></primary></indexterm>
1901
1902 <para>GHCi is invoked with the command <literal>ghci</literal> or
1903 <literal>ghc &ndash;&ndash;interactive</literal>. One or more modules or
1904 filenames can also be specified on the command line; this
1905 instructs GHCi to load the specified modules or filenames (and all
1906 the modules they depend on), just as if you had said
1907 <literal>:load <replaceable>modules</replaceable></literal> at the
1908 GHCi prompt (see <xref linkend="ghci-commands" />). For example, to
1909 start GHCi and load the program whose topmost module is in the
1910 file <literal>Main.hs</literal>, we could say:</para>
1911
1912 <screen>
1913 $ ghci Main.hs
1914 </screen>
1915
1916 <para>Most of the command-line options accepted by GHC (see <xref
1917 linkend="using-ghc"/>) also make sense in interactive mode. The ones
1918 that don't make sense are mostly obvious.</para>
1919
1920 <sect2>
1921 <title>Packages</title>
1922 <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
1923
1924 <para>Most packages (see <xref linkend="using-packages"/>) are
1925 available without needing to specify any extra flags at all:
1926 they will be automatically loaded the first time they are
1927 needed.</para>
1928
1929 <para>For hidden packages, however, you need to request the
1930 package be loaded by using the <literal>-package</literal> flag:</para>
1931
1932 <screen>
1933 $ ghci -package readline
1934 GHCi, version 6.8.1: http://www.haskell.org/ghc/ :? for help
1935 Loading package base ... linking ... done.
1936 Loading package readline-1.0 ... linking ... done.
1937 Prelude>
1938 </screen>
1939
1940 <para>The following command works to load new packages into a
1941 running GHCi:</para>
1942
1943 <screen>
1944 Prelude> :set -package <replaceable>name</replaceable>
1945 </screen>
1946
1947 <para>But note that doing this will cause all currently loaded
1948 modules to be unloaded, and you'll be dumped back into the
1949 <literal>Prelude</literal>.</para>
1950 </sect2>
1951
1952 <sect2>
1953 <title>Extra libraries</title>
1954 <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
1955
1956 <para>Extra libraries may be specified on the command line using
1957 the normal <literal>-l<replaceable>lib</replaceable></literal>
1958 option. (The term <emphasis>library</emphasis> here refers to
1959 libraries of foreign object code; for using libraries of Haskell
1960 source code, see <xref linkend="ghci-modules-filenames"/>.) For
1961 example, to load the &ldquo;m&rdquo; library:</para>
1962
1963 <screen>
1964 $ ghci -lm
1965 </screen>
1966
1967 <para>On systems with <literal>.so</literal>-style shared
1968 libraries, the actual library loaded will the
1969 <filename>lib<replaceable>lib</replaceable>.so</filename>. GHCi
1970 searches the following places for libraries, in this order:</para>
1971
1972 <itemizedlist>
1973 <listitem>
1974 <para>Paths specified using the
1975 <literal>-L<replaceable>path</replaceable></literal>
1976 command-line option,</para>
1977 </listitem>
1978 <listitem>
1979 <para>the standard library search path for your system,
1980 which on some systems may be overridden by setting the
1981 <literal>LD_LIBRARY_PATH</literal> environment
1982 variable.</para>
1983 </listitem>
1984 </itemizedlist>
1985
1986 <para>On systems with <literal>.dll</literal>-style shared
1987 libraries, the actual library loaded will be
1988 <filename><replaceable>lib</replaceable>.dll</filename>. Again,
1989 GHCi will signal an error if it can't find the library.</para>
1990
1991 <para>GHCi can also load plain object files
1992 (<literal>.o</literal> or <literal>.obj</literal> depending on
1993 your platform) from the command-line. Just add the name the
1994 object file to the command line.</para>
1995
1996 <para>Ordering of <option>-l</option> options matters: a library
1997 should be mentioned <emphasis>before</emphasis> the libraries it
1998 depends on (see <xref linkend="options-linker"/>).</para>
1999 </sect2>
2000
2001 </sect1>
2002
2003 <sect1 id="ghci-commands">
2004 <title>GHCi commands</title>
2005
2006 <para>GHCi commands all begin with
2007 &lsquo;<literal>:</literal>&rsquo; and consist of a single command
2008 name followed by zero or more parameters. The command name may be
2009 abbreviated, with ambiguities being resolved in favour of the more
2010 commonly used commands.</para>
2011
2012 <variablelist>
2013 <varlistentry>
2014 <term>
2015 <literal>:abandon</literal>
2016 <indexterm><primary><literal>:abandon</literal></primary></indexterm>
2017 </term>
2018 <listitem>
2019 <para>Abandons the current evaluation (only available when stopped at
2020 a breakpoint).</para>
2021 </listitem>
2022 </varlistentry>
2023
2024 <varlistentry>
2025 <term>
2026 <literal>:add</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
2027 <indexterm><primary><literal>:add</literal></primary></indexterm>
2028 </term>
2029 <listitem>
2030 <para>Add <replaceable>module</replaceable>(s) to the
2031 current <firstterm>target set</firstterm>, and perform a
2032 reload. Normally pre-compiled code for the module will be
2033 loaded if available, or otherwise the module will be
2034 compiled to byte-code. Using the <literal>*</literal>
2035 prefix forces the module to be loaded as byte-code.</para>
2036 </listitem>
2037 </varlistentry>
2038
2039 <varlistentry>
2040 <term>
2041 <literal>:back</literal>
2042 <indexterm><primary><literal>:back</literal></primary></indexterm>
2043 </term>
2044 <listitem>
2045 <para>Travel back one step in the history. See <xref
2046 linkend="tracing" />. See also:
2047 <literal>:trace</literal>, <literal>:history</literal>,
2048 <literal>:forward</literal>.</para>
2049 </listitem>
2050 </varlistentry>
2051
2052 <varlistentry>
2053 <term>
2054 <literal>:break [<replaceable>identifier</replaceable> |
2055 [<replaceable>module</replaceable>] <replaceable>line</replaceable>
2056 [<replaceable>column</replaceable>]]</literal>
2057 </term>
2058 <indexterm><primary><literal>:break</literal></primary></indexterm>
2059 <listitem>
2060 <para>Set a breakpoint on the specified function or line and
2061 column. See <xref linkend="setting-breakpoints" />.</para>
2062 </listitem>
2063 </varlistentry>
2064
2065 <varlistentry>
2066 <term>
2067 <literal>:browse</literal><optional><literal>!</literal></optional> <optional><optional><literal>*</literal></optional><replaceable>module</replaceable></optional> ...
2068 <indexterm><primary><literal>:browse</literal></primary></indexterm>
2069 </term>
2070 <listitem>
2071 <para>Displays the identifiers exported by the module
2072 <replaceable>module</replaceable>, which must be either
2073 loaded into GHCi or be a member of a package. If
2074 <replaceable>module</replaceable> is omitted, the most
2075 recently-loaded module is used.</para>
2076
2077 <para>Like all other GHCi commands, the output is always
2078 displayed in the current GHCi scope (<xref linkend="ghci-scope"/>).</para>
2079
2080 <para>There are two variants of the browse command:
2081 <itemizedlist>
2082 <listitem>
2083 <para>If the <literal>*</literal> symbol is placed before
2084 the module name, then <emphasis>all</emphasis> the
2085 identifiers in scope in <replaceable>module</replaceable>
2086 (rather that just its exports) are shown. </para>
2087
2088 <para>The <literal>*</literal>-form is only available for modules
2089 which are interpreted; for compiled modules (including
2090 modules from packages) only the non-<literal>*</literal>
2091 form of <literal>:browse</literal> is available.</para>
2092 </listitem>
2093 <listitem>
2094 <para>Data constructors and class methods are usually
2095 displayed in the context of their data type or class declaration.
2096 However, if the <literal>!</literal> symbol is appended to the
2097 command, thus <literal>:browse!</literal>,
2098 they are listed individually.
2099 The <literal>!</literal>-form also annotates the listing
2100 with comments giving possible imports for each group of
2101 entries. Here is an example:
2102 <screen>
2103 Prelude> :browse! Data.Maybe
2104 -- not currently imported
2105 Data.Maybe.catMaybes :: [Maybe a] -> [a]
2106 Data.Maybe.fromJust :: Maybe a -> a
2107 Data.Maybe.fromMaybe :: a -> Maybe a -> a
2108 Data.Maybe.isJust :: Maybe a -> Bool
2109 Data.Maybe.isNothing :: Maybe a -> Bool
2110 Data.Maybe.listToMaybe :: [a] -> Maybe a
2111 Data.Maybe.mapMaybe :: (a -> Maybe b) -> [a] -> [b]
2112 Data.Maybe.maybeToList :: Maybe a -> [a]
2113 -- imported via Prelude
2114 Just :: a -> Maybe a
2115 data Maybe a = Nothing | Just a
2116 Nothing :: Maybe a
2117 maybe :: b -> (a -> b) -> Maybe a -> b
2118 </screen>
2119 This output shows that, in the context of the current session (ie in the scope
2120 of <literal>Prelude</literal>), the first group of items from
2121 <literal>Data.Maybe</literal> are not in scope (althought they are available in
2122 fully qualified form in the GHCi session - see <xref
2123 linkend="ghci-scope"/>), whereas the second group of items are in scope
2124 (via <literal>Prelude</literal>) and are therefore available either
2125 unqualified, or with a <literal>Prelude.</literal> qualifier.
2126 </para>
2127 </listitem>
2128 </itemizedlist>
2129 </para>
2130 </listitem>
2131 </varlistentry>
2132
2133 <varlistentry>
2134 <term>
2135 <literal>:cd</literal> <replaceable>dir</replaceable>
2136 <indexterm><primary><literal>:cd</literal></primary></indexterm>
2137 </term>
2138 <listitem>
2139 <para>Changes the current working directory to
2140 <replaceable>dir</replaceable>. A
2141 &lsquo;<literal>&tilde;</literal>&rsquo; symbol at the
2142 beginning of <replaceable>dir</replaceable> will be replaced
2143 by the contents of the environment variable
2144 <literal>HOME</literal>.</para>
2145
2146 <para>NOTE: changing directories causes all currently loaded
2147 modules to be unloaded. This is because the search path is
2148 usually expressed using relative directories, and changing
2149 the search path in the middle of a session is not
2150 supported.</para>
2151 </listitem>
2152 </varlistentry>
2153
2154 <varlistentry>
2155 <term>
2156 <literal>:cmd</literal> <replaceable>expr</replaceable>
2157 <indexterm><primary><literal>:cmd</literal></primary></indexterm>
2158 </term>
2159 <listitem>
2160 <para>Executes <replaceable>expr</replaceable> as a computation of
2161 type <literal>IO String</literal>, and then executes the resulting
2162 string as a list of GHCi commands. Multiple commands are separated
2163 by newlines. The <literal>:cmd</literal> command is useful with
2164 <literal>:def</literal> and <literal>:set stop</literal>.</para>
2165 </listitem>
2166 </varlistentry>
2167
2168 <varlistentry>
2169 <term>
2170 <literal>:continue</literal>
2171 <indexterm><primary><literal>:continue</literal></primary></indexterm>
2172 </term>
2173 <listitem><para>Continue the current evaluation, when stopped at a
2174 breakpoint.</para>
2175 </listitem>
2176 </varlistentry>
2177
2178 <varlistentry>
2179 <term>
2180 <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
2181 <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
2182 <indexterm><primary><literal>:etags</literal></primary>
2183 </indexterm>
2184 <indexterm><primary><literal>:etags</literal></primary>
2185 </indexterm>
2186 </term>
2187 <listitem>
2188 <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
2189 (<literal>:ctags</literal>) or
2190 Emacs-style editors (<literal>:etags</literal>). If
2191 no filename is specified, the default <filename>tags</filename> or
2192 <filename>TAGS</filename> is
2193 used, respectively. Tags for all the functions, constructors and
2194 types in the currently loaded modules are created. All modules must
2195 be interpreted for these commands to work.</para>
2196 </listitem>
2197 </varlistentry>
2198
2199 <varlistentry>
2200 <term>
2201 <literal>:def<optional>!</optional> <optional><replaceable>name</replaceable> <replaceable>expr</replaceable></optional></literal>
2202 <indexterm><primary><literal>:def</literal></primary></indexterm>
2203 </term>
2204 <listitem>
2205 <para><literal>:def</literal> is used to define new
2206 commands, or macros, in GHCi. The command
2207 <literal>:def</literal> <replaceable>name</replaceable>
2208 <replaceable>expr</replaceable> defines a new GHCi command
2209 <literal>:<replaceable>name</replaceable></literal>,
2210 implemented by the Haskell expression
2211 <replaceable>expr</replaceable>, which must have type
2212 <literal>String -> IO String</literal>. When
2213 <literal>:<replaceable>name</replaceable>
2214 <replaceable>args</replaceable></literal> is typed at the
2215 prompt, GHCi will run the expression
2216 <literal>(<replaceable>name</replaceable>
2217 <replaceable>args</replaceable>)</literal>, take the
2218 resulting <literal>String</literal>, and feed it back into
2219 GHCi as a new sequence of commands. Separate commands in
2220 the result must be separated by
2221 &lsquo;<literal>\n</literal>&rsquo;.</para>
2222
2223 <para>That's all a little confusing, so here's a few
2224 examples. To start with, here's a new GHCi command which
2225 doesn't take any arguments or produce any results, it just
2226 outputs the current date &amp; time:</para>
2227
2228 <screen>
2229 Prelude> let date _ = Time.getClockTime >>= print >> return ""
2230 Prelude> :def date date
2231 Prelude> :date
2232 Fri Mar 23 15:16:40 GMT 2001
2233 </screen>
2234
2235 <para>Here's an example of a command that takes an argument.
2236 It's a re-implementation of <literal>:cd</literal>:</para>
2237
2238 <screen>
2239 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
2240 Prelude> :def mycd mycd
2241 Prelude> :mycd ..
2242 </screen>
2243
2244 <para>Or I could define a simple way to invoke
2245 &ldquo;<literal>ghc &ndash;&ndash;make Main</literal>&rdquo; in the
2246 current directory:</para>
2247
2248 <screen>
2249 Prelude> :def make (\_ -> return ":! ghc &ndash;&ndash;make Main")
2250 </screen>
2251
2252 <para>We can define a command that reads GHCi input from a
2253 file. This might be useful for creating a set of bindings
2254 that we want to repeatedly load into the GHCi session:</para>
2255
2256 <screen>
2257 Prelude> :def . readFile
2258 Prelude> :. cmds.ghci
2259 </screen>
2260
2261 <para>Notice that we named the command
2262 <literal>:.</literal>, by analogy with the
2263 &lsquo;<literal>.</literal>&rsquo; Unix shell command that
2264 does the same thing.</para>
2265
2266 <para>Typing <literal>:def</literal> on its own lists the
2267 currently-defined macros. Attempting to redefine an
2268 existing command name results in an error unless the
2269 <literal>:def!</literal> form is used, in which case the old
2270 command with that name is silently overwritten.</para>
2271 </listitem>
2272 </varlistentry>
2273
2274 <varlistentry>
2275 <term>
2276 <literal>:delete * | <replaceable>num</replaceable> ...</literal>
2277 <indexterm><primary><literal>:delete</literal></primary></indexterm>
2278 </term>
2279 <listitem>
2280 <para>Delete one or more breakpoints by number (use <literal>:show
2281 breaks</literal> to see the number of each breakpoint). The
2282 <literal>*</literal> form deletes all the breakpoints.</para>
2283 </listitem>
2284 </varlistentry>
2285
2286 <varlistentry>
2287 <term>
2288 <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
2289 <indexterm><primary><literal>:edit</literal></primary></indexterm>
2290 </term>
2291 <listitem>
2292 <para>Opens an editor to edit the file
2293 <replaceable>file</replaceable>, or the most recently loaded
2294 module if <replaceable>file</replaceable> is omitted. The
2295 editor to invoke is taken from the <literal>EDITOR</literal>
2296 environment variable, or a default editor on your system if
2297 <literal>EDITOR</literal> is not set. You can change the
2298 editor using <literal>:set editor</literal>.</para>
2299 </listitem>
2300 </varlistentry>
2301
2302 <varlistentry>
2303 <term>
2304 <literal>:etags</literal>
2305 </term>
2306 <listitem>
2307 <para>See <literal>:ctags</literal>.</para>
2308 </listitem>
2309 </varlistentry>
2310
2311 <varlistentry>
2312 <term>
2313 <literal>:force <replaceable>identifier</replaceable> ...</literal>
2314 <indexterm><primary><literal>:force</literal></primary></indexterm>
2315 </term>
2316 <listitem>
2317 <para>Prints the value of <replaceable>identifier</replaceable> in
2318 the same way as <literal>:print</literal>. Unlike
2319 <literal>:print</literal>, <literal>:force</literal> evaluates each
2320 thunk that it encounters while traversing the value. This may
2321 cause exceptions or infinite loops, or further breakpoints (which
2322 are ignored, but displayed).</para>
2323 </listitem>
2324 </varlistentry>
2325
2326 <varlistentry>
2327 <term>
2328 <literal>:forward</literal>
2329 <indexterm><primary><literal>:forward</literal></primary></indexterm>
2330 </term>
2331 <listitem>
2332 <para>Move forward in the history. See <xref
2333 linkend="tracing" />. See also:
2334 <literal>:trace</literal>, <literal>:history</literal>,
2335 <literal>:back</literal>.</para>
2336 </listitem>
2337 </varlistentry>
2338
2339 <varlistentry>
2340 <term>
2341 <literal>:help</literal>
2342 <indexterm><primary><literal>:help</literal></primary></indexterm>
2343 </term>
2344 <term>
2345 <literal>:?</literal>
2346 <indexterm><primary><literal>:?</literal></primary></indexterm>
2347 </term>
2348 <listitem>
2349 <para>Displays a list of the available commands.</para>
2350 </listitem>
2351 </varlistentry>
2352
2353 <varlistentry>
2354 <term>
2355 <literal>:</literal>
2356 <indexterm><primary><literal>:</literal></primary></indexterm>
2357 </term>
2358 <listitem>
2359 <para>Repeat the previous command.</para>
2360 </listitem>
2361 </varlistentry>
2362
2363 <varlistentry>
2364
2365 <term>
2366 <literal>:history [<replaceable>num</replaceable>]</literal>
2367 <indexterm><primary><literal>:history</literal></primary></indexterm>
2368 </term>
2369 <listitem>
2370 <para>Display the history of evaluation steps. With a number,
2371 displays that many steps (default: 20). For use with
2372 <literal>:trace</literal>; see <xref
2373 linkend="tracing" />.</para>
2374 </listitem>
2375 </varlistentry>
2376
2377 <varlistentry>
2378 <term>
2379 <literal>:info</literal> <replaceable>name</replaceable> ...
2380 <indexterm><primary><literal>:info</literal></primary></indexterm>
2381 </term>
2382 <listitem>
2383 <para>Displays information about the given name(s). For
2384 example, if <replaceable>name</replaceable> is a class, then
2385 the class methods and their types will be printed; if
2386 <replaceable>name</replaceable> is a type constructor, then
2387 its definition will be printed; if
2388 <replaceable>name</replaceable> is a function, then its type
2389 will be printed. If <replaceable>name</replaceable> has
2390 been loaded from a source file, then GHCi will also display
2391 the location of its definition in the source.</para>
2392 <para>For types and classes, GHCi also summarises instances that
2393 mention them. To avoid showing irrelevant information, an instance
2394 is shown only if (a) its head mentions <replaceable>name</replaceable>,
2395 and (b) all the other things mentioned in the instance
2396 are in scope (either qualified or otherwise) as a result of
2397 a <literal>:load</literal> or <literal>:module</literal> commands. </para>
2398 </listitem>
2399 </varlistentry>
2400
2401 <varlistentry>
2402 <term>
2403 <literal>:kind</literal><optional><literal>!</literal></optional>
2404 <replaceable>type</replaceable>
2405 <indexterm><primary><literal>:kind</literal></primary></indexterm>
2406 </term>
2407 <listitem>
2408 <para>Infers and prints the kind of
2409 <replaceable>type</replaceable>. The latter can be an arbitrary
2410 type expression, including a partial application of a type constructor,
2411 such as <literal>Either Int</literal>. If you specify the
2412 optional "<literal>!</literal>", GHC will in addition normalise the type
2413 by expanding out type synonyms and evaluating type-function applications,
2414 and display the normalised result.</para>
2415 </listitem>
2416 </varlistentry>
2417
2418 <varlistentry>
2419 <term>
2420 <literal>:load</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
2421 <indexterm><primary><literal>:load</literal></primary></indexterm>
2422 </term>
2423 <listitem>
2424 <para>Recursively loads the specified
2425 <replaceable>module</replaceable>s, and all the modules they
2426 depend on. Here, each <replaceable>module</replaceable>
2427 must be a module name or filename, but may not be the name
2428 of a module in a package.</para>
2429
2430 <para>All previously loaded modules, except package modules,
2431 are forgotten. The new set of modules is known as the
2432 <firstterm>target set</firstterm>. Note that
2433 <literal>:load</literal> can be used without any arguments
2434 to unload all the currently loaded modules and
2435 bindings.</para>
2436
2437 <para>Normally pre-compiled code for a module will be loaded
2438 if available, or otherwise the module will be compiled to
2439 byte-code. Using the <literal>*</literal> prefix forces a
2440 module to be loaded as byte-code.</para>
2441
2442 <para>After a <literal>:load</literal> command, the current
2443 context is set to:</para>
2444
2445 <itemizedlist>
2446 <listitem>
2447 <para><replaceable>module</replaceable>, if it was loaded
2448 successfully, or</para>
2449 </listitem>
2450 <listitem>
2451 <para>the most recently successfully loaded module, if
2452 any other modules were loaded as a result of the current
2453 <literal>:load</literal>, or</para>
2454 </listitem>
2455 <listitem>
2456 <para><literal>Prelude</literal> otherwise.</para>
2457 </listitem>
2458 </itemizedlist>
2459 </listitem>
2460 </varlistentry>
2461
2462 <varlistentry>
2463 <term>
2464 <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
2465 <indexterm><primary><literal>:main</literal></primary></indexterm>
2466 </term>
2467 <listitem>
2468 <para>
2469 When a program is compiled and executed, it can use the
2470 <literal>getArgs</literal> function to access the
2471 command-line arguments.
2472 However, we cannot simply pass the arguments to the
2473 <literal>main</literal> function while we are testing in ghci,
2474 as the <literal>main</literal> function doesn't take its
2475 arguments directly.
2476 </para>
2477
2478 <para>
2479 Instead, we can use the <literal>:main</literal> command.
2480 This runs whatever <literal>main</literal> is in scope, with
2481 any arguments being treated the same as command-line arguments,
2482 e.g.:
2483 </para>
2484
2485 <screen>
2486 Prelude> let main = System.Environment.getArgs >>= print
2487 Prelude> :main foo bar
2488 ["foo","bar"]
2489 </screen>
2490
2491 <para>
2492 We can also quote arguments which contains characters like
2493 spaces, and they are treated like Haskell strings, or we can
2494 just use Haskell list syntax:
2495 </para>
2496
2497 <screen>
2498 Prelude> :main foo "bar baz"
2499 ["foo","bar baz"]
2500 Prelude> :main ["foo", "bar baz"]
2501 ["foo","bar baz"]
2502 </screen>
2503
2504 <para>
2505 Finally, other functions can be called, either with the
2506 <literal>-main-is</literal> flag or the <literal>:run</literal>
2507 command:
2508 </para>
2509
2510 <screen>
2511 Prelude> let foo = putStrLn "foo" >> System.Environment.getArgs >>= print
2512 Prelude> let bar = putStrLn "bar" >> System.Environment.getArgs >>= print
2513 Prelude> :set -main-is foo
2514 Prelude> :main foo "bar baz"
2515 foo
2516 ["foo","bar baz"]
2517 Prelude> :run bar ["foo", "bar baz"]
2518 bar
2519 ["foo","bar baz"]
2520 </screen>
2521
2522 </listitem>
2523 </varlistentry>
2524
2525 <varlistentry>
2526 <term>
2527 <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
2528 <indexterm><primary><literal>:module</literal></primary></indexterm>
2529 </term>
2530 <term>
2531 <literal>import <replaceable>mod</replaceable></literal>
2532 </term>
2533 <listitem>
2534 <para>Sets or modifies the current context for statements
2535 typed at the prompt. The form <literal>import
2536 <replaceable>mod</replaceable></literal> is equivalent to
2537 <literal>:module +<replaceable>mod</replaceable></literal>.
2538 See <xref linkend="ghci-scope"/> for
2539 more details.</para>
2540 </listitem>
2541 </varlistentry>
2542
2543 <varlistentry>
2544 <term>
2545 <literal>:print </literal> <replaceable>names</replaceable> ...
2546 <indexterm><primary><literal>:print</literal></primary></indexterm>
2547 </term>
2548 <listitem>
2549 <para>Prints a value without forcing its evaluation.
2550 <literal>:print</literal> may be used on values whose types are
2551 unknown or partially known, which might be the case for local
2552 variables with polymorphic types at a breakpoint. While inspecting
2553 the runtime value, <literal>:print</literal> attempts to
2554 reconstruct the type of the value, and will elaborate the type in
2555 GHCi's environment if possible. If any unevaluated components
2556 (thunks) are encountered, then <literal>:print</literal> binds
2557 a fresh variable with a name beginning with <literal>_t</literal>
2558 to each thunk. See <xref linkend="breakpoints" /> for more
2559 information. See also the <literal>:sprint</literal> command,
2560 which works like <literal>:print</literal> but does not bind new
2561 variables.</para>
2562 </listitem>
2563 </varlistentry>
2564
2565 <varlistentry>
2566 <term>
2567 <literal>:quit</literal>
2568 <indexterm><primary><literal>:quit</literal></primary></indexterm>
2569 </term>
2570 <listitem>
2571 <para>Quits GHCi. You can also quit by typing control-D
2572 at the prompt.</para>
2573 </listitem>
2574 </varlistentry>
2575
2576 <varlistentry>
2577 <term>
2578 <literal>:reload</literal>
2579 <indexterm><primary><literal>:reload</literal></primary></indexterm>
2580 </term>
2581 <listitem>
2582 <para>Attempts to reload the current target set (see
2583 <literal>:load</literal>) if any of the modules in the set,
2584 or any dependent module, has changed. Note that this may
2585 entail loading new modules, or dropping modules which are no
2586 longer indirectly required by the target.</para>
2587 </listitem>
2588 </varlistentry>
2589
2590 <varlistentry>
2591 <term>
2592 <literal>:run</literal>
2593 <indexterm><primary><literal>:run</literal></primary></indexterm>
2594 </term>
2595 <listitem>
2596 <para>See <literal>:main</literal>.</para>
2597 </listitem>
2598 </varlistentry>
2599
2600 <varlistentry>
2601 <term>
2602 <literal>:script</literal> <optional><replaceable>n</replaceable></optional>
2603 <literal>filename</literal>
2604 <indexterm><primary><literal>:script</literal></primary></indexterm>
2605 </term>
2606 <listitem>
2607 <para>Executes the lines of a file as a series of GHCi commands. This command
2608 is compatible with multiline statements as set by <literal>:set +m</literal>
2609 </para>
2610 </listitem>
2611 </varlistentry>
2612
2613 <varlistentry>
2614 <term>
2615 <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
2616 <indexterm><primary><literal>:set</literal></primary></indexterm>
2617 </term>
2618 <listitem>
2619 <para>Sets various options. See <xref linkend="ghci-set"/> for a list of
2620 available options and <xref linkend="interactive-mode-options"/> for a
2621 list of GHCi-specific flags. The <literal>:set</literal> command by
2622 itself shows which options are currently set. It also lists the current
2623 dynamic flag settings, with GHCi-specific flags listed separately.</para>
2624 </listitem>
2625 </varlistentry>
2626
2627 <varlistentry>
2628 <term>
2629 <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
2630 <indexterm><primary><literal>:set args</literal></primary></indexterm>
2631 </term>
2632 <listitem>
2633 <para>Sets the list of arguments which are returned when the
2634 program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
2635 </indexterm>.</para>
2636 </listitem>
2637 </varlistentry>
2638
2639 <varlistentry>
2640 <term>
2641 <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
2642 </term>
2643 <listitem>
2644 <para>Sets the command used by <literal>:edit</literal> to
2645 <replaceable>cmd</replaceable>.</para>
2646 </listitem>
2647 </varlistentry>
2648
2649 <varlistentry>
2650 <term>
2651 <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
2652 <indexterm><primary><literal>:set prog</literal></primary></indexterm>
2653 </term>
2654 <listitem>
2655 <para>Sets the string to be returned when the program calls
2656 <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
2657 </indexterm>.</para>
2658 </listitem>
2659 </varlistentry>
2660
2661 <varlistentry>
2662 <term>
2663 <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
2664 </term>
2665 <listitem>
2666 <para>Sets the string to be used as the prompt in GHCi.
2667 Inside <replaceable>prompt</replaceable>, the sequence
2668 <literal>%s</literal> is replaced by the names of the
2669 modules currently in scope, and <literal>%%</literal> is
2670 replaced by <literal>%</literal>. If <replaceable>prompt</replaceable>
2671 starts with &quot; then it is parsed as a Haskell String;
2672 otherwise it is treated as a literal string.</para>
2673 </listitem>
2674 </varlistentry>
2675
2676 <varlistentry>
2677 <term>
2678 <literal>:set</literal> <literal>stop</literal>
2679 [<replaceable>num</replaceable>] <replaceable>cmd</replaceable>
2680 </term>
2681 <listitem>
2682 <para>Set a command to be executed when a breakpoint is hit, or a new
2683 item in the history is selected. The most common use of
2684 <literal>:set stop</literal> is to display the source code at the
2685 current location, e.g. <literal>:set stop :list</literal>.</para>
2686
2687 <para>If a number is given before the command, then the commands are
2688 run when the specified breakpoint (only) is hit. This can be quite
2689 useful: for example, <literal>:set stop 1 :continue</literal>
2690 effectively disables breakpoint 1, by running
2691 <literal>:continue</literal> whenever it is hit (although GHCi will
2692 still emit a message to say the breakpoint was hit). What's more,
2693 with cunning use of <literal>:def</literal> and
2694 <literal>:cmd</literal> you can use <literal>:set stop</literal> to
2695 implement conditional breakpoints:</para>
2696 <screen>
2697 *Main> :def cond \expr -> return (":cmd if (" ++ expr ++ ") then return \"\" else return \":continue\"")
2698 *Main> :set stop 0 :cond (x &lt; 3)
2699 </screen>
2700 <para>Ignoring breakpoints for a specified number of iterations is
2701 also possible using similar techniques.</para>
2702 </listitem>
2703 </varlistentry>
2704
2705 <varlistentry>
2706 <term>
2707 <literal>:seti</literal> <optional><replaceable>option</replaceable>...</optional>
2708 <indexterm><primary><literal>:seti</literal></primary></indexterm>
2709 </term>
2710 <listitem>
2711 <para>
2712 Like <literal>:set</literal>, but options set with
2713 <literal>:seti</literal> affect only expressions and
2714 commands typed at the prompt, and not modules loaded with
2715 <literal>:load</literal> (in contrast, options set with
2716 <literal>:set</literal> apply everywhere). See <xref
2717 linkend="ghci-interactive-options" />.
2718 </para>
2719 <para>
2720 Without any arguments, displays the current set of options
2721 that are applied to expressions and commands typed at the
2722 prompt.
2723 </para>
2724 </listitem>
2725 </varlistentry>
2726
2727 <varlistentry>
2728 <term>
2729 <literal>:show bindings</literal>
2730 <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
2731 </term>
2732 <listitem>
2733 <para>Show the bindings made at the prompt and their
2734 types.</para>
2735 </listitem>
2736 </varlistentry>
2737
2738 <varlistentry>
2739 <term>
2740 <literal>:show breaks</literal>
2741 <indexterm><primary><literal>:show breaks</literal></primary></indexterm>
2742 </term>
2743 <listitem>
2744 <para>List the active breakpoints.</para>
2745 </listitem>
2746 </varlistentry>
2747
2748 <varlistentry>
2749 <term>
2750 <literal>:show context</literal>
2751 <indexterm><primary><literal>:show context</literal></primary></indexterm>
2752 </term>
2753 <listitem>
2754 <para>List the active evaluations that are stopped at breakpoints.</para>
2755 </listitem>
2756 </varlistentry>
2757
2758 <varlistentry>
2759 <term>
2760 <literal>:show imports</literal>
2761 <indexterm><primary><literal>:show imports</literal></primary></indexterm>
2762 </term>
2763 <listitem>
2764 <para>Show the imports that are currently in force, as
2765 created by <literal>import</literal> and
2766 <literal>:module</literal> commands.</para>
2767 </listitem>
2768 </varlistentry>
2769
2770 <varlistentry>
2771 <term>
2772 <literal>:show modules</literal>
2773 <indexterm><primary><literal>:show modules</literal></primary></indexterm>
2774 </term>
2775 <listitem>
2776 <para>Show the list of modules currently loaded.</para>
2777 </listitem>
2778 </varlistentry>
2779
2780 <varlistentry>
2781 <term>
2782 <literal>:show packages</literal>
2783 <indexterm><primary><literal>:show packages</literal></primary></indexterm>
2784 </term>
2785 <listitem>
2786 <para>Show the currently active package flags, as well as the list of
2787 packages currently loaded.</para>
2788 </listitem>
2789 </varlistentry>
2790
2791 <varlistentry>
2792 <term>
2793 <literal>:show languages</literal>
2794 <indexterm><primary><literal>:show languages</literal></primary></indexterm>
2795 </term>
2796 <listitem>
2797 <para>Show the currently active language flags.</para>
2798 </listitem>
2799 </varlistentry>
2800
2801
2802 <varlistentry>
2803 <term>
2804 <literal>:show [args|prog|prompt|editor|stop]</literal>
2805 <indexterm><primary><literal>:show</literal></primary></indexterm>
2806 </term>
2807 <listitem>
2808 <para>Displays the specified setting (see
2809 <literal>:set</literal>).</para>
2810 </listitem>
2811 </varlistentry>
2812
2813 <varlistentry>
2814 <term>
2815 <literal>:sprint</literal>
2816 <indexterm><primary><literal>:sprint</literal></primary></indexterm>
2817 </term>
2818 <listitem>
2819 <para>Prints a value without forcing its evaluation.
2820 <literal>:sprint</literal> is similar to <literal>:print</literal>,
2821 with the difference that unevaluated subterms are not bound to new
2822 variables, they are simply denoted by &lsquo;_&rsquo;.</para>
2823 </listitem>
2824 </varlistentry>
2825
2826 <varlistentry>
2827 <term>
2828 <literal>:step [<replaceable>expr</replaceable>]</literal>
2829 <indexterm><primary><literal>:step</literal></primary></indexterm>
2830 </term>
2831 <listitem>
2832 <para>Single-step from the last breakpoint. With an expression
2833 argument, begins evaluation of the expression with a
2834 single-step.</para>
2835 </listitem>
2836 </varlistentry>
2837
2838 <varlistentry>
2839 <term>
2840 <literal>:trace [<replaceable>expr</replaceable>]</literal>
2841 <indexterm><primary><literal>:trace</literal></primary></indexterm>
2842 </term>
2843 <listitem>
2844 <para>Evaluates the given expression (or from the last breakpoint if
2845 no expression is given), and additionally logs the evaluation
2846 steps for later inspection using <literal>:history</literal>. See
2847 <xref linkend="tracing" />.</para>
2848 </listitem>
2849 </varlistentry>
2850
2851 <varlistentry>
2852 <term>
2853 <literal>:type</literal> <replaceable>expression</replaceable>
2854 <indexterm><primary><literal>:type</literal></primary></indexterm>
2855 </term>
2856 <listitem>
2857 <para>Infers and prints the type of
2858 <replaceable>expression</replaceable>, including explicit
2859 forall quantifiers for polymorphic types. The monomorphism
2860 restriction is <emphasis>not</emphasis> applied to the
2861 expression during type inference.</para>
2862 </listitem>
2863 </varlistentry>
2864
2865 <varlistentry>
2866 <term>
2867 <literal>:undef</literal> <replaceable>name</replaceable>
2868 <indexterm><primary><literal>:undef</literal></primary></indexterm>
2869 </term>
2870 <listitem>
2871 <para>Undefines the user-defined command
2872 <replaceable>name</replaceable> (see <literal>:def</literal>
2873 above).</para>
2874 </listitem>
2875 </varlistentry>
2876
2877 <varlistentry>
2878 <term>
2879 <literal>:unset</literal> <replaceable>option</replaceable>...
2880 <indexterm><primary><literal>:unset</literal></primary></indexterm>
2881 </term>
2882 <listitem>
2883 <para>Unsets certain options. See <xref linkend="ghci-set"/>
2884 for a list of available options.</para>
2885 </listitem>
2886 </varlistentry>
2887
2888 <varlistentry>
2889 <term>
2890 <literal>:!</literal> <replaceable>command</replaceable>...
2891 <indexterm><primary><literal>:!</literal></primary></indexterm>
2892 <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
2893 </term>
2894 <listitem>
2895 <para>Executes the shell command
2896 <replaceable>command</replaceable>.</para>
2897 </listitem>
2898 </varlistentry>
2899
2900 </variablelist>
2901 </sect1>
2902
2903 <sect1 id="ghci-set">
2904 <title>The <literal>:set</literal> and <literal>:seti</literal> commands</title>
2905 <indexterm><primary><literal>:set</literal></primary></indexterm>
2906 <indexterm><primary><literal>:seti</literal></primary></indexterm>
2907
2908 <para>The <literal>:set</literal> command sets two types of
2909 options: GHCi options, which begin with
2910 &lsquo;<literal>+</literal>&rsquo;, and &ldquo;command-line&rdquo;
2911 options, which begin with &lsquo;-&rsquo;. </para>
2912
2913 <para>NOTE: at the moment, the <literal>:set</literal> command
2914 doesn't support any kind of quoting in its arguments: quotes will
2915 not be removed and cannot be used to group words together. For
2916 example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
2917 you expect.</para>
2918
2919 <sect2>
2920 <title>GHCi options</title>
2921 <indexterm><primary>options</primary><secondary>GHCi</secondary>
2922 </indexterm>
2923
2924 <para>GHCi options may be set using <literal>:set</literal> and
2925 unset using <literal>:unset</literal>.</para>
2926
2927 <para>The available GHCi options are:</para>
2928
2929 <variablelist>
2930 <varlistentry>
2931 <term>
2932 <literal>+m</literal>
2933 <indexterm><primary><literal>+m</literal></primary></indexterm>
2934 </term>
2935 <listitem>
2936 <para>Enable parsing of multiline commands. A multiline command
2937 is prompted for when the current input line contains open layout
2938 contexts (see <xref linkend="ghci-multiline" />).</para>
2939 </listitem>
2940 </varlistentry>
2941
2942 <varlistentry>
2943 <term>
2944 <literal>+r</literal>
2945 <indexterm><primary><literal>+r</literal></primary></indexterm>
2946 <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
2947 <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
2948 </term>
2949 <listitem>
2950 <para>Normally, any evaluation of top-level expressions
2951 (otherwise known as CAFs or Constant Applicative Forms) in
2952 loaded modules is retained between evaluations. Turning
2953 on <literal>+r</literal> causes all evaluation of
2954 top-level expressions to be discarded after each
2955 evaluation (they are still retained
2956 <emphasis>during</emphasis> a single evaluation).</para>
2957
2958 <para>This option may help if the evaluated top-level
2959 expressions are consuming large amounts of space, or if
2960 you need repeatable performance measurements.</para>
2961 </listitem>
2962 </varlistentry>
2963
2964 <varlistentry>
2965 <term>
2966 <literal>+s</literal>
2967 <indexterm><primary><literal>+s</literal></primary></indexterm>
2968 </term>
2969 <listitem>
2970 <para>Display some stats after evaluating each expression,
2971 including the elapsed time and number of bytes allocated.
2972 NOTE: the allocation figure is only accurate to the size
2973 of the storage manager's allocation area, because it is
2974 calculated at every GC. Hence, you might see values of
2975 zero if no GC has occurred.</para>
2976 </listitem>
2977 </varlistentry>
2978
2979 <varlistentry>
2980 <term>
2981 <literal>+t</literal>
2982 <indexterm><primary><literal>+t</literal></primary></indexterm>
2983 </term>
2984 <listitem>
2985 <para>Display the type of each variable bound after a
2986 statement is entered at the prompt. If the statement is a
2987 single expression, then the only variable binding will be
2988 for the variable
2989 &lsquo;<literal>it</literal>&rsquo;.</para>
2990 </listitem>
2991 </varlistentry>
2992 </variablelist>
2993 </sect2>
2994
2995 <sect2 id="ghci-cmd-line-options">
2996 <title>Setting GHC command-line options in GHCi</title>
2997
2998 <para>Normal GHC command-line options may also be set using
2999 <literal>:set</literal>. For example, to turn on
3000 <option>-fwarn-missing-signatures</option>, you would say:</para>
3001
3002 <screen>
3003 Prelude> :set -fwarn-missing-signatures
3004 </screen>
3005
3006 <para>Any GHC command-line option that is designated as
3007 <firstterm>dynamic</firstterm> (see the table in <xref
3008 linkend="flag-reference"/>), may be set using
3009 <literal>:set</literal>. To unset an option, you can set the
3010 reverse option:</para>
3011 <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
3012
3013 <screen>
3014 Prelude> :set -fno-warn-incomplete-patterns -XNoMultiParamTypeClasses
3015 </screen>
3016
3017 <para><xref linkend="flag-reference"/> lists the reverse for each
3018 option where applicable.</para>
3019
3020 <para>Certain static options (<option>-package</option>,
3021 <option>-I</option>, <option>-i</option>, and
3022 <option>-l</option> in particular) will also work, but some may
3023 not take effect until the next reload.</para>
3024 <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
3025 </sect2>
3026
3027 <sect2 id="ghci-interactive-options">
3028 <title>Setting options for interactive evaluation only</title>
3029
3030 <para>
3031 GHCi actually maintains two sets of options: one set that
3032 applies when loading modules, and another set that applies for
3033 expressions and commands typed at the prompt. The
3034 <literal>:set</literal> command modifies both, but there is
3035 also a <literal>:seti</literal> command (for "set
3036 interactive") that affects only the second set.
3037 </para>
3038
3039 <para>
3040 The two sets of options can be inspected using the
3041 <literal>:set</literal> and <literal>:seti</literal> commands
3042 respectively, with no arguments. For example, in a clean GHCi
3043 session we might see something like this:
3044 </para>
3045
3046 <screen>
3047 Prelude> :seti
3048 base language is: Haskell2010
3049 with the following modifiers:
3050 -XNoDatatypeContexts
3051 -XNondecreasingIndentation
3052 -XExtendedDefaultRules
3053 GHCi-specific dynamic flag settings:
3054 other dynamic, non-language, flag settings:
3055 -fimplicit-import-qualified
3056 warning settings:
3057 </screen>
3058
3059 <para>
3060 Note that the option <option>-XExtendedDefaultRules</option>
3061 is on, because we apply special defaulting rules to
3062 expressions typed at the prompt (see <xref
3063 linkend="extended-default-rules" />).
3064 </para>
3065
3066 <para>
3067 It is often useful to change the language options for
3068 expressions typed at the prompt only, without having that
3069 option apply to loaded modules too. A good example is
3070 <screen>
3071 :seti -XNoMonomorphismRestriction
3072 </screen>
3073 It would be undesirable if
3074 <option>-XNoMonomorphismRestriction</option> were to apply to
3075 loaded modules too: that might cause a compilation error, but
3076 more commonly it will cause extra recompilation, because GHC
3077 will think that it needs to recompile the module because the
3078 flags have changed.
3079 </para>
3080
3081 <para>
3082 It is therefore good practice if you are setting language
3083 options in your <literal>.ghci</literal> file, to use
3084 <literal>:seti</literal> rather than <literal>:set</literal>
3085 unless you really do want them to apply to all modules you
3086 load in GHCi.
3087 </para>
3088 </sect2>
3089 </sect1>
3090
3091 <sect1 id="ghci-dot-files">
3092 <title>The <filename>.ghci</filename> file</title>
3093 <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
3094 </indexterm>
3095 <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
3096 </indexterm>
3097
3098 <para>When it starts, unless the <literal>-ignore-dot-ghci</literal>
3099 flag is given, GHCi reads and executes commands from the following
3100 files, in this order, if they exist:</para>
3101
3102 <orderedlist>
3103 <listitem>
3104 <para><filename>./.ghci</filename></para>
3105 </listitem>
3106 <listitem>
3107 <para><literal><replaceable>appdata</replaceable>/ghc/ghci.conf</literal>,
3108 where <replaceable>appdata</replaceable> depends on your system,
3109 but is usually something like <literal>C:/Documents and Settings/<replaceable>user</replaceable>/Application Data</literal></para>
3110 </listitem>
3111 <listitem>
3112 <para>On Unix: <literal>$HOME/.ghc/ghci.conf</literal></para>
3113 </listitem>
3114 <listitem>
3115 <para><literal>$HOME/.ghci</literal></para>
3116 </listitem>
3117 </orderedlist>
3118
3119 <para>The <filename>ghci.conf</filename> file is most useful for
3120 turning on favourite options (eg. <literal>:set +s</literal>), and
3121 defining useful macros. Note: when setting language options in
3122 this file it is usually desirable to use <literal>:seti</literal>
3123 rather than <literal>:set</literal> (see <xref
3124 linkend="ghci-interactive-options" />).
3125 </para>
3126
3127 <para>
3128 Placing a <filename>.ghci</filename> file
3129 in a directory with a Haskell project is a useful way to set
3130 certain project-wide options so you don't have to type them
3131 every time you start GHCi: eg. if your project uses multi-parameter
3132 type classes, scoped type variables,
3133 and CPP, and has source files in three subdirectories A, B and C,
3134 you might put the following lines in
3135 <filename>.ghci</filename>:</para>
3136
3137 <screen>
3138 :set -XMultiParamTypeClasses -XScopedTypeVariables -cpp
3139 :set -iA:B:C
3140 </screen>
3141
3142 <para>(Note that strictly speaking the <option>-i</option> flag is
3143 a static one, but in fact it works to set it using
3144 <literal>:set</literal> like this. The changes won't take effect
3145 until the next <literal>:load</literal>, though.)</para>
3146
3147 <para>Once you have a library of GHCi macros, you may want
3148 to source them from separate files, or you may want to source
3149 your <filename>.ghci</filename> file into your running GHCi
3150 session while debugging it</para>
3151
3152 <screen>
3153 :def source readFile
3154 </screen>
3155
3156 <para>With this macro defined in your <filename>.ghci</filename>
3157 file, you can use <literal>:source file</literal> to read GHCi
3158 commands from <literal>file</literal>. You can find (and contribute!-)
3159 other suggestions for <filename>.ghci</filename> files on this Haskell
3160 wiki page: <ulink
3161 url="http://haskell.org/haskellwiki/GHC/GHCi">GHC/GHCi</ulink></para>
3162
3163 <para>Additionally, any files specified with
3164 <literal>-ghci-script</literal> flags will be read after the
3165 standard files, allowing the use of custom .ghci files.</para>
3166
3167 <para>Two command-line options control whether the
3168 startup files files are read:</para>
3169
3170 <variablelist>
3171 <varlistentry>
3172 <term>
3173 <option>-ignore-dot-ghci</option>
3174 <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
3175 </term>
3176 <listitem>
3177 <para>Don't read either <filename>./.ghci</filename> or the
3178 other startup files when starting up.</para>
3179 </listitem>
3180 </varlistentry>
3181 <varlistentry>
3182 <term>
3183 <option>-ghci-script</option>
3184 <indexterm><primary><option>-ghci-script</option></primary></indexterm>
3185 </term>
3186 <listitem>
3187 <para>Read a specific file after the usual startup files.
3188 Maybe be specified repeatedly for multiple inputs.</para>
3189 </listitem>
3190 </varlistentry>
3191 </variablelist>
3192
3193 </sect1>
3194
3195 <sect1 id="ghci-obj">
3196 <title>Compiling to object code inside GHCi</title>
3197
3198 <para>By default, GHCi compiles Haskell source code into byte-code
3199 that is interpreted by the runtime system. GHCi can also compile
3200 Haskell code to object code: to turn on this feature, use the
3201 <option>-fobject-code</option> flag either on the command line or
3202 with <literal>:set</literal> (the option
3203 <option>-fbyte-code</option> restores byte-code compilation
3204 again). Compiling to object code takes longer, but typically the
3205 code will execute 10-20 times faster than byte-code.</para>
3206
3207 <para>Compiling to object code inside GHCi is particularly useful
3208 if you are developing a compiled application, because the
3209 <literal>:reload</literal> command typically runs much faster than
3210 restarting GHC with <option>--make</option> from the command-line,
3211 because all the interface files are already cached in
3212 memory.</para>
3213
3214 <para>There are disadvantages to compiling to object-code: you
3215 can't set breakpoints in object-code modules, for example. Only
3216 the exports of an object-code module will be visible in GHCi,
3217 rather than all top-level bindings as in interpreted
3218 modules.</para>
3219 </sect1>
3220
3221 <sect1 id="ghci-faq">
3222 <title>FAQ and Things To Watch Out For</title>
3223
3224 <variablelist>
3225 <varlistentry>
3226 <term>The interpreter can't load modules with foreign export
3227 declarations!</term>
3228 <listitem>
3229 <para>Unfortunately not. We haven't implemented it yet.
3230 Please compile any offending modules by hand before loading
3231 them into GHCi.</para>
3232 </listitem>
3233 </varlistentry>
3234
3235 <varlistentry>
3236 <term>
3237 <literal>-O</literal> doesn't work with GHCi!
3238 <indexterm><primary><option>-O</option></primary></indexterm>
3239 </term>
3240 <listitem>
3241 <para>For technical reasons, the bytecode compiler doesn't
3242 interact well with one of the optimisation passes, so we
3243 have disabled optimisation when using the interpreter. This
3244 isn't a great loss: you'll get a much bigger win by
3245 compiling the bits of your code that need to go fast, rather
3246 than interpreting them with optimisation turned on.</para>
3247 </listitem>
3248 </varlistentry>
3249
3250 <varlistentry>
3251 <term>Unboxed tuples don't work with GHCi</term>
3252 <listitem>
3253 <para>That's right. You can always compile a module that
3254 uses unboxed tuples and load it into GHCi, however.
3255 (Incidentally the previous point, namely that
3256 <literal>-O</literal> is incompatible with GHCi, is because
3257 the bytecode compiler can't deal with unboxed
3258 tuples).</para>
3259 </listitem>
3260 </varlistentry>
3261
3262 <varlistentry>
3263 <term>Concurrent threads don't carry on running when GHCi is
3264 waiting for input.</term>
3265 <listitem>
3266 <para>This should work, as long as your GHCi was built with
3267 the <option>-threaded</option> switch, which is the default.
3268 Consult whoever supplied your GHCi installation.</para>
3269 </listitem>
3270 </varlistentry>
3271
3272 <varlistentry>
3273 <term>After using <literal>getContents</literal>, I can't use
3274 <literal>stdin</literal> again until I do
3275 <literal>:load</literal> or <literal>:reload</literal>.</term>
3276
3277 <listitem>
3278 <para>This is the defined behaviour of
3279 <literal>getContents</literal>: it puts the stdin Handle in
3280 a state known as <firstterm>semi-closed</firstterm>, wherein
3281 any further I/O operations on it are forbidden. Because I/O
3282 state is retained between computations, the semi-closed
3283 state persists until the next <literal>:load</literal> or
3284 <literal>:reload</literal> command.</para>
3285
3286 <para>You can make <literal>stdin</literal> reset itself
3287 after every evaluation by giving GHCi the command
3288 <literal>:set +r</literal>. This works because
3289 <literal>stdin</literal> is just a top-level expression that
3290 can be reverted to its unevaluated state in the same way as
3291 any other top-level expression (CAF).</para>
3292 </listitem>
3293 </varlistentry>
3294
3295 <varlistentry>
3296 <term>I can't use Control-C to interrupt computations in
3297 GHCi on Windows.</term>
3298 <listitem>
3299 <para>See <xref linkend="ghci-windows"/>.</para>
3300 </listitem>
3301 </varlistentry>
3302
3303 <varlistentry>
3304 <term>The default buffering mode is different in GHCi to GHC.</term>
3305 <listitem>
3306 <para>
3307 In GHC, the stdout handle is line-buffered by default.
3308 However, in GHCi we turn off the buffering on stdout,
3309 because this is normally what you want in an interpreter:
3310 output appears as it is generated.
3311 </para>
3312 <para>
3313 If you want line-buffered behaviour, as in GHC, you can
3314 start your program thus:
3315 <programlisting>
3316 main = do { hSetBuffering stdout LineBuffering; ... }
3317 </programlisting>
3318 </para>
3319 </listitem>
3320 </varlistentry>
3321 </variablelist>
3322 </sect1>
3323
3324 </chapter>
3325
3326 <!-- Emacs stuff:
3327 ;;; Local Variables: ***
3328 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
3329 ;;; End: ***
3330 -->