X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fghci.xml;h=d9295484c85e2f84ddf5ea1550b7604f6898e7a4;hb=aedb94f5f220b5e442b23ecc445fd38c8d9b6ba0;hp=f56c079114e8438f16ea49b17853152162eaa54e;hpb=c61001a6d252f2f260c271de30d76e64e4626c4f;p=ghc-hetmet.git diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index f56c079..d929548 100644 --- a/docs/users_guide/ghci.xml +++ b/docs/users_guide/ghci.xml @@ -14,11 +14,13 @@ , then you'll be right at home with GHCi. However, GHCi also has support for interactively loading compiled code, as well as supporting allexcept foreign export, at the moment - the language extensions that GHC provides. + the language extensions that GHC provides. FFIGHCi support - Foreign Function InterfaceGHCi support + Foreign Function + InterfaceGHCi support. + GHCi also includes an interactive debugger (see ). - + Introduction to GHCi Let's start with an example GHCi session. You can fire up @@ -26,64 +28,15 @@ $ ghci - ___ ___ _ - / _ \ /\ /\/ __(_) - / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98. -/ /_\\/ __ / /___| | http://www.haskell.org/ghc/ -\____/\/ /_/\____/|_| Type :? for help. - +GHCi, version 6.8.1: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> There may be a short pause while GHCi loads the prelude and - standard libraries, after which the prompt is shown. If we follow - the instructions and type :? for help, we - get: - - - Commands available from the prompt: - - <stmt> evaluate/run <stmt> - :add <filename> ... add module(s) to the current target set - :browse [*]<module> display the names defined by <module> - :cd <dir> change directory to <dir> - :def <cmd> <expr> define a command :<cmd> - :edit <file> edit file - :edit edit last module - :help, :? display this list of commands - :info [<name> ...] display information about the given names - :load <filename> ... load module(s) and their dependents - :module [+/-] [*]<mod> ... set the context for expression evaluation - :main [<arguments> ...] run the main function with the given arguments - :reload reload the current module set - - :set <option> ... set options - :set args <arg> ... set the arguments returned by System.getArgs - :set prog <progname> set the value returned by System.getProgName - :set prompt <prompt> set the prompt used in GHCi - :set editor <cmd> set the command used for :edit - - :show modules show the currently loaded modules - :show bindings show the current bindings made at the prompt - - :ctags [<file>] create tags file for Vi (default: "tags") - :etags [<file>] create tags file for Emacs (default: "TAGS") - :type <expr> show the type of <expr> - :kind <type> show the kind of <type> - :undef <cmd> undefine user-defined command :<cmd> - :unset <option> ... unset options - :quit exit GHCi - :!<command> run the shell command <command> - - Options for ':set' and ':unset': - - +r revert top-level expressions after each evaluation - +s print timing/memory stats after each evaluation - +t print type after evaluation - -<flags> most GHC command line flags can also be set here - (eg. -v2, -fglasgow-exts, etc.) - + standard libraries, after which the prompt is shown. As the banner + says, you can type :? to see the list of commands + available, and a half line description of each of them. We'll explain most of these commands as we go along. For Hugs users: many things work the same as in Hugs, so you should be @@ -106,7 +59,7 @@ Prelude> enter, GHCi will attempt to evaluate it. - + Loading source files Suppose we have the following Haskell source code, which we @@ -249,12 +202,12 @@ Ok, modules loaded: Main. very often, and use the interpreter for the code being actively developed. - When loading up source files with :load, - GHCi looks for any corresponding compiled object files, and will - use one in preference to interpreting the source if possible. For - example, suppose we have a 4-module program consisting of modules - A, B, C, and D. Modules B and C both import D only, - and A imports both B & C: + When loading up source modules with :load, + GHCi normally looks for any corresponding compiled object files, + and will use one in preference to interpreting the source if + possible. For example, suppose we have a 4-module program + consisting of modules A, B, C, and D. Modules B and C both import + D only, and A imports both B & C: A / \ @@ -266,19 +219,17 @@ Ok, modules loaded: Main. Prelude> :! ghc -c D.hs Prelude> :load A -Skipping D ( D.hs, D.o ) -Compiling C ( C.hs, interpreted ) Compiling B ( B.hs, interpreted ) +Compiling C ( C.hs, interpreted ) Compiling A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. *Main> - In the messages from the compiler, we see that it skipped D, - and used the object file D.o. The message - Skipping module - indicates that compilation for module - isn't necessary, because the source and everything it depends on + In the messages from the compiler, we see that there is no line + for D. This is because + it isn't necessary to compile D, + because the source and everything it depends on is unchanged since the last compilation. At any time you can use the command @@ -294,7 +245,7 @@ B ( B.hs, interpreted ) A ( A.hs, interpreted ) *Main> - If we now modify the source of D (or pretend to: using Unix + If we now modify the source of D (or pretend to: using the Unix command touch on the source file is handy for this), the compiler will no longer be able to use the object file, because it might be out of date: @@ -303,9 +254,6 @@ A ( A.hs, interpreted ) *Main> :! touch D.hs *Main> :reload Compiling D ( D.hs, interpreted ) -Skipping C ( C.hs, interpreted ) -Skipping B ( B.hs, interpreted ) -Skipping A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. *Main> @@ -321,8 +269,8 @@ Ok, modules loaded: A, B, C, D. *Main> :! ghc -c C.hs *Main> :load A Compiling D ( D.hs, interpreted ) -Compiling C ( C.hs, interpreted ) Compiling B ( B.hs, interpreted ) +Compiling C ( C.hs, interpreted ) Compiling A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. @@ -345,25 +293,50 @@ Ok, modules loaded: A, B, C, D. *Main> :load A -Skipping D ( D.hs, D.o ) -Skipping C ( C.hs, C.o ) Compiling B ( B.hs, interpreted ) Compiling A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. + The automatic loading of object files can sometimes lead to + confusion, because non-exported top-level definitions of a module + are only available for use in expressions at the prompt when the + module is interpreted (see ). For + this reason, you might sometimes want to force GHCi to load a + module using the interpreter. This can be done by prefixing + a * to the module name or filename when + using :load, for example + + +Prelude> :load *A +Compiling A ( A.hs, interpreted ) +*A> + + +When the * is used, GHCi ignores any + pre-compiled object code and interprets the module. If you have + already loaded a number of modules as object code and decide that + you wanted to interpret one of them, instead of re-loading the whole + set you can use :add *M to specify that you want + M to be interpreted (note that this might cause + other modules to be interpreted too, because compiled modules cannot + depend on interpreted ones). + +To always compile everything to object code and never use the + interpreter, use the -fobject-code option (see + ). + HINT: since GHCi will only use a compiled object file if it can be sure that the compiled version is up-to-date, a good technique when working on a large program is to occasionally run ghc ––make to compile the whole project (say before you go for lunch :-), then continue working in the - interpreter. As you modify code, the new modules will be + interpreter. As you modify code, the changed modules will be interpreted, but the rest of the project will remain compiled. - - + Interactive evaluation at the prompt When you type an expression at the prompt, GHCi immediately @@ -422,7 +395,6 @@ hello IO monad. Prelude> x <- return 42 -42 Prelude> print x 42 Prelude> @@ -434,7 +406,8 @@ Prelude> x in future statements, for example to print it as we did above. - GHCi will print the result of a statement if and only if: + If is set then + GHCi will print the result of a statement if and only if: The statement is not a binding, or it is a monadic binding @@ -447,13 +420,8 @@ Prelude> Show - The automatic printing of binding results can be supressed with - (this does not - supress printing the result of non-binding statements). - . - You might want to do this to prevent the result of binding - statements from being fully evaluated by the act of printing - them, for example. + . + Of course, you can also bind normal non-IO expressions using the let-statement: @@ -478,6 +446,45 @@ Prelude> Note that let bindings do not automatically print the value bound, unlike monadic bindings. + Hint: you can also use let-statements + to define functions at the prompt: + +Prelude> let add a b = a + b +Prelude> add 1 2 +3 +Prelude> + + However, this quickly gets tedious when defining functions + with multiple clauses, or groups of mutually recursive functions, + because the complete definition has to be given on a single line, + using explicit braces and semicolons instead of layout: + +Prelude> let { f op n [] = n ; f op n (h:t) = h `op` f op n t } +Prelude> f (+) 0 [1..3] +6 +Prelude> + + To alleviate this issue, GHCi commands can be split over + multiple lines, by wrapping them in :{ and + :} (each on a single line of its own): + +Prelude> :{ +Prelude| let { g op n [] = n +Prelude| ; g op n (h:t) = h `op` g op n t +Prelude| } +Prelude| :} +Prelude> g (*) 1 [1..3] +6 + + Such multiline commands can be used with any GHCi command, + and the lines between :{ and + :} are simply merged into a single line for + interpretation. That implies that each such group must form a single + valid command when merged, and that no layout rule is used. + The main purpose of multiline commands is not to replace module + loading but to make definitions in .ghci-files (see ) more readable and maintainable. + Any exceptions raised during the evaluation or execution of the statement are caught and printed by the GHCi command line interface (for more information on exceptions, see the module @@ -557,10 +564,14 @@ Compiling Main ( Main.hs, interpreted ) scopes from multiple modules, in any mixture of * and non-* forms. GHCi combines the scopes from all of these modules to form the scope - that is in effect at the prompt. For technical reasons, GHCi - can only support the *-form for modules which - are interpreted, so compiled modules and package modules can - only contribute their exports to the current scope. + that is in effect at the prompt. + + NOTE: for technical reasons, GHCi can only support the + *-form for modules that are interpreted. + Compiled modules and package modules can only contribute their + exports to the current scope. To ensure that GHCi loads the + interpreted version of a module, add the * + when loading the module, e.g. :load *M. The scope is manipulated using the :module command. For example, if the current @@ -575,7 +586,9 @@ hello Prelude IO> - (Note: :module can be shortened to + (Note: you can use import M as an + alternative to :module +M, and + :module can also be shortened to :m). The full syntax of the :module command is: @@ -616,20 +629,56 @@ Prelude IO> Hint: GHCi will tab-complete names that are in scope; for example, if you run GHCi and type J<tab> - then GHCi will expand it to Just . + then GHCi will expand it to “Just ”. + <literal>:module</literal> and + <literal>:load</literal> + + It might seem that :module and + :load do similar things: you can use both + to bring a module into scope. However, there is a clear + difference. GHCi is concerned with two sets of modules: + + + + The set of modules that are + currently loaded. This set is + modified + by :load, :add + and :reload. + + + + The set of modules that are currently in + scope at the prompt. This set is modified + by :module, and it is also set + automatically + after :load, :add, + and :reload. + + + + You cannot add a module to the scope if it is not + loaded. This is why trying to + use :module to load a new module results + in the message “module M is not + loaded”. + + + Qualified names To make life slightly easier, the GHCi prompt also behaves as if there is an implicit import qualified declaration for every module in every - package, and every module currently loaded into GHCi. + package, and every module currently loaded into GHCi. This + behaviour can be disabled with the flag . - The <literal>:main</literal> command + The <literal>:main</literal> and <literal>:run</literal> commands When a program is compiled and executed, it can use the @@ -654,6 +703,37 @@ Prelude> :main foo bar ["foo","bar"] + + We can also quote arguments which contains characters like + spaces, and they are treated like Haskell strings, or we can + just use Haskell list syntax: + + + +Prelude> :main foo "bar baz" +["foo","bar baz"] +Prelude> :main ["foo", "bar baz"] +["foo","bar baz"] + + + + Finally, other functions can be called, either with the + -main-is flag or the :run + command: + + + +Prelude> let foo = putStrLn "foo" >> System.Environment.getArgs >>= print +Prelude> let bar = putStrLn "bar" >> System.Environment.getArgs >>= print +Prelude> :set -main-is foo +Prelude> :main foo "bar baz" +foo +["foo","bar baz"] +Prelude> :run bar ["foo", "bar baz"] +bar +["foo","bar baz"] + + @@ -747,25 +827,826 @@ it <- e standard rules take each group of constraints (C1 a, C2 a, ..., Cn a) for each type variable a, and defaults the type variable if - - The type variable a - appears in no other constraints - All the classes Ci are standard. - At least one of the classes Ci is - numeric. + + + + The type variable a appears in no + other constraints + + + + + All the classes Ci are standard. + + + + + At least one of the classes Ci is + numeric. + + + + At the GHCi prompt, or with GHC if the + -XExtendedDefaultRules flag is given, + the following additional differences apply: + + + + Rule 2 above is relaxed thus: + All of the classes + Ci are single-parameter type classes. + + + + + Rule 3 above is relaxed this: + At least one of the classes Ci is + numeric, or is Show, + Eq, or + Ord. + + + + + The unit type () is added to the + start of the standard list of types which are tried when + doing type defaulting. + + + + The last point means that, for example, this program: + +main :: IO () +main = print def + +instance Num () + +def :: (Num a, Enum a) => a +def = toEnum 0 + + prints () rather than 0 as the + type is defaulted to () rather than + Integer. + + + The motivation for the change is that it means IO a + actions default to IO (), which in turn means that + ghci won't try to print a result when running them. This is + particularly important for printf, which has an + instance that returns IO a. + However, it is only able to return + undefined + (the reason for the instance having this type is so that printf + doesn't require extensions to the class system), so if the type defaults to + Integer then ghci gives an error when running a + printf. + + + + + + The GHCi Debugger + debuggerin GHCi + + + GHCi contains a simple imperative-style debugger in which you can + stop a running computation in order to examine the values of + variables. The debugger is integrated into GHCi, and is turned on by + default: no flags are required to enable the debugging + facilities. There is one major restriction: breakpoints and + single-stepping are only available in interpreted modules; + compiled code is invisible to the debuggerNote that packages + only contain compiled code, so debugging a package requires + finding its source and loading that directly.. + + The debugger provides the following: + + + The ability to set a breakpoint on a + function definition or expression in the program. When the function + is called, or the expression evaluated, GHCi suspends + execution and returns to the prompt, where you can inspect the + values of local variables before continuing with the + execution. + + + Execution can be single-stepped: the + evaluator will suspend execution approximately after every + reduction, allowing local variables to be inspected. This is + equivalent to setting a breakpoint at every point in the + program. + + + Execution can take place in tracing + mode, in which the evaluator remembers each + evaluation step as it happens, but doesn't suspend execution until + an actual breakpoint is reached. When this happens, the history of + evaluation steps can be inspected. + + + Exceptions (e.g. pattern matching failure and + error) can be treated as breakpoints, to help + locate the source of an exception in the program. + - At the GHCi prompt, the second and third rules are relaxed as follows - (differences italicised): - - All of the classes - Ci are single-parameter type classes. - At least one of the classes Ci is - numeric, or is Show, - Eq, or Ord. + + + There is currently no support for obtaining a “stack + trace”, but the tracing and history features provide a + useful second-best, which will often be enough to establish the + context of an error. For instance, it is possible to break + automatically when an exception is thrown, even if it is thrown + from within compiled code (see ). + + + Breakpoints and inspecting variables + + Let's use quicksort as a running example. Here's the code: + + +qsort [] = [] +qsort (a:as) = qsort left ++ [a] ++ qsort right + where (left,right) = (filter (<=a) as, filter (>a) as) + +main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18]) + + + First, load the module into GHCi: + + +Prelude> :l qsort.hs +[1 of 1] Compiling Main ( qsort.hs, interpreted ) +Ok, modules loaded: Main. +*Main> + + + Now, let's set a breakpoint on the right-hand-side of the second + equation of qsort: + + +*Main> :break 2 +Breakpoint 0 activated at qsort.hs:2:15-46 +*Main> + + + The command :break 2 sets a breakpoint on line + 2 of the most recently-loaded module, in this case + qsort.hs. Specifically, it picks the + leftmost complete subexpression on that line on which to set the + breakpoint, which in this case is the expression + (qsort left ++ [a] ++ qsort right). + + Now, we run the program: + + +*Main> main +Stopped at qsort.hs:2:15-46 +_result :: [a] +a :: a +left :: [a] +right :: [a] +[qsort.hs:2:15-46] *Main> + + + Execution has stopped at the breakpoint. The prompt has changed to + indicate that we are currently stopped at a breakpoint, and the location: + [qsort.hs:2:15-46]. To further clarify the + location, we can use the :list command: + + +[qsort.hs:2:15-46] *Main> :list +1 qsort [] = [] +2 qsort (a:as) = qsort left ++ [a] ++ qsort right +3 where (left,right) = (filter (<=a) as, filter (>a) as) + + + The :list command lists the source code around + the current breakpoint. If your output device supports it, then GHCi + will highlight the active subexpression in bold. + + GHCi has provided bindings for the free variablesWe + originally provided bindings for all variables in scope, rather + than just + the free variables of the expression, but found that this affected + performance considerably, hence the current restriction to just the + free variables. + of the expression + on which the + breakpoint was placed (a, left, + right), and additionally a binding for the result of + the expression (_result). These variables are just + like other variables that you might define in GHCi; you + can use them in expressions that you type at the prompt, you can ask + for their types with :type, and so on. There is one + important difference though: these variables may only have partial + types. For example, if we try to display the value of + left: + + +[qsort.hs:2:15-46] *Main> left + +<interactive>:1:0: + Ambiguous type variable `a' in the constraint: + `Show a' arising from a use of `print' at <interactive>:1:0-3 + Cannot resolve unknown runtime types: a + Use :print or :force to determine these types + + + This is because qsort is a polymorphic function, + and because GHCi does not carry type information at runtime, it cannot + determine the runtime types of free variables that involve type + variables. Hence, when you ask to display left at + the prompt, GHCi can't figure out which instance of + Show to use, so it emits the type error above. + + Fortunately, the debugger includes a generic printing command, + :print, which can inspect the actual runtime value of a + variable and attempt to reconstruct its type. If we try it on + left: + + +[qsort.hs:2:15-46] *Main> :set -fprint-evld-with-show +[qsort.hs:2:15-46] *Main> :print left +left = (_t1::[a]) + + + This isn't particularly enlightening. What happened is that + left is bound to an unevaluated computation (a + suspension, or thunk), and + :print does not force any evaluation. The idea is + that :print can be used to inspect values at a + breakpoint without any unfortunate side effects. It won't force any + evaluation, which could cause the program to give a different answer + than it would normally, and hence it won't cause any exceptions to be + raised, infinite loops, or further breakpoints to be triggered (see + ). + Rather than forcing thunks, :print + binds each thunk to a fresh variable beginning with an + underscore, in this case + _t1. + + The flag -fprint-evld-with-show instructs + :print to reuse + available Show instances when possible. This happens + only when the contents of the variable being inspected + are completely evaluated. + + + If we aren't concerned about preserving the evaluatedness of a + variable, we can use :force instead of + :print. The :force command + behaves exactly like :print, except that it forces + the evaluation of any thunks it encounters: + + +[qsort.hs:2:15-46] *Main> :force left +left = [4,0,3,1] + + + Now, since :force has inspected the runtime + value of left, it has reconstructed its type. We + can see the results of this type reconstruction: + + +[qsort.hs:2:15-46] *Main> :show bindings +_result :: [Integer] +a :: Integer +left :: [Integer] +right :: [Integer] +_t1 :: [Integer] + + + Not only do we now know the type of left, but + all the other partial types have also been resolved. So we can ask + for the value of a, for example: + + +[qsort.hs:2:15-46] *Main> a +8 + + + You might find it useful to use Haskell's + seq function to evaluate individual thunks rather + than evaluating the whole expression with :force. + For example: + + +[qsort.hs:2:15-46] *Main> :print right +right = (_t1::[Integer]) +[qsort.hs:2:15-46] *Main> seq _t1 () +() +[qsort.hs:2:15-46] *Main> :print right +right = 23 : (_t2::[Integer]) + + + We evaluated only the _t1 thunk, revealing the + head of the list, and the tail is another thunk now bound to + _t2. The seq function is a + little inconvenient to use here, so you might want to use + :def to make a nicer interface (left as an exercise + for the reader!). + + Finally, we can continue the current execution: + + +[qsort.hs:2:15-46] *Main> :continue +Stopped at qsort.hs:2:15-46 +_result :: [a] +a :: a +left :: [a] +right :: [a] +[qsort.hs:2:15-46] *Main> + + + The execution continued at the point it previously stopped, and has + now stopped at the breakpoint for a second time. + + + + Setting breakpoints + + Breakpoints can be set in various ways. Perhaps the easiest way to + set a breakpoint is to name a top-level function: + + + :break identifier + + + Where identifier names any top-level + function in an interpreted module currently loaded into GHCi (qualified + names may be used). The breakpoint will be set on the body of the + function, when it is fully applied but before any pattern matching has + taken place. + + Breakpoints can also be set by line (and optionally column) + number: + + + :break line + :break line column + :break module line + :break module line column + + + When a breakpoint is set on a particular line, GHCi sets the + breakpoint on the + leftmost subexpression that begins and ends on that line. If two + complete subexpressions start at the same + column, the longest one is picked. If there is no complete + subexpression on the line, then the leftmost expression starting on + the line is picked, and failing that the rightmost expression that + partially or completely covers the line. + + When a breakpoint is set on a particular line and column, GHCi + picks the smallest subexpression that encloses that location on which + to set the breakpoint. Note: GHC considers the TAB character to have a + width of 1, wherever it occurs; in other words it counts + characters, rather than columns. This matches what some editors do, + and doesn't match others. The best advice is to avoid tab + characters in your source code altogether (see + in ). + + If the module is omitted, then the most recently-loaded module is + used. + + Not all subexpressions are potential breakpoint locations. Single + variables are typically not considered to be breakpoint locations + (unless the variable is the right-hand-side of a function definition, + lambda, or case alternative). The rule of thumb is that all redexes + are breakpoint locations, together with the bodies of functions, + lambdas, case alternatives and binding statements. There is normally + no breakpoint on a let expression, but there will always be a + breakpoint on its body, because we are usually interested in inspecting + the values of the variables bound by the let. + + + + Listing and deleting breakpoints + + The list of breakpoints currently enabled can be displayed using + :show breaks: + +*Main> :show breaks +[0] Main qsort.hs:1:11-12 +[1] Main qsort.hs:2:15-46 + + + To delete a breakpoint, use the :delete + command with the number given in the output from :show breaks: + + +*Main> :delete 0 +*Main> :show breaks +[1] Main qsort.hs:2:15-46 + + + To delete all breakpoints at once, use :delete *. + + + + + + Single-stepping + + Single-stepping is a great way to visualise the execution of your + program, and it is also a useful tool for identifying the source of a + bug. GHCi offers two variants of stepping. Use + :step to enable all the + breakpoints in the program, and execute until the next breakpoint is + reached. Use :steplocal to limit the set + of enabled breakpoints to those in the current top level function. + Similarly, use :stepmodule to single step only on + breakpoints contained in the current module. + For example: + + +*Main> :step main +Stopped at qsort.hs:5:7-47 +_result :: IO () + + + The command :step + expr begins the evaluation of + expr in single-stepping mode. If + expr is omitted, then it single-steps from + the current breakpoint. :stepover + works similarly. + + The :list command is particularly useful when + single-stepping, to see where you currently are: + + +[qsort.hs:5:7-47] *Main> :list +4 +5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18]) +6 +[qsort.hs:5:7-47] *Main> + + + In fact, GHCi provides a way to run a command when a breakpoint is + hit, so we can make it automatically do + :list: + + +[qsort.hs:5:7-47] *Main> :set stop :list +[qsort.hs:5:7-47] *Main> :step +Stopped at qsort.hs:5:14-46 +_result :: [Integer] +4 +5 main = print (qsort [8, 4, 0, 3, 1, 23, 11, 18]) +6 +[qsort.hs:5:14-46] *Main> + + + + + Nested breakpoints + When GHCi is stopped at a breakpoint, and an expression entered at + the prompt triggers a + second breakpoint, the new breakpoint becomes the “current” + one, and the old one is saved on a stack. An arbitrary number of + breakpoint contexts can be built up in this way. For example: + + +[qsort.hs:2:15-46] *Main> :st qsort [1,3] +Stopped at qsort.hs:(1,0)-(3,55) +_result :: [a] +... [qsort.hs:(1,0)-(3,55)] *Main> + + + While stopped at the breakpoint on line 2 that we set earlier, we + started a new evaluation with :step qsort [1,3]. + This new evaluation stopped after one step (at the definition of + qsort). The prompt has changed, now prefixed with + ..., to indicate that there are saved breakpoints + beyond the current one. To see the stack of contexts, use + :show context: + + +... [qsort.hs:(1,0)-(3,55)] *Main> :show context +--> main + Stopped at qsort.hs:2:15-46 +--> qsort [1,3] + Stopped at qsort.hs:(1,0)-(3,55) +... [qsort.hs:(1,0)-(3,55)] *Main> + + + To abandon the current evaluation, use + :abandon: + + +... [qsort.hs:(1,0)-(3,55)] *Main> :abandon +[qsort.hs:2:15-46] *Main> :abandon +*Main> + + + + + The <literal>_result</literal> variable + When stopped at a breakpoint or single-step, GHCi binds the + variable _result to the value of the currently + active expression. The value of _result is + presumably not available yet, because we stopped its evaluation, but it + can be forced: if the type is known and showable, then just entering + _result at the prompt will show it. However, + there's one caveat to doing this: evaluating _result + will be likely to trigger further breakpoints, starting with the + breakpoint we are currently stopped at (if we stopped at a real + breakpoint, rather than due to :step). So it will + probably be necessary to issue a :continue + immediately when evaluating _result. Alternatively, + you can use :force which ignores breakpoints. + + + + Tracing and history + + A question that we often want to ask when debugging a program is + “how did I get here?”. Traditional imperative debuggers + usually provide some kind of stack-tracing feature that lets you see + the stack of active function calls (sometimes called the “lexical + call stack”), describing a path through the code + to the current location. Unfortunately this is hard to provide in + Haskell, because execution proceeds on a demand-driven basis, rather + than a depth-first basis as in strict languages. The + “stack“ in GHC's execution engine bears little + resemblance to the lexical call stack. Ideally GHCi would maintain a + separate lexical call stack in addition to the dynamic call stack, and + in fact this is exactly + what our profiling system does (), and what + some other Haskell debuggers do. For the time being, however, GHCi + doesn't maintain a lexical call stack (there are some technical + challenges to be overcome). Instead, we provide a way to backtrack from a + breakpoint to previous evaluation steps: essentially this is like + single-stepping backwards, and should in many cases provide enough + information to answer the “how did I get here?” + question. + + To use tracing, evaluate an expression with the + :trace command. For example, if we set a breakpoint + on the base case of qsort: + + +*Main> :list qsort +1 qsort [] = [] +2 qsort (a:as) = qsort left ++ [a] ++ qsort right +3 where (left,right) = (filter (<=a) as, filter (>a) as) +4 +*Main> :b 1 +Breakpoint 1 activated at qsort.hs:1:11-12 +*Main> + + + and then run a small qsort with + tracing: + + +*Main> :trace qsort [3,2,1] +Stopped at qsort.hs:1:11-12 +_result :: [a] +[qsort.hs:1:11-12] *Main> + + + We can now inspect the history of evaluation steps: + + +[qsort.hs:1:11-12] *Main> :hist +-1 : qsort.hs:3:24-38 +-2 : qsort.hs:3:23-55 +-3 : qsort.hs:(1,0)-(3,55) +-4 : qsort.hs:2:15-24 +-5 : qsort.hs:2:15-46 +-6 : qsort.hs:3:24-38 +-7 : qsort.hs:3:23-55 +-8 : qsort.hs:(1,0)-(3,55) +-9 : qsort.hs:2:15-24 +-10 : qsort.hs:2:15-46 +-11 : qsort.hs:3:24-38 +-12 : qsort.hs:3:23-55 +-13 : qsort.hs:(1,0)-(3,55) +-14 : qsort.hs:2:15-24 +-15 : qsort.hs:2:15-46 +-16 : qsort.hs:(1,0)-(3,55) +<end of history> + + + To examine one of the steps in the history, use + :back: + + +[qsort.hs:1:11-12] *Main> :back +Logged breakpoint at qsort.hs:3:24-38 +_result :: [a] +as :: [a] +a :: a +[-1: qsort.hs:3:24-38] *Main> + + + Note that the local variables at each step in the history have been + preserved, and can be examined as usual. Also note that the prompt has + changed to indicate that we're currently examining the first step in + the history: -1. The command + :forward can be used to traverse forward in the + history. + + The :trace command can be used with or without + an expression. When used without an expression, tracing begins from + the current breakpoint, just like :step. + + The history is only available when + using :trace; the reason for this is we found that + logging each breakpoint in the history cuts performance by a factor of + 2 or more. GHCi remembers the last 50 steps in the history (perhaps in + the future we'll make this configurable). + + + + Debugging exceptions + Another common question that comes up when debugging is + “where did this exception come from?”. Exceptions such as + those raised by error or head [] + have no context information attached to them. Finding which + particular call to head in your program resulted in + the error can be a painstaking process, usually involving + Debug.Trace.trace, or compiling with + profiling and using +RTS -xc (see ). + + The GHCi debugger offers a way to hopefully shed some light on + these errors quickly and without modifying or recompiling the source + code. One way would be to set a breakpoint on the location in the + source code that throws the exception, and then use + :trace and :history to establish + the context. However, head is in a library and + we can't set a breakpoint on it directly. For this reason, GHCi + provides the flags -fbreak-on-exception which causes + the evaluator to stop when an exception is thrown, and + -fbreak-on-error, which works similarly but stops only on + uncaught exceptions. When stopping at an exception, GHCi will act + just as it does when a breakpoint is hit, with the deviation that it + will not show you any source code location. Due to this, these + commands are only really useful in conjunction with + :trace, in order to log the steps leading up to the + exception. For example: + + +*Main> :set -fbreak-on-exception +*Main> :trace qsort ("abc" ++ undefined) +“Stopped at <exception thrown> +_exception :: e +[<exception thrown>] *Main> :hist +-1 : qsort.hs:3:24-38 +-2 : qsort.hs:3:23-55 +-3 : qsort.hs:(1,0)-(3,55) +-4 : qsort.hs:2:15-24 +-5 : qsort.hs:2:15-46 +-6 : qsort.hs:(1,0)-(3,55) +<end of history> +[<exception thrown>] *Main> :back +Logged breakpoint at qsort.hs:3:24-38 +_result :: [a] +as :: [a] +a :: a +[-1: qsort.hs:3:24-38] *Main> :force as +*** Exception: Prelude.undefined +[-1: qsort.hs:3:24-38] *Main> :print as +as = 'b' : 'c' : (_t1::[Char]) + + + The exception itself is bound to a new variable, + _exception. + + Breaking on exceptions is particularly useful for finding out what + your program was doing when it was in an infinite loop. Just hit + Control-C, and examine the history to find out what was going + on. + + + Example: inspecting functions + + It is possible to use the debugger to examine function values. + When we are at a breakpoint and a function is in scope, the debugger + cannot show + you the source code for it; however, it is possible to get some + information by applying it to some arguments and observing the result. + + + + The process is slightly complicated when the binding is polymorphic. + We show the process by means of an example. + To keep things simple, we will use the well known map function: + +import Prelude hiding (map) + +map :: (a->b) -> [a] -> [b] +map f [] = [] +map f (x:xs) = f x : map f xs + + + + + We set a breakpoint on map, and call it. + +*Main> :break 5 +Breakpoint 0 activated at map.hs:5:15-28 +*Main> map Just [1..5] +Stopped at map.hs:(4,0)-(5,12) +_result :: [b] +x :: a +f :: a -> b +xs :: [a] + + GHCi tells us that, among other bindings, f is in scope. + However, its type is not fully known yet, + and thus it is not possible to apply it to any + arguments. Nevertheless, observe that the type of its first argument is the + same as the type of x, and its result type is shared + with _result. + + + + As we demonstrated earlier (), the + debugger has some intelligence built-in to update the type of + f whenever the types of x or + _result are discovered. So what we do in this + scenario is + force x a bit, in order to recover both its type + and the argument part of f. + +*Main> seq x () +*Main> :print x +x = 1 + + + + We can check now that as expected, the type of x + has been reconstructed, and with it the + type of f has been too: + +*Main> :t x +x :: Integer +*Main> :t f +f :: Integer -> b + + + From here, we can apply f to any argument of type Integer and observe + the results. + let b = f 10 +*Main> :t b +b :: b +*Main> b +:1:0: + Ambiguous type variable `b' in the constraint: + `Show b' arising from a use of `print' at :1:0 +*Main> :p b +b = (_t2::a) +*Main> seq b () +() +*Main> :t b +b :: a +*Main> :p b +b = Just 10 +*Main> :t b +b :: Maybe Integer +*Main> :t f +f :: Integer -> Maybe Integer +*Main> f 20 +Just 20 +*Main> map f [1..5] +[Just 1, Just 2, Just 3, Just 4, Just 5] +]]> + In the first application of f, we had to do + some more type reconstruction + in order to recover the result type of f. + But after that, we are free to use + f normally. + + + + Limitations + + + When stopped at a breakpoint, if you try to evaluate a variable + that is already under evaluation, the second evaluation will hang. + The reason is + that GHC knows the variable is under evaluation, so the new + evaluation just waits for the result before continuing, but of + course this isn't going to happen because the first evaluation is + stopped at a breakpoint. Control-C can interrupt the hung + evaluation and return to the prompt. + The most common way this can happen is when you're evaluating a + CAF (e.g. main), stop at a breakpoint, and ask for the value of the + CAF at the prompt again. + + + Implicit parameters (see ) are only available + at the scope of a breakpoint if there is an explicit type signature. + + - The same type-default behaviour can be enabled in an ordinary Haskell - module, using the flag -fextended-default-rules. - @@ -780,7 +1661,7 @@ it <- e instructs GHCi to load the specified modules or filenames (and all the modules they depend on), just as if you had said :load modules at the - GHCi prompt (see ). For example, to + GHCi prompt (see ). For example, to start GHCi and load the program whose topmost module is in the file Main.hs, we could say: @@ -790,9 +1671,7 @@ $ ghci Main.hs Most of the command-line options accepted by GHC (see ) also make sense in interactive mode. The ones - that don't make sense are mostly obvious; for example, GHCi - doesn't generate interface files, so options related to interface - file generation won't have any effect. + that don't make sense are mostly obvious. Packages @@ -803,17 +1682,12 @@ $ ghci Main.hs they will be automatically loaded the first time they are needed. - For non-auto packages, however, you need to request the + For hidden packages, however, you need to request the package be loaded by using the -package flag: $ ghci -package readline - ___ ___ _ - / _ \ /\ /\/ __(_) - / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98. -/ /_\\/ __ / /___| | http://www.haskell.org/ghc/ -\____/\/ /_/\____/|_| Type :? for help. - +GHCi, version 6.8.1: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Loading package readline-1.0 ... linking ... done. Prelude> @@ -894,34 +1768,110 @@ $ ghci -lm - :add module ... + :abandon + :abandon + + + Abandons the current evaluation (only available when stopped at + a breakpoint). + + + + + + :add *module ... :add Add module(s) to the current target set, and perform a - reload. + reload. Normally pre-compiled code for the module will be + loaded if available, or otherwise the module will be + compiled to byte-code. Using the * + prefix forces the module to be loaded as byte-code. - :browse *module ... + :back + :back + + + Travel back one step in the history. See . See also: + :trace, :history, + :forward. + + + + + + :break [identifier | + [module] line + [column]] + + :break + + Set a breakpoint on the specified function or line and + column. See . + + + + + + :browse! *module ... :browse Displays the identifiers defined by the module module, which must be either - loaded into GHCi or be a member of a package. If the - * symbol is placed before the module - name, then all the identifiers defined - in module are shown; otherwise - the list is limited to the exports of + loaded into GHCi or be a member of a package. If + module is omitted, the most + recently-loaded module is used. + + If the * symbol is placed before + the module name, then all the + identifiers in scope in module are + shown; otherwise the list is limited to the exports of module. The *-form is only available for modules which are interpreted; for compiled modules (including modules from packages) only the non-* - form of :browse is available. + form of :browse is available. + If the ! symbol is appended to the + command, data constructors and class methods will be + listed individually, otherwise, they will only be listed + in the context of their data type or class declaration. + The !-form also annotates the listing + with comments giving possible imports for each group of + entries. + +Prelude> :browse! Data.Maybe +-- not currently imported +Data.Maybe.catMaybes :: [Maybe a] -> [a] +Data.Maybe.fromJust :: Maybe a -> a +Data.Maybe.fromMaybe :: a -> Maybe a -> a +Data.Maybe.isJust :: Maybe a -> Bool +Data.Maybe.isNothing :: Maybe a -> Bool +Data.Maybe.listToMaybe :: [a] -> Maybe a +Data.Maybe.mapMaybe :: (a -> Maybe b) -> [a] -> [b] +Data.Maybe.maybeToList :: Maybe a -> [a] +-- imported via Prelude +Just :: a -> Maybe a +data Maybe a = Nothing | Just a +Nothing :: Maybe a +maybe :: b -> (a -> b) -> Maybe a -> b + + + This output shows that, in the context of the current session, in the scope + of Prelude, the first group of items from + Data.Maybe have not been imported (but are available in + fully qualified form in the GHCi session - see ), whereas the second group of items have been + imported via Prelude and are therefore available either + unqualified, or with a Prelude. qualifier. + @@ -948,26 +1898,73 @@ $ ghci -lm - :def name expr + :cmd expr + :cmd + + + Executes expr as a computation of + type IO String, and then executes the resulting + string as a list of GHCi commands. Multiple commands are separated + by newlines. The :cmd command is useful with + :def and :set stop. + + + + + + :continue + :continue + + Continue the current evaluation, when stopped at a + breakpoint. + + + + + + :ctags filename + :etags filename + :etags + + :etags + + + + Generates a “tags” file for Vi-style editors + (:ctags) or + Emacs-style editors (:etags). If + no filename is specified, the default tags or + TAGS is + used, respectively. Tags for all the functions, constructors and + types in the currently loaded modules are created. All modules must + be interpreted for these commands to work. + See also . + + + + + + :def! name expr :def - The command :def - name - expr defines a new GHCi command - :name, - implemented by the Haskell expression - expr, which must have type - String -> IO String. When - :name - args is typed at the - prompt, GHCi will run the expression - (name - args), take the - resulting String, and feed it back into - GHCi as a new sequence of commands. Separate commands in - the result must be separated by - ‘\n’. + :def is used to define new + commands, or macros, in GHCi. The command + :def name + expr defines a new GHCi command + :name, + implemented by the Haskell expression + expr, which must have type + String -> IO String. When + :name + args is typed at the + prompt, GHCi will run the expression + (name + args), take the + resulting String, and feed it back into + GHCi as a new sequence of commands. Separate commands in + the result must be separated by + ‘\n’. That's all a little confusing, so here's a few examples. To start with, here's a new GHCi command which @@ -1011,6 +2008,24 @@ Prelude> :. cmds.ghci :., by analogy with the ‘.’ Unix shell command that does the same thing. + + Typing :def on its own lists the + currently-defined macros. Attempting to redefine an + existing command name results in an error unless the + :def! form is used, in which case the old + command with that name is silently overwritten. + + + + + + :delete * | num ... + :delete + + + Delete one or more breakpoints by number (use :show + breaks to see the number of each breakpoint). The + * form deletes all the breakpoints. @@ -1032,6 +2047,43 @@ Prelude> :. cmds.ghci + :etags + + + See :ctags. + + + + + + :force identifier ... + :force + + + Prints the value of identifier in + the same way as :print. Unlike + :print, :force evaluates each + thunk that it encounters while traversing the value. This may + cause exceptions or infinite loops, or further breakpoints (which + are ignored, but displayed). + + + + + + :forward + :forward + + + Move forward in the history. See . See also: + :trace, :history, + :back. + + + + + :help :help @@ -1045,6 +2097,30 @@ Prelude> :. cmds.ghci + + : + : + + + Repeat the previous command. + + + + + + + :history [num] + :history + + + Display the history of evaluation steps. With a number, + displays that many steps (default: 20). For use with + :trace; see . + + + + :info name ... :info @@ -1059,12 +2135,31 @@ Prelude> :. cmds.ghci will be printed. If name has been loaded from a source file, then GHCi will also display the location of its definition in the source. + For types and classes, GHCi also summarises instances that + mention them. To avoid showing irrelevant information, an instance + is shown only if (a) its head mentions name, + and (b) all the other things mentioned in the instance + are in scope (either qualified or otherwise) as a result of + a :load or :module commands. + + + + + + :kind type + :kind + + + Infers and prints the kind of + type. The latter can be an arbitrary + type expression, including a partial application of a type constructor, + such as Either Int. - :load module ... + :load *module ... :load @@ -1081,6 +2176,11 @@ Prelude> :. cmds.ghci to unload all the currently loaded modules and bindings. + Normally pre-compiled code for a module will be loaded + if available, or otherwise the module will be compiled to + byte-code. Using the * prefix forces a + module to be loaded as byte-code. + After a :load command, the current context is set to: @@ -1114,7 +2214,7 @@ Prelude> :. cmds.ghci However, we cannot simply pass the arguments to the main function while we are testing in ghci, as the main function doesn't take its - directly. + arguments directly. @@ -1130,6 +2230,37 @@ Prelude> :main foo bar ["foo","bar"] + + We can also quote arguments which contains characters like + spaces, and they are treated like Haskell strings, or we can + just use Haskell list syntax: + + + +Prelude> :main foo "bar baz" +["foo","bar baz"] +Prelude> :main ["foo", "bar baz"] +["foo","bar baz"] + + + + Finally, other functions can be called, either with the + -main-is flag or the :run + command: + + + +Prelude> let foo = putStrLn "foo" >> System.Environment.getArgs >>= print +Prelude> let bar = putStrLn "bar" >> System.Environment.getArgs >>= print +Prelude> :set -main-is foo +Prelude> :main foo "bar baz" +foo +["foo","bar baz"] +Prelude> :run bar ["foo", "bar baz"] +bar +["foo","bar baz"] + + @@ -1138,20 +2269,48 @@ Prelude> :main foo bar :module +|- *mod1 ... *modn :module + + import mod + Sets or modifies the current context for statements - typed at the prompt. See for + typed at the prompt. The form import + mod is equivalent to + :module +mod. + See for more details. + :print names ... + :print + + + Prints a value without forcing its evaluation. + :print may be used on values whose types are + unknown or partially known, which might be the case for local + variables with polymorphic types at a breakpoint. While inspecting + the runtime value, :print attempts to + reconstruct the type of the value, and will elaborate the type in + GHCi's environment if possible. If any unevaluated components + (thunks) are encountered, then :print binds + a fresh variable with a name beginning with _t + to each thunk. See for more + information. See also the :sprint command, + which works like :print but does not bind new + variables. + + + + + :quit :quit - Quits GHCi. You can also quit by typing a control-D + Quits GHCi. You can also quit by typing control-D at the prompt. @@ -1176,10 +2335,11 @@ Prelude> :main foo bar :set - Sets various options. See - for a list of available options. The - :set command by itself shows which - options are currently set. + Sets various options. See for a list of + available options and for a + list of GHCi-specific flags. The :set command by + itself shows which options are currently set. It also lists the current + dynamic flag settings, with GHCi-specific flags listed separately. @@ -1226,12 +2386,43 @@ Prelude> :main foo bar Inside prompt, the sequence %s is replaced by the names of the modules currently in scope, and %% is - replaced by %. + replaced by %. If prompt + starts with " then it is parsed as a Haskell String; + otherwise it is treated as a literal string. + :set stop + [num] cmd + + + Set a command to be executed when a breakpoint is hit, or a new + item in the history is selected. The most common use of + :set stop is to display the source code at the + current location, e.g. :set stop :list. + + If a number is given before the command, then the commands are + run when the specified breakpoint (only) is hit. This can be quite + useful: for example, :set stop 1 :continue + effectively disables breakpoint 1, by running + :continue whenever it is hit (although GHCi will + still emit a message to say the breakpoint was hit). What's more, + with cunning use of :def and + :cmd you can use :set stop to + implement conditional breakpoints: + +*Main> :def cond \expr -> return (":cmd if (" ++ expr ++ ") then return \"\" else return \":continue\"") +*Main> :set stop 0 :cond (x < 3) + + Ignoring breakpoints for a specified number of iterations is + also possible using similar techniques. + + + + + :show bindings :show bindings @@ -1243,59 +2434,116 @@ Prelude> :main foo bar + :show breaks + :show breaks + + + List the active breakpoints. + + + + + + :show context + :show context + + + List the active evaluations that are stopped at breakpoints. + + + + + :show modules :show modules - Show the list of modules currently load. + Show the list of modules currently loaded. - :ctags filename - :etags filename - :etags - - :etags - - + :show packages + :show packages + - Generates a “tags” file for Vi-style editors - (:ctags) or Emacs-style editors (etags). If - no filename is specified, the defaulit tags or - TAGS is - used, respectively. Tags for all the functions, constructors and - types in the currently loaded modules are created. All modules must - be interpreted for these commands to work. - See also . + Show the currently active package flags, as well as the list of + packages currently loaded. - :type expression - :type + :show languages + :show languages - Infers and prints the type of - expression, including explicit - forall quantifiers for polymorphic types. The monomorphism - restriction is not applied to the - expression during type inference. + Show the currently active language flags. + - :kind type - :kind + :show [args|prog|prompt|editor|stop] + :show - Infers and prints the kind of - type. The latter can be an arbitrary - type expression, including a partial application of a type constructor, - such as Either Int. + Displays the specified setting (see + :set). + + + + + + :sprint + :sprint + + + Prints a value without forcing its evaluation. + :sprint is similar to :print, + with the difference that unevaluated subterms are not bound to new + variables, they are simply denoted by ‘_’. + + + + + + :step [expr] + :step + + + Single-step from the last breakpoint. With an expression + argument, begins evaluation of the expression with a + single-step. + + + + + + :trace [expr] + :trace + + + Evaluates the given expression (or from the last breakpoint if + no expression is given), and additionally logs the evaluation + steps for later inspection using :history. See + . + + + + + + :type expression + :type + + + Infers and prints the type of + expression, including explicit + forall quantifiers for polymorphic types. The monomorphism + restriction is not applied to the + expression during type inference. @@ -1343,7 +2591,7 @@ Prelude> :main foo bar The :set command sets two types of options: GHCi options, which begin with - ‘+” and “command-line” + ‘+’, and “command-line” options, which begin with ‘-’. NOTE: at the moment, the :set command @@ -1448,7 +2696,6 @@ Prelude> :set -fno-glasgow-exts staticoptions - The <filename>.ghci</filename> file .ghcifile @@ -1456,18 +2703,35 @@ Prelude> :set -fno-glasgow-exts startupfiles, GHCi - When it starts, GHCi always reads and executes commands from - $HOME/.ghci, followed by - ./.ghci. - - The .ghci in your home directory is - most useful for turning on favourite options (eg. :set - +s), and defining useful macros. Placing a - .ghci file in a directory with a Haskell - project is a useful way to set certain project-wide options so you - don't have to type them everytime you start GHCi: eg. if your - project uses GHC extensions and CPP, and has source files in three - subdirectories A B and C, you might put the following lines in + When it starts, unless the -ignore-dot-ghci + flag is given, GHCi reads and executes commands from the following + files, in this order, if they exist: + + + + ./.ghci + + + appdata/ghc/ghci.conf, + where appdata depends on your system, + but is usually something like C:/Documents and Settings/user/Application Data + + + On Unix: $HOME/.ghc/ghci.conf + + + $HOME/.ghci + + + + The ghci.conf file is most useful for + turning on favourite options (eg. :set +s), and + defining useful macros. Placing a .ghci file + in a directory with a Haskell project is a useful way to set + certain project-wide options so you don't have to type them + everytime you start GHCi: eg. if your project uses GHC extensions + and CPP, and has source files in three subdirectories A, B and C, + you might put the following lines in .ghci: @@ -1480,8 +2744,24 @@ Prelude> :set -fno-glasgow-exts :set like this. The changes won't take effect until the next :load, though.) + Once you have a library of GHCi macros, you may want + to source them from separate files, or you may want to source + your .ghci file into your running GHCi + session while debugging it + + +:def source readFile + + + With this macro defined in your .ghci + file, you can use :source file to read GHCi + commands from file. You can find (and contribute!-) + other suggestions for .ghci files on this Haskell + wiki page: GHC/GHCi + Two command-line options control whether the - .ghci files are read: + startup files files are read: @@ -1490,8 +2770,8 @@ Prelude> :set -fno-glasgow-exts - Don't read either ./.ghci or - $HOME/.ghci when starting up. + Don't read either ./.ghci or the + other startup files when starting up. @@ -1500,8 +2780,8 @@ Prelude> :set -fno-glasgow-exts - Read .ghci and - $HOME/.ghci. This is normally the + Read ./.ghci and the other + startup files (see above). This is normally the default, but the option may be used to override a previous option. @@ -1511,6 +2791,32 @@ Prelude> :set -fno-glasgow-exts + + Compiling to object code inside GHCi + + By default, GHCi compiles Haskell source code into byte-code + that is interpreted by the runtime system. GHCi can also compile + Haskell code to object code: to turn on this feature, use the + flag either on the command line or + with :set (the option + restores byte-code compilation + again). Compiling to object code takes longer, but typically the + code will execute 10-20 times faster than byte-code. + + Compiling to object code inside GHCi is particularly useful + if you are developing a compiled application, because the + :reload command typically runs much faster than + restarting GHC with from the command-line, + because all the interface files are already cached in + memory. + + There are disadvantages to compiling to object-code: you + can't set breakpoints in object-code modules, for example. Only + the exports of an object-code module will be visible in GHCi, + rather than all top-level bindings as in interpreted + modules. + + FAQ and Things To Watch Out For @@ -1589,7 +2895,19 @@ Prelude> :set -fno-glasgow-exts I can't use Control-C to interrupt computations in GHCi on Windows. - See . + See . + + + + + The default buffering mode is different in GHCi to GHC. + + + In GHC, the stdout handle is line-buffered by default. + However, in GHCi we turn off the buffering on stdout, + because this is normally what you want in an interpreter: + output appears as it is generated. +