Any way to print out a type of a variable in a do / while / let block?

gatoatigrado picture gatoatigrado · Jul 20, 2011 · Viewed 8.5k times · Source

Is there any way to print out the inferred type of a nested variable in ghci? Consider the code,

let f = g where
    g (x :: Int) = x

then, it'd be nice to query the type of g, e.g. :t f.g would print out Int -> Int.

Answer

Anthony picture Anthony · Jul 20, 2011

You can coax this information out by giving an appropriately wrong type annotation and checking the error message.

*Main> let f = g where g::a; g (x::Int) = x

<interactive>:1:23:
    Couldn't match type `a1' with `Int -> Int'
      `a1' is a rigid type variable bound by...