Container Unification

Folkert de Vries, Sven-Bodo Scholz, Sjaak Smeters

 september 2020

for uniqueness types

Uniqueness Types

Track in the

type

that a value has just one pointer to it

at runtime

System 1: Clean

System 2: UTS

  • defined on the lambda calculus
  • easier to add advanced type system features

Uniqueness

unique

exactly 1 reference

\texttt{MyType}^\bullet

non-unique

many references

\texttt{MyType}^\times

Identity

\texttt{identity} = \lambda x. x

Duplicate

\texttt{duplicate} = \lambda x. (x, x)

Uniqueness Relations

non-unique container

unique

element

UTS

idea: use logic expressions to encode relations

\bullet \text{: unique \& boolean True}
\times \text{: non-unique \& boolean False}

UTS

idea: use logic expressions to encode relations

\texttt{fst} :: (t^u, s^v)^{u \lor w} \to t^u

My Problem

\texttt{swap} :: (s^v, t^u)^{u \lor v \lor w} \to (t^u, s^v)^{w'}
\texttt{swap r = (snd r} \texttt{, fst r} \texttt{)}
\texttt{swap} :: (s^{(\lnot v \land u) \lor (\lnot v \land w) \lor (u1 \land u) \lor (u1 \land w)}, t^u)^{u \lor w} \to (t^u, s^{(\lnot v \land u) \lor (\lnot v \land w) \lor (u1 \land u) \lor (u1 \land w)})^{v1}

Boolean Unification

idea: when are two formulae equivalent?

finds the (most-general) unifier

u1 \mapsto (\neg u2 \land v1) \lor (\neg u2 \land v2) \lor (u1 \land v1) \lor (u1 \land v2) \\ u2 \mapsto (u2 \land v1) \lor (u2 \land v2)
u1 \lor u2 \doteq v1 \lor v2

Boolean Unification

claim: not a good fit in practice

😕

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\)
( t^u, s^v)^{u \lor v \lor w }

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

u \lor v \lor w \doteq \bullet
w \geq u, v
\doteq \bullet

Disjunction & Unique

Disjunction & Disjunction

u_1 \geq u_2 \doteq v_1 \geq v_2
  • unify dominators
  • accumulate dominated
v_1 \geq v_2, u_2 \text{ where } [ u_1 \mapsto v_ 1 ]

Polymorphic records

\{ name :: String ~|~ a \} \doteq \{ age :: Int~|~ b \}
\{ age :: Int, name :: String ~|~ c \}

unifies to:

"at least field \(name\) and at least field \(age\)"

A Polymorphic Type System for Extensible Records and Variants (Gaster & Jones, 1996)

w \geq u, v \Rightarrow (w \geq \{ u, v | \alpha \})

Container Annotation

Record-Inspired Unification

(w_2, \{ u_1, \dots u_n, v_1, \dots v_m ~|~ \gamma \})

Container Unification

rules

Swap

with container unification

\texttt{swap} :: (s^v, t^u)^{(w \geq \{ u, v | \alpha \} )} \to (t^u, s^v)^{w'}

Conclusion

\texttt{swap} :: (s^v, t^u)^{(w \geq \{ u, v | \alpha \} )} \to (t^u, s^v)^{w'}
\texttt{swap} :: (s^{(\lnot v \land u) \lor (\lnot v \land w) \lor (u1 \land u) \lor (u1 \land w)}, t^u)^{u \lor w} \to (t^u, s^{(\lnot v \land u) \lor (\lnot v \land w) \lor (u1 \land u) \lor (u1 \land w)})^{v1}
  • introduce hierarchy
  • steal unification trick from records

IFL

By folkert de vries