Abstract
Pretty printers make trade-offs between the expressiveness of their pretty printing language, the optimality objective that they minimize when choosing between different ways to lay out a document, and the performance of their algorithm. This paper presents a new pretty printer, Πe, that is strictly more expressive than all pretty printers in the literature and provably minimizes an optimality objective. Furthermore, the time complexity of Πe is better than many existing pretty printers. When choosing among different ways to lay out a document, Πe consults a user-supplied cost factory, which determines the optimality objective, giving Πe a unique degree of flexibility. We use the Lean theorem prover to verify the correctness (validity and optimality) of Πe, and implement Πe concretely as a pretty printer that we call PrettyExpressive. To evaluate our pretty printer against others, we develop a formal framework for reasoning about the expressiveness of pretty printing languages, and survey pretty printers in the literature, comparing their expressiveness, optimality, worst-case time complexity, and practical running time. Our evaluation shows that PrettyExpressive is efficient and effective at producing optimal layouts. PrettyExpressive has also seen real-world adoption: it serves as a foundation of a code formatter for Racket.
- Pablo R Azero Alcocer and S Doaitse Swierstra. 1998. Optimal pretty-printing combinators. https://web.archive.org/web/20040911044443/http://www.cs.uu.nl/groups/ST/Software/PP/pabloicfp.ps Google Scholar
- Jean-Philippe Bernardy. 2017. Disjunctionless. https://github.com/jyp/prettiest/pull/10 Google Scholar
- Jean-Philippe Bernardy. 2017. prettiest. https://github.com/jyp/prettiest/blob/5e7a12cf37bb01467485bbe1e9d8f272fa4f8cd5/Text/PrettyPrint/Compact/Core.hs Google Scholar
- Jean-Philippe Bernardy. 2017. A Pretty but Not Greedy Printer (Functional Pearl). Proc. ACM Program. Lang., 1, ICFP (2017), Article 6, Aug., 21 pages. https://doi.org/10.1145/3110250 Google ScholarDigital Library
- Olaf Chitil. 2005. Pretty Printing with Lazy Dequeues. ACM Trans. Program. Lang. Syst., 27, 1 (2005), jan, 163–184. issn:0164-0925 https://doi.org/10.1145/1053468.1053473 Google ScholarDigital Library
- Joëlle Coutaz. 1984. The box, a layout abstraction for user interface toolkits. Dec., https://doi.org/10.1184/R1/6610382.v1 Google ScholarCross Ref
- Merijn De Jonge. 2002. Pretty-printing for software reengineering. In International Conference on Software Maintenance, 2002. Proceedings.. 550–559. Google ScholarCross Ref
- Leonardo De Moura and Nikolaj Bjørner. 2008. Z3: An Efficient SMT Solver. In Proceedings of the 14th International Conference on Tools and Algorithms for the Construction and Analysis of Systems (TACAS). Budapest, Hungary. 337–340. Google ScholarCross Ref
- ESLint. 2014. Change no-comma-dangle to comma-dangle. https://github.com/eslint/eslint/issues/1350 Google Scholar
- Matthias Felleisen. 1991. On the expressive power of programming languages. Science of Computer Programming, 17, 1 (1991), 35–75. issn:0167-6423 https://doi.org/10.1016/0167-6423(91)90036-W Google ScholarDigital Library
- Matthias Felleisen, Robert Bruce Findler, Matthew Flatt, Shriram Krishnamurthi, Eli Barzilay, Jay McCarthy, and Sam Tobin-Hochstadt. 2018. A Programmable Programming Language. Commun. ACM, 61, 3 (2018), March, 62–71. https://doi.org/10.1145/3127323 Google ScholarDigital Library
- John Hughes. 1995. The design of a pretty-printing library. In Advanced Functional Programming, Johan Jeuring and Erik Meijer (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 53–96. isbn:978-3-540-49270-2 Google Scholar
- Oleg Kiselyov, Simon Peyton-Jones, and Amr Sabry. 2012. Lazy v. Yield: Incremental, Linear Pretty-Printing. In Programming Languages and Systems, Ranjit Jhala and Atsushi Igarashi (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 190–206. isbn:978-3-642-35182-2 Google Scholar
- Daan Leijen. 2000. wl-pprint: The Wadler/Leijen Pretty Printer. https://hackage.haskell.org/package/wl-pprint Google Scholar
- Leonardo de Moura and Sebastian Ullrich. 2021. The Lean 4 Theorem Prover and Programming Language. In Automated Deduction – CADE 28, André Platzer and Geoff Sutcliffe (Eds.). Springer International Publishing, Cham. 625–635. isbn:978-3-030-79876-5 Google Scholar
- Dereck C. Oppen. 1980. Prettyprinting. ACM Trans. Program. Lang. Syst., 2, 4 (1980), Oct., 465–483. issn:0164-0925 https://doi.org/10.1145/357114.357115 Google ScholarDigital Library
- Susan Owicki and David Gries. 1976. Verifying Properties of Parallel Programs: An Axiomatic Approach. Commun. ACM, 19, 5 (1976), May, 279–285. issn:0001-0782 https://doi.org/10.1145/360051.360224 Google ScholarDigital Library
- Simon Peyton-Jones. 1997. A pretty printer library in Haskell. https://web.archive.org/web/20080221052958/http://research.microsoft.com/Users/simonpj/downloads/pretty-printer/pretty.html The identified mistakes are noted at Google Scholar
- Anton Podkopaev and Dmitri Boulytchev. 2015. Polynomial-Time Optimal Pretty-Printing Combinators with Choice. In Perspectives of System Informatics, Andrei Voronkov and Irina Virbitskaite (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg. 257–265. isbn:978-3-662-46823-4 Google Scholar
- Sorawee Porncharoenwase, Luke Nelson, Xi Wang, and Emina Torlak. 2022. A Formal Foundation for Symbolic Evaluation with Merging. Proc. ACM Program. Lang., 6, POPL (2022), Article 47, Jan., 28 pages. https://doi.org/10.1145/3498709 Google ScholarDigital Library
- Sorawee Porncharoenwase, Justin Pombrio, and Emina Torlak. 2023. Artifact for A Pretty Expressive Printer. https://doi.org/10.5281/zenodo.8332960 Google ScholarDigital Library
- Sorawee Porncharoenwase, Justin Pombrio, and Emina Torlak. 2023. A Pretty Expressive Printer (with Appendices). arxiv:2310.01530. Google Scholar
- Prettier. 2016. Technical Details. https://prettier.io/docs/en/technical-details.html Google Scholar
- S Doaitse Swierstra, Pablo R Azero Alcocer, and Joao Saraiva. 1999. Designing and Implementing Combinator Languages. In Third Summer School on Advanced Functional Programming, volume 1608 of LNCS. Springer-Verlag, 150–206. Google Scholar
- The Python Language Reference. 2010. Lexical analysis. https://docs.python.org/2.7/reference/lexical_analysis.html Google Scholar
- Emina Torlak and Rastislav Bodik. 2014. A Lightweight Symbolic Virtual Machine for Solver-Aided Host Languages. In Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI). Edinburgh, United Kingdom. 530–541. https://doi.org/10.1145/2666356.2594340 Google ScholarDigital Library
- Philip Wadler. 2003. A prettier printer. The Fun of Programming, Cornerstones of Computing, 223–243. Google Scholar
- Phillip Yelland. 2015. rfmt: A code formatter for R. https://github.com/google/rfmt Google Scholar
- Phillip Yelland. 2016. A New Approach to Optimal Code Formatting. Technical note for open source project rfmt; https://github.com/google/rfmt Google Scholar
Index Terms
- A Pretty Expressive Printer
Recommendations
Using the new common Lisp pretty printer
Although not part of the initial definition of the language, pretty printing has been an important feature of Lisp programming environments for twenty years or more [1]. By the time Common Lisp was being defined, the importance of pretty printing was ...
An Abstract Pretty Printer
The author has distilled the basic operations of the pretty printer into an abstract pretty printer that uses procedural parameters to perform low-level printing actions. By encapsulating the algorithm in one place, all the pretty-printing utilities ...
A pretty but not greedy printer (functional pearl)
This paper proposes a new specification of pretty printing which is stronger than the state of the art: we require the output to be the shortest possible, and we also offer the ability to align sub-documents at will. We argue that our specification ...
Comments