Opened 6 years ago

#8161 new feature request

Associated type parameters that are more specific than the instance header

Reported by: adamgundry Owned by:
Priority: low Milestone:
Component: Compiler (Type checker) Version: 7.7
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


It would be nice if type parameters of associated types could be more specific than those in the instance header. This is currently rejected with the message "Type indexes must match class instance head", but could be accepted:

{-# LANGUAGE TypeFamilies #-}

class C a where
  type T a

instance C [a] where
  type T [Bool] = Int
  type T [Int]  = Int

More typically, this is useful where we want to use an equality constraint to make type inference easier, but need to match on the actual type in an associated type:

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}

class C a where
  type T a

instance a ~ [b] => C a where
  type T [b] = Int

This showed up in the implementation of OverloadedRecordFields. Of course, one can always work around it using a normal (non-associated) type family.

Note that we already allow type families to specialise variables that do not occur in the instance header:

{-# LANGUAGE TypeFamilies #-}

class C a where
  type T a b

instance C [a] where
  type T [a] [Bool] = Bool
  type T [a] [Int]  = Int

Change History (0)

Note: See TracTickets for help on using tickets.