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 11 May, 2018 The University of Hong Kong 1 Background There has been ongoing de- bate about which language paradigm, static typing or dynamic typing, is better 2


  1. Consistent Subtyping for All Ningning Xie Xuan Bi Bruno C. d. S. Oliveira 11 May, 2018 The University of Hong Kong 1

  2. Background There has been ongoing de- bate about which language paradigm, static typing or dynamic typing, is better 2

  3. Background 1 Some people are in favor of static typing: • Communication • Reliability • Efficiency • Productivity 1 Adapted from POPL2017 tutorial. 3

  4. Background 1 Some people are in favor of the other prefer dynamic typing: static typing: • Don’t have to write type • Communication annotations • Reliability • Expressiveness • Efficiency • Cognitive load • Productivity • Learning curve 1 Adapted from POPL2017 tutorial. 3

  5. Gradual Typing From a Programmer’s View Gradual typing enables the evolution of programs from untyped to typed, and provides fine-grained control over which parts are statically checked. 2 4 2 Examples courtesy of Garcia’s slides at POPL’16

  6. Gradual Typing From a Programmer’s View Gradual typing enables the evolution of programs from untyped to typed, and provides fine-grained control over which parts are statically checked. 2 • Program with no type information (dynamic checking) def f(x) = x + 2 def h(g) = g(1) h f 4 2 Examples courtesy of Garcia’s slides at POPL’16

  7. Gradual Typing From a Programmer’s View Gradual typing enables the evolution of programs from untyped to typed, and provides fine-grained control over which parts are statically checked. 2 • Program with no type information (dynamic checking) def f(x) = x + 2 def h(g) = g(1) h f • Program with full type information (static checking) def f(x : Int ) = x + 2 def h(g : Int → Int ) = g(1) h f 4 2 Examples courtesy of Garcia’s slides at POPL’16

  8. Gradual Typing From a Programmer’s View Gradual typing enables the evolution of programs from untyped to typed, and provides fine-grained control over which parts are statically checked. 2 • Program with no type information (dynamic checking) def f(x) = x + 2 def h(g) = g(1) h f • Program with full type information (static checking) def f(x : Int ) = x + 2 def h(g : Int → Int ) = g(1) h f • Program with some type information (mixed checking) def f(x : Int ) = x + 2 def h(g) = g(1) h f 4 2 Examples courtesy of Garcia’s slides at POPL’16

  9. Gradual Typing 101 • A gradual type system enforces static type discipline whenever possible: def f(x : Bool ) = x + 2 -- static error def h (g) = g(1) h f 5

  10. Gradual Typing 101 • A gradual type system enforces static type discipline whenever possible: def f(x : Bool ) = x + 2 -- static error def h (g) = g(1) h f “Inside every gradual language is a small static language struggling to get out...” Anonymous 5

  11. Gradual Typing 101 • A gradual type system enforces static type discipline whenever possible: def f(x : Bool ) = x + 2 -- static error def h (g) = g(1) h f “Inside every gradual language is a small static language struggling to get out...” Anonymous • When the type information is not available, it delegates to dynamic checking at runtime: def f(x : Int ) = x + 2 def h(g) = g( True ) h f -- runtime error 5

  12. 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 → ⋆, . . . Int ∼ Int Bool ∼ Bool Int = Int Int �∼ Bool extend = = = ⇒ Bool = Bool ⋆ ∼ Int Int � = Bool Int → ⋆ ∼ ⋆ → Int . . . 6

  13. 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) 6

  14. ❘ 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 • . . . 7

  15. 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] 7

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

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

  18. 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 9

  19. 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 9

  20. 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 9

  21. 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 10

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

  23. 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 ( ⋆ < : ⋆ ) 11

  24. 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 ( ⋆ < : ⋆ ) 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 . 11

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

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

  27. 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. 3 3 Note that here we are mostly concerned with static semantics. 13

  28. Problem with Existing Definition

  29. 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 14

  30. 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 15

  31. 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 Ψ ⊢ ⋆ < : ⋆ 15

  32. ❘ 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 16

  33. ❘ 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 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend