Commit Types First: Grounding Dynamic-to-Static Code Translation in Observed Runtime Types

When a language model translates Python into Java or C++, it has to invent information the source never wrote down: the type of every argument, return value, and field. Those guesses come first in the output, as signatures and class declarations, and everything after them is written to fit them. One wrong guess near the top therefore spoils the whole program, and the compiler's reply arrives as a long error dump about the whole program. The model then has to read that dump and work out where things went wrong, which it does poorly. This proposal splits translation into two stages. In the first, we run the source program on its own tests, record the types that actually flow through each function boundary, and have the model commit to a typed skeleton: signatures, fields, and imports, with stub bodies. The target compiler checks that skeleton on its own, so its error messages stay short and only concern types. In the second stage the model fills in the bodies against a skeleton already known to compile. If a body later fails, we decide whether the fault lies in the skeleton or the body, and roll back only that stage.

The opportunity comes from a measured asymmetry. Translating from a statically typed language into a dynamic one mostly works. The reverse mostly fails: on RepoTransBench, Java-to-Python succeeds 45–63% of the time while Python-to-C++ is "often below 10%." Three findings from other settings suggest a common cause and a remedy. Errors in the first step of a chain dominate final failure (First-Step Advantage). Models fix errors well once told where they are but locate them badly (Tyen et al.). Long diagnostic text hurts repair (Haque et al.; Grubisic et al.). In dynamic-to-static translation, the type skeleton is the first step. Checking it separately localizes errors by construction and keeps compiler feedback short.

Core Claim

Most dynamic-to-static translation failures are type-commitment failures in the skeleton that then spread into the bodies. We claim that grounding the skeleton in runtime-observed types from the source program, and verifying it against the target compiler before any body is written, beats whole-program repair loops at equal compute. This should hold whether those loops pass the model raw compiler output or summarized compiler output. We also claim it lowers the rate of escape-hatch types (Object, any, void*), which pass the compiler while giving up type safety.

The claim can be tested and can fail in a specific way. If the stronger competitor, summarized diagnostics fed into a whole-program loop, matches us, then the benefit comes from removing noise and not from committing to types first. In that case the staged design is unnecessary.

Relation to Prior Work

Several papers motivate the claim by showing where translation breaks. TransCoder learned cross-language translation without parallel data but suffered from type guesswork going into static targets. TypeWeaver checked model-predicted TypeScript annotations with the official compiler. Its InCoder model went from 69% file-level to 21% package-level type-check success, and DeepTyper fell back to any/any[] in roughly 60% of predictions. That paper diagnoses models as local type predictors and proposes no multi-step remedy. RepoTransBench's agent, which reasons and calls tools in a loop, passes raw build output back into the prompt and tops out at 32.8% overall.

Other papers supply mechanisms we reuse in a new place. CoTran fine-tunes a translator with reinforcement learning, rewarding how far compilation gets before the first error and whether symbolic-execution tests pass. It uses the compiler only as a training-time number. We use the compiler during inference, at a stage boundary, and draw type evidence from executing the source. On-the-fly Rust generation closes partial syntax trees with placeholders, calls rustc mid-generation, and aborts early. This cuts compiler errors by 48.8% and adds 11 functional-correctness points over post-hoc feedback. However, it suppresses undefined-symbol and type errors and was tested on C-to-Rust, where the source is already typed. Our skeleton stage is a coarser version of that idea aimed at exactly the case it skips: unannotated source types. Haque et al. execute programs and summarize the traces for repair, raising HumanEval-Java Correct Fix Accuracy from 0.511 to 0.572, but only within one language. We borrow the idea that execution evidence should be compact. Our compact evidence is a type table recorded from the source run, not a trace of the failing target.

Two results constrain the design. Grubisic et al. found that high-temperature independent sampling overtakes iterative compiler feedback at ten or more attempts. Any staged loop must therefore be compared with sampling at matched budget, not with single-shot generation. Tyen et al. found GPT-4 locates reasoning errors only 52.87% of the time on BIG-Bench Mistake. This rules out letting the model judge for itself whether a failure belongs to the skeleton or the body. That judgment must come from the compiler and tests.

Research Program

The system has three components.

The first is a type observer. It runs the source program on its available tests and records the concrete runtime types at every function entry and exit and at every attribute write. It then condenses them into a short table for each function, giving observed types, union sets, container element types, and which entries were never observed because no test reached them.

The second is a skeleton stage. The model receives the source and the type table and emits target-language declarations with stub bodies (throw new UnsupportedOperationException() in Java, an abort() stub in C++, throw in TypeScript). The target compiler checks the skeleton. Its diagnostics are short because there is no body code to complain about, and they go back to the model for a small number of skeleton repairs. Observed union types are flagged explicitly. This is the point where a local annotation is not enough and the model must choose an overload, a generic, or a small class hierarchy. That choice is the "global refactoring" decision that TypeWeaver found models skip.

The third is a body stage with rollback. Bodies are generated function by function against the fixed skeleton and checked by compiling and then running the source's tests. When a body fails, we attribute the failure to the skeleton if the compiler error points at a declaration or if every body that calls the same signature fails. Otherwise we attribute it to the body. We then roll back only that stage.

Alongside the system we run an analysis study that tests the premise directly. On the parallel corpora we replace the model's skeleton with signatures taken from the human-written reference translation, keep body generation unchanged, and measure how much of the gap to perfect accuracy closes. This is the code analogue of the oracle-first-step experiment in First-Step Advantage, where a GPT-4 first step lifted OlMo-7B on GSM8K from 13.6% to 37.9%. If oracle signatures close little of the gap, the core claim is wrong, and we would report that instead of building further.

