# Type signatures as good style

### From HaskellWiki

(Difference between revisions)

(signatures are good documentation and cannot always be infered automatically) |
(mention rank n types) |
||

Line 20: | Line 20: | ||

''I remember that for some type extensions the automatic inference fails. Examples?'' |
''I remember that for some type extensions the automatic inference fails. Examples?'' |
||

+ | |||

+ | Higher-order types, e.g., the type of <hask>Control.Monad.ST.runST</hask>: |
||

+ | <haskell> |
||

+ | runST :: (forall s . ST s a) -> a |
||

+ | </haskell> |
||

+ | cannot be inferred in general, because the problem is undecidable. In GHC, they are enabled with the language pragma <code>RankNTypes</code>. |
||

== How to add a bunch of signatures? == |
== How to add a bunch of signatures? == |

## Revision as of 20:55, 8 July 2008

## 1 Question

Since Haskell type checkers can automatically derive types of expressions why shall I put explicit type signatures in my programs?

## 2 Answer

Using explicit type signatures is good style and GHC with option `-Wall`

warns about missing signatures.
Signatures are a good documentation and not all Haskell program readers have a type inference algorithm built-in.
There are also some cases where the infered signature is too general for your purposes.

asTypeOf

a -> b -> a

asTypeOf

a -> a -> a

Another example:

emptyString :: ShowS emptyString = id

ShowS

String -> String

a -> a

*I remember that for some type extensions the automatic inference fails. Examples?*

Control.Monad.ST.runST

runST :: (forall s . ST s a) -> a

cannot be inferred in general, because the problem is undecidable. In GHC, they are enabled with the language pragma `RankNTypes`

.

## 3 How to add a bunch of signatures?

Ok, this convinced me. How can I add all the signatures I did not write so far?