Sketch¶
Program sketches with holes for synthesis.
Overview¶
Program sketches define the structure of code to be synthesized, with "holes" (??) to be filled by the synthesizer.
Class Documentation¶
upir.synthesis.sketch.ProgramSketch
dataclass
¶
A program sketch - partial program with holes to be filled by synthesis.
Per CEGIS methodology, a sketch is a template program with "holes" representing unknown parts. The synthesis engine fills these holes to produce a complete program that satisfies the specification.
Template uses HOLE_{id} markers that get replaced during instantiation.
Attributes:
| Name | Type | Description |
|---|---|---|
template |
str
|
Code template with HOLE_{id} markers |
holes |
List[Hole]
|
List of holes to fill |
language |
str
|
Programming language (e.g., "python", "java") |
framework |
str
|
Framework/platform (e.g., "Apache Beam", "Spark") |
constraints |
List[Any]
|
Global constraints across multiple holes |
Example
template = ''' ... def process(data): ... windowed = data.window( ... window_size=HOLE_h1 ... ) ... return windowed.batch( ... batch_size=HOLE_h2 ... ) ... ''' sketch = ProgramSketch( ... template=template, ... holes=[ ... Hole(id="h1", name="window_size", hole_type="value", ... constraints=[("range", 1, 60)]), ... Hole(id="h2", name="batch_size", hole_type="value", ... constraints=[("range", 1, 1000)]) ... ], ... language="python", ... framework="Apache Beam" ... ) unfilled = sketch.get_unfilled_holes() len(unfilled) 2 sketch.fill_hole("h1", 10) True sketch.fill_hole("h2", 100) True code = sketch.instantiate() "window_size=10" in code True
References: - CEGIS: Program sketch definition - TD Commons: Sketch-based synthesis
Source code in upir/synthesis/sketch.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
Functions¶
__post_init__()
¶
Validate program sketch configuration.
Source code in upir/synthesis/sketch.py
get_unfilled_holes()
¶
Get list of holes that have not been filled yet.
Returns:
| Type | Description |
|---|---|
List[Hole]
|
List of unfilled Hole objects |
Example
sketch = ProgramSketch( ... template="x = HOLE_h1 + HOLE_h2", ... holes=[ ... Hole(id="h1", name="a", hole_type="value"), ... Hole(id="h2", name="b", hole_type="value") ... ] ... ) len(sketch.get_unfilled_holes()) 2 sketch.fill_hole("h1", 10) True len(sketch.get_unfilled_holes()) 1
Source code in upir/synthesis/sketch.py
fill_hole(hole_id, value)
¶
Fill a hole with a specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hole_id
|
str
|
ID of the hole to fill |
required |
value
|
Any
|
Value to fill the hole with |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if hole was filled successfully, False if hole not found |
Example
sketch = ProgramSketch( ... template="size = HOLE_h1", ... holes=[Hole(id="h1", name="size", hole_type="value")] ... ) sketch.fill_hole("h1", 42) True sketch.fill_hole("nonexistent", 10) False
Source code in upir/synthesis/sketch.py
instantiate()
¶
Instantiate the sketch by replacing all holes with their filled values.
Replaces all HOLE_{id} markers in the template with the corresponding filled values. Handles type conversion (bool to "True"/"False", etc.).
Returns:
| Type | Description |
|---|---|
str
|
Complete code with all holes filled |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any holes are unfilled |
Example
template = "result = HOLE_h1 + HOLE_h2" sketch = ProgramSketch( ... template=template, ... holes=[ ... Hole(id="h1", name="a", hole_type="value"), ... Hole(id="h2", name="b", hole_type="value") ... ] ... ) sketch.fill_hole("h1", 10) True sketch.fill_hole("h2", 20) True code = sketch.instantiate() code 'result = 10 + 20'
References: - CEGIS: Sketch instantiation - String replacement for code generation
Source code in upir/synthesis/sketch.py
to_dict()
¶
Serialize program sketch to dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with all sketch fields |
Example
sketch = ProgramSketch( ... template="x = HOLE_h1", ... holes=[Hole(id="h1", name="x", hole_type="value")], ... language="python" ... ) d = sketch.to_dict() d["language"] 'python'
Source code in upir/synthesis/sketch.py
from_dict(data)
classmethod
¶
Deserialize program sketch from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary containing sketch fields |
required |
Returns:
| Type | Description |
|---|---|
ProgramSketch
|
ProgramSketch instance |
Example
data = { ... "template": "x = HOLE_h1", ... "holes": [{ ... "id": "h1", ... "name": "x", ... "hole_type": "value", ... "constraints": [], ... "possible_values": None, ... "filled_value": None, ... "location": None ... }], ... "language": "python", ... "framework": "", ... "constraints": [] ... } sketch = ProgramSketch.from_dict(data) sketch.language 'python'
Source code in upir/synthesis/sketch.py
__str__()
¶
Human-readable representation.
Source code in upir/synthesis/sketch.py
__repr__()
¶
Developer-friendly representation.
upir.synthesis.sketch.Hole
dataclass
¶
A hole in a program sketch that needs to be filled by synthesis.
Per CEGIS methodology, holes represent unknown parts of a program that the synthesis engine must determine. Each hole has a type (value, expression, predicate, function) and constraints on valid values.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for this hole |
name |
str
|
Descriptive name (e.g., "window_size", "batch_count") |
hole_type |
str
|
Type of hole - "value", "expression", "predicate", "function" |
constraints |
List[Any]
|
List of constraints on valid values Format: [("range", min, max), ("oneof", [v1, v2, v3]), ...] |
possible_values |
Optional[List[Any]]
|
Optional explicit list of possible values |
filled_value |
Optional[Any]
|
The value this hole has been filled with (if any) |
location |
Optional[Dict[str, Any]]
|
Optional location info (line number, context) |
Example
Integer value hole with range constraint¶
hole = Hole( ... id="h1", ... name="window_size", ... hole_type="value", ... constraints=[("range", 1, 100)], ... location={"line": 42, "context": "GroupByKey"} ... ) hole.is_filled() False hole.filled_value = 10 hole.is_filled() True
References: - CEGIS: Holes are unknowns to be synthesized - TD Commons: Program sketch representation
Source code in upir/synthesis/sketch.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
Functions¶
__post_init__()
¶
Validate hole configuration.
Source code in upir/synthesis/sketch.py
is_filled()
¶
Check if this hole has been filled with a value.
Returns:
| Type | Description |
|---|---|
bool
|
True if hole has a filled_value, False otherwise |
Example
hole = Hole(id="h1", name="size", hole_type="value") hole.is_filled() False hole.filled_value = 42 hole.is_filled() True
Source code in upir/synthesis/sketch.py
to_z3_var()
¶
Convert hole to Z3 variable for SMT-based synthesis.
Creates appropriate Z3 variable based on hole type: - "value": Integer variable (z3.Int) - "expression": Real variable (z3.Real) - "predicate": Boolean variable (z3.Bool) - "function": Returns None (requires special handling)
Returns:
| Type | Description |
|---|---|
Any
|
Z3 variable or None for function holes |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If Z3 is not available |
Example
hole = Hole(id="h1", name="count", hole_type="value") var = hole.to_z3_var()
var is z3.Int("hole_h1")¶
References: - Z3 API: Variable creation - CEGIS: Encoding unknowns as SMT variables
Source code in upir/synthesis/sketch.py
get_constraints_as_z3()
¶
Convert hole constraints to Z3 constraints.
Translates constraint specifications into Z3 constraint expressions that can be added to the solver.
Returns:
| Type | Description |
|---|---|
List[Any]
|
List of Z3 constraint expressions |
Example
hole = Hole( ... id="h1", ... name="size", ... hole_type="value", ... constraints=[("range", 1, 100)] ... ) z3_constraints = hole.get_constraints_as_z3()
z3_constraints[0] is (hole_h1 >= 1)¶
z3_constraints[1] is (hole_h1 <= 100)¶
References: - Z3: Constraint construction - CEGIS: Encoding hole constraints
Source code in upir/synthesis/sketch.py
__str__()
¶
Human-readable representation.
Source code in upir/synthesis/sketch.py
See Also¶
- CEGIS - Synthesize code from sketches