Linear Haskell: practical linearity in a higher-order polymorphic language

Jean-Philippe Bernardy, Mathieu Boespflug, Ryan R. Newton, Simon Peyton Jones, Arnaud Spiwack

This paper, a landmark in functional programming research, describes the design and implementation of Linear Haskell, an extension to the Haskell language that introduces linear types. By ensuring certain values are "consumed exactly once," Linear Haskell bridges the gap between high-level functional purity and low-level resource management.

TL;DR

Linear Haskell introduces a practical, backwards-compatible way to integrate linear types into a mature language by attaching linearity to function arrows rather than types. This allows developers to write high-performance code with safe in-place updates and strictly enforced I/O protocols while reusing existing libraries and codebases.

What Problem Does This Paper Address?

In standard functional languages like Haskell, values can be used any number of times (zero, one, or many). While this is flexible, it makes it difficult to manage resources like file handles, network sockets, or large memory buffers safely and efficiently.

If you want to update an array "in-place" for performance, you must ensure no other part of the program holds a reference to the old version—otherwise, you break referential transparency. Traditionally, Haskell handles this with complex "Monads" (like the ST monad), which force a strict, sequential execution order that can be cumbersome and limits parallelism.

Previous attempts to add linear types (which guarantee a value is used exactly once) often required "bifurcating" the language—creating two versions of every library (one linear, one not). The authors set out to find a "non-invasive" way to add linearity that allows for massive code reuse and doesn't break the existing 25-year-old Haskell ecosystem.

Methods

The core innovation of the paper is linearity-on-the-arrow. Instead of saying a type is linear, they say a function is linear.

  • The Linear Arrow ($\multimap$): A function $f :: s \multimap t$ guarantees that if its result is consumed exactly once, then its argument $s$ is also consumed exactly once.
  • Multiplicity Polymorphism: To avoid writing two versions of a function like map, the authors introduced a way for functions to be polymorphic over "multiplicity" ($p$). A function can have the type $a \to_p b$, where $p$ can be $1$ (linear) or $\omega$ (unrestricted).
  • $\lambda^q_\to$ Calculus: The authors developed a formal core calculus where multiplicities form a semi-ring. This mathematical foundation allows the compiler to "count" how many times a variable is used across different branches of code (like case expressions).
  • GHC Implementation: They implemented a prototype of this system in the Glasgow Haskell Compiler (GHC), the industry standard for Haskell development.

Datasets

As a programming languages paper, the "datasets" consist of existing codebases and custom benchmarks:

  • Backwards Compatibility Test: The authors compiled the standard GHC libraries and the nofib benchmark suite—totaling 195,000 lines of Haskell code—to prove that the extension doesn't break existing programs.
  • Performance Benchmarks: They created benchmarks for processing serialized binary data (binary trees) to compare linear types against traditional monadic approaches.

Key Insights and Ideas

  • The "Aha" Moment on Arrows: By putting linearity on the arrow, the authors realized that an unrestricted value can always be passed to a linear function, but a linear value can only be passed to a linear function. This "subsumption" is what makes the system backwards compatible.
  • Safe Mutation in Pure Code: Linear types allow for "pure" interfaces to "impure" operations. You can have a function that looks pure but performs an in-place array update under the hood, because the compiler guarantees no one else is looking at that array.
  • Protocol Enforcement: Linearity can turn "documentation" into "compiler errors." For example, a socket API can be designed so that the close function must be called exactly once, and the socket cannot be used after it is closed.

Key Results

  • Zero-Cost Safety: The system provides static guarantees with no runtime overhead.
  • Massive Speedups: In benchmarks for processing serialized data, the Linear Haskell version was over 20x faster than the traditional "unpack-process-repack" approach on large trees. This is because linear types allowed the program to operate directly on the binary buffer without allocating new heap objects.
  • Unobtrusive Integration: The implementation required modifying only about 1,100 lines of GHC's 100,000+ line codebase, demonstrating that the design fits naturally into the existing compiler architecture.

Limitations and Future Work

  • Type Inference: While the system works, the current prototype requires more explicit type annotations than standard Haskell. Full "linearity inference" remains an open research challenge.
  • Borrowing: The authors acknowledge that "threading" linear variables through a long chain of functions can be tedious. They suggest that a "borrowing" system (similar to Rust's) could be built on top of Linear Haskell to make it more ergonomic.
  • Standard Library Migration: While the system is compatible, the full benefit will only be realized once the broader Haskell ecosystem begins adopting linear types in its core APIs.

Summary updated

BibTeX
@article{bernardy2018linear,
  author = {Jean-Philippe Bernardy and Mathieu Boespflug and Ryan R. Newton and Simon Peyton Jones and Arnaud Spiwack},
  title = {{Linear Haskell: practical linearity in a higher-order polymorphic language}},
  journal = {Proc. ACM Program. Lang.},
  year = {2018},
  doi = {10.1145/3158093},
}
Figure