From 177b31ca6ebe01f8011926e30c4ac7ee32791753 Mon Sep 17 00:00:00 2001 From: "simonpj@microsoft.com" Date: Wed, 15 Oct 2008 08:05:09 +0000 Subject: [PATCH] Update manual: tidy up instances, say more about type families in instance decls --- docs/users_guide/flags.xml | 2 +- docs/users_guide/glasgow_exts.xml | 133 +++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 50 deletions(-) diff --git a/docs/users_guide/flags.xml b/docs/users_guide/flags.xml index 4ccad78..c8eedd2 100644 --- a/docs/users_guide/flags.xml +++ b/docs/users_guide/flags.xml @@ -932,7 +932,7 @@ - Enable type synonyms. + Enable type synonyms. dynamic diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 2169bba..dee62f4 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -3247,9 +3247,6 @@ sets of instance declarations. Instance declarations - -Relaxed rules for instance declarations - An instance declaration has the form instance ( assertion1, ..., assertionn) => class type1 ... typem where ... @@ -3259,19 +3256,73 @@ The part before the "=>" is the "=>" is the head of the instance declaration. + +Relaxed rules for the instance head + In Haskell 98 the head of an instance declaration must be of the form C (T a1 ... an), where -C is the class, T is a type constructor, +C is the class, T is a data type constructor, and the a1 ... an are distinct type variables. -Furthermore, the assertions in the context of the instance declaration +GHC relaxes these rules in two ways. + + + +The flag allows the head of the instance +declaration to mention arbitrary nested types. +For example, this becomes a legal instance declaration + + instance C (Maybe Int) where ... + +See also the rules on overlap. + + +With the flag, instance heads may use type +synonyms. As always, using a type synonym is just shorthand for +writing the RHS of the type synonym definition. For example: + + + + type Point = (Int,Int) + instance C Point where ... + instance C [Point] where ... + + + +is legal. However, if you added + + + + instance C (Int,Int) where ... + + + +as well, then the compiler will complain about the overlapping +(actually, identical) instance declarations. As always, type synonyms +must be fully applied. You cannot, for example, write: + + + type P a = [[a]] + instance Monad P where ... + + + + + + + + +Relaxed rules for instance contexts + +In Haskell 98, the assertions 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 flag loosens these restrictions -considerably. Firstly, multi-parameter type classes are permitted. Secondly, -the context and head of the instance declaration can each consist of arbitrary +The flag relaxes this rule, as well +as the corresponding rule for type signatures (see ). +With this flag the context of the instance declaration can each consist of arbitrary (well-kinded) assertions (C t1 ... tn) subject only to the following rules: @@ -3569,47 +3620,6 @@ hard to pin down.) We are interested to receive feedback on these points. - -Type synonyms in the instance head - - -Unlike Haskell 98, instance heads may use type -synonyms. (The instance "head" is the bit after the "=>" in an instance decl.) -As always, using a type synonym is just shorthand for -writing the RHS of the type synonym definition. For example: - - - - type Point = (Int,Int) - instance C Point where ... - instance C [Point] where ... - - - -is legal. However, if you added - - - - instance C (Int,Int) where ... - - - -as well, then the compiler will complain about the overlapping -(actually, identical) instance declarations. As always, type synonyms -must be fully applied. You cannot, for example, write: - - - - type P a = [[a]] - instance Monad P where ... - - - -This design decision is independent of all the others, and easily -reversed, but it makes sense to me. - - - @@ -4328,6 +4338,31 @@ class (F a ~ b) => C a b where + + Type families and instance declarations + Type families require us to extend the rules for + the form of instance heads, which are given + in . + Specifically: + + Data type families may appear in an instance head + Type synonym families may not appear (at all) in an instance head + +The reason for the latter restriction is that there is no way to check for. Consider + + type family F a + type instance F Bool = Int + + class C a + + instance C Int + instance C (F a) + +Now a constraint (C (F Bool)) would match both instances. +The situation is especially bad because the type instance for F Bool +might be in another module, or even in a module that is not yet written. + + -- 1.7.10.4