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

Towards Neural Functional Program Evaluation

Torsten Scholak, Jonathan Pilault, Joey Velez-Ginorio

TL;DR

Can standard Transformer language models "run" code without a traditional interpreter? This paper demonstrates that pretrained T5 models can evaluate functional programs formulated as lambda calculus reductions, reaching over 98% exact-match accuracy on standard splits and generalizing surprisingly well to unseen types and composed functions. Crucially, the authors show that syntactic sugar makes neural evaluation easier, and that pretraining on natural language is vital for code execution tasks.

What Problem Does This Paper Address?

Traditional software interpreters are rigid: they reject incomplete or syntactically invalid code. A neural interpreter that learns to evaluate code directly could predict outputs for draft programs, partial snippets, or code containing minor bugs, aiding developer tools and program synthesis.

However, prior neural program execution work focused largely on imperative languages (like Python or FORTH). In those settings, mutable state, loops, and branching lead to complicated architectures that struggle to exceed ~62% accuracy. This paper pivots to functional programming (specifically lambda calculi), where evaluation is purely equational rewriting without hidden side effects. The authors investigate whether standard Seq2Seq Transformers can learn these reduction semantics and generalize across unseen data distributions.

Methods

Rather than mapping (program, input) -> output, the authors recast evaluation as term reduction: taking an expression and rewriting it into its normal form.

The evaluation compares two execution strategies:

  1. Lazy evaluation (WHNF): Reduces an expression to Weak Head Normal Form (stops before evaluating inside unapplied lambdas or non-root application subterms).
  2. Eager evaluation (DNF): Reduces the root and all subterms to Deep Normal Form.

The authors compare two syntactic representations:

  • 1st Lambda Calculus (1st LC): A minimal, untyped calculus containing only variables, lambdas, and applications ($e = x \mid \lambda x.e \mid e_1 \, e_2$). Data types like booleans, integers, and lists are represented using pure Church encodings.
  • 2nd Lambda Calculus (2nd LC): A typed calculus with explicit syntactic sugar for booleans, unit values, lists, conditionals (ite), and right folds (Foldr).

Using standard T5-Small (60M parameters) and T5-Large (770M parameters) models, the authors train the network as a sequence-to-sequence translator mapping input expressions to their reduced forms.

       [Input Program] ──> [Pretrained T5 Encoder-Decoder] ──> [Reduced Normal Form]

Datasets

The authors generated a synthetic dataset of 1 million unique terminating programs using a top-down, type-driven generation algorithm in the 2nd LC, then translated them into semantically equivalent 1st LC terms via Church encoding. Models were trained on 90,000 examples and evaluated on 500-example test splits across four setups:

  1. Uniform Random Split: In-distribution baseline.
  2. Split by Type: Models train on common primitive types (80% of data) and test on rare, deeply nested types (e.g., (Unit -> List List Bool -> List Unit) -> ...).
  3. Split by Function Composition: New expressions created by applying two training terms together ($e_1 \, e_2$).
  4. Split by Reduction Steps: Models train on low evaluation depth ($\le 6$ steps for WHNF, $\le 8$ for DNF) and test on complex terms requiring up to 12 or 32 reduction steps.

Key Insights and Ideas

  • Syntactic sugar reduces cognitive load on Transformers: While pure lambda calculus (1st LC) is mathematically simpler, it translates to ~2.5× longer token sequences. As a result, T5 consistently performs better on 2nd LC (the "sugared" version). Higher-level abstractions prevent the model from getting lost in long-range dependency tracking.
  • NLP pretraining transfers to symbolic reduction: Initializing with pretrained T5 weights provides a staggering ~40 percentage point advantage over training from scratch with Xavier initialization. Pretraining on natural text seems to instill latent structured reasoning skills that directly benefit program evaluation.
  • Out-of-distribution robustness varies by dimension:
  • Type generalization is surprisingly easy: T5-Large barely degrades when evaluating types never seen during training.
  • Computational depth generalization is hard: When tested on terms requiring many more reduction steps than seen during training, performance dropped sharply, especially on the unsugared 1st LC (dropping by roughly one-third).

Key Results

Exact-match string accuracy between the predicted reduction and ground truth:

Split / Scenario T5-Large (Pretrained) T5-Small (Pretrained) T5-Large (From Scratch)
Random Split (2nd LC, WHNF) 99.6% 92.6% 53.2%
Random Split (1st LC, WHNF) 99.0% 88.6% 53.0%
Split by Type (2nd LC, WHNF) 98.6% 87.0% 50.8%
Compositional Split (2nd LC, DNF) 97.2% — —
Reduction Steps Split (2nd LC, DNF) 96.0% — —
Reduction Steps Split (1st LC, WHNF) 68.6% — —

Across all benchmarks, pretrained T5-Large reliably achieved high 90% accuracy on 2nd LC tasks. Variable renaming (VR vs. NVR) produced minor differences without a clear, decisive trend.

Limitations and Future Work

  • Bounded Complexity: Experiments were limited to terminating expressions without arbitrary general recursion (recursion was constrained through primitive foldr operators), keeping input tokens $\le 512$ and output tokens $\le 256$.
  • Synthetic Domain: The code snippets are synthetically generated lambda terms rather than idiomatic functional software libraries (e.g., real-world Haskell packages containing user-defined data types, module imports, or I/O).
  • Execution Step Limits: Like many neural architectures, the model struggles when required to extrapolate to computational step counts substantially higher than those seen during fine-tuning.
  • Future Directions: Scaling to full functional programming environments, exploring step-by-step scratchpad execution (chain-of-thought style intermediate reduction), and evaluating whether neural interpreters can repair malformed or partially written programs.

Summary updated

BibTeX
@inproceedings{scholak2021towards,
  author = {Torsten Scholak and Jonathan Pilault and Joey Velez-Ginorio},
  title = {{Towards Neural Functional Program Evaluation}},
  booktitle = {AIPLANS @ NeurIPS},
  year = {2021},
  url = {https://openreview.net/forum?id=pFy0jbqiCDY},
}

Loading related content...

Figure