[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.sgml
index ff113aa..baf9ef7 100644 (file)
@@ -65,18 +65,6 @@ with GHC.
       </varlistentry>
 
       <varlistentry>
-       <term><option>-fwith</option>:</term>
-       <indexterm><primary><option>-fwith</option></primary></indexterm>
-       <listitem>
-         <para>This option enables the deprecated <literal>with</literal>
-         keyword for implicit parameters; it is merely provided for backwards
-         compatibility.
-          It is independent of the <option>-fglasgow-exts</option>
-          flag. </para>
-       </listitem>
-      </varlistentry>
-
-      <varlistentry>
        <term><option>-fno-monomorphism-restriction</option>:</term>
        <indexterm><primary><option>-fno-monomorphism-restriction</option></primary></indexterm>
        <listitem>
@@ -1241,17 +1229,19 @@ declarations.  So this is illegal:
 </programlisting>
 
 
-Reason: a value of type <literal>T</literal> must be represented as a pair
-of a dictionary for <literal>Ord t</literal> and a value of type <literal>t</literal>.
-That contradicts the idea that <literal>newtype</literal> should have no
-concrete representation.  You can get just the same efficiency and effect
-by using <literal>data</literal> instead of <literal>newtype</literal>.  If there is no
-overloading involved, then there is more of a case for allowing
-an existentially-quantified <literal>newtype</literal>, because the <literal>data</literal>
-because the <literal>data</literal> version does carry an implementation cost,
-but single-field existentially quantified constructors aren't much
-use.  So the simple restriction (no existential stuff on <literal>newtype</literal>)
-stands, unless there are convincing reasons to change it.
+Reason: a value of type <literal>T</literal> must be represented as a
+pair of a dictionary for <literal>Ord t</literal> and a value of type
+<literal>t</literal>.  That contradicts the idea that
+<literal>newtype</literal> should have no concrete representation.
+You can get just the same efficiency and effect by using
+<literal>data</literal> instead of <literal>newtype</literal>.  If
+there is no overloading involved, then there is more of a case for
+allowing an existentially-quantified <literal>newtype</literal>,
+because the <literal>data</literal> version does carry an
+implementation cost, but single-field existentially quantified
+constructors aren't much use.  So the simple restriction (no
+existential stuff on <literal>newtype</literal>) stands, unless there
+are convincing reasons to change it.
 
 
 </para>
@@ -1445,14 +1435,14 @@ With the <option>-fglasgow-exts</option> GHC lifts this restriction.
 
 <sect3><title>The context of a type signature</title>
 <para>
-Unlike Haskell 1.4, constraints in types do <emphasis>not</emphasis> have to be of
-the form <emphasis>(class type-variables)</emphasis>.  Thus, these type signatures
-are perfectly OK
+Unlike Haskell 98, constraints in types do <emphasis>not</emphasis> have to be of
+the form <emphasis>(class type-variable)</emphasis> or
+<emphasis>(class (type-variable type-variable ...))</emphasis>.  Thus,
+these type signatures are perfectly OK
 <programlisting>
-  f :: Eq (m a) => [m a] -> [m a]
   g :: Eq [a] => ...
+  g :: Ord (T a ()) => ...
 </programlisting>
-This choice recovers principal types, a property that Haskell 1.4 does not have.
 </para>
 <para>
 GHC imposes the following restrictions on the constraints in a type signature.
@@ -1463,7 +1453,7 @@ Consider the type:
 </programlisting>
 
 (Here, we write the "foralls" explicitly, although the Haskell source
-language omits them; in Haskell 1.4, all the free type variables of an
+language omits them; in Haskell 98, all the free type variables of an
 explicit source-language type signature are universally quantified,
 except for the class type variables in a class declaration.  However,
 in GHC, you can give the foralls if you want.  See <xref LinkEnd="universal-quantification">).
