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.
- 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 Scholar
Digital Library
- Edwin Brady. 2013. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of functional programming 23, 5 (2013), 552–593.
Google Scholar
Cross Ref
- Edwin Brady. 2014. Resource-dependent algebraic effects. In International Symposium on Trends in Functional Programming. Springer, 18–33.
Google Scholar
Cross Ref
- Niklas Bülow. 2022. Proof Visualization for the Lean 4 Theorem Prover. https://pp.ipd.kit.edu/publication.php?id=b
Google Scholar
- Thierry Coquand and Gérard Huet. 1988. The calculus of constructions. Inform. and Comput. 76, 2-3 (1988), 95–120. issn:0890-5401
Google Scholar
Digital Library
- Thierry Coquand and Christine Paulin. 1990. Inductively defined types. In COLOG-88 (Tallinn, 1988). Springer, Berlin, 50–66.
Google Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Son Ho and Jonathan Protzenko. 2022. Aeneas: Rust Verification by Functional Translation. (2022). To appear at International Conference on Functional Programming (ICFP).
Google Scholar
- Simon Peyton Jones. 2003. Haskell 98 language and libraries: the revised report. Cambridge University Press.
Google Scholar
- Richard A Kelsey. 1995. A correspondence between continuation passing style and static single assignment form. ACM SIGPLAN Notices 30, 3 (1995), 13–22.
Google Scholar
Digital Library
- Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. Electronic Proceedings in Theoretical Computer Science 153 (jun 2014), 100–126.
Google Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Conor McBride and RA Paterson. 2008. Applicative programming with effects. Journal of functional programming 18, 1 (2008), 1–13.
Google Scholar
Digital Library
- Eugenio Moggi. 1991. Notions of computation and monads. Information and computation 93, 1 (1991), 55–92.
Google Scholar
Digital Library
- Tobias Nipkow. 1998. Winskel is (almost) Right: Towards a Mechanized Semantics Textbook. Formal Aspects of Computing 10 (1998), 171–186.
Google Scholar
Cross Ref
- Ross Paterson. 2001. A new notation for arrows. ACM SIGPLAN Notices 36, 10 (2001), 229–240.
Google Scholar
Digital Library
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- Sebastian Ullrich and Leonardo de Moura. 2022. Beyond Notations: Hygienic Macro Expansion for Theorem Proving Languages. Log. Methods Comput. Sci. 18, 2.
Google Scholar
Cross Ref
- Sebastian Ullrich and Leonardo de Moura. 2022. Supplement of “do’ Unchained: Embracing Local Imperativity in a Purely Functional Language".
Google Scholar
Digital Library
- Philip Wadler. 1990. Comprehending monads. In Proceedings of the 1990 ACM conference on LISP and functional programming. 61–78.
Google Scholar
Cross Ref
- 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 Scholar
Digital Library
Index Terms
‘do’ unchained: embracing local imperativity in a purely functional language (functional pearl)
Recommendations
Systems programming in the functional language FP
AbstractThe development of systems software in functional programming language is still an open problem. This paper describes a preliminary experiment with systems programming in Backus’ FP systems[1]. It consists of two systems programs. One is a self-...
Immutables in C++: Language Foundation for Functional Programming
Central European Functional Programming SchoolAbstractThe C++ programming language is a multiparadigm language, with a rich set of procedural, object-oriented, generative and, since C++11, functional language elements. The language is also well-known for its capability to map certain semantic ...
Using functional languages to facilitate C++ metaprogramming
WGP '12: Proceedings of the 8th ACM SIGPLAN workshop on Generic programmingTemplate and Preprocessor Metaprogramming are both well-known in the C++ community to have much in common with Functional Programming (FP). Recently, very few research threads on underpinning these commonalities have emerged to empower cross-development ...






Comments