haskellghci

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


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.


Solution

  • 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...