@@ -1607,9 +1597,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.
@@ -1694,7 +1684,7 @@ change that decision, at least for <literal>Main</literal>.)
 <title>Type synonyms in the instance head</title>
 
 <para>
-<emphasis>Unlike Haskell 1.4, instance heads may use type
+<emphasis>Unlike Haskell 98, instance heads may use type
 synonyms</emphasis>.  (The instance "head" is the bit after the "=>" in an instance decl.)
 As always, using a type synonym is just shorthand for
 writing the RHS of the type synonym definition.  For example:
@@ -2306,8 +2296,8 @@ the <literal>forall</literal> is on the left of a function arrrow.  As <literal>
 shows, the polymorphic type on the left of the function arrow can be overloaded.
 </para>
 <para>
-The functions <literal>f3</literal> and <literal>g3</literal> have rank-3 types;
-they have rank-2 types on the left of a function arrow.
+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
@@ -2319,7 +2309,7 @@ including an operational type class context, is legal:
 <listitem> <para> On the left of a function arrow </para> </listitem>
 <listitem> <para> On the right of a function arrow (see <xref linkend="hoist">) </para> </listitem>
 <listitem> <para> As the argument of a constructor, or type of a field, in a data type declaration. For
-example, any of the <literal>f1,f2,f3,g1,g2,g3</literal> above would be valid
+example, any of the <literal>f1,f2,f3,g1,g2</literal> above would be valid
 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>
@@ -3115,17 +3105,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:
@@ -3187,6 +3184,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. 
@@ -3292,6 +3295,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
 
@@ -3302,9 +3306,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
 
@@ -3327,14 +3330,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>
 
@@ -3791,7 +3794,7 @@ a new <literal>form</literal> keyword.
 Although only GHC implements arrow notation directly,
 there is also a preprocessor
 (available from the 
-<ulink url="http://www.haskell.org/arrows/">arrows web page></ulink>)
+<ulink url="http://www.haskell.org/arrows/">arrows web page</ulink>)
 that translates arrow notation into Haskell 98
 for use with other Haskell systems.
 You would still want to check arrow programs with GHC;
@@ -3946,7 +3949,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>
@@ -3971,7 +3974,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>
@@ -4257,7 +4268,71 @@ of the pragma.
 
 </sect2>
 
-
+    <sect2 id="unpack-pragma">
+      <title>UNPACK pragma</title>
+
+      <indexterm><primary>UNPACK</primary></indexterm>
+      
+      <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
+           {-# UNPACK #-} !Float
+</ProgramListing>
+
+      <para>will create a constructor <literal>T</literal> containing
+      two unboxed floats.  This may not always be an optimisation: if
+      the <Function>T</Function> constructor is scrutinised and the
+      floats passed to a non-strict function for example, they will
+      have to be reboxed (this is done automatically by the
+      compiler).</para>
+
+      <para>Unpacking constructor fields should only be used in
+      conjunction with <option>-O</option>, in order to expose
+      unfoldings to the compiler so the reboxing can be removed as
+      often as possible.  For example:</para>
+
+<ProgramListing>
+f :: T -&#62; Float
+f (T f1 f2) = f1 + f2
+</ProgramListing>
+
+      <para>The compiler will avoid reboxing <Function>f1</Function>
+      and <Function>f2</Function> by inlining <Function>+</Function>
+      on floats, but only when <option>-O</option> is on.</para>
+
+      <para>Any single-constructor data is eligible for unpacking; for
+      example</para>
+
+<ProgramListing>
+data T = T {-# UNPACK #-} !(Int,Int)
+</ProgramListing>
+
+      <para>will store the two <literal>Int</literal>s directly in the
+      <Function>T</Function> constructor, by flattening the pair.
+      Multi-level unpacking is also supported:</para>
+
+<ProgramListing>
+data T = T {-# UNPACK #-} !S
+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.  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
+      constructor field.</para>
+    </sect2>
 
 </sect1>