Implement generalised list comprehensions
[ghc-hetmet.git] / docs / users_guide / glasgow_exts.xml
index 9415dfb..de69b60 100644 (file)
@@ -319,7 +319,7 @@ it is always correct!  It is also intended for processing into text.</para>
 <para> Indeed,
 the result of such processing is part of the description of the 
  <ulink
-      url="http://haskell.cs.yale.edu/ghc/docs/papers/core.ps.gz">External
+      url="http://www.haskell.org/ghc/docs/papers/core.ps.gz">External
         Core language</ulink>.
 So that document is a good place to look for a type-set version.
 We would be very happy if someone wanted to volunteer to produce an SGML
@@ -993,7 +993,7 @@ and improve termination (Section 3.2 of the paper).
 </para>
 
 <para>
-The web page: <ulink url="http://www.cse.ogi.edu/PacSoft/projects/rmb">http://www.cse.ogi.edu/PacSoft/projects/rmb</ulink>
+The web page: <ulink url="http://www.cse.ogi.edu/PacSoft/projects/rmb/">http://www.cse.ogi.edu/PacSoft/projects/rmb/</ulink>
 contains up to date information on recursive monadic bindings.
 </para>
 
@@ -1058,6 +1058,166 @@ This name is not supported by GHC.
     branches.</para>
 
   </sect2>
+  
+  <!-- ===================== TRANSFORM LIST COMPREHENSIONS ===================  -->
+
+  <sect2 id="generalised-list-comprehensions">
+    <title>Generalised (SQL-Like) List Comprehensions</title>
+    <indexterm><primary>list comprehensions</primary><secondary>generalised</secondary>
+    </indexterm>
+    <indexterm><primary>extended list comprehensions</primary>
+    </indexterm>
+    <indexterm><primary>group</primary></indexterm>
+    <indexterm><primary>sql</primary></indexterm>
+
+
+    <para>Generalised list comprehensions are a further enhancement to the
+    list comprehension syntatic sugar to allow operations such as sorting
+    and grouping which are familiar from SQL.   They are fully described in the
+       paper <ulink url="http://research.microsoft.com/~simonpj/papers/list-comp">
+         Comprehensive comprehensions: comprehensions with "order by" and "group by"</ulink>,
+    except that the syntax we use differs slightly from the paper.</para>
+<para>Here is an example: 
+<programlisting>
+employees = [ ("Simon", "MS", 80)
+, ("Erik", "MS", 100)
+, ("Phil", "Ed", 40)
+, ("Gordon", "Ed", 45)
+, ("Paul", "Yale", 60)]
+
+output = [ (the dept, sum salary)
+| (name, dept, salary) &lt;- employees
+, then group by dept
+, then sortWith by (sum salary)
+, then take 5 ]
+</programlisting>
+In this example, the list <literal>output</literal> would take on 
+    the value:
+    
+<programlisting>
+[("Yale", 60), ("Ed", 85), ("MS", 180)]
+</programlisting>
+</para>
+<para>There are three new keywords: <literal>group</literal>, <literal>by</literal>, and <literal>using</literal>.
+(The function <literal>sortWith</literal> is not a keyword; it is an ordinary
+function that is exported by <literal>GHC.Exts</literal>.)</para>
+
+<para>There are five new forms of compehension qualifier,
+all introduced by the (existing) keyword <literal>then</literal>:
+    <itemizedlist>
+    <listitem>
+    
+<programlisting>
+then f
+</programlisting>
+
+    This statement requires that <literal>f</literal> have the type <literal>
+    forall a. [a] -> [a]</literal>. You can see an example of it's use in the
+    motivating example, as this form is used to apply <literal>take 5</literal>.
+    
+    </listitem>
+    
+    
+    <listitem>
+<para>
+<programlisting>
+then f by e
+</programlisting>
+
+    This form is similar to the previous one, but allows you to create a function
+    which will be passed as the first argument to f. As a consequence f must have 
+    the type <literal>forall a. (a -> t) -> [a] -> [a]</literal>. As you can see
+    from the type, this function lets f &quot;project out&quot; some information 
+    from the elements of the list it is transforming.</para>
+
+    <para>An example is shown in the opening example, where <literal>sortWith</literal> 
+    is supplied with a function that lets it find out the <literal>sum salary</literal> 
+    for any item in the list comprehension it transforms.</para>
+
+    </listitem>
+
+
+    <listitem>
+
+<programlisting>
+then group by e using f
+</programlisting>
+
+    <para>This is the most general of the grouping-type statements. In this form,
+    f is required to have type <literal>forall a. (a -> t) -> [a] -> [[a]]</literal>.
+    As with the <literal>then f by e</literal> case above, the first argument
+    is a function supplied to f by the compiler which lets it compute e on every
+    element of the list being transformed. However, unlike the non-grouping case,
+    f additionally partitions the list into a number of sublists: this means that
+    at every point after this statement, binders occuring before it in the comprehension
+    refer to <emphasis>lists</emphasis> of possible values, not single values. To help understand
+    this, let's look at an example:</para>
+    
+<programlisting>
+-- This works similarly to groupWith in GHC.Exts, but doesn't sort its input first
+groupRuns :: Eq b => (a -> b) -> [a] -> [[a]]
+groupRuns f = groupBy (\x y -> f x == f y)
+
+output = [ (the x, y)
+| x &lt;- ([1..3] ++ [1..2])
+, y &lt;- [4..6]
+, then group by x using groupRuns ]
+</programlisting>
+
+    <para>This results in the variable <literal>output</literal> taking on the value below:</para>
+
+<programlisting>
+[(1, [4, 5, 6]), (2, [4, 5, 6]), (3, [4, 5, 6]), (1, [4, 5, 6]), (2, [4, 5, 6])]
+</programlisting>
+
+    <para>Note that we have used the <literal>the</literal> function to change the type 
+    of x from a list to its original numeric type. The variable y, in contrast, is left 
+    unchanged from the list form introduced by the grouping.</para>
+
+    </listitem>
+
+    <listitem>
+
+<programlisting>
+then group by e
+</programlisting>
+
+    <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
+    is the form of the group statement that we made use of in the opening example.</para>
+
+    </listitem>
+    
+    
+    <listitem>
+
+<programlisting>
+then group using f
+</programlisting>
+
+    <para>With this form of the group statement, f is required to simply have the type
+    <literal>forall a. [a] -> [[a]]</literal>, which will be used to group up the
+    comprehension so far directly. An example of this form is as follows:</para>
+    
+<programlisting>
+output = [ x
+| y &lt;- [1..5]
+, x &lt;- "hello"
+, then group using inits]
+</programlisting>
+
+    <para>This will yield a list containing every prefix of the word "hello" written out 5 times:</para>
+
+<programlisting>
+["","h","he","hel","hell","hello","helloh","hellohe","hellohel","hellohell","hellohello","hellohelloh",...]
+</programlisting>
+
+    </listitem>
+</itemizedlist>
+</para>
+  </sect2>
 
    <!-- ===================== REBINDABLE SYNTAX ===================  -->
 
