Preliminary monad-comprehension patch (Trac #4370)
[ghc-hetmet.git] / docs / users_guide / glasgow_exts.xml
index cacf97d..54a4833 100644 (file)
@@ -3,8 +3,9 @@
 <indexterm><primary>language, GHC</primary></indexterm>
 <indexterm><primary>extensions, GHC</primary></indexterm>
 As with all known Haskell systems, GHC implements some extensions to
-the language.  They are all enabled by options; by default GHC
-understands only plain Haskell 98.
+the language.  They can all be enabled or disabled by commandline flags
+or language pragmas. By default GHC understands the most recent Haskell
+version it supports, plus a handful of extensions.
 </para>
 
 <para>
@@ -39,8 +40,7 @@ documentation</ulink> describes all the libraries that come with GHC.
     </indexterm>
 
     <para>The language option flags control what variation of the language are
-    permitted.  Leaving out all of them gives you standard Haskell
-    98.</para>
+    permitted.</para>
 
     <para>Language options can be controlled in two ways:
     <itemizedlist>
@@ -56,38 +56,7 @@ documentation</ulink> describes all the libraries that come with GHC.
     <para>The flag <option>-fglasgow-exts</option>
           <indexterm><primary><option>-fglasgow-exts</option></primary></indexterm>
          is equivalent to enabling the following extensions: 
-          <option>-XPrintExplicitForalls</option>,
-          <option>-XForeignFunctionInterface</option>,
-          <option>-XUnliftedFFITypes</option>,
-          <option>-XGADTs</option>,
-          <option>-XImplicitParams</option>,
-          <option>-XScopedTypeVariables</option>,
-          <option>-XUnboxedTuples</option>,
-          <option>-XTypeSynonymInstances</option>,
-          <option>-XStandaloneDeriving</option>,
-          <option>-XDeriveDataTypeable</option>,
-          <option>-XFlexibleContexts</option>,
-          <option>-XFlexibleInstances</option>,
-          <option>-XConstrainedClassMethods</option>,
-          <option>-XMultiParamTypeClasses</option>,
-          <option>-XFunctionalDependencies</option>,
-          <option>-XMagicHash</option>,
-          <option>-XPolymorphicComponents</option>,
-          <option>-XExistentialQuantification</option>,
-          <option>-XUnicodeSyntax</option>,
-          <option>-XPostfixOperators</option>,
-          <option>-XPatternGuards</option>,
-          <option>-XLiberalTypeSynonyms</option>,
-          <option>-XExplicitForAll</option>,
-          <option>-XRankNTypes</option>,
-          <option>-XImpredicativeTypes</option>,
-          <option>-XTypeOperators</option>,
-          <option>-XDoRec</option>,
-          <option>-XParallelListComp</option>,
-          <option>-XEmptyDataDecls</option>,
-          <option>-XKindSignatures</option>,
-          <option>-XGeneralizedNewtypeDeriving</option>,
-          <option>-XTypeFamilies</option>.
+          &what_glasgow_exts_does;
            Enabling these options is the <emphasis>only</emphasis> 
            effect of <option>-fglasgow-exts</option>.
           We are trying to move away from this portmanteau flag, 
@@ -110,7 +79,7 @@ While you really can use this stuff to write fast code,
 
 <para>All these primitive data types and operations are exported by the 
 library <literal>GHC.Prim</literal>, for which there is 
-<ulink url="../libraries/ghc-prim/GHC-Prim.html">detailed online documentation</ulink>.
+<ulink url="&libraryGhcPrimLocation;/GHC-Prim.html">detailed online documentation</ulink>.
 (This documentation is generated from the file <filename>compiler/prelude/primops.txt.pp</filename>.)
 </para>
 <para>
@@ -400,14 +369,6 @@ Indeed, the bindings can even be recursive.
               <entry>LEFTWARDS ARROW</entry>
            </row>
           </tbody>
-         <tbody>
-           <row>
-             <entry>..</entry>
-             <entry>&hellip;</entry>
-             <entry>0x22EF</entry>
-             <entry>MIDLINE HORIZONTAL ELLIPSIS</entry>
-           </row>
-          </tbody>
 
          <tbody>
            <row>
@@ -478,10 +439,10 @@ Indeed, the bindings can even be recursive.
          <listitem><para> <literal>'x'&num;</literal> has type <literal>Char&num;</literal></para> </listitem>
          <listitem><para> <literal>&quot;foo&quot;&num;</literal> has type <literal>Addr&num;</literal></para> </listitem>
          <listitem><para> <literal>3&num;</literal> has type <literal>Int&num;</literal>. In general,
-         any Haskell 98 integer lexeme followed by a <literal>&num;</literal> is an <literal>Int&num;</literal> literal, e.g.
+         any Haskell integer lexeme followed by a <literal>&num;</literal> is an <literal>Int&num;</literal> literal, e.g.
             <literal>-0x3A&num;</literal> as well as <literal>32&num;</literal></para>.</listitem>
          <listitem><para> <literal>3&num;&num;</literal> has type <literal>Word&num;</literal>. In general,
-         any non-negative Haskell 98 integer lexeme followed by <literal>&num;&num;</literal> 
+         any non-negative Haskell integer lexeme followed by <literal>&num;&num;</literal>
              is a <literal>Word&num;</literal>. </para> </listitem>
          <listitem><para> <literal>3.2&num;</literal> has type <literal>Float&num;</literal>.</para> </listitem>
          <listitem><para> <literal>3.2&num;&num;</literal> has type <literal>Double&num;</literal></para> </listitem>
@@ -489,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>
-        (in Haskell 98 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>
-        (in Haskell 98 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 in Haskell 98 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 Haskell
-        98 syntax for qualified operators is not accepted, so this
-        option may cause existing Haskell 98 code to break.</para>
-
-    </sect2>
-        
-
     <!-- ====================== HIERARCHICAL MODULES =======================  -->
 
 
@@ -826,10 +750,8 @@ let {(x -> y) = e1 ;
      (y -> x) = e2 } in x
 </programlisting>
 
-(We may lift this
-restriction in the future; the only cost is that type checking patterns
-would get a little more complicated.)  
-
+(For some amplification on this design choice see 
+<ulink url="http://hackage.haskell.org/trac/ghc/ticket/4061">Trac #4061</ulink>.)
 
 </para>
 </listitem>
@@ -911,7 +833,7 @@ it, you can use the <option>-XNoNPlusKPatterns</option> flag.
 
     <!-- ===================== Recursive do-notation ===================  -->
 
-<sect2 id="mdo-notation">
+<sect2 id="recursive-do-notation">
 <title>The recursive do-notation
 </title>
 
@@ -932,7 +854,7 @@ justOnes = do { rec { xs &lt;- Just (1:xs) }
 As you can guess <literal>justOnes</literal> will evaluate to <literal>Just [-1,-1,-1,...</literal>.
 </para>
 <para>
-The background and motivation for recusrive do-notation is described in
+The background and motivation for recursive do-notation is described in
 <ulink url="http://sites.google.com/site/leventerkok/">A recursive do for Haskell</ulink>,
 by Levent Erkok, John Launchbury,
 Haskell Workshop 2002, pages: 29-37. Pittsburgh, Pennsylvania. 
@@ -1047,7 +969,7 @@ It supports rebindable syntax (see <xref linkend="rebindable-syntax"/>).
 </para>
 </sect3>
 
-<sect3> <title> Mdo-notation (deprecated) </title>
+<sect3 id="mdo-notation"> <title> Mdo-notation (deprecated) </title>
 
 <para> GHC used to support the flag <option>-XRecursiveDo</option>,
 which enabled the keyword <literal>mdo</literal>, precisely as described in
@@ -1279,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">
@@ -1299,8 +1383,8 @@ output = [ x
             hierarchy.  It completely defeats that purpose if the
             literal "1" means "<literal>Prelude.fromInteger
             1</literal>", which is what the Haskell Report specifies.
-            So the <option>-XNoImplicitPrelude</option> 
-             flag <emphasis>also</emphasis> causes
+            So the <option>-XRebindableSyntax</option> 
+             flag causes
             the following pieces of built-in syntax to refer to
             <emphasis>whatever is in scope</emphasis>, not the Prelude
             versions:
@@ -1332,6 +1416,11 @@ output = [ x
              </para></listitem>
 
              <listitem>
+               <para>Conditionals (e.g. "<literal>if</literal> e1 <literal>then</literal> e2 <literal>else</literal> e3")
+               means "<literal>ifThenElse</literal> e1 e2 e3".  However <literal>case</literal> expressions are unaffected.
+             </para></listitem>
+
+             <listitem>
          <para>"Do" notation is translated using whatever
              functions <literal>(>>=)</literal>,
              <literal>(>>)</literal>, and <literal>fail</literal>,
@@ -1351,6 +1440,9 @@ output = [ x
                to use this, ask!
              </para></listitem>
            </itemizedlist>
+<option>-XRebindableSyntax</option> implies <option>-XNoImplicitPrelude</option>.
+</para>
+<para>
 In all cases (apart from arrow notation), the static semantics should be that of the desugared form,
 even if that is a little unexpected. For example, the 
 static semantics of the literal <literal>368</literal>
@@ -1697,7 +1789,7 @@ and the fixity declaration applies wherever the binding is in scope.
 For example, in a <literal>let</literal>, it applies in the right-hand
 sides of other <literal>let</literal>-bindings and the body of the
 <literal>let</literal>C. Or, in recursive <literal>do</literal>
-expressions (<xref linkend="mdo-notation"/>), the local fixity
+expressions (<xref linkend="recursive-do-notation"/>), the local fixity
 declarations of a <literal>let</literal> statement scope over other
 statements in the group, just as the bound name does.
 </para>
@@ -1904,6 +1996,26 @@ not <literal>*</literal> then an explicit kind annotation must be used
 Nevertheless, they can be useful when defining "phantom types".</para>
 </sect2>
 
+<sect2 id="datatype-contexts">
+<title>Data type contexts</title>
+
+<para>Haskell allows datatypes to be given contexts, e.g.</para>
+
+<programlisting>
+data Eq a => Set a = NilSet | ConsSet a (Set a)
+</programlisting>
+
+<para>give constructors with types:</para>
+
+<programlisting>
+NilSet :: Set a
+ConsSet :: Eq a => a -> Set a -> Set a
+</programlisting>
+
+<para>In GHC this feature is an extension called
+<literal>DatatypeContexts</literal>, and on by default.</para>
+</sect2>
+
 <sect2 id="infix-tycons">
 <title>Infix type constructors, classes, and type variables</title>
 
@@ -2484,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
@@ -4054,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?" 
@@ -4075,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>
@@ -5784,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
@@ -5909,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>
@@ -5945,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>
@@ -7213,16 +7314,23 @@ forces evaluation anyway does nothing.
 There is one (apparent) exception to this general rule that a bang only
 makes a difference when it precedes a variable or wild-card: a bang at the
 top level of a <literal>let</literal> or <literal>where</literal>
-binding makes the binding strict, regardless of the pattern. For example:
+binding makes the binding strict, regardless of the pattern.
+(We say "apparent" exception because the Right Way to think of it is that the bang
+at the top of a binding is not part of the <emphasis>pattern</emphasis>; rather it
+is part of the syntax of the <emphasis>binding</emphasis>,
+creating a "bang-pattern binding".)
+For example:
 <programlisting>
 let ![x,y] = e in b
 </programlisting>
-is a strict binding: operationally, it evaluates <literal>e</literal>, matches
-it against the pattern <literal>[x,y]</literal>, and then evaluates <literal>b</literal>.
-(We say "apparent" exception because the Right Way to think of it is that the bang
-at the top of a binding is not part of the <emphasis>pattern</emphasis>; rather it
-is part of the syntax of the <emphasis>binding</emphasis>.)
-Nested bangs in a pattern binding behave uniformly with all other forms of 
+is a bang-pattern binding. Operationally, it behaves just like a case expression:
+<programlisting>
+case e of [x,y] -> b
+</programlisting>
+Like a case expression, a bang-pattern binding must be non-recursive, and
+is monomorphic.
+
+However, <emphasis>nested</emphasis> bangs in a pattern binding behave uniformly with all other forms of
 pattern matching.  For example
 <programlisting>
 let (!x,[y]) = e in b
@@ -7485,11 +7593,11 @@ Assertion failures can be caught, see the documentation for the
       </para>
 
       <para>A list of all supported language extensions can be obtained by invoking
-       <literal>ghc --supported-languages</literal> (see <xref linkend="modes"/>).</para>
+       <literal>ghc --supported-extensions</literal> (see <xref linkend="modes"/>).</para>
 
       <para>Any extension from the <literal>Extension</literal> type defined in
        <ulink
-         url="../libraries/Cabal/Language-Haskell-Extension.html"><literal>Language.Haskell.Extension</literal></ulink>
+         url="&libraryCabalLocation;/Language-Haskell-Extension.html"><literal>Language.Haskell.Extension</literal></ulink>
        may be used.  GHC will report an error if any of the requested extensions are not supported.</para>
     </sect2>
 
@@ -7615,21 +7723,68 @@ key_function :: Int -> String -> (Bool, Double)
        function "<literal>f</literal>" has a number of other effects:
 <itemizedlist>
 <listitem><para>
-No functions are inlined into <literal>f</literal>.  Otherwise
-GHC might inline a big function into <literal>f</literal>'s right hand side, 
-making <literal>f</literal> big; and then inline <literal>f</literal> blindly.
+While GHC is keen to inline the function, it does not do so
+blindly.  For example, if you write
+<programlisting>
+map key_function xs
+</programlisting>
+there really isn't any point in inlining <literal>key_function</literal> to get
+<programlisting>
+map (\x -> <replaceable>body</replaceable>) xs
+</programlisting>
+In general, GHC only inlines the function if there is some reason (no matter
+how slight) to supose that it is useful to do so.
 </para></listitem>
+
 <listitem><para>
-The float-in, float-out, and common-sub-expression transformations are not 
-applied to the body of <literal>f</literal>.  
+Moreover, GHC will only inline the function if it is <emphasis>fully applied</emphasis>, 
+where "fully applied"
+means applied to as many arguments as appear (syntactically) 
+on the LHS of the function
+definition.  For example:
+<programlisting>
+comp1 :: (b -> c) -> (a -> b) -> a -> c
+{-# INLINE comp1 #-}
+comp1 f g = \x -> f (g x)
+
+comp2 :: (b -> c) -> (a -> b) -> a -> c
+{-# INLINE comp2 #-}
+comp2 f g x = f (g x)
+</programlisting>
+The two functions <literal>comp1</literal> and <literal>comp2</literal> have the 
+same semantics, but <literal>comp1</literal> will be inlined when applied
+to <emphasis>two</emphasis> arguments, while <literal>comp2</literal> requires
+<emphasis>three</emphasis>.  This might make a big difference if you say
+<programlisting>
+map (not `comp1` not) xs
+</programlisting>
+which will optimise better than the corresponding use of `comp2`.
+</para></listitem>
+
+<listitem><para> 
+It is useful for GHC to optimise the definition of an
+INLINE function <literal>f</literal> just like any other non-INLINE function, 
+in case the non-inlined version of <literal>f</literal> is
+ultimately called.  But we don't want to inline 
+the <emphasis>optimised</emphasis> version
+of <literal>f</literal>;
+a major reason for INLINE pragmas is to expose functions 
+in <literal>f</literal>'s RHS that have
+rewrite rules, and it's no good if those functions have been optimised
+away.
+</para>
+<para>
+So <emphasis>GHC guarantees to inline precisely the code that you wrote</emphasis>, no more
+and no less.  It does this by capturing a copy of the definition of the function to use
+for inlining (we call this the "inline-RHS"), which it leaves untouched,
+while optimising the ordinarly RHS as usual.  For externally-visible functions
+the inline-RHS (not the optimised RHS) is recorded in the interface file.
 </para></listitem>
 <listitem><para>
 An INLINE function is not worker/wrappered by strictness analysis.
 It's going to be inlined wholesale instead.
 </para></listitem>
 </itemizedlist>
-All of these effects are aimed at ensuring that what gets inlined is
-exactly what you asked for, no more and no less.
 </para>
 <para>GHC ensures that inlining cannot go on forever: every mutually-recursive
 group is cut by one or more <emphasis>loop breakers</emphasis> that is never inlined
@@ -7657,8 +7812,9 @@ itself, so an INLINE pragma is always ignored.</para>
 {-# INLINE returnUs #-}
 </programlisting>
 
-       <para>See also the <literal>NOINLINE</literal> pragma (<xref
-        linkend="noinline-pragma"/>).</para>
+       <para>See also the <literal>NOINLINE</literal> (<xref linkend="inlinable-pragma"/>) 
+        and <literal>INLINABLE</literal> (<xref linkend="noinline-pragma"/>) 
+        pragmas.</para>
 
        <para>Note: the HBC compiler doesn't like <literal>INLINE</literal> pragmas,
          so if you want your code to be HBC-compatible you'll have to surround
@@ -7667,6 +7823,57 @@ itself, so an INLINE pragma is always ignored.</para>
 
       </sect3>
 
+      <sect3 id="inlinable-pragma">
+       <title>INLINABLE pragma</title>
+
+<para>An <literal>{-# INLINABLE f #-}</literal> pragma on a
+function <literal>f</literal> has the following behaviour:
+<itemizedlist>
+<listitem><para>
+While <literal>INLINE</literal> says "please inline me", the <literal>INLINABLE</literal>
+says "feel free to inline me; use your
+discretion".  In other words the choice is left to GHC, which uses the same
+rules as for pragma-free functions.  Unlike <literal>INLINE</literal>, that decision is made at
+the <emphasis>call site</emphasis>, and
+will therefore be affected by the inlining threshold, optimisation level etc.
+</para></listitem>
+<listitem><para>
+Like <literal>INLINE</literal>, the <literal>INLINABLE</literal> pragma retains a
+copy of the original RHS for
+inlining purposes, and persists it in the interface file, regardless of
+the size of the RHS.
+</para></listitem>
+
+<listitem><para>
+One way to use <literal>INLINABLE</literal> is in conjunction with
+the special function <literal>inline</literal> (<xref linkend="special-ids"/>).
+The call <literal>inline f</literal> tries very hard to inline <literal>f</literal>.
+To make sure that <literal>f</literal> can be inlined,
+it is a good idea to mark the definition
+of <literal>f</literal> as <literal>INLINABLE</literal>,
+so that GHC guarantees to expose an unfolding regardless of how big it is.
+Moreover, by annotating <literal>f</literal> as <literal>INLINABLE</literal>,
+you ensure that <literal>f</literal>'s original RHS is inlined, rather than
+whatever random optimised version of <literal>f</literal> GHC's optimiser
+has produced.
+</para></listitem>
+
+<listitem><para>
+The <literal>INLINABLE</literal> pragma also works with <literal>SPECIALISE</literal>:
+if you mark function <literal>f</literal> as <literal>INLINABLE</literal>, then
+you can subsequently <literal>SPECIALISE</literal> in another module
+(see <xref linkend="specialize-pragma"/>).</para></listitem>
+
+<listitem><para>
+Unlike <literal>INLINE</literal>, it is OK to use
+an <literal>INLINABLE</literal> pragma on a recursive function.
+The principal reason do to so to allow later use of <literal>SPECIALISE</literal>
+</para></listitem>
+</itemizedlist>
+</para>
+
+      </sect3>
+
       <sect3 id="noinline-pragma">
        <title>NOINLINE pragma</title>
        
@@ -7923,6 +8130,9 @@ RULE with a somewhat-complex left-hand side (try it yourself), so it might not f
 well.  If you use this kind of specialisation, let us know how well it works.
 </para>
 
+    <sect3 id="specialize-inline">
+      <title>SPECIALIZE INLINE</title>
+
 <para>A <literal>SPECIALIZE</literal> pragma can optionally be followed with a
 <literal>INLINE</literal> or <literal>NOINLINE</literal> pragma, optionally 
 followed by a phase, as described in <xref linkend="inline-noinline-pragma"/>.
@@ -7951,6 +8161,66 @@ specialisation, whose body is also inlined.  The result is a type-based
 unrolling of the indexing function.</para>
 <para>Warning: you can make GHC diverge by using <literal>SPECIALISE INLINE</literal>
 on an ordinarily-recursive function.</para>
+</sect3>
+
+<sect3><title>SPECIALIZE for imported functions</title>
+
+<para>
+Generally, you can only give a <literal>SPECIALIZE</literal> pragma
+for a function defined in the same module.
+However if a function <literal>f</literal> is given an <literal>INLINABLE</literal>
+pragma at its definition site, then it can subequently be specialised by
+importing modules (see <xref linkend="inlinable-pragma"/>).
+For example
+<programlisting>
+module Map( lookup, blah blah ) where
+  lookup :: Ord key => [(key,a)] -> key -> Maybe a
+  lookup = ...
+  {-# INLINABLE lookup #-}
+
+module Client where
+  import Map( lookup )
+
+  data T = T1 | T2 deriving( Eq, Ord )
+  {-# SPECIALISE lookup :: [(T,a)] -> T -> Maybe a
+</programlisting>
+Here, <literal>lookup</literal> is declared <literal>INLINABLE</literal>, but
+it cannot be specialised for type <literal>T</literal> at its definition site,
+because that type does not exist yet.  Instead a client module can define <literal>T</literal>
+and then specialise <literal>lookup</literal> at that type.
+</para>
+<para>
+Moreover, every module that imports <literal>Client</literal> (or imports a module
+that imports <literal>Client</literal>, transitively) will "see", and make use of,
+the specialised version of <literal>lookup</literal>.  You don't need to put
+a <literal>SPECIALIZE</literal> pragma in every module.
+</para>
+<para>
+Moreover you often don't even need the <literal>SPECIALIZE</literal> pragma in the
+first place. When compiling a module M,
+GHC's optimiser (with -O) automatically considers each top-level
+overloaded function declared in M, and specialises it
+for the different types at which it is called in M.  The optimiser
+<emphasis>also</emphasis> considers each <emphasis>imported</emphasis>
+<literal>INLINABLE</literal> overloaded function, and specialises it
+for the different types at which it is called in M.
+So in our example, it would be enough for <literal>lookup</literal> to
+be called at type <literal>T</literal>:
+<programlisting>
+module Client where
+  import Map( lookup )
+
+  data T = T1 | T2 deriving( Eq, Ord )
+
+  findT1 :: [(T,a)] -> Maybe a
+  findT1 m = lookup m T1   -- A call of lookup at type T
+</programlisting>
+However, sometimes there are no such calls, in which case the
+pragma can be useful.
+</para>
+</sect3>
+
+<sect3><title>Obselete SPECIALIZE syntax</title>
 
       <para>Note: In earlier versions of GHC, it was possible to provide your own
       specialised function for a given type:
@@ -7961,6 +8231,7 @@ on an ordinarily-recursive function.</para>
 
       This feature has been removed, as it is now subsumed by the
       <literal>RULES</literal> pragma (see <xref linkend="rule-spec"/>).</para>
+</sect3>
 
     </sect2>
 
@@ -8045,10 +8316,6 @@ data S = S {-# UNPACK #-} !Int {-# UNPACK #-} !Int
       directly in the <function>T</function> constructor.  The
       unpacker can see through newtypes, too.</para>
 
-      <para>If a field cannot be unpacked, you will not get a warning,
-      so it might be an idea to check the generated code with
-      <option>-ddump-simpl</option>.</para>
-
       <para>See also the <option>-funbox-strict-fields</option> flag,
       which essentially has the effect of adding
       <literal>{-#&nbsp;UNPACK&nbsp;#-}</literal> to every strict
@@ -8089,7 +8356,7 @@ Here is an example:
 <para>
 Use the debug flag <option>-ddump-simpl-stats</option> to see what rules fired.
 If you need more information, then <option>-ddump-rule-firings</option> shows you
-each individual rule firing in detail.
+each individual rule firing and <option>-ddump-rule-rewrites</option> also shows what the code looks like before and after the rewrite.
 </para>
 
 <sect2>
@@ -8674,7 +8941,8 @@ If you add <option>-dppr-debug</option> you get a more detailed listing.
 
 <listitem>
 <para>
- Use <option>-ddump-rule-firings</option> to see in great detail what rules are being fired.
+ Use <option>-ddump-rule-firings</option> or <option>-ddump-rule-rewrites</option>
+to see in great detail what rules are being fired.
 If you add <option>-dppr-debug</option> you get a still more detailed listing.
 </para>
 </listitem>
@@ -8780,8 +9048,24 @@ r) ->
 <title>Special built-in functions</title>
 <para>GHC has a few built-in functions with special behaviour.  These
 are now described in the module <ulink
-url="../libraries/ghc-prim/GHC-Prim.html"><literal>GHC.Prim</literal></ulink>
-in the library documentation.</para>
+url="&libraryGhcPrimLocation;/GHC-Prim.html"><literal>GHC.Prim</literal></ulink>
+in the library documentation.
+In particular:
+<itemizedlist>
+<listitem><para>
+<ulink url="&libraryGhcPrimLocation;/GHC-Prim.html#v%3Ainline"><literal>inline</literal></ulink>
+allows control over inlining on a per-call-site basis.
+</para></listitem>
+<listitem><para>
+<ulink url="&libraryGhcPrimLocation;/GHC-Prim.html#v%3Alazy"><literal>lazy</literal></ulink>
+restrains the strictness analyser.
+</para></listitem>
+<listitem><para>
+<ulink url="&libraryGhcPrimLocation;/GHC-Prim.html#v%3AunsafeCoerce%23"><literal>lazy</literal></ulink> 
+allows you to fool the type checker.
+</para></listitem>
+</itemizedlist>
+</para>
 </sect1>
 
 
@@ -8795,7 +9079,7 @@ An example will give the idea:
 </para>
 
 <programlisting>
-  import Generics
+  import Data.Generics
 
   class Bin a where
     toBin   :: a -> [Int]
@@ -8815,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
@@ -8837,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
@@ -9087,7 +9373,6 @@ standard behaviour.
 
 <!-- Emacs stuff:
      ;;; Local Variables: ***
-     ;;; mode: xml ***
      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
      ;;; ispell-local-dictionary: "british" ***
      ;;; End: ***