Manuscript Offprint

arXiv:2608.25657

Narcissus: Program Synthesis Using Context-Aware LLM Approximations

Tilman Hinnerichs, Sebastijan Dumancic, Neil Yorke-Smith

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Abstract

Large language models (LLMs) excel at programming, but not when the task fixes the target language: prompted with a grammar rare in their training data, their programs usually break the grammar or fail the given specification. Enumerative synthesizers search the space of syntactically correct programs systematically guided by LLMs; the state of the art guides them by approximating LLM proposals into rule frequencies, which loses where each construct belongs and prunes every rule the proposals miss, exactly when the proposals are wrong. We present NARCISSUS, a synthesizer that keeps the proposals as syntax trees and scores each expansion of a candidate program in its context: does a proposal with the same surrounding structure continue the same way, and does the expansion rebuild a fragment the proposals repeat? A regularization term keeps every rule reachable, so wrong proposals delay the solution but cannot hide it. Across five domains and two search backends, NARCISSUS beats static guidance at every budget and consistently outperforms re-prompting the LLM to fix its own proposals; it reaches proposal-like programs an order of magnitude sooner and solves 40% of ARC tasks where the raw proposals solve 13%, all without a single LLM call during search.

Introduction

Large language models (LLMs) have proven to excel at a wide range of programming tasks. The typical workflow is a loop: prompt the model with the task, sample a program from it, run the program, and check the result; if the program is wrong, sample again or feed the failure back (Li et al. 2022; Chen et al. 2024; Shinn et al. 2023). For everyday programming this loop usually suffices: common languages dominate the model's training data, so a correct program is only a few prompts away.

The loop breaks when the task fixes the language the program must be written in. Such tasks are everywhere: a compiler for a processor may only emit that processor's instructions, a tool inside a spreadsheet only formulas the spreadsheet understands (Gulwani, Polozov, and Singh 2017), and abstract-reasoning benchmarks are attacked with hand-designed domain-specific languages (Hodel 2023); a program outside the language cannot run at all. These specific languages are rare in the LLM's training data, and a grammar in the prompt does not replace training on it: asked for a program in the grammar of Figure 1, even a strong LLM reaches for familiar string functions (substring, split, upper) the grammar does not provide. Retrying does not fix this: each attempt is a fresh and expensive model call drawn from the same distribution, so the loop mostly resamples the same mistakes. Programs sampled this way frequently break the grammar or fail the examples (Barke et al. 2024; Li, Parsert, and Polgreen 2024); in our benchmarks, even the best proposal model solves at most a third of the tasks outright. We call the programs an LLM emits for a task its proposals.

Program synthesis formalizes this setting: given a grammar defining the language and a specification, e.g., a set of input-output examples, find a program of the grammar that satisfies it. The LLM of the loop above already acts as a synthesizer, just one that is free to ignore the grammar. Enumerative synthesizers (Gulwani, Polozov, and Singh 2017; Padhi et al. 2019; Si et al. 2019) instead search the language systematically: they build candidate programs, from now on candidates, from the grammar and test each finished one against the examples. Their weakness is order: the number of programs grows exponentially with program size, so within any budget, success depends on which candidates are tried first. The naïve way to combine them keeps the LLM in the loop, asking at every expansion which rule fits the program built so far; conditioning on context is exactly what makes LLMs strong. But a guided synthesizer may expand millions of partial candidates per task, so the LLM's judgment must be compiled into something cheaper.

Recent work approximates the proposals with a heuristic. HySynth (Barke et al. 2024) and Li, Parsert, and Polgreen (2024) prompt the LLM a handful of times per task before search and compile the returned proposals into a static, context-free prior: each grammar rule scores by how often the proposals use it, independent of the context where it is used. However, the proposals also strongly indicate where a construct belongs; we call the rules already placed around an open position the context of the decision that fills it. For example, if every proposal program ends with concat (x, "."), then concat is strong evidence at the end of a program and none elsewhere, and the whole subterm is worth rebuilding together; a plain frequency count captures neither. Even worse, a rule no proposal uses receives negligible weight, locking the search out of it, exactly when the proposals are wrong and the search would have to correct

