Specialization by unification Agda uses unification to: • eliminate impossible cases • specialize the result type The output of unification can change Agda’s notion of equality! Main question: How to make sure the output of unification is correct? 10 / 52
Depending on equations Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms
Q: What is the fastest way to start a fight between type theorists? 11 / 52
Q: What is the fastest way to start a fight between type theorists? A: Mention the topic of equality. 11 / 52
The identity type x ≡ A y . . . a dependent type depending on x , y : A . 12 / 52
The identity type x ≡ A y . . . a dependent type depending on x , y : A . . . . type theory’s built-in notion of equality. 12 / 52
The identity type x ≡ A y . . . a dependent type depending on x , y : A . . . . type theory’s built-in notion of equality. . . . the type of proofs that x = y . 12 / 52
Operations on the identity type : x ≡ A x refl 13 / 52
Operations on the identity type : x ≡ A x refl : x ≡ A y → y ≡ A x sym 13 / 52
Operations on the identity type : x ≡ A x refl : x ≡ A y → y ≡ A x sym : x ≡ A y → y ≡ A z → x ≡ A z trans 13 / 52
Operations on the identity type : x ≡ A x refl : x ≡ A y → y ≡ A x sym : x ≡ A y → y ≡ A z → x ≡ A z trans : x ≡ A y → f x ≡ B f y cong f 13 / 52
Operations on the identity type : x ≡ A x refl : x ≡ A y → y ≡ A x sym : x ≡ A y → y ≡ A z → x ≡ A z trans : x ≡ A y → f x ≡ B f y cong f subst P : x ≡ A y → P x → P y 13 / 52
Unification problems as telescopes A unification problem consists of 1. Flexible variables x 1 : A 1 , x 2 : A 2 , . . . 2. Equations u 1 = v 1 : B 1 , . . . 14 / 52
Unification problems as telescopes A unification problem consists of 1. Flexible variables x 1 : A 1 , x 2 : A 2 , . . . 2. Equations u 1 = v 1 : B 1 , . . . This can be represented as a telescope : ( x 1 : A 1 )( x 2 : A 2 ) . . . ( e 1 : u 1 ≡ B 1 v 1 )( e 2 : u 2 ≡ B 2 v 2 ) . . . e.g. ( k : N )( n : N )( e : suc k ≡ N suc n ) 14 / 52
Unification problems as telescopes A unification problem consists of 1. Flexible variables Γ 2. Equations u 1 = v 1 : B 1 , . . . This can be represented as a telescope : Γ ( e 1 : u 1 ≡ B 1 v 1 )( e 2 : u 2 ≡ B 2 v 2 ) . . . e.g. ( k : N )( n : N )( e : suc k ≡ N suc n ) 14 / 52
Unification problems as telescopes A unification problem consists of 1. Flexible variables Γ 2. Equations ¯ u = ¯ v : ∆ This can be represented as a telescope : Γ(¯ e : ¯ u ≡ ∆ ¯ v ) e.g. ( k : N )( n : N )( e : suc k ≡ N suc n ) 14 / 52
Unifiers as telescope maps A unifier of ¯ u and ¯ v is a substitution σ : Γ ′ → Γ such that ¯ u σ = ¯ v σ . 15 / 52
Unifiers as telescope maps A unifier of ¯ u and ¯ v is a substitution σ : Γ ′ → Γ such that ¯ u σ = ¯ v σ . This can be represented as a telescope map : f : Γ ′ → Γ(¯ e : ¯ u ≡ ∆ ¯ v ) e.g. f : () → ( n : N )( e : n ≡ N zero ) f () = zero ; refl 15 / 52
Evidence of unification A map f : () → ( n : N )( e : n ≡ N zero ) gives us two things: 16 / 52
Evidence of unification A map f : () → ( n : N )( e : n ≡ N zero ) gives us two things: 1. A value for n such that n ≡ N zero 16 / 52
Evidence of unification A map f : () → ( n : N )( e : n ≡ N zero ) gives us two things: 1. A value for n such that n ≡ N zero 2. Explicit evidence e of n ≡ N zero 16 / 52
Evidence of unification A map f : () → ( n : N )( e : n ≡ N zero ) gives us two things: 1. A value for n such that n ≡ N zero 2. Explicit evidence e of n ≡ N zero = ⇒ Unification is guaranteed to respect ≡ ! 16 / 52
Three valid unifiers f 1 : ( k : N ) → ( k n : N )( e : k ≡ N n ) f 1 k = k ; k ; refl f 2 : () → ( k n : N )( e : k ≡ N n ) f 2 () = zero ; zero ; refl f 3 : ( k n : N ) → ( k n : N )( e : k ≡ N n ) f 3 k n = k ; k ; refl 17 / 52
Most general unifiers A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ ′ with ¯ u σ ′ = ¯ v σ ′ , there is a ν such that σ ′ = σ ◦ ν . 18 / 52
Most general unifiers A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ ′ with ¯ u σ ′ = ¯ v σ ′ , there is a ν such that σ ′ = σ ◦ ν . This is quite difficult to translate to type theory directly. . . 18 / 52
Most general unifiers A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ ′ with ¯ u σ ′ = ¯ v σ ′ , there is a ν such that σ ′ = σ ◦ ν . This is quite difficult to translate to type theory directly. . . Intuition: if f : Γ ′ → Γ(¯ u ≡ ∆ ¯ e : ¯ v ) is MGU, v ) to Γ ′ we can go back from Γ(¯ e : ¯ u ≡ ∆ ¯ without losing any information. 18 / 52
Equivalences A function f : A → B is an equivalence if it has both a left and a right inverse: isLinv : ( x : A ) → g 1 ( f x ) ≡ A x isRinv : ( y : B ) → f ( g 2 y ) ≡ B y In this case, we write f : A ≃ B . 19 / 52
Most general unifiers are equivalences! v ) ≃ Γ ′ f : Γ(¯ e : ¯ u ≡ ∆ ¯ 20 / 52
Example of unification ( k n : N )( e : suc k ≡ N suc n ) 21 / 52
Example of unification ( k n : N )( e : suc k ≡ N suc n ) ≃ ( k n : N )( e : k ≡ N n ) 21 / 52
Example of unification ( k n : N )( e : suc k ≡ N suc n ) ≃ ( k n : N )( e : k ≡ N n ) ≃ ( k : N ) 21 / 52
Example of unification ( k n : N )( e : suc k ≡ N suc n ) ≃ ( k n : N )( e : k ≡ N n ) ≃ ( k : N ) f : ( k : N ) → ( k n : N )( e : suc k ≡ N suc n ) f k = k ; k ; refl 21 / 52
The solution rule solution : ( x : A )( e : x ≡ A t ) ≃ () 22 / 52
The deletion rule deletion : ( e : t ≡ A t ) ≃ () 23 / 52
The injectivity rule injectivity suc : ( e : suc x ≡ N suc y ) ≃ ( e ′ : x ≡ N y ) 24 / 52
Negative unification rules A negative unification rule applies to impossible equations, e.g. suc x = zero . 25 / 52
Negative unification rules A negative unification rule applies to impossible equations, e.g. suc x = zero . This can be represented by an equivalence: ( e : suc x ≡ N zero ) ≃ ⊥ where ⊥ is the empty type . 25 / 52
The conflict rule conflict suc , zero : ( e : suc x ≡ N zero ) ≃ ⊥ 26 / 52
The cycle rule cycle n , suc n : ( e : n ≡ N suc n ) ≃ ⊥ 27 / 52
Unifiers as equivalences By requiring unifiers to be equivalences : • we exclude bad unification rules • we can safely introduce new rules 28 / 52
Unifiers as equivalences By requiring unifiers to be equivalences : • we exclude bad unification rules • we can safely introduce new rules Next, we’ll explore how this idea can help us. Any questions so far? 28 / 52
Depending on equations Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms
Time for the interesting bits! • Equations between types • Heterogeneous equations • Equations on indexed datatypes • Equations between equations 29 / 52
Equations between types Types are first-class terms of type Type : Bool : Type , N : Type , N → N : Type , . . . 30 / 52
Equations between types Types are first-class terms of type Type : Bool : Type , N : Type , N → N : Type , . . . We can form equations between types, e.g. Bool ≡ Type Bool . 30 / 52
Equations between types Types are first-class terms of type Type : Bool : Type , N : Type , N → N : Type , . . . We can form equations between types, e.g. Bool ≡ Type Bool . Q: Can we apply the deletion rule? 30 / 52
Equations between types Types are first-class terms of type Type : Bool : Type , N : Type , N → N : Type , . . . We can form equations between types, e.g. Bool ≡ Type Bool . Q: Can we apply the deletion rule? A: Depends on which type theory we use! 30 / 52
The univalence axiom (2009) Vladimir Voevodsky 31 / 52
The univalence axiom (2009) “Isomorphic types can be identified.” Vladimir Voevodsky 31 / 52
The univalence axiom (2009) “Isomorphic types can be identified.” ( A ≡ B ) ≃ ( A ≃ B ) Vladimir Voevodsky 31 / 52
The univalence axiom (2009) Bool is equal to Bool in two ways: Bool true false 32 / 52
The univalence axiom (2009) Bool is equal to Bool in two ways: Bool true false true false Bool 32 / 52
Recommend
More recommend