Natural Language Embedded Programs for Hybrid Language Symbolic Reasoning (2024)

Abstract: How can we perform computations over natural language representations to solve tasks that require symbolic and numeric reasoning? We propose natural language embedded programs (NLEP) as a unifying framework for addressing math/symbolic reasoning, natural language understanding, and instruction following tasks. Our approach prompts a language model to generate full Python programs that define functions over data structures which contain natural language representations of structured knowledge. A Python interpreter then executes the generated code and prints the output. Despite using a task-general prompt, we find that this approach can improve upon strong baselines across a range of different tasks including math and symbolic reasoning, text classification, question answering, and instruction following. We found that the generated programs are interpretable since they outline the exact reasoning process followed by the program interpreter.

## introduction solving complex language tasks often requires performing computations over natural language representations. for language-based reasoning, chainof-thought prompting (cot; @xcite has emerged as a promising approach for surfacing the symbolic reasoning capabilities of large language models (llms). however, certain types of computations (e.g., arithmetic) are unnatural to perform in pure language space, and hence present difficulties for llms. general-purpose programming languages, on the other hand, provide convenient abstractions as well as predefined libraries and functions for natively implementing many types of symbolic computations, and there * equal contribution. correspondence to hongyin luo at hyluo@mit.edu. work done during jiaxin ge visiting mit. source code is available at https://github.com/ luohongyin/langcode . this work describes a task-general approach for combining the language-based reasoning capabilities of llms with symbolic computations enabled by the use of programs. specifically, we prompt llms to generate natural language embedded programs (nleps), which are fully executable python programs containing appropriate package importing, structured natural language representations of knowledge, function definitions for problem solving, and response printing. the generated nlep is then executed using a python interpreter that captures the standard output of the program as the response. an example of an nlep generated by gpt-4 is shown in figure 1 . nleps use code as a scaffold to reason over natural language representations of data and knowledge. this makes our approach different from toolformer @xcite and language model as tool maker @xcite , which instead use language as the scaffold and interleave api calls within natural language sentences during llm generation. compared to programof-thought (pot; @xcite and program aided language models (pal; @xcite , which mainly focus on math and symbolic problems, nleps handles natural language, math, and symbolic tasks with significantly better generalization ability based on task-general prompts. compared to existing neuro-symbolic augmentations approaches needing new logical language (li et al., ## approach: nlep prompting an nlep is a program containing both programming code and natural language. nleps use natural language in several different ways. first, it uses natural language comments to guide step-by-step program generation. second, language is used to represent structured knowledge through python's native data structures (e.g., dictionaries and lists). finally, an nlep uses language to print fluent responses to the user input by constructing a standard output string containing references to program variables. the hybrid language-symbolic design of nlep enables generalized problem solving for natural language, math, symbolic reasoning, and api calling tasks, which have traditionally been tackled by separate mechanisms. this approach combines the benefits of language-based reasoning with program synthesis: comments and knowledge in natural language improve program generation, while the structured/symbolic reasoning powered by program interpreters provides more accurate computations than would have been obtained via direct decoding from llms. an example of an nlep for answering a question is shown in figure 5 . in the generated program, each section is preceded by comments in natural language, and the defined counting function uses knowledge stored in a key-value dictionary (which itself is generated from gpt-4's internal knowledge) to find the correct answer. finally, the answer is printed through a natural language response. in this example, we generated 5 independent nleps and found that they achieve 100% accuracy, compared to 60% for chatgpt-4 and 40% gpt-4 api. nlep structure. more generally, each nlep contains four sections: (1) importing necessary libraries, (2) defining variables containing structured knowledge, (3) implementing problem-solving functions, and (4) printing the response in natural language. instead of providing direct solutions for each task, we guide the model to arrive at a solution following this four-step process. the structured and modularized programming design disentangles the knowledge extraction and reasoning steps, in contrast to pot @xcite , where different segments may intertwine. as show in the example in figure 1 , an nlep answers the question by constructing a structured knowledge dictionary containing the birthday and start date of the us presidents. to recognize the weekdays, the program utilizes pre-defined functions in the datetime package. the selected answers are stored in a list and then embedded into an output template. the nlep also handles the situation when no answer is found. the correct answer is then printed by the nlep. ## experiments we evaluate natural language embedded programs (nleps) on 16 tasks across three broad task categories. the tasks and corresponding prompts are summarized in ## conclusion this work describes natural language embedded programs (nlep), which flexibly combine natural language reasoning with program synthesis within prompt-based learning to tackle a variety of tasks. our experiments demonstrate that nleps expand the scope of applications that can be addressed by program synthesis by more closely incorporating natural language during code generation.