Update an example on the ghci debugger section
[ghc-hetmet.git] / docs / users_guide / ghci.xml
index aaa9c46..62a9c3b 100644 (file)
@@ -966,10 +966,6 @@ $ ghci -lm
         </term>
        <listitem>
          <para>Permits to add, delete or list the breakpoints in a debugging session.
-         In order to make this command available, the 
-         <literal>-fdebugging</literal> flag must be active. The easiest way is to launch
-         GHCi with the <literal>-fdebugging</literal> option. For more
-         details on how the debugger works, see <xref linkend="ghci-debugger"/>.
          </para>
        </listitem>
       </varlistentry>
@@ -1261,7 +1257,7 @@ li - [Just 1,(_t6::Maybe Integer),Just 3,(_t7::Maybe Integer),Just 4]
          The example uses <literal>:print</literal> and  <literal>:sprint</literal> 
         to help us observe how the <literal>li</literal> variable is evaluated progressively as we operate
          with it. Note for instance how <quote>last</quote> traverses all the elements of
-        the list to compute its result, but without evaluating the individual elements.</para>
+        the list to compute its result, but without evaluating the individual elements.
          </para>
        </listitem>
       </varlistentry>
@@ -1395,7 +1391,7 @@ li - [Just 1,_,_,_,Just 5]
 </screen>
          The example uses <literal>:sprint</literal> to help us observe how the <literal>li</literal> variable is evaluated progressively as we operate
          with it. Note for instance how <quote>last</quote> traverses all the elements of
-        the list to compute its result, but without evaluating the individual elements.</para>
+        the list to compute its result, but without evaluating the individual elements.
          </para>
        </listitem>
       </varlistentry>
@@ -1606,9 +1602,6 @@ Prelude> :set -fno-glasgow-exts
          <listitem>
           <para>The module must have been loaded interpreted, i.e. not loaded from an <filename>.o</filename> file compiled by ghc </para>
         </listitem>
-        <listitem>
-          <para>The module must have been loaded with the <literal>-fdebugging</literal> flag
-          </para></listitem>
        </itemizedlist></para>
     <sect2><title>Using the debugger</title>
     <para>The debugger allows the insertion of breakpoints at specific locations in the source code. These locations are governed by event sites, and not by line as in traditional debuggers such as gdb. </para> <para>
@@ -1664,17 +1657,19 @@ main = <coref linkend="name-binding-co"/> do {
       Simply use ghci to evaluate your Haskell expressions and whenever a breakpoint
       is hit, the debugger will enter the stage:
 <programlisting>
-*main:Main> :break add Main 2
-Breakpoint set at (2,15)
-
-*main:Main> qsort [10,9..1]
-Local bindings in scope:
-  x :: a, xs :: [a], left :: [a], right :: [a]
-
-qsort2.hs:2:15-46>   
+*main:Main> :break qsort
+Breakpoint 0 activated at ../QSort.hs:(4,0)-(6,54)
+*QSort> qsort [10,9,1]
+Stopped at ../QSort.hs:(4,0)-(6,54)
+_result :: [a]
+xs :: [a]
+x :: a
+left :: [a]
+right :: [a]
+[../QSort.hs:(4,0)-(6,54)] *QSort> 
 </programlisting>
       What is happening here is that GHCi has interrupted the evaluation of 
-      <literal>qsort</literal> at the breakpoint set in line 2, as the prompt indicates.
+      <literal>qsort</literal> at the breakpoint, as the prompt indicates.
       At this point you can freely explore the contents of the bindings in scope,
       but with two catches. </para><para>
       First, take into account that due to the lazy nature of Haskell, some of
@@ -1684,66 +1679,65 @@ qsort2.hs:2:15-46>
       GHCi has left its types parameterised by a variable!
       Look at the type of <literal>qsort</literal>, which is 
       polymorphic on the type of its argument. It does not 
-      tell us really what the types of <literal>x</literal> and <literal>xs</literal> can be. 
-      In general, polymorphic programs deal with polymorphic values,
+      tell us really what the types of <literal>x</literal> 
+      and <literal>xs</literal> can be. 
+      In general, polymorphic functions deal with polymorphic values,
       and this means that some of the bindings available in a breakpoint site
       will be parametrically typed.
       </para><para>
       So, what can we do with a value without concrete type? Very few interesting
-      things. The <literal>:print</literal> command in ghci allows you to 
+      things, not even using <literal>show</literal> on it. 
+      The <literal>:print</literal> command in ghci allows you to 
       explore its contents and see if it is evaluated or not. 
-      This is useful because you cannot just type <literal>x</literal> in the 
-      prompt and expect GHCi to return you its value. Perhaps you know for 
-      sure that 
-      <literal>x</literal> is of type <literal>Int</literal>, which is an instance of 
-      <literal>Show</literal>, but GHCi does not have this information. 
-      <literal>:print</literal> however is fine, because it does not need to know the 
-      type to do its work. </para>
+      <literal>:print</literal> works here because it does not need the 
+      type information to do its work. In fact, as we will see later,
+      <literal>:print</literal> can even recover the missing type information.</para>
+
       <para> Let's go on with the debugging session of the <literal>qsort</literal>
       example:
 <example id="debuggingEx"><title>A short debugging session</title>
 <programlisting>
 qsort2.hs:2:15-46> x
-This is an untyped, unevaluated computation. You can use seq to 
-force its evaluation and then :print to recover its type <co id="seq1"/>
+&lt;interactive&gt;:1:0:
+    Ambiguous type variable `a' in the constraint: <co id="seq1"/>
+      `Show a' arising from a use of `print' at &lt;interactive&gt;:1:0
 qsort2.hs:2:15-46> seq x ()  <co id="seq2"/>
 () 
 qsort2.hs:2:15-46> x <co id="seq3"/>
-This is an untyped, unevaluated computation. You can use seq to 
-force its evaluation and then :print to recover its type
-
+&lt;interactive&gt;:1:0:
+    Ambiguous type variable `a' in the constraint:
+      `Show a' arising from a use of `print' at &lt;interactive&gt;:1:0
 qsort2.hs:2:15-46> :t x
-x :: GHC.Base.Unknown
-qsort2.hs:2:15-46> :p x <co id="seq4"/>
-x - 10
+x :: a
+qsort2.hs:2:15-46> :print x <co id="seq4"/>
+x = 10
 qsort2.hs:2:15-46> :t x <co id="seq5"/>
-x :: Int
+x :: Integer
 </programlisting>
 </example>
       <calloutlist>
        <callout arearefs="seq1">
-         <para>GHCi reminds us that this value is untyped, and instructs us to force its evaluation </para>
+         <para>GHCi reminds us that <literal>x</literal> is untyped </para>
        </callout>
        <callout arearefs="seq2">
          <para>This line forces the evaluation of <literal>x</literal> </para>
        </callout>
        <callout arearefs="seq3">
-         <para>Even though x has been evaluated, we cannot simply use its name to see its value! 
-         This is a bit counterintuitive, but currently in GHCi the type of a binding
-         cannot be a type variable <literal>a</literal>. 
-         Thus, the binding <literal>x</literal> gets assigned the concrete type Unknown.</para>
+         <para>Even though x has been evaluated, 
+            we have not updated its type yet. </para>
        </callout>
        <callout arearefs="seq4">
          <para>We can explore <literal>x</literal> using the <literal>:print</literal> 
-         command, which does find out that <literal>x</literal> is of type Int and prints
-         its value accordingly.</para>
+         command, which does find out that <literal>x</literal> is of type Int and 
+         prints its value.</para>
        </callout>
        <callout arearefs="seq5">
-          <para><literal>:print</literal> also updates the type of <literal>x</literal> with
-          the most concrete type information available.</para>
+          <para>In addition, <literal>:print</literal> also updates 
+            its type information.</para>
        </callout>
       </calloutlist>
-      The example shows the standard way to proceeed with polymorphic values in a breakpoint. 
+
+      This example shows the standard way to proceeed with polymorphic values in a breakpoint. 
       </para>
     </sect2>
     <sect2><title>Commands</title>
@@ -1831,30 +1825,93 @@ x :: Int
 </varlistentry>
 </variablelist></para>
     </sect2>
-    <sect2><title>Limitations</title>
-     <para>
-      <itemizedlist>
-       <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.
-       </para></listitem>
-      </itemizedlist>
-      <itemizedlist>
-       <listitem><para>
-         Modules compiled by GHCi under the <literal>-fdebugging
-       </literal> flag  will perform slower: the debugging mode introduces some overhead.
-      Modules compiled to object code by ghc are not affected.
-       </para></listitem>
-      </itemizedlist>      
-     </para>
+    <sect2><title>Debugging Higher-Order functions</title>
+      It is possible to use the debugger to examine lambdas. 
+      When we are at a breakpoint and a lambda is in scope, the debugger cannot show 
+      you the source code that constitutes 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 will use a example to show the process.
+      To keep it simple, we will use the well known <literal>map</literal> function:
+      <programlisting>
+import Prelude hiding (map)
+
+map :: (a->b) -> a -> b
+map f [] = []
+map f (x:xs) = f x : map f xs
+      </programlisting>
+      We set a breakpoint on <literal>map</literal>, and call it.
+      <programlisting>
+*Main> :break map
+Breakpoint 0 activated at map.hs:(4,0)-(5,12)
+*Main> map Just [1..5]
+Stopped at map.hs:(4,0)-(5,12)
+_result :: [b]
+x :: a
+f :: a -> b
+xs :: [a]
+      </programlisting>
+      GHCi tells us that, among other bindings, <literal>f</literal> is in scope. 
+      However, its type is not fully known yet,  
+      and thus it is not possible to apply it yet to any 
+      arguments. Nevertheless, observe that the type of its first argument is the
+      same as the type of <literal>x</literal>, and its result type is the
+      same as the type of <literal>_result</literal>.
+      The debugger has some intelligence built-in to update the type of 
+      <literal>f</literal> whenever the types of <literal>x</literal> or 
+      <literal>_result</literal> are reconstructed. So what we do in this scenario is
+      force <literal>x</literal> a bit, in order to recover both its type 
+      and the argument part of <literal>f</literal>.  
+      <programlisting>
+*Main> seq x ()
+*Main> :print x
+x = 1
+      </programlisting>
+      We can check now that as expected, the type of <literal>x</literal>
+      has been reconstructed, and with it the 
+      type of <literal>f</literal> has been too:
+      <programlisting>
+*Main> :t x
+x :: Integer
+*Main> :t f
+f :: Integer -> b
+      </programlisting>
+      From here, we can apply f to any argument of type Integer and observe the 
+      results. 
+      <programlisting><![CDATA[
+*Main> let b = f 10
+*Main> :t b
+b :: b
+*Main> b
+<interactive>:1:0:
+    Ambiguous type variable `b' in the constraint:
+      `Show b' arising from a use of `print' at <interactive>: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]
+      ]]></programlisting>
+      In the first application of <literal>f</literal>, we had to do 
+      some more type reconstruction
+      in order to recover the result type of <literal>f</literal>. 
+      But after that, we are free to use 
+      <literal>f</literal> normally.
     </sect2>
     <sect2><title>Tips</title>
       <variablelist>
