How to get the type as a String in Haskell?

In summary, the answer is to enable template haskell and use ' and ''

{-# LANGUAGE TemplateHaskell #-}
main = do
  putStrLn $ show 'read

If your type derives Typeable (which ghc can do automatically) then you can just call typeOf from Data.Typeable to get a showable representation.

If you want to get types of certain polymorphic functions, the polytypeable package on Hackage allows you to do so: http://hackage.haskell.org/packages/archive/polytypeable/0.1.0.0/doc/html/Data-PolyTypeable.html

This is a sort of insane type-level thing written by Oleg and packaged by Lennart, mind you. And it has.. quirks. The most glaring is that it can't give you (nor can I imagine how anything could, frankly) class constraint contexts. So show will be given a type of a -> String rather than forall a. Show a => a -> String.

If you need more than that, and are satisfied with doing certain things only at compile time, then using template haskell to extract type information directly from ghc is the only way to go. See reify and Info particularly: http://hackage.haskell.org/packages/archive/template-haskell/2.5.0.0/doc/html/Language-Haskell-TH.html


You can't define theMagic as a function (since it would need a type argument), but you can get close.

import Data.Typeable
...
putStrLn $ show $ typeOf (undefined :: MyType)

You can use a Proxy for this:

Prelude> import Data.Typeable
Prelude Data.Typeable> show $ typeRep (Proxy :: Proxy [Int])
"[Int]"