Bidirectional Programming Languages
Total Page:16
File Type:pdf, Size:1020Kb
University of Pennsylvania ScholarlyCommons Technical Reports (CIS) Department of Computer & Information Science 3-13-2010 Bidirectional Programming Languages John Nathan Foster University of Pennsylvania Follow this and additional works at: https://repository.upenn.edu/cis_reports Recommended Citation John Nathan Foster, "Bidirectional Programming Languages", . March 2010. University of Pennsylvania Department of Computer and Information Science Technical Report No. MS-CIS-10-08 This paper is posted at ScholarlyCommons. https://repository.upenn.edu/cis_reports/921 For more information, please contact [email protected]. Bidirectional Programming Languages Abstract The need to edit data through a view arises in a host of applications across many different areas of computing. Unfortunately, few existing systems provide support for updatable views. In practice, when they are needed, updatable views are usually implemented using two separate programs: one to compute the view from the source and another to handle updates. This rudimentary design is tedious for programmers, difficult to reason about, and a nightmare to maintain. This dissertation describes bidirectional programming languages, which provide an elegant mechanism for describing updatable views. Unlike programs written in an ordinary language, which only work in one direction, programs written in a bidirectional language can be run both forwards and backwards: from left to right, they describe functions that map sources to views, and from right to left, they describe functions that map updated views back to updated sources. Besides eliminating redundancy, these languages can be designed to ensure correctness, guaranteeing by construction that the two functions work well together. Starting from the foundations, we identify a general semantic space of well-behaved bidirectional transformations called lenses. Then, building on this foundation, we describe a specific language for defining lenses for strings with syntax based on the regular operators. We also present extensions to the basic framework that address the complications that arise when lenses are used to manipulate sources containing inessential, ordered, and confidential data, and we describe the implementation of these features in the Boomerang language. Comments University of Pennsylvania Department of Computer and Information Science Technical Report No. MS- CIS-10-08 This technical report is available at ScholarlyCommons: https://repository.upenn.edu/cis_reports/921 Dissertation Computer & Information Science BIDIRECTIONAL PROGRAMMING LANGUAGES John Nathan Foster Technical Report MS-CIS-10-08 Department of Computer & Information Science University of Pennsylvania March 13, 2010 Abstract The need to edit data through a view arises in a host of applications across many different areas of computing. Unfortunately, few existing systems provide support for updatable views. In practice, when they are needed, updatable views are usually implemented using two separate programs: one to compute the view from the source and another to handle updates. This rudimentary design is tedious for programmers, difcult to reason about, and a nightmare to maintain. This dissertation describes bidirectional programming languages, which provide an elegant mechanism for describing updatable views. Unlike programs written in an ordinary language, which only work in one direction, programs written in a bidirectional language can be run both forwards and backwards: from left to right, they describe functions that map sources to views, and from right to left, they describe functions that map updated views back to updated sources. Besides eliminating redundancy, these languages can be designed to ensure correctness, guaranteeing by construction that the two functions work well together. Starting from the foundations, we identify a general semantic space of well-behaved bidirectional transformations called lenses. Then, building on this foundation, we describe a specic language for dening lenses for strings with syntax based on the regular oper- ators. We also present extensions to the basic framework that address the complications that arise when lenses are used to manipulate sources containing inessential, ordered, and condential data, and we describe the implementation of these features in the Boomerang language. BIDIRECTIONAL PROGRAMMING LANGUAGES John Nathan Foster A DISSERTATION in Computer and Information Science Presented to the Faculties of the University of Pennsylvania in Partial Fulllment of the Requirements for the Degree of Doctor of Philosophy 2009 Supervisor of Dissertation Benjamin C. Pierce Graduate Group Chairperson Jianbo Shi Dissertation Committee Zachary G. Ives Val Tannen Philip Wadler Steve Zdancewic ⃝c 2010 John Nathan Foster This report was typeset in Sabon by the author using the LATEX document processing system. Preface This report is a revised version of my PhD dissertation. Its technical content is essentially the same as the submitted version but several aspects of its presentation have been improved. Specically, an early chapter xing notation has been eliminated, with the material folded into the other chapters, and Chapter 4 has been revised signicantly. Also, examples have been added, proofs streamlined, typos xed, and writing polished throughout. Philadelphia, PA March 13, 2010 v Acknowledgments Some dissertation projects go like this: the student arrives on campus, the adviser sets a topic, the student disappears into the library, and emerges several years later with the nished product. Mine, thankfully, was nothing like this. The material described in this dissertation is the result of an extended collaboration with Benjamin Pierce and Alan Schmitt, with additional contributions from Davi Barbosa, Aaron Bohannon, Julien Cretin, Malo Deni´elou, Michael Greenberg, Adam Magee, St´ephane Lescuyer, Danny Puller, Alexandre Pilkiewicz, and Steve Zdancewic. I am lucky to count these talented individuals as colleagues and friends and I gratefully acknowledge their contributions to this dissertation. Throughout my time at Penn my adviser Benjamin Pierce, has always been available to brainstorm, crank through a proof, hack on a piece of code, or polish some writing. Thank you for setting the bar high, and for encouraging, nurturing, and providing help along the way. Zack Ives, Val Tannen, Phil Wadler, and Steve Zdancewic all graciously agreed to serve on my dissertation committee. I am grateful for the time they spent reading my proposal document and this dissertation. Their many insightful comments and helpful suggestions have improved it in numerous ways. Kim Bruce gave me my rst research experience as an undergraduate nearly ten years ago and has been a mentor ever since. Alan Schmitt helped me navigate the transition to graduate school and has continued to be a collaborator and close friend. Alan also hosted me during an enjoyable visit to INRIA Rhone-Alpes^ in the spring of 2006. Ravi Konuru, J´erome^ Sim´eon, and Lionel Villard were my mentors during a productive internship at IBM Research in the summer of 2007. To the members of Penn’s PLClub and Database Groups, thank you for creating a friendly and stimulating research environment. To my close friends in the department, Colin Blundell, TJ Green, Greg Karvounarakis, Micah Sherr, Nick Taylor, and Svilen Mihaylov, thank you for all the lunches, coffee breaks, and pleasant distractions from work. Thank you to my running partners, Adam Aviv, John Blitzer, Pavol Cernˇ ´y, and Jeff Vaughan for many miles of good conversation. To Dimitrios Vytin- iotis, it’s been... OK :-) To Mike Felker and Gail Shannon, thank you for solving so many bureaucratic puzzles. To my family, especially Clare, Mollie, Tita Mary, Ahmad, my parents, stepparents, and grandpar- ents, thank you for keeping me grounded and always believing in me. To Sara, thank you for reminding me how fun life can be. I’ll meet you for coffee anytime. Work supported in part by an National Science Foundation Graduate Research Fellowship and grants CPA-0429836 Har- mony: The Art of Reconciliation, IIS-0534592 Linguistic Foundations for XML View Update, and CT-T-0716469 Manifest Security. Any opinions, findings, and conclusions or recommendations expressed in this dissertation are those of the author and do not necessarily reflect the views of the National Science Foundation. vii Contents 1 Introduction 1 1.1 The View Update Problem ................................. 2 1.2 Bidirectional Programming Languages ........................... 2 1.3 Goals and Contributions .................................. 7 1.4 Acknowledgments ...................................... 9 2 Basic Lenses 11 2.1 Semantics .......................................... 11 2.2 Properties .......................................... 15 2.3 Syntax ............................................ 19 2.4 Summary .......................................... 35 3 Quotient Lenses 37 3.1 Semantics .......................................... 39 3.2 Syntax ............................................ 40 3.3 Loosening Lens Types .................................... 54 3.4 Typechecking ........................................ 55 3.5 Examples .......................................... 57 3.6 Summary .......................................... 59 4 Resourceful Lenses 61 4.1 Semantics .......................................... 66 4.2 Syntax ............................................ 70 4.3 Alignments ......................................... 77 4.4 Extensions