A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Linear Types

Research Depth 94 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
513prerequisites beneath it
See this on the map →
Type Systems OverviewCurry-Howard Correspondence→→Session Types
linear-logic affine-types resource-management ownership rust-borrow-checker

Core Idea

Linear types enforce that values are used exactly once: they cannot be duplicated (used twice) or discarded (unused). This discipline, rooted in Girard's linear logic, treats values as consumable resources rather than freely copyable data. Linear types statically guarantee resource management properties — file handles are closed exactly once, memory is freed exactly once, protocol steps are followed in order. Rust's ownership and borrow-checking system is the most commercially successful application of linear (specifically, affine) type discipline, preventing use-after-free, double-free, and data races at compile time.

Explainer

In conventional type systems, values can be freely copied and discarded. You can pass the same variable to multiple functions, bind it to several names, or simply ignore it. This works for pure data (integers, strings) but is problematic for resources — entities whose lifecycle must be carefully managed. A file handle must be closed exactly once: failing to close it leaks the resource; closing it twice causes a runtime error. A memory allocation must be freed exactly once: forgetting causes a leak; freeing twice corrupts the heap. Linear types enforce this discipline statically by requiring that every value is consumed exactly once.

The theoretical foundation is Girard's linear logic (1987), which treats logical hypotheses as resources that are consumed by use. In classical logic, you can use a premise as many times as you like; in linear logic, each premise is available for exactly one use unless explicitly marked as reusable (with the ! modality). Through the Curry-Howard correspondence, this gives rise to linear type theory: values of linear type cannot be duplicated (used more than once) or weakened (discarded without use). The type system statically tracks that every linear value has exactly one consumer.

Affine types relax the constraint to "at most once" — values can be discarded but not duplicated. Relevant types enforce "at least once" — values cannot be discarded but can be duplicated. Linear is the intersection: exactly once. In practice, affine types are more ergonomic than strictly linear types because they allow values to go out of scope (with automatic cleanup via destructors), and this is the variant most languages adopt.

Rust is the most commercially successful application of these ideas. Rust's ownership system is affine: each value has exactly one owner, and when the owner goes out of scope, the value is dropped (its destructor runs). Ownership can be transferred (moved) but not copied (for non-Copy types). Rust adds borrowing — temporary references that do not transfer ownership — with the constraint that you can have either multiple shared immutable borrows (&T) or one exclusive mutable borrow (&mut T), but not both simultaneously. This combination prevents use-after-free, double-free, and data races at compile time, with zero runtime overhead.

Beyond memory safety, linear types enable protocol enforcement. A session type (a closely related concept) describes the sequence of operations on a communication channel; linearity ensures the protocol is followed step by step. A typestate system uses linear types to track object state (file open vs. closed, connection established vs. disconnected) and prevent invalid operations (reading from a closed file). Linear types also enable safe manual memory management without a garbage collector: since the type system ensures every allocation has exactly one owner and is freed exactly once, memory safety is guaranteed statically. This is the core value proposition of Rust and similar systems.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Type Systems Overview → Curry-Howard Correspondence → Linear Types

Longest path: 95 steps · 513 total prerequisite topics

Prerequisites (2)

Leads To (1)