How does the OCaml type inferencing algorithm work?

蓝咒 提交于 2019-11-30 03:02:04

问题


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 about the algorithm in the published paper but the notation threw me off. Can anyone describe the step-by-step process for me?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12717690/how-does-the-ocaml-type-inferencing-algorithm-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!