[project @ 2004-02-25 13:57:38 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.sgml
index b51278b..8312aee 100644 (file)
@@ -2,17 +2,19 @@
 <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.  To use them, you'll need to give a <option>-fglasgow-exts</option>
-<indexterm><primary>-fglasgow-exts option</primary></indexterm> option.
+the language.  They are all enabled by options; by default GHC
+understands only plain Haskell 98.
 </para>
 
 <para>
-Virtually all of the Glasgow extensions serve to give you access to
-the underlying facilities with which we implement Haskell.  Thus, you
-can get at the Raw Iron, if you are willing to write some non-standard
-code at a more primitive level.  You need not be &ldquo;stuck&rdquo; on
-performance because of the implementation costs of Haskell's
-&ldquo;high-level&rdquo; features&mdash;you can always code &ldquo;under&rdquo; them.  In an extreme case, you can write all your time-critical code in C, and then just glue it together with Haskell!
+Some of the Glasgow extensions serve to give you access to the
+underlying facilities with which we implement Haskell.  Thus, you can
+get at the Raw Iron, if you are willing to write some non-portable
+code at a more primitive level.  You need not be &ldquo;stuck&rdquo;
+on performance because of the implementation costs of Haskell's
+&ldquo;high-level&rdquo; features&mdash;you can always code
+&ldquo;under&rdquo; them.  In an extreme case, you can write all your
+time-critical code in C, and then just glue it together with Haskell!
 </para>
 
 <para>
@@ -20,8 +22,8 @@ Before you get too carried away working at the lowest level (e.g.,
 sloshing <literal>MutableByteArray&num;</literal>s around your
 program), you may wish to check if there are libraries that provide a
 &ldquo;Haskellised veneer&rdquo; over the features you want.  The
-separate libraries documentation describes all the libraries that come
-with GHC.
+separate <ulink url="../libraries/index.html">libraries
+documentation</ulink> describes all the libraries that come with GHC.
 </para>
 
 <!-- LANGUAGE OPTIONS -->
@@ -35,10 +37,38 @@ with GHC.
     <indexterm><primary>extensions</primary><secondary>options controlling</secondary>
     </indexterm>
 
-    <para> These flags control what variation of the language are
+    <para>These flags control what variation of the language are
     permitted.  Leaving out all of them gives you standard Haskell
     98.</para>
 
+    <para>NB. turning on an option that enables special syntax
+    <emphasis>might</emphasis> cause working Haskell 98 code to fail
+    to compile, perhaps because it uses a variable name which has
+    become a reserved word.  So, together with each option below, we
+    list the special syntax which is enabled by this option.  We use
+    notation and nonterminal names from the Haskell 98 lexical syntax
+    (see the Haskell 98 Report).  There are two classes of special
+    syntax:</para>
+
+    <itemizedlist>
+      <listitem>
+       <para>New reserved words and symbols: character sequences
+        which are no longer available for use as identifiers in the
+        program.</para>
+      </listitem>
+      <listitem>
+       <para>Other special syntax: sequences of characters that have
+       a different meaning when this particular option is turned
+       on.</para>
+      </listitem>
+    </itemizedlist>
+
+    <para>We are only listing syntax changes here that might affect
+    existing working programs (i.e. "stolen" syntax).  Many of these
+    extensions will also enable new context-free syntax, but in all
+    cases programs written to use the new syntax would not be
+    compilable without the option enabled.</para>
+
     <variablelist>
 
       <varlistentry>
@@ -49,6 +79,19 @@ with GHC.
           Haskell 98 described in <xref
           linkend="ghc-language-features">, except where otherwise
           noted. </para>
+
+         <para>New reserved words: <literal>forall</literal> (only in
+         types), <literal>mdo</literal>.</para>
+
+         <para>Other syntax stolen:
+             <replaceable>varid</replaceable>{<literal>&num;</literal>},
+             <replaceable>char</replaceable><literal>&num;</literal>,      
+             <replaceable>string</replaceable><literal>&num;</literal>,    
+             <replaceable>integer</replaceable><literal>&num;</literal>,    
+             <replaceable>float</replaceable><literal>&num;</literal>,    
+             <replaceable>float</replaceable><literal>&num;&num;</literal>,    
+             <literal>(&num;</literal>, <literal>&num;)</literal>,         
+             <literal>|)</literal>, <literal>{|</literal>.</para>
        </listitem>
       </varlistentry>
 
@@ -61,6 +104,8 @@ with GHC.
          Haskell 98 Foreign Function Interface Addendum plus deprecated
          syntax of previous versions of the FFI for backwards
          compatibility.</para> 
+
+         <para>New reserved words: <literal>foreign</literal>.</para>
        </listitem>
       </varlistentry>
 
@@ -103,6 +148,14 @@ with GHC.
        <listitem>
          <para>See <xref LinkEnd="arrow-notation">.  Independent of
           <option>-fglasgow-exts</option>.</para>
+
+         <para>New reserved words/symbols: <literal>rec</literal>,
+         <literal>proc</literal>, <literal>-&lt;</literal>,
+         <literal>&gt;-</literal>, <literal>-&lt;&lt;</literal>,
+         <literal>&gt;&gt;-</literal>.</para>
+
+         <para>Other syntax stolen: <literal>(|</literal>,
+         <literal>|)</literal>.</para>
        </listitem>
       </varlistentry>
 
@@ -149,6 +202,12 @@ with GHC.
          <para>Enables Template Haskell (see <xref
          linkend="template-haskell">).  Currently also implied by
          <option>-fglasgow-exts</option>.</para>
+
+         <para>Syntax stolen: <literal>[|</literal>,
+         <literal>[e|</literal>, <literal>[p|</literal>,
+         <literal>[d|</literal>, <literal>[t|</literal>,
+         <literal>$(</literal>,
+         <literal>$<replaceable>varid</replaceable></literal>.</para>
        </listitem>
       </varlistentry>
 
@@ -158,6 +217,10 @@ with GHC.
          <para>Enables implicit parameters (see <xref
          linkend="implicit-parameters">).  Currently also implied by 
          <option>-fglasgow-exts</option>.</para>
+
+         <para>Syntax stolen:
+         <literal>?<replaceable>varid</replaceable></literal>,
+         <literal>%<replaceable>varid</replaceable></literal>.</para>
        </listitem>
       </varlistentry>
 
@@ -1597,9 +1660,9 @@ declarations
   instance context2 => C type2 where ...
 </programlisting>
 
-
-"overlap" if <literal>type1</literal> and <literal>type2</literal> unify
-
+"overlap" if <literal>type1</literal> and <literal>type2</literal> unify.
+</para>
+<para>
 However, if you give the command line option
 <option>-fallow-overlapping-instances</option><indexterm><primary>-fallow-overlapping-instances
 option</primary></indexterm> then overlapping instance declarations are permitted.
@@ -3105,17 +3168,24 @@ where
   <literal>S</literal> is a type constructor, 
 </para></listitem>
 <listitem><para>
-  <literal>t1...tk</literal> are types,
+  The <literal>t1...tk</literal> are types,
 </para></listitem>
 <listitem><para>
-  <literal>vk+1...vn</literal> are type variables which do not occur in any of
+  The <literal>vk+1...vn</literal> are type variables which do not occur in any of
   the <literal>ti</literal>, and
 </para></listitem>
 <listitem><para>
-  the <literal>ci</literal> are partial applications of
+  The <literal>ci</literal> are partial applications of
   classes of the form <literal>C t1'...tj'</literal>, where the arity of <literal>C</literal>
   is exactly <literal>j+1</literal>.  That is, <literal>C</literal> lacks exactly one type argument.
 </para></listitem>
+<listitem><para>
+  None of the <literal>ci</literal> is <literal>Read</literal>, <literal>Show</literal>, 
+               <literal>Typeable</literal>, or <literal>Data</literal>.  These classes
+               should not "look through" the type or its constructor.  You can still
+               derive these classes for a newtype, but it happens in the usual way, not 
+               via this new mechanism.  
+</para></listitem>
 </itemizedlist>
 Then, for each <literal>ci</literal>, the derived instance
 declaration is:
@@ -3177,6 +3247,12 @@ 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>" (Proc Haskell Workshop 2002).
+The details of the Template Haskell design are still in flux.  Make sure you
+consult the <ulink url="http://www.haskell.org/ghc/docs/latest/html/libraries/index.html">online library reference material</ulink> 
+(search for the type ExpQ).
+[Temporary: many changes to the original design are described in 
+      <ulink url="http://research.microsoft.com/~simonpj/tmp/notes2.ps">"http://research.microsoft.com/~simonpj/tmp/notes2.ps"</ulink>.
+Not all of these changes are in GHC 6.2.]
 </para>
 
 <para> The first example from that paper is set out below as a worked example to help get you started. 
@@ -3282,6 +3358,7 @@ Tim Sheard is going to expand it.)
   First cut and paste the two modules below into "Main.hs" and "Printf.hs":</para>
 
 <programlisting>
+
 {- Main.hs -}
 module Main where
 
@@ -3292,9 +3369,8 @@ import Printf ( pr )
 -- generated at compile time by "pr" and splices it into
 -- the argument of "putStrLn".
 main = putStrLn ( $(pr "Hello") )
-</programlisting>
 
-<programlisting>
+
 {- Printf.hs -}
 module Printf where
 
@@ -3317,14 +3393,14 @@ parse s   = [ L s ]
 -- Generate Haskell source code from a parsed representation
 -- of the format string.  This code will be spliced into
 -- the module which calls "pr", at compile time.
-gen :: [Format] -> Expr
+gen :: [Format] -> ExpQ
 gen [D]   = [| \n -> show n |]
 gen [S]   = [| \s -> s |]
-gen [L s] = string s
+gen [L s] = stringE s
 
 -- Here we generate the Haskell code for the splice
 -- from an input format string.
-pr :: String -> Expr
+pr :: String -> ExpQ
 pr s      = gen (parse s)
 </programlisting>
 
@@ -3936,7 +4012,7 @@ Assertion failures can be caught, see the documentation for the
 
       <para>The DEPRECATED pragma lets you specify that a particular
       function, class, or type, is deprecated.  There are two
-      forms.</para>
+      forms.
 
       <itemizedlist>
        <listitem>
@@ -3961,7 +4037,15 @@ Assertion failures can be caught, see the documentation for the
           message.</para>
        </listitem>
       </itemizedlist>
-
+      Any use of the deprecated item, or of anything from a deprecated
+      module, will be flagged with an appropriate message.  However,
+      deprecations are not reported for
+      (a) uses of a deprecated function within its defining module, and
+      (b) uses of a deprecated function in an export list.
+      The latter reduces spurious complaints within a library
+      in which one module gathers together and re-exports 
+      the exports of several others.
+      </para>
       <para>You can suppress the warnings with the flag
       <option>-fno-warn-deprecations</option>.</para>
     </sect2>
@@ -4250,12 +4334,12 @@ of the pragma.
     <sect2 id="unpack-pragma">
       <title>UNPACK pragma</title>
 
-      <indexterm><primary>UNPACK</primary> </indexterm>
+      <indexterm><primary>UNPACK</primary></indexterm>
       
-      <para>There is another use for the <literal>UNPACK</literal>
-      pragma: to indicate that the compiler should unpack the contents
-      of a constructor field into the constructor itself, removing a
-      level of indirection.  For example:</para>
+      <para>The <literal>UNPACK</literal> indicates to the compiler
+      that it should unpack the contents of a constructor field into
+      the constructor itself, removing a level of indirection.  For
+      example:</para>
 
 <ProgramListing>
 data T = T {-# UNPACK #-} !Float
@@ -4300,7 +4384,12 @@ data S = S {-# UNPACK #-} !Int {-# UNPACK #-} !Int
 </ProgramListing>
 
       <para>will store two unboxed <literal>Int&num;</literal>s
-      directly in the <Function>T</Function> constructor.</para>
+      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