consistent subtyping for all
play

Consistent Subtyping for All Ningning Xie Xuan Bi Bruno C. d. S. - PowerPoint PPT Presentation

Consistent Subtyping for All Ningning Xie Xuan Bi Bruno C. d. S. Oliveira 16 April, 2018 The University of Hong Kong ESOP 2018, Thessaloniki, Greece 1 Gradual Typing 101 The key external feature of every gradual type system is the


  1. Consistent Subtyping for All Ningning Xie Xuan Bi Bruno C. d. S. Oliveira 16 April, 2018 The University of Hong Kong ESOP 2018, Thessaloniki, Greece 1

  2. Gradual Typing 101 • The key external feature of every gradual type system is the unknown type ⋆ . f (x : Int ) = x + 2 -- static checking h (g : ⋆ ) = g 1 -- dynamic checking h f • Central to gradual typing is type consistency ∼ , which relaxes type equality: ⋆ ∼ Int, ⋆ → Int ∼ Int → ⋆, . . . • Dynamic semantics is defined by type-directed translation to an internal language with runtime casts: ( � ⋆ ֒ → ⋆ → ⋆ � g ) ( � Int ֒ → ⋆ � 1) 2

  3. ❘ Many Successes Gradual typing has seen great popularity both in academia and industry. Over the years, there emerge many gradual type disciplines: • Subtyping • Parametric Polymorphism • Type inference • Security Typing • Effects • . . . 3

  4. Many Successes, But... Gradual typing has seen great popularity both in academia and industry. Over the years, there emerge many gradual type disciplines: • Subtyping • Parametric Polymorphism • Type inference • Security Typing • Effects • . . . ❘ As type systems get more complex, it becomes more difficult to adapt notions of gradual typing. [Garcia et al., 2016] 3

  5. Problem • Can we design a gradual type system with implicit higher-rank polymorphism ? 4

  6. Problem • Can we design a gradual type system with implicit higher-rank polymorphism ? • State-of-art techniques are inadequate. 4

  7. Why It Is interesting • Haskell supports implicit higher-rank polymorphism, but some “safe” programs are rejected: foo :: ([ Int ], [ Char ]) foo = let f x = (x [1, 2], x [’a’, ’b’]) in f reverse -- GHC rejects 5

  8. Why It Is interesting • Haskell supports implicit higher-rank polymorphism, but some “safe” programs are rejected: foo :: ([ Int ], [ Char ]) foo = let f x = (x [1, 2], x [’a’, ’b’]) in f reverse -- GHC rejects • If we had gradual typing... let f (x : ⋆ ) = (x [1, 2], x [’a’, ’b’]) in f reverse 5

  9. Why It Is interesting • Haskell supports implicit higher-rank polymorphism, but some “safe” programs are rejected: foo :: ([ Int ], [ Char ]) foo = let f x = (x [1, 2], x [’a’, ’b’]) in f reverse -- GHC rejects • If we had gradual typing... let f (x : ⋆ ) = (x [1, 2], x [’a’, ’b’]) in f reverse • Moving to more precised version still type checks, but with more static safety guarantee: let f (x : ∀ a. [a] → [a]) = (x [1, 2], x [’a’, ’b’]) in f reverse 5

  10. Contributions • A new specification of consistent subtyping that works for implicit higher-rank polymorphism • An easy-to-follow recipe for turning subtyping into consistent subtyping • A gradually typed calculus with implicit higher-rank polymorphism • Satisfies correctness criteria (formalized in Coq) • A sound and complete algorithm 6

  11. What Is Consistent Subtyping • Consistent subtyping ( � ) is the extension of subtyping to gradual types. [Siek and Taha, 2007] 7

  12. What Is Consistent Subtyping • Consistent subtyping ( � ) is the extension of subtyping to gradual types. [Siek and Taha, 2007] • A static subtyping relation ( < :) over gradual types, with the key insight that ⋆ is neutral to subtyping ( ⋆ < : ⋆ ) 7

  13. What Is Consistent Subtyping • Consistent subtyping ( � ) is the extension of subtyping to gradual types. [Siek and Taha, 2007] • A static subtyping relation ( < :) over gradual types, with the key insight that ⋆ is neutral to subtyping ( ⋆ < : ⋆ ) • An algorithm for consistent subtyping in terms of masking A | B 7

  14. What Is Consistent Subtyping • Consistent subtyping ( � ) is the extension of subtyping to gradual types. [Siek and Taha, 2007] • A static subtyping relation ( < :) over gradual types, with the key insight that ⋆ is neutral to subtyping ( ⋆ < : ⋆ ) • An algorithm for consistent subtyping in terms of masking A | B Definition (Consistent Subtyping ` a la Siek and Taha) The following two are equivalent : 1. A � B if and only if A ∼ C and C < : B for some C . 2. A � B if and only if A < : C and C ∼ B for some C . 7

  15. Design Principle ❘ Gradual typing and subtyping are orthogonal and can be combined in a principled fashion. – Siek and Taha 8

  16. ❘ Challenge • Polymorphic types induce a subtyping relation: ∀ a . a → a < : Int → Int • Design consistent subtyping that combines 1) consistency 2) subtyping 3) polymorphism. 9

  17. Challenge • Polymorphic types induce a subtyping relation: ∀ a . a → a < : Int → Int • Design consistent subtyping that combines 1) consistency 2) subtyping 3) polymorphism. ❘ Gradual typing and polymorphism are orthogonal and can be combined in a principled fashion. 1 1 Note that here we are mostly concerned with static semantics. 9

  18. Problem with Existing Definition

  19. Odersky-L¨ aufer Type System • The underlying static language is the well-established type system for higher-rank types. [Odersky and L¨ aufer, 1996] Types A , B ::= Int | a | A → B | ∀ a . A Monotypes ::= Int | a | τ → σ τ, σ Terms e ::= x | n | λ x : A . e | λ x . e | e 1 e 2 ::= Contexts Ψ • | Ψ , x : A | Ψ , a 10

  20. Subtyping Ψ ⊢ A < : B (Subtyping) a ∈ Ψ Ψ ⊢ B 1 < : A 1 Ψ ⊢ A 2 < : B 2 Ψ ⊢ a < : a Ψ ⊢ Int < : Int Ψ ⊢ A 1 → A 2 < : B 1 → B 2 Ψ ⊢ τ Ψ ⊢ A [ a �→ τ ] < : B Ψ , a ⊢ A < : B Ψ ⊢ ∀ a . A < : B Ψ ⊢ A < : ∀ a . B 11

  21. Subtyping with Unknown Types Ψ ⊢ A < : B (Subtyping) a ∈ Ψ Ψ ⊢ B 1 < : A 1 Ψ ⊢ A 2 < : B 2 Ψ ⊢ a < : a Ψ ⊢ Int < : Int Ψ ⊢ A 1 → A 2 < : B 1 → B 2 Ψ ⊢ τ Ψ ⊢ A [ a �→ τ ] < : B Ψ , a ⊢ A < : B Ψ ⊢ ∀ a . A < : B Ψ ⊢ A < : ∀ a . B Ψ ⊢ ⋆ < : ⋆ 11

  22. ❘ Type Consistency A ∼ B (Type Consistency) A 1 ∼ B 1 A 2 ∼ B 2 A ∼ A A ∼ ⋆ ⋆ ∼ A A 1 → A 2 ∼ B 1 → B 2 12

  23. ❘ Type Consistency with Polymorphic Types A ∼ B (Type Consistency) A 1 ∼ B 1 A 2 ∼ B 2 A ∼ A A ∼ ⋆ ⋆ ∼ A A 1 → A 2 ∼ B 1 → B 2 A ∼ B ∀ a . A ∼ ∀ a . B 12

  24. Type Consistency with Polymorphic Types A ∼ B (Type Consistency) A 1 ∼ B 1 A 2 ∼ B 2 A ∼ A A ∼ ⋆ ⋆ ∼ A A 1 → A 2 ∼ B 1 → B 2 A ∼ B ∀ a . A ∼ ∀ a . B ❘ The simplicity comes from the orthogonality between consistency and subtyping! 12

  25. Bad News Definition (Consistent Subtyping ` a la Siek and Taha) The following two are equivalent: 1. A � B if and only if A ∼ C and C < : B for some C . 2. A � B if and only if A < : C and C ∼ B for some C . ❘ Equivalence is broken in the polymorphic setting! 13

  26. Bad News Definition (Consistent Subtyping ` a la Siek and Taha) The following two are equivalent: 1. A � B if and only if A ∼ C and C < : B for some C . ✓ 2. A � B if and only if A < : C and C ∼ B for some C . ✗ ❘ Equivalence is broken in the polymorphic setting! ∼ ⊥ ( ⋆ → Int) → Int < : < : ( ∀ a . a → Int) → Int ( ∀ a .⋆ → Int) → Int ∼ 13

  27. Bad News Definition (Consistent Subtyping ` a la Siek and Taha) The following two are equivalent: 1. A � B if and only if A ∼ C and C < : B for some C . ✗ 2. A � B if and only if A < : C and C ∼ B for some C . ✓ ❘ Equivalence is broken in the polymorphic setting! ∼ Int → Int Int → ⋆ < : < : ∀ a . a ⊥ ∼ 13

  28. Bad News Definition (Consistent Subtyping ` a la Siek and Taha) The following two are equivalent: 1. A � B if and only if A ∼ C and C < : B for some C . ✗ 2. A � B if and only if A < : C and C ∼ B for some C . ✗ ❘ Equivalence is broken in the polymorphic setting! ∼ ⊥ ( (( ⋆ → Int) → Int) → Bool) → (Int → ⋆ ) < : < : ( (( ∀ a . a → Int) → Int) → Bool) → ( ∀ a . a ) ⊥ ∼ 13

  29. Revisiting Consistent Subtyping

  30. Consistent Subtyping vs. Subtyping • Subtyping validates the subsumption principle Ψ ⊢ e : A A < : B Ψ ⊢ e : B 14

  31. Consistent Subtyping vs. Subtyping • Subtyping validates the subsumption principle , so should consistent subtyping Ψ ⊢ e : A A � B Ψ ⊢ e : B 14

  32. Consistent Subtyping vs. Subtyping • Subtyping validates the subsumption principle , so should consistent subtyping Ψ ⊢ e : A A � B Ψ ⊢ e : B • Subtyping is transitive, but consistent subtyping is not 14

  33. Observations Observation (I) If A < : B and B � C, then A � C. ∼ T 1 C � < : < : � B T 2 ∼ < : A 15

  34. Observations Observation (I) If A < : B and B � C, then A � C. ∼ T 1 C � < : < : � B T 2 ∼ < : A 15

  35. Observations Observation (I) If A < : B and B � C, then A � C. ∼ T 1 C � < : < : � B T 2 ∼ < : A 15

  36. Observations Observation (I) If A < : B and B � C, then A � C. Observation (II) If C � B and B < : A, then C � A. ∼ T 1 C A � � < : < : < : ∼ � T 1 B B T 2 ∼ � < : < : < : C T 2 A ∼ 15

  37. Observations Observation (I) If A < : B and B � C, then A � C. Observation (II) If C � B and B < : A, then C � A. ∼ T 1 C A � � < : < : < : ∼ � T 1 B B T 2 ∼ � < : < : < : C T 2 A ∼ 15

Recommend


More recommend