Linear types for the masses

Arnaud Spiwack

Linear types

{-# LANGUAGE LinearTypes #-}

f :: a ⊸ b
f x = … -- consumes x exactly once

Since GHC 9.0

Linear types, by examples (1/3)

id x = x

linear

dup x = (x,x)

not linear

swap (x,y) = (y,x)

linear

forget x = ()

not linear

Linear types, by examples (2/3)

f (Left x) = x
f (Right y) = y

linear

linear

not linear

h x b = case b of
  True -> x
  False -> x
g z = case z of
  Left x -> x
  Right y -> y
k x b = case b of
  True -> x
  False -> ()

linear

Linear types, by examples (3/3)

f x = dup x

linear

not linear

h u = u 0
g x = id (id x)
k u = u (u 0)

linear

not linear

Files with typestate

GHC Core

F\omega +

Coercion stuff

Newtypes

Type families

GADTs

Back to files with typestate

The same

(Inter)Mezzo

Doesn't return a new copy of ys

Constraints

show :: Show a => a -> String

A constraint

Prolog-like language

Constrained

With paramodulation

Idea: teach constraints linear logic

Typestate with linear constraints

https://slides.com/aspiwack/pps2021

https://www.tweag.io/blog/tags/linear-types/

\chi

https://arxiv.org/abs/2103.06127

Linear types for the masses

By Arnaud Spiwack

Linear types for the masses

I’ve been working, in the past few years, at extending a production-grade compiler (namely GHC, the main Haskell compiler) with linear types. This has the potential of letting linear-logic-style type systems touch a wider public. I’ll be speaking about my experience, and will discuss the newest results on making linear typing easier to use: namely linear constraints, by which we extend Haskell’s type class mechanism to linear logic.

  • 544