@@ -1152,16 +1312,16 @@ GHC allows a small extension to the syntax of left operator sections, which
 allows you to define postfix operators.  The extension is this:  the left section
 <programlisting>
   (e !)
-</programlisting> 
+</programlisting>
 is equivalent (from the point of view of both type checking and execution) to the expression
 <programlisting>
   ((!) e)
-</programlisting> 
+</programlisting>
 (for any expression <literal>e</literal> and operator <literal>(!)</literal>.
 The strict Haskell 98 interpretation is that the section is equivalent to
 <programlisting>
   (\y -> (!) e y)
-</programlisting> 
+</programlisting>
 That is, the operator must be a function of two arguments.  GHC allows it to
 take only one argument, and that in turn allows you to write the function
 postfix.
@@ -1689,8 +1849,8 @@ adding a new existential quantification construct.
 
 </sect3>
 
-<sect3>
-<title>Type classes</title>
+<sect3 id="existential-with-context">
+<title>Existentials and type classes</title>
 
 <para>
 An easy extension is to allow
@@ -1744,11 +1904,6 @@ dictionaries for <literal>Eq</literal> and <literal>Show</literal> respectively,
 extract it on pattern matching.
 </para>
 
-<para>
-Notice the way that the syntax fits smoothly with that used for
-universal quantification earlier.
-</para>
-
 </sect3>
 
 <sect3 id="existential-records">
@@ -2021,19 +2176,8 @@ In the example, the equality dictionary is used to satisfy the equality constrai
 generated by the call to <literal>elem</literal>, so that the type of
 <literal>insert</literal> itself has no <literal>Eq</literal> constraint.
 </para>
-<para>This behaviour contrasts with Haskell 98's peculiar treatment of 
-contexts on a data type declaration (Section 4.2.1 of the Haskell 98 Report).
-In Haskell 98 the definition
-<programlisting>
-  data Eq a => Set' a = MkSet' [a]
-</programlisting>
-gives <literal>MkSet'</literal> the same type as <literal>MkSet</literal> above.  But instead of 
-<emphasis>making available</emphasis> an <literal>(Eq a)</literal> constraint, pattern-matching
-on <literal>MkSet'</literal> <emphasis>requires</emphasis> an <literal>(Eq a)</literal> constraint!
-GHC faithfully implements this behaviour, odd though it is.  But for GADT-style declarations,
-GHC's behaviour is much more useful, as well as much more intuitive.</para>
 <para>
-For example, a possible application of GHC's behaviour is to reify dictionaries:
+For example, one possible application is to reify dictionaries:
 <programlisting>
    data NumInst a where
      MkNumInst :: Num a => NumInst a
@@ -2047,6 +2191,38 @@ For example, a possible application of GHC's behaviour is to reify dictionaries:
 Here, a value of type <literal>NumInst a</literal> is equivalent 
 to an explicit <literal>(Num a)</literal> dictionary.
 </para>
+<para>
+All this applies to constructors declared using the syntax of <xref linkend="existential-with-context"/>.
+For example, the <literal>NumInst</literal> data type above could equivalently be declared 
+like this:
+<programlisting>
+   data NumInst a 
+      = Num a => MkNumInst (NumInst a)
+</programlisting>
+Notice that, unlike the situation when declaring an existental, there is 
+no <literal>forall</literal>, because the <literal>Num</literal> constrains the
+data type's univerally quantified type variable <literal>a</literal>.  
+A constructor may have both universal and existential type variables: for example,
+the following two declarations are equivalent:
+<programlisting>
+   data T1 a 
+       = forall b. (Num a, Eq b) => MkT1 a b
+   data T2 a where
+       MkT2 :: (Num a, Eq b) => a -> b -> T2 a
+</programlisting>
+</para>
+<para>All this behaviour contrasts with Haskell 98's peculiar treatment of 
+contexts on a data type declaration (Section 4.2.1 of the Haskell 98 Report).
+In Haskell 98 the definition
+<programlisting>
+  data Eq a => Set' a = MkSet' [a]
+</programlisting>
+gives <literal>MkSet'</literal> the same type as <literal>MkSet</literal> above.  But instead of 
+<emphasis>making available</emphasis> an <literal>(Eq a)</literal> constraint, pattern-matching
+on <literal>MkSet'</literal> <emphasis>requires</emphasis> an <literal>(Eq a)</literal> constraint!
+GHC faithfully implements this behaviour, odd though it is.  But for GADT-style declarations,
+GHC's behaviour is much more useful, as well as much more intuitive.
+</para>
 
 <para>
 The rest of this section gives further details about GADT-style data
@@ -2198,7 +2374,7 @@ the type <literal>a</literal> is refined to <literal>Int</literal>.  That's the
 A precise specification of the type rules is beyond what this user manual aspires to, 
 but the design closely follows that described in
 the paper <ulink
-url="http://research.microsoft.com/%7Esimonpj/papers/gadt/index.htm">Simple
+url="http://research.microsoft.com/%7Esimonpj/papers/gadt/">Simple
 unification-based type inference for GADTs</ulink>,
 (ICFP 2006).
 The general principle is this: <emphasis>type refinement is only carried out 
@@ -2217,7 +2393,7 @@ the result type of the <literal>case</literal> expression.  Hence the addition <
 <para>
 These and many other examples are given in papers by Hongwei Xi, and
 Tim Sheard. There is a longer introduction
-<ulink url="http://haskell.org/haskellwiki/GADT">on the wiki</ulink>,
+<ulink url="http://www.haskell.org/haskellwiki/GADT">on the wiki</ulink>,
 and Ralf Hinze's
 <ulink url="http://www.informatik.uni-bonn.de/~ralf/publications/With.pdf">Fun with phantom types</ulink> also has a number of examples. Note that papers
 may use different notation to that implemented in GHC.
@@ -2393,14 +2569,14 @@ Haskell 98, you can inherit instances of <literal>Eq</literal>, <literal>Ord</li
 other classes you have to write an explicit instance declaration. For
 example, if you define
 
-<programlisting> 
+<programlisting>
   newtype Dollars = Dollars Int 
-</programlisting> 
+</programlisting>
 
 and you want to use arithmetic on <literal>Dollars</literal>, you have to
 explicitly define an instance of <literal>Num</literal>:
 
-<programlisting> 
+<programlisting>
   instance Num Dollars where
     Dollars a + Dollars b = Dollars (a+b)
     ...
@@ -2418,17 +2594,17 @@ dictionary, only slower!
 GHC now permits such instances to be derived instead, 
 using the flag <option>-XGeneralizedNewtypeDeriving</option>,
 so one can write 
-<programlisting> 
+<programlisting>
   newtype Dollars = Dollars Int deriving (Eq,Show,Num)
-</programlisting> 
+</programlisting>
 
 and the implementation uses the <emphasis>same</emphasis> <literal>Num</literal> dictionary
 for <literal>Dollars</literal> as for <literal>Int</literal>. Notionally, the compiler
 derives an instance declaration of the form
 
-<programlisting> 
+<programlisting>
   instance Num Int => Num Dollars
-</programlisting> 
+</programlisting>
 
 which just adds or removes the <literal>newtype</literal> constructor according to the type.
 </para>
@@ -2438,27 +2614,27 @@ We can also derive instances of constructor classes in a similar
 way. For example, suppose we have implemented state and failure monad
 transformers, such that
 
-<programlisting> 
+<programlisting>
   instance Monad m => Monad (State s m) 
   instance Monad m => Monad (Failure m)
-</programlisting> 
+</programlisting>
 In Haskell 98, we can define a parsing monad by 
-<programlisting> 
+<programlisting>
   type Parser tok m a = State [tok] (Failure m) a
-</programlisting> 
+</programlisting>
 
 which is automatically a monad thanks to the instance declarations
 above. With the extension, we can make the parser type abstract,
 without needing to write an instance of class <literal>Monad</literal>, via
 
-<programlisting> 
+<programlisting>
   newtype Parser tok m a = Parser (State [tok] (Failure m) a)
                          deriving Monad
 </programlisting>
 In this case the derived instance declaration is of the form 
-<programlisting> 
+<programlisting>
   instance Monad (State [tok] (Failure m)) => Monad (Parser tok m) 
-</programlisting> 
+</programlisting>
 
 Notice that, since <literal>Monad</literal> is a constructor class, the
 instance is a <emphasis>partial application</emphasis> of the new type, not the
@@ -2473,12 +2649,12 @@ newtype is the last class parameter. In this case, a ``partial
 application'' of the class appears in the <literal>deriving</literal>
 clause. For example, given the class
 
-<programlisting> 
+<programlisting>
   class StateMonad s m | m -> s where ... 
   instance Monad m => StateMonad s (State s m) where ... 
-</programlisting> 
+</programlisting>
 then we can derive an instance of <literal>StateMonad</literal> for <literal>Parser</literal>s by 
-<programlisting> 
+<programlisting>
   newtype Parser tok m a = Parser (State [tok] (Failure m) a)
                          deriving (Monad, StateMonad [tok])
 </programlisting>
@@ -2486,7 +2662,7 @@ then we can derive an instance of <literal>StateMonad</literal> for <literal>Par
 The derived instance is obtained by completing the application of the
 class to the new type:
 
-<programlisting> 
+<programlisting>
   instance StateMonad [tok] (State [tok] (Failure m)) =>
            StateMonad [tok] (Parser tok m)
 </programlisting>
@@ -2506,9 +2682,9 @@ the newtype and its representation.
 Derived instance declarations are constructed as follows. Consider the
 declaration (after expansion of any type synonyms)
 
-<programlisting> 
+<programlisting>
   newtype T v1...vn = T' (t vk+1...vn) deriving (c1...cm) 
-</programlisting> 
+</programlisting>
 
 where 
  <itemizedlist>
@@ -2537,17 +2713,17 @@ where
 </itemizedlist>
 Then, for each <literal>ci</literal>, the derived instance
 declaration is:
-<programlisting> 
+<programlisting>
   instance ci t => ci (T v1...vk)
 </programlisting>
 As an example which does <emphasis>not</emphasis> work, consider 
-<programlisting> 
+<programlisting>
   newtype NonMonad m s = NonMonad (State s m s) deriving Monad 
-</programlisting> 
+</programlisting>
 Here we cannot derive the instance 
-<programlisting> 
+<programlisting>
   instance Monad (State s m) => Monad (NonMonad m) 
-</programlisting> 
+</programlisting>
 
 because the type variable <literal>s</literal> occurs in <literal>State s m</literal>,
 and so cannot be "eta-converted" away. It is a good thing that this
@@ -2561,7 +2737,7 @@ Notice also that the <emphasis>order</emphasis> of class parameters becomes
 important, since we can only derive instances for the last one. If the
 <literal>StateMonad</literal> class above were instead defined as
 
-<programlisting> 
+<programlisting>
   class StateMonad m s | m -> s where ... 
 </programlisting>
 
@@ -2593,8 +2769,8 @@ the standard method is used or the one described here.)
 <para>
 This section, and the next one, documents GHC's type-class extensions.
 There's lots of background in the paper <ulink
