Pantograph: A Fluid and Typed Structure Editor

Jacob Prinz,Henry Blanchette,Leonidas Lampropoulos
DOI: https://doi.org/10.1145/3704864
2024-11-26
Abstract:Structure editors operate directly on a program's syntactic tree structure. At first glance, this allows for the exciting possibility that such an editor could enforce correctness properties: programs could be well-formed and sometimes even well-typed by construction. Unfortunately, traditional approaches to structure editing that attempt to rigidly enforce these properties face a seemingly fundamental problem, known in the literature as viscosity. Making changes to existing programs often requires temporarily breaking program structure -- but disallowing such changes makes it difficult to edit programs! In this paper, we present a scheme for structure editing which always maintains a valid program structure without sacrificing the fluidity necessary to freely edit programs. Two key pieces help solve this puzzle: first, we develop a novel generalization of selection for tree-based structures that properly generalizes text-based selection and editing, allowing users to freely rearrange pieces of code by cutting and pasting one-hole contexts; second, we type these one-hole contexts with a category of type diffs and explore the metatheory of the system that arises for maintaining well-typedness systematically. We implement our approach as an editor called Pantograph, and we conduct a study in which we successfully taught students to program with Pantograph and compare their performance against a traditional text editor.
Programming Languages
What problem does this paper attempt to address?
This paper attempts to solve the **viscosity problem** in structural editors, that is, how to make editing operations more flexible while maintaining the program structure and type - correctness. Traditional structural editors, in order to enforce the correctness of grammar and types, often require users to operate only on entire items, which makes it very difficult to modify existing code. Specifically: 1. **Background of the viscosity problem**: - Structural editors directly operate on the syntax - tree structure of the program. - Traditional methods attempt to strictly enforce these properties (such as the correctness of grammar and types), but this approach will lead to a problem: when modifying existing code, it is usually necessary to temporarily break the program structure, and prohibiting this temporary break will make editing difficult. 2. **Goals of the paper**: - Propose a new structural editing scheme that can provide a more flexible editing experience while maintaining the program structure and type - correctness. - Solve the viscosity problem encountered when editing existing code and ensure that the editing process is both flexible and will not lead to grammar or type errors. 3. **Main contributions**: - **Generalized selection mechanism**: Introduced a selection mechanism based on one - hole contexts, which can well generalize the selection operations in text editing and allow users to freely rearrange code fragments. - **Type - difference system**: Introduced type diffs for one - hole contexts to systematically maintain type - correctness. - **Implementation and experiments**: Implemented this scheme as an editor named Pantograph and verified its effect through user studies, comparing the programming performance of students using Pantograph and traditional text editors. 4. **Specific implementation**: - **One - hole context**: When cutting a one - hole context, the empty space in the expression will be filled; when pasting onto an expression, the expression will be filled into the empty space. - **Type - difference**: Each one - hole context has a type - difference, representing the difference between its internal type and external type. In this way, the editor can automatically update relevant parts during the editing process to ensure type - correctness. 5. **Advantages**: - Users do not need to learn specific editing actions, but only need to master a small number of consistent general - purpose language interfaces. - The grammar and type - correctness of the program are always maintained during the editing process, while providing flexibility similar to text editing. In short, this paper aims to solve the contradiction between flexibility and correctness in structural editors by introducing new editing mechanisms and type systems, thereby enhancing the user's programming experience.