How does the OCaml type inferencing algorithm work?

前端 未结 2 1508
南方客
南方客 2021-01-04 11:35

I\'m currently learning OCaml, and I\'m curious to HOW OCaml does its type inferencing. I know that it\'s done through a process called unification, and I tried reading abou

相关标签:
2条回答
  • 2021-01-04 12:12

    Actually, it can be argued that unification is an implementation detail of the algorithm. The type system is only a set of rules. The rules allow to check an existing typing derivation. The rules do not mention unification explicitly, although unification is a technique that naturally comes to mind when thinking of implementing an algorithm that automatically produces type derivations from expressions.

    I really enjoyed reading this “Functional programming using Caml Light” tutorial by Michel Mauny when I had the same question as you. The tutorial shows its age a little bit now, but the chapter you are interested in (chapter 15) is still as good now as it was then.

    0 讨论(0)
  • 2021-01-04 12:22

    The canonical reference for learning about HM type inference in ML would probably be Ben Pierce's "Types and Programming Languages." You can find the topic covered in chapter 22 of that book.

    The first instance of the type inference algorithm is known as Algorithm W.

    However, it may surprise you to know that -- in fact -- OCaml's implementation does not simply generate the constraints and solve them! In fact, it does a much more efficient graph based algorithm for type inference which (while speedy) leads to occasionally strange type errors being reported. You can look to references on explaining type errors in ML.

    0 讨论(0)
提交回复
热议问题