-url="http://research.microsoft.com/~simonpj/Papers/type-class-design-space" >Type
-classes: exploring the design space</ulink > (Simon Peyton Jones, Mark
+url="http://research.microsoft.com/~simonpj/Papers/type-class-design-space/">Type
+classes: exploring the design space</ulink> (Simon Peyton Jones, Mark
 Jones, Erik Meijer).
 </para>
 <para>
@@ -2673,7 +2849,7 @@ class type variable, thus:
 The type of <literal>elem</literal> is illegal in Haskell 98, because it
 contains the constraint <literal>Eq a</literal>, constrains only the 
 class type variable (in this case <literal>a</literal>).
-GHC lifts this restriction.
+GHC lifts this restriction (flag <option>-XConstrainedClassMethods</option>).
 </para>
 
 
@@ -2685,7 +2861,7 @@ GHC lifts this restriction.
 </title>
 
 <para> Functional dependencies are implemented as described by Mark Jones
-in &ldquo;<ulink url="http://www.cse.ogi.edu/~mpj/pubs/fundeps.html">Type Classes with Functional Dependencies</ulink>&rdquo;, Mark P. Jones, 
+in &ldquo;<ulink url="http://citeseer.ist.psu.edu/jones00type.html">Type Classes with Functional Dependencies</ulink>&rdquo;, Mark P. Jones, 
 In Proceedings of the 9th European Symposium on Programming, 
 ESOP 2000, Berlin, Germany, March 2000, Springer-Verlag LNCS 1782,
 .
@@ -3020,7 +3196,7 @@ must be of the form <literal>C a</literal> where <literal>a</literal>
 is a type variable that occurs in the head.
 </para>
 <para>
-The <option>-fglasgow-exts</option> flag loosens these restrictions
+The <option>-XFlexibleInstances</option> flag loosens these restrictions
 considerably.  Firstly, multi-parameter type classes are permitted.  Secondly,
 the context and head of the instance declaration can each consist of arbitrary
 (well-kinded) assertions <literal>(C t1 ... tn)</literal> subject only to the
@@ -3149,7 +3325,7 @@ typechecker loop:
   class F a b | a->b
   instance F [a] [[a]]
   instance (D c, F a c) => D [a]   -- 'c' is not mentioned in the head
-</programlisting>  
+</programlisting>
 Similarly, it can be tempting to lift the coverage condition:
 <programlisting>
   class Mul a b c | a b -> c where
@@ -3385,7 +3561,7 @@ instance of <literal>Num</literal> <emphasis>or</emphasis> of <literal>IsString<
 </para></listitem>
 
 <listitem><para>
-The standard defaulting rule (<ulink url="http://haskell.org/onlinereport/decls.html#sect4.3.4">Haskell Report, Section 4.3.4</ulink>)
+The standard defaulting rule (<ulink url="http://www.haskell.org/onlinereport/decls.html#sect4.3.4">Haskell Report, Section 4.3.4</ulink>)
 is extended thus: defaulting applies when all the unresolved constraints involve standard classes
 <emphasis>or</emphasis> <literal>IsString</literal>; and at least one is a numeric class
 <emphasis>or</emphasis> <literal>IsString</literal>.
@@ -3426,7 +3602,7 @@ to work since it gets translated into an equality comparison.
 <sect2 id="type-restrictions">
 <title>Type signatures</title>
 
-<sect3><title>The context of a type signature</title>
+<sect3 id="flexible-contexts"><title>The context of a type signature</title>
 <para>
 Unlike Haskell 98, constraints in types do <emphasis>not</emphasis> have to be of
 the form <emphasis>(class type-variable)</emphasis> or
@@ -4062,9 +4238,18 @@ The function <literal>f3</literal> has a rank-3 type;
 it has rank-2 types on the left of a function arrow.
 </para>
 <para>
-GHC allows types of arbitrary rank; you can nest <literal>forall</literal>s
-arbitrarily deep in function arrows.   (GHC used to be restricted to rank 2, but
-that restriction has now been lifted.)
+GHC has three flags to control higher-rank types:
+<itemizedlist>
+<listitem><para>
+ <option>-XPolymorphicComponents</option>: data constructors (only) can have polymorphic argment types.
+</para></listitem>
+<listitem><para>
+ <option>-XRank2Types</option>: any function (including data constructors) can have a rank-2 type.
+</para></listitem>
+<listitem><para>
+ <option>-XRankNTypes</option>: any function (including data constructors) can have an arbitrary-rank type.
+That is,  you can nest <literal>forall</literal>s
+arbitrarily deep in function arrows.
 In particular, a forall-type (also called a "type scheme"),
 including an operational type class context, is legal:
 <itemizedlist>
@@ -4076,6 +4261,8 @@ field type signatures.</para> </listitem>
 <listitem> <para> As the type of an implicit parameter </para> </listitem>
 <listitem> <para> In a pattern type signature (see <xref linkend="scoped-type-variables"/>) </para> </listitem>
 </itemizedlist>
+</para></listitem>
+</itemizedlist>
 Of course <literal>forall</literal> becomes a keyword; you can't use <literal>forall</literal> as
 a type variable any more!
 </para>
@@ -4327,7 +4514,7 @@ Notice here that the <literal>Maybe</literal> type is parameterised by the
 [a])</literal>.
 </para>
 <para>The technical details of this extension are described in the paper
