summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJoachim Breitner <mail@joachim-breitner.de>2013-08-19 12:24:18 +0200
committerAustin Seipp <aseipp@pobox.com>2013-08-29 20:46:43 -0500
commitd9f43665fefdc89f36150e991a431de23fc09ee1 (patch)
treeee6243ab5983d6cc39598f087d4186129eabb88c /docs
parent099f9542e8ad2e38196de2be928ccbfaee545b44 (diff)
downloadhaskell-d9f43665fefdc89f36150e991a431de23fc09ee1.tar.gz
Improve documentation of the new IncoherentInstances behaviour
Signed-off-by: Austin Seipp <aseipp@pobox.com>
Diffstat (limited to 'docs')
-rw-r--r--docs/users_guide/glasgow_exts.xml42
1 files changed, 28 insertions, 14 deletions
diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml
index 9056804779..38c9cc9928 100644
--- a/docs/users_guide/glasgow_exts.xml
+++ b/docs/users_guide/glasgow_exts.xml
@@ -4505,21 +4505,33 @@ and <option>-XIncoherentInstances</option>
<indexterm><primary>-XIncoherentInstances
</primary></indexterm>, as this section discusses. Both these
flags are dynamic flags, and can be set on a per-module basis, using
-an <literal>OPTIONS_GHC</literal> pragma if desired (<xref linkend="source-file-options"/>).</para>
+an <literal>LANGUAGE</literal> pragma if desired (<xref linkend="language-pragma"/>).</para>
<para>
The <option>-XOverlappingInstances</option> flag instructs GHC to loosen
the instance resolution described in <xref linkend="instance-resolution"/>, by
-allowing more than one instance to match, <emphasis>provided there is a most specific one</emphasis>.
+allowing more than one instance to match, <emphasis>provided there is a most
+specific one</emphasis>. The <option>-XIncoherentInstances</option> flag
+further loosens the resolution, by allowing more than one instance to match,
+irespective of whether there is a most specific one.
+</para>
+
+<para>
For example, consider
<programlisting>
- instance context1 => C Int a where ... -- (A)
+ instance context1 => C Int b where ... -- (A)
instance context2 => C a Bool where ... -- (B)
- instance context3 => C Int [a] where ... -- (C)
+ instance context3 => C a [b] where ... -- (C)
instance context4 => C Int [Int] where ... -- (D)
</programlisting>
-The constraint <literal>C Int [Int]</literal> matches instances (A),
-(C) and (D), but the last is more specific, and hence is chosen. If there is no
-most-specific match, the program is rejected.
+compiled with <option>-XOverlappingInstances</option> enabled. The constraint
+<literal>C Int [Int]</literal> matches instances (A), (C) and (D), but the last
+is more specific, and hence is chosen.
+</para>
+<para>If (D) did not exist then (A) and (C) would still be matched, but neither is
+most specific. In that case, the program would be rejected even with
+<option>-XOverlappingInstances</option>. With
+<option>-XIncoherentInstances</option> enabled, it would be accepted and (A) or
+(C) would be chosen arbitrarily.
</para>
<para>
An instance declaration is <emphasis>more specific</emphasis> than another iff
@@ -4534,17 +4546,15 @@ However, GHC is conservative about committing to an overlapping instance. For e
f x = ...
</programlisting>
Suppose that from the RHS of <literal>f</literal> we get the constraint
-<literal>C Int [b]</literal>. But
+<literal>C b [b]</literal>. But
GHC does not commit to instance (C), because in a particular
call of <literal>f</literal>, <literal>b</literal> might be instantiate
to <literal>Int</literal>, in which case instance (D) would be more specific still.
So GHC rejects the program.</para>
<para>
-If, however, you add the flag <option>-XIncoherentInstances</option>,
-GHC will instead pick (C), without complaining about
-the problem of subsequent instantiations. In general, the flag
-<option>-XIncoherentInstances</option> will cause GHC to ignore an instance if
-there is another instance that matches the constraint.
+If, however, you add the flag <option>-XIncoherentInstances</option> when
+compiling the module that contians (D), GHC will instead pick (C), without
+complaining about the problem of subsequent instantiations.
</para>
<para>
Notice that we gave a type signature to <literal>f</literal>, so GHC had to
@@ -4554,7 +4564,7 @@ it instead. In this case, GHC will refrain from
simplifying the constraint <literal>C Int [b]</literal> (for the same reason
as before) but, rather than rejecting the program, it will infer the type
<programlisting>
- f :: C Int [b] => [b] -> [b]
+ f :: C b [b] => [b] -> [b]
</programlisting>
That postpones the question of which instance to pick to the
call site for <literal>f</literal>
@@ -4652,6 +4662,10 @@ some other constraint. But if the instance declaration was compiled with
<option>-XIncoherentInstances</option>, GHC will skip the "does-it-unify?"
check for that declaration.
</para></listitem>
+<listitem><para>
+If two instance declarations are matched and either is compiled with
+<option>-XIncoherentInstances</option>, then that declaration is ignored.
+</para></listitem>
</itemizedlist>
These rules make it possible for a library author to design a library that relies on
overlapping instances without the library client having to know.