In programming languages with Hindley-Milner type inference and imperative features, in particular the ML programming language family, the value restriction means that declarations are only polymorphically generalized if they are syntactic values (also called non-expansive).
The value restriction prevents reference cells from holding values of different types and preserves type safety.
But naively giving multiple types to references breaks type safety.
The following[1] are typing rules for references and related operators in ML-like languages.
(assignment) updates a reference to contain a new value and returns a value of the unit type.
Given these, the following program[1] unsoundly applies a function meant for integers to a Boolean value.The above program type checks using Hindley-Milner because c is given the type
when typing the assignment c := (fn x => x + 1), and
ref when typing the dereference !c true.
Under the value restriction, the types of let bound expressions are only generalized if the expressions are syntactic values.
In his paper,[1] Wright considers the following to be syntactic values: constants, variables,
-expressions and constructors applied to values.
The function and operator applications are not considered values.
It is safe to generalize type variables of syntactic values because their evaluation cannot cause any side-effects such as writing to a reference.
The above example is rejected by the type checker under the value restriction as follows.