structural prototypes
play

Structural Prototypes A Sane Framework for Statically-Typed OO+FP - PowerPoint PPT Presentation

Structural Prototypes A Sane Framework for Statically-Typed OO+FP Avoiding Religion Were doing static typing, like it or not! Assume with me that OO is good Expressiveness is good More expressiveness is better? Class Types


  1. Structural Prototypes A Sane Framework for Statically-Typed OO+FP

  2. Avoiding Religion • We’re doing static typing, like it or not! • Assume with me that OO is good • Expressiveness is good • More expressiveness is better?

  3. Class Types

  4. Parametric Polymorphism Class Types

  5. Declaration Site Variance Parametric Polymorphism Class Types

  6. Existentials Declaration Site Variance Parametric Polymorphism Class Types

  7. n o i t a c fi i t n a u Q d e d n Existentials u o B Declaration Site Variance Parametric Polymorphism Class Types

  8. P n a o t i h t a c fi i t n a u Q Dependent d e d n Existentials u o B Declaration Site Variance Parametric Polymorphism Class Types

  9. P n a o t i h t a c fi i t n a u Q Dependent d Higher Kinds e d n Existentials u o B Declaration Site Variance Parametric Polymorphism Class Types

  10. P n a o t i h t a c fi i t n a u Q Dependent d Higher Kinds e d n Existentials u o B Declaration Site Variance Parametric Polymorphism Class Types

  11. FP Makes It Worse… • Higher-Kinds are really necessary

  12. FP Makes It Worse… • Higher-Kinds are really necessary • Typeclasses • (and a zillion resolution strategies)

  13. FP Makes It Worse… • Higher-Kinds are really necessary • Typeclasses • (and a zillion resolution strategies) • Higher-Rank polymorphism

  14. FP Makes It Worse… • Higher-Kinds are really necessary • Typeclasses • (and a zillion resolution strategies) • Higher-Rank polymorphism • Functional utility methods

  15. Turing Completeness • Higher-Kinds give us simply-typed λ -calculus

  16. Turing Completeness • Higher-Kinds give us simply-typed λ -calculus • Object-Orientation implies open recursion

  17. Turing Completeness • Higher-Kinds give us simply-typed λ -calculus • Object-Orientation implies open recursion • Path-dependent types reify open recursion • Escapes object context!

  18. Turing Completeness • Higher-Kinds give us simply-typed λ -calculus • Object-Orientation implies open recursion • Path-dependent types reify open recursion • Escapes object context! • Type-Level Fixpoint

  19. Scala is Inevitable!

  20. y t i l i b a d i c e d n U Scala is Inevitable!

  21. Starting over…

  22. Hindley-Milner • Not particularly powerful (read: expressive)

  23. Hindley-Milner • Not particularly powerful (read: expressive) • Lots of significant limitations • And yet, it’s still very popular!

  24. Hindley-Milner • Not particularly powerful (read: expressive) • Lots of significant limitations • And yet, it’s still very popular! • Hindley-Milner is Good Enough™

  25. What is Object-Orientation?

  26. What is Object-Orientation? • Message Passing • Open Recursion

  27. What is Object-Orientation? • Message Passing • Open Recursion … O O > S O L C y l t n e r a p p a

  28. Simplify, Simplify • Q: What is the simplest way to encode objects? • (hint: it’s not π -calculus)

  29. Simplify, Simplify • Q: What is the simplest way to encode objects? • (hint: it’s not π -calculus) • A: Records of functions

  30. Simplify, Simplify • Q: What is the simplest way to encode objects? • (hint: it’s not π -calculus) • A: Records of functions • Primitive self pointer

  31. What is Functional?

  32. What is Functional? λ

  33. Simplify, Simplify • Q: What’s the simplest way to encode lambdas?

  34. Simplify, Simplify • Q: What’s the simplest way to encode lambdas? • A: Uh…with lambdas?

  35. Simplify, Simplify • Q: What’s the simplest way to encode lambdas? • A: Uh…with lambdas? • Let-bound polymorphism

  36. Putting It Together • Records of lambdas

  37. Putting It Together • Records of lambdas • Primitive self pointer

  38. Putting It Together • Records of lambdas • Primitive self pointer • Let-bound polymorphism • (requires extension for objects)

  39. Putting It Together • Records of lambdas • Primitive self pointer • Let-bound polymorphism • (requires extension for objects) • Principal typing

  40. Structural Prototypes • We only have records, no classes • Object abstraction must be prototypical

  41. Structural Prototypes • We only have records, no classes • Object abstraction must be prototypical • Record types are fully-structural • Not quite OCaml!

  42. Structural Prototypes • We only have records, no classes • Object abstraction must be prototypical • Record types are fully-structural • Not quite OCaml! • Full expressivity of both FP and OO

  43. Structural Prototypes • We only have records, no classes • Object abstraction must be prototypical • Record types are fully-structural • Not quite OCaml! • Full expressivity of both FP and OO • Very simple and orthogonal

  44. Apricot = Origin.with( sweetness: 7 bites: 0 takeBite: #(this.with(bites: bites + 1))) Grapefruit = Apricot.with(sweetness: 1) Apple = Apricot.with(sweetness: 5)

  45. Some = #(x, Origin.with( getOrElse: #(_, x) map: #(f, Some(f(x))) flatMap: #(f, f(x)))) None = Origin.with( getOrElse: #(x, x) map: #(_, this) flatMap: #(_, this))

  46. Seq = #(foldRight, Origin.with( builder: #(xs, Seq(xs.foldRight)) foldRight: foldRight map: #(f, res = foldRight(Vector0, #(x, acc, acc.prepend(f(x)))))))

  47. Seq = #(foldRight, Origin.with( builder: #(xs, Seq(xs.foldRight)) foldRight: foldRight map: #(f, res = foldRight(Vector0, #(x, acc, acc.prepend(f(x))))))) ListImpl = #(foldRight, Seq(foldRight).with( builder: #(xs, xs.foldRight(Nil, Cons)))) Cons = #(head, tail, foldRight = #(z, f, f(head, tail.foldRight(z, f))) ListImpl(foldRight)) Nil = ListImpl(#(_, z, z))

  48. Vector = #(xs, ds = Origin.with( /* hard core data structure */ ) Seq(ds.foldRight).with( builder: Vector prepend: #(x, … ))) Vector0 = … // empty vector

  49. Problems • Structural subtyping is usually tricky • Error messages • Branded types?

  50. Problems • Structural subtyping is usually tricky • Error messages • Branded types? • Prototypes are not always fantastic

  51. Problems • Structural subtyping is usually tricky • Error messages • Branded types? • Prototypes are not always fantastic • Let-bound polymorphism is very restrictive

  52. Is this the Future? • I don’t know (probably not) • We need to keep trying! • Static typing is very hard • …complicated • …and divisive

  53. Questions?

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