skip to main content

A Pretty Expressive Printer

Published:16 October 2023Publication History
Skip Abstract Section

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.

References

  1. 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 ScholarGoogle Scholar
  2. Jean-Philippe Bernardy. 2017. Disjunctionless. https://github.com/jyp/prettiest/pull/10 Google ScholarGoogle Scholar
  3. Jean-Philippe Bernardy. 2017. prettiest. https://github.com/jyp/prettiest/blob/5e7a12cf37bb01467485bbe1e9d8f272fa4f8cd5/Text/PrettyPrint/Compact/Core.hs Google ScholarGoogle Scholar
  4. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  5. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  6. Joëlle Coutaz. 1984. The box, a layout abstraction for user interface toolkits. Dec., https://doi.org/10.1184/R1/6610382.v1 Google ScholarGoogle ScholarCross RefCross Ref
  7. Merijn De Jonge. 2002. Pretty-printing for software reengineering. In International Conference on Software Maintenance, 2002. Proceedings.. 550–559. Google ScholarGoogle ScholarCross RefCross Ref
  8. 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 ScholarGoogle ScholarCross RefCross Ref
  9. ESLint. 2014. Change no-comma-dangle to comma-dangle. https://github.com/eslint/eslint/issues/1350 Google ScholarGoogle Scholar
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. 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 ScholarGoogle Scholar
  13. 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 ScholarGoogle Scholar
  14. Daan Leijen. 2000. wl-pprint: The Wadler/Leijen Pretty Printer. https://hackage.haskell.org/package/wl-pprint Google ScholarGoogle Scholar
  15. 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 ScholarGoogle Scholar
  16. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle Scholar
  19. 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 ScholarGoogle Scholar
  20. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  21. Sorawee Porncharoenwase, Justin Pombrio, and Emina Torlak. 2023. Artifact for A Pretty Expressive Printer. https://doi.org/10.5281/zenodo.8332960 Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Sorawee Porncharoenwase, Justin Pombrio, and Emina Torlak. 2023. A Pretty Expressive Printer (with Appendices). arxiv:2310.01530. Google ScholarGoogle Scholar
  23. Prettier. 2016. Technical Details. https://prettier.io/docs/en/technical-details.html Google ScholarGoogle Scholar
  24. 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 ScholarGoogle Scholar
  25. The Python Language Reference. 2010. Lexical analysis. https://docs.python.org/2.7/reference/lexical_analysis.html Google ScholarGoogle Scholar
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. Philip Wadler. 2003. A prettier printer. The Fun of Programming, Cornerstones of Computing, 223–243. Google ScholarGoogle Scholar
  28. Phillip Yelland. 2015. rfmt: A code formatter for R. https://github.com/google/rfmt Google ScholarGoogle Scholar
  29. Phillip Yelland. 2016. A New Approach to Optimal Code Formatting. Technical note for open source project rfmt; https://github.com/google/rfmt Google ScholarGoogle Scholar

Index Terms

  1. A Pretty Expressive Printer

      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)694
        • Downloads (Last 6 weeks)79

        Other Metrics

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader