Introduction
Code Generation and Emulated EXecution (COGEX) is a novel approach that allows language models (LMs) to reason through "pseudo-programs"—Python code where complex, "soft" reasoning steps are left as undefined functions. Instead of relying on a literal Python interpreter, the model emulates the execution itself, filling in the gaps with its latent knowledge. Combined with a new program search method called COTACS, this approach significantly outperforms standard prompting on both algorithmic and commonsense reasoning tasks.
The Problem: Rigid Code vs. Flexible Language
Language models have become remarkably adept at generating code to solve algorithmic problems. However, many human reasoning tasks—such as detecting sarcasm, making moral judgments, or navigating social common sense—do not translate easily into strict, compilable code.
This creates a structural gap: while code provides a helpful logical scaffold (loops, variables, clear step-by-step execution), it is too rigid for "soft" reasoning. Conversely, natural language reasoning (like Chain-of-Thought) is highly flexible but often lacks the rigorous structural constraints needed to prevent the model's logic from wandering. The core question addressed by this research is: Can we combine the structural benefits of programming with the semantic flexibility of natural language?
Methods: COGEX and COTACS
To bridge this gap, the researchers introduce two core components:
- COGEX (The Paradigm): Instead of writing a fully executable program, the model generates a pseudo-program. This is a Python function where the high-level logic is explicitly defined, but "leaf functions" (e.g.,
identify_sarcasm(text)) are left unimplemented. The LM then emulates the execution. It generates an output dictionary containing the results of these intermediate "calls" and the final answer. Essentially, the LM acts as a "fuzzy interpreter" that uses its internal knowledge to resolve the undefined code. - COTACS (The Search): To adapt to new tasks without expensive fine-tuning, the authors propose COGEX Task Adaptation via Code Search. The model generates multiple candidate programs for a task, tests them on a small subset of training data, and selects the single "General Program" that yields the highest accuracy. This optimal program is then used as a universal template for all future test instances in that dataset.
Datasets and Evaluation
To train the model, the authors created a new version of the Alpaca dataset (52k instances), using GPT-4 to convert standard natural language instructions into COGEX-style pseudo-programs.
The model was evaluated across a diverse battery of 9 tasks:
- Text Classification: CoLA (linguistic acceptability), Emotion, and SST2 (sentiment).
- Symbolic/Math: Word Sorting, Coin Flip tracking, Number Summing, and SVAMP (math word problems).
- Commonsense Reasoning: CommonsenseQA and Social IQa.
Key Insights and Results
By emulating code rather than running it, the model successfully handles "underspecified" functions. This allows the LM to use programmatic logic for structured problem parts (like iterating through a list) while relying on its semantic intuition for "soft" parts (like understanding a social cue).
- Outperforming Baselines: COGEX models (based on Llama-2 7B and 13B) consistently outperformed standard Llama-2 and Alpaca-tuned models. They also outperformed standard Chain-of-Thought (CoT) prompting across the board, particularly on tasks requiring complex state tracking or iteration.
- Massive Gains in Symbolic Tasks: On tasks like "Number Summing" and "Coin Flip," COGEX achieved improvements of 10-20% over standard prompting.
- Efficiency in Adaptation: Using COTACS with just 10 training examples to find a general program often outperformed 2-shot BM25 retrieval baselines that utilized 1,000 examples.
- Generalization via Templates: Finding one "General Program" for an entire task via COTACS proved more effective than generating a unique program for every single instance. A general template forces the model to stick to a consistent, proven reasoning strategy, reducing instance-specific hallucinations.
Limitations and Future Work
While powerful, COGEX has limitations. The LM is not a perfect interpreter; occasionally, it generates a correct program but "fails" the execution by producing an answer that contradicts its own code logic. Furthermore, emulating execution requires generating significantly more tokens (intermediate variables, function results) than a direct-to-answer approach, which increases latency and compute costs.
For purely algorithmic tasks where a real Python interpreter could work, using a literal interpreter remains safer than emulation. Future work may explore hybrid systems that utilize a real interpreter for strict, "hard" functions and LM emulation for "soft," semantic ones.
Summary updated