Determining the type of an expression
How can I find out the type of an expression or a subexpression?
2.1 Type of expressions
Prelude> :type map fromEnum "Text" map fromEnum "Text" :: [Int]
If the expression is a top-level binding in a module,
you can use the
Prelude> :info map map :: (a -> b) -> [a] -> [b]
you can show the types of all top-level variables.
This way you can write many functions in a module
without a type signature, find them out and add them afterwards.
But be warned that due to the Monomorphism restriction
the automatically infered signatures
are not always the ones you want.
2.2 Type of subexpressionsSay, you have the expression
This is also possible, just type
Prelude> :type \ f -> map f "Text" == [1,2,3] \f -> map f "Text" == [1,2,3] :: Num a => (Char -> a) -> Bool
but is an instantiation of a polymorphic function.E.g. of what concrete type is
Prelude> :type \ f -> map (id `asTypeOf` f) "Text" \f -> map (asTypeOf id f) "Text" :: (Char -> Char) -> [Char]