cl-ds / src /cl_macros /synth /variation_generator.py
j14i's picture
977 CL macro transformation examples: CL-native pipeline with SBCL verification
d69fc90 verified
"""Generate training variations from verified macros."""
from __future__ import annotations
import random
from cl_macros.schema import TransformationExample
class VariationGenerator:
def generate_from_verified(
self,
verified: TransformationExample,
max_variations: int = 8,
) -> list[TransformationExample]:
"""Create variations using different call sites as input."""
variations = []
for i, call_site in enumerate(verified.call_sites[:max_variations]):
var = TransformationExample(
id=f"{verified.id}-var-{i}",
before_code=call_site,
problem_pattern=verified.problem_pattern,
macro_definition=verified.macro_definition,
after_expansion=verified.macroexpand_1_result or verified.after_expansion,
macro_category=verified.macro_category,
technique=verified.technique,
source=verified.source,
complexity=verified.complexity,
has_capture_risk=verified.has_capture_risk,
requires_gensyms=verified.requires_gensyms,
library_name=verified.library_name,
macro_name=verified.macro_name,
is_verified=verified.is_verified,
is_synthetic=True,
macroexpand_1_result=verified.macroexpand_1_result,
commentary=verified.commentary,
)
variations.append(var)
return variations
def generate_multi_example(
self,
verified: TransformationExample,
n_examples: int = 3,
) -> list[TransformationExample]:
"""Generate macro-from-spec records using multiple call sites."""
if len(verified.call_sites) < n_examples:
return self.generate_from_verified(verified, max_variations=1)
selected = random.sample(verified.call_sites, n_examples)
combined_input = "\n\n".join(
f"Example {i+1}:\nCall: {cs}"
for i, cs in enumerate(selected)
)
spec = TransformationExample(
id=f"{verified.id}-spec",
before_code=combined_input,
problem_pattern=f"Macro that implements: {verified.nl_description or verified.problem_pattern}",
macro_definition=verified.macro_definition,
after_expansion=verified.macroexpand_1_result or verified.after_expansion,
macro_category=verified.macro_category,
technique=verified.technique,
source=verified.source,
complexity=verified.complexity,
has_capture_risk=verified.has_capture_risk,
requires_gensyms=verified.requires_gensyms,
library_name=verified.library_name,
macro_name=verified.macro_name,
formulation="macro-from-spec",
is_synthetic=True,
nl_description=verified.nl_description,
)
return [spec]