Nonuniform Coercions via Unification Hints Claudio Sacerdoti Coen 1 - - PowerPoint PPT Presentation

nonuniform coercions via unification hints
SMART_READER_LITE
LIVE PREVIEW

Nonuniform Coercions via Unification Hints Claudio Sacerdoti Coen 1 - - PowerPoint PPT Presentation

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


slide-1
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
SLIDE 2

Context of this work

◮ Interactive theorem prover Matita (CIC) ◮ Formalization of formal topology (Algebraic Structures)

slide-3
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
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
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
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
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
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
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
SLIDE 10

Outline

  • 1. Coercions

◮ Nonuniform coercions ◮ Examples

  • 2. Implementation

◮ Ingredients ◮ Declaring nonuniform coercions ◮ Reusing existing hints

  • 3. Conclusions
slide-11
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
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
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
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
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
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
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
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
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
SLIDE 20

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γ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
SLIDE 21

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:

  • Γi ⊢ Si

→ Ti si → ti

  • ∈ ∆

Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.

slide-22
SLIDE 22

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:

  • Γi ⊢ Si

→ Ti si → ti

  • ∈ ∆

S

?

= Si Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.

slide-23
SLIDE 23

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:

  • Γi ⊢ Si

→ Ti si → ti

  • ∈ ∆

S

?

= Si s

?

= si Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.

slide-24
SLIDE 24

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:

  • Γi ⊢ Si

→ Ti si → ti

  • ∈ ∆

S

?

= Si s

?

= si T

?

= Ti Γ ⊢ s : S : T where variables in Γi are replaced by unification variables.

slide-25
SLIDE 25

Nonuniform coercions

∆ =

  • Γ1

⊢ S1 → T1 s1 → t1 . . . Γn ⊢ Sn → Tn sn → tn

  • where

Γi ⊢ si : Si Γi ⊢ ti : Ti Inserting a nonuniform coercion works as follows:

  • Γi ⊢ Si

→ Ti si → ti

  • ∈ ∆

S

?

= Si s

?

= si T

?

= Ti Γ ⊢ s : S ti : T where variables in Γi are replaced by unification variables.

slide-26
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
SLIDE 27

Cheap implementation: ingredient #1

Unification hints: − → ?x := − → H Γ ⊢ myhint P ≡ Q

slide-28
SLIDE 28

Cheap implementation: ingredient #1

Unification hints: − → ?x := − → H Γ ⊢ myhint P ≡ Q Examples: ?G := Z ⊢ Z ≡ carr ?G

slide-29
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
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
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
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
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

  • n s : ? >

− − − > target ???.

✝ ✆

slide-34
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

  • n s : ? >

− − − > target ???.

✝ ✆

s result ? s ?sol

slide-35
SLIDE 35

Declaring nonuniform coercions

⊢ Type → Group Z → Z Γ ⊢ Z : Type : Group

slide-36
SLIDE 36

Declaring nonuniform coercions

⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group

slide-37
SLIDE 37

Declaring nonuniform coercions

⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group

slide-38
SLIDE 38

Declaring nonuniform coercions

⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ Z : Type : Group

slide-39
SLIDE 39

Declaring nonuniform coercions

⊢ Type → Group Z → Z (result, (∗, target)) ∈ ∆ Γ ⊢ result ? Z ?sol : target Type Z ?sol Γ ⊢ Z : Type : Group

slide-40
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
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
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
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
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
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
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
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
SLIDE 48

Thanks

Thanks for your attention!