Linear Types

Research Depth 68 in the knowledge graph I know this Set as goal
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

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionTime and Space ComplexityAmortized AnalysisHash TablesSymbol Tables and Scope ResolutionSemantic Analysis PhaseType Systems OverviewCurry-Howard CorrespondenceLinear Types

Longest path: 69 steps · 381 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.