Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.


1 805 vues

Publié le

The talk covers foundations and applications of implicit function types. It is an updated and more condensed version of the "What to Leave Implicit Talk" given at CurryOn/PLDI 2017.

Publié dans : Technologie
  • Identifiez-vous pour voir les commentaires


  1. 1. (S)implicitly Foundation and Applications of Implicit Function Types Martin Odersky, Olivier Blanvillain, Fenyun Liu, Aggelos Biboudis, Heather Miller, Sandro Stucki POPL 2018
  2. 2. Con-text what comes with the text, but is not in the text
  3. 3. Context is all around us • the current configuration • the current scope • the meaning of “<” on this type • the user on behalf of which the operation is performed • the security level in effect • …
  4. 4. • globals rigid if immutable, unsafe if mutable • monkey patching • dependency injection (e.g. Spring, Guice) Traditional ways to express context
  5. 5. “Parameterize all the things” The Functional Way
  6. 6. - no side effects - type safe - fine-grained control Functional is Good
  7. 7. - sea of parameters - most of which hardly ever change - repetitive, boring, prone to mistakes But sometimes it’s too much of a good thing …
  8. 8. If passing a lot of parameters gets tedious, leave some of them implicit. A more direct approach
  9. 9. If passing a lot of parameters gets tedious, leave some of them implicit. “Trade types for terms”: We provide the type and have the compiler synthesize a matching term. A more direct approach
  10. 10. • If you do not give an argument to an implicit parameter, one will be provided for you. • Eligible are all implicit values that are visible at the point of call. • If there are more than one eligible candidate, the most specific one is chosen. • If there’s no unique most specific candidate, an ambiguity error Is reported. Ground Rules
  11. 11. Implicit parameters can • prove theorems • establish context • set configurations • inject dependencies • model capabilities • implement type classes Use Cases
  12. 12. Implicits are Ubiquitous
  13. 13. Handling Context Example*: conference management system. Reviewers should only see (directly or indirectly) the scores of papers where they have no conflict with an author. *Suggested by Nadia Polikarpova.
  14. 14. Handling Context Example: conference management system. Context is usually stable, can change at specific points.
  15. 15. Can we do better? Having to write a couple of times does not look so bad. But in the new Scala compiler dotc there are > 2600 occurrences of the parameter Would it not be nice to get rid of them?
  16. 16. Implicit Function Types Let’s massage the definition of viewRankings a bit: What is its type? So far: Viewers => List[Paper] From now on: implicit Viewers => List[Paper]
  17. 17. Two Rules for Typing 1. Implicit functions get implicit arguments just like implicit methods. Given: f expands to f(a). 2. Implicit functions get created on demand. If the expected type of b is implicit A => B, then b expands to implicit (_: A) => b
  18. 18. Using Implicit Function Types Assume: Then reformulate: “Trade types for parameters”
  19. 19. Trade Types for Parameters • By specifying a type of an expression E, we can provide in a very general way a context for E. • Types can be declared directly, or be inferred with local type inference.
  20. 20. A Formalization Bidirectional typing rules: Γ ⊢ t :▷ T (Synthesis) Γ ⊢ t ◁: T (Checking)
  21. 21. SI Typing Rules: Functions
  22. 22. SI Typing Rules: Variables
  23. 23. SI Typing Rules: Let
  24. 24. SI Typing Rules: Everything
  25. 25. Translation to System F
  26. 26. • The reader monad also allows abstracting context into a type • Implicit functions are more lightweight, since they don’t force you into monadic style. • They also compose better. For instance: val a: implicit A => implicit B => R = ??? val b: implicit B => implicit A => R = a • In the end, Monads are about sequencing, they are overkill for passing context. Comparison with the Reader Monad
  27. 27. • Builder pattern • Tagless interpreters • Free structures (all of them in the paper) • Algebraic effects (Brachthäuser, Schuster, Scala Symp. 2017) Other Applications
  28. 28. Neat way to define structure-building DSLs, like this: Natively supported in Groovy and in Kotlin via “receiver functions”. An Encore: The Builder Pattern
  29. 29. Scala Implementation
  30. 30. Expansion
  31. 31. • Implicit parameters in Haskell (Lewis et al, 2000) • Modular implicits in OCaml (White, Bour, Yallop, 2015) • Agda’s implicit instances (Devriese and Piessens, 2011) • Implicit Calculus (Olivera, Schrjvers, Wadler et al., 2012, 2017, Rouvoet 2016) Related Work
  32. 32. Dotty: Dmitry Petrashko Nicolas Stucki Guillaume Martres Felix Mulder Ondrej Lhotak Aggelos Biboudis Liu Fengyun Vera Salvis Olivier Blanvillain Enno Runne Sebastien Douraene … and many others DOT: Nada Amin Tiark Rompf Sandro Stucki Samuel Grütter scalac at Adriaan Moors Seth Tisue Lightbend: Jason Zaugg Stefan Zeiger Lukas Rytz Scala Heather Miller Julien Richard-Foy Center: Jorge Cantero Olafur Geirsson Guillaume Massé Martin Duheim Travis Lee Credits Thank You
  33. 33. (S)implicitly Martin Odersky, Olivier Blanvillain, Fenyun Liu, Aggelos Biboudis, Heather Miller, Sandro Stucki EPFL How to Abstract Over Context? - Implicit parameters (aka type classes) - synthesize programs based on types. - Implicit function types - make implicits even nicer to use - have simple foundations