6.8.8. Instance declarations and resolution¶

The part before the “ => ” is the context, while the part after the “ => ” is the head of the instance declaration.

When GHC tries to resolve, say, the constraint C Int Bool , it tries to match every instance declaration against the constraint, by instantiating the head of the instance declaration. Consider these declarations:

instance context1 => C Int a where . -- (A) instance context2 => C a Bool where . -- (B) 

GHC’s default behaviour is that exactly one instance must match the constraint it is trying to resolve. For example, the constraint C Int Bool matches instances (A) and (B), and hence would be rejected; while C Int Char matches only (A) and hence (A) is chosen.

See also Overlapping instances for flags that loosen the instance resolution rules.

6.8.8.1. Relaxed rules for the instance head¶

TypeSynonymInstances ¶
Implied by: FlexibleInstances
Since:6.8.1
Status:Included in GHC2021

Allow definition of type class instances for type synonyms.

Implies: TypeSynonymInstances
Since:6.8.1
Status:Included in GHC2021

Allow definition of type class instances with arbitrary nested types in 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 data type constructor, and the a1 . an are distinct type variables. In the case of multi-parameter type classes, this rule applies to each parameter of the instance head (Arguably it should be okay if just one has this form and the others are type variables, but that’s the rules at the moment).

GHC relaxes this rule in two ways:

type Point a = (a,a) instance C (Point a) where . 
is legal. The instance declaration is equivalent to
instance C (a,a) where . 
As always, type synonyms must be fully applied. You cannot, for example, write:
instance Monad Point where . 
instance C (Maybe Int) where . 

However, the instance declaration must still conform to the rules for instance termination: see Instance termination rules .

6.8.8.2. Formal syntax for instance declaration types¶

The top of an instance declaration only permits very specific forms of types. To make more precise what forms of types are or are not permitted, we provide a BNF-style grammar for the tops of instance declarations below.

inst_top ::= 'instance' opt_forall opt_ctxt inst_head opt_where opt_forall ::= | 'forall' tv_bndrs '.' tv_bndrs ::= | tv_bndr tv_bndrs tv_bndr ::= tyvar | '(' tyvar '::' ctype ')' opt_ctxt ::= | btype '=>' | '(' ctxt ')' '=>' ctxt ::= ctype | ctype ',' ctxt inst_head ::= '(' inst_head ')' | prefix_cls_tycon arg_types | arg_type infix_cls_tycon arg_type | '(' arg_type infix_cls_tycon arg_type ')' arg_types arg_type ::= | arg_type arg_types opt_where ::= | 'where'

This is a simplified grammar that does not fully delve into all of the implementation details of GHC’s parser (such as the placement of Haddock comments), but it is sufficient to attain an understanding of what is syntactically allowed. Some further various observations about this grammar:

instance forall a. forall b. C (Either a b) where . 

6.8.8.3. Instance termination rules¶

Regardless of FlexibleInstances and FlexibleContexts , instance declarations must conform to some rules that ensure that instance resolution will terminate. The restrictions can be lifted with UndecidableInstances (see Undecidable instances and loopy superclasses ).

