Fixed spelling error in compiler/ghci/InteractiveUI.hs and docs/users_guide/ghci.xml
[ghc-hetmet.git] / docs / users_guide / ghci.xml
index 786815d..f9efe34 100644 (file)
 $ ghci
    ___         ___ _
   / _ \ /\  /\/ __(_)
- / /_\// /_/ / /  | |      GHC Interactive, version 5.04, for Haskell 98.
+ / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
 \____/\/ /_/\____/|_|      Type :? for help.
 
 Loading package base ... linking ... done.
-Loading package haskell98 ... linking ... done.
 Prelude> 
 </screen>
 
@@ -45,40 +44,44 @@ Prelude>
 <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;
-   :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
-   :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
-   :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 (defauilt: "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
+   &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>
 
@@ -350,7 +353,7 @@ Ok, modules loaded: A, B, C, D.
 </screen>
 
     <para>HINT: since GHCi will only use a compiled object file if it
-    can sure that the compiled version is up-to-date, a good technique
+    can be sure that the compiled version is up-to-date, a good technique
     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
@@ -402,7 +405,7 @@ hello
 </screen>
 </para></sect2>
 
-    <sect2>
+    <sect2 id="ghci-stmts">
       <title>Using <literal>do-</literal>notation at the prompt</title>
       <indexterm><primary>do-notation</primary><secondary>in GHCi</secondary></indexterm>
       <indexterm><primary>statements</primary><secondary>in GHCi</secondary></indexterm>
@@ -444,7 +447,13 @@ Prelude>
              <literal>Show</literal></para>
          </listitem>
        </itemizedlist>
-      </para>
+      The automatic printing of binding results can be supressed with
+      <option>:set -fno-print-bind-result</option> (this does not
+      supress printing the result of non-binding statements).
+      <indexterm><primary><option>-fno-print-bind-result</option></primary></indexterm><indexterm><primary><option>-fprint-bind-result</option></primary></indexterm>.
+      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.</para>
 
       <para>Of course, you can also bind normal non-IO expressions
       using the <literal>let</literal>-statement:</para>
@@ -561,9 +570,9 @@ Compiling Main             ( Main.hs, interpreted )
 
 <screen>
 Prelude> :module +IO
-Prelude,IO> hPutStrLn stdout "hello\n"
+Prelude IO> hPutStrLn stdout "hello\n"
 hello
-Prelude,IO>
+Prelude IO>
 </screen>
 
       <para>(Note: <literal>:module</literal> can be shortened to
@@ -593,7 +602,7 @@ Prelude,IO>
       <literal>Bar</literal>, then the scope will be set to
       <literal>*Bar</literal> if <literal>Bar</literal> is
       interpreted, or if <literal>Bar</literal> is compiled it will be
-      set to <literal>Prelude,Bar</literal> (GHCi automatically adds
+      set to <literal>Prelude Bar</literal> (GHCi automatically adds
       <literal>Prelude</literal> if it isn't present and there aren't
       any <literal>*</literal>-form modules).</para>
 
@@ -604,6 +613,12 @@ Prelude,IO>
       behaves in the same way for expressions typed at the
       prompt.</para>
 
+      <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>.
+      </para>
+
       <sect3>
        <title>Qualified names</title>
 
@@ -612,6 +627,34 @@ Prelude,IO>
         qualified</literal> declaration for every module in every
         package, and every module currently loaded into GHCi.</para>
       </sect3>
+
+      <sect3>
+        <title>The <literal>:main</literal> command</title>
+
+        <para>
+          When a program is compiled and executed, it can use the
+          <literal>getArgs</literal> function to access the
+          command-line arguments.
+          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.
+        </para>
+
+        <para>
+          Instead, we can use the <literal>:main</literal> command.
+          This runs whatever <literal>main</literal> is in scope, with
+          any arguments being treated the same as command-line arguments,
+          e.g.:
+        </para>
+
+<screen>
+Prelude> let main = System.Environment.getArgs >>= print
+Prelude> :main foo bar
+["foo","bar"]
+</screen>
+
+      </sect3>
     </sect2>
   
 
@@ -633,9 +676,9 @@ Prelude> it * 2
     expression, and if it doesn't have an <literal>IO</literal> type,
     then it transforms it as follows: an expression
     <replaceable>e</replaceable> turns into 
-<screen>     
-             let it = <replaceable>e</replaceable>;
-             print it
+<screen>
+let it = <replaceable>e</replaceable>;
+print it
 </screen>
     which is then run as an IO-action.</para>
 
@@ -644,10 +687,14 @@ Prelude> it * 2
     complain:</para>
 
 <screen>
-Prelude> id
-No instance for `Show (a -> a)'
-arising from use of `print'
-in a `do' expression pattern binding: print it
+Prelude&gt; id
+
+&lt;interactive&gt;:1:0:
+    No instance for (Show (a -&gt; a))
+      arising from use of `print' at &lt;interactive&gt;:1:0-1
+    Possible fix: add an instance declaration for (Show (a -> a))
+    In the expression: print it
+    In a 'do' expression: print it
 </screen>
 
     <para>The error message contains some clues as to the
@@ -659,14 +706,15 @@ in a `do' expression pattern binding: print it
       which is of type <literal>a</literal>.  eg.:</para>
 <screen>
 Prelude> Time.getClockTime
+Wed Mar 14 12:23:13 GMT 2001
 Prelude> print it
 Wed Mar 14 12:23:13 GMT 2001
 </screen>
 
       <para>The corresponding translation for an IO-typed
       <replaceable>e</replaceable> is
-<screen>     
-             it &lt;- <replaceable>e</replaceable>
+<screen>
+it &lt;- <replaceable>e</replaceable>
 </screen>
       </para>
 
@@ -676,7 +724,7 @@ Wed Mar 14 12:23:13 GMT 2001
 
     </sect2>
 
-    <sect2>
+    <sect2 id="extended-default-rules">
       <title>Type defaulting in GHCi</title>
     <indexterm><primary>Type default</primary></indexterm>
     <indexterm><primary><literal>Show</literal> class</primary></indexterm>
@@ -715,6 +763,8 @@ Wed Mar 14 12:23:13 GMT 2001
            numeric, <emphasis>or is <literal>Show</literal>, 
                <literal>Eq</literal>, or <literal>Ord</literal></emphasis>.</para></listitem>
       </itemizedlist>
+   The same type-default behaviour can be enabled in an ordinary Haskell
+   module, using the flag <literal>-fextended-default-rules</literal>.
    </para>
     </sect2>
   </sect1>
@@ -757,22 +807,15 @@ $ ghci Main.hs
       package be loaded by using the <literal>-package</literal> flag:</para>
 
 <screen>
-$ ghci -package data
+$ ghci -package readline
    ___         ___ _
   / _ \ /\  /\/ __(_)
- / /_\// /_/ / /  | |      GHC Interactive, version 5.05, for Haskell 98.
+ / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
 / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
 \____/\/ /_/\____/|_|      Type :? for help.
 
 Loading package base ... linking ... done.
-Loading package haskell98 ... linking ... done.
-Loading package lang ... linking ... done.
-Loading package concurrent ... linking ... done.
-Loading package readline ... linking ... done.
-Loading package unix ... linking ... done.
-Loading package posix ... linking ... done.
-Loading package util ... linking ... done.
-Loading package data ... linking ... done.
+Loading package readline-1.0 ... linking ... done.
 Prelude> 
 </screen>
 
@@ -845,10 +888,8 @@ $ ghci -lm
     <para>GHCi commands all begin with
     &lsquo;<literal>:</literal>&rsquo; and consist of a single command
     name followed by zero or more parameters.  The command name may be
-    abbreviated, as long as the abbreviation is not ambiguous.  All of
-    the builtin commands, with the exception of
-    <literal>:unset</literal> and <literal>:undef</literal>, may be
-    abbreviated to a single letter.</para>
+    abbreviated, with ambiguities being resolved in favour of the more
+    commonly used commands.</para>
 
     <variablelist>
       <varlistentry>
@@ -975,6 +1016,22 @@ Prelude> :. cmds.ghci
 
       <varlistentry>
        <term>
+          <literal>:edit <optional><replaceable>file</replaceable></optional></literal>
+          <indexterm><primary><literal>:edit</literal></primary></indexterm>
+        </term>
+       <listitem>
+         <para>Opens an editor to edit the file
+         <replaceable>file</replaceable>, or the most recently loaded
+         module if <replaceable>file</replaceable> is omitted.  The
+         editor to invoke is taken from the <literal>EDITOR</literal>
+         environment variable, or a default editor on your system if
+         <literal>EDITOR</literal> is not set.  You can change the
+         editor using <literal>:set editor</literal>.</para>
+       </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>
           <literal>:help</literal>
           <indexterm><primary><literal>:help</literal></primary></indexterm>
         </term>
@@ -1046,6 +1103,38 @@ Prelude> :. cmds.ghci
 
       <varlistentry>
        <term>
+          <literal>:main <replaceable>arg<subscript>1</subscript></replaceable> ... <replaceable>arg<subscript>n</subscript></replaceable></literal>
+          <indexterm><primary><literal>:main</literal></primary></indexterm>
+        </term>
+        <listitem>
+          <para>
+            When a program is compiled and executed, it can use the
+            <literal>getArgs</literal> function to access the
+            command-line arguments.
+            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.
+          </para>
+
+          <para>
+            Instead, we can use the <literal>:main</literal> command.
+            This runs whatever <literal>main</literal> is in scope, with
+            any arguments being treated the same as command-line arguments,
+            e.g.:
+          </para>
+
+<screen>
+Prelude> let main = System.Environment.getArgs >>= print
+Prelude> :main foo bar
+["foo","bar"]
+</screen>
+
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>
           <literal>:module <optional>+|-</optional> <optional>*</optional><replaceable>mod<subscript>1</subscript></replaceable> ... <optional>*</optional><replaceable>mod<subscript>n</subscript></replaceable></literal>
           <indexterm><primary><literal>:module</literal></primary></indexterm>
         </term>
@@ -1108,6 +1197,16 @@ Prelude> :. cmds.ghci
 
       <varlistentry>
        <term>
+           <literal>:set</literal> <literal>editor</literal> <replaceable>cmd</replaceable>
+        </term>
+       <listitem>
+         <para>Sets the command used by <literal>:edit</literal> to
+         <replaceable>cmd</replaceable>.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+       <term>
            <literal>:set</literal> <literal>prog</literal> <replaceable>prog</replaceable>
            <indexterm><primary><literal>:set prog</literal></primary></indexterm>
         </term>