Abstract
Chain-of-Thought (CoT) plays a crucial role in reasoning for math problem solving. We conduct a comprehensive examination of methods for designing CoT, comparing conventional natural language CoT with various program CoTs, including the self-describing program, the comment-describing program, and the non-describing program. Furthermore, we investigate the impact of programming language on program CoTs, comparing Python and Wolfram Language. Through extensive experiments on GSM8K, MATHQA, and SVAMP, we find that program CoTs often have superior effectiveness in math problem solving. Notably, the best performing combination with 30B parameters beats GPT-3.5-turbo by a significant margin. The results show that self-describing program offers greater diversity and thus can generally achieve higher performance. We also find that Python is a better choice of language than Wolfram for program CoTs. The experimental results provide a valuable guideline for future CoT designs that take into account both programming language and coding style for further advancements.
Introduction
While Large Language Models (LLMs) have become adept at multi-step reasoning through Chain-of-Thought (CoT) prompting, most research focuses on natural language (NL) explanations. However, NL is prone to "hallucinations" and logical slips that are hard to verify. Program-based CoTs—where the model writes and executes code (like Python) to find an answer—offer a solution because the logic can be strictly verified by an interpreter. This paper addresses whether the choice of programming language and the specific style of the code (e.g., variable naming or commenting) impact the model's reasoning performance.
Methods
The authors compared three main CoT "flavors" across two programming languages (Python and Wolfram Language) and traditional Natural Language:
- Self-Describing Program (SDP): The code uses variable names directly from the problem text (e.g.,
apples_bought = 5). - Comment-Describing Program (CDP): The code uses abstract variables (
v1,v2) but includes natural language comments to explain each step. - Non-Describing Program (NDP): Pure code with abstract variables and no comments.
To train these models, a semi-automatic annotation pipeline was developed. As shown in Figure 1, a retrieval-based approach used GPT-3.5-turbo to generate CoT candidates, which were then filtered by an execution engine (Wolfram Mathematica or a Python interpreter) to ensure only correct reasoning paths were used for fine-tuning Galactica models (6.7B and 30B versions).
Datasets
The study evaluated performance on three standard math word problem benchmarks:
- GSM8K: Grade school math word problems.
- MATHQA: A large-scale dataset requiring multi-step operations.
- SVAMP: A challenge set of math word problems with varying structures.
Key Insights and Analysis
- Code > Text: Program-based CoTs generally perform better than natural language CoTs. The rigid structure of code helps the model maintain logical consistency.
- The Power of Naming: The Self-Describing Program (SDP) style was the clear winner. By using descriptive variable names, the model "anchors" its mathematical logic to the linguistic context of the question, making it easier to generate correct reasoning steps.
- Python vs. Wolfram: Despite Wolfram Language's high-level mathematical design, Python proved to be a better reasoning tool. This is likely because LLMs encounter significantly more Python code during pre-training.
- Diversity vs. Precision: SDP is more "diverse," allowing the model to express logic in various ways, which benefits Majority Voting. In contrast, CDP is more deterministic and precise but less flexible.
Key Results
The performance gains were substantial. Using the 30B Galactica model with Reward Model reranking and Python SDP, the system outperformed GPT-3.5-turbo's few-shot performance across all benchmarks.
Specifically, on GSM8K, the model achieved 80.9% accuracy (vs. 75.3% for GPT-3.5). On MATHQA, it reached 78.1% (vs. 60.6%), and on SVAMP, it reached 87.0% (vs. 73.0%). The results demonstrate that even smaller models (6.7B), when equipped with optimized program CoTs, can rival much larger models using standard natural language.
Conclusion
The study highlights that the design of CoT—specifically the choice of programming language and coding style—is critical for reasoning performance. While Wolfram Language was hindered by data scarcity, Python SDP emerged as a powerful paradigm. Future work will explore better integration of natural language intuition with the rigorous verification of code execution, and potentially pre-training models on a more diverse set of scientific languages.
Summary updated