Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

ghci

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.

like image 863
gatoatigrado Avatar asked Jul 20 '11 03:07

gatoatigrado


2 Answers

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...
like image 60
Anthony Avatar answered Oct 14 '22 09:10

Anthony


ghci debugger can print it for you with a properly placed breakpoint (but you'll need to load your definition within a module):

{-# LANGUAGE ScopedTypeVariables #-} 

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

Then in ghci:

Prelude> :l tmp2.hs
[1 of 1] Compiling Main             ( tmp2.hs, interpreted )
Ok, modules loaded: Main.
*Main> :b 3 9
Breakpoint 0 activated at tmp2.hs:3:7-9
*Main> f undefined
Stopped at tmp2.hs:3:7-9
_result :: Int = _
a :: Int = _
g :: Int -> Int = _
[tmp2.hs:3:7-9] *Main>
like image 29
Ed'ka Avatar answered Oct 14 '22 09:10

Ed'ka