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 ∀ = α λ β →
Why coercions? A naive algorithm Constraint-based algorithm Conclusion Outline Why coercions? A naive algorithm Constraint-based algorithm Conclusion
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
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 →∞
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
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”
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 . . .
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
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
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
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
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
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
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
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
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
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
Why coercions? A naive algorithm Constraint-based algorithm Conclusion This is “normal” behaviour of coercions. Coq Reference Manual
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 θ Γ
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 θ Γ
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 θ Γ
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 ∪ { τ = α → β, σ < : α } .
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
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 θ Γ
Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α
Why coercions? A naive algorithm Constraint-based algorithm Conclusion Constraint simplification • Goal: only atomic constraints α < : β , α < : T , T < : α σ list < : τ list ⇔ σ < : τ
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
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