Analog for :t from ghci

Viewed 119

Reading haskell book about types I want to repeat sample for :t command:

ghci> :t 'a'  
'a' :: Char  
ghci> :t True  
True :: Bool  
ghci> :t "HELLO!"  
"HELLO!" :: [Char]  
ghci> :t (True, 'a')  
(True, 'a') :: (Bool, Char)  
ghci> :t 4 == 5  
4 == 5 :: Bool

Unfortunately I can't install haskell locally, thatswhy I use ideone.com

But I can't understand how create the same sample.

I tried like this:

main = do
    putStrLn "Start"
    :t 'a'
    putStrLn "Finish"

or this:

:t 'a'

or:

putStrLn ("Finish" ++ show :t 'a')

Is there a possibility to show types without installing ghci? Maybe, there is another web fiddle that can do this?

1 Answers

You can use typeOf :: forall a. Typeable a => a -> TypeRep to obtain the TypeRep object. It requires that your type is Typeable, but with the given examples that is the case.

For example:

import Data.Typeable(typeOf)

main = putStrLn ("Finish" ++ show (typeOf 'a'))

This will not print full polymorphic types, since at compile time the types are determined. You thus see the type as is derived by the Haskell compiler.

Related