Programming with Purity Reflection: Peaceful Coexistence of Effects, Laziness, and Parallelism

Authors Magnus Madsen , Jaco van de Pol



PDF
Thumbnail PDF

File

LIPIcs.ECOOP.2023.18.pdf
  • Filesize: 0.95 MB
  • 27 pages

Document Identifiers

Author Details

Magnus Madsen
  • Department of Computer Science, Aarhus University, Denmark
Jaco van de Pol
  • Department of Computer Science, Aarhus University, Denmark

Cite As Get BibTex

Magnus Madsen and Jaco van de Pol. Programming with Purity Reflection: Peaceful Coexistence of Effects, Laziness, and Parallelism. In 37th European Conference on Object-Oriented Programming (ECOOP 2023). Leibniz International Proceedings in Informatics (LIPIcs), Volume 263, pp. 18:1-18:27, Schloss Dagstuhl – Leibniz-Zentrum für Informatik (2023) https://doi.org/10.4230/LIPIcs.ECOOP.2023.18

Abstract

We present purity reflection, a programming language feature that enables higher-order functions to inspect the purity of their function arguments and to vary their behavior based on this information. The upshot is that operations on data structures can selectively use lazy and/or parallel evaluation while ensuring that side effects are never lost or re-ordered. The technique builds on a recent Hindley-Milner style type and effect system based on Boolean unification which supports both effect polymorphism and complete type inference. We illustrate that avoiding the so-called 'poisoning problem' is crucial to support purity reflection.
We propose several new data structures that use purity reflection to switch between eager and lazy, sequential and parallel evaluation. We propose a DelayList, which is maximally lazy but switches to eager evaluation for impure operations. We also propose a DelayMap which is maximally lazy in its values, but also exploits eager and parallel evaluation. 
We implement purity reflection as an extension of the Flix programming language. We present a new effect-aware form of monomorphization that eliminates purity reflection at compile-time. And finally, we evaluate the cost of this new monomorphization on compilation time and on code size, and determine that it is minimal.

Subject Classification

ACM Subject Classification
  • Theory of computation → Program semantics
Keywords
  • type and effect systems
  • purity reflection
  • lazy evaluation
  • parallel evaluation

Metrics

  • Access Statistics
  • Total Accesses (updated on a weekly basis)
    0
    PDF Downloads

