Skip to main content
QUICK REVIEW

[Paper Review] Linear Haskell: practical linearity in a higher-order polymorphic language

Jean-Philippe Bernardy, Mathieu Boespflug|arXiv (Cornell University)|Oct 26, 2017
Logic, programming, and type systemsComputer Science20 citations
TL;DR

This paper introduces Linear Haskell, a non-invasive extension to Haskell that integrates linear types via linearity annotations on function arrows, enabling safe mutation and resource protocol enforcement without breaking existing code. The key contribution is a practical, backwards-compatible design that supports linearity polymorphism and seamless interoperation between linear and non-linear code, demonstrated via a prototype in GHC with formal semantics and real-world use cases.

ABSTRACT

Linear type systems have a long and storied history, but not a clear path forward to integrate with existing languages such as OCaml or Haskell. In this paper, we study a linear type system designed with two crucial properties in mind: backwards-compatibility and code reuse across linear and non-linear users of a library. Only then can the benefits of linear types permeate conventional functional programming. Rather than bifurcate types into linear and non-linear counterparts, we instead attach linearity to function arrows. Linear functions can receive inputs from linearly-bound values, but can also operate over unrestricted, regular values. To demonstrate the efficacy of our linear type system - both how easy it can be integrated in an existing language implementation and how streamlined it makes it to write programs with linear types - we implemented our type system in GHC, the leading Haskell compiler, and demonstrate two kinds of applications of linear types: mutable data with pure interfaces; and enforcing protocols in I/O-performing functions.

Motivation & Objective

  • To design a practical linear type system for Haskell that preserves backwards compatibility and code reuse between linear and non-linear code.
  • To address the challenge of integrating linear types into a mainstream functional language without requiring invasive changes to existing libraries or programs.
  • To enable safe, efficient mutation of data structures (e.g., arrays) and enforcement of resource usage protocols (e.g., file I/O, sockets) using static type checking.
  • To demonstrate that linear types can be integrated into a lazy, higher-order, polymorphic language like Haskell without sacrificing performance or expressiveness.

Proposed method

  • Annotating function arrows with linearity, so functions consume their arguments exactly once, while allowing unrestricted (non-linear) values to coexist.
  • Extending the type system to allow linear and non-linear values in the same data types, enabling mixed-linearity data structures.
  • Supporting linearity polymorphism by generalizing standard higher-order functions (e.g., map, compose) to work uniformly across linear and non-linear contexts.
  • Formalizing the system in a core calculus with progress and preservation properties, ensuring type safety.
  • Implementing a prototype in GHC that supports linearity inference and integrates with the existing compiler stack.
  • Demonstrating the system through case studies: mutable arrays with pure interfaces and I/O protocols for files and sockets.

Experimental results

Research questions

  • RQ1Can linear types be integrated into an existing, widely used language like Haskell without breaking backwards compatibility or requiring code duplication?
  • RQ2How can linearity be applied to function arrows in a way that supports both linear and non-linear values in the same program and data structures?
  • RQ3Can linearity polymorphism be achieved so that common higher-order functions work uniformly across linear and non-linear code?
  • RQ4Can the integration of linear types be achieved with minimal changes to the compiler and runtime, preserving performance and semantics of existing programs?
  • RQ5Can real-world use cases—such as safe mutable data structures and resource usage protocols—be effectively expressed and enforced using this system?

Key findings

  • The proposed system successfully extends Haskell with linear types in a backwards-compatible way, allowing existing programs to typecheck unchanged.
  • Linear functions can now consume both linear and unrestricted values, enabling safe mutation and efficient in-place updates without runtime overhead.
  • The system supports linearity polymorphism, allowing functions like map and compose to be reused across linear and non-linear contexts with no code duplication.
  • The prototype implementation in GHC demonstrates that low-level features like mutable arrays and off-heap data structures can be safely exposed via pure, linear interfaces.
  • Case studies show that linear types prevent common bugs such as resource misuse (e.g., using a closed file) and incorrect stream consumption in I/O.
  • The formal core calculus satisfies standard type safety properties, including progress and preservation, validating the soundness of the design.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.