Beautifying Haskell programs (was: Re: pretty newby )

Wolfgang Jeltsch wolfgang at jeltsch.net
Wed Sep 24 20:06:43 EDT 2003


Am Mittwoch, 24. September 2003, 18:01 schrieb Keith Wansbrough:
> [...]

> And your other point, Luc, about generating type signatures automatically,
> shows up something about your approach to debugging code.  You should always
> put the type signatures in as you go - preferably, before you write the
> function!  This is not just good design practice and good documentation, it
> helps you debug the function.  With type signatures, the compiler can see
> what you intended to write, and verify that what you did write matches it.
> Without type signatures, all it can see is that two things don't match - it
> has no idea what you meant to type.  Try it: try putting in type signatures,
> and see how much better the compiler's error messages become.

Let me add that there are situations where you don't want the most general 
type (which is yielded by type inference) as the type of a specific variable 
(which can be a function, of course).

One reason might be that the most general type doesn't fit your idea about 
what the variable (function) shall describe. You could, for example, describe 
mappings from keys to values as lists of key value pairs. An empty mapping 
would be implemented as []. The type of this would be infered as [a] but what 
you want is [(a,b)].

Another reason for giving a type signature with a restricted type is that you 
want to enforce certain constraints by the type system. A good example is 
Peter Thiemanns handling of HTML documents in his WASH/CGI software.

Note also that the asTypeOf function from the prelude is essentially the const 
function; the only difference is the type. It's the restricted type of 
asTypeOf that lets this function fulfill its purpose.

> Hope this helps..
>
> --KW 8-)

Wolfgang



More information about the Haskell mailing list