Course contentsShow
← Computer Science
Lesson 2013 of 2,87245. Type Systems and SemanticsFree lesson

Let-Polymorphism

How let bindings enable polymorphic type inference without explicit quantifiers.

Let-Polymorphism

You'll learn how let bindings enable polymorphic type inference without writing explicit type quantifiers.

What is Let-Polymorphism?

In the Hindley-Milner type system (which you just learned), let-polymorphism is a special rule that allows variables bound in let expressions to have polymorphic types automatically. This means the type checker can generalize a type at the let binding and instantiate it differently at each use—all without you writing any type annotations.

The Core Mechanism

When you write something like:

let id = fun x -> x in
(id 5, id true)

The type inference engine:

  1. Infers id has type α → α (using a type variable)
  2. Generalizes it to ∀α. α → α at the let binding
  3. Instantiates it with fresh type variables at each use:
    • id 5 uses Int → Int
    • id true uses Bool → Bool

This is different from just passing id as a parameter to a function—arguments don't get automatically generalized, only let-bound names do.

Why "Let" Specifically?

The algorithm treats let specially because it marks a definition scope. Inside the body of the let, the bound variable can be used polymorphically. This restriction (called the value restriction in some contexts) ensures type soundness while keeping inference decidable.

Key Takeaway:
Let-polymorphism allows let-bound variables to be automatically generalized and reused at multiple types, giving you parametric polymorphism without explicit type quantifiers.