Abstract
This paper investigates whether language models (LMs) trained on programs develop internal representations of formal semantics or merely mimic surface-level patterns. Using a 350M parameter Transformer trained on Karel programs, we provide empirical evidence that LMs build internal "world models" of execution states. Through a novel interventional probing technique, we demonstrate that these semantic representations are intrinsic to the model's hidden states and emerge during a distinct "phase transition" in training, correlating strongly with generative success.
Introduction
A central debate in artificial intelligence is whether models trained purely on textual "form" can ever capture underlying "meaning" or semantics. In the context of programming languages, this translates to whether an LM simply learns the statistical likelihood of token sequences (e.g., a turnRight() following a move()) or if it actually tracks the state of the "world" the program manipulates. This research addresses this by asking if an LM is truly modeling program execution or just maintaining a sophisticated syntactic record of the tokens it has processed.
Methodology
The study utilizes a 350M parameter CodeGen architecture Transformer trained on a synthetic corpus of 500,000 Karel programs. Karel is a domain-specific language designed for navigating a 2D grid world. Each training example consists of five input-output grid pairs—serving as the program specification—followed by a straight-line program of 6 to 10 steps that transforms those inputs into the desired outputs.
To analyze the model's internal representations, the authors employ two primary techniques:
- Probing: Small auxiliary classifiers (linear and MLPs) are trained to predict the "abstract state" of the Karel world (robot position, direction, and obstacles) from the LM's internal hidden states during code generation.
- Interventional Probing: To ensure the LM isn't just storing a syntactic list of tokens, the authors attempt to train probes to extract alternative (fake) semantics from the same hidden states. If the LM's states are truly aligned with real semantics, mapping them to an adversarial version of the world should be significantly more difficult.
Results and Discussion
The authors identify a clear progression in the learning process, characterized by three distinct phases: "babbling" (random tokens), "syntax acquisition" (learning language rules), and finally "semantics acquisition," where the model's ability to generate correct programs suddenly spikes.
Emergent World Models and Intent
As the LM achieves high generative accuracy (92.4% on unseen specifications), its hidden states become increasingly "probe-able" for the actual state of the grid world. This suggests the LM performs a form of "latent execution" to determine the next token. Furthermore, the hidden states contain information about future states (1-2 steps ahead) with approximately 75% accuracy, implying the model represents its "intent" before generating tokens.
Rejecting the Syntactic Record Hypothesis
The interventional experiments provide critical evidence against the idea that the LM merely stores a neutral list of past tokens. Probes for "adversarial" semantics perform significantly worse than those for the original semantics, confirming that the LM's hidden states are specifically structured to represent the true logic of the program.
Conclusion
This research provides strong empirical evidence for the emergence of semantic world models in LMs trained on formal languages. While the current study is limited to straight-line programs without loops or conditionals, the high correlation ($R^2 > 0.9$) between semantic content and generative success suggests that "meaning" is a functional component of the model's performance. Future work should investigate whether these robust world models also emerge in larger, general-purpose models trained on complex, non-linear code.
Summary updated