skip to main content

‘do’ unchained: embracing local imperativity in a purely functional language (functional pearl)

Published:31 August 2022Publication History
Skip Abstract Section

Abstract

Purely functional programming languages pride themselves with reifying effects that are implicit in imperative languages into reusable and composable abstractions such as monads. This reification allows for more exact control over effects as well as the introduction of new or derived effects. However, despite libraries of more and more powerful abstractions over effectful operations being developed, syntactically the common 'do' notation still lags behind equivalent imperative code it is supposed to mimic regarding verbosity and code duplication. In this paper, we explore extending 'do' notation with other imperative language features that can be added to simplify monadic code: local mutation, early return, and iteration. We present formal translation rules that compile these features back down to purely functional code, show that the generated code can still be reasoned over using an implementation of the translation in the Lean 4 theorem prover, and formally prove the correctness of the translation rules relative to a simple static and dynamic semantics in Lean.

References

  1. Ana Bove, Peter Dybjer, and Ulf Norell. 2009. A brief overview of Agda–a functional language with dependent types. In International Conference on Theorem Proving in Higher Order Logics. Springer, 73–78. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Edwin Brady. 2013. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of functional programming 23, 5 (2013), 552–593. Google ScholarGoogle ScholarCross RefCross Ref
  3. Edwin Brady. 2014. Resource-dependent algebraic effects. In International Symposium on Trends in Functional Programming. Springer, 18–33. Google ScholarGoogle ScholarCross RefCross Ref
  4. Niklas Bülow. 2022. Proof Visualization for the Lean 4 Theorem Prover. https://pp.ipd.kit.edu/publication.php?id=b Google ScholarGoogle Scholar
  5. Thierry Coquand and Gérard Huet. 1988. The calculus of constructions. Inform. and Comput. 76, 2-3 (1988), 95–120. issn:0890-5401 Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Thierry Coquand and Christine Paulin. 1990. Inductively defined types. In COLOG-88 (Tallinn, 1988). Springer, Berlin, 50–66. Google ScholarGoogle ScholarCross RefCross Ref
  7. Leonardo de Moura and Sebastian Ullrich. 2021. The Lean 4 Theorem Prover and Programming Language. In International Conference on Automated Deduction. Springer, 625–635. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Levent Erkök and John Launchbury. 2002. A recursive do for Haskell. In Proceedings of the 2002 ACM SIGPLAN workshop on Haskell. 29–37. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. J Nathan Foster, Michael B Greenwald, Jonathan T Moore, Benjamin C Pierce, and Alan Schmitt. 2007. Combinators for bidirectional tree transformations: A linguistic approach to the view-update problem. ACM Transactions on Programming Languages and Systems (TOPLAS) 29, 3 (2007), 17–es. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Jeremy Gibbons and Bruno César dos Santos Oliveira. 2009. The essence of the iterator pattern. Journal of functional programming 19, 3 and 4 (2009). Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Son Ho and Jonathan Protzenko. 2022. Aeneas: Rust Verification by Functional Translation. (2022). To appear at International Conference on Functional Programming (ICFP). Google ScholarGoogle Scholar
  12. Simon Peyton Jones. 2003. Haskell 98 language and libraries: the revised report. Cambridge University Press. Google ScholarGoogle Scholar
  13. Richard A Kelsey. 1995. A correspondence between continuation passing style and static single assignment form. ACM SIGPLAN Notices 30, 3 (1995), 13–22. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. Electronic Proceedings in Theoretical Computer Science 153 (jun 2014), 100–126. Google ScholarGoogle ScholarCross RefCross Ref
  15. Simon Marlow, Simon Peyton Jones, Edward Kmett, and Andrey Mokhov. 2016. Desugaring Haskell’s do-notation into applicative operations. ACM SIGPLAN Notices 51, 12 (2016), 92–104. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Nicholas D. Matsakis and Felix S. Klock, II. 2014. The Rust Language. In Proceedings of the 2014 ACM SIGAda Annual Conference on High Integrity Language Technology (Portland, Oregon, USA) (HILT ’14). ACM, New York, NY, USA, 103–104. isbn:978-1-4503-3217-0 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Luke Maurer, Paul Downen, Zena M. Ariola, and Simon Peyton Jones. 2017. Compiling Without Continuations. In Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation (Barcelona, Spain) (PLDI 2017). ACM, New York, NY, USA, 482–494. isbn:978-1-4503-4988-8 Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Conor McBride and RA Paterson. 2008. Applicative programming with effects. Journal of functional programming 18, 1 (2008), 1–13. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Eugenio Moggi. 1991. Notions of computation and monads. Information and computation 93, 1 (1991), 55–92. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Tobias Nipkow. 1998. Winskel is (almost) Right: Towards a Mechanized Semantics Textbook. Formal Aspects of Computing 10 (1998), 171–186. Google ScholarGoogle ScholarCross RefCross Ref
  21. Ross Paterson. 2001. A new notation for arrows. ACM SIGPLAN Notices 36, 10 (2001), 229–240. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Simon L. Peyton Jones. 1996. Compiling Haskell by program transformation: a report from the trenches. In Proc. European Symp. on Programming. Springer-Verlag, 18–44. Google ScholarGoogle ScholarCross RefCross Ref
  23. Clément Pit-Claudel. 2020. Untangling Mechanized Proofs. In Proceedings of the 13th ACM SIGPLAN International Conference on Software Language Engineering (Virtual, USA) (SLE 2020). Association for Computing Machinery, New York, NY, USA, 155–174. isbn:9781450381765 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Barry K Rosen, Mark N Wegman, and F Kenneth Zadeck. 1988. Global value numbers and redundant computations. In Proceedings of the 15th ACM SIGPLAN-SIGACT symposium on Principles of programming languages. 12–27. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Sebastian Ullrich. 2016. Simple Verification of Rust Programs via Functional Purification. Master’s thesis. Karlsruher Institut für Technologie (KIT). https://pp.ipd.kit.edu/publication.php?id=ullrich16masterarbeit Google ScholarGoogle Scholar
  26. Sebastian Ullrich and Leonardo de Moura. 2022. Beyond Notations: Hygienic Macro Expansion for Theorem Proving Languages. Log. Methods Comput. Sci. 18, 2. Google ScholarGoogle ScholarCross RefCross Ref
  27. Sebastian Ullrich and Leonardo de Moura. 2022. Supplement of “do’ Unchained: Embracing Local Imperativity in a Purely Functional Language". Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Philip Wadler. 1990. Comprehending monads. In Proceedings of the 1990 ACM conference on LISP and functional programming. 61–78. Google ScholarGoogle ScholarCross RefCross Ref
  29. Philip Wadler and Stephen Blott. 1989. How to make ad-hoc polymorphism less ad hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT symposium on Principles of programming languages. ACM, 60–76. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. ‘do’ unchained: embracing local imperativity in a purely functional language (functional pearl)

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Article Metrics

      • Downloads (Last 12 months)412
      • Downloads (Last 6 weeks)55

      Other Metrics

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!