[Paper Review] Chain of Code: Reasoning with a Language Model-Augmented Code Emulator
Chain of Code (CoC) prompts a language model to generate code and to emulate code execution with an LMulator when code cannot be executed, yielding stateful reasoning that outperforms Chain of Thought on BBH tasks and scales across model sizes.
Code provides a general syntactic structure to build complex programs and perform precise computations when paired with a code interpreter - we hypothesize that language models (LMs) can leverage code-writing to improve Chain of Thought reasoning not only for logic and arithmetic tasks, but also for semantic ones (and in particular, those that are a mix of both). For example, consider prompting an LM to write code that counts the number of times it detects sarcasm in an essay: the LM may struggle to write an implementation for "detect_sarcasm(string)" that can be executed by the interpreter (handling the edge cases would be insurmountable). However, LMs may still produce a valid solution if they not only write code, but also selectively "emulate" the interpreter by generating the expected output of "detect_sarcasm(string)". In this work, we propose Chain of Code (CoC), a simple yet surprisingly effective extension that improves LM code-driven reasoning. The key idea is to encourage LMs to format semantic sub-tasks in a program as flexible pseudocode that the interpreter can explicitly catch undefined behaviors and hand off to simulate with an LM (as an "LMulator"). Experiments demonstrate that Chain of Code outperforms Chain of Thought and other baselines across a variety of benchmarks; on BIG-Bench Hard, Chain of Code achieves 84%, a gain of 12% over Chain of Thought. In a nutshell, CoC broadens the scope of reasoning questions that LMs can answer by "thinking in code".
Motivation & Objective
- Motivate the need for combining code-based reasoning with language-model reasoning to handle semantic and numeric tasks.
- Propose Chain of Code (CoC) as a two-stage process: code generation and execution (via interpreter) or LMulator simulation when execution fails.
- Show that interleaving Python execution and LM-based simulation improves performance across diverse tasks.
- Demonstrate scalability across model sizes and evaluate cross-task prompting as a measure of general-purpose reasoning.
Proposed method
- The LM first generates reasoning in the form of code, pseudocode, or natural language to structure subproblems.
- Code execution proceeds line-by-line using a Python interpreter when possible; otherwise an LMulator simulates the effect of executing unresolved lines.
- A shared program state is updated after each line executed or simulated, enabling interleaved execution of code and LM outputs.
- An LMulator leverages the LM’s own reasoning (potentially using chain-of-thought) to produce intermediate state traces when code execution fails.
- The approach is evaluated against baselines including Direct QA and Chain of Thought across BBH tasks and GSM8K, with several ablations (e.g., Python-only, LM-only, LM state) to isolate components.
Experimental results
Research questions
- RQ1How does CoC perform across a diverse set of challenging reasoning tasks compared to baselines?
- RQ2Which task types (algorithmic, semantic, or mixed) benefit most from CoC?
- RQ3What is the impact of each CoC component (Python execution, LMulation, interleaving) on performance?
- RQ4How does CoC scale with model size and with cross-task prompting as a general-purpose reasoner?
- RQ5How does CoC fare against instruction-tuned chat models with and without tools?
Key findings
- CoC achieves 84% on BIG-Bench Hard, surpassing Chain of Thought and, in some tasks, human baselines in aggregate.
- Python-enabled CoC variants reach near or at 100% on several tasks when code is executable; LMulator-driven variants enable semantic tasks where code is not easily executable.
- CoC outperforms baselines across both algorithmic and semantic tasks and scales with model size, with improvements evident for smaller models as well.
- Cross-task prompting with CoC remains competitive and approaches human performance at scale, indicating potential as a general-purpose reasoner.
- Ablations show that interleaving interpreter execution with LM-based simulation and maintaining a program state are crucial for peak performance.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.