# Which simple types have a unique inhabitant? - Gallium

```Which simple types have a unique inhabitant?
Gabriel Scherer, Didier R´emy
Gallium – INRIA
March 30, 2015
1
Which types have a unique inhabitant?
in a given type system (STLC with atoms, products and sums)
modulo some program equivalence (βη)
2
Which types have a unique inhabitant?
in a given type system (STLC with atoms, products and sums)
modulo some program equivalence (βη)
Motivation: a principal way to study code inference.
We can infer the instance declaration for the exception monad A 7→ A + E :
X + E → (X → Y + E ) → Y + E
2
STLC with sums
∆, x : A ` t : B
∆`t:A→B
∆ ` λ(x) t : A → B
∆`t:A
∆`u:B
∆ ` (t, u) : A ∗ B
∆`u:A
∆`tu:B
∆ ` t : A1 ∗ A2
∆ ` πi t : Ai
∆, x : A ` x : A
∆ ` t : Ai
∆ ` σi t : A1 + A2
∆ ` t : A1 + A2
∆, x1 : A1 ` u1 : C
∆ ` δ(t, x1 .u1 , x2 .u2 ) : C
3
∆, x2 : A2 ` u2 : C
βη-equivalence
(λ(x) t) u →β t[u/x]
πi (t1 , t2 ) →β ti
(t : A → B) =η λ(x) t x
(t : A ∗ B) =η (π1 t, π2 t)
δ(σi t, x1 .u1 , x2 .u2 ) →β ui [t/xi ]
∀C [],
C [t : A + B] =η δ(t, x.C [σ1 x], x.C [σ2 x])
Equivalence algorithm dedicable — since 1995.
4
You said β-short η-long normal forms?
In presence of negative connectives only (or positive only), focused proof
search enumerates distinct normal forms.
This fails when sums (positives) are mixed with arrows (negatives).
5
The obvious idea...
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
6
The obvious idea... does not work
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
Infinitely many duplicates −→ non-termination.
f : (X → Y + Y ), x : X ` ? : X
6
The obvious idea... does not work
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
Infinitely many duplicates −→ non-termination.
f : (X → Y + Y ), x : X ` ? : X
x
6
The obvious idea... does not work
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
Infinitely many duplicates −→ non-termination.
f : (X → Y + Y ), x : X ` ? : X
x
δ(f x, y1 .x, y2 .x)
6
The obvious idea... does not work
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
Infinitely many duplicates −→ non-termination.
f : (X → Y + Y ), x : X ` ? : X
x
δ(f x, y1 .x, y2 .x)
δ(f x, y1 .δ(f x, z1 .x, z2 .x), y2 .x)
...
6
The obvious idea... does not work
Enumerate all derivations in a reasonable (focused) system.
Remove duplicates using the equivalence algorithm.
Stop if two proofs are found.
Infinitely many duplicates −→ non-termination.
f : (X → Y + Y ), x : X ` ? : X
x
δ(f x, y1 .x, y2 .x)
δ(f x, y1 .δ(f x, z1 .x, z2 .x), y2 .x)
...
We need a more canonical proof search process.
6
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
Computational completeness: all programs
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
Computational completeness: all programs
Unicity completeness: at least two
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
Computational completeness: all programs
Unicity completeness: at least two
Canonicity: no duplicates — more canonical
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
Computational completeness: all programs
Unicity completeness: at least two
Canonicity: no duplicates — more canonical
Termination — of failure
7
Terminology (a contribution?)
We consider search processes of the form “enumerating the derivations of
this restricted system of inference rules”.
Various concepts of interest:
Provability completeness: at least one
Computational completeness: all programs
Unicity completeness: at least two
Canonicity: no duplicates — more canonical
Termination — of failure
Focused proof search: computationally complete, not canonical.
Focused proof search quotiented by equivalence: complete, canonical,
non-terminating.
7
Our approach
We promised an algorithm that decides uniqueness of inhabitation.
We distinguish specification and implementation.
8
Our approach
We promised an algorithm that decides uniqueness of inhabitation.
We distinguish specification and implementation.
Specification: a novel focused logic that is computationally complete
and canonical.
8
Our approach
We promised an algorithm that decides uniqueness of inhabitation.
We distinguish specification and implementation.
Specification: a novel focused logic that is computationally complete
and canonical.
Implementation: a restriction of this logic that is unicity complete and
terminating.
8
The core idea
Some proof rules are inversible and some are not – they lead to dead ends.
A+B `A+B
(Focusing separates invertible and non-invertible steps.)
9
The core idea
Some proof rules are inversible and some are not – they lead to dead ends.
A+B `A+B
(Focusing separates invertible and non-invertible steps.)
To avoid all dead ends, we will saturate by deducing everything we can
(on the left) before any risky move on the right.
“Cut the positives as soon as possible.”
9
Polarized simply-typed lambda-calculus, with non-biased
atoms
A, B, C , D ::=
| X,Y,Z
| P, Q
| N, M
types
atoms
positive types
negative types
P, Q
N, M
::= A + B
::= A → B | A ∗ B
strict positive
strict negative
Pat , Qat
Nat , Mat
::= P, Q | X , Y , Z
::= N, M | X , Y , Z
positive or atomic
negative or atomic
Γ
∆
::= varmap(Nat )
::= varmap(A)
negative or atomic context
general context
10
Focused natural deduction for intuitionistic logic
inv-sum
Γ; ∆, x : A `inv t : C
Γ; ∆, x : B `inv u : C
Γ; ∆, x : A + B `inv δ(x, x.t, x.u) : C
inv-pair
Γ; ∆ `inv t : A
Γ; ∆ `inv u : B
Γ; ∆ `inv (t, u) : A ∗ B
11
inv-arr
Γ; ∆, x : A `inv t : B
Γ; ∆ `inv λ(x) t : A → B
inv-end
Γ, Γ0 `foc t : Pat
Γ; Γ0 `inv t : Pat
Focused natural deduction for intuitionistic logic
inv-sum
Γ; ∆, x : A `inv t : C
Γ; ∆, x : B `inv u : C
Γ; ∆, x : A + B `inv δ(x, x.t, x.u) : C
inv-pair
Γ; ∆ `inv t : A
Γ; ∆ `inv u : B
Γ; ∆ `inv (t, u) : A ∗ B
foc-elim
Γ`n⇓P
Γ; x : P `inv t : Qat
Γ `foc let x = n in t : Qat
inv-arr
Γ; ∆, x : A `inv t : B
Γ; ∆ `inv λ(x) t : A → B
inv-end
Γ, Γ0 `foc t : Pat
Γ; Γ0 `inv t : Pat
foc-intro
Γ`t⇑P
Γ `foc t : P
11
foc-atom
Γ`n⇓X
Γ `foc n : X
Focused natural deduction for intuitionistic logic
inv-sum
Γ; ∆, x : A `inv t : C
Γ; ∆, x : B `inv u : C
Γ; ∆, x : A + B `inv δ(x, x.t, x.u) : C
inv-pair
Γ; ∆ `inv t : A
Γ; ∆ `inv u : B
Γ; ∆ `inv (t, u) : A ∗ B
foc-elim
Γ`n⇓P
Γ; x : P `inv t : Qat
Γ `foc let x = n in t : Qat
inv-arr
Γ; ∆, x : A `inv t : B
Γ; ∆ `inv λ(x) t : A → B
inv-end
Γ, Γ0 `foc t : Pat
Γ; Γ0 `inv t : Pat
foc-intro
Γ`t⇑P
Γ `foc t : P
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
intro-end
Γ; ∅ `inv t : Nat
Γ ` t ⇑ Nat
11
foc-atom
Γ`n⇓X
Γ `foc n : X
Focused natural deduction for intuitionistic logic
inv-sum
Γ; ∆, x : A `inv t : C
Γ; ∆, x : B `inv u : C
Γ; ∆, x : A + B `inv δ(x, x.t, x.u) : C
inv-pair
Γ; ∆ `inv t : A
Γ; ∆ `inv u : B
Γ; ∆ `inv (t, u) : A ∗ B
foc-elim
Γ`n⇓P
Γ; x : P `inv t : Qat
Γ `foc let x = n in t : Qat
inv-end
Γ, Γ0 `foc t : Pat
Γ; Γ0 `inv t : Pat
foc-intro
Γ`t⇑P
Γ `foc t : P
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
inv-arr
Γ; ∆, x : A `inv t : B
Γ; ∆ `inv λ(x) t : A → B
foc-atom
Γ`n⇓X
Γ `foc n : X
intro-end
Γ; ∅ `inv t : Nat
Γ ` t ⇑ Nat
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat
11
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
12
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
¯ = {(n, P) | (Γ, Γ0 ` n ⇓ P)∧ n uses Γ0 }
(¯
n, P)
Γ, Γ0 ; x¯ : P¯ `sinv t : Qat
∀x ∈ x¯, t uses x
0
Γ; Γ `sat let x¯ = n¯ in t : Qat
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
saturation
X , (X → Y + Y ) ` Y
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
¯ = {(n, P) | (Γ, Γ0 ` n ⇓ P) ∧ n uses Γ0 }
(¯
n, P)
Γ, Γ0 ; x¯ : P¯ `sinv t : Qat
∀x ∈ x¯, t uses x
0
Γ; Γ `sat let x¯ = n¯ in t : Qat
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
canonicity
X , (X → X + X ) ` X
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
¯ ⊆ {(n, P) | (Γ, Γ0 ` n ⇓ P) ∧ n uses Γ0 }
(¯
n, P)
Γ, Γ0 ; x¯ : P¯ `sinv t : Qat
∀x ∈ x¯, t uses x
finite derivations
Γ; Γ0 `sat let x¯ = n¯ in t : Qat
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Saturating focused natural deduction
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
¯ ⊆ {(n, P) | (Γ, Γ0 ` n ⇓ P) ∧ n uses Γ0 }
(¯
n, P)
Γ, Γ0 ; x¯ : P¯ `sinv t : Qat
∀x ∈ x¯, t uses x
Γ; Γ0 `sat let x¯ = n¯ in t : Qat
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
X , (X → X + X ) ` X
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 12
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
What we have so far:
What we lack:
13
What we have so far:
computational completeness (fresh look at focusing)
What we lack:
13
What we have so far:
computational completeness (fresh look at focusing)
canonicity (modulo inversible commuting conversions) (the hard part)
What we lack:
13
What we have so far:
computational completeness (fresh look at focusing)
canonicity (modulo inversible commuting conversions) (the hard part)
What we lack:
termination
13
Usual termination arguments
Subformula property: finite number of judgments Γ ` A.
Not true for typing contexts (multisets)
14
Usual termination arguments
Subformula property: finite number of judgments Γ ` A.
Not true for typing contexts (multisets)
No need for recurrent judgments.
?
Γ`A
...
...
...
Γ`A
...
Breaks computational completeness
14
Usual termination arguments
Subformula property: finite number of judgments Γ ` A.
Not true for typing contexts (multisets)
No need for recurrent judgments.
?
Γ`A
...
...
...
Γ`A
...
Breaks computational completeness
14
Usual termination arguments
Subformula property: finite number of judgments Γ ` A.
Not true for typing contexts (multisets)
No need for recurrent judgments.
?
Γ`A
...
...
...
Γ`A
...
Breaks computational completeness
14
Termination 1: bounded multisets
There exists a M2 ∈ N such that, by keeping at most M2 variables of each
type/formula in Γ, then we can find at least 2 distinct proofs of Γ ` A if
they exist.
15
Termination 1: bounded multisets
There exists a M2 ∈ N such that, by keeping at most M2 variables of each
type/formula in Γ, then we can find at least 2 distinct proofs of Γ ` A if
they exist.
In fact M2 := 2 suffices.
15
Termination 2: recurring at most twice
?
Γ`A
...
...
...
Γ`A
...
16
Termination 2: recurring at most twice
?
Γ`A
...
...
...
Γ`A
...
Computational completeness is lost, but
Unicity completeness regained.
16
Algorithm
Our algorithm searches for all saturated proofs under these two
search-space restriction.
Optimization 1: redundancy (elim and intro).
Optimization 2: monotonicity.
17
(The logic again)
sinv-sum
Γ; ∆, x : A `sinv t : C
Γ; ∆, x : B `sinv u : C
Γ; ∆, x : A + B `sinv δ(x, x.t, x.u) : C
sinv-pair
Γ; ∆ `sinv t : A
Γ; ∆ `sinv u : B
Γ; ∆ `sinv (t, u) : A ∗ B
sinv-end
Γ; Γ0 `sat t : Pat
Γ; Γ0 `sinv t : Pat
¯ ⊆ {(n, P) | (Γ, Γ0 ` n ⇓ P) ∧ n uses Γ0 }
(¯
n, P)
Γ, Γ0 ; x¯ : P¯ `sinv t : Qat
∀x ∈ x¯, t uses x
Γ; Γ0 `sat let x¯ = n¯ in t : Qat
sat-atom
Γ`n⇓X
Γ; ∅ `sat n : X
elim-pair
Γ ` n ⇓ A1 ∗ A2
Γ ` πi n ⇓ Ai
intro-sum
Γ ` t ⇑ Ai
Γ ` σi t ⇑ A1 + A2
elim-start
(x : Nat ) ∈ Γ
Γ ` x ⇓ Nat 18
sinv-arr
Γ; ∆, x : A `sinv t : B
Γ; ∆ `sinv λ(x) t : A → B
sat-intro
Γ`t⇑P
Γ; ∅ `sat t : P
intro-end
Γ; ∅ `sinv t : Nat
Γ ` t ⇑ Nat
elim-arr
Γ`n⇓A→B
Γ`u⇑A
Γ`nu⇓B
Conclusion
Future work: extend to polymorphism and dependent types.
19
Conclusion
Future work: extend to polymorphism and dependent types.
Thanks. Any question?
Paper draft:
gallium.inria.fr/~scherer/drafts/unique_stlc_sums.pdf
19
```