Preliminary monad-comprehension patch (Trac #4370)
[ghc-hetmet.git] / docs / users_guide / glasgow_exts.xml
index a29e747..54a4833 100644 (file)
@@ -450,43 +450,6 @@ Indeed, the bindings can even be recursive.
       </para>
    </sect2>
 
-    <sect2 id="new-qualified-operators">
-      <title>New qualified operator syntax</title>
-
-      <para>A new syntax for referencing qualified operators is
-        planned to be introduced by Haskell', and is enabled in GHC
-        with
-        the <option>-XNewQualifiedOperators</option><indexterm><primary><option>-XNewQualifiedOperators</option></primary></indexterm>
-        option.  In the new syntax, the prefix form of a qualified
-        operator is
-        written <literal><replaceable>module</replaceable>.(<replaceable>symbol</replaceable>)</literal>
-        (without NewQualifiedOperators this would
-        be <literal>(<replaceable>module</replaceable>.<replaceable>symbol</replaceable>)</literal>),
-        and the infix form is
-        written <literal>`<replaceable>module</replaceable>.(<replaceable>symbol</replaceable>)`</literal>
-        (without NewQualifiedOperators this would
-        be <literal>`<replaceable>module</replaceable>.<replaceable>symbol</replaceable>`</literal>.
-        For example:
-<programlisting>
-  add x y = Prelude.(+) x y
-  subtract y = (`Prelude.(-)` y)
-</programlisting>
-        The new form of qualified operators is intended to regularise
-        the syntax by eliminating odd cases
-        like <literal>Prelude..</literal>.  For example,
-        when <literal>NewQualifiedOperators</literal> is on, it is possible to
-        write the enumerated sequence <literal>[Monday..]</literal>
-        without spaces, whereas without NewQualifiedOperators this would be a
-        reference to the operator &lsquo;<literal>.</literal>&lsquo;
-        from module <literal>Monday</literal>.</para>
-
-      <para>When <option>-XNewQualifiedOperators</option> is on, the old
-        syntax for qualified operators is not accepted, so this
-        option may cause existing code to break.</para>
-
-    </sect2>
-        
-
     <!-- ====================== HIERARCHICAL MODULES =======================  -->
 
 
@@ -1238,6 +1201,168 @@ output = [ x
 </para>
   </sect2>
 
+   <!-- ===================== MONAD COMPREHENSIONS ===================== -->
+
+<sect2 id="monad-comprehensions">
+    <title>Monad comprehensions</title>
+    <indexterm><primary>monad comprehensions</primary></indexterm>
+
+    <para>
+        Monad comprehesions generalise the list comprehension notation to work
+        for any monad.
+    </para>
+
+    <para>Monad comprehensions support:</para>
+
+    <itemizedlist>
+        <listitem>
+            <para>
+                Bindings:
+            </para>
+
+<programlisting>
+[ x + y | x &lt;- Just 1, y &lt;- Just 2 ]
+</programlisting>
+
+            <para>
+                Bindings are translated with the <literal>(&gt;&gt;=)</literal> and
+                <literal>return</literal> functions to the usual do-notation:
+            </para>
+
+<programlisting>
+do x &lt;- Just 1
+   y &lt;- Just 2
+   return (x+y)
+</programlisting>
+
+        </listitem>
+        <listitem>
+            <para>
+                Guards:
+            </para>
+
+<programlisting>
+[ x | x &lt;- [1..10], x &lt;= 5 ]
+</programlisting>
+
+            <para>
+                Guards are translated with the <literal>guard</literal> function,
+                which requires a <literal>MonadPlus</literal> instance:
+            </para>
+
+<programlisting>
+do x &lt;- [1..10]
+   guard (x &lt;= 5)
+   return x
+</programlisting>
+
+        </listitem>
+        <listitem>
+            <para>
+                Transform statements (as with <literal>-XTransformListComp</literal>):
+            </para>
+
+<programlisting>
+[ x+y | x &lt;- [1..10], y &lt;- [1..x], then take 2 ]
+</programlisting>
+
+            <para>
+                This translates to:
+            </para>
+
+<programlisting>
+do (x,y) &lt;- take 2 (do x &lt;- [1..10]
+                       y &lt;- [1..x]
+                       return (x,y))
+   return (x+y)
+</programlisting>
+
+        </listitem>
+        <listitem>
+            <para>
+                Group statements (as with <literal>-XTransformListComp</literal>):
+            </para>
+
+<programlisting>
+[ x | x &lt;- [1,1,2,2,3], then group by x ]
+[ x | x &lt;- [1,1,2,2,3], then group by x using GHC.Exts.groupWith ]
+[ x | x &lt;- [1,1,2,2,3], then group using myGroup ]
+</programlisting>
+
+            <para>
+                The basic <literal>then group by e</literal> statement is
+                translated using the <literal>mgroupWith</literal> function, which
+                requires a <literal>MonadGroup</literal> instance, defined in
+                <ulink url="&libraryBaseLocation;/Control-Monad-Group.html"><literal>Control.Monad.Group</literal></ulink>:
+            </para>
+
+<programlisting>
+do x &lt;- mgroupWith (do x &lt;- [1,1,2,2,3]
+                       return x)
+   return x
+</programlisting>
+
+            <para>
+                Note that the type of <literal>x</literal> is changed by the
+                grouping statement.
+            </para>
+
+            <para>
+                The grouping function can also be defined with the
+                <literal>using</literal> keyword.
+            </para>
+
+        </listitem>
+        <listitem>
+            <para>
+                Parallel statements (as with <literal>-XParallelListComp</literal>):
+            </para>
+
+<programlisting>
+[ (x+y) | x &lt;- [1..10]
+        | y &lt;- [11..20]
+        ]
+</programlisting>
+
+            <para>
+                Parallel statements are translated using the
+                <literal>mzip</literal> function, which requires a
+                <literal>MonadZip</literal> instance defined in
+                <ulink url="&libraryBaseLocation;/Control-Monad-Zip.html"><literal>Control.Monad.Zip</literal></ulink>:
+            </para>
+
+<programlisting>
+do (x,y) &lt;- mzip (do x &lt;- [1..10]
+                     return x)
+                 (do y &lt;- [11..20]
+                     return y)
+   return (x+y)
+</programlisting>
+
+        </listitem>
+    </itemizedlist>
+
+    <para>
+        All these features are enabled by default if the
+        <literal>MonadComprehensions</literal> extension is enabled. The types
+        and more detailed examples on how to use comprehensions are explained
+        in the previous chapters <xref
+            linkend="generalised-list-comprehensions"/> and <xref
+            linkend="parallel-list-comprehensions"/>. In general you just have
+        to replace the type <literal>[a]</literal> with the type
+        <literal>Monad m => m a</literal> for monad comprehensions.
+    </para>
+
+    <para>
+        Note: Even though most of these examples are using the list monad,
+        monad comprehensions work for any monad.
+        The <literal>base</literal> package offers all necessary instances for
+        lists, which make <literal>MonadComprehensions</literal> backward
+        compatible to built-in, transform and parallel list comprehensions.
+    </para>
+
+</sect2>
+
    <!-- ===================== REBINDABLE SYNTAX ===================  -->
 
 <sect2 id="rebindable-syntax">
@@ -2471,7 +2596,8 @@ declarations.  Define your own instances!
 <sect2 id="gadt-style">
 <title>Declaring data types with explicit constructor signatures</title>
 
-<para>GHC allows you to declare an algebraic data type by 
+<para>When the <literal>GADTSyntax</literal> extension is enabled,
+GHC allows you to declare an algebraic data type by
 giving the type signatures of constructors explicitly.  For example:
 <programlisting>
   data Maybe a where
@@ -4041,18 +4167,21 @@ The willingness to be overlapped or incoherent is a property of
 the <emphasis>instance declaration</emphasis> itself, controlled by the
 presence or otherwise of the <option>-XOverlappingInstances</option> 
 and <option>-XIncoherentInstances</option> flags when that module is
-being defined.  Neither flag is required in a module that imports and uses the
-instance declaration.  Specifically, during the lookup process:
+being defined.  Specifically, during the lookup process:
 <itemizedlist>
 <listitem><para>
-An instance declaration is ignored during the lookup process if (a) a more specific
-match is found, and (b) the instance declaration was compiled with 
-<option>-XOverlappingInstances</option>.  The flag setting for the
-more-specific instance does not matter.
+If the constraint being looked up matches two instance declarations IA and IB,
+and
+<itemizedlist>
+<listitem><para>IB is a substitution instance of IA (but not vice versa);
+that is, IB is strictly more specific than IA</para></listitem>
+<listitem><para>either IA or IB was compiled with <option>-XOverlappingInstances</option></para></listitem>
+</itemizedlist>
+then the less-specific instance IA is ignored.
 </para></listitem>
 <listitem><para>
 Suppose an instance declaration does not match the constraint being looked up, but
-does unify with it, so that it might match when the constraint is further 
+does <emphasis>unify</emphasis> with it, so that it might match when the constraint is further
 instantiated.  Usually GHC will regard this as a reason for not committing to
 some other constraint.  But if the instance declaration was compiled with
 <option>-XIncoherentInstances</option>, GHC will skip the "does-it-unify?" 
@@ -4062,18 +4191,6 @@ check for that declaration.
 These rules make it possible for a library author to design a library that relies on 
 overlapping instances without the library client having to know.  
 </para>
-<para>
-If an instance declaration is compiled without
-<option>-XOverlappingInstances</option>,
-then that instance can never be overlapped.  This could perhaps be
-inconvenient.  Perhaps the rule should instead say that the
-<emphasis>overlapping</emphasis> instance declaration should be compiled in
-this way, rather than the <emphasis>overlapped</emphasis> one.  Perhaps overlap
-at a usage site should be permitted regardless of how the instance declarations
-are compiled, if the <option>-XOverlappingInstances</option> flag is
-used at the usage site.  (Mind you, the exact usage site can occasionally be
-hard to pin down.)  We are interested to receive feedback on these points.
-</para>
 <para>The <option>-XIncoherentInstances</option> flag implies the
 <option>-XOverlappingInstances</option> flag, but not vice versa.
 </para>
@@ -5771,9 +5888,6 @@ for rank-2 types.
 <sect2 id="impredicative-polymorphism">
 <title>Impredicative polymorphism
 </title>
-<para><emphasis>NOTE: the impredicative-polymorphism feature is deprecated in GHC 6.12, and
-will be removed or replaced in GHC 6.14.</emphasis></para>
-
 <para>GHC supports <emphasis>impredicative polymorphism</emphasis>, 
 enabled with <option>-XImpredicativeTypes</option>.  
 This means
@@ -5896,7 +6010,7 @@ signature is explicit.  For example:
   g (x:xs) = xs ++ [ x :: a ]
 </programlisting>
 This program will be rejected, because "<literal>a</literal>" does not scope
-over the definition of "<literal>f</literal>", so "<literal>x::a</literal>"
+over the definition of "<literal>g</literal>", so "<literal>x::a</literal>"
 means "<literal>x::forall a. a</literal>" by Haskell's usual implicit
 quantification rules.
 </para></listitem>
@@ -5932,7 +6046,7 @@ type variables, in the annotated expression.  For example:
 <programlisting>
   f = runST ( (op >>= \(x :: STRef s Int) -> g x) :: forall s. ST s Bool )
 </programlisting>
-Here, the type signature <literal>forall a. ST s Bool</literal> brings the 
+Here, the type signature <literal>forall s. ST s Bool</literal> brings the 
 type variable <literal>s</literal> into scope, in the annotated expression 
 <literal>(op >>= \(x :: STRef s Int) -> g x)</literal>.
 </para>
@@ -8965,7 +9079,7 @@ An example will give the idea:
 </para>
 
 <programlisting>
-  import Generics
+  import Data.Generics
 
   class Bin a where
     toBin   :: a -> [Int]
@@ -8985,7 +9099,7 @@ An example will give the idea:
 <para>
 This class declaration explains how <literal>toBin</literal> and <literal>fromBin</literal>
 work for arbitrary data types.  They do so by giving cases for unit, product, and sum,
-which are defined thus in the library module <literal>Generics</literal>:
+which are defined thus in the library module <literal>Data.Generics</literal>:
 </para>
 <programlisting>
   data Unit    = Unit
@@ -9007,14 +9121,16 @@ where clause and over-ride whichever methods you please.
       <para>To use generics you need to</para>
       <itemizedlist>
        <listitem>
-         <para>Use the flags <option>-fglasgow-exts</option> (to enable the extra syntax), 
-                <option>-XGenerics</option> (to generate extra per-data-type code),
-                and <option>-package lang</option> (to make the <literal>Generics</literal> library
-                available.  </para>
+         <para>
+            Use the flags <option>-XGenerics</option> (to enable the
+            extra syntax and generate extra per-data-type code),
+            and <option>-package syb</option> (to make the
+            <literal>Data.Generics</literal> module available.
+          </para>
        </listitem>
        <listitem>
-         <para>Import the module <literal>Generics</literal> from the
-          <literal>lang</literal> package.  This import brings into
+         <para>Import the module <literal>Data.Generics</literal> from the
+          <literal>syb</literal> package.  This import brings into
           scope the data types <literal>Unit</literal>,
           <literal>:*:</literal>, and <literal>:+:</literal>.  (You
           don't need this import if you don't mention these types