-       <varlistentry><term>* Use PRAGMAs to fine tune which modules are loaded under debugging mode</term>
-         <listitem>
-           <programlisting>{-# OPTIONS_GHC -fdebugging #-}</programlisting>
-         </listitem>
-       </varlistentry>
        <varlistentry> <term>* Repeated use of <literal>seq</literal> and 
            <literal>:print</literal> may be necessary to observe unevaluated
            untyped bindings</term> 
@@ -1865,8 +1922,8 @@ x :: Int
          <listitem><para><programlisting>
 type MyLongType a = [Maybe [Maybe a]]
 
-main:Main> :m +GHC.Exts
-main:Main> main
+*Main> :m +GHC.Exts
+*Main> main
 Local bindings in scope:
   x :: a
 Main.hs:15> let x' = unsafeCoerce x :: MyLongType Bool
@@ -1885,7 +1942,18 @@ Main.hs:15> x'
          </para></listitem>
        </varlistentry> 
     </variablelist>
-    </sect2></sect1>
+    </sect2>
+    <sect2><title>Limitations</title>
+     <para>
+      <itemizedlist>
+       <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.
+       </para></listitem>
+      </itemizedlist>
+     </para>
+    </sect2>
+  </sect1>
   <sect1 id="ghci-dot-files">
     <title>The <filename>.ghci</filename> file</title>
     <indexterm><primary><filename>.ghci</filename></primary><secondary>file</secondary>