Inferensys

Glossary

Sketch-Based Synthesis

Sketch-based synthesis is a program synthesis technique where a user provides a partial program (a sketch) with holes, and an AI synthesizer automatically fills these holes with code fragments to satisfy a given specification.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
PROGRAM SYNTHESIS

What is Sketch-Based Synthesis?

A technique for automatically generating executable code by completing a user-provided partial template.

Sketch-based synthesis is a program synthesis technique where a developer provides a high-level program outline, or sketch, containing intentional gaps ("holes") to be filled automatically. The synthesizer's task is to search for code fragments that, when inserted into these holes, produce a complete program that satisfies a formal specification, such as input-output examples or logical constraints. This approach balances user control with automation, guiding the search within a constrained space defined by the sketch's structure.

The technique is foundational for building reliable autonomous agents and automated systems, as it translates partial intent into verifiably correct code. It is closely related to Syntax-Guided Synthesis (SyGuS) and often employs Satisfiability Modulo Theories (SMT) solvers or neurosymbolic methods to efficiently explore the solution space. By requiring a sketch, it reduces the combinatorial explosion inherent in synthesis from scratch, making it practical for generating complex, bug-free routines for data transformation, API orchestration, or hierarchical task network execution.

SKETCH-BASED SYNTHESIS

Key Components of a Sketch

A sketch is a partial program template that defines the program's high-level structure while leaving intentional gaps, or 'holes', for the synthesizer to fill. This breakdown details its core elements.

01

Program Skeleton

The program skeleton is the concrete, user-written code that forms the fixed structure of the sketch. It defines control flow (e.g., loops, conditionals), function signatures, and data types. This skeleton provides critical constraints that drastically reduce the search space for the synthesizer, guiding it towards a syntactically and semantically plausible solution. For example, a skeleton for a sorting function would explicitly define the function name, parameters, and a loop structure, leaving only the comparison and swap logic as holes.

02

Holes (Placeholders)

Holes (denoted by ?? or _ in tools like Sketch) are placeholders within the skeleton where code fragments must be synthesized. Each hole has an associated type (e.g., integer, boolean, list) and sometimes a grammar defining the set of allowed expressions. The synthesizer's core task is to find expressions of the correct type to fill these holes such that the complete program satisfies the specification. Holes can represent:

  • Single expressions (e.g., a boolean condition ?? in an if statement).
  • Sequences of statements.
  • Entire function bodies.
03

Specification

The specification is the formal or semi-formal description of the program's required behavior. It acts as the correctness criterion for the synthesized code filling the holes. In sketch-based synthesis, specifications are often provided as:

  • Assertions and Pre/Post-conditions: Logical constraints within the skeleton (e.g., assert(sorted(arr))).
  • Reference Implementrations (oracles): A simpler, possibly inefficient, version of the function that defines correct behavior.
  • Input-Output Examples: A set of concrete test cases the completed program must pass.
  • Temporal Logic Formulas: For reactive systems, properties expressed in logics like LTL.
04

Grammar/Search Space Constraints

This component explicitly defines the set of possible code fragments that can be used to fill each hole. It is often specified via a context-free grammar or a set of component functions. For example, a hole for an integer expression might be constrained to the grammar: Expr := Constant | Variable | Expr + Expr | Expr * Expr. This constraint is critical for tractability; without it, the search space would be infinite. The grammar encodes domain knowledge, ensuring the synthesizer only explores relevant, efficient, or safe constructs.

05

Synthesis Engine & Solver

The synthesis engine is the algorithmic core that searches for valid hole completions. It typically reduces the problem to a constraint satisfaction problem. The engine:

  1. Encodes the skeleton, hole grammars, and specification into a system of logical constraints.
  2. Uses a Satisfiability Modulo Theories (SMT) solver like Z3 to find a satisfying assignment for the holes.
  3. Translates the solver's model back into concrete source code. This process often occurs within a Counterexample-Guided Inductive Synthesis (CEGIS) loop, where counterexamples from failed verification refine the constraints iteratively.
06

Related Concept: Syntax-Guided Synthesis (SyGuS)

Syntax-Guided Synthesis (SyGuS) is the standardized formal framework that underlies most modern sketch-based synthesis tools. It explicitly separates the three key components:

  • A grammar defining the search space (the 'Syntax' guide).
  • A logical specification (typically in first-order logic).
  • The synthesis engine that solves the SyGuS problem. Sketch-based synthesis is a powerful instantiation of the SyGuS paradigm, where the user-provided skeleton and holes implicitly define a tailored grammar for the problem at hand. The SyGuS format enables competition and innovation among different solver backends.
PROGRAM SYNTHESIS

How Sketch-Based Synthesis Works

A technical overview of the synthesis paradigm where a partial program template guides the automated search for a complete, correct implementation.

Sketch-based synthesis is a program synthesis technique where a developer provides a high-level program sketch—a partial template with intentional gaps called holes—and a formal specification. The synthesizer's role is to search the constrained space of possible code fragments to fill these holes, producing a complete program that provably satisfies the given specification. This approach balances user guidance with automated search, making synthesis tractable for complex problems by reducing the combinatorial search space.

The core technical mechanism involves encoding the sketch, specification, and a background theory (defining operations and their semantics) into logical constraints for a Satisfiability Modulo Theories (SMT) solver. The synthesizer queries the solver to find concrete expressions that, when placed into the sketch's holes, yield a valid program. Modern implementations often use a Counterexample-Guided Inductive Synthesis (CEGIS) loop, where candidate solutions are generated, checked against the specification, and refined using counterexamples from failed verification attempts.

SKETCH-BASED SYNTHESIS

Frequently Asked Questions

Sketch-based synthesis is a program synthesis technique where the user provides a partial program (a sketch) with holes, and the synthesizer automatically fills these holes with code fragments to satisfy a given specification. This FAQ addresses common technical questions about its mechanisms, applications, and relationship to other synthesis paradigms.

Sketch-based synthesis is a program synthesis technique where a developer writes a partial program template, called a sketch, containing intentional gaps or "holes" (denoted by ??), and an automated synthesizer fills these holes with concrete code fragments to produce a complete, correct program. The process works by combining a sketch with a formal specification (e.g., input-output examples, logical assertions, or a reference implementation). The synthesizer treats the holes as unknown constants or expressions and uses a constraint solver, typically a Satisfiability Modulo Theories (SMT) solver like Z3, to search for values that satisfy the specification. This transforms an open-ended search problem into a more constrained, and therefore more tractable, constraint satisfaction problem.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.