ArticleslgStudy

science

Flow-sensitive typing

Flow-sensitive typing 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 Flow-sensitive typing rather than just read about it. In short: In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow. In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it.

Key takeaways

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

Reference excerpt

In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow. In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it. However, in flow-sensitive typing, an expression's type may be updated to a more specific type if it follows an operation that validates its type. Validating operations can include type predicates, imperative updates, and control flow.

Examples

Ceylon This example in Ceylon illustrates the concept:

and which outputs:

Kotlin This example is in Kotlin:

Benefits This technique coupled with type inference reduces the need for writing type annotations for all variables or to do type casting, like is seen with dynamic languages that use duck typing. It reduces verbosity and makes for terser code, easier to read and modify. It can also help language implementers provide implementations that execute dynamic languages faster by predicting the type of objects statically. Finally, it increases type safety and can prevent problems due to null pointers, labeled by C.A.R. Hoare—the null reference inventor—as "the billion dollar mistake" From a Programming Languages perspective, it's reasonable to say that flow-sensitive typing is the feature that finally made it possible to build usable type-safe programming languages with union types and without rampant dynamic checking. Until this event, attempts to add this feature to languages such as Scheme generally resulted in intractably large type representations. One example of a system with limited support for union types is Wright and Cartwright's Soft Scheme.

History and Implementations The history of the idea goes back at least to Tony Hoare's "record class discriminators" from the mid-1960s. However, practical type systems including it as a feature are much more recent. Typed Scheme, a type system for Scheme developed by Sam Tobin-Hochstadt and first published in 2008, was the first type system to include occurrence typing. Its successor, Typed Racket (a dialect of Racket), is also based on occurrence typing. Shortly after Typed Scheme, David J. Pearce independently reinvented flow-typing in Whiley. Typed JavaScript observed that in "scripting" languages, flow-typing depends on more than conditional predicates; it also depends on state and control flow. This style has since been adopted in languages like Ceylon, TypeScript and Facebook Flow. There are also a few languages that don't have union types but do have nullable types, that have a limited form of this feature that only applies to nullable types, such as C#, Kotlin, and Lobster.

Alternatives Pattern matching reaches the same goals as flow-sensitive typing, namely reducing verbosity and making up for terser code, easier to read and modify. It achieves this is in a different way, it allows to match the type of a structure, extract data out of it at the same time by declaring new variable. As such, it reduces the ceremony around type casting and value extraction. Pattern matching works best when used in conjunction with algebraic data types because all the cases can be enumerated and statically checked by the compiler. See this example mock for Java:

In a statically typed language, the advantage of pattern matching over flow-sensitive typing is that the type of a variable always stays the same: it does not change depending on control flow. When writing down the pattern to be matched, a new variable is declared that will have the new type.

References

Worked examples

Example 1 — a first encounter with Flow-sensitive typing

Start with the simplest possible case. Write down what Flow-sensitive typing 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 Flow-sensitive typing 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 Flow-sensitive typing 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 Flow-sensitive typing

In research
Flow-sensitive typing 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 Flow-sensitive typing 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
Flow-sensitive typing is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Program analysis, Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Flow-sensitive typing 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 “Flow-sensitive typing” →

How to study Flow-sensitive typing in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Flow-sensitive typing 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 Flow-sensitive typing out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Flow-sensitive typing in simple terms?

In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow. In statically typed languages, a type of an expression is determined by the types of the sub-expressions…

Why does Flow-sensitive typing 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 Flow-sensitive typing?

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 Flow-sensitive typing.

Tags

  • Data types
  • Program analysis
  • Programming language comparisons
  • Type systems
  • Type theory

Keep exploring