Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the OCaml type inferencing algorithm work?

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?

like image 880
Kira Avatar asked Oct 03 '12 22:10

Kira


People also ask

How does type inference work?

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given.

How does rust type inference work?

Type inference means that if you don't tell the compiler the type, but it can decide by itself, it will decide. The compiler always needs to know the type of the variables, but you don't always need to tell it. Actually, usually you don't need to tell it. For example, for let my_number = 8 , my_number will be an i32 .

What is type inference in TypeScript?

In TypeScript, there are several places where type inference is used to provide type information when there is no explicit type annotation. For example, in this code. let x = 3; let x: number. The type of the x variable is inferred to be number .

What is type checking inference?

Type Inference. A Type Checker only verifies that the given declarations are consistent with their use. Examples: type checkers for Pascal, C. A Type Inference system generates consistent type declarations from information implicit in the program.


1 Answers

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.

like image 143
Pascal Cuoq Avatar answered Sep 23 '22 11:09

Pascal Cuoq