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

Neural Functional Programming

John K. Feser, Marc Brockschmidt, Alexander L. Gaunt, Daniel TarlowarXiv 2016

Abstract

Programming by Example (PBE) is the task of inducing computer programs from input-output examples. It can be seen as a type of machine learning where the hypothesis space is the set of legal programs in some programming language. Recent work on differentiable interpreters relaxes the discrete space of programs into a continuous space so that search over programs can be performed using gradient-based optimization. While conceptually powerful, so far differentiable interpreter-based program synthesis has only been capable of solving very simple problems. In this work, we study modeling choices that arise when constructing a differentiable programming language and their impact on the success of synthesis. The main motivation for the modeling choices comes from functional programming: we study the effect of memory allocation schemes, immutable data, type systems, and built-in control-flow structures. Empirically we show that incorporating functional programming ideas into differentiable programming languages allows us to learn much more complex programs than is possible with existing differentiable languages.

Introduction

Programming by Example (PBE) aims to automate software engineering by generating code from a few input-output pairs. Traditionally, this relies on discrete search—a computationally expensive process akin to finding a needle in a haystack. Differentiable interpreters offer a promising alternative by relaxing discrete code into a continuous space, enabling the use of gradient descent to optimize program parameters.

However, early differentiable interpreters were limited to "toy" problems. Their reliance on low-level, assembly-like languages created a "noisy" optimization landscape. Raw jumps (GOTO) and manual memory management made it difficult for gradients to provide a clear signal, often leading to "blurred" representations where the model could not distinguish between different memory states or instruction targets.

Methodology: A Functional Approach

To address these limitations, we propose a differentiable functional programming language built on four key modeling principles:

  1. Structured Control Flow (L): Replacing raw jumps with higher-order functions like map, filter, and fold. This provides a rigid skeleton for the program, ensuring the instruction pointer's location is always well-defined.
  2. Fixed Memory Allocation (F): To prevent memory "blurring," we allocate exactly one immutable heap cell per timestep.
  3. Immutable Registers (I): We enforce immutability by ensuring each instruction writes to a fresh, dedicated register, preventing data from being overwritten.
  4. Type Systems (T): A simple type system (Integers, Pointers, Booleans) with separate storage slots prevents the model from attempting nonsensical operations, such as adding an integer to a pointer.
Structured control flow (L) provides a clear program skeleton, contrasting with the ambiguity of low-level assembly jumps.

These features are implemented within Terpret, a probabilistic programming framework, and optimized using RMSProp.

Evaluation

We evaluated our approach on a suite of synthetic list-manipulation benchmarks, ranging from straight-line tasks like dupK (duplicate an input $k$ times) to complex functional tasks like allGtK and pairwiseSum. Our models were compared against state-of-the-art discrete search tools ($\lambda^2$) and Neural Random Access Machines (NRAM).

Key Insights

  • Solving the "Blurring" Problem: In low-level models, pointer uncertainty causes the model to "read" from multiple memory locations simultaneously, averaging their values and destroying the gradient signal. Functional immutability ensures that once data is written, it remains distinct and accessible.
  • The Power of Structure: Combinators like fold focus the optimization on the core logic rather than the mechanics of loop iteration.
  • Sample Efficiency: Unlike standard neural networks, these models can generalize perfectly from as few as five examples due to the strong inductive bias provided by the functional language structure.

Results

The "Full" model (C+T+I), which incorporates all our recommendations, significantly outperformed baseline assembly-like models. On the sum task, the baseline (A) failed completely (0% success), while the functional model achieved a 91.67% success rate.

Success ratio of various models on straight-line programs of increasing length. The functional model (C+T+I) maintains high performance as program length increases.

As shown in the figure above, functional features allow the model to synthesize programs twice as long as previous differentiable methods. Furthermore, our approach maintains high accuracy even with minimal training data, whereas NRAM-like models struggle to generalize under such constraints.

Conclusion and Future Work

While discrete search tools like $\lambda^2$ remain superior for pure code synthesis, differentiable functional interpreters excel in scenarios requiring integration with "fuzzy" neural components. Future work will focus on extending support to more complex data structures like arrays and maps, and addressing the challenges of true recursion in a differentiable context.

Summary updated

BibTeX
@misc{feser2016neural,
  author = {John K. Feser and Marc Brockschmidt and Alexander L. Gaunt and Daniel Tarlow},
  title = {{Neural Functional Programming}},
  year = {2016},
  eprint = {1611.01988},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1611.01988},
}
Figure