-<ulink url="http://research.microsoft.com/%7Esimonpj/papers/boxy">Boxy types:
+<ulink url="http://research.microsoft.com/%7Esimonpj/papers/boxy/">Boxy types:
 type inference for higher-rank types and impredicativity</ulink>,
 which appeared at ICFP 2006.  
 </para>
@@ -4390,7 +4577,7 @@ A <emphasis>lexically scoped type variable</emphasis> can be bound by:
 <para>
 In Haskell, a programmer-written type signature is implicitly quantified over
 its free type variables (<ulink
-url="http://haskell.org/onlinereport/decls.html#sect4.1.2">Section
+url="http://www.haskell.org/onlinereport/decls.html#sect4.1.2">Section
 4.1.2</ulink> 
 of the Haskel Report).
 Lexically scoped type variables affect this implicit quantification rules
@@ -4455,7 +4642,7 @@ type variable <literal>s</literal> into scope, in the annotated expression
 <title>Pattern type signatures</title>
 <para>
 A type signature may occur in any pattern; this is a <emphasis>pattern type
-signature</emphasis>.  
+signature</emphasis>. 
 For example:
 <programlisting>
   -- f and g assume that 'a' is already in scope
@@ -4468,9 +4655,27 @@ already in scope (i.e. bound by the enclosing context), matters are simple: the
 signature simply constrains the type of the pattern in the obvious way.
 </para>
 <para>
