Better document :stepover and its limitations
[ghc-hetmet.git] / docs / users_guide / ghci.xml
index 4119836..82ee330 100644 (file)
@@ -34,53 +34,9 @@ Prelude>
 </screen>
 
     <para>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 <literal>:?</literal> for help, we
-    get:</para>
-
-<screen>
- Commands available from the prompt:
-
-   &lt;stmt&gt;                      evaluate/run &lt;stmt&gt;
-   :add &lt;filename&gt; ...         add module(s) to the current target set
-   :browse [*]&lt;module&gt;         display the names defined by &lt;module&gt;
-   :cd &lt;dir&gt;                   change directory to &lt;dir&gt;
-   :def &lt;cmd&gt; &lt;expr&gt;           define a command :&lt;cmd&gt;
-   :edit &lt;file&gt;                edit file
-   :edit                       edit last module
-   :help, :?                   display this list of commands
-   :info [&lt;name&gt; ...]          display information about the given names
-   :load &lt;filename&gt; ...        load module(s) and their dependents
-   :module [+/-] [*]&lt;mod&gt; ...  set the context for expression evaluation
-   :main [&lt;arguments&gt; ...]     run the main function with the given arguments
-   :reload                     reload the current module set
-
-   :set &lt;option&gt; ...           set options
-   :set args &lt;arg&gt; ...         set the arguments returned by System.getArgs
-   :set prog &lt;progname&gt;        set the value returned by System.getProgName
-   :set prompt &lt;prompt&gt;        set the prompt used in GHCi
-   :set editor &lt;cmd&gt;        set the command used for :edit
-
-   :show modules               show the currently loaded modules
-   :show bindings              show the current bindings made at the prompt
-
-   :ctags [&lt;file&gt;]             create tags file for Vi (default: "tags")
-   :etags [&lt;file&gt;]             create tags file for Emacs (default: "TAGS")
-   :type &lt;expr&gt;                show the type of &lt;expr&gt;
-   :kind &lt;type&gt;                show the kind of &lt;type&gt;
-   :undef &lt;cmd&gt;                undefine user-defined command :&lt;cmd&gt;
-   :unset &lt;option&gt; ...         unset options
-   :quit                       exit GHCi
-   :!&lt;command&gt;                 run the shell command &lt;command&gt;
-
- 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
-    -&lt;flags&gt;      most GHC command line flags can also be set here
-                         (eg. -v2, -fglasgow-exts, etc.)
-</screen>
+    standard libraries, after which the prompt is shown. As the banner
+    says, you can type <literal>:?</literal> to see the list of commands
+    available, and a half line description of each of them.</para>
 
     <para>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
@@ -263,19 +219,17 @@ Ok, modules loaded: Main.
 <screen>
 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>
 </screen>
 
-    <para>In the messages from the compiler, we see that it skipped D,
-    and used the object file <filename>D.o</filename>.  The message
-    <literal>Skipping</literal> <replaceable>module</replaceable>
-    indicates that compilation for <replaceable>module</replaceable>
-    isn't necessary, because the source and everything it depends on
+    <para>In the messages from the compiler, we see that there is no line
+    for <literal>D</literal>. This is because
+    it isn't necessary to compile <literal>D</literal>,
+    because the source and everything it depends on
     is unchanged since the last compilation.</para>
 
     <para>At any time you can use the command 
@@ -291,7 +245,7 @@ B                ( B.hs, interpreted )
 A                ( A.hs, interpreted )
 *Main></screen>
 
