Data.Typeable
を import して,
showType = show . typeOf
という関数を呼び出せば文字列が得られます.これを出力することで可能です(一部この方法でできないものもありますが,大抵は大丈夫だと思います)
GHCi 上でも次のようにできます:
>>> import Data.Typeable
>>> showType = show . typeOf
>>> putStrLn $ showType $ Just 1
Maybe Integer
ところで,コンパイルエラーで型を教えてくれるTyped Hole という機能が, GHC には搭載されており,型を知りたい式に
(Just () :: _) <> Nothing
というように
:: _
という表記を記載すると型情報をコンパイル時にエラーメッセージとして表示してくれます.GHCi 上でも可能で
>>> (Just () :: _) <> Nothing
(Just () :: _) <> Nothing
<interactive>:8:14: error:
• Found type wildcard '_' standing for 'Maybe ()'
To use the inferred type, enable PartialTypeSignatures
• In an expression type signature: _
In the first argument of '(<>)', namely '(Just () :: _)'
In the expression: (Just () :: _) <> Nothing
• Relevant bindings include
it :: Maybe () (bound at <interactive>:8:2)
という感じです(少し見にくいですが,上のメッセージが主で “Found type wildcard ‘_’ standing for ‘Maybe ()’” で型が
Maybe ()
であることを教えてくれます)