Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

A Low-Level Look at A-Normal Form

Published: 08 October 2024 Publication History

Abstract

A-normal form (ANF) is a widely studied intermediate form in which local control and data flow is made explicit in syntax, and a normal form in which many programs with equivalent control-flow graphs have a single normal syntactic representation. However, ANF is difficult to implement effectively and, as we formalize, difficult to extend with new lexically scoped constructs such as scoped region-based allocation. The problem, as has often been observed, is that normalization of commuting conversions is hard. This traditional view of ANF that normalizing commuting conversions is hard, found in formal models and informed by high-level calculi, is wrong. By studying the low-level intensional aspects of ANF, we can derive a normal form in which normalizing commuting conversion is easy, does not require join points, or code duplication, or renormalization after inlining, and is easily extended with new lexically scoped effects. We formalize the connection between ANF and monadic form and their intensional properties, derive an imperative ANF, and design a compiler pipeline from an untyped -calculus with scoped regions, to monadic form, to a low-level imperative monadic form in which A-normalization is trivial and safe for regions. We prove that any such compiler preserves, or optimizes, stack and memory behaviour compared to ANF. Our formalization reconstructs and systematizes pragmatic choices found in practice, including current production-ready compilers. The main take-away from this work is that, in general, monadic form should be preferred over ANF, and A-normalization should only be done in a low-level imperative intermediate form. This maximizes the advantages of each form, and avoids all the standard problems with ANF.

References

