[project @ 2005-03-02 18:15:36 by simonpj]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.xml
index ff46b39..a25d42e 100644 (file)
@@ -925,18 +925,34 @@ Nevertheless, they can be useful when defining "phantom types".</para>
 </sect3>
 
 <sect3 id="infix-tycons">
-<title>Infix type constructors</title>
+<title>Infix type constructors and classes</title>
 
 <para>
-GHC allows type constructors to be operators, and to be written infix, very much 
+GHC allows type constructors and classes to be operators, and to be written infix, very much 
 like expressions.  More specifically:
 <itemizedlist>
 <listitem><para>
-  A type constructor can be an operator, beginning with a colon; e.g. <literal>:*:</literal>.
+  A type constructor or class can be an operator, beginning with a colon; e.g. <literal>:*:</literal>.
   The lexical syntax is the same as that for data constructors.
   </para></listitem>
 <listitem><para>
-  Types can be written infix.  For example <literal>Int :*: Bool</literal>.  
+  Data type and type-synonym declarations can be written infix, parenthesised
+  if you want further arguments.  E.g.
+<screen>
+  data a :*: b = Foo a b
+  type a :+: b = Either a b
+  class a :=: b where ...
+
+  data (a :**: b) x = Baz a b x
+  type (a :++: b) y = Either (a,b) y
+</screen>
+  </para></listitem>
+<listitem><para>
+  Types, and class constraints, can be written infix.  For example
+  <screen>
+       x :: Int :*: Bool
+        f :: (a :=: b) => a -> b
+  </screen>
   </para></listitem>
 <listitem><para>
   Back-quotes work
@@ -944,7 +960,7 @@ like expressions.  More specifically:
   <literal>Int `a` Bool</literal>.  Similarly, parentheses work the same; e.g.  <literal>(:*:) Int Bool</literal>.
   </para></listitem>
 <listitem><para>
-  Fixities may be declared for type constructors just as for data constructors.  However,
+  Fixities may be declared for type constructors, or classes, just as for data constructors.  However,
   one cannot distinguish between the two in a fixity declaration; a fixity declaration
   sets the fixity for a data constructor and the corresponding type constructor.  For example:
 <screen>
@@ -958,13 +974,6 @@ like expressions.  More specifically:
   Function arrow is <literal>infixr</literal> with fixity 0.  (This might change; I'm not sure what it should be.)
   </para></listitem>
 <listitem><para>
-  Data type and type-synonym declarations can be written infix.  E.g.
-<screen>
-  data a :*: b = Foo a b
-  type a :+: b = Either a b
-</screen>
-  </para></listitem>
-<listitem><para>
   The only thing that differs between operators in types and operators in expressions is that
   ordinary non-constructor operators, such as <literal>+</literal> and <literal>*</literal>
   are not allowed in types. Reason: the uniform thing to do would be to make them type
@@ -1722,7 +1731,7 @@ means
 <sect2 id="instance-decls">
 <title>Instance declarations</title>
 
-<sect3>
+<sect3 id="instance-overlap">
 <title>Overlapping instances</title>
 <para>
 In general, <emphasis>GHC requires that that it be unambiguous which instance
@@ -1778,6 +1787,11 @@ So GHC rejects the program.  If you add the flag <option>-fallow-incoherent-inst
 GHC will instead pick (C), without complaining about 
 the problem of subsequent instantiations.
 </para>
+<para>
+Because overlaps are checked and reported lazily, as described above, you need
+the <option>-fallow-overlapping-instances</option> in the module that <emphasis>calls</emphasis> 
+the overloaded function, rather than in the module that <emphasis>defines</emphasis> it.</para>
+
 </sect3>
 
 <sect3>
@@ -2664,22 +2678,19 @@ for rank-2 types.
 </title>
 
 <para>
-A <emphasis>pattern type signature</emphasis> can introduce a <emphasis>scoped type
-variable</emphasis>.  For example
-</para>
-
-<para>
-
+A <emphasis>lexically scoped type variable</emphasis> can be bound by:
+<itemizedlist>
+<listitem><para>A declaration type signature (<xref linkend="decl-type-sigs"/>)</para></listitem>
+<listitem><para>A pattern type signature (<xref linkend="pattern-type-sigs"/>)</para></listitem>
+<listitem><para>A result type signature (<xref linkend="result-type-sigs"/>)</para></listitem>
+</itemizedlist>
+For example:
 <programlisting>
 f (xs::[a]) = ys ++ ys
            where
               ys :: [a]
               ys = reverse xs
 </programlisting>
-
-</para>
-
-<para>
 The pattern <literal>(xs::[a])</literal> includes a type signature for <varname>xs</varname>.
 This brings the type variable <literal>a</literal> into scope; it scopes over
 all the patterns and right hand sides for this equation for <function>f</function>.
@@ -2687,8 +2698,6 @@ In particular, it is in scope at the type signature for <varname>y</varname>.
 </para>
 
 <para>
- Pattern type signatures are completely orthogonal to ordinary, separate
-type signatures.  The two can be used independently or together.
 At ordinary type signatures, such as that for <varname>ys</varname>, any type variables
 mentioned in the type signature <emphasis>that are not in scope</emphasis> are
 implicitly universally quantified.  (If there are no type variables in
@@ -2711,10 +2720,10 @@ So much for the basic idea.  Here are the details.
 </para>
 
 <sect3>
-<title>What a pattern type signature means</title>
+<title>What a scoped type variable means</title>
 <para>
-A type variable brought into scope by a pattern type signature is simply
-the name for a type.   The restriction they express is that all occurrences
+A lexically-scoped type variable is simply
+the name for a type.   The restriction it expresses is that all occurrences
 of the same name mean the same type.  For example:
 <programlisting>
   f :: [Int] -> Int -> Int
@@ -2884,7 +2893,33 @@ scope over the methods defined in the <literal>where</literal> part.  For exampl
 
 </sect3>
 
-<sect3>
+<sect3 id="decl-type-sigs">
+<title>Declaration type signatures</title>
+<para>A declaration type signature that has <emphasis>explicit</emphasis>
+quantification (using <literal>forall</literal>) brings into scope the
+explicitly-quantified
+type variables, in the definition of the named function(s).  For example:
+<programlisting>
+  f :: forall a. [a] -> [a]
+  f (x:xs) = xs ++ [ x :: a ]
+</programlisting>
+The "<literal>forall a</literal>" brings "<literal>a</literal>" into scope in
+the definition of "<literal>f</literal>".
+</para>
+<para>This only happens if the quantification in <literal>f</literal>'s type
+signature is explicit.  For example:
+<programlisting>
+  g :: [a] -> [a]
+  g (x:xs) = xs ++ [ x :: a ]
+</programlisting>
+This program will be rejected, because "<literal>a</literal>" does not scope
+over the definition of "<literal>f</literal>", so "<literal>x::a</literal>"
+means "<literal>x::forall a. a</literal>" by Haskell's usual implicit
+quantification rules.
+</para>
+</sect3>
+
+<sect3 id="pattern-type-sigs">
 <title>Where a pattern type signature can occur</title>
 
 <para>
@@ -3001,10 +3036,12 @@ in <literal>f4</literal>'s scope.
 </listitem>
 </itemizedlist>
 </para>
+<para>Pattern type signatures are completely orthogonal to ordinary, separate
+type signatures.  The two can be used independently or together.</para>
 
 </sect3>
 
-<sect3>
+<sect3 id="result-type-sigs">
 <title>Result type signatures</title>
 
 <para>