TL;DR
This paper introduces REFAZER, a novel technique that automatically learns program transformations from examples of code edits. By leveraging programming-by-example (PBE) and a new domain-specific language (DSL), REFAZER can synthesize transformations to fix student programming errors and automate repetitive code refactorings, significantly reducing manual effort.
What Problem Does This Paper Address?
Software development and education often involve repetitive code modifications. In professional settings, developers frequently encounter situations where they need to apply the same fix or refactoring across multiple parts of a codebase. Similarly, in educational contexts, instructors face the challenge of providing personalized feedback to a large number of students who often make similar coding errors. Manually creating and maintaining these transformations is time-consuming and error-prone. Existing tools often rely on predefined catalogs of transformations, making them difficult to extend. This paper addresses the need for an automated approach to learn these program transformations directly from observed code edits.
Methods
REFAZER employs a programming-by-example (PBE) methodology, building upon the PROSE framework. The core of REFAZER consists of three main components:
- Domain-Specific Language (DSL) for Program Transformations: A novel DSL, denoted LT, is introduced to describe program transformations. This DSL allows for the specification of rewrite rules that can partially abstract code edits, capturing commonalities between different examples. It includes operators for tree edits (insert, delete, update), list processing, and pattern matching on Abstract Syntax Trees (ASTs).
- Domain-Specific Deductive Algorithms: REFAZER uses a backpropagation-based synthesis algorithm. This algorithm iteratively decomposes the problem of finding a transformation into smaller subproblems by "pushing" the desired output specification backward through the DSL operators. Witness functions are crucial for this process, enabling efficient top-down synthesis.
- Ranking Functions: Since example-based specifications can be ambiguous, leading to multiple possible transformations, REFAZER employs ranking functions. These functions prioritize transformations based on robustness, aiming to select those that are more likely to be correct on unseen programs. Principles like favoring reference over constant nodes and prioritizing patterns with non-root paths are used.
Datasets
The paper evaluates REFAZER on two distinct domains:
- Introductory Programming Assignments: Data was collected from an introductory Python programming course (CS61A at UC Berkeley) involving 720 students and 4 programming assignments. The dataset consists of pairs of incorrect and correct student submissions.
- Repetitive Codebase Edits: The evaluation involved analyzing commit histories from three large C# open-source projects: Roslyn, Entity Framework, and NuGet. 59 distinct scenarios of repetitive edits were identified, with each scenario involving edits applied to multiple locations within the codebase.
Key Insights and Ideas
- Code Edits as Implicit Specifications: The fundamental insight is that observed code edits, whether from student submissions or developer commits, can serve as valuable input-output examples for learning program transformations.
- Abstraction is Key: The challenge lies in generalizing specific code edits into robust transformations. REFAZER's DSL and synthesis approach are designed to abstract away irrelevant details (like variable names or specific expressions) while retaining the essential structure of the edit.
- PBE for Program Transformations: Applying PBE techniques, traditionally used for tasks like string manipulation or data extraction, to the domain of program transformations is a significant extension.
- Domain-Specific DSL is Crucial: The effectiveness of the PBE approach is heavily reliant on a well-designed DSL that can express the types of transformations relevant to the target domain.
Key Results
- Fixing Student Submissions: In the educational domain, REFAZER successfully learned transformations that fixed incorrect submissions for 87% of students across 4 programming tasks. On average, the learned transformations helped fix submissions 3.5 submissions earlier than students would have on their own.
- Automating Repetitive Edits: For repetitive code edits in large C# projects, REFAZER learned the intended program transformation in 83% of the cases. This was achieved using an average of just 2.8 examples. In 21 cases, REFAZER identified additional locations that developers had missed, which were later confirmed as correct.
Limitations and Future Work
- Limitations:
- The current DSL's pattern matching can be too general, sometimes leading to over-selection of locations.
- Some transformations might require more advanced analysis (e.g., data flow analysis) to identify correct locations, which is not currently supported.
- The reliance on test cases for evaluating correctness means that incomplete test suites can lead to undetected faults.
- Synthesized transformations can sometimes represent multiple distinct mistakes, making it hard to provide granular feedback.
- Future Work:
- Enhance the expressiveness of tree pattern expressions to avoid over-generalization.
- Incorporate control-flow and data-flow analyses into witness functions.
- Develop new user interaction models for applying and debugging synthesized transformations.
- Explore tools for merging or splitting clusters of student submissions to provide more targeted feedback.
- Investigate the use of negative examples to specify undesired transformations.
Summary updated