1 <?xml version="1.0" encoding="iso-8859-1"?>
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>
9 <para>The ‘i’ stands for “Interactive”</para>
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>
23 <sect1 id="ghci-introduction">
24 <title>Introduction to GHCi</title>
26 <para>Let's start with an example GHCi session. You can fire up
27 GHCi with the command <literal>ghci</literal>:</para>
33 / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98.
34 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
35 \____/\/ /_/\____/|_| Type :? for help.
37 Loading package base ... linking ... done.
41 <para>There may be a short pause while GHCi loads the prelude and
42 standard libraries, after which the prompt is shown. If we follow
43 the instructions and type <literal>:?</literal> for help, we
47 Commands available from the prompt:
49 <stmt> evaluate/run <stmt>
50 :add <filename> ... add module(s) to the current target set
51 :browse [*]<module> display the names defined by <module>
52 :cd <dir> change directory to <dir>
53 :def <cmd> <expr> define a command :<cmd>
54 :edit <file> edit file
55 :edit edit last module
56 :help, :? display this list of commands
57 :info [<name> ...] display information about the given names
58 :load <filename> ... load module(s) and their dependents
59 :module [+/-] [*]<mod> ... set the context for expression evaluation
60 :main [<arguments> ...] run the main function with the given arguments
61 :reload reload the current module set
63 :set <option> ... set options
64 :set args <arg> ... set the arguments returned by System.getArgs
65 :set prog <progname> set the value returned by System.getProgName
66 :set prompt <prompt> set the prompt used in GHCi
67 :set editor <cmd> set the command used for :edit
69 :show modules show the currently loaded modules
70 :show bindings show the current bindings made at the prompt
72 :ctags [<file>] create tags file for Vi (default: "tags")
73 :etags [<file>] create tags file for Emacs (default: "TAGS")
74 :type <expr> show the type of <expr>
75 :kind <type> show the kind of <type>
76 :undef <cmd> undefine user-defined command :<cmd>
77 :unset <option> ... unset options
79 :!<command> run the shell command <command>
81 Options for ':set' and ':unset':
83 +r revert top-level expressions after each evaluation
84 +s print timing/memory stats after each evaluation
85 +t print type after evaluation
86 -<flags> most GHC command line flags can also be set here
87 (eg. -v2, -fglasgow-exts, etc.)
90 <para>We'll explain most of these commands as we go along. For
91 Hugs users: many things work the same as in Hugs, so you should be
92 able to get going straight away.</para>
94 <para>Haskell expressions can be typed at the prompt:</para>
95 <indexterm><primary>prompt</primary><secondary>GHCi</secondary>
101 Prelude> let x = 42 in x / 9
106 <para>GHCi interprets the whole line as an expression to evaluate.
107 The expression may not span several lines - as soon as you press
108 enter, GHCi will attempt to evaluate it.</para>
111 <sect1 id="loading-source-files">
112 <title>Loading source files</title>
114 <para>Suppose we have the following Haskell source code, which we
115 place in a file <filename>Main.hs</filename>:</para>
118 main = print (fac 20)
121 fac n = n * fac (n-1)
124 <para>You can save <filename>Main.hs</filename> anywhere you like,
125 but if you save it somewhere other than the current
126 directory<footnote><para>If you started up GHCi from the command
127 line then GHCi's current directory is the same as the current
128 directory of the shell from which it was started. If you started
129 GHCi from the “Start” menu in Windows, then the
130 current directory is probably something like
131 <filename>C:\Documents and Settings\<replaceable>user
132 name</replaceable></filename>.</para> </footnote> then we will
133 need to change to the right directory in GHCi:</para>
136 Prelude> :cd <replaceable>dir</replaceable>
139 <para>where <replaceable>dir</replaceable> is the directory (or
140 folder) in which you saved <filename>Main.hs</filename>.</para>
142 <para>To load a Haskell source file into GHCi, use the
143 <literal>:load</literal> command:</para>
144 <indexterm><primary><literal>:load</literal></primary></indexterm>
148 Compiling Main ( Main.hs, interpreted )
149 Ok, modules loaded: Main.
153 <para>GHCi has loaded the <literal>Main</literal> module, and the
154 prompt has changed to “<literal>*Main></literal>” to
155 indicate that the current context for expressions typed at the
156 prompt is the <literal>Main</literal> module we just loaded (we'll
157 explain what the <literal>*</literal> means later in <xref
158 linkend="ghci-scope"/>). So we can now type expressions involving
159 the functions from <filename>Main.hs</filename>:</para>
166 <para>Loading a multi-module program is just as straightforward;
167 just give the name of the “topmost” module to the
168 <literal>:load</literal> command (hint: <literal>:load</literal>
169 can be abbreviated to <literal>:l</literal>). The topmost module
170 will normally be <literal>Main</literal>, but it doesn't have to
171 be. GHCi will discover which modules are required, directly or
172 indirectly, by the topmost module, and load them all in dependency
175 <sect2 id="ghci-modules-filenames">
176 <title>Modules vs. filenames</title>
177 <indexterm><primary>modules</primary><secondary>and filenames</secondary></indexterm>
178 <indexterm><primary>filenames</primary><secondary>of modules</secondary></indexterm>
180 <para>Question: How does GHC find the filename which contains
181 module <replaceable>M</replaceable>? Answer: it looks for the
182 file <literal><replaceable>M</replaceable>.hs</literal>, or
183 <literal><replaceable>M</replaceable>.lhs</literal>. This means
184 that for most modules, the module name must match the filename.
185 If it doesn't, GHCi won't be able to find it.</para>
187 <para>There is one exception to this general rule: when you load
188 a program with <literal>:load</literal>, or specify it when you
189 invoke <literal>ghci</literal>, you can give a filename rather
190 than a module name. This filename is loaded if it exists, and
191 it may contain any module you like. This is particularly
192 convenient if you have several <literal>Main</literal> modules
193 in the same directory and you can't call them all
194 <filename>Main.hs</filename>.</para>
196 <para>The search path for finding source files is specified with
197 the <option>-i</option> option on the GHCi command line, like
199 <screen>ghci -i<replaceable>dir<subscript>1</subscript></replaceable>:...:<replaceable>dir<subscript>n</subscript></replaceable></screen>
201 <para>or it can be set using the <literal>:set</literal> command
202 from within GHCi (see <xref
203 linkend="ghci-cmd-line-options"/>)<footnote><para>Note that in
204 GHCi, and <option>––make</option> mode, the <option>-i</option>
205 option is used to specify the search path for
206 <emphasis>source</emphasis> files, whereas in standard
207 batch-compilation mode the <option>-i</option> option is used to
208 specify the search path for interface files, see <xref
209 linkend="search-path"/>.</para> </footnote></para>
211 <para>One consequence of the way that GHCi follows dependencies
212 to find modules to load is that every module must have a source
213 file. The only exception to the rule is modules that come from
214 a package, including the <literal>Prelude</literal> and standard
215 libraries such as <literal>IO</literal> and
216 <literal>Complex</literal>. If you attempt to load a module for
217 which GHCi can't find a source file, even if there are object
218 and interface files for the module, you'll get an error
223 <title>Making changes and recompilation</title>
224 <indexterm><primary><literal>:reload</literal></primary></indexterm>
226 <para>If you make some changes to the source code and want GHCi
227 to recompile the program, give the <literal>:reload</literal>
228 command. The program will be recompiled as necessary, with GHCi
229 doing its best to avoid actually recompiling modules if their
230 external dependencies haven't changed. This is the same
231 mechanism we use to avoid re-compiling modules in the batch
232 compilation setting (see <xref linkend="recomp"/>).</para>
236 <sect1 id="ghci-compiled">
237 <title>Loading compiled code</title>
238 <indexterm><primary>compiled code</primary><secondary>in GHCi</secondary></indexterm>
240 <para>When you load a Haskell source module into GHCi, it is
241 normally converted to byte-code and run using the interpreter.
242 However, interpreted code can also run alongside compiled code in
243 GHCi; indeed, normally when GHCi starts, it loads up a compiled
244 copy of the <literal>base</literal> package, which contains the
245 <literal>Prelude</literal>.</para>
247 <para>Why should we want to run compiled code? Well, compiled
248 code is roughly 10x faster than interpreted code, but takes about
249 2x longer to produce (perhaps longer if optimisation is on). So
250 it pays to compile the parts of a program that aren't changing
251 very often, and use the interpreter for the code being actively
254 <para>When loading up source files with <literal>:load</literal>,
255 GHCi looks for any corresponding compiled object files, and will
256 use one in preference to interpreting the source if possible. For
257 example, suppose we have a 4-module program consisting of modules
258 A, B, C, and D. Modules B and C both import D only,
259 and A imports both B & C:</para>
267 <para>We can compile D, then load the whole program, like this:</para>
269 Prelude> :! ghc -c D.hs
271 Skipping D ( D.hs, D.o )
272 Compiling C ( C.hs, interpreted )
273 Compiling B ( B.hs, interpreted )
274 Compiling A ( A.hs, interpreted )
275 Ok, modules loaded: A, B, C, D.
279 <para>In the messages from the compiler, we see that it skipped D,
280 and used the object file <filename>D.o</filename>. The message
281 <literal>Skipping</literal> <replaceable>module</replaceable>
282 indicates that compilation for <replaceable>module</replaceable>
283 isn't necessary, because the source and everything it depends on
284 is unchanged since the last compilation.</para>
286 <para>At any time you can use the command
287 <literal>:show modules</literal>
288 to get a list of the modules currently loaded
294 C ( C.hs, interpreted )
295 B ( B.hs, interpreted )
296 A ( A.hs, interpreted )
299 <para>If we now modify the source of D (or pretend to: using Unix
300 command <literal>touch</literal> on the source file is handy for
301 this), the compiler will no longer be able to use the object file,
302 because it might be out of date:</para>
307 Compiling D ( D.hs, interpreted )
308 Skipping C ( C.hs, interpreted )
309 Skipping B ( B.hs, interpreted )
310 Skipping A ( A.hs, interpreted )
311 Ok, modules loaded: A, B, C, D.
315 <para>Note that module D was compiled, but in this instance
316 because its source hadn't really changed, its interface remained
317 the same, and the recompilation checker determined that A, B and C
318 didn't need to be recompiled.</para>
320 <para>So let's try compiling one of the other modules:</para>
323 *Main> :! ghc -c C.hs
325 Compiling D ( D.hs, interpreted )
326 Compiling C ( C.hs, interpreted )
327 Compiling B ( B.hs, interpreted )
328 Compiling A ( A.hs, interpreted )
329 Ok, modules loaded: A, B, C, D.
332 <para>We didn't get the compiled version of C! What happened?
333 Well, in GHCi a compiled module may only depend on other compiled
334 modules, and in this case C depends on D, which doesn't have an
335 object file, so GHCi also rejected C's object file. Ok, so let's
336 also compile D:</para>
339 *Main> :! ghc -c D.hs
341 Ok, modules loaded: A, B, C, D.
344 <para>Nothing happened! Here's another lesson: newly compiled
345 modules aren't picked up by <literal>:reload</literal>, only
346 <literal>:load</literal>:</para>
350 Skipping D ( D.hs, D.o )
351 Skipping C ( C.hs, C.o )
352 Compiling B ( B.hs, interpreted )
353 Compiling A ( A.hs, interpreted )
354 Ok, modules loaded: A, B, C, D.
357 <para>HINT: since GHCi will only use a compiled object file if it
358 can be sure that the compiled version is up-to-date, a good technique
359 when working on a large program is to occasionally run
360 <literal>ghc ––make</literal> to compile the whole project (say
361 before you go for lunch :-), then continue working in the
362 interpreter. As you modify code, the new modules will be
363 interpreted, but the rest of the project will remain
368 <sect1 id="interactive-evaluation">
369 <title>Interactive evaluation at the prompt</title>
371 <para>When you type an expression at the prompt, GHCi immediately
372 evaluates and prints the result:
374 Prelude> reverse "hello"
381 <sect2><title>I/O actions at the prompt</title>
383 <para>GHCi does more than simple expression evaluation at the prompt.
384 If you type something of type <literal>IO a</literal> for some
385 <literal>a</literal>, then GHCi <emphasis>executes</emphasis> it
386 as an IO-computation.
390 Prelude> putStrLn "hello"
393 Furthermore, GHCi will print the result of the I/O action if (and only
396 <listitem><para>The result type is an instance of <literal>Show</literal>.</para></listitem>
397 <listitem><para>The result type is not
398 <literal>()</literal>.</para></listitem>
400 For example, remembering that <literal>putStrLn :: String -> IO ()</literal>:
402 Prelude> putStrLn "hello"
404 Prelude> do { putStrLn "hello"; return "yes" }
410 <sect2 id="ghci-stmts">
411 <title>Using <literal>do-</literal>notation at the prompt</title>
412 <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
413 <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
415 <para>GHCi actually accepts <firstterm>statements</firstterm>
416 rather than just expressions at the prompt. This means you can
417 bind values and functions to names, and use them in future
418 expressions or statements.</para>
420 <para>The syntax of a statement accepted at the GHCi prompt is
421 exactly the same as the syntax of a statement in a Haskell
422 <literal>do</literal> expression. However, there's no monad
423 overloading here: statements typed at the prompt must be in the
424 <literal>IO</literal> monad.
426 Prelude> x <- return 42
432 The statement <literal>x <- return 42</literal> means
433 “execute <literal>return 42</literal> in the
434 <literal>IO</literal> monad, and bind the result to
435 <literal>x</literal>”. We can then use
436 <literal>x</literal> in future statements, for example to print
437 it as we did above.</para>
439 <para>GHCi will print the result of a statement if and only if:
442 <para>The statement is not a binding, or it is a monadic binding
443 (<literal>p <- e</literal>) that binds exactly one
447 <para>The variable's type is not polymorphic, is not
448 <literal>()</literal>, and is an instance of
449 <literal>Show</literal></para>
452 The automatic printing of binding results can be supressed with
453 <option>:set -fno-print-bind-result</option> (this does not
454 supress printing the result of non-binding statements).
455 <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
456 You might want to do this to prevent the result of binding
457 statements from being fully evaluated by the act of printing
458 them, for example.</para>
460 <para>Of course, you can also bind normal non-IO expressions
461 using the <literal>let</literal>-statement:</para>
468 <para>Another important difference between the two types of binding
469 is that the monadic bind (<literal>p <- e</literal>) is
470 <emphasis>strict</emphasis> (it evaluates <literal>e</literal>),
471 whereas with the <literal>let</literal> form, the expression
472 isn't evaluated immediately:</para>
474 Prelude> let x = error "help!"
480 <para>Note that <literal>let</literal> bindings do not automatically
481 print the value bound, unlike monadic bindings.</para>
483 <para>Any exceptions raised during the evaluation or execution
484 of the statement are caught and printed by the GHCi command line
485 interface (for more information on exceptions, see the module
486 <literal>Control.Exception</literal> in the libraries
487 documentation).</para>
489 <para>Every new binding shadows any existing bindings of the
490 same name, including entities that are in scope in the current
491 module context.</para>
493 <para>WARNING: temporary bindings introduced at the prompt only
494 last until the next <literal>:load</literal> or
495 <literal>:reload</literal> command, at which time they will be
496 simply lost. However, they do survive a change of context with
497 <literal>:module</literal>: the temporary bindings just move to
498 the new location.</para>
500 <para>HINT: To get a list of the bindings currently in scope, use the
501 <literal>:show bindings</literal> command:</para>
504 Prelude> :show bindings
508 <para>HINT: if you turn on the <literal>+t</literal> option,
509 GHCi will show the type of each variable bound by a statement.
511 <indexterm><primary><literal>+t</literal></primary></indexterm>
514 Prelude> let (x:xs) = [1..]
521 <sect2 id="ghci-scope">
522 <title>What's really in scope at the prompt?</title>
524 <para>When you type an expression at the prompt, what
525 identifiers and types are in scope? GHCi provides a flexible
526 way to control exactly how the context for an expression is
527 constructed. Let's start with the simple cases; when you start
528 GHCi the prompt looks like this:</para>
530 <screen>Prelude></screen>
532 <para>Which indicates that everything from the module
533 <literal>Prelude</literal> is currently in scope. If we now
534 load a file into GHCi, the prompt will change:</para>
537 Prelude> :load Main.hs
538 Compiling Main ( Main.hs, interpreted )
542 <para>The new prompt is <literal>*Main</literal>, which
543 indicates that we are typing expressions in the context of the
544 top-level of the <literal>Main</literal> module. Everything
545 that is in scope at the top-level in the module
546 <literal>Main</literal> we just loaded is also in scope at the
547 prompt (probably including <literal>Prelude</literal>, as long
548 as <literal>Main</literal> doesn't explicitly hide it).</para>
551 <literal>*<replaceable>module</replaceable></literal> indicates
552 that it is the full top-level scope of
553 <replaceable>module</replaceable> that is contributing to the
554 scope for expressions typed at the prompt. Without the
555 <literal>*</literal>, just the exports of the module are
558 <para>We're not limited to a single module: GHCi can combine
559 scopes from multiple modules, in any mixture of
560 <literal>*</literal> and non-<literal>*</literal> forms. GHCi
561 combines the scopes from all of these modules to form the scope
562 that is in effect at the prompt. For technical reasons, GHCi
563 can only support the <literal>*</literal>-form for modules which
564 are interpreted, so compiled modules and package modules can
565 only contribute their exports to the current scope.</para>
567 <para>The scope is manipulated using the
568 <literal>:module</literal> command. For example, if the current
569 scope is <literal>Prelude</literal>, then we can bring into
570 scope the exports from the module <literal>IO</literal> like
575 Prelude IO> hPutStrLn stdout "hello\n"
580 <para>(Note: you can use <literal>import M</literal> as an
581 alternative to <literal>:module +M</literal>, and
582 <literal>:module</literal> can also be shortened to
583 <literal>:m</literal>). The full syntax of the
584 <literal>:module</literal> command is:</para>
587 :module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable>
590 <para>Using the <literal>+</literal> form of the
591 <literal>module</literal> commands adds modules to the current
592 scope, and <literal>-</literal> removes them. Without either
593 <literal>+</literal> or <literal>-</literal>, the current scope
594 is replaced by the set of modules specified. Note that if you
595 use this form and leave out <literal>Prelude</literal>, GHCi
596 will assume that you really wanted the
597 <literal>Prelude</literal> and add it in for you (if you don't
598 want the <literal>Prelude</literal>, then ask to remove it with
599 <literal>:m -Prelude</literal>).</para>
601 <para>The scope is automatically set after a
602 <literal>:load</literal> command, to the most recently loaded
603 "target" module, in a <literal>*</literal>-form if possible.
604 For example, if you say <literal>:load foo.hs bar.hs</literal>
605 and <filename>bar.hs</filename> contains module
606 <literal>Bar</literal>, then the scope will be set to
607 <literal>*Bar</literal> if <literal>Bar</literal> is
608 interpreted, or if <literal>Bar</literal> is compiled it will be
609 set to <literal>Prelude Bar</literal> (GHCi automatically adds
610 <literal>Prelude</literal> if it isn't present and there aren't
611 any <literal>*</literal>-form modules).</para>
613 <para>With multiple modules in scope, especially multiple
614 <literal>*</literal>-form modules, it is likely that name
615 clashes will occur. Haskell specifies that name clashes are
616 only reported when an ambiguous identifier is used, and GHCi
617 behaves in the same way for expressions typed at the
621 Hint: GHCi will tab-complete names that are in scope; for
622 example, if you run GHCi and type <literal>J<tab></literal>
623 then GHCi will expand it to <literal>Just </literal>.
627 <title>Qualified names</title>
629 <para>To make life slightly easier, the GHCi prompt also
630 behaves as if there is an implicit <literal>import
631 qualified</literal> declaration for every module in every
632 package, and every module currently loaded into GHCi.</para>
636 <title>The <literal>:main</literal> command</title>
639 When a program is compiled and executed, it can use the
640 <literal>getArgs</literal> function to access the
641 command-line arguments.
642 However, we cannot simply pass the arguments to the
643 <literal>main</literal> function while we are testing in ghci,
644 as the <literal>main</literal> function doesn't take its
649 Instead, we can use the <literal>:main</literal> command.
650 This runs whatever <literal>main</literal> is in scope, with
651 any arguments being treated the same as command-line arguments,
656 Prelude> let main = System.Environment.getArgs >>= print
657 Prelude> :main foo bar
666 <title>The <literal>it</literal> variable</title>
667 <indexterm><primary><literal>it</literal></primary>
670 <para>Whenever an expression (or a non-binding statement, to be
671 precise) is typed at the prompt, GHCi implicitly binds its value
672 to the variable <literal>it</literal>. For example:</para>
679 <para>What actually happens is that GHCi typechecks the
680 expression, and if it doesn't have an <literal>IO</literal> type,
681 then it transforms it as follows: an expression
682 <replaceable>e</replaceable> turns into
684 let it = <replaceable>e</replaceable>;
687 which is then run as an IO-action.</para>
689 <para>Hence, the original expression must have a type which is an
690 instance of the <literal>Show</literal> class, or GHCi will
696 <interactive>:1:0:
697 No instance for (Show (a -> a))
698 arising from use of `print' at <interactive>:1:0-1
699 Possible fix: add an instance declaration for (Show (a -> a))
700 In the expression: print it
701 In a 'do' expression: print it
704 <para>The error message contains some clues as to the
705 transformation happening internally.</para>
707 <para>If the expression was instead of type <literal>IO a</literal> for
708 some <literal>a</literal>, then <literal>it</literal> will be
709 bound to the result of the <literal>IO</literal> computation,
710 which is of type <literal>a</literal>. eg.:</para>
712 Prelude> Time.getClockTime
713 Wed Mar 14 12:23:13 GMT 2001
715 Wed Mar 14 12:23:13 GMT 2001
718 <para>The corresponding translation for an IO-typed
719 <replaceable>e</replaceable> is
721 it <- <replaceable>e</replaceable>
725 <para>Note that <literal>it</literal> is shadowed by the new
726 value each time you evaluate a new expression, and the old value
727 of <literal>it</literal> is lost.</para>
731 <sect2 id="extended-default-rules">
732 <title>Type defaulting in GHCi</title>
733 <indexterm><primary>Type default</primary></indexterm>
734 <indexterm><primary><literal>Show</literal> class</primary></indexterm>
736 Consider this GHCi session:
740 What should GHCi do? Strictly speaking, the program is ambiguous. <literal>show (reverse [])</literal>
741 (which is what GHCi computes here) has type <literal>Show a => a</literal> and how that displays depends
742 on the type <literal>a</literal>. For example:
744 ghci> (reverse []) :: String
746 ghci> (reverse []) :: [Int]
749 However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting
750 rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. The
751 standard rules take each group of constraints <literal>(C1 a, C2 a, ..., Cn
752 a)</literal> for each type variable <literal>a</literal>, and defaults the
757 The type variable <literal>a</literal> appears in no
763 All the classes <literal>Ci</literal> are standard.
768 At least one of the classes <literal>Ci</literal> is
773 At the GHCi prompt, or with GHC if the
774 <literal>-fextended-default-rules</literal> flag is given,
775 the following additional differences apply:
779 Rule 2 above is relaxed thus:
780 <emphasis>All</emphasis> of the classes
781 <literal>Ci</literal> are single-parameter type classes.
786 Rule 3 above is relaxed this:
787 At least one of the classes <literal>Ci</literal> is
788 numeric, <emphasis>or is <literal>Show</literal>,
789 <literal>Eq</literal>, or
790 <literal>Ord</literal></emphasis>.
795 The unit type <literal>()</literal> is added to the
796 start of the standard list of types which are tried when
797 doing type defaulting.
801 The last point means that, for example, this program:
808 def :: (Num a, Enum a) => a
811 prints <literal>()</literal> rather than <literal>0</literal> as the
812 type is defaulted to <literal>()</literal> rather than
813 <literal>Integer</literal>.
816 The motivation for the change is that it means <literal>IO a</literal>
817 actions default to <literal>IO ()</literal>, which in turn means that
818 ghci won't try to print a result when running them. This is
819 particularly important for <literal>printf</literal>, which has an
820 instance that returns <literal>IO a</literal>.
821 However, it is only able to return
822 <literal>undefined</literal>
823 (the reason for the instance having this type is to not require
824 extensions to the class system), so if the type defaults to
825 <literal>Integer</literal> then ghci gives an error when running a
831 <sect1 id="ghci-debugger">
832 <title>The GHCi Debugger</title>
833 <indexterm><primary>debugger</primary><secondary>in GHCi</secondary>
836 <para>GHCi contains a simple imperative-style debugger in which you can
837 stop a running computation in order to examine the values of
838 variables. The debugger is integrated into GHCi, and is turned on by
839 default: no flags are required to enable the debugging facilities. There
840 is one major restriction: breakpoints and single-stepping are only
841 available in <emphasis>interpreted</emphasis> modules; compiled code is
842 invisible to the debugger.</para>
844 <para>The debugger provides the following:
847 <para>The abilty to set a <firstterm>breakpoint</firstterm> on a
848 function definition or expression in the program. When the function
849 is called, or the expression evaluated, GHCi suspends
850 execution and returns to the prompt, where you can inspect the
851 values of local variables before continuing with the
855 <para>Execution can be <firstterm>single-stepped</firstterm>: the
856 evaluator will suspend execution approximately after every
857 reduction, allowing local variables to be inspected. This is
858 equivalent to setting a breakpoint at every point in the
862 <para>Execution can take place in <firstterm>tracing
863 mode</firstterm>, in which the evaluator remembers each
864 evaluation step as it happens, but doesn't suspend execution until
865 an actual breakpoint is reached. When this happens, the history of
866 evaluation steps can be inspected.</para>
869 <para>Exceptions (e.g. pattern matching failure and
870 <literal>error</literal>) can be treated as breakpoints, to help
871 locate the source of an exception in the program.</para>
876 <para>There is currently no support for obtaining a “stack
877 trace”, but the tracing and history features provide a useful
878 second-best, which will often be enough to establish the context of an
881 <sect2 id="breakpoints">
882 <title>Breakpoints and inspecting variables</title>
884 <para>Let's use quicksort as a running example. Here's the code:</para>
888 qsort (a:as) = qsort left ++ [a] ++ qsort right
889 where (left,right) = (filter (<=a) as, filter (>a) as)
891 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
894 <para>First, load the module into GHCi:</para>
898 [1 of 1] Compiling Main ( qsort.hs, interpreted )
899 Ok, modules loaded: Main.
903 <para>Now, let's set a breakpoint on the right-hand-side of the second
904 equation of qsort:</para>
908 Breakpoint 0 activated at qsort.hs:2:15-46
912 <para>The command <literal>:break 2</literal> sets a breakpoint on line
913 2 of the most recently-loaded module, in this case
914 <literal>qsort.hs</literal>. Specifically, it picks the
915 leftmost complete subexpression on that line on which to set the
916 breakpoint, which in this case is the expression
917 <literal>(qsort left ++ [a] ++ qsort right)</literal>.</para>
919 <para>Now, we run the program:</para>
923 Stopped at qsort.hs:2:15-46
928 [qsort.hs:2:15-46] *Main>
931 <para>Execution has stopped at the breakpoint. The prompt has changed to
932 indicate that we are currently stopped at a breakpoint, and the location:
933 <literal>[qsort.hs:2:15-46]</literal>. To further clarify the
934 location, we can use the <literal>:list</literal> command:</para>
937 [qsort.hs:2:15-46] *Main> :list
939 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
940 3 where (left,right) = (filter (<=a) as, filter (>a) as)
943 <para>The <literal>:list</literal> command lists the source code around
944 the current breakpoint. If your output device supports it, then GHCi
945 will highlight the active subexpression in bold.</para>
947 <para>GHCi has provided bindings for the free variables<footnote><para>We
948 originally provided bindings for all variables in scope, rather
950 the free variables of the expression, but found that this affected
951 performance considerably, hence the current restriction to just the
952 free variables.</para>
953 </footnote> of the expression
955 breakpoint was placed (<literal>a</literal>, <literal>left</literal>,
956 <literal>right</literal>), and additionally a binding for the result of
957 the expression (<literal>_result</literal>). These variables are just
958 like other variables that you might define in GHCi; you
959 can use them in expressions that you type at the prompt, you can ask
960 for their types with <literal>:type</literal>, and so on. There is one
961 important difference though: these variables may only have partial
962 types. For example, if we try to display the value of
963 <literal>left</literal>:</para>
966 [qsort.hs:2:15-46] *Main> left
968 <interactive>:1:0:
969 Ambiguous type variable `a' in the constraint:
970 `Show a' arising from a use of `print' at <interactive>:1:0-3
971 Cannot resolve unknown runtime types: a
972 Use :print or :force to determine these types
975 <para>This is because <literal>qsort</literal> is a polymorphic function,
976 and because GHCi does not carry type information at runtime, it cannot
977 determine the runtime types of free variables that involve type
978 variables. Hence, when you ask to display <literal>left</literal> at
979 the prompt, GHCi can't figure out which instance of
980 <literal>Show</literal> to use, so it emits the type error above.</para>
982 <para>Fortunately, the debugger includes a generic printing command,
983 <literal>:print</literal>, which can inspect the actual runtime value of a
984 variable and attempt to reconstruct its type. If we try it on
985 <literal>left</literal>:</para>
988 [qsort.hs:2:15-46] *Main> :print left
992 <para>This isn't particularly enlightening. What happened is that
993 <literal>left</literal> is bound to an unevaluated computation (a
994 suspension, or <firstterm>thunk</firstterm>), and
995 <literal>:print</literal> does not force any evaluation. The idea is
996 that <literal>:print</literal> can be used to inspect values at a
997 breakpoint without any unfortunate side effects. It won't force any
998 evaluation, which could cause the program to give a different answer
999 than it would normally, and hence it won't cause any exceptions to be
1000 raised, infinite loops, or further breakpoints to be triggered (see
1001 <xref linkend="nested-breakpoints" />).
1002 Rather than forcing thunks, <literal>:print</literal>
1003 binds each thunk to a fresh variable beginning with an
1004 underscore, in this case
1005 <literal>_t1</literal>.</para>
1007 <para>If we aren't concerned about preserving the evaluatedness of a
1008 variable, we can use <literal>:force</literal> instead of
1009 <literal>:print</literal>. The <literal>:force</literal> command
1010 behaves exactly like <literal>:print</literal>, except that it forces
1011 the evaluation of any thunks it encounters:</para>
1014 [qsort.hs:2:15-46] *Main> :force left
1018 <para>Now, since <literal>:force</literal> has inspected the runtime
1019 value of <literal>left</literal>, it has reconstructed its type. We
1020 can see the results of this type reconstruction:</para>
1023 [qsort.hs:2:15-46] *Main> :show bindings
1024 _result :: [Integer]
1031 <para>Not only do we now know the type of <literal>left</literal>, but
1032 all the other partial types have also been resolved. So we can ask
1033 for the value of <literal>a</literal>, for example:</para>
1036 [qsort.hs:2:15-46] *Main> a
1040 <para>You might find it useful to use Haskell's
1041 <literal>seq</literal> function to evaluate individual thunks rather
1042 than evaluating the whole expression with <literal>:force</literal>.
1046 [qsort.hs:2:15-46] *Main> :print right
1047 right = (_t1::[Integer])
1048 [qsort.hs:2:15-46] *Main> seq _t1 ()
1050 [qsort.hs:2:15-46] *Main> :print right
1051 right = 23 : (_t2::[Integer])
1054 <para>We evaluated only the <literal>_t1</literal> thunk, revealing the
1055 head of the list, and the tail is another thunk now bound to
1056 <literal>_t2</literal>. The <literal>seq</literal> function is a
1057 little inconvenient to use here, so you might want to use
1058 <literal>:def</literal> to make a nicer interface (left as an exercise
1059 for the reader!).</para>
1061 <para>Finally, we can continue the current execution:</para>
1064 [qsort.hs:2:15-46] *Main> :continue
1065 Stopped at qsort.hs:2:15-46
1070 [qsort.hs:2:15-46] *Main>
1073 <para>The execution continued at the point it previously stopped, and has
1074 now stopped at the breakpoint for a second time.</para>
1076 <sect3 id="setting-breakpoings">
1077 <title>Setting breakpoints</title>
1079 <para>Breakpoints can be set in various ways. Perhaps the easiest way to
1080 set a breakpoint is to name a top-level function:</para>
1083 :break <replaceable>identifier</replaceable>
1086 <para>Where <replaceable>identifier</replaceable> names any top-level
1087 function in an interpreted module currently loaded into GHCi (qualified
1088 names may be used). The breakpoint will be set on the body of the
1089 function, when it is fully applied but before any pattern matching has
1092 <para>Breakpoints can also be set by line (and optionally column)
1096 :break <replaceable>line</replaceable>
1097 :break <replaceable>line</replaceable> <replaceable>column</replaceable>
1098 :break <replaceable>module</replaceable> <replaceable>line</replaceable>
1099 :break <replaceable>module</replaceable> <replaceable>line</replaceable> <replaceable>column</replaceable>
1102 <para>When a breakpoint is set on a particular line, GHCi sets the
1104 leftmost subexpression that begins and ends on that line. If two
1105 complete subexpressions start at the same
1106 column, the longest one is picked. If there is no complete
1107 subexpression on the line, then the leftmost expression starting on
1108 the line is picked, and failing that the rightmost expression that
1109 partially or completely covers the line.</para>
1111 <para>When a breakpoint is set on a particular line and column, GHCi
1112 picks the smallest subexpression that encloses that location on which
1113 to set the breakpoint. Note: GHC considers the TAB character to have a
1114 width of 1, wherever it occurs; in other words it counts
1115 characters, rather than columns. This matches what some editors do,
1116 and doesn't match others. The best advice is to avoid tab
1117 characters in your source code altogether (see
1118 <option>-fwarn-tabs</option> in <xref linkend="options-sanity"
1121 <para>If the module is omitted, then the most recently-loaded module is
1124 <para>Not all subexpressions are potential breakpoint locations. Single
1125 variables are typically not considered to be breakpoint locations
1126 (unless the variable is the right-hand-side of a function definition,
1127 lambda, or case alternative). The rule of thumb is that all redexes
1128 are breakpoint locations, together with the bodies of functions,
1129 lambdas, case alternatives and binding statements. There is normally
1130 no breakpoint on a let expression, but there will always be a
1131 breakpoint on its body, because we are usually interested in inspecting
1132 the values of the variables bound by the let.</para>
1136 <title>Listing and deleting breakpoints</title>
1138 <para>The list of breakpoints currently enabled can be displayed using
1139 <literal>:show breaks</literal></para>:
1142 [0] Main qsort.hs:1:11-12
1143 [1] Main qsort.hs:2:15-46
1146 <para>To delete a breakpoint, use the <literal>:delete</literal>
1147 command with the number given in the output from <literal>:show breaks</literal>:</para>
1152 [1] Main qsort.hs:2:15-46
1155 <para>To delete all breakpoints at once, use <literal>:delete *</literal>.</para>
1160 <sect2 id="single-stepping">
1161 <title>Single-stepping</title>
1163 <para>Single-stepping is a great way to visualise the execution of your
1164 program, and it is also a useful tool for identifying the source of a
1165 bug. The concept is simple: single-stepping enables all the
1166 breakpoints in the program and executes until the next breakpoint is
1167 reached, at which point you can single-step again, or continue
1168 normally. For example:</para>
1172 Stopped at qsort.hs:5:7-47
1176 <para>The command <literal>:step
1177 <replaceable>expr</replaceable></literal> begins the evaluation of
1178 <replaceable>expr</replaceable> in single-stepping mode. If
1179 <replaceable>expr</replaceable> is ommitted, then it single-steps from
1180 the current breakpoint.</para>
1182 <para>The <literal>:list</literal> command is particularly useful when
1183 single-stepping, to see where you currently are:</para>
1186 [qsort.hs:5:7-47] *Main> :list
1188 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1190 [qsort.hs:5:7-47] *Main>
1193 <para>In fact, GHCi provides a way to run a command when a breakpoint is
1194 hit, so we can make it automatically do
1195 <literal>:list</literal>:</para>
1198 [qsort.hs:5:7-47] *Main> :set stop :list
1199 [qsort.hs:5:7-47] *Main> :step
1200 Stopped at qsort.hs:5:14-46
1201 _result :: [Integer]
1203 5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18])
1205 [qsort.hs:5:14-46] *Main>
1209 <sect2 id="nested-breakpoints">
1210 <title>Nested breakpoints</title>
1211 <para>When GHCi is stopped at a breakpoint, and an expression entered at
1212 the prompt triggers a
1213 second breakpoint, the new breakpoint becomes the “current”
1214 one, and the old one is saved on a stack. An arbitrary number of
1215 breakpoint contexts can be built up in this way. For example:</para>
1218 [qsort.hs:2:15-46] *Main> :st qsort [1,3]
1219 Stopped at qsort.hs:(1,0)-(3,55)
1221 ... [qsort.hs:(1,0)-(3,55)] *Main>
1224 <para>While stopped at the breakpoint on line 2 that we set earlier, we
1225 started a new evaluation with <literal>:step qsort [1,3]</literal>.
1226 This new evaluation stopped after one step (at the definition of
1227 <literal>qsort</literal>). The prompt has changed, now prefixed with
1228 <literal>...</literal>, to indicate that there are saved breakpoints
1229 beyond the current one. To see the stack of contexts, use
1230 <literal>:show context</literal>:</para>
1233 ... [qsort.hs:(1,0)-(3,55)] *Main> :show context
1235 Stopped at qsort.hs:2:15-46
1237 Stopped at qsort.hs:(1,0)-(3,55)
1238 ... [qsort.hs:(1,0)-(3,55)] *Main>
1241 <para>To abandon the current evaluation, use
1242 <literal>:abandon</literal>:</para>
1245 ... [qsort.hs:(1,0)-(3,55)] *Main> :abandon
1246 [qsort.hs:2:15-46] *Main> :abandon
1251 <sect2 id="ghci-debugger-result">
1252 <title>The <literal>_result</literal> variable</title>
1253 <para>When stopped at a breakpoint or single-step, GHCi binds the
1254 variable <literal>_result</literal> to the value of the currently
1255 active expression. The value of <literal>_result</literal> is
1256 presumably not available yet, because we stopped its evaluation, but it
1257 can be forced: if the type is known and showable, then just entering
1258 <literal>_result</literal> at the prompt will show it. However,
1259 there's one caveat to doing this: evaluating <literal>_result</literal>
1260 will be likely to trigger further breakpoints, starting with the
1261 breakpoint we are currently stopped at (if we stopped at a real
1262 breakpoint, rather than due to <literal>:step</literal>). So it will
1263 probably be necessary to issue a <literal>:continue</literal>
1264 immediately when evaluating <literal>_result</literal>. Alternatively,
1265 you can use <literal>:force</literal> which ignores breakpoints.</para>
1268 <sect2 id="tracing">
1269 <title>Tracing and history</title>
1271 <para>A question that we often want to ask when debugging a program is
1272 “how did I get here?”. Traditional imperative debuggers
1273 usually provide some kind of stack-tracing feature that lets you see
1274 the stack of active function calls (sometimes called the “lexical
1275 call stack”), describing a path through the code
1276 to the current location. Unfortunately this is hard to provide in
1277 Haskell, because execution proceeds on a demand-driven basis, rather
1278 than a depth-first basis as in strict languages. The
1279 “stack“ in GHC's execution engine bears little
1280 resemblance to the lexical call stack. Ideally GHCi would maintain a
1281 separate lexical call stack in addition to the dynamic call stack, and
1282 in fact this is exactly
1283 what our profiling system does (<xref linkend="profiling" />), and what
1284 some other Haskell debuggers do. For the time being, however, GHCi
1285 doesn't maintain a lexical call stack (there are some technical
1286 challenges to be overcome). Instead, we provide a way to backtrack from a
1287 breakpoint to previous evaluation steps: essentially this is like
1288 single-stepping backwards, and should in many cases provide enough
1289 information to answer the “how did I get here?”
1292 <para>To use tracing, evaluate an expression with the
1293 <literal>:trace</literal> command. For example, if we set a breakpoint
1294 on the base case of <literal>qsort</literal>:</para>
1297 *Main> :list qsort
1299 2 qsort (a:as) = qsort left ++ [a] ++ qsort right
1300 3 where (left,right) = (filter (<=a) as, filter (>a) as)
1303 Breakpoint 1 activated at qsort.hs:1:11-12
1307 <para>and then run a small <literal>qsort</literal> with
1311 *Main> :trace qsort [3,2,1]
1312 Stopped at qsort.hs:1:11-12
1314 [qsort.hs:1:11-12] *Main>
1317 <para>We can now inspect the history of evaluation steps:</para>
1320 [qsort.hs:1:11-12] *Main> :hist
1321 -1 : qsort.hs:3:24-38
1322 -2 : qsort.hs:3:23-55
1323 -3 : qsort.hs:(1,0)-(3,55)
1324 -4 : qsort.hs:2:15-24
1325 -5 : qsort.hs:2:15-46
1326 -6 : qsort.hs:3:24-38
1327 -7 : qsort.hs:3:23-55
1328 -8 : qsort.hs:(1,0)-(3,55)
1329 -9 : qsort.hs:2:15-24
1330 -10 : qsort.hs:2:15-46
1331 -11 : qsort.hs:3:24-38
1332 -12 : qsort.hs:3:23-55
1333 -13 : qsort.hs:(1,0)-(3,55)
1334 -14 : qsort.hs:2:15-24
1335 -15 : qsort.hs:2:15-46
1336 -16 : qsort.hs:(1,0)-(3,55)
1337 <end of history>
1340 <para>To examine one of the steps in the history, use
1341 <literal>:back</literal>:</para>
1344 [qsort.hs:1:11-12] *Main> :back
1345 Logged breakpoint at qsort.hs:3:24-38
1349 [-1: qsort.hs:3:24-38] *Main>
1352 <para>Note that the local variables at each step in the history have been
1353 preserved, and can be examined as usual. Also note that the prompt has
1354 changed to indicate that we're currently examining the first step in
1355 the history: <literal>-1</literal>. The command
1356 <literal>:forward</literal> can be used to traverse forward in the
1359 <para>The <literal>:trace</literal> command can be used with or without
1360 an expression. When used without an expression, tracing begins from
1361 the current breakpoint, just like <literal>:step</literal>.</para>
1363 <para>The history is only available when
1364 using <literal>:trace</literal>; the reason for this is we found that
1365 logging each breakpoint in the history cuts performance by a factor of
1366 2 or more. GHCi remembers the last 50 steps in the history (perhaps in
1367 the future we'll make this configurable).</para>
1370 <sect2 id="ghci-debugger-exceptions">
1371 <title>Debugging exceptions</title>
1372 <para>Another common question that comes up when debugging is
1373 “where did this exception come from?”. Exceptions such as
1374 those raised by <literal>error</literal> or <literal>head []</literal>
1375 have no context information attached to them. Finding which
1376 particular call to <literal>head</literal> in your program resulted in
1377 the error can be a painstaking process, usually involving
1378 <literal>Debug.Trace.trace</literal>, or compiling with
1379 profiling and using <literal>+RTS -xc</literal> (see <xref
1380 linkend="prof-time-options" />).</para>
1382 <para>The GHCi debugger offers a way to hopefully shed some light on
1383 these errors quickly and without modifying or recompiling the source
1384 code. One way would be to set a breakpoint on the location in the
1385 source code that throws the exception, and then use
1386 <literal>:trace</literal> and <literal>:history</literal> to establish
1387 the context. However, <literal>head</literal> is in a library and
1388 we can't set a breakpoint on it directly. For this reason, GHCi
1389 provides the flag <literal>-fbreak-on-exception</literal> which causes
1390 the evaluator to stop when an exception is thrown, just as it does when
1391 a breakpoint is hit. This is only really useful in conjunction with
1392 <literal>:trace</literal>, in order to log the steps leading up to the
1393 exception. For example:</para>
1396 *Main> :set -fbreak-on-exception
1397 *Main> :trace qsort ("abc" ++ undefined)
1398 "Stopped at <exception thrown>
1400 [<exception thrown>] *Main> :hist
1401 -1 : qsort.hs:3:24-38
1402 -2 : qsort.hs:3:23-55
1403 -3 : qsort.hs:(1,0)-(3,55)
1404 -4 : qsort.hs:2:15-24
1405 -5 : qsort.hs:2:15-46
1406 -6 : qsort.hs:(1,0)-(3,55)
1407 <end of history>
1408 [<exception thrown>] *Main> :back
1409 Logged breakpoint at qsort.hs:3:24-38
1413 [-1: qsort.hs:3:24-38] *Main> :force as
1414 *** Exception: Prelude.undefined
1415 [-1: qsort.hs:3:24-38] *Main> :print as
1416 as = 'b' : 'c' : (_t1::[Char])
1419 <para>The exception itself is bound to a new variable,
1420 <literal>_exception</literal>.</para>
1422 <para>Breaking on exceptions is particularly useful for finding out what
1423 your program was doing when it was in an infinite loop. Just hit
1424 Control-C, and examine the history to find out what was going
1428 <sect2><title>Example: inspecting functions</title>
1430 It is possible to use the debugger to examine function values.
1431 When we are at a breakpoint and a function is in scope, the debugger
1433 you the source code for it; however, it is possible to get some
1434 information by applying it to some arguments and observing the result.
1438 The process is slightly complicated when the binding is polymorphic.
1439 We show the process by means of an example.
1440 To keep things simple, we will use the well known <literal>map</literal> function:
1442 import Prelude hiding (map)
1444 map :: (a->b) -> a -> b
1446 map f (x:xs) = f x : map f xs
1451 We set a breakpoint on <literal>map</literal>, and call it.
1454 Breakpoint 0 activated at map.hs:5:15-28
1455 *Main> map Just [1..5]
1456 Stopped at map.hs:(4,0)-(5,12)
1462 GHCi tells us that, among other bindings, <literal>f</literal> is in scope.
1463 However, its type is not fully known yet,
1464 and thus it is not possible to apply it to any
1465 arguments. Nevertheless, observe that the type of its first argument is the
1466 same as the type of <literal>x</literal>, and its result type is shared
1467 with <literal>_result</literal>.
1471 As we demonstrated earlier (<xref linkend="breakpoints" />), the
1472 debugger has some intelligence built-in to update the type of
1473 <literal>f</literal> whenever the types of <literal>x</literal> or
1474 <literal>_result</literal> are discovered. So what we do in this
1476 force <literal>x</literal> a bit, in order to recover both its type
1477 and the argument part of <literal>f</literal>.
1485 We can check now that as expected, the type of <literal>x</literal>
1486 has been reconstructed, and with it the
1487 type of <literal>f</literal> has been too:</para>
1495 From here, we can apply f to any argument of type Integer and observe
1503 Ambiguous type variable `b' in the constraint:
1504 `Show b' arising from a use of `print' at <interactive>:1:0
1516 f :: Integer -> Maybe Integer
1520 [Just 1, Just 2, Just 3, Just 4, Just 5]
1522 In the first application of <literal>f</literal>, we had to do
1523 some more type reconstruction
1524 in order to recover the result type of <literal>f</literal>.
1525 But after that, we are free to use
1526 <literal>f</literal> normally.
1530 <sect2><title>Limitations</title>
1533 <para>When stopped at a breakpoint, if you try to evaluate a variable
1534 that is already under evaluation, the second evaluation will hang.
1536 that GHC knows the variable is under evaluation, so the new
1537 evaluation just waits for the result before continuing, but of
1538 course this isn't going to happen because the first evaluation is
1539 stopped at a breakpoint. Control-C can interrupt the hung
1540 evaluation and return to the prompt.</para>
1541 <para>The most common way this can happen is when you're evaluating a
1542 CAF (e.g. main), stop at a breakpoint, and ask for the value of the
1543 CAF at the prompt again.</para>
1546 Implicit parameters (see <xref linkend="implicit-parameters"/>) are only available
1547 at the scope of a breakpoint if there is a explicit type signature.
1554 <sect1 id="ghci-invocation">
1555 <title>Invoking GHCi</title>
1556 <indexterm><primary>invoking</primary><secondary>GHCi</secondary></indexterm>
1557 <indexterm><primary><option>––interactive</option></primary></indexterm>
1559 <para>GHCi is invoked with the command <literal>ghci</literal> or
1560 <literal>ghc ––interactive</literal>. One or more modules or
1561 filenames can also be specified on the command line; this
1562 instructs GHCi to load the specified modules or filenames (and all
1563 the modules they depend on), just as if you had said
1564 <literal>:load <replaceable>modules</replaceable></literal> at the
1565 GHCi prompt (see <xref linkend="ghci-commands" />). For example, to
1566 start GHCi and load the program whose topmost module is in the
1567 file <literal>Main.hs</literal>, we could say:</para>
1573 <para>Most of the command-line options accepted by GHC (see <xref
1574 linkend="using-ghc"/>) also make sense in interactive mode. The ones
1575 that don't make sense are mostly obvious; for example, GHCi
1576 doesn't generate interface files, so options related to interface
1577 file generation won't have any effect.</para>
1580 <title>Packages</title>
1581 <indexterm><primary>packages</primary><secondary>with GHCi</secondary></indexterm>
1583 <para>Most packages (see <xref linkend="using-packages"/>) are
1584 available without needing to specify any extra flags at all:
1585 they will be automatically loaded the first time they are
1588 <para>For hidden packages, however, you need to request the
1589 package be loaded by using the <literal>-package</literal> flag:</para>
1592 $ ghci -package readline
1595 / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98.
1596 / /_\\/ __ / /___| | http://www.haskell.org/ghc/
1597 \____/\/ /_/\____/|_| Type :? for help.
1599 Loading package base ... linking ... done.
1600 Loading package readline-1.0 ... linking ... done.
1604 <para>The following command works to load new packages into a
1605 running GHCi:</para>
1608 Prelude> :set -package <replaceable>name</replaceable>
1611 <para>But note that doing this will cause all currently loaded
1612 modules to be unloaded, and you'll be dumped back into the
1613 <literal>Prelude</literal>.</para>
1617 <title>Extra libraries</title>
1618 <indexterm><primary>libraries</primary><secondary>with GHCi</secondary></indexterm>
1620 <para>Extra libraries may be specified on the command line using
1621 the normal <literal>-l<replaceable>lib</replaceable></literal>
1622 option. (The term <emphasis>library</emphasis> here refers to
1623 libraries of foreign object code; for using libraries of Haskell
1624 source code, see <xref linkend="ghci-modules-filenames"/>.) For
1625 example, to load the “m” library:</para>
1631 <para>On systems with <literal>.so</literal>-style shared
1632 libraries, the actual library loaded will the
1633 <filename>lib<replaceable>lib</replaceable>.so</filename>. GHCi
1634 searches the following places for libraries, in this order:</para>
1638 <para>Paths specified using the
1639 <literal>-L<replaceable>path</replaceable></literal>
1640 command-line option,</para>
1643 <para>the standard library search path for your system,
1644 which on some systems may be overridden by setting the
1645 <literal>LD_LIBRARY_PATH</literal> environment
1650 <para>On systems with <literal>.dll</literal>-style shared
1651 libraries, the actual library loaded will be
1652 <filename><replaceable>lib</replaceable>.dll</filename>. Again,
1653 GHCi will signal an error if it can't find the library.</para>
1655 <para>GHCi can also load plain object files
1656 (<literal>.o</literal> or <literal>.obj</literal> depending on
1657 your platform) from the command-line. Just add the name the
1658 object file to the command line.</para>
1660 <para>Ordering of <option>-l</option> options matters: a library
1661 should be mentioned <emphasis>before</emphasis> the libraries it
1662 depends on (see <xref linkend="options-linker"/>).</para>
1667 <sect1 id="ghci-commands">
1668 <title>GHCi commands</title>
1670 <para>GHCi commands all begin with
1671 ‘<literal>:</literal>’ and consist of a single command
1672 name followed by zero or more parameters. The command name may be
1673 abbreviated, with ambiguities being resolved in favour of the more
1674 commonly used commands.</para>
1679 <literal>:abandon</literal>
1680 <indexterm><primary><literal>:abandon</literal></primary></indexterm>
1683 <para>Abandons the current evaluation (only available when stopped at
1684 a breakpoint).</para>
1690 <literal>:add</literal> <replaceable>module</replaceable> ...
1691 <indexterm><primary><literal>:add</literal></primary></indexterm>
1694 <para>Add <replaceable>module</replaceable>(s) to the
1695 current <firstterm>target set</firstterm>, and perform a
1702 <literal>:back</literal>
1703 <indexterm><primary><literal>:back</literal></primary></indexterm>
1706 <para>Travel back one step in the history. See <xref
1707 linkend="tracing" />. See also:
1708 <literal>:trace</literal>, <literal>:history</literal>,
1709 <literal>:forward</literal>.</para>
1715 <literal>:break [<replaceable>identifier</replaceable> |
1716 [<replaceable>module</replaceable>] <replaceable>line</replaceable>
1717 [<replaceable>column</replaceable>]]</literal>
1719 <indexterm><primary><literal>:break</literal></primary></indexterm>
1721 <para>Set a breakpoint on the specified function or line and
1722 column. See <xref linkend="setting-breakpoints" />.</para>
1728 <literal>:browse</literal> <optional><literal>*</literal></optional><replaceable>module</replaceable> ...
1729 <indexterm><primary><literal>:browse</literal></primary></indexterm>
1732 <para>Displays the identifiers defined by the module
1733 <replaceable>module</replaceable>, which must be either
1734 loaded into GHCi or be a member of a package. If the
1735 <literal>*</literal> symbol is placed before the module
1736 name, then <emphasis>all</emphasis> the identifiers defined
1737 in <replaceable>module</replaceable> are shown; otherwise
1738 the list is limited to the exports of
1739 <replaceable>module</replaceable>. The
1740 <literal>*</literal>-form is only available for modules
1741 which are interpreted; for compiled modules (including
1742 modules from packages) only the non-<literal>*</literal>
1743 form of <literal>:browse</literal> is available.</para>
1749 <literal>:cd</literal> <replaceable>dir</replaceable>
1750 <indexterm><primary><literal>:cd</literal></primary></indexterm>
1753 <para>Changes the current working directory to
1754 <replaceable>dir</replaceable>. A
1755 ‘<literal>˜</literal>’ symbol at the
1756 beginning of <replaceable>dir</replaceable> will be replaced
1757 by the contents of the environment variable
1758 <literal>HOME</literal>.</para>
1760 <para>NOTE: changing directories causes all currently loaded
1761 modules to be unloaded. This is because the search path is
1762 usually expressed using relative directories, and changing
1763 the search path in the middle of a session is not
1770 <literal>:continue</literal>
1771 <indexterm><primary><literal>:continue</literal></primary></indexterm>
1773 <listitem><para>Continue the current evaluation, when stopped at a
1780 <literal>:cmd</literal> <replaceable>expr</replaceable>
1781 <indexterm><primary><literal>:cmd</literal></primary></indexterm>
1784 <para>Executes <replaceable>expr</replaceable> as a computation of
1785 type <literal>IO String</literal>, and then executes the resulting
1786 string as a list of GHCi commands. Multiple commands are separated
1787 by newlines. The <literal>:cmd</literal> command is useful with
1788 <literal>:def</literal> and <literal>:set stop</literal>.</para>
1794 <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
1795 <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
1796 <indexterm><primary><literal>:etags</literal></primary>
1798 <indexterm><primary><literal>:etags</literal></primary>
1802 <para>Generates a “tags” file for Vi-style editors
1803 (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>). If
1804 no filename is specified, the defaulit <filename>tags</filename> or
1805 <filename>TAGS</filename> is
1806 used, respectively. Tags for all the functions, constructors and
1807 types in the currently loaded modules are created. All modules must
1808 be interpreted for these commands to work.</para>
1809 <para>See also <xref linkend="hasktags" />.</para>
1815 <literal>:def</literal> <replaceable>name</replaceable> <replaceable>expr</replaceable>
1816 <indexterm><primary><literal>:def</literal></primary></indexterm>
1819 <para>The command <literal>:def</literal>
1820 <replaceable>name</replaceable>
1821 <replaceable>expr</replaceable> defines a new GHCi command
1822 <literal>:<replaceable>name</replaceable></literal>,
1823 implemented by the Haskell expression
1824 <replaceable>expr</replaceable>, which must have type
1825 <literal>String -> IO String</literal>. When
1826 <literal>:<replaceable>name</replaceable>
1827 <replaceable>args</replaceable></literal> is typed at the
1828 prompt, GHCi will run the expression
1829 <literal>(<replaceable>name</replaceable>
1830 <replaceable>args</replaceable>)</literal>, take the
1831 resulting <literal>String</literal>, and feed it back into
1832 GHCi as a new sequence of commands. Separate commands in
1833 the result must be separated by
1834 ‘<literal>\n</literal>’.</para>
1836 <para>That's all a little confusing, so here's a few
1837 examples. To start with, here's a new GHCi command which
1838 doesn't take any arguments or produce any results, it just
1839 outputs the current date & time:</para>
1842 Prelude> let date _ = Time.getClockTime >>= print >> return ""
1843 Prelude> :def date date
1845 Fri Mar 23 15:16:40 GMT 2001
1848 <para>Here's an example of a command that takes an argument.
1849 It's a re-implementation of <literal>:cd</literal>:</para>
1852 Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""
1853 Prelude> :def mycd mycd
1857 <para>Or I could define a simple way to invoke
1858 “<literal>ghc ––make Main</literal>” in the
1859 current directory:</para>
1862 Prelude> :def make (\_ -> return ":! ghc ––make Main")
1865 <para>We can define a command that reads GHCi input from a
1866 file. This might be useful for creating a set of bindings
1867 that we want to repeatedly load into the GHCi session:</para>
1870 Prelude> :def . readFile
1871 Prelude> :. cmds.ghci
1874 <para>Notice that we named the command
1875 <literal>:.</literal>, by analogy with the
1876 ‘<literal>.</literal>’ Unix shell command that
1877 does the same thing.</para>
1883 <literal>:delete * | <replaceable>num</replaceable> ...</literal>
1884 <indexterm><primary><literal>:delete</literal></primary></indexterm>
1887 <para>Delete one or more breakpoints by number (use <literal>:show
1888 breaks</literal> to see the number of each breakpoint). The
1889 <literal>*</literal> form deletes all the breakpoints.</para>
1895 <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
1896 <indexterm><primary><literal>:edit</literal></primary></indexterm>
1899 <para>Opens an editor to edit the file
1900 <replaceable>file</replaceable>, or the most recently loaded
1901 module if <replaceable>file</replaceable> is omitted. The
1902 editor to invoke is taken from the <literal>EDITOR</literal>
1903 environment variable, or a default editor on your system if
1904 <literal>EDITOR</literal> is not set. You can change the
1905 editor using <literal>:set editor</literal>.</para>
1911 <literal>:force <replaceable>identifier</replaceable> ...</literal>
1912 <indexterm><primary><literal>:force</literal></primary></indexterm>
1915 <para>Prints the value of <replaceable>identifier</replaceable> in
1916 the same way as <literal>:print</literal>. Unlike
1917 <literal>:print</literal>, <literal>:force</literal> evaluates each
1918 thunk that it encounters while traversing the value. This may
1919 cause exceptions or infinite loops, or further breakpoints (which
1920 are ignored, but displayed).</para>
1926 <literal>:forward</literal>
1927 <indexterm><primary><literal>:forward</literal></primary></indexterm>
1930 <para>Move forward in the history. See <xref
1931 linkend="tracing" />. See also:
1932 <literal>:trace</literal>, <literal>:history</literal>,
1933 <literal>:back</literal>.</para>
1939 <literal>:help</literal>
1940 <indexterm><primary><literal>:help</literal></primary></indexterm>
1943 <literal>:?</literal>
1944 <indexterm><primary><literal>:?</literal></primary></indexterm>
1947 <para>Displays a list of the available commands.</para>
1953 <literal>:history [<replaceable>num</replaceable>]</literal>
1954 <indexterm><primary><literal>:history</literal></primary></indexterm>
1957 <para>Display the history of evaluation steps. With a number,
1958 displays that many steps (default: 20). For use with
1959 <literal>:trace</literal>; see <xref
1960 linkend="tracing" />.</para>
1966 <literal>:info</literal> <replaceable>name</replaceable> ...
1967 <indexterm><primary><literal>:info</literal></primary></indexterm>
1970 <para>Displays information about the given name(s). For
1971 example, if <replaceable>name</replaceable> is a class, then
1972 the class methods and their types will be printed; if
1973 <replaceable>name</replaceable> is a type constructor, then
1974 its definition will be printed; if
1975 <replaceable>name</replaceable> is a function, then its type
1976 will be printed. If <replaceable>name</replaceable> has
1977 been loaded from a source file, then GHCi will also display
1978 the location of its definition in the source.</para>
1984 <literal>:kind</literal> <replaceable>type</replaceable>
1985 <indexterm><primary><literal>:kind</literal></primary></indexterm>
1988 <para>Infers and prints the kind of
1989 <replaceable>type</replaceable>. The latter can be an arbitrary
1990 type expression, including a partial application of a type constructor,
1991 such as <literal>Either Int</literal>.</para>
1997 <literal>:load</literal> <replaceable>module</replaceable> ...
1998 <indexterm><primary><literal>:load</literal></primary></indexterm>
2001 <para>Recursively loads the specified
2002 <replaceable>module</replaceable>s, and all the modules they
2003 depend on. Here, each <replaceable>module</replaceable>
2004 must be a module name or filename, but may not be the name
2005 of a module in a package.</para>
2007 <para>All previously loaded modules, except package modules,
2008 are forgotten. The new set of modules is known as the
2009 <firstterm>target set</firstterm>. Note that
2010 <literal>:load</literal> can be used without any arguments
2011 to unload all the currently loaded modules and
2014 <para>After a <literal>:load</literal> command, the current
2015 context is set to:</para>
2019 <para><replaceable>module</replaceable>, if it was loaded
2020 successfully, or</para>
2023 <para>the most recently successfully loaded module, if
2024 any other modules were loaded as a result of the current
2025 <literal>:load</literal>, or</para>
2028 <para><literal>Prelude</literal> otherwise.</para>
2036 <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
2037 <indexterm><primary><literal>:main</literal></primary></indexterm>
2041 When a program is compiled and executed, it can use the
2042 <literal>getArgs</literal> function to access the
2043 command-line arguments.
2044 However, we cannot simply pass the arguments to the
2045 <literal>main</literal> function while we are testing in ghci,
2046 as the <literal>main</literal> function doesn't take its
2051 Instead, we can use the <literal>:main</literal> command.
2052 This runs whatever <literal>main</literal> is in scope, with
2053 any arguments being treated the same as command-line arguments,
2058 Prelude> let main = System.Environment.getArgs >>= print
2059 Prelude> :main foo bar
2068 <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
2069 <indexterm><primary><literal>:module</literal></primary></indexterm>
2072 <literal>import <replaceable>mod</replaceable></literal>
2075 <para>Sets or modifies the current context for statements
2076 typed at the prompt. The form <literal>import
2077 <replaceable>mod</replaceable></literal> is equivalent to
2078 <literal>:module +<replaceable>mod</replaceable></literal>.
2079 See <xref linkend="ghci-scope"/> for
2080 more details.</para>
2086 <literal>:print </literal> <replaceable>names</replaceable> ...
2087 <indexterm><primary><literal>:print</literal></primary></indexterm>
2090 <para>Prints a value without forcing its evaluation.
2091 <literal>:print</literal> may be used on values whose types are
2092 unkonwn or partially known, which might be the case for local
2093 variables with polymorphic types at a breakpoint. While inspecting
2094 the runtime value, <literal>:print</literal> attempts to
2095 reconstruct the type of the value, and will elaborate the type in
2096 GHCi's environment if possible. If any unevaluated components
2097 (thunks) are encountered, then <literal>:print</literal> binds
2098 a fresh variable with a name beginning with <literal>_t</literal>
2099 to each thunk. See <xref linkend="breakpoints" /> for more
2100 information. See also the <literal>:sprint</literal> command,
2101 which works like <literal>:print</literal> but does not bind new
2108 <literal>:quit</literal>
2109 <indexterm><primary><literal>:quit</literal></primary></indexterm>
2112 <para>Quits GHCi. You can also quit by typing a control-D
2113 at the prompt.</para>
2119 <literal>:reload</literal>
2120 <indexterm><primary><literal>:reload</literal></primary></indexterm>
2123 <para>Attempts to reload the current target set (see
2124 <literal>:load</literal>) if any of the modules in the set,
2125 or any dependent module, has changed. Note that this may
2126 entail loading new modules, or dropping modules which are no
2127 longer indirectly required by the target.</para>
2133 <literal>:set</literal> <optional><replaceable>option</replaceable>...</optional>
2134 <indexterm><primary><literal>:set</literal></primary></indexterm>
2137 <para>Sets various options. See <xref linkend="ghci-set"/>
2138 for a list of available options. The
2139 <literal>:set</literal> command by itself shows which
2140 options are currently set.</para>
2146 <literal>:set</literal> <literal>args</literal> <replaceable>arg</replaceable> ...
2147 <indexterm><primary><literal>:set args</literal></primary></indexterm>
2150 <para>Sets the list of arguments which are returned when the
2151 program calls <literal>System.getArgs</literal><indexterm><primary>getArgs</primary>
2152 </indexterm>.</para>
2158 <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
2161 <para>Sets the command used by <literal>:edit</literal> to
2162 <replaceable>cmd</replaceable>.</para>
2168 <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
2169 <indexterm><primary><literal>:set prog</literal></primary></indexterm>
2172 <para>Sets the string to be returned when the program calls
2173 <literal>System.getProgName</literal><indexterm><primary>getProgName</primary>
2174 </indexterm>.</para>
2180 <literal>:set</literal> <literal>prompt</literal> <replaceable>prompt</replaceable>
2183 <para>Sets the string to be used as the prompt in GHCi.
2184 Inside <replaceable>prompt</replaceable>, the sequence
2185 <literal>%s</literal> is replaced by the names of the
2186 modules currently in scope, and <literal>%%</literal> is
2187 replaced by <literal>%</literal>.</para>
2193 <literal>:set</literal> <literal>stop</literal>
2194 [<replaceable>num</replaceable>] <replaceable>cmd</replaceable>
2197 <para>Set a command to be executed when a breakpoint is hit, or a new
2198 item in the history is selected. The most common use of
2199 <literal>:set stop</literal> is to display the source code at the
2200 current location, e.g. <literal>:set stop :list</literal>.</para>
2202 <para>If a number is given before the command, then the commands are
2203 run when the specified breakpoint (only) is hit. This can be quite
2204 useful: for example, <literal>:set stop 1 :continue</literal>
2205 effectively disables breakpoint 1, by running
2206 <literal>:continue</literal> whenever it is hit (although GHCi will
2207 still emit a message to say the breakpoint was hit). What's more,
2208 with cunning use of <literal>:def</literal> and
2209 <literal>:cmd</literal> you can use <literal>:set stop</literal> to
2210 implement conditional breakpoints:</para>
2212 *Main> :def cond \expr -> return (":cmd if (" ++ expr ++ ") then return \"\" else return \":continue\"")
2213 *Main> :set stop 0 :cond (x < 3)
2215 <para>Ignoring breakpoints for a specified number of iterations is
2216 also possible using similar techniques.</para>
2222 <literal>:show bindings</literal>
2223 <indexterm><primary><literal>:show bindings</literal></primary></indexterm>
2226 <para>Show the bindings made at the prompt and their
2233 <literal>:show breaks</literal>
2234 <indexterm><primary><literal>:show breaks</literal></primary></indexterm>
2237 <para>List the active breakpoints.</para>
2243 <literal>:show context</literal>
2244 <indexterm><primary><literal>:show context</literal></primary></indexterm>
2247 <para>List the active evaluations that are stopped at breakpoints.</para>
2253 <literal>:show modules</literal>
2254 <indexterm><primary><literal>:show modules</literal></primary></indexterm>
2257 <para>Show the list of modules currently load.</para>
2263 <literal>:show [args|prog|prompt|editor|stop]</literal>
2264 <indexterm><primary><literal>:show</literal></primary></indexterm>
2267 <para>Displays the specified setting (see
2268 <literal>:set</literal>).</para>
2274 <literal>:sprint</literal>
2275 <indexterm><primary><literal>:sprint</literal></primary></indexterm>
2278 <para>Prints a value without forcing its evaluation.
2279 <literal>:sprint</literal> is similar to <literal>:print</literal>,
2280 with the difference that unevaluated subterms are not bound to new
2281 variables, they are simply denoted by ‘_’.</para>
2287 <literal>:step [<replaceable>expr</replaceable>]</literal>
2288 <indexterm><primary><literal>:step</literal></primary></indexterm>
2291 <para>Single-step from the last breakpoint. With an expression
2292 argument, begins evaluation of the expression with a
2299 <literal>:trace [<replaceable>expr</replaceable>]</literal>
2300 <indexterm><primary><literal>:trace</literal></primary></indexterm>
2303 <para>Evaluates the given expression (or from the last breakpoint if
2304 no expression is given), and additionally logs the evaluation
2305 steps for later inspection using <literal>:history</literal>. See
2306 <xref linkend="tracing" />.</para>
2312 <literal>:type</literal> <replaceable>expression</replaceable>
2313 <indexterm><primary><literal>:type</literal></primary></indexterm>
2316 <para>Infers and prints the type of
2317 <replaceable>expression</replaceable>, including explicit
2318 forall quantifiers for polymorphic types. The monomorphism
2319 restriction is <emphasis>not</emphasis> applied to the
2320 expression during type inference.</para>
2326 <literal>:undef</literal> <replaceable>name</replaceable>
2327 <indexterm><primary><literal>:undef</literal></primary></indexterm>
2330 <para>Undefines the user-defined command
2331 <replaceable>name</replaceable> (see <literal>:def</literal>
2338 <literal>:unset</literal> <replaceable>option</replaceable>...
2339 <indexterm><primary><literal>:unset</literal></primary></indexterm>
2342 <para>Unsets certain options. See <xref linkend="ghci-set"/>
2343 for a list of available options.</para>
2349 <literal>:!</literal> <replaceable>command</replaceable>...
2350 <indexterm><primary><literal>:!</literal></primary></indexterm>
2351 <indexterm><primary>shell commands</primary><secondary>in GHCi</secondary></indexterm>
2354 <para>Executes the shell command
2355 <replaceable>command</replaceable>.</para>
2362 <sect1 id="ghci-set">
2363 <title>The <literal>:set</literal> command</title>
2364 <indexterm><primary><literal>:set</literal></primary></indexterm>
2366 <para>The <literal>:set</literal> command sets two types of
2367 options: GHCi options, which begin with
2368 ‘<literal>+</literal>” and “command-line”
2369 options, which begin with ‘-’. </para>
2371 <para>NOTE: at the moment, the <literal>:set</literal> command
2372 doesn't support any kind of quoting in its arguments: quotes will
2373 not be removed and cannot be used to group words together. For
2374 example, <literal>:set -DFOO='BAR BAZ'</literal> will not do what
2378 <title>GHCi options</title>
2379 <indexterm><primary>options</primary><secondary>GHCi</secondary>
2382 <para>GHCi options may be set using <literal>:set</literal> and
2383 unset using <literal>:unset</literal>.</para>
2385 <para>The available GHCi options are:</para>
2390 <literal>+r</literal>
2391 <indexterm><primary><literal>+r</literal></primary></indexterm>
2392 <indexterm><primary>CAFs</primary><secondary>in GHCi</secondary></indexterm>
2393 <indexterm><primary>Constant Applicative Form</primary><see>CAFs</see></indexterm>
2396 <para>Normally, any evaluation of top-level expressions
2397 (otherwise known as CAFs or Constant Applicative Forms) in
2398 loaded modules is retained between evaluations. Turning
2399 on <literal>+r</literal> causes all evaluation of
2400 top-level expressions to be discarded after each
2401 evaluation (they are still retained
2402 <emphasis>during</emphasis> a single evaluation).</para>
2404 <para>This option may help if the evaluated top-level
2405 expressions are consuming large amounts of space, or if
2406 you need repeatable performance measurements.</para>
2412 <literal>+s</literal>
2413 <indexterm><primary><literal>+s</literal></primary></indexterm>
2416 <para>Display some stats after evaluating each expression,
2417 including the elapsed time and number of bytes allocated.
2418 NOTE: the allocation figure is only accurate to the size
2419 of the storage manager's allocation area, because it is
2420 calculated at every GC. Hence, you might see values of
2421 zero if no GC has occurred.</para>
2427 <literal>+t</literal>
2428 <indexterm><primary><literal>+t</literal></primary></indexterm>
2431 <para>Display the type of each variable bound after a
2432 statement is entered at the prompt. If the statement is a
2433 single expression, then the only variable binding will be
2435 ‘<literal>it</literal>’.</para>
2441 <sect2 id="ghci-cmd-line-options">
2442 <title>Setting GHC command-line options in GHCi</title>
2444 <para>Normal GHC command-line options may also be set using
2445 <literal>:set</literal>. For example, to turn on
2446 <option>-fglasgow-exts</option>, you would say:</para>
2449 Prelude> :set -fglasgow-exts
2452 <para>Any GHC command-line option that is designated as
2453 <firstterm>dynamic</firstterm> (see the table in <xref
2454 linkend="flag-reference"/>), may be set using
2455 <literal>:set</literal>. To unset an option, you can set the
2456 reverse option:</para>
2457 <indexterm><primary>dynamic</primary><secondary>options</secondary></indexterm>
2460 Prelude> :set -fno-glasgow-exts
2463 <para><xref linkend="flag-reference"/> lists the reverse for each
2464 option where applicable.</para>
2466 <para>Certain static options (<option>-package</option>,
2467 <option>-I</option>, <option>-i</option>, and
2468 <option>-l</option> in particular) will also work, but some may
2469 not take effect until the next reload.</para>
2470 <indexterm><primary>static</primary><secondary>options</secondary></indexterm>
2473 <sect1 id="ghci-dot-files">
2474 <title>The <filename>.ghci</filename> file</title>
2475 <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>
2477 <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
2480 <para>When it starts, GHCi always reads and executes commands from
2481 <filename>$HOME/.ghci</filename>, followed by
2482 <filename>./.ghci</filename>.</para>
2484 <para>The <filename>.ghci</filename> in your home directory is
2485 most useful for turning on favourite options (eg. <literal>:set
2486 +s</literal>), and defining useful macros. Placing a
2487 <filename>.ghci</filename> file in a directory with a Haskell
2488 project is a useful way to set certain project-wide options so you
2489 don't have to type them everytime you start GHCi: eg. if your
2490 project uses GHC extensions and CPP, and has source files in three
2491 subdirectories A B and C, you might put the following lines in
2492 <filename>.ghci</filename>:</para>
2495 :set -fglasgow-exts -cpp
2499 <para>(Note that strictly speaking the <option>-i</option> flag is
2500 a static one, but in fact it works to set it using
2501 <literal>:set</literal> like this. The changes won't take effect
2502 until the next <literal>:load</literal>, though.)</para>
2504 <para>Two command-line options control whether the
2505 <filename>.ghci</filename> files are read:</para>
2510 <option>-ignore-dot-ghci</option>
2511 <indexterm><primary><option>-ignore-dot-ghci</option></primary></indexterm>
2514 <para>Don't read either <filename>./.ghci</filename> or
2515 <filename>$HOME/.ghci</filename> when starting up.</para>
2520 <option>-read-dot-ghci</option>
2521 <indexterm><primary><option>-read-dot-ghci</option></primary></indexterm>
2524 <para>Read <filename>.ghci</filename> and
2525 <filename>$HOME/.ghci</filename>. This is normally the
2526 default, but the <option>-read-dot-ghci</option> option may
2527 be used to override a previous
2528 <option>-ignore-dot-ghci</option> option.</para>
2535 <sect1 id="ghci-obj">
2536 <title>Compiling to object code inside GHCi</title>
2538 <para>By default, GHCi compiles Haskell source code into byte-code
2539 that is interpreted by the runtime system. GHCi can also compile
2540 Haskell code to object code: to turn on this feature, use the
2541 <option>-fobject-code</option> flag either on the command line or
2542 with <literal>:set</literal> (the option
2543 <option>-fbyte-code</option> restores byte-code compilation
2544 again). Compiling to object code takes longer, but typically the
2545 code will execute 10-20 times faster than byte-code.</para>
2547 <para>Compiling to object code inside GHCi is particularly useful
2548 if you are developing a compiled application, because the
2549 <literal>:reload</literal> command typically runs much faster than
2550 restarting GHC with <option>--make</option> from the command-line,
2551 because all the interface files are already cached in
2554 <para>There are disadvantages to compiling to object-code: you
2555 can't set breakpoints in object-code modules, for example. Only
2556 the exports of an object-code module will be visible in GHCi,
2557 rather than all top-level bindings as in interpreted
2561 <sect1 id="ghci-faq">
2562 <title>FAQ and Things To Watch Out For</title>
2566 <term>The interpreter can't load modules with foreign export
2567 declarations!</term>
2569 <para>Unfortunately not. We haven't implemented it yet.
2570 Please compile any offending modules by hand before loading
2571 them into GHCi.</para>
2577 <literal>-O</literal> doesn't work with GHCi!
2578 <indexterm><primary><option>-O</option></primary></indexterm>
2581 <para>For technical reasons, the bytecode compiler doesn't
2582 interact well with one of the optimisation passes, so we
2583 have disabled optimisation when using the interpreter. This
2584 isn't a great loss: you'll get a much bigger win by
2585 compiling the bits of your code that need to go fast, rather
2586 than interpreting them with optimisation turned on.</para>
2591 <term>Unboxed tuples don't work with GHCi</term>
2593 <para>That's right. You can always compile a module that
2594 uses unboxed tuples and load it into GHCi, however.
2595 (Incidentally the previous point, namely that
2596 <literal>-O</literal> is incompatible with GHCi, is because
2597 the bytecode compiler can't deal with unboxed
2603 <term>Concurrent threads don't carry on running when GHCi is
2604 waiting for input.</term>
2606 <para>This should work, as long as your GHCi was built with
2607 the <option>-threaded</option> switch, which is the default.
2608 Consult whoever supplied your GHCi installation.</para>
2613 <term>After using <literal>getContents</literal>, I can't use
2614 <literal>stdin</literal> again until I do
2615 <literal>:load</literal> or <literal>:reload</literal>.</term>
2618 <para>This is the defined behaviour of
2619 <literal>getContents</literal>: it puts the stdin Handle in
2620 a state known as <firstterm>semi-closed</firstterm>, wherein
2621 any further I/O operations on it are forbidden. Because I/O
2622 state is retained between computations, the semi-closed
2623 state persists until the next <literal>:load</literal> or
2624 <literal>:reload</literal> command.</para>
2626 <para>You can make <literal>stdin</literal> reset itself
2627 after every evaluation by giving GHCi the command
2628 <literal>:set +r</literal>. This works because
2629 <literal>stdin</literal> is just a top-level expression that
2630 can be reverted to its unevaluated state in the same way as
2631 any other top-level expression (CAF).</para>
2636 <term>I can't use Control-C to interrupt computations in
2637 GHCi on Windows.</term>
2639 <para>See <xref linkend="ghci-windows"/></para>
2648 ;;; Local Variables: ***
2650 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***