One tool, many languages: language-parametric transformation with incremental parametric syntax

James Koppel, Varot Premtoon, Armando Solar-Lezama

TL;DR

This paper introduces "incremental parametric syntax" (IPS), a novel approach to building source-to-source program transformations that can run across multiple programming languages with minimal per-language effort. By representing languages with a mix of generic and language-specific parts, CUBIX enables transformations to be written once and applied broadly, producing readable and correct code.

What Problem Does This Paper Address?

Imagine needing to refactor code across multiple programming languages. Traditionally, this means writing separate tools for each language, a time-consuming and error-prone process. For instance, Facebook and Dropbox both faced massive refactoring challenges that required significant engineering effort due to their codebases being in different languages (PHP/Python for Facebook, Python for Dropbox). Existing approaches like intermediate representations (IRs) often lose crucial source code information, leading to "mutilated" output that's hard for humans to read. The core problem is the lack of a unified, yet flexible, way to write program transformations that work across diverse languages while preserving code quality and correctness.

Methods

The paper's key innovation is incremental parametric syntax (IPS). Instead of a one-size-fits-all IR, IPS represents languages using a blend of language-specific and generic components. Transformations operate on these generic components, leaving language-specific parts untouched. This allows for high-quality, source-level transformations.

The approach involves:

  • Modular Syntax: Languages are broken down into independent, reusable "fragments" (like algebraic data types).
  • Parametric Syntax: Transformations are written to be generic, operating on these fragments.
  • Incrementalization: Instead of defining a fully generic representation upfront, developers can start with existing language frontends (parsers/pretty-printers) and incrementally "genericize" parts of the language as needed for new transformations.
  • Sort Injections: A crucial technique that allows language-specific constructs to be mapped to generic ones, enabling transformations to handle language nuances without explicit per-language code for every detail.

The CUBIX system in Haskell implements these ideas, providing a framework for building IPS and running transformations.

Datasets

  • RWUS (Real World, Unchanged Semantics) Suite: A newly created benchmark suite consisting of 50 functions across 5 languages (C, Java, JavaScript, Lua, Python), randomly selected from top GitHub projects. Each function comes with a thorough test suite designed to ensure semantic equivalence after transformation.
  • Compiler Test Suites: Standard test suites for C (gcc-torture), Java (K-Java), JavaScript (test262), Lua (Lua Tests), and Python (CPython Tests) were used to evaluate the correctness of transformations.

Key Insights and Ideas

  • "Incremental" is Key: The most significant insight is that building fully generic language representations upfront is prohibitively expensive. IPS allows developers to gradually introduce generic components as transformations require them, making it practical for real-world languages.
  • Sort Injections Bridge the Gap: These are essential for handling language-specific variations (e.g., assignments being expressions in C/Java but statements in Lua/Python) without sacrificing generality. They allow transformations to operate on a "blown-down" view of the code.
  • Readability is Achievable: Unlike IR-based approaches that often mangle code, IPS transformations can produce output that is as readable as hand-written code, as validated by a human study.
  • Correctness Across Languages is Feasible: The approach allows transformations to handle language corner-cases, as demonstrated by high pass rates on compiler test suites.

Key Results

  • Language Support: CUBIX supports C, Java, JavaScript, Lua, and Python.
  • Transformation Efficiency: Adding support for a new language takes approximately 1-2 days.
  • Readability: A human study showed that machine-transformed code was rated as favorably as, or even slightly better than, human-transformed code across languages.
  • Correctness: Transformations achieved a 100% pass rate on language test suites (excluding specific self-referential or buggy tests).
  • IPT Tool: A whole-program refactoring tool for threading variables through function calls was successfully built and applied to real-world Java and Python codebases (SimpleDB, Flask, Tornado), modifying hundreds of lines of code with minimal manual intervention.
  • Benchmark Transformations: Three smaller transformations (Hoist, Testcov, TAC) were implemented, demonstrating the system's capabilities across multiple languages.

Limitations and Future Work

  • Usability: CUBIX is not a tool for lay-programmers; it requires significant learning time for new users.
  • Performance Engineering: The current implementation has overhead, though optimization ideas exist.
  • Language Support: While 5 languages are supported, extending to others requires a third-party parser. Some languages (like Prolog) might be too semantically different to integrate easily.
  • Future Work: The authors suggest further work on verifying language-parametric transformations, using modular type systems for type-aware transformations, and dealing with differing memory models and formatting preservation. They also plan to support efforts from Microsoft Research and Uber who are interested in using their approach.

Summary updated

BibTeX
@inproceedings{koppel2017one,
  author = {James Koppel and Varot Premtoon and Armando Solar-Lezama},
  title = {{One tool, many languages: language-parametric transformation with incremental parametric syntax}},
  booktitle = {SPLASH (Companion Volume)},
  year = {2017},
  doi = {10.1145/3135932.3135940},
}
Figure