ReGAL: Refactoring Programs to Discover Generalizable Abstractions

Elias Stengel-Eskin, Archiki Prasad, Mohit Bansal

TL;DR

ReGAL (Refactoring for Generalizable Abstraction Learning) is a gradient-free method that allows Large Language Models (LLMs) to discover and store reusable code abstractions by refactoring a small set of existing programs. By building a "code bank" of verified helper functions, ReGAL significantly boosts program synthesis accuracy, enabling smaller open-source models like CodeLlama to outperform giants like GPT-3.5 in specialized domains.

What Problem Does This Paper Address?

When we ask an LLM to write code—whether it's to draw a shape, solve a math problem, or play a text-based game—the model usually generates the entire program from scratch. This "one-off" approach has two major flaws:

  1. Lack of Reusability: The model often "reinvents the wheel," repeating the same logic across different tasks. This is inefficient and increases the chance of making a math error or a logic slip in a subroutine it has already solved correctly elsewhere.
  2. Lack of Abstraction: Mapping a complex natural language query directly to low-level primitive operations (like forward(1) or left(90)) is cognitively demanding for the model. It’s much easier to map "draw a square" to a draw_square() function than to a sequence of individual movement commands.

While humans naturally build libraries of helper functions to manage complexity, LLMs lack this global view and typically treat every prompt as an isolated event.

ReGAL Overview

Methods

The authors introduce ReGAL, a framework that learns a library of reusable functions through an iterative, execution-verified process. It consists of two main phases:

1. The Training (Induction) Phase

Instead of training neural weights, ReGAL "trains" a library of functions.

  • Refactoring: Given a batch of successful "primitive-only" programs, an LLM is asked to refactor them—identifying common patterns and extracting them into helper functions.
  • Verification: Crucially, ReGAL doesn't just trust the LLM. It executes the refactored code and compares the output to the original. If the output changes, the abstraction is rejected or sent for a retry using execution error messages as feedback.
  • Library Management: The system periodically edits functions to make them more general (e.g., adding parameters) and prunes ineffective ones that lead to frequent failures.
ReGAL Pipeline

2. The Testing (Synthesis) Phase

At test time, when a new query arrives, the system retrieves the most relevant helper functions from its "Code Bank" and a few demonstrations from its "Demo Bank." The LLM then uses these high-level tools to solve the new problem.

Datasets

The researchers evaluated ReGAL across five diverse domains:

  • LOGO: Generating Turtle graphics (drawing geometric shapes).
  • Date Reasoning: Solving word problems involving calendar dates.
  • TextCraft: A Minecraft-inspired text game requiring multi-step crafting recipes.
  • MATH (Algebra): High-school level algebra word problems.
  • TabMWP: Math word problems that require reasoning over tabular data.

Key Insights and Ideas

  • Refactoring vs. Generation: ReGAL doesn't ask the model to "hallucinate" useful functions from thin air. By refactoring existing successful programs, the abstractions are grounded in functional logic.
  • The "Small Model" Equalizer: One of the most striking findings is that abstractions help smaller models more than larger ones. By providing a domain-specific "language" (the helper functions), ReGAL reduces the reasoning "hop" the model needs to make, allowing a 13B parameter model to punch way above its weight class.
  • Curriculum Learning: Presenting clusters of related problems in order of difficulty (shorter queries first) is vital. This allows the model to build simple building blocks before trying to refactor complex, compositional logic.
  • Interpretability: Unlike neural weights, the "learned" knowledge in ReGAL is a set of Python functions. Humans can read, debug, and even manually edit the library.

Key Results

ReGAL delivered substantial performance gains across the board, particularly for open-source models:

  • CodeLlama-13B saw absolute accuracy increases of +11.5% on LOGO, +26.1% on Date reasoning, and +8.1% on TextCraft.
  • Beating the Giants: On the LOGO and TextCraft benchmarks, CodeLlama-13B augmented with ReGAL abstractions actually outperformed GPT-3.5 (which was generating programs from scratch).
  • Data Efficiency: ReGAL is highly efficient; it can learn a useful library from as few as 25 training examples.
  • Robustness: In "distribution shift" scenarios (e.g., being asked to craft items in TextCraft it never saw during training), ReGAL's abstractions proved more robust than primitive-only baselines.

Limitations and Future Work

  • Static Abstractions: The learned functions are code-based and symbolic. While this makes them interpretable, they may be less flexible than neural policies in environments that change dynamically.
  • Human Alignment: The model sometimes creates functions that work perfectly but aren't named or structured the way a human programmer would prefer.
  • Domain Specificity: ReGAL shines in domains with clear subroutines (like geometry or crafting). Its impact was less dramatic in domains like MATH, where problems are often highly unique and share less structural overlap.

Future work could involve "closing the loop" by using the learned abstractions to generate even more synthetic training data, further refining the library in a self-supervised cycle.

Summary updated

BibTeX
@inproceedings{stengeleskin2024regal,
  author = {Elias Stengel-Eskin and Archiki Prasad and Mohit Bansal},
  title = {{ReGAL: Refactoring Programs to Discover Generalizable Abstractions}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=FovMAzXUpj},
}
Figure