Value restriction

This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)

This article includes a list of general references, but it lacks sufficient corresponding inline citations. Please help to improve this article by introducing more precise citations. (July 2018) (Learn how and when to remove this template message)

|

This article provides insufficient context for those unfamiliar with the subject. Please help improve the article by providing more context for the reader. (July 2018) (Learn how and when to remove this template message)

|

(Learn how and when to remove this template message)

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.

## A Counter Example to Type Safety[edit]

In the Hindley–Milner type system, expressions can be given multiple types through parametric polymorphism. 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.

r e f : ∀ α . α -> ( α r e f ) ! : ∀ α . ( α r e f ) -> α := : ∀ α . ( α r e f ) -> α -> u n i t {\displaystyle {\begin{aligned}{\mathtt {ref}}&:\forall \alpha .\alpha \to (\alpha \ {\mathtt {ref}})\\\\{\mathtt {!}}&:\forall \alpha .(\alpha \ {\mathtt {ref}})\to \alpha \\\\{\mathtt {:=}}&:\forall \alpha .(\alpha \ {\mathtt {ref}})\to \alpha \to {\mathtt {unit}}\end{aligned}}}

The operators have the following semantics:  r e f {\textstyle {\mathtt {ref}}} takes a value and creates a reference containing that value,  ! {\textstyle {\mathtt {!}}} (dereference) takes a reference and reads the value in that reference, and  := {\textstyle {\mathtt {:=}}} (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.

let val c = ref (fn x => x)
in  c := (fn x => x + 1);
!c true
end

The above program type checks using Hindley-Milner because `c` is given the type  ∀ α . ( α -> α ) r e f {\textstyle \forall \alpha .(\alpha \to \alpha )\ {\mathtt {ref}}} , which is then instantiated to be of the type  ( i n t -> i n t ) r e f {\textstyle ({\mathtt {int}}\to {\mathtt {int}})\ {\mathtt {ref}}} when typing the assignment `c := (fn x => x + 1)`, and  ( b o o l -> b o o l ) r e f {\textstyle ({\mathtt {bool}}\to {\mathtt {bool}})\ {\mathtt {ref}}} ref when typing the dereference `!c true`.

## The Value Restriction[edit]

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,  λ {\textstyle \lambda } -expressions and constructors applied to values. The function and operator applications are not considered values. In particular, applications of the  r e f {\displaystyle {\mathtt {ref}}} operator are not generalized. 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.

* First `c` is given the type  ( α -> α ) r e f {\textstyle (\alpha \to \alpha )\ {\mathtt {ref}}} . This type is not generalized and  α {\textstyle \alpha } is a free variable in the typing context for the body of the let binding.
* When the assignment is typed, the type of `c` is modified in the typing context to be of type  ( i n t -> i n t ) r e f {\textstyle ({\mathtt {int}}\to {\mathtt {int}})\ {\mathtt {ref}}} via unification.
* The dereference `!c` is typed as  i n t -> i n t {\displaystyle {\mathtt {int}}\to {\mathtt {int}}} , but is applied to a value of type  b o o l {\textstyle {\mathtt {bool}}} , and the type checker rejects the program.

## See also[edit]

* Hindley-Milner type inference

## References[edit]

1. ^ a b c Wright, Andrew K. (1995-12-01). "Simple imperative polymorphism". LISP and Symbolic Computation. 8 (4): 343–355. doi:10.1007/BF01018828. ISSN 1573-0557. S2CID 19286877.

* Mads Tofte (1988). Operational Semantics and Polymorphic Type Inference. PhD thesis.
* M. Tofte (1990). "Type inference for polymorphic references".
* O'Toole (1990). "Type Abstraction Rules for Reference: A Comparison of Four Which Have Achieved Notoriety".
* Xavier Leroy & Pierre Weis (1991). "Polymorphic type inference and assignment". POPL '91.
* A. K. Wright (1992). "Typing references by effect inference".
* My Hoang, John C. Mitchell and Ramesh Viswanathan (1993). "Standard ML-NJ weak polymorphism and imperative constructs".
* Andrew Wright (1995). "Simple imperative polymorphism". In LISP and Symbolic Computation, p. 343–356.
* Jacques Garrigue (2004). "Relaxing the Value Restriction".

## External links[edit]

* Value Restriction -- MLton
* Notes on SML97's Value Restriction -- Principles of Programming Languages, Geoffrey Smith, Florida International University