Type signatures in Haskell

Monday, 1st June, 2009

From section Record Syntax (p. 55), RWH starts surreptitiously adding type signatures to its code samples. The code samples work just as well without the type signatures, and RWH does not explain why they are being used (in fact, RWH doesn’t mention these code sample type signatures at all).

Haskell is a typed language, but it uses type inference. So why use type signatures at all? RWH waits until Chapter 5, Section Type Inference is a Double-Edged Sword (p. 117) before telling us.

One reason might be that Haskell might not infer the type you want it to infer. Trivial example:

v :: Int
v = 5

Without the type signature, Haskell will infer that v is of type Integer. OK, that probably won’t make any bridges collapse, but a similar false inference with more complex types might.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.