diff options
author | Richard Eisenberg <rae@richarde.dev> | 2021-01-14 21:36:43 -0500 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-01-27 17:41:44 -0500 |
commit | 189efc39e0cf111bcb6d2ad5d474fcd01a706eb8 (patch) | |
tree | 420e4ecfc6ddb644c3321a36b95f444933363943 /docs | |
parent | 2f689a8bdf0bb6342794e0d8243e86e6cc4f4962 (diff) | |
download | haskell-189efc39e0cf111bcb6d2ad5d474fcd01a706eb8.tar.gz |
Remove some redundant validity checks.
This commit also consolidates documentation in the user
manual around UndecidableSuperClasses, UndecidableInstances,
and FlexibleContexts.
Close #19186.
Close #19187.
Test case: typecheck/should_compile/T19186,
typecheck/should_fail/T19187{,a}
Diffstat (limited to 'docs')
-rw-r--r-- | docs/users_guide/9.2.1-notes.rst | 4 | ||||
-rw-r--r-- | docs/users_guide/exts/constraints.rst | 1 | ||||
-rw-r--r-- | docs/users_guide/exts/flexible_contexts.rst | 62 | ||||
-rw-r--r-- | docs/users_guide/exts/flexible_contexts_signature.rst | 19 | ||||
-rw-r--r-- | docs/users_guide/exts/instances.rst | 84 | ||||
-rw-r--r-- | docs/users_guide/exts/type_signatures.rst | 1 | ||||
-rw-r--r-- | docs/users_guide/exts/typeclasses.rst | 1 | ||||
-rw-r--r-- | docs/users_guide/exts/undecidable_super_classes.rst | 2 |
8 files changed, 30 insertions, 144 deletions
diff --git a/docs/users_guide/9.2.1-notes.rst b/docs/users_guide/9.2.1-notes.rst index 717da75c33..a38e334cd6 100644 --- a/docs/users_guide/9.2.1-notes.rst +++ b/docs/users_guide/9.2.1-notes.rst @@ -47,6 +47,10 @@ Language -- Alternatively, -- type Codomain ('KProxy :: KProxy o) = NatTr (Proxy :: o -> Type) +* Previously, ``-XUndecidableInstances`` accidentally implied ``-XFlexibleContexts``. + This is now fixed, but it means that some programs will newly require + ``-XFlexibleContexts``. + Compiler ~~~~~~~~ diff --git a/docs/users_guide/exts/constraints.rst b/docs/users_guide/exts/constraints.rst index 6011dd6ea7..d52392b3b8 100644 --- a/docs/users_guide/exts/constraints.rst +++ b/docs/users_guide/exts/constraints.rst @@ -4,6 +4,7 @@ Constraints .. toctree:: :maxdepth: 1 + flexible_contexts equality_constraints constraint_kind quantified_constraints diff --git a/docs/users_guide/exts/flexible_contexts.rst b/docs/users_guide/exts/flexible_contexts.rst index 7823a36bec..9bfccfda2a 100644 --- a/docs/users_guide/exts/flexible_contexts.rst +++ b/docs/users_guide/exts/flexible_contexts.rst @@ -1,54 +1,26 @@ -.. _superclass-rules: +.. _flexible-contexts: -Flexible contexts ------------------ +Loosening restrictions on class contexts +---------------------------------------- .. extension:: FlexibleContexts - :shortdesc: Enable flexible contexts. + :shortdesc: Remove some restrictions on class contexts :since: 6.8.1 - Allow the use of complex constraints in class declaration contexts. + Remove the type-variable restriction on class contexts. -In Haskell 98 the context of a class declaration (which introduces -superclasses) must be simple; that is, each predicate must consist of a -class applied to type variables. The extension :extension:`FlexibleContexts` -(:ref:`flexible-contexts`) lifts this restriction, so that the only -restriction on the context in a class declaration is that the class -hierarchy must be acyclic. So these class declarations are OK: :: - - class Functor (m k) => FiniteMap m k where - ... - - class (Monad m, Monad (t m)) => Transform t m where - lift :: m a -> (t m) a - -As in Haskell 98, the class hierarchy must be acyclic. However, the -definition of "acyclic" involves only the superclass relationships. For -example, this is okay: :: - - class C a where - op :: D b => a -> b -> b - - class C a => D a where ... - -Here, ``C`` is a superclass of ``D``, but it's OK for a class operation -``op`` of ``C`` to mention ``D``. (It would not be OK for ``D`` to be a -superclass of ``C``.) - -With the extension that adds a :ref:`kind of -constraints <constraint-kind>`, you can write more exotic superclass -definitions. The superclass cycle check is even more liberal in these -cases. For example, this is OK: :: - - class A cls c where - meth :: cls c => c -> c - - class A B c => B c where - -A superclass context for a class ``C`` is allowed if, after expanding -type synonyms to their right-hand-sides, and uses of classes (other than -``C``) to their superclasses, ``C`` does not occur syntactically in the -context. +The :extension:`FlexibleContexts` extension lifts the Haskell 98 restriction that +the type-class constraints (anywhere they appear) must have the form *(class +type-variable)* or *(class (type-variable type1 type2 ... typen))*. With +:extension:`FlexibleContexts` these type signatures are perfectly okay:: + g :: Eq [a] => ... + g :: Ord (T a ()) => ... +This extension does not affect equality constraints in an instance +context; they are permitted by :extension:`TypeFamilies` or :extension:`GADTs`. + +Note that :extension:`FlexibleContexts` affects usages of class constraints, +in type signatures and other contexts. In contrast, :extension:`FlexibleInstances` +loosens a similar restriction in place when declaring a new instance. diff --git a/docs/users_guide/exts/flexible_contexts_signature.rst b/docs/users_guide/exts/flexible_contexts_signature.rst deleted file mode 100644 index cd2bd2866b..0000000000 --- a/docs/users_guide/exts/flexible_contexts_signature.rst +++ /dev/null @@ -1,19 +0,0 @@ -.. _flexible-contexts: - -The context of a type signature -------------------------------- - -The :extension:`FlexibleContexts` extension lifts the Haskell 98 restriction that -the type-class constraints in a type signature must have the form *(class -type-variable)* or *(class (type-variable type1 type2 ... typen))*. With -:extension:`FlexibleContexts` these type signatures are perfectly okay -:: - - g :: Eq [a] => ... - g :: Ord (T a ()) => ... - -The flag :extension:`FlexibleContexts` also lifts the corresponding restriction -on class declarations (:ref:`superclass-rules`) and instance -declarations (:ref:`instance-rules`). - - 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 :: diff --git a/docs/users_guide/exts/type_signatures.rst b/docs/users_guide/exts/type_signatures.rst index 19eec9d27f..9d0fcdc5bb 100644 --- a/docs/users_guide/exts/type_signatures.rst +++ b/docs/users_guide/exts/type_signatures.rst @@ -7,7 +7,6 @@ Type signatures :maxdepth: 1 explicit_forall - flexible_contexts_signature ambiguous_types kind_signatures scoped_type_variables diff --git a/docs/users_guide/exts/typeclasses.rst b/docs/users_guide/exts/typeclasses.rst index f3f63f6f5d..41aa4d985e 100644 --- a/docs/users_guide/exts/typeclasses.rst +++ b/docs/users_guide/exts/typeclasses.rst @@ -13,7 +13,6 @@ space <http://research.microsoft.com/~simonpj/Papers/type-class-design-space/>`_ :maxdepth: 1 multi_param_type_classes - flexible_contexts undecidable_super_classes constrained_class_methods default_signatures diff --git a/docs/users_guide/exts/undecidable_super_classes.rst b/docs/users_guide/exts/undecidable_super_classes.rst index c984b375d8..ed55da29a9 100644 --- a/docs/users_guide/exts/undecidable_super_classes.rst +++ b/docs/users_guide/exts/undecidable_super_classes.rst @@ -1,3 +1,5 @@ +.. _superclass-rules: + Undecidable (or recursive) superclasses --------------------------------------- |