Other than loading a Haskell file into GHCi and then using :type <expr>
, is there any way to have the compiler display types for all of the functions as they are compiled?
Functions can also be passed as arguments or returned (as we have seen). Their types are given in the type signature. *Main> :t map map :: (a -> b) -> [a] -> [b] *Main> :t filter filter :: (a -> Bool) -> [a] -> [a] flip_args :: (a -> b -> c) -> b -> a -> c flip_args f x y = f y x.
a. to just print it during compilation, you could enable PartialSignatures extension, and add to arbitrary expression signature :: _ , then its type will be printed during compilation. b. to actually get the type as a data for runtime handling, you could use TemplateHaskell extension and function reify from it.
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.
The shows functions return a function that prepends the output String to an existing String . This allows constant-time concatenation of results using function composition.
If you pass -fwarn-missing-signatures
(or -Wall
) to GHC it will list every top-level function that has no type annotation in the file, together with the inferred type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With