Figure

Figure 1: Overview of the NARCISSUS pipeline (on the SLIA task of abbreviating names to initials). (1) The LLM is prompted once with the grammar G and specification E for a handful of proposals II, which are neither guaranteed solutions nor grammatical (e.g. concat (true, ".").) (2) Proposals are parsed into abstract syntax trees (ASTs) and repaired against the grammar (syntactically wrong sub-terms become typed holes); (3) recurring fragments are mined into macro-rules, extending the grammar to G+G^{+}; (4) the proposal ASTs yield a context-aware heuristic (prefix alignment along the root-to-hole path, where _ marks a position the context ignores; sub-program reuse; regularization); and (5) heuristic-based search (e.g., bottom-up or genetic) enumerates programs in G+G^{+}, scoring each expansion in its context, and returns the first candidate satisfying E.

them. The compiled guidance is thus blind to where constructs belong and blocked from what the proposals missed. Ideally, a heuristic built from proposals must be aware of the context, cheap, and still functional when the proposals are poor.

We present NARCISSUS, a synthesizer using context-aware LLM approximations as guidance. 1^{1} During search, NARCISSUS ranks every rule by how often the proposals use it in the same context. To do so, NARCISSUS samples proposals once per task and parses them into abstract syntax trees (ASTs), repairs the parts that violate the grammar, and mines their repeated fragments into shortcut rules (Figure 1). Three signals then score each expansion of a candidate: prefix alignment, whether proposals that share the candidate's context use the same rule; sub-program reuse, whether the rule rebuilds a fragment the proposals repeat; and regularization, a soft bias towards the proposals' program size plus a positive floor that keeps every rule reachable. This guidance is cheap: it costs a tree lookup per expansion, and the LLM is never called during search. Further, since no rule is ever pruned, tasks remain solvable even when the proposals are misleading.

We evaluate NARCISSUS with two backends, a cost-based bottom-up beam and a genetic top-down search, across five domains (SLIA, BV, DeepCoder, ARC, ARGA) (Padhi et al. 2019; Alur et al. 2013; Balog et al. 2017; Chollet 2019; Xu, Khalil, and Sanner 2023), with proposals from several LLMs ranging from mostly correct to almost never grammar-valid. The key finding is that context drives the gain: NARCISSUS beats the static prior on all benchmarks under both backends, and plain re-prompting, the loop we opened with, by a wide margin, staying reliable even when few proposals are grammar-valid or correct. Our heuristic reaches proposal-like programs about an order of magnitude sooner, and on ARC it solves 40% of the tasks while the raw proposals, kept to the target grammar, solve 13%.

In summary, we contribute (i) a context-aware heuristic that scores each grammar choice by prefix alignment, subprogram reuse, and regularization, cheaply enough to consult at millions of expansions; (ii) a pipeline that turns raw LLM text into grammar-aware guidance, repairing ill-formed proposals and mining their recurring fragments into grammar extensions; and (iii) evidence across five domains and two backends that context-awareness improves over static LLM-guided synthesis and over prompting the LLM directly, and that regularization makes poor proposals safe.

