# Type signatures as good style

### From HaskellWiki

(Difference between revisions)

(mention rank n types) |
(→Answer: link to type signature) |
||

(One intermediate revision by one user not shown) | |||

Line 6: | Line 6: | ||

== Answer == | == Answer == | ||

− | Using explicit type | + | Using explicit [[type signature]]s is good style and [[GHC]] with option <code>-Wall</code> warns about missing signatures. |

Signatures are a good documentation and not all Haskell program readers have a type inference algorithm built-in. | 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. | There are also some cases where the infered signature is too general for your purposes. | ||

Line 19: | Line 19: | ||

Where <hask>ShowS</hask> is <hask>String -> String</hask> rather than <hask>a -> a</hask>. | Where <hask>ShowS</hask> is <hask>String -> String</hask> rather than <hask>a -> a</hask>. | ||

− | + | Even more, for some type extensions the automatic inference fails, | |

− | + | e.g. the higher-order types used by <hask>Control.Monad.ST.runST</hask> | |

− | + | ||

<haskell> | <haskell> | ||

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

## Latest revision as of 00:12, 30 December 2010

## [edit] 1 Question

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

## [edit] 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

Even more, for some type extensions the automatic inference fails,

e.g. the higher-order types used byControl.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`

.

## [edit] 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?