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

Made with Slides.com