-    <para>If we now modify the source of D (or pretend to: using Unix
+    <para>If we now modify the source of D (or pretend to: using the Unix
     command <literal>touch</literal> 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:</para>
@@ -300,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> 
 </screen>
@@ -318,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.
 </screen>
@@ -342,8 +293,6 @@ Ok, modules loaded: A, B, C, D.
 
 <screen>
 *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.
@@ -354,7 +303,7 @@ Ok, modules loaded: A, B, C, D.
     when working on a large program is to occasionally run
     <literal>ghc &ndash;&ndash;make</literal> 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.</para>
 
@@ -615,7 +564,7 @@ Prelude IO>
       <para>
         Hint: GHCi will tab-complete names that are in scope; for
         example, if you run GHCi and type <literal>J&lt;tab&gt;</literal>
-        then GHCi will expand it to <literal>Just </literal>.
+        then GHCi will expand it to &ldquo;<literal>Just </literal>&rdquo;.
       </para>
 
       <sect3>
@@ -766,7 +715,7 @@ it &lt;- <replaceable>e</replaceable>
         </listitem>
     </orderedlist>
     At the GHCi prompt, or with GHC if the
-    <literal>-fextended-default-rules</literal> flag is given,
+    <literal>-XExtendedDefaultRules</literal> flag is given,
     the following additional differences apply:
     <itemizedlist>
         <listitem>
@@ -815,8 +764,8 @@ def = toEnum 0
     instance that returns <literal>IO a</literal>.
     However, it is only able to return
     <literal>undefined</literal>
-    (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
     <literal>Integer</literal> then ghci gives an error when running a
     printf.
    </para>
@@ -839,7 +788,7 @@ def = toEnum 0
     <para>The debugger provides the following:
     <itemizedlist>
         <listitem>
-          <para>The abilty to set a <firstterm>breakpoint</firstterm> on a
+          <para>The ability to set a <firstterm>breakpoint</firstterm> 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
@@ -1068,7 +1017,7 @@ right :: [a]
       <para>The execution continued at the point it previously stopped, and has
         now stopped at the breakpoint for a second time.</para>
 
-      <sect3 id="setting-breakpoings">
+      <sect3 id="setting-breakpoints">
         <title>Setting breakpoints</title>
 
         <para>Breakpoints can be set in various ways.  Perhaps the easiest way to
@@ -1131,7 +1080,7 @@ right :: [a]
         <title>Listing and deleting breakpoints</title>
 
         <para>The list of breakpoints currently enabled can be displayed using
-          <literal>:show&nbsp;breaks</literal></para>:
+          <literal>:show&nbsp;breaks</literal>:</para>
 <screen>
 *Main> :show breaks
 [0] Main qsort.hs:1:11-12
@@ -1157,10 +1106,12 @@ right :: [a]
 
       <para>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.  The concept is simple: single-stepping enables all the
-        breakpoints in the program and executes until the next breakpoint is
-        reached, at which point you can single-step again, or continue
-        normally.  For example:</para>
+        bug. GHCi offers two variants of stepping. Use 
+       <literal>:step</literal>  to enable all the
+        breakpoints in the program, and execute until the next breakpoint is
+        reached. Use <literal>:stepover</literal> to step over function
+       applications, which of course are executed all the same. 
+       For example:</para>
 
 <screen>
 *Main> :step main
@@ -1169,13 +1120,53 @@ _result :: IO ()
 </screen>
 
       <para>The command <literal>:step
-          <replaceable>expr</replaceable></literal> begins the evaluation of
+        <replaceable>expr</replaceable></literal> begins the evaluation of
         <replaceable>expr</replaceable> in single-stepping mode.  If
         <replaceable>expr</replaceable> is ommitted, then it single-steps from
-        the current breakpoint.</para>
-
+        the current breakpoint. <literal>:stepover</literal> 
+        works similarly.</para>
+      <para>In the current version of the debugger, <literal>:stepover</literal>
+       is limited to work locally in the lexical sense, that is in the context
+       of the current expression body.
+       When you run to the end of the expression, <literal>:stepover</literal>
+       stops working and switches to behave like regular <literal>:step</literal>. 
+       This means 
+       that it will fail to step over the last function application. As a result,
+       currently <literal>:stepover</literal> works great for monadic code, but 
+       interacts less perfectly with pure code. For example, if stopped at the 
+       line 2, on the entire expression 
+       <literal>qsort left ++ [a] ++ qsort right</literal>:</para>
+<screen>
+... [qsort2.hs:2:15-46] *Main> :step
+Stopped at qsort2.hs:2:15-46
+
+... [qsort2.hs:2:15-46] *Main> :list
+2  qsort (a:as) = qsort left ++ [a] ++ qsort right
+</screen>
+       
+      <para> The first <literal>:stepover</literal> will step over the first
+           <literal>qsort</literal> recursive call, as expected. The second one 
+           will step over the evaluation of <literal>[a]</literal>, again as
+           expected. However, the third one has lexically <quote>run out</quote>
+           of the current expression, and will behave like regular 
+           <literal>:step</literal>, performing one step of lazy evaluation and
+           stopping at the next breakpoint. In this case it is indeed the second
+           recursive application of <literal>qsort</literal>.</para>
+<screen>
+[qsort2.hs:2:36-46] *Main> :stepover
+Warning: no more breakpoints in this function body, switching to :step
+Stopped at qsort2.hs:(1,0)-(3,55)
+
+[qsort2.hs:2:36-46] *Main> :list
+_result :: [a]
+1  qsort [] = []
+2  qsort (a:as) = qsort left ++ [a] ++ qsort right
+3    where (left,right) = (filter (&lt;=a) as, filter (>a) as)
+</screen>
       <para>The <literal>:list</literal> command is particularly useful when
-        single-stepping, to see where you currently are:</para>
+        single-stepping, to see where you currently are, as just shown
+       in the above example.</para>
 
 <screen>
 [qsort.hs:5:7-47] *Main> :list
@@ -1537,9 +1528,13 @@ Just 20
             CAF (e.g. main), stop at a breakpoint, and ask for the value of the
             CAF at the prompt again.</para>
         </listitem>
+       <listitem> <literal>:stepover</literal> only works lexically locally, in the
+         body of the current expression. As a result, it can be rather impredictable
+         when used in pure functional code, as opposed to monadic code.
+       </listitem>
        <listitem><para>
          Implicit parameters (see <xref linkend="implicit-parameters"/>) 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.
        </para>
         </listitem>
       </itemizedlist>
@@ -1567,9 +1562,7 @@ $ ghci Main.hs
 
     <para>Most of the command-line options accepted by GHC (see <xref
     linkend="using-ghc"/>) 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.</para>
+    that don't make sense are mostly obvious.</para>
 
     <sect2>
       <title>Packages</title>
@@ -1585,12 +1578,7 @@ $ ghci Main.hs
 
 <screen>
 $ 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> 
@@ -1762,16 +1750,6 @@ $ ghci -lm
 
       <varlistentry>
        <term>
-          <literal>:continue</literal> 
-          <indexterm><primary><literal>:continue</literal></primary></indexterm>
-        </term>
-       <listitem><para>Continue the current evaluation, when stopped at a
-            breakpoint.</para>
-       </listitem>
-      </varlistentry>
-
-      <varlistentry>
-       <term>
           <literal>:cmd</literal> <replaceable>expr</replaceable>
           <indexterm><primary><literal>:cmd</literal></primary></indexterm>
         </term>
@@ -1786,6 +1764,16 @@ $ ghci -lm
 
       <varlistentry>
        <term>
+          <literal>:continue</literal> 
+          <indexterm><primary><literal>:continue</literal></primary></indexterm>
+        </term>
+       <listitem><para>Continue the current evaluation, when stopped at a
+            breakpoint.</para>
+       </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>
          <literal>:ctags</literal> <optional><replaceable>filename</replaceable></optional>
          <literal>:etags</literal> <optional><replaceable>filename</replaceable></optional>
          <indexterm><primary><literal>:etags</literal></primary>
@@ -1795,7 +1783,8 @@ $ ghci -lm
        </term>
        <listitem>
          <para>Generates a &ldquo;tags&rdquo; file for Vi-style editors
-           (<literal>:ctags</literal>) or Emacs-style editors (<literal>etags</literal>).  If
+           (<literal>:ctags</literal>) or
+        Emacs-style editors (<literal>:etags</literal>).  If
            no filename is specified, the defaulit <filename>tags</filename> or
            <filename>TAGS</filename> is
            used, respectively.  Tags for all the functions, constructors and
@@ -1903,6 +1892,15 @@ Prelude> :. cmds.ghci
 
       <varlistentry>
        <term>
+          <literal>:etags</literal> 
+        </term>
+       <listitem>
+         <para>See <literal>:ctags</literal>.</para>
+       </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>
           <literal>:force <replaceable>identifier</replaceable> ...</literal>
           <indexterm><primary><literal>:force</literal></primary></indexterm>
         </term>
@@ -1971,6 +1969,12 @@ Prelude> :. cmds.ghci
          will be printed.  If <replaceable>name</replaceable> has
          been loaded from a source file, then GHCi will also display
          the location of its definition in the source.</para>
+         <para>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 <replaceable>name</replaceable>, 
+         and (b) all the other things mentioned in the instance
+         are in scope (either qualified or otherwise) as a result of 
+         a <literal>:load</literal> or <literal>:module</literal> commands. </para>
        </listitem>
       </varlistentry>
 
@@ -2039,7 +2043,7 @@ Prelude> :. cmds.ghci
             However, we cannot simply pass the arguments to the
             <literal>main</literal> function while we are testing in ghci,
             as the <literal>main</literal> function doesn't take its
-            directly.
+            arguments directly.
           </para>
 
           <para>
@@ -2084,7 +2088,7 @@ Prelude> :main foo bar
        <listitem>
          <para>Prints a value without forcing its evaluation.
             <literal>:print</literal> may be used on values whose types are
-            unkonwn or partially known, which might be the case for local
+            unknown or partially known, which might be the case for local
             variables with polymorphic types at a breakpoint.  While inspecting
             the runtime value, <literal>:print</literal> attempts to
             reconstruct the type of the value, and will elaborate the type in
@@ -2104,7 +2108,7 @@ Prelude> :main foo bar
           <indexterm><primary><literal>:quit</literal></primary></indexterm>
         </term>
        <listitem>
-         <para>Quits GHCi.  You can also quit by typing a control-D
+         <para>Quits GHCi.  You can also quit by typing control-D
          at the prompt.</para>
        </listitem>
       </varlistentry>
@@ -2249,7 +2253,7 @@ Prelude> :main foo bar
           <indexterm><primary><literal>:show modules</literal></primary></indexterm>
         </term>
        <listitem>
-         <para>Show the list of modules currently load.</para>
+         <para>Show the list of modules currently loaded.</para>
        </listitem>
       </varlistentry>
 
@@ -2360,7 +2364,7 @@ Prelude> :main foo bar
 
     <para>The <literal>:set</literal> command sets two types of
     options: GHCi options, which begin with
-    &lsquo;<literal>+</literal>&rdquo; and &ldquo;command-line&rdquo;
+    &lsquo;<literal>+</literal>&rsquo;, and &ldquo;command-line&rdquo;
     options, which begin with &lsquo;-&rsquo;.  </para>
 
     <para>NOTE: at the moment, the <literal>:set</literal> command
@@ -2472,9 +2476,10 @@ Prelude> :set -fno-glasgow-exts
     <indexterm><primary>startup</primary><secondary>files, GHCi</secondary>
     </indexterm>
 
-    <para>When it starts, GHCi always reads and executes commands from
-    <filename>$HOME/.ghci</filename>, followed by
-    <filename>./.ghci</filename>.</para>
+    <para>When it starts, unless the <literal>-ignore-dot-ghci</literal>
+    flag is given, GHCi reads and executes commands from
+    <filename>./.ghci</filename>, followed by
+    <filename>$HOME/.ghci</filename>.</para>
 
     <para>The <filename>.ghci</filename> in your home directory is
     most useful for turning on favourite options (eg. <literal>:set
@@ -2483,7 +2488,7 @@ Prelude> :set -fno-glasgow-exts
     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
+    subdirectories A, B and C, you might put the following lines in
     <filename>.ghci</filename>:</para>
 
 <screen>
@@ -2631,7 +2636,19 @@ Prelude> :set -fno-glasgow-exts
        <term>I can't use Control-C to interrupt computations in
           GHCi on Windows.</term>
         <listitem>
-          <para>See <xref linkend="ghci-windows"/></para>
+          <para>See <xref linkend="ghci-windows"/>.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>The default buffering mode is different in GHCi to GHC.</term>
+        <listitem>
+          <para>
+            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.
+          </para>
         </listitem>
       </varlistentry>
     </variablelist>