Inferring uniqueness types
Folkert de Vries, august 2020
Pure FP: the ideal
Pure FP: Impementation
Referential transparency
An expression is
referentially transparent
when it can always be replaced by its definition without changing the program output
Referential transparency
copy original, modify copy
time | space | ||
---|---|---|---|
Array set element | O(1) | O(1) | ๐ |
Array copy & set element | O(n) | O(n) | ๐ |
but copying is expensive:
we can modify values
ifย theyย are no longer used
we can modify values
ifย theyย are dead
1. Reclaim Memory
i.e. garbage collection
2. Destructive Updates
if "dead", modify in-place
Runtime
compile time
- accurate
- runtime overhead
- unpredictable
- less precise
- no overhead
- statically guaranteed
System 1: Clean
System 2: UTS
Uniqueness Types
idea: annotate base types with a uniqueness attribute
values of a unique type can be updated destructively
unique string
non-unique string
Uniqueness Relations
UTS
idea: use logic expressions to encode relations
UTS
idea: use logic expressions to encode relations
My Problem
UTS inference is slow, and infers large types
My Problem
Unification
idea: when are two types equivalent?
well-typed if the type of \(x\) matches the type of \(f\)'s argument
Unification
idea: when are two types equivalent?
Unification
idea: when are two types equivalent?
syntactic unification is compositional
(barring the occurs check)
Boolean Unification
idea: when are two formulae equivalent?
finds the (most-general) unifier
Boolean Unification
claim: not a good fit in practice
- unneeded connectives are introduced
- signatures become large
- simplifying formulae is expensive
๐
Toward Solutions
faster unification of uniqueness attributes
๐
Disjunctions are for Containers
There is a hierarchy between variables
- \(u, v\) are lower boundsย
- define \( w \) as an upper bound
- \(w\) dominates \(u\) and \(v\)
There is a hierarchy between variables
Disjunction & Unique
we don't know which variable will be \(\bullet\), therefore have to keep all options available in the unifier
now we can immediately pick \(w = \bullet\)
Disjunction & Unique
Disjunction & Disjunction
- unify dominators
- accumulate dominated
Polymorphic records
unifies to:
"at least field \(name\) and at least field \(age\)"
A Polymorphic Type System for Extensible Records and Variants (Gaster & Jones, 1996)
Container Annotation
Record-Inspired Unification
Container Unification
rules
Container Unification
deriving swap
Container Unification
deriving swap
Container Unification
deriving swap
Container Unification
deriving swap
inferred
inferred
Technical Summary
Technical Summary
pure functional programs can be slow
Technical Summary
dead values can be modified destructively
Technical Summary
uniqueness type enable this
(and more)
Technical Summary
UTS is slow & infers large types
Technical Summary
Improved unification solves that
Thanks to:
Sven-Bodo Scholz
Sjaak Smetsers
Edsko de Vries
Richard Feldman
Master Thesis
By folkert de vries
Master Thesis
- 53