Let-Polymorphism and the Value Restriction

Explain when a let-bound definition can be reused at several types and how mutable allocation changes the rule.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Each polymorphic use chooses its own type
  2. A mutable cell has one concrete history

Each polymorphic use chooses its own type

In pure Hindley–Milner, a binding of identity can generalize its fresh argument type, giving the scheme \(\forall a.\,a\to a\). Two uses within the same expression can then instantiate it separately for an integer and a Boolean. Variables already fixed by the outer typing environment stay shared. This is part of type inference, where generalization and ordinary application have different jobs.

A mutable cell has one concrete history

Allocating a cell creates one location shared by all uses of the binding. If its element type could be instantiated independently at different uses, type checking could assign incompatible views to that same location. A value restriction limits generalization of effectful expressions, with details depending on the language. The issue can be understood without any unsafe execution. Track the single store location under reference and ownership rules, then distinguish allocation of the cell from reusable function definitions that allocate separate cells when called.[1][2]

References

  1. Luís Damas and Robin Milner, Principal Type-Schemes for Functional Programs ↩
  2. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