Program synthesis

On this page 7

Program synthesis is the automatic construction of a program that satisfies a given specification, where the specification may be input-output examples, a formal logical property, a partial sketch or a natural-language description. In AI for coding, it is the long-standing research goal behind every tool that writes code, from classical search-based synthesizers to large language models that generate whole functions.

Unlike compilation, which translates a program that already exists, synthesis must search an enormous space of possible programs for one that meets the intent. The field therefore combines search, formal verification and machine learning, and its central difficulty is that specifications are almost always incomplete, so the synthesizer must guess what the user actually means.

How program synthesis searches for programs

Every synthesizer has three ingredients: a specification that states what the program must do, a search space defined by a language or grammar, and a search strategy that explores that space. A candidate program is accepted when a checker confirms it meets the specification, either by running tests or by formal proof.

Classical systems use enumerative search over a domain-specific language, ordered from small to large programs, with pruning of observationally equivalent candidates. Counterexample-guided inductive synthesis, known as CEGIS, alternates between proposing a candidate and asking a verifier, often an SMT solver, for an input on which it fails. Neural systems instead sample candidates from a learned distribution and filter them by execution.

The following table contrasts the main search strategies.

StrategyHow candidates are producedStrength
Enumerative searchSystematic listing by sizeFinds the smallest correct program
Constraint-based synthesisEncoding the search as SAT or SMTStrong correctness guarantees
CEGISCandidate and verifier in a loopHandles infinite input domains
Neural generationSampling from a trained modelScales to general-purpose languages
Neuro-symbolic searchModel-guided enumerationCombines learned priors with pruning
Main search strategies in program synthesis

Kinds of specifications

The form of the specification determines both how easy the task is to state and how strongly the result can be trusted. Formal specifications allow proof, while informal ones are convenient but ambiguous.

SpecificationExampleMain trade-off
Input-output examplesThree pairs of strings and resultsEasy to give, highly ambiguous
Formal logicPostcondition in first-order logicPrecise, hard for most users to write
Program sketchCode with holes to fillNarrows search, requires expertise
Reference implementationSlow program to optimizeExact, used in superoptimization
Natural languageDocstring or chat requestAccessible, open to interpretation
Specification types and their trade-offs

Programming by example is the most familiar case: spreadsheet features that infer a string transformation from a few typed examples rely on synthesis over a small domain-specific language. Ambiguity is resolved through ranking functions that prefer shorter or more general programs.

From classical synthesis to code language models

Large language models changed program synthesis by making natural-language specifications practical for general-purpose languages. Models such as AlphaCode, Code Llama and StarCoder generate many candidate solutions, then filter them with tests, an approach often called sample-and-filter.

AspectClassical synthesisNeural synthesis
Target languageSmall domain-specific languagePython, Java, C++ and others
SpecificationExamples or formal logicMostly natural language
CorrectnessVerified or tested by constructionProbable, checked by tests afterward
ScalabilityShort programs onlyFunctions and multi-file changes
Failure modeSearch timeoutPlausible but wrong code
Classical and neural program synthesis compared

Benchmarks such as MBPP, HumanEval and LiveCodeBench measure functional correctness with metrics like pass@k, the probability that at least one of k samples passes hidden tests. Agentic coding systems extend synthesis with execution feedback and self-debugging, iterating on candidates instead of producing a single answer.

Practical applications and design choices

Program synthesis is used wherever intent is easier to state than code. Practical deployments include data wrangling by example, code completion in AI-native IDEs, query generation for databases, compiler superoptimization and automated repair of failing tests.

SituationRecommended approachReason
Safety-critical routine with formal specConstraint-based synthesisResult is proven correct
Repetitive data transformationProgramming by exampleUsers only supply sample outputs
General application featureLanguage model with testsCovers broad libraries and idioms
Performance-critical instruction sequenceSuperoptimizationExhaustive search finds optimal code
Choosing a synthesis approach

A sound design pairs generation with independent verification. Generated code should pass unit tests, type checks and static analysis written or reviewed by people, because a model that both writes the code and the tests can confirm its own mistakes.

Limitations and open problems

The search space grows exponentially with program length, so classical synthesizers rarely exceed a few dozen operations. Neural methods scale further but lose guarantees, and their output can include package hallucination, insecure patterns or subtle logic errors that pass weak tests.

  • Specification gap: examples and prose never capture every requirement, so overfitting to the given cases is common.
  • Verification cost: proving correctness of realistic programs remains expensive and needs formal specifications.
  • Benchmark contamination: public test problems may appear in training data and inflate reported scores.
  • Maintainability: synthesized code can be correct yet hard for people to read and extend.

Active research addresses these gaps by combining language models with formal verifiers, generating specifications and proofs alongside code, and using interactive clarification so the system asks the user about ambiguous cases instead of guessing. Hybrid approaches that let a model propose and a symbolic checker decide are widely seen as the most promising route to trustworthy synthesized software, although no method yet guarantees correctness for large, general-purpose programs.

Put simply

Program synthesis means a computer writes a program for you. You describe the goal. You might give a few examples, a rule, or a sentence. The system then searches for code that does the job.

It is like asking a skilled assistant to build a tool from a description. The assistant may try many versions. It keeps the one that passes your checks. The better your description, the better the result.

In practice

A finance analyst receives a column of five thousand customer names written in mixed formats, such as “SMITH, john” and “Mary Jones”. She needs them as “John Smith” for a mailing system but cannot write code.

In her spreadsheet she types the corrected form for two example rows. The built-in programming-by-example feature synthesizes a short transformation program in its internal language that splits on the comma, reorders the parts and fixes capitalization. It fills the remaining rows within a second.

She reviews the results and finds one name with a middle initial handled incorrectly, so she adds a third example for that pattern. The synthesizer refines the program, every row is now correct, and a task that would have taken hours by hand is finished in a few minutes.