SLIDE 1 Nonuniform Coercions via Unification Hints
Claudio Sacerdoti Coen1, Enrico Tassi2
1University of Bologna - Department of Computer Science 2Microsoft Research-INRIA Joint Center
TYPES 2010 — 15 October 2010 — Warsaw
SLIDE 2 Context of this work
◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)
SLIDE 3 Context of this work
◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)
≡
“Type Inference” Matita Type Checking
?
=
Kernel Refiner
SLIDE 4 Context of this work
◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)
≡
“Type Inference” Matita Type Checking
?
=
Kernel Refiner ◮ Unification made user-extensible (Unification Hints)
SLIDE 5 Context of this work
◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)
≡
“Type Inference” Matita Type Checking
?
=
Kernel Refiner ◮ Unification made user-extensible (Unification Hints) ◮ In some corner cases the system is unable to exploit the
knowledge given by hints
SLIDE 6
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).
✝ ✆
SLIDE 7 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.
SLIDE 8 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).
✝ ✆
SLIDE 9 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.
SLIDE 10 Outline
◮ Nonuniform coercions ◮ Examples
◮ Ingredients ◮ Declaring nonuniform coercions ◮ Reusing existing hints
SLIDE 11 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
SLIDE 12 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
SLIDE 13 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
SLIDE 14 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
SLIDE 15 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
SLIDE 16 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
SLIDE 17 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
SLIDE 18 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
SLIDE 19 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
SLIDE 20 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows: Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.
SLIDE 21 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:
→ Ti si → ti
Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.
SLIDE 22 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:
→ Ti si → ti
S
?
= Si Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.
SLIDE 23 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:
→ Ti si → ti
S
?
= Si s
?
= si Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.
SLIDE 24 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:
→ Ti si → ti
S
?
= Si s
?
= si T
?
= Ti Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.
SLIDE 25 Nonuniform coercions
∆ =
⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn
Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:
→ Ti si → ti
S
?
= Si s
?
= si T
?
= Ti Γ ⊢ s : S ti : T where variables in Γi are replaced by unification variables.
SLIDE 26
Nonuniform coercions: examples
Uniform coercions x : N ⊢ N → Z x → k x Nonuniform coercions ⊢ Type → Group Z → Z ⊢ Type → Group Q → Q
SLIDE 27
Cheap implementation: ingredient #1
Unification hints: − → ?x := − → H Γ ⊢ myhint P ≡ Q
SLIDE 28
Cheap implementation: ingredient #1
Unification hints: − → ?x := − → H Γ ⊢ myhint P ≡ Q Examples: ?G := Z ⊢ Z ≡ carr ?G
SLIDE 29
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
SLIDE 30 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 Z
?
= carr Z Γ ⊢ s : N k s : carr Z
SLIDE 31 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 Z
?
= carr Z Γ ⊢ s : N k s : carr Z
SLIDE 32 Cheap implementation: ingredient #2
Uniform coercion loosely indexed: (result, (∗, target)) ∈ ∆ Γ ⊢ result s : target target
?
= T Γ ⊢ x : S result s : T Note that T and target can be in the same equivalence class.
SLIDE 33 Encoding nonuniform coercions
✞ ☎
record solution (S : Type) (s : S) : Type :={ target : Type; (∗ T ∗) result : target (∗ t ∗) }. coercion result : ∀ S:Type.∀ s:S.∀ sol:solution S s. target S s sol
− − − > target ???.
✝ ✆
SLIDE 34 Encoding nonuniform coercions
✞ ☎
record solution (S : Type) (s : S) : Type :={ target : Type; (∗ T ∗) result : target (∗ t ∗) }. coercion result : ∀ S:Type.∀ s:S.∀ sol:solution S s. target S s sol
− − − > target ???.
✝ ✆
s result ? s ?sol
SLIDE 35
Declaring nonuniform coercions
⊢ Type → Group Z → Z Γ ⊢ Z : Type : Group
SLIDE 36
Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group
SLIDE 37
Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group
SLIDE 38
Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group
SLIDE 39
Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol Γ ⊢ Z : Type : Group
SLIDE 40 Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol target Type Z ?sol
?
= Group Γ ⊢ Z : Type : Group
SLIDE 41 Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol target Type Z ?sol
?
= Group Γ ⊢ Z : Type result ? Z ?sol : Group
SLIDE 42 Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol target Type Z ?sol
?
= Group Γ ⊢ Z : Type result ? Z ?sol : Group We declare the following hint: ?sol := mk solution Type Z Group Z ⊢ target Type Z ?sol ≡ Group
SLIDE 43 Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol target Type Z ?sol
?
= Group Γ ⊢ Z : Type result ? Z ?sol : Group We declare the following hint: ?sol := mk solution Type Z Group Z ⊢ target Type Z ?sol ≡ Group Note that: target Type Z ?sol ⊲ Group
SLIDE 44 Declaring nonuniform coercions
⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol target Type Z ?sol
?
= Group Γ ⊢ Z : Type result ? Z ?sol : Group We declare the following hint: ?sol := mk solution Type Z Group Z ⊢ target Type Z ?sol ≡ Group Note that: target Type Z ?sol ⊲ Group result Type Z ?sol ⊲ Z
SLIDE 45
Declaring nonuniform coercions (the right way)
This is unsatisfactory, we need one new hint per coercion ?sol := mk solution Type Z Group Z ⊢ target Type Z ?sol ≡ Group Moreover, the system is already aware that ?G := Z Γ ⊢ Z ≡ carr ?G We need only this hint: ?Z := carr G ?sol := mk solution Type ?Z Group G G : Group ⊢ target Type ?Z ?sol ≡ Group
SLIDE 46 Conclusion
Nonuniform coercions:
◮ Generalization of type-theoretic coercions ◮ Cheap implementation on top of unification hints ◮ Both type inference and unification can exploit the
knowledge expressed in terms of Unification Hints
SLIDE 47 Conclusion
Nonuniform coercions:
◮ Generalization of type-theoretic coercions ◮ Cheap implementation on top of unification hints ◮ Both type inference and unification can exploit the
knowledge expressed in terms of Unification Hints Further research:
◮ Notion of coherence (sanity check on ∆ as a whole) ◮ Notion of composition for nonuniform coercions
SLIDE 48
Thanks
Thanks for your attention!