Remove NewQualifiedOperators
[ghc-hetmet.git] / docs / users_guide / glasgow_exts.xml
index 093858b..e57b294 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>-XRecursiveDo</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>
@@ -351,6 +320,15 @@ Indeed, the bindings can even be recursive.
              <entry>Name</entry>
            </row>
          </thead>
+
+<!--
+               to find the DocBook entities for these characters, find
+               the Unicode code point (e.g. 0x2237), and grep for it in
+               /usr/share/sgml/docbook/xml-dtd-*/ent/* (or equivalent on
+               your system.  Some of these Unicode code points don't have
+               equivalent DocBook entities.
+            -->
+
          <tbody>
            <row>
              <entry><literal>::</literal></entry>
@@ -391,14 +369,52 @@ Indeed, the bindings can even be recursive.
               <entry>LEFTWARDS ARROW</entry>
            </row>
           </tbody>
+
+         <tbody>
+           <row>
+             <entry>-&lt;</entry>
+             <entry>&larrtl;</entry>
+             <entry>0x2919</entry>
+             <entry>LEFTWARDS ARROW-TAIL</entry>
+           </row>
+          </tbody>
+
+         <tbody>
+           <row>
+             <entry>&gt;-</entry>
+             <entry>&rarrtl;</entry>
+             <entry>0x291A</entry>
+             <entry>RIGHTWARDS ARROW-TAIL</entry>
+           </row>
+          </tbody>
+
+         <tbody>
+           <row>
+             <entry>-&lt;&lt;</entry>
+             <entry></entry>
+             <entry>0x291B</entry>
+             <entry>LEFTWARDS DOUBLE ARROW-TAIL</entry>
+           </row>
+          </tbody>
+
+         <tbody>
+           <row>
+             <entry>&gt;&gt;-</entry>
+             <entry></entry>
+             <entry>0x291C</entry>
+             <entry>RIGHTWARDS DOUBLE ARROW-TAIL</entry>
+           </row>
+          </tbody>
+
          <tbody>
            <row>
-             <entry>..</entry>
-             <entry>&hellip;</entry>
-             <entry>0x22EF</entry>
-             <entry>MIDLINE HORIZONTAL ELLIPSIS</entry>
+             <entry>*</entry>
+             <entry>&starf;</entry>
+             <entry>0x2605</entry>
+             <entry>BLACK STAR</entry>
            </row>
           </tbody>
+
         </tgroup>
       </informaltable>
     </sect2>
@@ -423,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>
@@ -434,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 =======================  -->
 
 
@@ -771,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>
@@ -856,96 +833,157 @@ 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>
 
-<para> The recursive do-notation (also known as mdo-notation) is implemented as described in
-<ulink url="http://citeseer.ist.psu.edu/erk02recursive.html">A recursive do for Haskell</ulink>,
-by Levent Erkok, John Launchbury,
-Haskell Workshop 2002, pages: 29-37. Pittsburgh, Pennsylvania. 
-This paper is essential reading for anyone making non-trivial use of mdo-notation,
-and we do not repeat it here.
-</para>
 <para>
-The do-notation of Haskell does not allow <emphasis>recursive bindings</emphasis>,
+The do-notation of Haskell 98 does not allow <emphasis>recursive bindings</emphasis>,
 that is, the variables bound in a do-expression are visible only in the textually following 
 code block. Compare this to a let-expression, where bound variables are visible in the entire binding
 group. It turns out that several applications can benefit from recursive bindings in
-the do-notation, and this extension provides the necessary syntactic support.
+the do-notation.  The <option>-XDoRec</option> flag provides the necessary syntactic support.
 </para>
 <para>
-Here is a simple (yet contrived) example:
-</para>
+Here is a simple (albeit contrived) example:
 <programlisting>
-import Control.Monad.Fix
-
-justOnes = mdo xs &lt;- Just (1:xs)
-               return xs
+{-# LANGUAGE DoRec #-}
+justOnes = do { rec { xs &lt;- Just (1:xs) }
+              ; return (map negate xs) }
 </programlisting>
+As you can guess <literal>justOnes</literal> will evaluate to <literal>Just [-1,-1,-1,...</literal>.
+</para>
 <para>
-As you can guess <literal>justOnes</literal> will evaluate to <literal>Just [1,1,1,...</literal>.
+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. 
+The theory behind monadic value recursion is explained further in Erkok's thesis
+<ulink url="http://sites.google.com/site/leventerkok/erkok-thesis.pdf">Value Recursion in Monadic Computations</ulink>.
+However, note that GHC uses a different syntax than the one described in these documents.
 </para>
 
+<sect3>
+<title>Details of recursive do-notation</title>
 <para>
-The Control.Monad.Fix library introduces the <literal>MonadFix</literal> class.  Its definition is:
+The recursive do-notation is enabled with the flag <option>-XDoRec</option> or, equivalently,
+the LANGUAGE pragma <option>DoRec</option>.  It introduces the single new keyword "<literal>rec</literal>",
+which wraps a mutually-recursive group of monadic statements,
+producing a single statement.
 </para>
+<para>Similar to a <literal>let</literal>
+statement, the variables bound in the <literal>rec</literal> are 
+visible throughout the <literal>rec</literal> group, and below it.
+For example, compare
 <programlisting>
-class Monad m => MonadFix m where
-   mfix :: (a -> m a) -> m a
+do { a &lt;- getChar              do { a &lt;- getChar                    
+   ; let { r1 = f a r2            ; rec { r1 &lt;- f a r2      
+         ; r2 = g r1 }                  ; r2 &lt;- g r1 }      
+   ; return (r1 ++ r2) }          ; return (r1 ++ r2) }
 </programlisting>
+In both cases, <literal>r1</literal> and <literal>r2</literal> are 
+available both throughout the <literal>let</literal> or <literal>rec</literal> block, and
+in the statements that follow it.  The difference is that <literal>let</literal> is non-monadic,
+while <literal>rec</literal> is monadic.  (In Haskell <literal>let</literal> is 
+really <literal>letrec</literal>, of course.)
+</para>
 <para>
-The function <literal>mfix</literal>
-dictates how the required recursion operation should be performed.  For example, 
-<literal>justOnes</literal> desugars as follows:
+The static and dynamic semantics of <literal>rec</literal> can be described as follows:  
+<itemizedlist>
+<listitem><para>
+First,
+similar to let-bindings, the <literal>rec</literal> is broken into 
+minimal recursive groups, a process known as <emphasis>segmentation</emphasis>.
+For example:
+<programlisting>
+rec { a &lt;- getChar      ===>     a &lt;- getChar
+    ; b &lt;- f a c                 rec { b &lt;- f a c
+    ; c &lt;- f b a                     ; c &lt;- f b a }
+    ; putChar c }                putChar c 
+</programlisting>
+The details of segmentation are described in Section 3.2 of
+<ulink url="http://sites.google.com/site/leventerkok/">A recursive do for Haskell</ulink>.
+Segmentation improves polymorphism, reduces the size of the recursive "knot", and, as the paper 
+describes, also has a semantic effect (unless the monad satisfies the right-shrinking law).
+</para></listitem>
+<listitem><para>
+Then each resulting <literal>rec</literal> is desugared, using a call to <literal>Control.Monad.Fix.mfix</literal>.
+For example, the <literal>rec</literal> group in the preceding example is desugared like this:
 <programlisting>
-justOnes = mfix (\xs' -&gt; do { xs &lt;- Just (1:xs'); return xs }
+rec { b &lt;- f a c     ===>    (b,c) &lt;- mfix (\~(b,c) -> do { b &lt;- f a c
+    ; c &lt;- f b a }                                        ; c &lt;- f b a
+                                                          ; return (b,c) })
 </programlisting>
-For full details of the way in which mdo is typechecked and desugared, see 
-the paper <ulink url="http://citeseer.ist.psu.edu/erk02recursive.html">A recursive do for Haskell</ulink>.
-In particular, GHC implements the segmentation technique described in Section 3.2 of the paper.
+In general, the statment <literal>rec <replaceable>ss</replaceable></literal>
+is desugared to the statement
+<programlisting>
+<replaceable>vs</replaceable> &lt;- mfix (\~<replaceable>vs</replaceable> -&gt; do { <replaceable>ss</replaceable>; return <replaceable>vs</replaceable> })
+</programlisting>
+where <replaceable>vs</replaceable> is a tuple of the variables bound by <replaceable>ss</replaceable>.
+</para><para>
+The original <literal>rec</literal> typechecks exactly 
+when the above desugared version would do so.  For example, this means that 
+the variables <replaceable>vs</replaceable> are all monomorphic in the statements
+following the <literal>rec</literal>, because they are bound by a lambda.
 </para>
 <para>
-If recursive bindings are required for a monad,
-then that monad must be declared an instance of the <literal>MonadFix</literal> class.
-The following instances of <literal>MonadFix</literal> are automatically provided: List, Maybe, IO. 
-Furthermore, the Control.Monad.ST and Control.Monad.ST.Lazy modules provide the instances of the MonadFix class 
-for Haskell's internal state monad (strict and lazy, respectively).
+The <literal>mfix</literal> function is defined in the <literal>MonadFix</literal> 
+class, in <literal>Control.Monad.Fix</literal>, thus:
+<programlisting>
+class Monad m => MonadFix m where
+   mfix :: (a -> m a) -> m a
+</programlisting>
+</para>
+</listitem>
+</itemizedlist>
 </para>
 <para>
-Here are some important points in using the recursive-do notation:
+Here are some other important points in using the recursive-do notation:
 <itemizedlist>
 <listitem><para>
-The recursive version of the do-notation uses the keyword <literal>mdo</literal> (rather
-than <literal>do</literal>).
+It is enabled with the flag <literal>-XDoRec</literal>, which is in turn implied by
+<literal>-fglasgow-exts</literal>.
 </para></listitem>
 
 <listitem><para>
-It is enabled with the flag <literal>-XRecursiveDo</literal>, which is in turn implied by
-<literal>-fglasgow-exts</literal>.
+If recursive bindings are required for a monad,
+then that monad must be declared an instance of the <literal>MonadFix</literal> class.
 </para></listitem>
 
 <listitem><para>
-Unlike ordinary do-notation, but like <literal>let</literal> and <literal>where</literal> bindings,
-name shadowing is not allowed; that is, all the names bound in a single <literal>mdo</literal> must
-be distinct (Section 3.3 of the paper).
+The following instances of <literal>MonadFix</literal> are automatically provided: List, Maybe, IO. 
+Furthermore, the Control.Monad.ST and Control.Monad.ST.Lazy modules provide the instances of the MonadFix class 
+for Haskell's internal state monad (strict and lazy, respectively).
 </para></listitem>
 
 <listitem><para>
-Variables bound by a <literal>let</literal> statement in an <literal>mdo</literal>
-are monomorphic in the <literal>mdo</literal> (Section 3.1 of the paper).  However
-GHC breaks the <literal>mdo</literal> into segments to enhance polymorphism,
-and improve termination (Section 3.2 of the paper).
+Like <literal>let</literal> and <literal>where</literal> bindings,
+name shadowing is not allowed within a <literal>rec</literal>; 
+that is, all the names bound in a single <literal>rec</literal> must
+be distinct (Section 3.3 of the paper).
+</para></listitem>
+<listitem><para>
+It supports rebindable syntax (see <xref linkend="rebindable-syntax"/>).
 </para></listitem>
 </itemizedlist>
 </para>
+</sect3>
 
+<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
+<ulink url="http://sites.google.com/site/leventerkok/">A recursive do for Haskell</ulink>,
+but this is now deprecated.  Instead of <literal>mdo { Q; e }</literal>, write
+<literal>do { rec Q; e }</literal>.
+</para>
 <para>
 Historical note: The old implementation of the mdo-notation (and most
 of the existing documents) used the name
 <literal>MonadRec</literal> for the class and the corresponding library.
 This name is not supported by GHC.
 </para>
+</sect3>
 
 </sect2>
 
@@ -1129,7 +1167,7 @@ then group by e
     <para>This form of grouping is essentially the same as the one described above. However,
     since no function to use for the grouping has been supplied it will fall back on the
     <literal>groupWith</literal> function defined in 
-    <ulink url="../libraries/base/GHC-Exts.html"><literal>GHC.Exts</literal></ulink>. This
+    <ulink url="&libraryBaseLocation;/GHC-Exts.html"><literal>GHC.Exts</literal></ulink>. This
     is the form of the group statement that we made use of in the opening example.</para>
 
     </listitem>
@@ -1183,8 +1221,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:
@@ -1216,6 +1254,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>,
@@ -1235,6 +1278,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>
@@ -1581,7 +1627,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>
@@ -1788,6 +1834,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>
 
@@ -3264,7 +3330,7 @@ There should be more documentation, but there isn't (yet).  Yell if you need it.
 <sect3><title>Rules for functional dependencies </title>
 <para>
 In a class declaration, all of the class type variables must be reachable (in the sense 
-mentioned in <xref linkend="type-restrictions"/>)
+mentioned in <xref linkend="flexible-contexts"/>)
 from the free variables of each method type.
 For example:
 
@@ -3889,6 +3955,51 @@ of the instance declaration, thus:
 (You need <link linkend="instance-rules"><option>-XFlexibleInstances</option></link> to do this.)
 </para>
 <para>
+Warning: overlapping instances must be used with care.  They 
+can give rise to incoherence (ie different instance choices are made
+in different parts of the program) even without <option>-XIncoherentInstances</option>. Consider:
+<programlisting>
+{-# LANGUAGE OverlappingInstances #-}
+module Help where
+
+    class MyShow a where
+      myshow :: a -> String
+
+    instance MyShow a => MyShow [a] where
+      myshow xs = concatMap myshow xs
+
+    showHelp :: MyShow a => [a] -> String
+    showHelp xs = myshow xs
+
+{-# LANGUAGE FlexibleInstances, OverlappingInstances #-}
+module Main where
+    import Help
+
+    data T = MkT
+
+    instance MyShow T where
+      myshow x = "Used generic instance"
+
+    instance MyShow [T] where
+      myshow xs = "Used more specific instance"
+
+    main = do { print (myshow [MkT]); print (showHelp [MkT]) }
+</programlisting>
+In function <literal>showHelp</literal> GHC sees no overlapping
+instances, and so uses the <literal>MyShow [a]</literal> instance
+without complaint.  In the call to <literal>myshow</literal> in <literal>main</literal>,
+GHC resolves the <literal>MyShow [T]</literal> constraint using the overlapping
+instance declaration in module <literal>Main</literal>. As a result, 
+the program prints
+<programlisting>
+  "Used more specific instance"
+  "Used generic instance"
+</programlisting>
+(An alternative possible behaviour, not currently implemented, 
+would be to reject module <literal>Help</literal>
+on the grounds that a later instance declaration might overlap the local one.)
+</para>
+<para>
 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> 
@@ -5623,6 +5734,9 @@ 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
@@ -6085,25 +6199,29 @@ Wiki page</ulink>.
                    <listitem><para> a list of top-level declarations; the spliced expression 
                     must have type <literal>Q [Dec]</literal></para></listitem>
                    </itemizedlist>
+            Note that pattern splices are not supported.
            Inside a splice you can can only call functions defined in imported modules,
-       not functions defined elsewhere in the same module.</para></listitem>
+           not functions defined elsewhere in the same module.</para></listitem>
 
              <listitem><para>
                  A expression quotation is written in Oxford brackets, thus:
                  <itemizedlist>
-                   <listitem><para> <literal>[| ... |]</literal>, where the "..." is an expression; 
+                   <listitem><para> <literal>[| ... |]</literal>, or <literal>[e| ... |]</literal>, 
+                             where the "..." is an expression; 
                              the quotation has type <literal>Q Exp</literal>.</para></listitem>
                    <listitem><para> <literal>[d| ... |]</literal>, where the "..." is a list of top-level declarations;
                              the quotation has type <literal>Q [Dec]</literal>.</para></listitem>
                    <listitem><para> <literal>[t| ... |]</literal>, where the "..." is a type;
-                             the quotation has type <literal>Q Typ</literal>.</para></listitem>
+                             the quotation has type <literal>Q Type</literal>.</para></listitem>
+                   <listitem><para> <literal>[p| ... |]</literal>, where the "..." is a pattern;
+                             the quotation has type <literal>Q Pat</literal>.</para></listitem>
                  </itemizedlist></para></listitem>
 
              <listitem><para>
                  A quasi-quotation can appear in either a pattern context or an
                  expression context and is also written in Oxford brackets:
                  <itemizedlist>
-                   <listitem><para> <literal>[$<replaceable>varid</replaceable>| ... |]</literal>,
+                   <listitem><para> <literal>[<replaceable>varid</replaceable>| ... |]</literal>,
                         where the "..." is an arbitrary string; a full description of the
                        quasi-quotation facility is given in <xref linkend="th-quasiquotation"/>.</para></listitem>
                  </itemizedlist></para></listitem>
@@ -6309,19 +6427,67 @@ several examples are documented in
 Nice to be Quoted: Quasiquoting for Haskell</ulink>" (Proc Haskell Workshop
 2007). The example below shows how to write a quasiquoter for a simple
 expression language.</para>
-
 <para>
-In the example, the quasiquoter <literal>expr</literal> is bound to a value of
-type <literal>Language.Haskell.TH.Quote.QuasiQuoter</literal> which contains two
-functions for quoting expressions and patterns, respectively. The first argument
-to each quoter is the (arbitrary) string enclosed in the Oxford brackets. The
-context of the quasi-quotation statement determines which of the two parsers is
-called: if the quasi-quotation occurs in an expression context, the expression
-parser is called, and if it occurs in a pattern context, the pattern parser is
-called.</para>
+Here are the salient features
+<itemizedlist>
+<listitem><para>
+A quasi-quote has the form
+<literal>[<replaceable>quoter</replaceable>| <replaceable>string</replaceable> |]</literal>.
+<itemizedlist>
+<listitem><para>
+The <replaceable>quoter</replaceable> must be the (unqualified) name of an imported 
+quoter; it cannot be an arbitrary expression.  
+</para></listitem>
+<listitem><para>
+The <replaceable>quoter</replaceable> cannot be "<literal>e</literal>", 
+"<literal>t</literal>", "<literal>d</literal>", or "<literal>p</literal>", since
+those overlap with Template Haskell quotations.
+</para></listitem>
+<listitem><para>
+There must be no spaces in the token
+<literal>[<replaceable>quoter</replaceable>|</literal>.
+</para></listitem>
+<listitem><para>
+The quoted <replaceable>string</replaceable> 
+can be arbitrary, and may contain newlines.
+</para></listitem>
+</itemizedlist>
+</para></listitem>
+
+<listitem><para>
+A quasiquote may appear in place of
+<itemizedlist>
+<listitem><para>An expression</para></listitem>
+<listitem><para>A pattern</para></listitem>
+<listitem><para>A type</para></listitem>
+<listitem><para>A top-level declaration</para></listitem>
+</itemizedlist>
+(Only the first two are described in the paper.)
+</para></listitem>
 
+<listitem><para>
+A quoter is a value of type <literal>Language.Haskell.TH.Quote.QuasiQuoter</literal>, 
+which is defined thus:
+<programlisting>
+data QuasiQuoter = QuasiQuoter { quoteExp  :: String -> Q Exp,
+                                 quotePat  :: String -> Q Pat,
+                                 quoteType :: String -> Q Type,
+                                 quoteDec  :: String -> Q [Dec] }
+</programlisting>
+That is, a quoter is a tuple of four parsers, one for each of the contexts
+in which a quasi-quote can occur.
+</para></listitem>
+<listitem><para>
+A quasi-quote is expanded by applying the appropriate parser to the string
+enclosed by the Oxford brackets.  The context of the quasi-quote (expression, pattern,
+type, declaration) determines which of the parsers is called.
+</para></listitem>
+</itemizedlist>
+</para>
 <para>
-Note that in the example we make use of an antiquoted
+The example below shows quasi-quotation in action.  The quoter <literal>expr</literal>
+is bound to a value of type <literal>QuasiQuoter</literal> defined in module <literal>Expr</literal>.
+The example makes use of an antiquoted
 variable <literal>n</literal>, indicated by the syntax <literal>'int:n</literal>
 (this syntax for anti-quotation was defined by the parser's
 author, <emphasis>not</emphasis> by GHC). This binds <literal>n</literal> to the
@@ -6333,12 +6499,6 @@ an expression parser that returns a value of type <literal>Q Exp</literal> and a
 pattern parser that returns a value of type <literal>Q Pat</literal>.
 </para>
 
-<para>In general, a quasi-quote has the form
-<literal>[$<replaceable>quoter</replaceable>| <replaceable>string</replaceable> |]</literal>.
-The <replaceable>quoter</replaceable> must be the name of an imported quoter; it
-cannot be an arbitrary expression.  The quoted <replaceable>string</replaceable> 
-can be arbitrary, and may contain newlines.
-</para>
 <para>
 Quasiquoters must obey the same stage restrictions as Template Haskell, e.g., in
 the example, <literal>expr</literal> cannot be defined
@@ -6346,22 +6506,21 @@ in <literal>Main.hs</literal> where it is used, but must be imported.
 </para>
 
 <programlisting>
-
-{- Main.hs -}
+{- ------------- file Main.hs --------------- -}
 module Main where
 
 import Expr
 
 main :: IO ()
-main = do { print $ eval [$expr|1 + 2|]
+main = do { print $ eval [expr|1 + 2|]
           ; case IntExpr 1 of
-              { [$expr|'int:n|] -> print n
+              { [expr|'int:n|] -> print n
               ;  _              -> return ()
               }
           }
 
 
-{- Expr.hs -}
+{- ------------- file Expr.hs --------------- -}
 module Expr where
 
 import qualified Language.Haskell.TH as TH
@@ -6388,7 +6547,7 @@ eval (BinopExpr op x y) = (opToFun op) (eval x) (eval y)
     opToFun MulOp = (*)
     opToFun DivOp = div
 
-expr = QuasiQuoter parseExprExp parseExprPat
+expr = QuasiQuoter { quoteExp = parseExprExp, quotePat =  parseExprPat }
 
 -- Parse an Expr, returning its representation as
 -- either a Q Exp or a Q Pat. See the referenced paper
@@ -6404,19 +6563,18 @@ parseExprPat ...
 </programlisting>
 
 <para>Now run the compiler:
-</para>
 <programlisting>
 $ ghc --make -XQuasiQuotes Main.hs -o main
 </programlisting>
+</para>
 
-<para>Run "main" and here is your output:</para>
-
+<para>Run "main" and here is your output:
 <programlisting>
 $ ./main
 3
 1
 </programlisting>
-
+</para>
 </sect2>
 
 </sect1>
@@ -6489,7 +6647,7 @@ The arrows web page at
 With the <option>-XArrows</option> flag, GHC supports the arrow
 notation described in the second of these papers,
 translating it using combinators from the
-<ulink url="../libraries/base/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
+<ulink url="&libraryBaseLocation;/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
 module.
 What follows is a brief introduction to the notation;
 it won't make much sense unless you've read Hughes's paper.
@@ -6604,7 +6762,7 @@ the arrow <literal>f</literal>, and matches its output against
 <literal>y</literal>.
 In the next line, the output is discarded.
 The arrow <function>returnA</function> is defined in the
-<ulink url="../libraries/base/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
+<ulink url="&libraryBaseLocation;/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
 module as <literal>arr id</literal>.
 The above example is treated as an abbreviation for
 <screen>
@@ -6621,7 +6779,7 @@ arr (\ x -> (x, x)) >>>
 Note that variables not used later in the composition are projected out.
 After simplification using rewrite rules (see <xref linkend="rewrite-rules"/>)
 defined in the
-<ulink url="../libraries/base/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
+<ulink url="&libraryBaseLocation;/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>
 module, this reduces to
 <screen>
 arr (\ x -> (x+1, x)) >>>
@@ -6917,7 +7075,7 @@ additional restrictions:
 <listitem>
 <para>
 The module must import
-<ulink url="../libraries/base/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>.
+<ulink url="&libraryBaseLocation;/Control-Arrow.html"><literal>Control.Arrow</literal></ulink>.
 </para>
 </listitem>
 
@@ -7005,16 +7163,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
@@ -7277,11 +7442,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>
 
@@ -7407,21 +7572,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
@@ -7449,8 +7661,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
@@ -7459,6 +7672,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>
        
@@ -7477,6 +7741,14 @@ itself, so an INLINE pragma is always ignored.</para>
         portable).</para>
       </sect3>
 
+      <sect3 id="conlike-pragma">
+       <title>CONLIKE modifier</title>
+       <indexterm><primary>CONLIKE</primary></indexterm>
+        <para>An INLINE or NOINLINE pragma may have a CONLIKE modifier, 
+        which affects matching in RULEs (only).  See <xref linkend="conlike"/>.
+        </para>
+      </sect3>
+
       <sect3 id="phase-control">
        <title>Phase control</title>
 
@@ -7707,6 +7979,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"/>.
@@ -7735,6 +8010,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:
@@ -7745,6 +8080,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>
 
@@ -7829,10 +8165,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
@@ -7873,7 +8205,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>
@@ -8112,18 +8444,24 @@ not be substituted, and the rule would not fire.
 
 </para>
 </listitem>
-<listitem>
+</itemizedlist>
+
+</para>
+
+</sect2>
+
+<sect2 id="conlike">
+<title>How rules interact with INLINE/NOINLINE and CONLIKE pragmas</title>
 
 <para>
 Ordinary inlining happens at the same time as rule rewriting, which may lead to unexpected
 results.  Consider this (artificial) example
 <programlisting>
 f x = x
-{-# RULES "f" f True = False #-}
-
 g y = f y
-
 h z = g True
+
+{-# RULES "f" f True = False #-}
 </programlisting>
 Since <literal>f</literal>'s right-hand side is small, it is inlined into <literal>g</literal>,
 to give
@@ -8137,14 +8475,37 @@ would have been a better chance that <literal>f</literal>'s RULE might fire.
 </para>
 <para>
 The way to get predictable behaviour is to use a NOINLINE 
-pragma on <literal>f</literal>, to ensure
+pragma, or an INLINE[<replaceable>phase</replaceable>] pragma, on <literal>f</literal>, to ensure
 that it is not inlined until its RULEs have had a chance to fire.
 </para>
-</listitem>
-</itemizedlist>
-
+<para>
+GHC is very cautious about duplicating work.  For example, consider
+<programlisting>
+f k z xs = let xs = build g
+           in ...(foldr k z xs)...sum xs...
+{-# RULES "foldr/build" forall k z g. foldr k z (build g) = g k z #-}
+</programlisting>
+Since <literal>xs</literal> is used twice, GHC does not fire the foldr/build rule.  Rightly
+so, because it might take a lot of work to compute <literal>xs</literal>, which would be
+duplicated if the rule fired.
+</para>
+<para>
+Sometimes, however, this approach is over-cautious, and we <emphasis>do</emphasis> want the
+rule to fire, even though doing so would duplicate redex.  There is no way that GHC can work out
+when this is a good idea, so we provide the CONLIKE pragma to declare it, thus:
+<programlisting>
+{-# INLINE[1] CONLIKE f #-}
+f x = <replaceable>blah</replaceable>
+</programlisting>
+CONLIKE is a modifier to an INLINE or NOINLINE pragam.  It specifies that an application
+of f to one argument (in general, the number of arguments to the left of the '=' sign)
+should be considered cheap enough to duplicate, if such a duplication would make rule
+fire.  (The name "CONLIKE" is short for "constructor-like", because constructors certainly
+have such a property.)
+The CONLIKE pragam is a modifier to INLINE/NOINLINE because it really only makes sense to match 
+<literal>f</literal> on the LHS of a rule if you are sure that <literal>f</literal> is
+not going to be inlined before the rule has a chance to fire.
 </para>
-
 </sect2>
 
 <sect2>
@@ -8404,8 +8765,8 @@ comparison.
 
 </sect2>
 
-<sect2>
-<title>Controlling what's going on</title>
+<sect2 id="controlling-rules">
+<title>Controlling what's going on in rewrite rules</title>
 
 <para>
 
@@ -8413,18 +8774,29 @@ comparison.
 <listitem>
 
 <para>
- Use <option>-ddump-rules</option> to see what transformation rules GHC is using.
+Use <option>-ddump-rules</option> to see the rules that are defined
+<emphasis>in this module</emphasis>.
+This includes rules generated by the specialisation pass, but excludes
+rules imported from other modules. 
 </para>
 </listitem>
-<listitem>
 
+<listitem>
 <para>
  Use <option>-ddump-simpl-stats</option> to see what rules are being fired.
 If you add <option>-dppr-debug</option> you get a more detailed listing.
 </para>
 </listitem>
+
 <listitem>
+<para>
+ 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>
 
+<listitem>
 <para>
  The definition of (say) <function>build</function> in <filename>GHC/Base.lhs</filename> looks like this:
 
@@ -8525,8 +8897,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>
 
 
@@ -8832,7 +9220,6 @@ standard behaviour.
 
 <!-- Emacs stuff:
      ;;; Local Variables: ***
-     ;;; mode: xml ***
      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
      ;;; ispell-local-dictionary: "british" ***
      ;;; End: ***