NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

The Magic of IF: Investigating Causal Reasoning Abilities in Large Language Models of Code

Xiao Liu, Da Yin, Chen Zhang, Yansong Feng, Dongyan Zhao

Abstract

Large Language Models (LLMs) have demonstrated remarkable capabilities across various natural language tasks, yet complex causal reasoning remains a significant challenge. This paper investigates whether the structured nature of programming languages can be leveraged to enhance these reasoning abilities. We introduce "Code Prompting," a technique that reformulates natural language reasoning problems into code-like structures, specifically utilizing conditional if statements. Our results indicate that Code-LLMs, such as Codex, exhibit significantly improved performance in abductive and counterfactual reasoning when prompted with code syntax compared to standard natural language instructions.

Introduction

Causal reasoning is fundamental to human intelligence, encompassing the ability to infer causes from effects (abductive reasoning) and to simulate alternative outcomes for past events (counterfactual reasoning). While text-only LLMs often struggle with the implicit nature of causality in narrative prose, programming languages require explicit causal declarations. In code, a statement like if condition: action represents an unambiguous causal link. This research explores whether the "causal priors" embedded in code pre-training can be "unlocked" to solve general reasoning tasks.

Methodology: The Power of Code Prompting

The core innovation of this work is Code Prompting. Instead of presenting a problem in plain text, the researchers wrap the premise, hypothesis, and conclusion within a Pythonic structure.

Examples of Code Prompting for Abductive and Counterfactual Reasoning tasks.

As shown in Figure 2, an abductive reasoning task is framed by defining functions for the premise() and ending(), then asking the model to complete a hypothesis() function within a main() execution block. This forces the model to treat the hypothesis as a logical condition necessary for the observed ending.

The authors also conducted various interventions to isolate the cause of performance gains, including structural interventions where if blocks were replaced with simple sequential calls to see if the conditional syntax itself was the primary driver.

Experimental Results

The researchers evaluated their approach on two major benchmarks: αNLI (for abductive reasoning) and TimeTravel (for counterfactual reasoning).

Quantitative results for Abductive Reasoning showing the superiority of Codex with code prompts.

As illustrated in Figure 3, the performance jump is substantial. On the abductive reasoning task, Codex with code prompts achieved a 13.7 BLEU4 score, nearly tripling the 4.9 score of the text-only davinci-002. Similar trends were observed in counterfactual reasoning, where code-prompted Codex reached 66.8 BLEU4 compared to 49.0 for text prompts. Human evaluations further supported these findings, with evaluators consistently rating code-prompted outputs as more coherent and better at preserving original context.

Discussion: Why Does Code Prompting Work?

The study reveals that the "magic" lies in the syntax. When the if block was removed in favor of sequential execution, performance dropped sharply. This suggests that Code-LLMs have developed a specialized understanding of conditional logic that is more robust than the reasoning patterns found in text-only models.

Interestingly, the researchers noted an "Alignment Tax." Instruction-tuned models (like text-davinci-002) sometimes underperformed compared to the base code model (code-davinci-002). This suggests that the process of aligning models to follow human conversational patterns in text might inadvertently suppress some of the raw logical reasoning capabilities acquired during code pre-training.

Conclusion

This research demonstrates that the explicit logic of programming languages provides a powerful framework for enhancing causal reasoning in LLMs. By mapping the messy causality of natural language onto the rigid structure of code, we can significantly improve a model's ability to navigate complex "what-if" and "why" scenarios. Future work will explore whether more complex programming constructs, such as recursion or nested loops, can unlock even deeper layers of machine intelligence.

Summary updated

BibTeX
@inproceedings{liu2023magic,
  author = {Xiao Liu and Da Yin and Chen Zhang and Yansong Feng and Dongyan Zhao},
  title = {{The Magic of IF: Investigating Causal Reasoning Abilities in Large Language Models of Code}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.findings-acl.574},
}
Figure