X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fghci.xml;h=7fd49e7e57a2a151708d2b00250ec246ed12e0dd;hb=7494b4052881cf795cc828c8f84b89183a9a17e7;hp=f3a50dc248fc9f8ec82dc0a0d28f119b5cf707ce;hpb=79fb37d4fe333f5c3dd7f08a6df102aed05394d9;p=ghc-hetmet.git diff --git a/docs/users_guide/ghci.xml b/docs/users_guide/ghci.xml index f3a50dc..7fd49e7 100644 --- a/docs/users_guide/ghci.xml +++ b/docs/users_guide/ghci.xml @@ -18,7 +18,7 @@ FFIGHCi support Foreign Function InterfaceGHCi support. - GHCi also includes an interactive debugger (see ).. + GHCi also includes an interactive debugger (see ). Introduction to GHCi @@ -28,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 @@ -268,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 @@ -296,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: @@ -305,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> @@ -323,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. @@ -347,8 +293,6 @@ 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. @@ -359,7 +303,7 @@ Ok, modules loaded: A, B, C, D. 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. @@ -577,7 +521,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: @@ -618,7 +564,7 @@ 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 ”. @@ -769,7 +715,7 @@ it <- e At the GHCi prompt, or with GHC if the - -fextended-default-rules flag is given, + -XExtendedDefaultRules flag is given, the following additional differences apply: @@ -818,8 +764,8 @@ def = toEnum 0 instance that returns IO a. However, it is only able to return undefined - (the reason for the instance having this type is to not require - extensions to the class system), so if the type defaults to + (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. @@ -842,7 +788,7 @@ def = toEnum 0 The debugger provides the following: - The abilty to set a breakpoint on a + 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 @@ -1071,7 +1017,7 @@ right :: [a] 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 @@ -1134,7 +1080,7 @@ right :: [a] Listing and deleting breakpoints The list of breakpoints currently enabled can be displayed using - :show breaks: + :show breaks: *Main> :show breaks [0] Main qsort.hs:1:11-12 @@ -1373,7 +1319,9 @@ a :: a 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. + 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 @@ -1540,7 +1488,7 @@ Just 20 Implicit parameters (see ) are only available - at the scope of a breakpoint if there is a explicit type signature. + at the scope of a breakpoint if there is an explicit type signature. @@ -1568,9 +1516,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 @@ -1586,12 +1532,7 @@ $ ghci Main.hs $ 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> @@ -1672,6 +1613,17 @@ $ ghci -lm + :abandon + :abandon + + + Abandons the current evaluation (only available when stopped at + a breakpoint). + + + + + :add module ... :add @@ -1684,12 +1636,27 @@ $ ghci -lm - :breakpoint list|add|continue|del|stop|step ... - :breakpoint + :back + :back + + + Travel back one step in the history. See . See also: + :trace, :history, + :forward. + + + + + + :break [identifier | + [module] line + [column]] + :break - Permits to add, delete or list the breakpoints in a debugging session. - + Set a breakpoint on the specified function or line and + column. See . @@ -1737,10 +1704,46 @@ $ ghci -lm + :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 - Shortcut to :breakpoint 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 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 . @@ -1814,6 +1817,18 @@ Prelude> :. cmds.ghci + :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. + + + + + :edit file :edit @@ -1830,6 +1845,34 @@ Prelude> :. cmds.ghci + :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 @@ -1844,6 +1887,19 @@ Prelude> :. cmds.ghci + :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 @@ -1862,6 +1918,19 @@ Prelude> :. cmds.ghci + :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 @@ -1936,9 +2005,15 @@ 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. @@ -1949,39 +2024,19 @@ Prelude> :main foo bar :print - Prints a semievaluated value without forcing its evaluation. - :print works just like :sprint but additionally, - :print binds the unevaluated parts -called - suspensions- - to names which you can play with. For example: - -Prelude> let li = map Just [1..5] -Prelude> :sp li -li - _ -Prelude> :p li -li - (_t1::[Maybe Integer]) -Prelude> head li -Just 1 -Prelude> :sp li -li - Just 1 : _ -Prelude> :p li -li - Just 1 : (_t2::[Maybe Integer]) -Prelude> last li -Just 5 -Prelude> :sp li -li - [Just 1,_,_,_,Just 5] -Prelude> :p li -li - [Just 1,(_t3::Maybe Integer),(_t4::Maybe Integer),(_t5::Maybe Integer),Just 4] -Prelude> _t4 -Just 3 -Prelude> :p li -li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] - - The example uses :print and :sprint - to help us observe how the li variable is evaluated progressively as we operate - with it. Note for instance how last traverses all the elements of - the list to compute its result, but without evaluating the individual elements. - + Prints a value without forcing its evaluation. + :print may be used on values whose types are + unkonwn 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. @@ -2072,6 +2127,35 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] + :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 @@ -2083,6 +2167,26 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] + :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 @@ -2090,79 +2194,67 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4] Show the list of modules currently load. + + + + :show [args|prog|prompt|editor|stop] + :show + + + Displays the specified setting (see + :set). + + + :sprint :sprint - Prints a semievaluated value without forcing its evaluation. - :sprint and its sibling :print - are very useful to observe how lazy evaluation works in your code. For example: - -Prelude> let li = map Just [1..5] -Prelude> :sp li -li - _ -Prelude> head li -Just 1 -Prelude> :sp li -li - Just 1 : _ -Prelude> last li -Just 5 -Prelude> :sp li -li - [Just 1,_,_,_,Just 5] - - The example uses :sprint to help us observe how the li variable is evaluated progressively as we operate - with it. Note for instance how last traverses all the elements of - the list to compute its result, but without evaluating the individual elements. - + 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 ‘_’. + - :ctags filename - :etags filename - :etags - - :etags - - + :step [expr] + :step + - 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 . + Single-step from the last breakpoint. With an expression + argument, begins evaluation of the expression with a + single-step. - :type expression - :type + :trace [expr] + :trace - 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. + 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 + . - :kind type - :kind + :type expression + :type - 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. + 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.