Search code examples
haskellfunctional-programmingtypeclassunificationtype-theory

Unification Weirdness in Typeclass Instance


Let's say I have the following (silly) class:

class BlindMap m where
    mapB :: m a -> m b

I could provide the following [] instance:

instance BlindMap [] where
    mapB = map id

The type of the RHS is [a] -> [a] which should unify with [a] -> [b], but GHC doesn't think so:

Couldn't match type ‘a’ with ‘b’
  ‘a’ is a rigid type variable bound by
      the type signature for mapB :: [a] -> [b] at dingas.hs:11:5
  ‘b’ is a rigid type variable bound by
      the type signature for mapB :: [a] -> [b] at dingas.hs:11:5
Expected type: a -> b
  Actual type: b -> b
Relevant bindings include
  mapB :: [a] -> [b]
In the first argument of ‘map’, namely ‘id’
In the expression: map id

What am I missing here?

Thanks in advance.


Solution

  • What am I missing here?

    map id produces a list of values of some arbitrary type given a list of such values. [a] -> [b] promises to produce a list of values of some arbitrary type given a list of values of potentially different type.

    Hence what it expects is a -> b, but your id-based function can only take what it returns, so b -> b.