Implementing Algorithm W for Hindley-Milner Type Inference in TypeScript

Added:

Setup & Teardown
Handle Variables
Abstraction Rule
Application Rule
Let Binding Rule
Core Testing
Built-in Types
Adding Syntax
Polymorphism Shown
Curried Functions

Setup & Teardown

0:00
Playing Section
  • 1

    Review previous setup: types, parser, helpers.

  • 2

    Define the goal: implement algorithm W.

  • 3

    Recap the required data structures and functions.

Familiarity with the Lambda Calculus and Abstract Syntax Trees (ASTs) for representing program structure.
Understanding the basics of Parametric Polymorphism and the distinction between monomorphic and polymorphic types.
Knowledge of Robinson's Unification Algorithm and how substitution works to solve type equations.
Intermediate proficiency in TypeScript, particularly using discriminated unions to represent algebraic data types and writing recursive functions.
Implementing Algorithm J, an imperative and more efficient variation of Algorithm W that uses mutable state (type variables with union-find) to avoid explicit substitutions.
Extending the type inference engine to support advanced language features like Row Polymorphism, Type Classes, or Higher-Kinded Types.
Exploring Bidirectional Type Inference to handle language features that pure Hindley-Milner struggles with, such as subtyping or explicit type annotations.
Integrating the implemented type checker into a complete toy compiler or interpreter pipeline (from lexing/parsing to execution).
1.7K views37likes19:46@adam-jonesOriginal Release: 2023-02-22

Algorithm W is a type inference algorithm for Hindley-Milner type systems that automatically determines the most general type of an expression by recursively processing four expression cases: variable expressions (lookup in typing environment), abstraction expressions (introduce new type variable for parameter and form function type), application expressions (unify function type with argument type), and let-in expressions (generalize type over context to enable polymorphism). The algorithm returns a substitution that captures type constraints and a monotype representing the inferred type, enabling type inference without explicit type annotations.