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.
- The WikiBook Haskell :: Functional Programming with Types has a nice little section on reasons to use type signatures.
- The tutorial at haskell.org, A Gentle Introduction to Haskell, mentions it in Section 2, Values, Types, and Other Goodies