Nonuniform Coercions via Unification Hints Claudio Sacerdoti Coen 1 , Enrico Tassi 2 1 University of Bologna - Department of Computer Science 2 Microsoft Research-INRIA Joint Center TYPES 2010 — 15 October 2010 — Warsaw
Context of this work ◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)
Context of this work ◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures) Kernel Refiner Type “Type Checking Inference” ? ≡ = Matita
Context of this work ◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures) Kernel Refiner Type “Type Checking Inference” ? ≡ = Matita ◮ Unification made user-extensible (Unification Hints)
Context of this work ◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures) Kernel Refiner Type “Type Checking Inference” ? ≡ = Matita ◮ Unification made user-extensible (Unification Hints) ◮ In some corner cases the system is unable to exploit the knowledge given by hints
Example ✞ ☎ record Group : Type := { carr : Type, ∗ : . . . } definition Z : Group := � Z, +, 0, . . . � . lemma mulg1: ∀ G:Group, ∀ a:carr G. a ∗ 1 = a. lemma cardG gt0 : ∀ G : Group, 0 < | G | . ✝ ✆ ✞ ☎ check (mulg1 ? G 2). ✝ ✆
Example ✞ ☎ record Group : Type := { carr : Type, ∗ : . . . } definition Z : Group := � Z, +, 0, . . . � . lemma mulg1: ∀ G:Group, ∀ a:carr G. a ∗ 1 = a. lemma cardG gt0 : ∀ G : Group, 0 < | G | . ✝ ✆ ✞ ☎ check (mulg1 ? G 2). ✝ ✆ Works, since 2 has type Z , and it’s context expects a term of type carr ? G and the unification algorithm knows a canonical ? solution for Z = carr ? G .
Example ✞ ☎ record Group : Type := { carr : Type, ∗ : . . . } definition Z : Group := � Z, +, 0, . . . � . lemma mulg1: ∀ G:Group, ∀ a:carr G. a ∗ 1 = a. lemma cardG gt0 : ∀ G : Group, 0 < | G | . ✝ ✆ ✞ ☎ check (mulg1 ? G 2). ✝ ✆ Works, since 2 has type Z , and it’s context expects a term of type carr ? G and the unification algorithm knows a canonical ? solution for Z = carr ? G . ✞ ☎ check (cardG gt0 Z). ✝ ✆
Example ✞ ☎ record Group : Type := { carr : Type, ∗ : . . . } definition Z : Group := � Z, +, 0, . . . � . lemma mulg1: ∀ G:Group, ∀ a:carr G. a ∗ 1 = a. lemma cardG gt0 : ∀ G : Group, 0 < | G | . ✝ ✆ ✞ ☎ check (mulg1 ? G 2). ✝ ✆ Works, since 2 has type Z , and it’s context expects a term of type carr ? G and the unification algorithm knows a canonical ? solution for Z = carr ? G . ✞ ☎ check (cardG gt0 Z). ✝ ✆ Error: Z has type Type but it’s context expects a term of type ? Group . The unification problem Type = Group has no solution.
Outline 1. Coercions ◮ Nonuniform coercions ◮ Examples 2. Implementation ◮ Ingredients ◮ Declaring nonuniform coercions ◮ Reusing existing hints 3. Conclusions
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ( k , ( N , Z )) ∈ ∆ Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ( k , ( N , Z )) ∈ ∆ Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ( k , ( N , Z )) ∈ ∆ Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ( k , ( N , Z )) ∈ ∆ Γ ⊢ k x : Z Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k x : Z = Z Z Γ ⊢ x : N � : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k x : Z = Z Z Γ ⊢ x : N � k x : Z
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k x : Z Z = Z Γ ⊢ x : N � k x : Z ◮ but (uniform) coercions are type theoretic functions whose insertion is type driven. ( λ . Z , ( Type , Group )) ∈ ∆ Γ ⊢ ( λ . Z ) Z : Group Γ ⊢ Z : Type � ( λ . Z ) Z : Group
Type inference and coercions ◮ These problems have to be addressed by type inference Γ ⊢ t : T � t ′ : T ′ ◮ Looks like coercions could solve these typing errors ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k x : Z Z = Z Γ ⊢ x : N � k x : Z ◮ but (uniform) coercions are type theoretic functions whose insertion is type driven. ( λ . Z , ( Type , Group )) ∈ ∆ Γ ⊢ ( λ . Z ) Q : Group Γ ⊢ Q : Type � ( λ . Z ) Q : Group
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: Γ ⊢ s : S � : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: � � Γ i ⊢ S i → T i �→ s i t i ∈ ∆ Γ ⊢ s : S � : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: ? = S S i � � Γ i ⊢ S i → T i �→ s i t i ∈ ∆ Γ ⊢ s : S � : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: ? = S S i ? s = s i � � Γ i ⊢ S i → T i �→ s i t i ∈ ∆ Γ ⊢ s : S � : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: ? = S S i ? s = s i � � Γ i ⊢ S i → T i ? �→ T = T i s i t i ∈ ∆ Γ ⊢ s : S � : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions � � S 1 → T 1 S n → T n ∆ = Γ 1 ⊢ Γ n ⊢ . . . �→ �→ s 1 t 1 s n t n where Γ i ⊢ s i : S i Γ i ⊢ t i : T i Inserting a nonuniform coercion works as follows: ? = S S i ? s = s i � � Γ i ⊢ S i → T i ? �→ T = T i s i t i ∈ ∆ Γ ⊢ s : S � t i : T where variables in Γ i are replaced by unification variables.
Nonuniform coercions: examples Uniform coercions → N Z x : N ⊢ x �→ k x Nonuniform coercions Type → Group ⊢ �→ Z Z → Type Group ⊢ Q �→ Q
Cheap implementation: ingredient #1 Unification hints: − → ? x := − → H myhint Γ ⊢ P ≡ Q
Cheap implementation: ingredient #1 Unification hints: − → ? x := − → H myhint Γ ⊢ P ≡ Q Examples: ? G := Z ⊢ Z ≡ carr ? G
Cheap implementation: ingredient #1 Unification hints: − → ? x := − → H myhint Γ ⊢ P ≡ Q Examples: ? G := Z ⊢ Z ≡ carr ? G ? A := carr G ? B := carr H ? X := product group G H G , H : Group ⊢ ? A × ? B ≡ carr ? X
Cheap implementation: ingredient #1 (cont.) Note that hints define “equivalence classes” of constants, thus approximated indexing for fast retrieval must take them into account. ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k s : Z = carr Z Z Γ ⊢ s : N � k s : carr Z
Cheap implementation: ingredient #1 (cont.) Note that hints define “equivalence classes” of constants, thus approximated indexing for fast retrieval must take them into account. ? ( k , ( N , Z )) ∈ ∆ Γ ⊢ k s : Z = carr Z Z Γ ⊢ s : N � k s : carr Z
Cheap implementation: ingredient #2 Uniform coercion loosely indexed: ? ( result , ( ∗ , target )) ∈ ∆ Γ ⊢ result s : target = T target Γ ⊢ x : S � result s : T Note that T and target can be in the same equivalence class.
Recommend
More recommend