[Haskell-beginners] Explicit specification of function types

Yitzchak Gale gale at sefer.org
Tue Mar 24 11:02:04 EDT 2009


Zachary Turner wrote:
>> it's generally considered good practice to specify the
>> full type of a function before the definition.  Why is this?
>> It almost seems to go against the principles of type inference.

Michael Mossey wrote:
> ...it seems a shame to disregard this power by declaring all types.

I concur with the reasons given by previous responders for
why it's important to specify type signatures for top-level
functions.

I'll add that this practice is not at all against the principles of
type inference, and you still use its full power:

o for auxiliary functions defined in let and where clauses
o for functions defined at the interactive prompt
o for quick scripting

Type inference provides a powerful technique for investigating
how to use various tools in Haskell: you use the :type command
at the interactive command prompt (or in our IRC channels) to
find out what type is assigned to an expression.

Besides these direct usages, the compiler makes essential
use of type inference whenever you write any non-trivial
expression. You may have specified the type of the final result,
but the compiler needs to know the type of every intermediate
result. This requires type inference because of the very
general kinds of polymorphism that Haskell allows.

-Yitz


More information about the Beginners mailing list