diff options
Diffstat (limited to 'docs/users_guide/exts/instances.rst')
-rw-r--r-- | docs/users_guide/exts/instances.rst | 84 |
1 files changed, 6 insertions, 78 deletions
diff --git a/docs/users_guide/exts/instances.rst b/docs/users_guide/exts/instances.rst index 4dfb7e1ecb..01655bb05b 100644 --- a/docs/users_guide/exts/instances.rst +++ b/docs/users_guide/exts/instances.rst @@ -173,27 +173,8 @@ syntactically allowed. Some further various observations about this grammar: instance, ``instance (C a)`` is accepted, as is ``instance forall a. (C a)``. .. _instance-rules: - -Relaxed rules for instance contexts -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -In Haskell 98, the class constraints in the context of the instance -declaration must be of the form ``C a`` where ``a`` is a type variable -that occurs in the head. - -The :extension:`FlexibleContexts` extension relaxes this rule, as well as relaxing -the corresponding rule for type signatures (see -:ref:`flexible-contexts`). Specifically, :extension:`FlexibleContexts`, allows -(well-kinded) class constraints of form ``(C t1 ... tn)`` in the context -of an instance declaration. - -Notice that the extension does not affect equality constraints in an instance -context; they are permitted by :extension:`TypeFamilies` or :extension:`GADTs`. - -However, the instance declaration must still conform to the rules for -instance termination: see :ref:`instance-termination`. - .. _instance-termination: +.. _undecidable-instances: Instance termination rules ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -284,60 +265,6 @@ because the derived instance conforms to the above rules. -A useful idiom permitted by the above rules is as follows. If one allows -overlapping instance declarations then it's quite convenient to have a -"default instance" declaration that applies if something more specific -does not: - -:: - - instance C a where - op = ... -- Default - -.. _undecidable-instances: - -Undecidable instances -~~~~~~~~~~~~~~~~~~~~~ - -.. index:: - single: -XUndecidableInstances - -Sometimes even the termination rules of :ref:`instance-termination` are -too onerous. So GHC allows you to experiment with more liberal rules: if -you use the experimental extension :extension:`UndecidableInstances`, both the Paterson -Conditions and the Coverage -Condition (described in :ref:`instance-termination`) are lifted. -Termination is still ensured by having a fixed-depth recursion stack. If -you exceed the stack depth you get a sort of backtrace, and the -opportunity to increase the stack depth with -``-freduction-depth=⟨n⟩``. However, if you should exceed the default -reduction depth limit, it is probably best just to disable depth -checking, with ``-freduction-depth=0``. The exact depth your program -requires depends on minutiae of your code, and it may change between -minor GHC releases. The safest bet for released code -- if you're sure -that it should compile in finite time -- is just to disable the check. - -For example, sometimes you might want to use the following to get the -effect of a "class synonym": - -:: - - class (C1 a, C2 a, C3 a) => C a where { } - - instance (C1 a, C2 a, C3 a) => C a where { } - -This allows you to write shorter signatures: - -:: - - f :: C a => ... - -instead of - -:: - - f :: (C1 a, C2 a, C3 a) => ... - The restrictions on functional dependencies (:ref:`functional-dependencies`) are particularly troublesome. It is tempting to introduce type variables in the context that do not appear @@ -509,6 +436,7 @@ As a more substantial example of the rules in action, consider :: instance {-# OVERLAPPABLE #-} context3 => C a [b] where ... -- (C) instance {-# OVERLAPPING #-} context4 => C Int [Int] where ... -- (D) +(These all need :extension:`FlexibleInstances`.) Now suppose that the type inference engine needs to solve the constraint ``C Int [Int]``. This constraint matches instances (A), (C) and (D), but the last is more specific, and hence is chosen. @@ -521,7 +449,7 @@ accepted and (A) or (C) would be chosen arbitrarily. An instance declaration is *more specific* than another iff the head of former is a substitution instance of the latter. For example (D) is "more specific" than (C) because you can get from (C) to (D) by -substituting ``a := Int``. +substituting ``a := Int`` and ``b := Int``. The final bullet (about unifying instances) makes GHC conservative about committing to an @@ -549,8 +477,8 @@ the type :: f :: C b [b] => [b] -> [b] That postpones the question of which instance to pick to the call site -for ``f`` by which time more is known about the type ``b``. You can -write this type signature yourself if you use the +for ``f`` by which time more is known about the type ``b``. You +will need the :extension:`FlexibleContexts` extension. Exactly the same situation can arise in instance declarations @@ -571,7 +499,7 @@ declaration, thus: :: instance C Int [b] => Foo [b] where f x = ... -(You need :extension:`FlexibleInstances` to do this.) +(You need :extension:`FlexibleContexts` to do this.) In the unification check in the final bullet, GHC also uses the "in-scope given constraints". Consider for example :: |