How to determine the type of an object in a Haskell program

There may be other ways to do this, but if you need to determine the type (type signature) of an object in a Haskell program, the following approach worked for me.

First, add this import statement to your program:

import Data.Typeable

What that does is give you access to a function named typeOf, which you can then use like this:

putStrLn ("type of action1 is: " ++ (show (typeOf action1)))
putStrLn ("type of action2 is: " ++ (show (typeOf action2)))

For the purpose of this solution it doesn’t matter what action1 and action2 are; I just use typeOf to determine their type, then use show to convert the output of typeOf to a String so I can print it. (Note to my future self: code that prints output like this must be in a do block.)

Now, when I run my Haskell program I see these two lines of debug output.

Style note: I’m relatively new to Haskell, and I prefer the use of parentheses to using $.

If you need to figure out what the type of an object is in a Haskell program, I hope this is helpful.

Note that if you are in GHCI, you can just put :type before your expression to determine the expression’s type, or use :set +t to see the type of every expression in GHCI.