Skip to content

Repository files navigation

PyAstSynth : A library from python program synthesis

Installation

pip install git+https://github.com/MathisFederico/PyAstSynth

Usage

First define a Domain specific language for your application. It can be a simple list of python functions like this:

# --> example_dsl.py# Define a small DSL## ConstantsTWO=2THREE=3## Operationsdefrepeat(string: str, times: int) ->str:
returnstring*timesdefconcat(string: str, other_string: str) ->str:
returnstring+other_string

Then use astsynth to find the all successful programs on the task and print the smallest:

# --> main.pyfrompathlibimportPathfromastsynth.synthesizerimportSynthesizerfromastsynth.taskimportTaskfromastsynth.dslimportload_symbols_from_python_file# Initialize the dsldsl_path=Path("./example_dsl.py")
dsl=load_symbols_from_python_file(dsl_path)
# Make a task from a few i/o examplestask=Task.from_tuples(
[
({"input_string": "abc"}, "abcabcabc"),
({"input_string": "ab"}, "ababab"),
({"input_string": "abcd"}, "abcdabcdabcd"),
]
)
# Augment the dsl with task inputsdsl.add_task_inputs(task)
# Synthesize all programs that succeeds at the task:synthesizer=Synthesizer(dsl=dsl, task=task)
synthesis_result=synthesizer.run(max_depth=2)
print(
f"Found {synthesis_result.stats.n_successful_programs} successful programs"f"over the {synthesis_result.stats.n_generated_programs} generated programs",
f" in {synthesis_result.stats.runtime:.2E}s",
)
# Write down the smallest solution programsmallest_program=min(synthesis_result.successful_programs, key=lambdap: len(p))
print(f"\nSmallest program found:\n\n{smallest_program.source}")
print("Other successful programs found:")
forprograminsynthesis_result.successful_programs:
ifprogramissmallest_program:
continueprint("-"*30)
print(f"\n{program.source}")

This will print:

Found 5 successful programs over the 20 generated in 1.40E-01s
Smallest program found:
THREE = 3
def repeat(string: str, times: int) ->str:
return string *times
def generated_func(input_string: str):
return repeat(input_string, THREE)
Other successful programs found:
------------------------------
TWO = 2
def concat(string: str, other_string: str) ->str:
return string + other_string
def repeat(string: str, times: int) ->str:
return string *times
def generated_func(input_string: str):
x0 = repeat(input_string, TWO)
return concat(input_string, x0)
------------------------------
def concat(string: str, other_string: str) ->str:
return string + other_string
def generated_func(input_string: str):
x0 = concat(input_string, input_string)
return concat(input_string, x0)
------------------------------
TWO = 2
def concat(string: str, other_string: str) ->str:
return string + other_string
def repeat(string: str, times: int) ->str:
return string *times
def generated_func(input_string: str):
x0 = repeat(input_string, TWO)
return concat(x0, input_string)
------------------------------
def concat(string: str, other_string: str) ->str:
return string + other_string
def generated_func(input_string: str):
x0 = concat(input_string, input_string)
return concat(x0, input_string)

Contributing

Fork this repository and clone the forked one:

git clone https://github.com/<Your Username>/PyAstSynth

Navigate to the local repository:

cd path/to/local/repo

Install in editable mode with dev requirements:

pip install -e .[dev]

Install pre-commit and pre-push hooks:

pre-commit install -t pre-commit -t pre-push

Run a pre-push check (that includes tests):

pre-commit run --hook-stage pre-push

Do your modifications and push them

git push

Ensure to have a test for each bugfix / feature you have done.

Then do a pull request from your fork to the original repository with the changes, mentionning issues you are solving or features you are adding.

About

Program synthesis in python from restricted abstract syntax tree

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages