Arnaud Spiwack
f :: a -> b -- A mathematical function
f x = …
-- Can't mutate x
-- Can't read a file
-- Can't print on stdout
…
e
…
e
…
Both are the same
f x = e
…
f u
…
f x = e
…
e[x\u]
…
{-# LANGUAGE LinearTypes #-}
f :: a ⊸ b
f x = … -- consumes x exactly once
Since GHC 9.0
id x = x
✓
linear
dup x = (x,x)
✗
not linear
swap (x,y) = (y,x)
✓
linear
forget x = ()
✗
not linear
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
f x = dup x
✓
linear
✗
not linear
h u = u 0
g x = id (id x)
k u = u (u 0)
✓
linear
✗
not linear
type IO a = RealWorld™ -> (a, RealWorld™)
readLine :: Handle -> IO String
type IO a = RealWorld™ ⊸ (a, RealWorld™)
readLine :: Handle -> IO String
Code
Stack
Seel also A dissection of L — Arnaud spiwack
https://slides.com/aspiwack/edinburgh-201103
https://www.tweag.io/blog/tags/linear-types/