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 CorrespondenceSession 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication 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 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsLogic Gates FundamentalsImplementing Boolean Functions with GatesKarnaugh Map SimplificationCombinational Circuit DesignFlip-Flops and LatchesFinite State Machines (FSMs)Deterministic Finite Automata (DFA)Nondeterministic Finite Automata (NFA)Two-Way Finite AutomataNFA to DFA Conversion (Subset Construction)DFA Properties and Minimization AlgorithmsRegular Languages: Definition and CharacterizationContext-Free Grammars (CFGs)Context-Free Grammar Properties and AmbiguityParse Trees, Derivations, and Ambiguity in CFGsContext-Free Grammars in Compiler DesignAbstract Syntax Trees (ASTs)Symbol Tables and Scope ResolutionSemantic Analysis PhaseType Systems OverviewCurry-Howard CorrespondenceLinear Types

Longest path: 95 steps · 513 total prerequisite topics

Prerequisites (2)

Leads To (1)