The rules are these:

  1. The Paterson Conditions: for each class constraint (C t1 . tn) in the context
    1. No type variable has more occurrences in the constraint than in the head
    2. The constraint has fewer constructors and variables (taken together and counting repetitions) than the head
    3. The constraint mentions no type functions. A type function application can in principle expand to a type of arbitrary size, and so are rejected out of hand

    If these three conditions hold we say that the constraint (C t1 . tn) is Paterson-smaller than the instance head.

    These restrictions ensure that instance resolution terminates: each reduction step makes the problem smaller by at least one constructor. You can find lots of background material about the reason for these restrictions in the paper Understanding functional dependencies via Constraint Handling Rules.

    For example, these are okay:

    instance C Int [a] -- Multiple parameters instance Eq (S [a]) -- Structured type in head -- Repeated type variable in head instance C4 a a => C4 [a] [a] instance Stateful (ST s) (MutVar s) -- Head can consist of type variables only instance C a instance (Eq a, Show b) => C2 a b -- Non-type variables in context instance Show (s a) => Show (Sized s a) instance C2 Int a => C3 Bool [a] instance C2 Int a => C3 [a] b 

    But these are not:

    -- Context assertion no smaller than head instance C a => C a where . -- (C b b) has more occurrences of b than the head instance C b b => Foo [b] where . 

    The same restrictions apply to instances generated by deriving clauses. Thus the following is accepted:

    data MinHeap h a = H a (h a) deriving (Show) 

    because the derived instance

    instance (Show a, Show (h a)) => Show (MinHeap h a) 

    conforms to the above rules.

    The restrictions on functional dependencies ( Functional dependencies ) are particularly troublesome. It is tempting to introduce type variables in the context that do not appear in the head, something that is excluded by the normal rules. For example:

    class HasConverter a b | a -> b where convert :: a -> b data Foo a = MkFoo a instance (HasConverter a b,Show b) => Show (Foo a) where show (MkFoo value) = show (convert value) 

    This is dangerous territory, however. Here, for example, is a program that would make the typechecker loop:

    class D a class F a b | a->b instance F [a] [[a]] instance (D c, F a c) => D [a] -- 'c' is not mentioned in the head 

    Similarly, it can be tempting to lift the coverage condition:

    class Mul a b c | a b -> c where (.*.) :: a -> b -> c instance Mul Int Int Int where (.*.) = (*) instance Mul Int Float Float where x .*. y = fromIntegral x * y instance Mul a b c => Mul a [b] [c] where x .*. v = map (x.*.) v 

    The third instance declaration does not obey the coverage condition; and indeed the (somewhat strange) definition:

    f = \ b x y -> if b then x .*. [y] else y 

    makes instance inference go into a loop, because it requires the constraint (Mul a [b] b) .

    6.8.8.4. Undecidable instances and loopy superclasses¶

    UndecidableInstances ¶

    Permit definition of instances which may lead to type-checker non-termination.

    The UndecidableInstances extension lifts the restrictions on on instance declarations described in Instance termination rules . The UndecidableInstances extension also lifts some of the restrictions imposed on type family instances; see Decidability of type synonym instances .

    With UndecidableInstances it is possible to create a superclass cycle, which leads to the program failing to terminate. To avoid this, GHC imposes rules on the way in which superclass constraints are satisfied in an instance declaration. These rules apply even when UndecidableInstances is enabled. Consider:

    class C a => D a where . instance Wombat [b] => D [b] where . 

    When typechecking this instance declaration, GHC must ensure that D ’s superclass, (C [b]) is satisfied. We say that (C [b]) is a Wanted superclass constraint of the instance declaration.

    If there is an instance blah => C [b] , which is often the case, GHC can use the instance declaration and all is well. But suppose there is no such instance, so GHC can only satisfy the Wanted (C [b]) from the context of the instance, namely the Given constraint (Wombat [b]) . Perhaps the declaration of Wombat looks like this:

    class C a => Wombat a 

    So the Given (Wombat [b]) has a superclass (C [b]) , and it looks as if we can satisfy the Wanted (C [b]) constraint from this superclass of Wombat . But it turns out that allowing this can lead to subtle looping dictionaries, and GHC prevents it.

    The rule is this: a Wanted superclass constraint can only be satisfied in one of these three ways:

      Directly from the context of the instance declaration. For example, if the declaration looked like this:

    instance (Wombat [b], C [b]) => D [b] where . 
    instance C b => C [b] where . 

    Rule (3) is the tricky one. Here is an example, taken from GHC’s own source code:

    class Ord r => UserOfRegs r a where . (i1) instance UserOfRegs r a => UserOfRegs r (Maybe a) where (i2) instance (Ord r, UserOfRegs r CmmReg) => UserOfRegs r CmmExpr where 

    For (i1) we can get the (Ord r) superclass by selection from (UserOfRegs r a) , since it (i.e. UserOfRegs r a ) is Paterson-smaller than the head of the instance declaration, namely (UserOfRegs r (Maybe a)) .

    But for (i2) that isn’t the case: (UserOfRegs r CmmReg) is not Paterson-smaller than the head of the instance (UserOfRegs r CmmExpr) , so we can’t use the superclasses of the former. Hence we must instead add an explicit, and perhaps surprising, (Ord r) argument to the instance declaration.

    This fix, of simply adding an apparently-redundant constraint to the context of an instance declaration, is robust: it always fixes the problem. (We considered adding it automatically, but decided that it was better be explicit.)

    Fixing this subtle superclass cycle has a long history; if you are interested, read Note [Recursive superclasses] and Note [Solving superclass constraints] in GHC.Tc.TyCl.Instance .

    6.8.8.5. Overlapping instances¶

    OverlappingInstances ¶
    Since:6.8.1
    Status:Deprecated

    Deprecated extension to weaken checks intended to ensure instance resolution termination.

    Since:6.8.1
    Status:Deprecated

    Deprecated extension to weaken checks intended to ensure instance resolution termination.

    In general, as discussed in Instance declarations and resolution , GHC requires that it be unambiguous which instance declaration should be used to resolve a type-class constraint. GHC also provides a way to loosen the instance resolution, by allowing more than one instance to match, provided there is a most specific one. Moreover, it can be loosened further, by allowing more than one instance to match irrespective of whether there is a most specific one. This section gives the details.

    To control the choice of instance, it is possible to specify the overlap behavior for individual instances with a pragma, written immediately after the instance keyword. The pragma may be one of: , , , or .

    The matching behaviour is also influenced by two module-level language extension flags: OverlappingInstances and IncoherentInstances . These extensions are now deprecated (since GHC 7.10) in favour of the fine-grained per-instance pragmas.

    A more precise specification is as follows. The willingness to be overlapped or incoherent is a property of the instance declaration itself, controlled as follows:

    Now suppose that, in some client module, we are searching for an instance of the target constraint (C ty1 .. tyn) . The search works like this: