Arithmetic Reasoning with LLM : P rolog Generation & Permutation (2024)

Abstract: Instructing large language models (LLMs) to solve elementary school math problems has shown great success using Chain of Thought (CoT). However, the CoT approach relies on an LLM to generate a sequence of arithmetic calculations which can be prone to cascaded calculation errors. We hypothesize that an LLM should focus on extracting predicates and generating symbolic formulas from the math problem description so that the underlying calculation can be done via an external code interpreter. We investigate using LLM to generate Prolog programs to solve mathematical questions. Experimental results show that our Prolog-based arithmetic problem-solving outperforms CoT generation in the GSM8K benchmark across three distinct LLMs. In addition, given the insensitive ordering of predicates and symbolic formulas in Prolog, we propose to permute the ground truth predicates for more robust LLM training via data augmentation.

## introduction large language models (llms), with their scaling of model size and data size, have demonstrated impressive performance across various understanding and generation tasks @xcite @xcite @xcite @xcite . nevertheless, such llms fall short in addressing mathematical problems that involves arithmetic, commonsense, and symbolic reasoning -topics that may appear deceptively simple to humans @xcite . existing works leveraged chain-of-thought (cot) reasoning that asks language models to generate both the answer and the step-by-step reasoning chain, which helps break down a complex reasoning task into a sequential thought process @xcite . particularly, arithmetic reasoning with cot is shown to be an emergent ability that language models acquired during the scaling process @xcite . yet, natural language reasoning is not native to mathematical operations and symbolic manipulations. a line of work has focused on augmenting language models with deterministic computation resources like a calculator @xcite or program-based tools @xcite . however, all such methods require a sequential reasoning trajectory, where models need to translate the natural language questions into sequential mathematical or logical operations. our research probes into the application of prolog, a logic programming language, in solving the arithmetic reasoning task. prolog solves arithmetic reasoning tasks by defining an unordered set of predicates and running queries over them. we further explain the unique properties of prolog in section 2. in prolog code generation for arithmetic reasoning, llms extract facts and rules in mathematical ques-tions and formulate them into prolog code. if the facts and rules are accurately captured, a prolog interpreter can precisely solve for a correct answer in a deterministic way. our research has the following contributions: 1) we curate and open-source the gsm8k-prolog dataset with a semi-automatic approach, which contains arithmetic reasoning problems and their corresponding prolog code solutions. 2) our experiments show that prolog code generation is consistently better than cot on the arithmetic reasoning task, indicating that llm can focus on predicate extractions and rely on an external tool to calculate and perform the logical induction to address mathematical problems. 3) given the non-sequential nature of predicates in prolog code, we propose predicate permutation as a data augmentation method and demonstrate its efficacy in robust llm training. ## preliminaries: prolog language prolog is a logic programming language, which was initially designed for artificial intelligence and computational linguistics @xcite @xcite . as shown in the upper graph of figure 2 , a prolog program defines a set of predicates that contains facts and goals. in the example, facts include earn(weng, 12) that declares the hourly salary of weng, and work(weng, 50) that defines the working minutes of weng; the goals constitute a rule in the form of @xmath0:-<goal_1>,<goal_2>, .... a rule is true when all the goals are satisfied. having all the facts and goals defined in the program, users can make a query to obtain the solutions that make the rule true given all the facts. moreover, prolog codes are not sequential like python, meaning that the order of facts and rules does not alter the result of the program. the lower graph in figure 2 , shows an equivalent sample that permutes the order of the predicates, which produces the same result as the original program. ## related work arithmetic reasoning the chain-of-thought (cot) prompting approach @xcite first proposes to prompt the model to generate the reasoning chain step-by-step to reach the final answer. afterwards, advancements have been made in llms' reasoning capacity via step-by-step methods @xcite @xcite . however, the natural language generation still performs poorly on complex or multi-step reasoning. therefore, one trajectory of efforts has been made to leverage reasoning structures like trees @xcite and graphs @xcite . another trajectory is to render the reasoning task based on external tools @xcite @xcite @xcite , which is the one that we are following. besides, @xcite rft method shares the idea of dataset augmentation, but they compile rejection samples from multiple models to form an augmented training set, which is different from proper's automatic permutation. neural symbolic reasoning neural symbolic reasoning @xcite @xcite @xcite aims to leverage both neural networks and symbolic reasoning to obtain better reasoning abilities and transparency. those methods suffer from low scalability of learning and reasoning components. llms are hence adopted to generate symbolic representations from natural language @xcite @xcite , where deterministic symbolic solvers will process the query and symbolic representations generated by llms to conduct reasoning or proofs. prolog has been a popular candidate for the format of symbolic representations. we are posited on this trajectory and in the specific field of arithmetic reasoning. ## conclusion in conclusion, we aim to enhance the reasoning performance of llms. we adopt the pipeline that the model generates prolog predicates from a mathematical question in natural language and an external prolog interpreter processes the query for a final result. we contribute an open-sourced corpus named gsm8k-prolog, which is a high-quality prolog-annotated version of gsm8k. we show that prolog generation substantially outperformed cot generation across all three 7b models for solving arithmetic reasoning problems. we also propose proper, a data augmentation method designed specifically for prolog code generation, which enables the finetuned models to learn the nonsequential nature of prolog predicates. proper further improves the model's accuracy on gsm8k-prolog and mitigates early convergence during training. lastly, due to the gap between crossentropy loss objective and accuracy, we suggest using validation accuracy instead of validation loss to pick the best checkpoint.