[1]
Nick Benton, Andrew Kennedy, and George Russell. 1998. Compiling Standard ML to Java Bytecodes. In International Conference on Functional Programming (ICFP). https://doi.org/10.1145/289423.289435
[2]
William Bowman. 2024. A low-level look at A-normal form (artifact). https://doi.org/10.5281/zenodo.13376916
[3]
William J. Bowman. 2018. Compiling with Dependent Types. Ph. D. Dissertation. Northeastern University. https://doi.org/10.17760/D20316239
[4]
Youyou Cong, Leo Osvald, Grégory M. Essertel, and Tiark Rompf. 2019. Compiling with continuations, or without? whatever. PACMPL, 3, ICFP (2019), 79:1–79:28. https://doi.org/10.1145/3341643
[5]
Olivier Danvy. 2003. A New One-Pass Transformation into Monadic Normal Form. In International Conference on Compiler Construction. https://doi.org/10.1007/3-540-36579-6_6
[6]
Matthias Felleisen. 1987. The Calculi of Lambda-v-cs Conversion: A Syntactic Theory of Control and State in Imperative Higher-order Programming Languages. Ph. D. Dissertation. https://www2.ccs.neu.edu/racket/pubs/dissertation-felleisen.pdf
[7]
Matthias Felleisen, Robert Bruce Findler, and Matthew Flatt. 2009. Semantics Engineering with PLT Redex. MIT Press. isbn:978-0-262-06275-6 https://mitpress.mit.edu/9780262062756/
[8]
Matthias Felleisen and Daniel P. Friedman. 1987. Control operators, the SECD-machine, and the λ -calculus. In Formal Description of Programming Concepts III, Martin Wirsing (Ed.). North-Holland, 193–222. https://legacy.cs.indiana.edu/ftp/techreports/TR197.pdf
[9]
Cormac Flanagan, Amr Sabry, Bruce F. Duba, and Matthias Felleisen. 1993. The Essence of Compiling with Continuations. In International Conference on Programming Language Design and Implementation (PLDI). https://doi.org/10.1145/155090.155113
[10]
Matthew Flatt. 2020. Functions and Calls. In ChezScheme Implementation Overview. https://github.com/cisco/ChezScheme/blob/1524f8065d8e731f6f8be2caaf36d296f4b91a32/IMPLEMENTATION.md?plain=1##L447 Accessed on Aug. 20, 2024
[11]
Matthew Fluet and Greg Morrisett. 2006. Monadic regions. J. Funct. Program., 16, 4-5 (2006), 485–545. https://doi.org/10.1017/S095679680600596X
[12]
Matthew Fluet, Greg Morrisett, and Amal J. Ahmed. 2006. Linear Regions Are All You Need. In European Symposium on Programming (ESOP). https://doi.org/10.1007/11693024_2
[13]
Andrew W. Keep and R. Kent Dybvig. 2013. A nanopass framework for commercial compiler development. In International Conference on Functional Programming (ICFP). https://doi.org/10.1145/2500365.2500618
[14]
Andrew Kennedy. 2007. Compiling with Continuations, Continued. In International Conference on Functional Programming (ICFP). https://doi.org/10.1145/1291220.1291179
[15]
Casey Klein, John Clements, Christos Dimoulas, Carl Eastlund, Matthias Felleisen, Matthew Flatt, Jay A. McCarthy, Jon Rafkind, Sam Tobin-Hochstadt, and Robert Bruce Findler. 2012. Run your research: on the effectiveness of lightweight mechanization. In Symposium on Principles of Programming Languages (POPL). ACM. https://doi.org/10.1145/2103656.2103691
[16]
Paulette Koronkevich, Ramon Rakow, Amal Ahmed, and William J. Bowman. 2022. ANF Preserves Dependent Types up to Extensional Equality. Journal of Functional Programming (JFP), 32 (2022), https://doi.org/10.1017/s0956796822000090
[17]
Luke Maurer, Paul Downen, Zena M. Ariola, and Simon Peyton Jones. 2017. Compiling without Continuations. In International Conference on Programming Language Design and Implementation (PLDI). https://doi.org/10.1145/3062341.3062380
[18]
Eugenio Moggi. 1991. Notions of Computation and Monads. Inf. Comput., 93, 1 (1991), 55–92. https://doi.org/10.1016/0890-5401(91)90052-4
[19]
Zoe Paraskevopoulou. 2020. Verified Optimizations for Functional Languages. Ph. D. Dissertation. Princeton University. https://www.cs.princeton.edu/techreports/2020/006.pdf
[20]
Zoe Paraskevopoulou and Anvay Grover. 2021. Compiling with continuations, correctly. Proc. ACM Program. Lang., 5, OOPSLA (2021), 1–29. https://doi.org/10.1145/3485491
[21]
Zoe Paraskevopoulou, John M. Li, and Andrew W. Appel. 2021. Compositional optimizations for CertiCoq. Proc. ACM Program. Lang., 5, ICFP (2021), 1–30. https://doi.org/10.1145/3473591
[22]
Zhong Shao and Andrew W. Appel. 1994. Space-Efficient Closure Representations. In Conference on LISP and Functional Programming (LFP). https://doi.org/10.1145/182409.156783
[23]
Jeremy Siek. 2012. My new favorite abstract machine: ECD on ANF. http://siek.blogspot.com/2012/07/my-new-favorite-abstract-machine-ecd-on.html Accessed on Aug. 20, 2024
[24]
David Tarditi. 1996. Design and implementation of code optimizations for a type-directed compiler for Standard ML. Ph. D. Dissertation. Carnegie Mellon University. https://csd.cmu.edu/sites/default/files/phd-thesis/CMU-CS-97-108.pdf
[25]
Mads Tofte and Jean-Pierre Talpin. 1994. Implementation of the typed call-by-value λ -calculus using a stack of regions. In Symposium on Principles of Programming Languages (POPL). https://doi.org/10.1145/174675.177855
[26]
Andrew P. Tolmach and Dino Oliva. 1998. From ML to Ada: Strongly-Typed Language Interoperability via Source Translation. J. Funct. Program., 8, 4 (1998), 367–412. https://doi.org/10.1017/S0956796898003086

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 8, Issue OOPSLA2
October 2024
2691 pages
EISSN:2475-1421
DOI:10.1145/3554319
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution International 4.0 License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 08 October 2024
Published in PACMPL Volume 8, Issue OOPSLA2

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. A-normal form
  2. CPS
  3. Compilers
  4. Intermediate Representation
  5. Monadic Form
  6. Normal Form
  7. Normalization
  8. Optimization

Qualifiers

  • Research-article

Funding Sources

  • Natural Sciences and Engineering Research Council of Canada NSERC
  • Defense Advanced Research Projects Agency DARPA
  • Naval Information Warfare Center Pacific NIWC Pacific

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • 0
    Total Citations
  • 238
    Total Downloads
  • Downloads (Last 12 months)238
  • Downloads (Last 6 weeks)51
Reflects downloads up to 10 Feb 2025

Other Metrics

Citations

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Login options

Full Access

Figures

Tables

Media

Share

Share

Share this Publication link

Share on social media