-There is only one situation in which you can write a pattern type signature that
-mentions a type variable that is not already in scope, namely in pattern match
-of an existential data constructor.  For example:
+Unlike expression and declaration type signatures, pattern type signatures are not implictly generalised.
+The pattern in a <emphasis>patterm binding</emphasis> may only mention type variables
+that are already in scope.  For example:
+<programlisting>
+  f :: forall a. [a] -> (Int, [a])
+  f xs = (n, zs)
+    where
+      (ys::[a], n) = (reverse xs, length xs) -- OK
+      zs::[a] = xs ++ ys                     -- OK
+
+      Just (v::b) = ...  -- Not OK; b is not in scope
+</programlisting>
+Here, the pattern signatures for <literal>ys</literal> and <literal>zs</literal>
+are fine, but the one for <literal>v</literal> is not because <literal>b</literal> is
+not in scope. 
+</para>
+<para>
+However, in all patterns <emphasis>other</emphasis> than pattern bindings, a pattern
+type signature may mention a type variable that is not in scope; in this case,
+<emphasis>the signature brings that type variable into scope</emphasis>.
+This is particularly important for existential data constructors.  For example:
 <programlisting>
   data T = forall a. MkT [a]
 
@@ -4480,14 +4685,19 @@ of an existential data constructor.  For example:
                    t3::[a] = [t,t,t]
 </programlisting>
 Here, the pattern type signature <literal>(t::a)</literal> mentions a lexical type
