Reputation: 5063
Let
f :: a -> Int
f arg = 2
a
is a type that will be deduced during compilation. Is it possible to learn how did Haskell deduce it?
Upvotes: 0
Views: 206
Reputation: 116139
I'm not sure whether this is actually useful... but here is one way:
> import Data.Typeable
> let argType f x = let _ = f x in typeOf x
Example:
> let f :: a->Int ; f arg = 2
> argType f 'a'
Char
> argType f 1
Integer
The last example shows the actual type of 1
, after GHC(i) defaulting.
A simpler alternative, working even in source files: when we have
foo (bar x) (baz y)
and we want to know the type of y
, we can just replace it with
foo (bar x) (baz (asTypeOf _ y))
We will get an error such as
• Found hole: _ :: Integer
which tells us the type of y
. Downside: we have to revert the code back for it to compile.
As a more low-level alternative, compile with -ddump-simpl
and observe the scary GHC Core which is being produced. There, every type argument is made explicit: we could read something like f @ Char 'a'
, where names could be possibly mangled a bit, e.g. Main.a$f @ GHC.Char 'a'
, but it should still be possible to understand what's going on.
Upvotes: 3