References (53)

  • Syntax-guided synthesis
  • Program Synthesis with Best-First Bottom-Up Search
  • DeepCoder: Learning to Write Programs
  • HYSYNTH: Context-Free LLM Approximation for Guiding Program Synthesis
  • Just-in-Time Learning for Bottom-Up Enumerative Synthesis
  • CodeIt: Self-Improving Language Models with Prioritized Hindsight Replay
  • Evaluating Large Language Models Trained on Code
  • Teaching Large Language Models to Self-Debug
  • On the Measure of Intelligence (opens arXiv in a new tab)
  • ARC Prize 2024: Technical Report (opens arXiv in a new tab)
  • DreamCoder: bootstrapping inductive program synthesis with wake-sleep library learning
  • Program synthesis using conflict-driven learning
  • Probabilistic Programs of Thought (opens arXiv in a new tab)
  • Program Synthesis
  • Herb.jl: A Unifying Program Synthesis Library (opens arXiv in a new tab)
  • Domain-Specific Language for the Abstraction and Reasoning Corpus
  • Accelerating search-based program synthesis using learned probabilistic models
  • Combining Induction and Transduction for Abstract Reasoning (opens arXiv in a new tab)
  • Competition-level code generation with AlphaCode
  • Guiding Enumerative Program Synthesis with Large Language Models
  • ReaComp: Compiling LLM Reasoning into Symbolic Solvers for Efficient Program Synthesis (opens arXiv in a new tab)
  • BUSTLE: Bottom-Up Program Synthesis Through Learning-Guided Exploration
  • Benchmarks for SyGuS Competition
  • Grammar-Aligned Decoding
  • Self-Improving Language Models for Evolutionary Program Synthesis: A Case Study on ARC-AGI
  • Mathematical Discoveries from Program Search with Large Language Models
  • Reflexion: language agents with verbal reinforcement learning
  • Learning a Meta-Solver for Syntax-Guided Program Synthesis
  • ConceptSearch: Towards Efficient Program Search Using LLMs for Abstraction and Reasoning Corpus (ARC) (opens arXiv in a new tab)
  • Enhancing Program Synthesis with Large Language Models Using Many-Objective Grammar-Guided Genetic Programming
  • Improving LLM Code Generation with Grammar Augmentation (opens arXiv in a new tab)
  • Hypothesis Search: Inductive Reasoning with Language Models
  • Graphs, Constraints, and Search for the Abstraction and Reasoning Corpus
  • <PREVIOUS> is the last program the model returned; <FEEDBACK> reports the failure, and <ISSUE> switches framing between an ungrammatical program and a grammatical one with wrong outputs
  • No backticks, no prose
  • Here is exactly what it did:
  • This is the proposal support referred to throughout the main text, and the lower block, which counts only proposals that are syntactically valid in the target grammar, is the accuracy the search results should be compared against
  • The grammar induces the program space $\mathcal{P}_{\mathcal{G}}$, drawn so that program size grows to the right; each dot is one program
  • We report the number (and share) of tasks for which at least one of the first $k$ proposals satisfies all examples; “all” uses every available proposal
  • We fix both by small sweeps on SLIA and reuse the chosen values unchanged in every other domain, so the numbers reported throughout the paper use a single, domain-independent configuration
  • Note that the reuse-only variant is still not equivalent to the static heuristic: it also mines the recurring fragments into the grammar and scores those macro-rules, which a rule-frequency prior does not do
  • Summarizing such a curve by its endpoint alone discards exactly what we care about, namely how early the tasks are solved, so we also report the area under the curve
  • the right-hand endpoint of the curve
  • the average share of the benchmark solved over the log-budget
  • The enumeration axis for the SLIA LLM comparison is omitted here, since it is reproduced as Table 1 in the main text
  • With the same backend, NARCISSUS dominates the static prior at every budget on both axes, and reaches solutions orders of magnitude earlier than the static heuristic
  • GPT-4o vs. DeepSeek proposals, by programs enumerated (left) and wall-clock time (right)
  • NARCISSUS stays above unguided enumeration, while both static-prior variants collapse far below it, because they effectively prune the rules the weak proposals never mention
  • programs enumerated (left) and wall-clock time (right)
  • This is the weakest-support setting in the paper
  • LLM comparison (GPT-4o vs. DeepSeek) : Time axis AUC (solving time, 0.03s to 300s (timeout)), out of 70 problems
  • LLM comparison (DeepSeek vs. Haiku) : Enumeration axis AUC (programs enumerated, 1 to $10^6$), out of 100 problems
  • LLM comparison (DeepSeek vs. Haiku) : Time axis AUC (solving time, 0.03s to 300s (timeout)), out of 100 problems