-variable that is not already in scope.  Indeed, it cannot already be in scope,
+variable that is not already in scope.  Indeed, it <emphasis>cannot</emphasis> already be in scope,
 because it is bound by the pattern match.  GHC's rule is that in this situation
 (and only then), a pattern type signature can mention a type variable that is
 not already in scope; the effect is to bring it into scope, standing for the
 existentially-bound type variable.
 </para>
 <para>
-If this seems a little odd, we think so too.  But we must have
+When a pattern type signature binds a type variable in this way, GHC insists that the 
+type variable is bound to a <emphasis>rigid</emphasis>, or fully-known, type variable.
+This means that any user-written type signature always stands for a completely known type.
+</para>
+<para>
+If all this seems a little odd, we think so too.  But we must have
 <emphasis>some</emphasis> way to bring such type variables into scope, else we
 could not name existentially-bound type variables in subsequent type signatures.
 </para>
@@ -4585,18 +4795,18 @@ scope over the methods defined in the <literal>where</literal> part.  For exampl
 The Haskell Report specifies that a group of bindings (at top level, or in a
 <literal>let</literal> or <literal>where</literal>) should be sorted into
 strongly-connected components, and then type-checked in dependency order
-(<ulink url="http://haskell.org/onlinereport/decls.html#sect4.5.1">Haskell
+(<ulink url="http://www.haskell.org/onlinereport/decls.html#sect4.5.1">Haskell
 Report, Section 4.5.1</ulink>).  
 As each group is type-checked, any binders of the group that
 have
 an explicit type signature are put in the type environment with the specified
 polymorphic type,
 and all others are monomorphic until the group is generalised 
-(<ulink url="http://haskell.org/onlinereport/decls.html#sect4.5.2">Haskell Report, Section 4.5.2</ulink>).
+(<ulink url="http://www.haskell.org/onlinereport/decls.html#sect4.5.2">Haskell Report, Section 4.5.2</ulink>).
 </para>
 
 <para>Following a suggestion of Mark Jones, in his paper
-<ulink url="http://www.cse.ogi.edu/~mpj/thih/">Typing Haskell in
+<ulink url="http://citeseer.ist.psu.edu/424440.html">Typing Haskell in
 Haskell</ulink>,
 GHC implements a more general scheme.  If <option>-XRelaxedPolyRec</option> is
 specified:
@@ -4657,7 +4867,7 @@ Currently, only the former are fully implemented, while we are still working
 on the latter.  As a result, the specification of the language extension is
 also still to some degree in flux.  Hence, a more detailed description of
 the language extension and its use is currently available
-from <ulink url="http://haskell.org/haskellwiki/GHC/Indexed_types">the Haskell
+from <ulink url="http://www.haskell.org/haskellwiki/GHC/Indexed_types">the Haskell
 wiki page on type families</ulink>.  The material will be moved to this user's
 guide when it has stabilised.
 </para>
@@ -4680,12 +4890,12 @@ Type families are enabled by the flag <option>-XTypeFamilies</option>.
 Haskell.  
 The background to
 the main technical innovations is discussed in "<ulink
-url="http://research.microsoft.com/~simonpj/papers/meta-haskell">
+url="http://research.microsoft.com/~simonpj/papers/meta-haskell/">
 Template Meta-programming for Haskell</ulink>" (Proc Haskell Workshop 2002).
 </para>
 <para>
 There is a Wiki page about
-Template Haskell at <ulink url="http://haskell.org/haskellwiki/Template_Haskell">
+Template Haskell at <ulink url="http://www.haskell.org/haskellwiki/Template_Haskell">
 http://www.haskell.org/haskellwiki/Template_Haskell</ulink>, and that is the best place to look for
 further details.
 You may also 
@@ -5508,7 +5718,7 @@ prefix notation:
 (!) f x = 3
 </programlisting>
 The semantics of Haskell pattern matching is described in <ulink
-url="http://haskell.org/onlinereport/exps.html#sect3.17.2">
+url="http://www.haskell.org/onlinereport/exps.html#sect3.17.2">
 Section 3.17.2</ulink> of the Haskell Report.  To this description add 
 one extra item 10, saying:
 <itemizedlist><listitem><para>Matching
@@ -5518,7 +5728,7 @@ the pattern <literal>!pat</literal> against a value <literal>v</literal> behaves
                <literal>v</literal></para></listitem>
 </itemizedlist>
 </para></listitem></itemizedlist>
-Similarly, in Figure 4 of  <ulink url="http://haskell.org/onlinereport/exps.html#sect3.17.3">
+Similarly, in Figure 4 of  <ulink url="http://www.haskell.org/onlinereport/exps.html#sect3.17.3">
 Section 3.17.3</ulink>, add a new case (t):
 <programlisting>
 case v of { !pat -> e; _ -> e' }
@@ -5526,7 +5736,7 @@ case v of { !pat -> e; _ -> e' }
 </programlisting>
 </para><para>
 That leaves let expressions, whose translation is given in 
-<ulink url="http://haskell.org/onlinereport/exps.html#sect3.12">Section
+<ulink url="http://www.haskell.org/onlinereport/exps.html#sect3.12">Section
 3.12</ulink>
 of the Haskell Report.
 In the translation box, first apply 
@@ -5840,8 +6050,26 @@ key_function :: Int -> String -> (Bool, Double)
         <para>The major effect of an <literal>INLINE</literal> pragma
         is to declare a function's &ldquo;cost&rdquo; to be very low.
         The normal unfolding machinery will then be very keen to
-        inline it.</para>
-
+        inline it.  However, an <literal>INLINE</literal> pragma for a 
+       function "<literal>f</literal>" has a number of other effects:
+<itemizedlist>
+<listitem><para>
+No funtions 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.
+</para></listitem>
+<listitem><para>
+The float-in, float-out, and common-sub-expression transformations are not 
+applied to the body of <literal>f</literal>.  
+</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>Syntactically, an <literal>INLINE</literal> pragma for a
         function can be put anywhere its type signature could be
         put.</para>
@@ -6029,7 +6257,7 @@ happen.
 
   h :: Eq a => a -> a -> a
   {-# SPECIALISE h :: (Eq a) => [a] -> [a] -> [a] #-}
-</programlisting>  
+</programlisting>
 The last of these examples will generate a 
 RULE with a somewhat-complex left-hand side (try it yourself), so it might not fire very
 well.  If you use this kind of specialisation, let us know how well it works.
@@ -7103,7 +7331,7 @@ carried out at let and where bindings.
           <indexterm><primary><option>-XNoMonomorphismRestriction</option></primary></indexterm>
 
 <para>Haskell's monomorphism restriction (see 
-<ulink url="http://haskell.org/onlinereport/decls.html#sect4.5.5">Section
+<ulink url="http://www.haskell.org/onlinereport/decls.html#sect4.5.5">Section
 4.5.5</ulink>
 of the Haskell Report)
 can be completely switched off by
@@ -7130,7 +7358,7 @@ can be completely switched off by
   [x] = e                    -- A pattern binding
 </programlisting>
 Experimentally, GHC now makes pattern bindings monomorphic <emphasis>by
-default</emphasis>.  Use <option>-XMonoPatBinds</option> to recover the
+default</emphasis>.  Use <option>-XNoMonoPatBinds</option> to recover the
 standard behaviour.
 </para>
 </sect2>