Remove the Unicode alternative for ".." (#3894)
[ghc-hetmet.git] / docs / users_guide / glasgow_exts.xml
index 6910a59..b7d43c9 100644 (file)
@@ -110,7 +110,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 +351,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 +400,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>
@@ -856,7 +903,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>
 
@@ -877,7 +924,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. 
@@ -992,9 +1039,9 @@ 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>-XREecursiveDo</option>,
+<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
@@ -1190,7 +1237,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>
@@ -1642,7 +1689,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>
@@ -3950,6 +3997,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> 
@@ -6149,25 +6241,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>
@@ -6373,19 +6469,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
@@ -6397,12 +6541,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
@@ -6410,22 +6548,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
@@ -6452,7 +6589,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
@@ -6468,19 +6605,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>
@@ -6553,7 +6689,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.
@@ -6668,7 +6804,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>
@@ -6685,7 +6821,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)) >>>
@@ -6981,7 +7117,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>
 
@@ -7345,7 +7481,7 @@ Assertion failures can be caught, see the documentation for the
 
       <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>
 
@@ -7541,6 +7677,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>
 
@@ -8176,18 +8320,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
@@ -8201,14 +8351,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>
@@ -8468,8 +8641,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>
 
@@ -8477,18 +8650,28 @@ 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> 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:
 
@@ -8589,7 +8772,7 @@ 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>
+url="&libraryGhcPrimLocation;/GHC-Prim.html"><literal>GHC.Prim</literal></ulink>
 in the library documentation.</para>
 </sect1>