Introduction
A central debate in artificial intelligence is whether language models (LMs) trained purely on textual patterns can ever grasp underlying "meaning" or semantics. Are they merely "stochastic parrots" mimicking surface-level syntax, or do they build internal models of the worlds they describe? In the context of code generation, this translates to a critical question: Does an LM generating a program actually model how that program will execute, or is it just predicting the next likely character based on statistical correlations?
This paper demonstrates that LMs trained solely on next-token prediction for program synthesis tasks develop internal, emergent representations of formal program semantics. By effectively "simulating" the program's execution in their hidden states, these models show a deeper grasp of code than simple pattern matching.
Experimental Setup
To investigate whether an LM tracks the unobserved, intermediate states of a program's execution, the researchers designed a controlled experiment using a synthetic programming environment based on a domain-specific language (DSL).
- The Domain: The environment is a 2D grid world inspired by Karel the Robot, where programs navigate a grid to move, turn, and manipulate markers.
- The Task: The model performs program synthesis. It is provided with a "specification" consisting of five input-output grid pairs and must generate the code that successfully transforms the inputs into the target outputs.
- The Model & Data: The authors trained a 350M parameter Transformer (CodeGen architecture) from scratch on 2.5 billion tokens. The training corpus consisted of 500,000 randomly sampled programs (lengths 6–10), formatted as
[Specification] + [Program]sequences.
Probing for Semantic Understanding
To peer inside the "black box" and see what the model knows during generation, the researchers employed probing classifiers. They froze the trained LM and trained small, separate neural networks to predict the robot's current position, direction, and surroundings directly from the LM's internal hidden states at each step of code generation.
Crucially, the intermediate execution states of the programs were never explicitly shown to the model during training. The probes were evaluated on a trace dataset created by executing LM-generated programs to obtain "ground truth" intermediate states.
Key Insights
- Predicting the Future: Remarkably, the LM's hidden states do not just represent the current state of the robot; they contain information about future states (one or two steps ahead) before the corresponding code is even generated. This suggests the model engages in a form of implicit look-ahead planning.
- The Semantic Intervention: To prove the LM actually "understands" the execution and isn't just memorizing a complex lookup table, the authors introduced a novel intervention. They re-mapped the meaning of commands in the probes (e.g., evaluating if the LM's state for
turnLeftactually correlated better with aturnRightaction). Probes trained on these "alternative" semantics performed significantly worse, confirming that the LM's internal states are specifically aligned with the true logic of the programming language.
Emergence and Phase Transitions
The model achieved a highly impressive 92.4% accuracy in generating correct programs for 10,000 unseen specifications. Furthermore, the probes successfully extracted the robot's state with high accuracy, with a 2-layer MLP probe achieving over 80% accuracy in tracking the robot's state during execution.
The training dynamics revealed that the model does not learn everything uniformly. It progresses through distinct phases: a "babbling" phase, a "syntax acquisition" phase, and finally a "semantics acquisition" phase.
As illustrated above, there is a sharp phase transition during training. The probe's ability to extract semantic information (Semantic Content) suddenly spikes in tandem with the model's ability to generate correct programs (Generative Accuracy). This strongly implies that the acquisition of formal semantics is the direct driver of the model's task success.
Limitations and Future Directions
While these findings provide compelling evidence for emergent semantic representations, the study relies on a simplified DSL featuring "straight-line" programs without loops or conditionals. Real-world programming is vastly more complex, and it remains an open question whether these emergent representations hold for Turing-complete languages. Additionally, the model was trained on a perfectly clean, synthetic dataset, whereas natural codebases are noisy and may require different scales of modeling to achieve similar semantic grounding.
Looking forward, this experimental framework could be extended to study how LMs handle more complex "notional machines" and whether similar semantic representations emerge in natural language tasks, such as tracking the state of a story's plot or logical reasoning steps.
Summary updated