Search code examples
haskellghci

How to list instances for a type with applied type parameter in GHCi


I noticed that I don't know how to make GHCi print info about composite type. Let's consider example

data X a = X (a Int)
type XList = X []

instance Show XList where show (X l) = "X (" ++ show l ++ ")"

I would like to see some how that "X []" implements Show.

Attempt 1

λ :i (X [])

<interactive>:1:2: error: parse error on input ‘X’

Attempt 2 - prints instances for list but not (X [])

λ :i X []

Attempt 3 - nothing about instances

λ :i XList
type XList = X []   -- Defined at <interactive>:20:1

Nonetheless Show instance is working when it is applicable

λ show (X [1,2,3])
"X ([1,2,3])"

λ show (X ['1'])

<interactive>:31:18: error:
    • Couldn't match expected type ‘Int’ with actual type ‘Char’

Solution

  • :info (what :i is short for) only works on names, not on expressions. To get the instances for an expression, use :instances instead:

    λ :instances (X [])
    instance [safe] Show XList -- Defined at <interactive>:6:10
    instance [safe] Show XList -- Defined at <interactive>:6:10