• Stars
    star
    356
  • Rank 115,342 (Top 3 %)
  • Language
    OCaml
  • Created about 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Hindley Milner Type Inference Algorithm

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.

Demo

λ-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.

More Repositories

1

awesome-courses

📚 List of awesome university courses for learning Computer Science!
54,006
star
2

docker-curriculum

🐬 A comprehensive tutorial on getting started with Docker!
SCSS
5,454
star
3

Algorithms

💻 Data Structures and Algorithms in Python
Python
3,009
star
4

FoodTrucks

🚚 San Francisco's finger-licking street food now at your fingertips.
JavaScript
424
star
5

react-surveyman

SurveyMan in React
JavaScript
153
star
6

react-term

A simple terminal emulator in React
JavaScript
139
star
7

progress-cpp

A flexible ASCII progress-bar for C++
CMake
101
star
8

ColorPhun

A Simple Color Game in Android
Java
82
star
9

shell

A simple shell in C
C
54
star
10

csds-material

Course material for the Computer Systems for Data Science class at Columbia
Java
43
star
11

JSJS

A strongly typed language for the web!
OCaml
41
star
12

dive-in

A docker extension to help you explore a docker image and discover ways to shrink the size
TypeScript
36
star
13

hugo-blog

📃
HTML
29
star
14

JokaStore

Mini - Ecommerce Store in Python
Python
24
star
15

monte-pie

Pi approximation using Monte Carlo Simulation
JavaScript
21
star
16

coding-challenges

Dan Shiffman's Coding Challenges in Clojure
Clojure
16
star
17

react-shopping-cart

A simple shopping cart React app that supports Drag and Drop!
JavaScript
13
star
18

gettup

A command line file sharing utility for http://ge.tt
Python
11
star
19

raytra

A simple raytracer written in C++
C++
11
star
20

CS253-Blag

Blog for Udacity Web App Engg course - C253
Python
10
star
21

VotingApplication

Election Application developed for university
Python
9
star
22

Python-With-Rohan

Teaching Python to Rohan
JavaScript
9
star
23

Paper-Summaries

Summaries of papers that I've read
9
star
24

cloud-projects

☁️ Projects for the Cloud & Big Data class at Columbia
JavaScript
9
star
25

ListIt

Dart
8
star
26

dotfiles

prakhar does dotfiles
Vim Script
8
star
27

Loops

Play | Pause | Repeat
JavaScript
8
star
28

Reco_Engine

Recommendation Engine in Python
Python
7
star
29

GoApps

Learning Golang
Go
6
star
30

reflux-starter-kit

Get started with reflux and react in minimum number of keystrokes!
JavaScript
6
star
31

go-hashmap

A naive hashmap implementation in Golang
Go
5
star
32

ocaml-js

A JS parser and pretty printer in OCaml
OCaml
5
star
33

advent-of-code-2018

Aoc 2018 attempts in Rust
Rust
5
star
34

ElevatorControlSystem

An elevator control system
Python
5
star
35

grunt-newman

Grunt plugin for Newman
JavaScript
5
star
36

Color-palette

Generate a website's color palette
Python
4
star
37

js_notes

Notes to accompany my javascript learning
4
star
38

webapp2_auth

Webapp2 Auth Example - Deployed on GAE
Python
4
star
39

Postman-demo-server

Demo server for postman
Python
4
star
40

R_Course

Computing for Data Analysis - Solutions
R
4
star
41

advent-of-code-2017

Crappy attempts in Kotlin
Kotlin
4
star
42

friendlychat

Example Flutter Chat App
C++
3
star
43

Gems

A candy inspired CMUS theme
3
star
44

devlog

CSS
3
star
45

Pastie

Your friendly neighbourhood pasting tool
JavaScript
3
star
46

playground

I like to play
Python
3
star
47

cs253-wiki

Final Exam for CS253
Python
3
star
48

gitbook-plugin-clojurescript

A ClojureScript REPL for your Gitbook
JavaScript
3
star
49

instabase

An Entity Resolution game
Python
3
star
50

lispy

Lisp Interpreter in C
C
3
star
51

exercism

My exercism attempts
Rust
3
star
52

prakhar1989

3
star
53

clj-spellchecker

Spell Checker in Clojure
Clojure
3
star
54

bekanjoos

💰 Be Kanjoos - Get an alert when your favorite products become cheaper!
Java
3
star
55

advent-of-code-2016

Working through Advent of Code challenges!
Clojure
2
star
56

4clojure-solutions

My solutions to the 4clojure problems
Clojure
2
star
57

cv

Latest copy of my CV
2
star
58

cluster-frontend

sample frontend for a cluster system
JavaScript
2
star
59

flask-tuts

Flask Tuts accompanying code files
Python
2
star
60

clojure-by-example

Under Construction
JavaScript
2
star
61

practical-common-lisp

Working my way through PCL - http://www.gigamonkeys.com/book/
Common Lisp
2
star
62

CS253

Web App Engg Course on Udacity
Python
2
star
63

YQLflickr

2
star
64

ADS

Jepsen test for RethinkDB
Clojure
2
star
65

qotd

Pearls of wisdom from a GPT model
Python
2
star
66

DailyProgrammer

Solving the DailyProgrammer Subreddit
Python
1
star
67

researchportal

Research and Conferences Management Portal
PHP
1
star
68

Stripe-CTF

Solutions to Stripe-CTF 3
Python
1
star
69

bingbing

COMS E6111 Advanced Database Systems - Project 1
Python
1
star
70

Course-Registration-Portal

Course Registration Portal @ IIM Calcutta
JavaScript
1
star
71

datastructures_ruby

Ruby
1
star
72

Vim-Configuration

My Gvim Config
Vim Script
1
star
73

techmashup

1
star
74

Associative-Array-in-C

Associative Array in C
C
1
star
75

Blogera

Postman for your blogs
JavaScript
1
star
76

Stanford-Etalks

Notes from Stanford E-Talks
1
star
77

watchtower-docker-extension

An extension for automating Docker container base image updates
TypeScript
1
star
78

CourseWeb

Course Web Portal @ IIM Calcutta
PHP
1
star
79

idx-templates

Java
1
star
80

WordCounter

Word Count Valdidator for Animoto
Python
1
star
81

dominion

Site for Dominion - IIMC Consult Club
JavaScript
1
star
82

rubykoan

Solving Ruby Koans
Ruby
1
star
83

Study-Group

Learning CS and SE
1
star
84

lisp-koans

On the road to enlightment
Common Lisp
1
star
85

react-todos

JavaScript
1
star
86

advent-of-code

My solutions to advent-of-code challenges in Clojure
Clojure
1
star
87

99-problems

I got 99 problems, but FP ain't one! 👯
1
star
88

ratingsystem

Ruby
1
star