ArticleslgStudy

science

Value restriction

Value restriction is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Value restriction rather than just read about it. In short: 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.

Key takeaways

  • Value restriction belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Value restriction to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Value restriction from memory before moving on to harder problems.

Reference excerpt

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 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 are typing rules for references and related operators in ML-like languages. 𝚛𝚎𝚏:∀α.α→(α 𝚛𝚎𝚏)!:∀α.(α 𝚛𝚎𝚏)→α:=:∀α.(α 𝚛𝚎𝚏)→α→𝚞𝚗𝚒𝚝 The operators have the following semantics: 𝚛𝚎𝚏 takes a value and creates a reference containing that value, ! (dereference) takes a reference and reads the value in that reference, and := (assignment) updates a reference to contain a new value and returns a value of the unit type. Given these, the following program 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 ∀α.(α→α) 𝚛𝚎𝚏, which is then instantiated to be of the type (𝚒𝚗𝚝→𝚒𝚗𝚝) 𝚛𝚎𝚏 when typing the assignment c := (fn x => x + 1), and (𝚋𝚘𝚘𝚕→𝚋𝚘𝚘𝚕) 𝚛𝚎𝚏 ref when typing the dereference !c true.

The value restriction Under the value restriction, the types of let bound expressions are only generalized if the expressions are syntactic values. In his paper, 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. In particular, applications of the 𝚛𝚎𝚏 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 (α→α) 𝚛𝚎𝚏. This type is not generalized and α 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 (𝚒𝚗𝚝→𝚒𝚗𝚝) 𝚛𝚎𝚏 via unification. The dereference !c is typed as 𝚒𝚗𝚝→𝚒𝚗𝚝, but is applied to a value of type 𝚋𝚘𝚘𝚕, and the type checker rejects the program.

See also Hindley–Milner type inference

References

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 Value Restriction — MLton Notes on SML97's Value Restriction — Principles of Programming Languages, Geoffrey Smith, Florida International University

Worked examples

Example 1 — a first encounter with Value restriction

Start with the simplest possible case. Write down what Value restriction claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Value restriction before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Value restriction ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Value restriction

In research
Value restriction appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Value restriction in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Value restriction is common in secondary-school and first-year university syllabi. It links to neighbouring topics Type inference, so understanding it makes those chapters shorter.
In everyday life
Look for Value restriction outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Value restriction” →

How to study Value restriction in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Value restriction means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Value restriction out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Value restriction in simple terms?

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 p…

Why does Value restriction matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Value restriction?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Value restriction.

Tags

  • Type inference

Keep exploring