extending hindley milner type inference with coercive
play

Extending Hindley-Milner Type Inference with Coercive Structural - PowerPoint PPT Presentation

Why coercions? A naive algorithm Constraint-based algorithm Conclusion Extending Hindley-Milner Type Inference with Coercive Structural Subtyping Dmitriy Traytel Stefan Berghofer Tobias Nipkow APLAS 2011 Isabelle nat<:int =


  1. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Extending Hindley-Milner Type Inference with Coercive Structural Subtyping Dmitriy Traytel Stefan Berghofer Tobias Nipkow APLAS 2011 Isabelle nat<:int ∀ = α λ β →

  2. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Outline Why coercions? A naive algorithm Constraint-based algorithm Conclusion

  3. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Real-world examples • 2004: Avigad verifies in Isabelle: ( λ x. pi x * ln (real x) / (real x)) ----> 1

  4. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Real-world examples • 2004: Avigad verifies in Isabelle: ( λ x. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem π ( x ) ln x lim = 1 x x →∞

  5. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Real-world examples • 2004: Avigad verifies in Isabelle: ( λ x. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem π ( x ) ln x lim = 1 x x →∞ • 2009: Hölzl uses 1061 explicit conversions in a single theory

  6. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Real-world examples • 2004: Avigad verifies in Isabelle: ( λ x. pi x * ln (real x) / (real x)) ----> 1 i.e. the prime number theorem π ( x ) ln x lim = 1 x x →∞ • 2009: Hölzl uses 1061 explicit conversions in a single theory • Both report “headaches”

  7. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Solution: coercive structural subtyping Related work • Subtyping part of the type system: Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . .

  8. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Solution: coercive structural subtyping Related work • Subtyping part of the type system: Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . . • Incomplete coercion inference system: Saïbi, Luo

  9. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Solution: coercive structural subtyping Related work • Subtyping part of the type system: Mitchell, Fuh & Mishra, Wand & O’Keefe, Pottier, Simonet Cardelli, Pratt & Tiuryn, Luo, Kießling, Frey, Benke, Barthe, Chen Reynolds, Swamy, Hicks & Bierman, Nordlander, Shields & Peyton Jones . . . • Incomplete coercion inference system: Saïbi, Luo • Complete coercion inference system: this publication

  10. Why coercions? A naive algorithm Constraint-based algorithm Conclusion The Hindley-Milner typing rules remain unchanged: No subtypes here Type inference is extended with coercion inference and coercion insertion

  11. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Our coercion inference system • Coercions: N < : real R • Lifted by map functions: N list < : map real R list • Programmer inputs terms omitting coercions • The system infers and inserts coercions • Result is well-typed according to Hindley-Milner

  12. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Our coercion inference system • Coercions: N < : real R • Lifted by map functions: N list < : map real R list • Programmer inputs terms omitting coercions • The system infers and inserts coercions • Result is well-typed according to Hindley-Milner • The coercion inference system: • is sound and complete • does not change the underlying type system

  13. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Local coercion insertion • Use judgement Γ ⊢ t � u : τ • Idea: insert coercions whenever the function’s domain does not match the argument type: ⊢ t 1 � u 1 : τ 11 → τ 12 ⊢ t 2 � u 2 : τ 2 τ 2 < : c τ 11 ⊢ t 1 t 2 � u 1 ( c u 2 ) : τ 12

  14. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Local coercion insertion • Use judgement Γ ⊢ t � u : τ • Idea: insert coercions whenever the function’s domain does not match the argument type: ⊢ t 1 � u 1 : τ 11 → τ 12 ⊢ t 2 � u 2 : τ 2 τ 2 < : c τ 11 ⊢ t 1 t 2 � u 1 ( c u 2 ) : τ 12 • Used in Coq

  15. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Problematic example Example: leq i n vs. leq n i • Signatures: leq :: α → α → B , n :: N and i :: Z • Declared coercion: N < : int Z

  16. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Problematic example Example: leq i n vs. leq n i • Signatures: leq :: α → α → B , n :: N and i :: Z • Declared coercion: N < : int Z • Correctly, leq i n becomes leq i (int n) , as • leq i :: Z → B • n :: N

  17. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Problematic example Example: leq i n vs. leq n i • Signatures: leq :: α → α → B , n :: N and i :: Z • Declared coercion: N < : int Z • Correctly, leq i n becomes leq i (int n) , as • leq i :: Z → B • n :: N • Unfortunately, the coercion inference of leq n i fails, as • leq n :: N → B • i :: Z • no coercion from Z to N

  18. Why coercions? A naive algorithm Constraint-based algorithm Conclusion This is “normal” behaviour of coercions. Coq Reference Manual

  19. Why coercions? A naive algorithm Constraint-based algorithm Conclusion The subtyping pipeline Input Constraint generation Weak unification test term t , context Γ Γ ⊢ t : τ � S Constraint simplification Build constraint graph Constraint resolution Output Coercion generation term u , and insertion Unification type θτ , θ Γ ⊢ θ t � u : θτ context θ Γ

  20. Why coercions? A naive algorithm Constraint-based algorithm Conclusion The subtyping pipeline Input Constraint generation Weak unification test term t , context Γ Γ ⊢ t : τ � S Constraint simplification Build constraint graph Constraint resolution Output Coercion generation term u , and insertion Unification type θτ , θ Γ ⊢ θ t � u : θτ context θ Γ

  21. Why coercions? A naive algorithm Constraint-based algorithm Conclusion The subtyping pipeline Input Constraint generation Weak unification test term t , context Γ Γ ⊢ t : τ � S Constraint simplification Build constraint graph Constraint resolution Output Coercion generation term u , and insertion Unification type θτ , θ Γ ⊢ θ t � u : θτ context θ Γ

  22. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint generation ⊢ t 1 : τ � S 1 ⊢ t 2 : σ � S 2 α, β fresh ⊢ t 1 t 2 : β � S 1 ∪ S 2 ∪ { τ = α → β, σ < : α } .

  23. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint generation ⊢ t 1 : τ � S 1 ⊢ t 2 : σ � S 2 α, β fresh ⊢ t 1 t 2 : β � S 1 ∪ S 2 ∪ { τ = α → β, σ < : α } . Example: leq n i leq :: α → α → B n :: N ⊢ leq : α → α → B � ∅ ⊢ n : N � ∅ i :: Z ⊢ leq n : β 2 � { α → α → B . = α 2 → β 2 , N < : α 2 } ⊢ i : Z � ∅  α → α → B = α 2 → β 2 ,  .     β 2 = α 1 → β 1 , .   ⊢ leq n i : β 1 � < : N α 2 ,     Z < : α 1  

  24. Why coercions? A naive algorithm Constraint-based algorithm Conclusion The subtyping pipeline Input Constraint generation Weak unification test term t , context Γ Γ ⊢ t : τ � S Constraint simplification Build constraint graph Constraint resolution Output Coercion generation term u , and insertion Unification type θτ , θ Γ ⊢ θ t � u : θτ context θ Γ

  25. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α

  26. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α σ list < : τ list ⇔ σ < : τ

  27. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α σ list < : τ list ⇔ σ < : τ σ 1 → σ 2 < : τ 1 → τ 2 ⇔ τ 1 < : σ 1 and σ 2 < : τ 2

  28. Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α σ list < : τ list ⇔ σ < : τ σ 1 → σ 2 < : τ 1 → τ 2 ⇔ τ 1 < : σ 1 and σ 2 < : τ 2 = α ′ list ∧ α ′ list < : τ list ∃ α ′ . α . α < : τ list ⇔

Recommend


More recommend