[project @ 2001-08-27 11:45:23 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.sgml
index c3b06ec..5c80925 100644 (file)
@@ -96,6 +96,13 @@ Executive summary of our extensions:
     </varlistentry>
 
     <varlistentry>
+      <term>Data types with no constructors</term>
+      <listitem>
+       <para>See <xref LinkEnd="nullary-types">.</para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term>Parallel list comprehensions</term>
       <listitem>
        <para>An extension to the list comprehension syntax to support
@@ -135,6 +142,9 @@ Executive summary of our extensions:
     <varlistentry>
       <term>Generic classes:</term>
       <listitem>
+       <para>(Note: support for generic classes is currently broken
+        in GHC 5.02).</para>
+
        <para>Generic class declarations allow you to define a class
         whose methods say how to work over an arbitrary data type.
         Then it's really easy to make any new type into an instance of
@@ -415,6 +425,24 @@ The libraries documentatation gives more details on all these
 </sect1>
 
 
+<sect1 id="nullary-types">
+<title>Data types with no constructors</title>
+
+<para>With the <option>-fglasgow-exts</option> flag, GHC lets you declare
+a data type with no constructors.  For example:</para>
+<programlisting>
+  data S      -- S :: *
+  data T a    -- T :: * -> *
+</programlisting>
+<para>Syntactically, the declaration lacks the "= constrs" part.  The 
+type can be parameterised, but only over ordinary types, of kind *; since
+Haskell does not have kind signatures, you cannot parameterise over higher-kinded
+types.</para>
+
+<para>Such data types have only one value, namely bottom.
+Nevertheless, they can be useful when defining "phantom types".</para>
+</sect1>
+
 <sect1 id="pattern-guards">
 <title>Pattern guards</title>
 
@@ -592,111 +620,6 @@ qualifier list has just one element, a boolean expression.
 
   </sect1>
 
-  <sect1 id="sec-ffi">
-    <title>The foreign interface</title>
-
-    <para>The foreign interface consists of the following components:</para>
-
-    <itemizedlist>
-      <listitem>
-       <para>The Foreign Function Interface language specification
-       (included in this manual, in <xref linkend="ffi">).</para>  
-       You must use the <option>-fglasgow-exts</option> command-line option
-       to make GHC understand the <literal>foreign</literal> declarations
-       defined by the FFI.
-      </listitem>
-
-      <listitem>
-       <para>The <literal>Foreign</literal> module (see <xref
-       linkend="sec-Foreign">) collects together several interfaces
-       which are useful in specifying foreign language
-       interfaces, including the following:</para>
-
-       <itemizedlist>
-         <listitem>
-           <para>The <literal>ForeignObj</literal> module (see <xref
-           linkend="sec-ForeignObj">), for managing pointers from
-           Haskell into the outside world.</para>
-         </listitem>
-      
-         <listitem>
-           <para>The <literal>StablePtr</literal> module (see <xref
-           linkend="sec-stable-pointers">), for managing pointers
-           into Haskell from the outside world.</para>
-         </listitem>
-      
-         <listitem>
-           <para>The <literal>CTypes</literal> module (see <xref
-           linkend="sec-CTypes">) gives Haskell equivalents for the
-           standard C datatypes, for use in making Haskell bindings
-           to existing C libraries.</para>
-         </listitem>
-      
-         <listitem>
-           <para>The <literal>CTypesISO</literal> module (see <xref
-           linkend="sec-CTypesISO">) gives Haskell equivalents for C
-           types defined by the ISO C standard.</para>
-         </listitem>
-      
-         <listitem>
-           <para>The <literal>Storable</literal> library, for
-           primitive marshalling of data types between Haskell and
-           the foreign language.</para>
-         </listitem>
-       </itemizedlist>
-
-      </listitem>
-    </itemizedlist>
-
-<para>The following sections also give some hints and tips on the use
-of the foreign function interface in GHC.</para>
-
-<sect2 id="glasgow-foreign-headers">
-<title>Using function headers
-</title>
-
-<para>
-<indexterm><primary>C calls, function headers</primary></indexterm>
-</para>
-
-<para>
-When generating C (using the <option>-fvia-C</option> directive), one can assist the
-C compiler in detecting type errors by using the <option>-&num;include</option> directive
-(<xref linkend="options-C-compiler">) to provide <filename>.h</filename> files containing function headers.
-</para>
-
-<para>
-For example,
-</para>
-
-<para>
-
-<programlisting>
-#include "HsFFI.h"
-
-void         initialiseEFS (HsInt size);
-HsInt        terminateEFS (void);
-HsForeignObj emptyEFS(void);
-HsForeignObj updateEFS (HsForeignObj a, HsInt i, HsInt x);
-HsInt        lookupEFS (HsForeignObj a, HsInt i);
-</programlisting>
-</para>
-
-      <para>The types <literal>HsInt</literal>,
-      <literal>HsForeignObj</literal> etc. are described in <xref
-      linkend="sec-mapping-table">.</para>
-
-      <para>Note that this approach is only
-      <emphasis>essential</emphasis> for returning
-      <literal>float</literal>s (or if <literal>sizeof(int) !=
-      sizeof(int *)</literal> on your architecture) but is a Good
-      Thing for anyone who cares about writing solid code.  You're
-      crazy not to do it.</para>
-
-</sect2>
-
-</sect1>
-
 <sect1 id="multi-param-type-classes">
 <title>Multi-parameter type classes
 </title>
@@ -2199,10 +2122,10 @@ must be a list of things of some type <literal>a</literal>; and that <literal>y<
 must have this same type.  The type signature on the expression <literal>(head xs)</literal>
 specifies that this expression must have the same type <literal>a</literal>.
 <emphasis>There is no requirement that the type named by "<literal>a</literal>" is
-in fact a type variable.  Indeed, in this case, the type named by "<literal>a</literal>" is
+in fact a type variable</emphasis>.  Indeed, in this case, the type named by "<literal>a</literal>" is
 <literal>Int</literal>.  (This is a slight liberalisation from the original rather complex
 rules, which specified that a pattern-bound type variable should be universally quantified.)
-For example, all of these are legal:
+For example, all of these are legal:</para>
 
 <programlisting>
   t (x::a) (y::a) = x+y*2
@@ -2221,7 +2144,6 @@ For example, all of these are legal:
   w (x::a) = x                  -- a unifies with [b]
 </programlisting>
 
-</para>
 </sect2>
 
 <sect2>
@@ -2509,15 +2431,28 @@ would get a monomorphic type.
 
 </sect1>
 
-<sect1 id="pragmas">
-<title>Pragmas
-</title>
+  <sect1 id="pragmas">
+    <title>Pragmas</title>
 
-<para>
-GHC supports several pragmas, or instructions to the compiler placed
-in the source code.  Pragmas don't affect the meaning of the program,
-but they might affect the efficiency of the generated code.
-</para>
+    <indexterm><primary>pragma</primary></indexterm>
+
+    <para>GHC supports several pragmas, or instructions to the
+    compiler placed in the source code.  Pragmas don't normally affect
+    the meaning of the program, but they might affect the efficiency
+    of the generated code.</para>
+
+    <para>Pragmas all take the form
+
+<literal>{-# <replaceable>word</replaceable> ... #-}</literal>  
+
+    where <replaceable>word</replaceable> indicates the type of
+    pragma, and is followed optionally by information specific to that
+    type of pragma.  Case is ignored in
+    <replaceable>word</replaceable>.  The various values for
+    <replaceable>word</replaceable> that GHC understands are described
+    in the following sections; any pragma encountered with an
+    unrecognised <replaceable>word</replaceable> is (silently)
+    ignored.</para>
 
 <sect2 id="inline-pragma">
 <title>INLINE pragma
@@ -2589,17 +2524,23 @@ For example, in GHC's own <literal>UniqueSupply</literal> monad code, we have:
 <title>NOINLINE pragma
 </title>
 
-<para>
 <indexterm><primary>NOINLINE pragma</primary></indexterm>
-<indexterm><primary>pragma, NOINLINE</primary></indexterm>
-</para>
+<indexterm><primary>pragma</primary><secondary>NOINLINE</secondary></indexterm>
+<indexterm><primary>NOTINLINE pragma</primary></indexterm>
+<indexterm><primary>pragma</primary><secondary>NOTINLINE</secondary></indexterm>
 
 <para>
-The <literal>NOINLINE</literal> pragma does exactly what you'd expect: it stops the
-named function from being inlined by the compiler.  You shouldn't ever
-need to do this, unless you're very cautious about code size.
+The <literal>NOINLINE</literal> pragma does exactly what you'd expect:
+it stops the named function from being inlined by the compiler.  You
+shouldn't ever need to do this, unless you're very cautious about code
+size.
 </para>
 
+<para><literal>NOTINLINE</literal> is a synonym for
+<literal>NOINLINE</literal> (<literal>NOTINLINE</literal> is specified
+by Haskell 98 as the standard way to disable inlining, so it should be
+used if you want your code to be portable).</para>
+
 </sect2>
 
     <sect2 id="specialize-pragma">
@@ -2729,6 +2670,42 @@ The RULES pragma lets you specify rewrite rules.  It is described in
 
 </sect2>
 
+<sect2 id="deprecated-pragma">
+<title>DEPRECATED pragma</title>
+
+<para>
+The DEPRECATED pragma lets you specify that a particular function, class, or type, is deprecated.  
+There are two forms.  
+</para>
+<itemizedlist>
+<listitem><para>
+You can deprecate an entire module thus:</para>
+<programlisting>
+   module Wibble {-# DEPRECATED "Use Wobble instead" #-} where
+     ...
+</programlisting>
+<para>
+When you compile any module that import <literal>Wibble</literal>, GHC will print
+the specified message.</para>
+</listitem>
+
+<listitem>
+<para>
+You can deprecate a function, class, or type, with the following top-level declaration:
+</para>
+<programlisting>
+   {-# DEPRECATED f, C, T "Don't use these" #-}
+</programlisting>
+<para>
+When you compile any module that imports and uses any of the specifed entities, 
+GHC will print the specified message.
+</para>
+</listitem>
+</itemizedlist>
+<para>You can suppress the warnings with the flag <option>-fno-warn-deprecations</option>.</para>
+
+</sect2>
+
 </sect1>
 
 <sect1 id="rewrite-rules">
@@ -3034,14 +3011,14 @@ The following are good producers:
  <function>++</function>
 </para>
 </listitem>
-<listitem>
 
+<listitem>
 <para>
  <function>map</function>
 </para>
 </listitem>
-<listitem>
 
+<listitem>
 <para>
  <function>filter</function>
 </para>
@@ -3091,8 +3068,14 @@ The following are good consumers:
  <function>++</function> (on its first argument)
 </para>
 </listitem>
+
 <listitem>
+<para>
+ <function>foldr</function>
+</para>
+</listitem>
 
+<listitem>
 <para>
  <function>map</function>
 </para>
@@ -3286,6 +3269,9 @@ program even if fusion doesn't happen.  More rules in <filename>PrelList.lhs</fi
 <sect1 id="generic-classes">
 <title>Generic classes</title>
 
+    <para>(Note: support for generic classes is currently broken in
+    GHC 5.02).</para>
+
 <para>
 The ideas behind this extension are described in detail in "Derivable type classes",
 Ralf Hinze and Simon Peyton Jones, Haskell Workshop, Montreal Sept 2000, pp94-105.
@@ -3441,10 +3427,10 @@ So this too is illegal:
 <programlisting>
   class Foo a where
     op1 :: a -> Bool
-    op {| a :*: b |} (Inl x) = True
+    op1 {| a :*: b |} (x :*: y) = True
 
     op2 :: a -> Bool
-    op {| p :*: q |} (Inr y) = False
+    op2 {| p :*: q |} (x :*: y) = False
 </programlisting>
 (The reason for this restriction is that we gather all the equations for a particular type consructor
 into a single generic instance declaration.)