[project @ 2003-07-21 11:06:22 by simonpj]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.sgml
index 38aa5b1..ded5567 100644 (file)
@@ -3177,11 +3177,13 @@ instances is most interesting.
 <sect1 id="template-haskell">
 <title>Template Haskell</title>
 
-<para>Template Haskell allows you to do compile-time meta-programming in Haskell.  The background 
-the main technical innovations are discussed in "<ulink
+<para>Template Haskell allows you to do compile-time meta-programming in Haskell.  There is a "home page" for
+Template Haskell at <ulink url="http://www.haskell.org/th/">
+http://www.haskell.org/th/</ulink>, while
+the background to
+the main technical innovations is discussed in "<ulink
 url="http://research.microsoft.com/~simonpj/papers/meta-haskell">
-Template Meta-programming for Haskell</ulink>", in 
-Proc Haskell Workshop 2002.
+Template Meta-programming for Haskell</ulink>" (Proc Haskell Workshop 2002).
 </para>
 
 <para> The first example from that paper is set out below as a worked example to help get you started. 
@@ -3407,8 +3409,9 @@ cmd   ::= exp1 -&lt;  exp2
        |  if exp then cmd1 else cmd2
        |  case exp of { calts }
        |  cmd1 qop cmd2
-       |  (| exp |) cmd1 .. cmdn
+       |  (| aexp cmd1 .. cmdn |)
        |  \ pat1 .. patn -> cmd
+       |  cmd aexp
        |  ( cmd )
 
 cstmt ::= let decls
@@ -3655,10 +3658,15 @@ there is also a more general syntax involving special brackets:
 <screen>
 proc x -> do
         y &lt;- f -&lt; x+1
-        (|untilA|) (increment -&lt; x+y) (within 0.5 -&lt; x)
+        (|untilA (increment -&lt; x+y) (within 0.5 -&lt; x)|)
 </screen>
 </para>
 
+</sect2>
+
+<sect2>
+<title>Primitive constructs</title>
+
 <para>
 Some operators will need to pass additional inputs to their subcommands.
 For example, in an arrow type supporting exceptions,
@@ -3697,7 +3705,7 @@ should have the form
 <screen>
 a (...(e,t1), ... tn) t
 </screen>
-where <replaceable>e</replaceable> is the polymorphic variable
+where <replaceable>e</replaceable> is a polymorphic variable
 (representing the environment)
 and <replaceable>ti</replaceable> are the types of the values on the stack,
 with <replaceable>t1</replaceable> being the <quote>top</quote>.
@@ -3711,21 +3719,37 @@ bracketA :: ... => a e b -> a (e,b) c -> a (e,c) d -> a e d
 runReader :: ... => a e c -> a' (e,State) c
 runState :: ... => a e c -> a' (e,State) (c,State)
 </screen>
-How can we supply the extra input required by the last two?
-We can define yet another operator, a counterpart of the monadic
-<literal>>>=</literal> operator:
+We can supply the extra input required by commands built with the last two
+by applying them to ordinary expressions, as in
+<screen>
+proc x -> do
+        s &lt;- ...
+        (|runReader (do { ... })|) s
+</screen>
+which adds <literal>s</literal> to the stack of inputs to the command
+built using <literal>runReader</literal>.
+</para>
+
+<para>
+The command versions of lambda abstraction and application are analogous to
+the expression versions.
+In particular, the beta and eta rules describe equivalences of commands.
+These three features (operators, lambda abstraction and application)
+are the core of the notation; everything else can be built using them,
+though the results would be somewhat clumsy.
+For example, we could simulate <literal>do</literal>-notation by defining
 <programlisting>
 bind :: Arrow a => a e b -> a (e,b) c -> a e c
 u `bind` f = returnA &&& u >>> f
+
+bind_ :: Arrow a => a e b -> a e c -> a e c
+u `bind_` f = u `bind` (arr fst >>> f)
+</programlisting>
+We could simulate <literal>do</literal> by defining
+<programlisting>
+cond :: ArrowChoice a => a e b -> a e b -> a (e,Bool) b
+cond f g = arr (\ (e,b) -> if b then Left e else Right e) >>> f ||| g
 </programlisting>
-and then build commands like
-<screen>
-proc x ->
-        (mkState -< x) `bind` (|runReader|) (do { ... })
-</screen>
-which uses the arrow <literal>mkState</literal> to create a state,
-and then provides this as an extra input to the command built using
-<literal>runReader</literal>.
 </para>
 
 </sect2>
@@ -4164,35 +4188,34 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value
       <para>A <literal>SPECIALIZE</literal> pragma for a function can
       be put anywhere its type signature could be put.</para>
 
-      <para>To get very fancy, you can also specify a named function
-      to use for the specialised value, as in:</para>
-
+<para>A <literal>SPECIALIZE</literal> has the effect of generating (a) a specialised
+version of the function and (b) a rewrite rule (see <xref linkend="rules">) that 
+rewrites a call to the un-specialised function into a call to the specialised
+one. You can, instead, provide your own specialised function and your own rewrite rule.
+For example, suppose that:
 <programlisting>
-{-# RULES "hammeredLookup" hammeredLookup = blah #-}
+  genericLookup :: Ord a => Table a b   -> a   -> b
+  intLookup     ::          Table Int b -> Int -> b
 </programlisting>
-
-      <para>where <literal>blah</literal> is an implementation of
-      <literal>hammerdLookup</literal> written specialy for
-      <literal>Widget</literal> lookups.  It's <emphasis>Your
+where <literal>intLookup</literal> is an implementation of <literal>genericLookup</literal>
+that works very fast for keys of type <literal>Int</literal>.  Then you can write the rule
+<programlisting>
+  {-# RULES "intLookup" genericLookup = intLookup #-}
+</programlisting>
+(see <xref linkend="rule-spec">). It is <emphasis>Your
       Responsibility</emphasis> to make sure that
-      <function>blah</function> really behaves as a specialised
-      version of <function>hammeredLookup</function>!!!</para>
-
-      <para>Note we use the <literal>RULE</literal> pragma here to
-      indicate that <literal>hammeredLookup</literal> applied at a
-      certain type should be replaced by <literal>blah</literal>.  See
-      <xref linkend="rules"> for more information on
-      <literal>RULES</literal>.</para>
+      <function>intLookup</function> really behaves as a specialised
+      version of <function>genericLookup</function>!!!</para>
 
       <para>An example in which using <literal>RULES</literal> for
       specialisation will Win Big:
 
 <programlisting>
-toDouble :: Real a => a -> Double
-toDouble = fromRational . toRational
+  toDouble :: Real a => a -> Double
+  toDouble = fromRational . toRational
 
-{-# RULES "toDouble/Int" toDouble = i2d #-}
-i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly
+  {-# RULES "toDouble/Int" toDouble = i2d #-}
+  i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly
 </programlisting>
 
       The <function>i2d</function> function is virtually one machine
@@ -4242,7 +4265,10 @@ of the pragma.
 
 <para>
 The programmer can specify rewrite rules as part of the source program
-(in a pragma).  GHC applies these rewrite rules wherever it can.
+(in a pragma).  GHC applies these rewrite rules wherever it can, provided (a) 
+the <option>-O</option> flag (<xref LinkEnd="options-optimise">) is on, 
+and (b) the <option>-frules-off</option> flag
+(<xref LinkEnd="options-f">) is not specified.
 </para>
 
 <para>
@@ -4690,7 +4716,7 @@ will fuse with one but not the other)
 
 </para>
 
-<para>
+ <para>
 So, for example, the following should generate no intermediate lists:
 
 <programlisting>