Which Programming Language and What Features at Pre-training Stage Affect Downstream Logical Inference Performance?

Fumiya Uchiyama, Takeshi Kojima, Andrew Gambardella, Qi Cao, Yusuke Iwasawa, Yutaka Matsuo

Abstract

This paper investigates the causal link between pre-training on programming languages and the development of logical reasoning capabilities in large language models (LLMs). By training models from scratch on controlled datasets of 200 million tokens across various programming and natural languages, the researchers demonstrate that code pre-training significantly enhances performance on formal logic benchmarks like FLD. The study identifies the hierarchical structure of code, measured via Abstract Syntax Tree (AST) depth, as a critical factor, suggesting a "Goldilocks" range of complexity for optimal reasoning gains.

Introduction

The "code is logic" hypothesis suggests that the structured nature of programming languages is responsible for the reasoning prowess of modern LLMs. However, isolating this effect is challenging due to the heterogeneous nature of training data in production models. This study provides a controlled comparison to determine if code pre-training inherently improves logical inference, whether specific languages offer unique advantages, and which structural features are most impactful.

Experimental Methodology

The researchers trained decoder-only models, including GPT-2 and LLaMA variants, using a fixed budget of 200 million tokens per dataset. The pre-training data spanned 10 programming languages (e.g., Python, Haskell, C) and 3 natural language corpora (Wikipedia, FineWeb, C4).

Evaluation focused on:

  1. Formal Logic Deduction (FLD): A benchmark for multi-step reasoning.
  2. bAbi: Simple reasoning tasks.
  3. GLUE: General natural language understanding.

To probe the "why" behind code's effectiveness, the team conducted ablation studies on Python code (removing comments, scrambling identifiers) and analyzed the structural complexity using Abstract Syntax Tree (AST) depth.

Results: The Logical Edge of Code

Models pre-trained on code consistently outperformed those trained on natural language in logical tasks. While models trained on FineWeb or C4 often failed to follow the logical deduction format entirely (scoring near 0.00 on FLD), code-trained models achieved significantly higher accuracy.

Table 1: Logical reasoning performance (FLD and bAbi) across different pre-training datasets. Code-based models significantly outperform natural language baselines.

The study found that the specific programming language mattered less than the presence of code itself, with Python, C, and Haskell showing comparable strengths. A notable exception was Java, which underperformed in certain configurations.

Structural Complexity and AST Depth

A key finding involves the relationship between code hierarchy and reasoning. By categorizing code by its AST depth—representing the nesting of loops, conditionals, and functions—the researchers identified a "Goldilocks" zone.

Figure 1: Distribution of AST depth in the pre-training data. The study found that moderate complexity (depth 8-11) is most beneficial for learning logical dependencies.

Code with "Middle" complexity (depth 8–11) provided the best training signal. Extremely deep code (depth 12–20) proved detrimental, likely exceeding the models' capacity to capture long-range dependencies at the tested scales.

Discussion and Limitations

The results suggest that the rigid syntax and internal dependencies of code act as a "logic gym," training models to follow strict rules and structural patterns. Ablation tests showed that while human-readable variable names are helpful, the underlying structural consistency is the primary driver of logical performance.

However, code pre-training is not a panacea. It does not improve performance on tasks requiring world knowledge or commonsense reasoning, where natural language remains superior. Furthermore, the study's scale (200M tokens, up to 1.5B parameters) is small compared to state-of-the-art models, leaving questions about how these trends scale to the trillion-token regime.

Conclusion

Pre-training on programming languages is a potent method for "unlocking" logical reasoning in LLMs. The hierarchical structure of code, rather than its specific semantic content, appears to be the key mechanism. Future research should focus on optimizing the blend of code and text to balance formal logic with general knowledge.

Summary updated

BibTeX
@inproceedings{uchiyama2024which,
  author = {Fumiya Uchiyama and Takeshi Kojima and Andrew Gambardella and Qi Cao and Yusuke Iwasawa and Yutaka Matsuo},
  title = {{Which Programming Language and What Features at Pre-training Stage Affect Downstream Logical Inference Performance?}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.1008},
}
Figure