Giter VIP home page Giter VIP logo

type-inference's Introduction

Hindley Milner Type Inference

Build Status

The Hindley Milner Type Inference or Algorithm W is a type-inference algorithm that infers types in a programming language.

This repository contains a working implementation written in OCaml to demonstrate type-inference on a small functional language.

λ-calculus

The language that this implementation works on is a small subset called the lambda calculus. In essence, the lambda calculus allows one to express any computation purely in terms of anonymous functions and application of these functions.

> (fun x -> x * x)          (* function declaration *)
> (fun x -> x * x) 10       (* function application *)

In pure lambda calculus, numerals and booleans are also expressed in terms of functions but to make it easy, the language supports integer and boolean literals, alongwith binary operations such as addition, multiplication, boolean and etc.

Types

Before we jump on to the type-inference algorithm, we need to define the types in our language. There are three primitive types that our language supports -

  • int: An integer type for integer literals. Binary operations such as + and *, work only on integers and return an integer type.
  • bool: Our language has boolean literals true and false, both of which have a bool type. To operate on bools && and || are provided. Lastly, two additional operators > and < work on any type, but return a bool type.
  • T -> U: The function type where the T is the type of the input and U is the return type of the function. So for example, a square function above has a type int -> int.

REPL

The project ships with an interactive Read-Eval-Print-Loop (REPL) that you can use to play with the algorithm. To build the REPL, you need OCaml installed.

If you prefer Docker, there's an image that you can use to try out the REPL. Simply run

$ docker run -w /home/opam/type-inference -it prakhar1989/type-infer /bin/bash

Compile the REPL with make and if all goes well, you should be good to go.

$ ./repl

Welcome to the REPL.
Type in expressions and let Hindley-Milner Type Inference run its magic.

Out of ideas? Try out a simple lambda expression: (fun x -> x + 10)

> 10 + 20 > 40
bool
> (fun x -> (x && true) || false)
(bool -> bool)
> (fun x -> x + 10) 20
int
> (fun f -> f 3)
((int -> 'a) -> 'a)
>  (fun f -> (fun g -> (fun x -> f (g x))))
(('a -> 'b) -> (('c -> 'a) -> ('c -> 'b)))

Tests

To run the tests, you need Alcotest package installed. Install it by running opam install alcotest.

$ make test

Thanks

Huge thanks to these lecture notes for providing an understandable breakdown of the algorithm.

type-inference's People

Contributors

bahuljain avatar prakhar1989 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.