Search code examples
haskelltypeclassghci

Find instances for arbitrary type expression


Is there a way to persuade GHCi to tell me what typeclass instances some arbitrary type expression implements?

Obviously, you can use the :info command to find all the instances for a type constructor. But what about some complex type expression?

For example, if I wanted to know what instances Maybe Int has (not Maybe, but Maybe Int), how can I do that?

I tried

 :info Maybe Int

but that just gives me information for Maybe, followed by information for Int. It's treating it as two separate type constructors, rather than a type expression.

Particularly when you have complicated monad transformer stacks, it's not always obvious exactly which instances a specific stack might have.


Solution

  • Since GHC 8.10.1 there is an :instances command in GHCi:

    > :instances (Maybe Int)
    instance Read (Maybe Int) -- Defined in ‘GHC.Read’
    instance Eq (Maybe Int) -- Defined in ‘GHC.Maybe’
    instance Ord (Maybe Int) -- Defined in ‘GHC.Maybe’
    instance Show (Maybe Int) -- Defined in ‘GHC.Show’