1 / 86

CSED321 Programming Languages Polymorphism

CSED321 Programming Languages Polymorphism. Kyungmin Bae Department of Computer Science and Engineering POSTECH Spring 2019. Polymorphism. Parametric Polymorphism operates on all types of objects in a uniform way. C++ templates, Java generics, … Subtype Polymorphism

glewis
Download Presentation

CSED321 Programming Languages Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSED321 Programming LanguagesPolymorphism Kyungmin Bae Department of Computer Science and Engineering POSTECH Spring 2019

  2. Polymorphism • Parametric Polymorphism • operates on all types of objects in a uniform way. • C++ templates, Java generics, … • Subtype Polymorphism • operates on subtypes of objects • subtypes on classes • Ad hoc Polymorphism • different behavior depending on the type of objects it operates on • operator overloading

  3. Outline • System F • Programming in System F • Type reconstruction • Implicit polymorphism • Type inference algorithm

  4. System F • Untyped -calculus + Parametric polymorphism • First step toward the polymorphic type system of OCaml

  5. Type of • Bind variable to some type • Decide the type of the resulting expression • In the simply typed -calculus:

  6. Type of : Goal • Bind variable to any type • Assign a polymorphic type to

  7. Ambiguity

  8. Type Abstraction • Declares as a fresh any type, or a fresh type variable

  9. Example: Identity Function

  10. Type Application • Type application • Example

  11. System F: Syntax

  12. System F: Syntax

  13. Reduction Rules

  14. Reduction Rules

  15. Example

  16. Typing Context

  17. Typing Judgement

  18. Type Judgement

  19. Typing Rules

  20. Example

  21. Type Safety • Type preservation If and , then • Progress If for some type , then either is a value or there exists such that

  22. Substitution Lemmas

  23. Outline • System F • Programming in System F • Type reconstruction • Implicit polymorphism • Type inference algorithm

  24. Power of System F • Can naturally encode complex data types • C.f. simply typed lambda calculus

  25. Programming in System F: Boolean • Type

  26. Programming in System F: Numerals • Type • zero • succ

  27. Programming in System F: Product Types • Type • pair • fst • snd

  28. Programming in System F: Sum Types • Type • inl • inr • case

  29. Self-application (a.k.a, Impredicativity)

  30. Strong Normalization • Every expression in System F eventually reduces to a value.

  31. Outline • System F • Programming in System F • Type reconstruction • Implicit polymorphism • Type inference algorithm

  32. Which is the Most Readable? fun f -> fun g -> (fun x -> g (f x)) fun f : ('a -> ‘b) -> fun g : ('b -> ‘c) -> (fun x : 'a -> g (f x)) fun f : ('a -> ‘b) -> fun g : ('b -> ‘c) -> ((fun x : 'a -> (g (f x)) : 'c): 'a -> 'c)

  33. Type Reconstruction • From: untyped (typeable) program • To: typed program

  34. Erasure Function

  35. Requirement of Type Reconstruction

  36. Example

  37. Bad News • Not every untyped expression is typeable in System F • Type reconstruction of System F is undecidable

  38. Predicative Polymorphic -Calculus • Sublanguage of System F • Decidable type reconstruction • Supports polymorphism

  39. Types in System F

  40. Types in Predicative Polymorphic -Calculus • A polytype in prenex form

  41. Predicative Polymorphic -Calculus: Syntax

  42. Typing Rules

  43. Typing Rules

  44. Example

  45. Let-Polymorphism • System F • Powerful, but undecidable type reconstruction • Predicative polymorphic -Calculus • Decidable type reconstruction, but useless • Let-Polymorphism • Decidable type reconstruction, and useful

  46. Let-Binding • binds to a polymorphic expression • not a syntactic sugar in terms of types!

  47. Example

  48. Syntax

  49. Let-Binding: Typing Rules

  50. Let-Binding: Reduction Rules

More Related