References

  1. Andrej Bauer and Matija Pretnar. Programming with Algebraic Effects and Handlers. Journal of Logical and Algebraic Methods in Programming, 84(1), 2015. URL: https://doi.org/10.1016/j.jlamp.2014.02.001.
  2. Chandrasekhar Boyapati, Robert Lee, and Martin Rinard. Ownership Types for Safe Programming: Preventing Data Races and Deadlocks. In International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA), 2002. URL: https://doi.org/10.1145/582419.582440.
  3. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. Effect Handlers for the Masses. Proc. of the ACM on Programming Languages, 2(OOPSLA), 2018. URL: https://doi.org/10.1145/3276481.
  4. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. Effects as Capabilities: Effect Handlers and Lightweight Effect Polymorphism. Proc. of the ACM on Programming Languages, 4(OOPSLA), 2020. URL: https://doi.org/10.1145/3428194.
  5. Duncan Coutts, Roman Leshchinskiy, and Don Stewart. Stream Fusion: From Lists to Streams to Nothing at All. International Conference of Functional Programming (ICFP), 42(9), 2007. URL: https://doi.org/10.1145/1291220.1291199.
  6. Andrew Craik and Wayne Kelly. Using Ownership to Reason about Inherent Parallelism in Object-Oriented Programs. In International Conference on Compiler Construction (CC), 2010. URL: https://doi.org/10.1007/978-3-642-11970-5_9.
  7. Luis Damas. Type Assignment in Programming Languages. PhD thesis, The University of Edinburgh, 1984. Google Scholar
  8. Jacques Garrigue. Relaxing the value restriction. In International Symposium on Functional and Logic Programming, 2004. URL: https://doi.org/10.1007/978-3-540-24754-8_15.
  9. Colin S Gordon, Matthew J Parkinson, Jared Parsons, Aleks Bromfield, and Joe Duffy. Uniqueness and Reference Immutability for Safe Parallelism. In International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA), 2012. URL: https://doi.org/10.1145/2398857.2384619.
  10. Dan Grossman, Greg Morrisett, Trevor Jim, Michael Hicks, Yanling Wang, and James Cheney. Region-based Memory Management in Cyclone. In Programming Language Design and Implementation (PLDI), 2002. URL: https://doi.org/10.1145/512529.512563.
  11. Robert Harper and Greg Morrisett. Compiling Polymorphism using Intensional Type Analysis. In Principles of Programming Languages (POPL), 1995. URL: https://doi.org/10.1145/199448.199475.
  12. Roger Hindley. The Principal Type-scheme of an Object in Combinatory Logic. Transactions of the American Mathematical Society (AMS), 1969. Google Scholar
  13. Simon Peyton Jones, Andrew Tolmach, and Tony Hoare. Playing by the Rules: Rewriting as a Practical Optimisation Technique in GHC. In Haskell Workshop, 2001. Google Scholar
  14. Ohad Kammar, Sam Lindley, and Nicolas Oury. Handlers in Action. International Conference on Functional Programming (ICFP), 48(9), 2013. URL: https://doi.org/10.1145/2544174.2500590.
  15. Ohad Kammar and Gordon D. Plotkin. Algebraic Foundations for Effect-dependent Optimisations. In Principles of Programming Languages (POPL), 2012. URL: https://doi.org/10.1145/2103656.2103698.
  16. Raffi Khatchadourian, Yiming Tang, and Mehdi Bagherzadeh. Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams. Science of Computer Programming, 2020. URL: https://doi.org/10.1016/j.scico.2020.102476.
  17. Raffi Khatchadourian, Yiming Tang, Mehdi Bagherzadeh, and Baishakhi Ray. An Empirical Study on the Use and Misuse of Java 8 Streams. In Fundamental Approaches to Software Engineering (FASE), 2020. URL: https://doi.org/10.1007/978-3-030-45234-6_5.
  18. Oleg Kiselyov, Aggelos Biboudis, Nick Palladinos, and Yannis Smaragdakis. Stream Fusion, to Completeness. In Principles of Programming Languages (POPL), 2017. URL: https://doi.org/10.1145/3009837.3009880.
  19. Oleg Kiselyov, Amr Sabry, and Cameron Swords. Extensible Effects: An Alternative to Monad Transformers. Haskell Workshop, 2013. URL: https://doi.org/10.1145/2578854.2503791.
  20. Daan Leijen. Extensible Records with Scoped Labels. Trends in Functional Programming (TFP), 2005. Google Scholar
  21. Daan Leijen. Type Directed Compilation of Row-typed Algebraic Effects. In Principles of Programming Languages (POPL), 2017. URL: https://doi.org/10.1145/3009837.3009872.
  22. Yuheng Long, Yu David Liu, and Hridesh Rajan. First-class Effect Reflection for Effect-Guided Programming. In International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA), 2016. URL: https://doi.org/10.1145/3022671.2984037.
  23. John M Lucassen and David K Gifford. Polymorphic Effect Systems. In Principles of Programming Languages (POPL), 1988. Google Scholar
  24. Magnus Madsen. The Principles of the Flix Programming Language. In International Symposium on New Ideas, New Paradigms, and Reflections on Programming and Software (Onward!), 2022. URL: https://doi.org/10.1145/3563835.3567661.
  25. Magnus Madsen and Ondřej Lhoták. Fixpoints for the Masses: Programming with First-class Datalog Constraints. Proc. of the ACM on Programming Languages, 4(OOPSLA), 2020. URL: https://doi.org/10.1145/3428193.
  26. Magnus Madsen, Jonathan Lindegaard Starup, and Ondřej Lhoták. Flix: A Meta Programming Language for Datalog. In Proc. International Workshop on the Resurgence of Datalog in Academia and Industry (Datalog-2.0 2022), 2022. Google Scholar
  27. Magnus Madsen and Jaco van de Pol. Polymorphic Types and Effects with Boolean Unification. Proc. of the ACM on Programming Languages, 4(OOPSLA), 2020. URL: https://doi.org/10.1145/3428222.
  28. Magnus Madsen and Jaco van de Pol. Relational Nullable Types with Boolean Unification. Proc. of the ACM on Programming Languages, 5(OOPSLA), 2021. URL: https://doi.org/10.1145/3485487.
  29. Urusula Martin and Tobias Nipkow. Boolean Unification - The Story So Far. Journal of Symbolic Computation, 1989. Google Scholar
  30. Robin Milner. A Theory of Type Polymorphism in Programming. Journal of Computer and System Sciences, 1978. Google Scholar
  31. Yaron Minsky, Anil Madhavapeddy, and Jason Hickey. Real World OCaml: Functional Programming for the Masses. O'Reilly Media, 2013. Google Scholar
  32. Anders Møller and Oskar Haarklou Veileborg. Eliminating Abstraction Overhead of Java Stream Pipelines using Ahead-of-Time Program Optimization. Proc. of the ACM on Programming Languages, 4(OOPSLA), 2020. URL: https://doi.org/10.1145/3428236.
  33. Matija Pretnar and Gordon D Plotkin. Handling Algebraic Effects. Logical Methods in Computer Science, 2013. URL: https://doi.org/10.2168/LMCS-9(4:23)2013.
  34. Aleksandar Prokopec, David Leopoldseder, Gilles Duboscq, and Thomas Würthinger. Making Collection Operations Optimal with Aggressive JIT Compilation. In Proc. International Symposium on Scala, 2017. URL: https://doi.org/10.1145/3136000.3136002.
  35. John C Reynolds. The meaning of types from intrinsic to extrinsic semantics. BRICS Report Series, 7(32), 2000. Google Scholar
  36. David Tarditi, Greg Morrisett, Perry Cheng, Chris Stone, Robert Harper, and Peter Lee. TIL: A Type-directed Optimizing Compiler for ML. In Programming Language Design and Implementation (PLDI), 1996. URL: https://doi.org/10.1145/249069.231414.
  37. Mads Tofte and Jean-Pierre Talpin. Region-based Memory Management. Information and Computation, 1997. URL: https://doi.org/10.1006/inco.1996.2613.
  38. Philip Wadler, Walid Taha, and David MacQueen. How to Add Laziness to a Strict Language Without Even Being Odd. In SML'98, The SML workshop, 1998. Google Scholar
  39. Mitchell Wand. Complete Type Inference for Simple Objects. In Logic in Computer Science, 1987. Google Scholar
  40. Keith Wansbrough and Simon L. Peyton Jones. Once Upon a Polymorphic Type. In Principles of Programming Languages (POPL), 1999. URL: https://doi.org/10.1145/292540.292545.
Questions / Remarks / Feedback
X

Feedback for Dagstuhl Publishing


Thanks for your feedback!

Feedback submitted

Could not send message

Please try again later or send an E-mail