Baselines

The naive reference point is single-shot translation by the same base model with no compiler or test feedback. Every other number is read against it.

The strongest repair-style competitor is a whole-program loop with summarized diagnostics. The model translates the full program and compiles it. A summarizer, built in the manner of Haque et al.'s optimized traces and Grubisic et al.'s fast feedback, reduces the compiler and test output to error codes, locations, and expected-versus-found types. The model then repairs the whole program. This is the alternative most likely to match us, because it captures the noise-reduction benefit without staging. A second loop identical except for passing raw stderr, as the RepoTransBench agent does, shows how much summarization alone buys.

The strongest search competitor is parallel sampling filtered by compiler and tests. We draw N independent whole-program samples at high temperature and keep any that compile and pass, with N chosen so that total generated tokens and compiler calls equal our method's average. This is the competitor that Grubisic et al.'s results say can beat iterative feedback.

For published reference on AVATAR-TC Python-to-Java, CoTran reports 48.68% functional-equivalence accuracy and 76.98% compilation accuracy. Its nearest competitor, PLBART-base, reports 38.26%. CoTran is a fine-tuned model of about 220M parameters, so we report it as an external reference point on the same test split and with the same metrics, not as a matched-compute comparison. Beating it with a prompted model would be notable but is not the decisive test.

Three ablations isolate the key ingredient. The first uses observed types without skeleton verification: the type table is added to the prompt, but there is a single whole-program pass with summarized-diagnostic repair. The second uses skeleton verification without observed types. The third removes rollback. The oracle-signature upper bound described above completes the set.

Fairness rules apply throughout. All methods use the same base models: one open-weight code model and one API model. They share the same test splits and the same budget of total generated tokens and compiler/test calls per problem. They get the same prompt-tuning budget on a held-out development slice. Beating a baseline means a gain in functional-equivalence accuracy at matched budget, with confidence intervals from a paired bootstrap over problems, together with an escape-hatch-type rate no worse than the baseline's.

Why not just…?

Why not just annotate the Python with the observed types and translate once? This is the most serious threat, because the type table may be doing all the work. Grounding the first step is supported by First-Step Advantage, but nothing in the file shows that observed types alone fix compilation. TypeWeaver's 69%-to-21% drop came from cross-file propagation and dynamic idioms that are not annotation choices. The ablation that uses observed types without skeleton verification settles the question. If it comes within noise of the full method, we will report that the verified stage is unnecessary and that the contribution reduces to execution-observed types as prompt evidence.

Why not just summarize the compiler diagnostics in an ordinary repair loop? Summarization helps in single-language repair (0.511 to 0.572 Correct Fix Accuracy in Haque et al.). It still leaves the model to work out which of many errors is the root cause, and Tyen et al. show models are weak at exactly that. Our skeleton stage makes diagnostics short and type-only by construction, and it localizes them to declarations. The summarized-loop baseline tests whether this matters. We will also report accuracy against the length of the diagnostic the model actually received, to separate "less text" from "the right stage."

Why not just sample many candidates and filter by tests? On bounded tasks this beats feedback: at 100 samples, Grubisic et al.'s base model reached 98% of an autotuner's performance. Nothing in the file reports compile-and-test-filtered sampling at matched compute for dynamic-to-static program translation. RepoTransBench's sub-10% figure comes from agents, not from a sampling study. The matched-budget sampling baseline answers this question. We expect sampling to win on short GeeksforGeeks functions and lose as the number of interdependent signatures grows, and we will plot the crossover against signature count.

Why not just use a larger model and ask it to check its own types? Frontier models in RepoTransBench still fall below 10% on Python-to-C++. Self-checking runs into the 52.87% localization ceiling. We include a self-verify prompt variant on the API model at equal token budget so the comparison is measured and not assumed.

Proposed Experiments

The primary testbed is AVATAR-TC Python-to-Java, with its human-written tests, scored by functional-equivalence accuracy (the share of programs whose output matches on all tests) and compilation accuracy. The second testbed is the GeeksforGeeks parallel corpus from TransCoder for Python-to-Java and Python-to-C++, scored by computational accuracy. For JavaScript-to-TypeScript we use the TypeWeaver npm-513 packages that ship runnable tests, scored by file-level and package-level tsc success and the share of any annotations. InCoder's 21% package-level rate is the reported reference point. A stretch evaluation uses the Python-to-C++ subset of RepoTransBench, where the reported state is "often below 10% success." No reported per-pair result for staged or sampling methods was found on Lacuna for this subset. On every benchmark we additionally report the escape-hatch-type rate and the share of signatures with unobserved types. The second measure exposes where test coverage, and not the method, limits us.

Three checks decide the outcome.

The first is the oracle-signature study. If reference signatures close at least half of the gap between single-shot and perfect accuracy on AVATAR-TC, type commitment is the bottleneck. If they close little, the premise fails.

The second is the comparison at matched budget of the full method against both the summarized-diagnostic loop and filtered sampling. The method must beat both on functional-equivalence accuracy without raising the escape-hatch rate.

The third is the comparison of observed types alone against the full method. It tells us whether verifying the skeleton separately is a real contribution or whether runtime type evidence alone explains the gain.

A fourth analysis targets cascades directly. For every failed program we record whether the earliest compiler or test failure traces to a declaration or to a body. We then test whether our method lowers the declaration-rooted share more than the baselines do, which is the specific mechanism the proposal claims.