I read of anecdotes of people who used ML in industry saying that it was rather frequent to encounter type inference limitations and have to use explicit type annotations in their programs. Can anyone give explicit examples of where ML's type inference encounters problems and maybe explain if the limitations are related to being undecidable?
-
Similar: [what-are-the-limits-of-type-inference](http://stackoverflow.com/questions/1251254/what-are-the-limits-of-type-inference?rq=1) – nawfal Jul 23 '14 at 12:24
2 Answers
I cannot give right now an example (it has been months that I didn't code in Ocaml), but what may happen in practice is when you make a type mistake (i.e. pass an argument of the wrong type), notably when using high-order functions, the type error messages may happen far away from your real mistake.
To alleviate that, I got the habit of explicitly typing most internal top-level functions, like
let f (x: int) : string =

- 223,805
- 18
- 296
- 547
Probably a duplicate of this question/answer. Quick answer: Hindley/Milner type inference (as used in ML) is restricted to rank-1 polymorphism, that is, polymorphic types are second class citizens of the type system and e.g. function arguments cannot be polymorphic themselves. Type inference for higher-ranked polymorphism (like System F aka the polymorphic lambda calculus) is known to be undecidable.

- 1
- 1

- 34,518
- 3
- 61
- 72