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.
| Strategy | How candidates are produced | Strength |
|---|---|---|
| Enumerative search | Systematic listing by size | Finds the smallest correct program |
| Constraint-based synthesis | Encoding the search as SAT or SMT | Strong correctness guarantees |
| CEGIS | Candidate and verifier in a loop | Handles infinite input domains |
| Neural generation | Sampling from a trained model | Scales to general-purpose languages |
| Neuro-symbolic search | Model-guided enumeration | Combines learned priors with pruning |
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.
| Specification | Example | Main trade-off |
|---|---|---|
| Input-output examples | Three pairs of strings and results | Easy to give, highly ambiguous |
| Formal logic | Postcondition in first-order logic | Precise, hard for most users to write |
| Program sketch | Code with holes to fill | Narrows search, requires expertise |
| Reference implementation | Slow program to optimize | Exact, used in superoptimization |
| Natural language | Docstring or chat request | Accessible, open to interpretation |
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.
| Aspect | Classical synthesis | Neural synthesis |
|---|---|---|
| Target language | Small domain-specific language | Python, Java, C++ and others |
| Specification | Examples or formal logic | Mostly natural language |
| Correctness | Verified or tested by construction | Probable, checked by tests afterward |
| Scalability | Short programs only | Functions and multi-file changes |
| Failure mode | Search timeout | Plausible but wrong code |
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.
| Situation | Recommended approach | Reason |
|---|---|---|
| Safety-critical routine with formal spec | Constraint-based synthesis | Result is proven correct |
| Repetitive data transformation | Programming by example | Users only supply sample outputs |
| General application feature | Language model with tests | Covers broad libraries and idioms |
| Performance-critical instruction sequence | Superoptimization | Exhaustive search finds optimal code |
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.