-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdemo_string_enumerator.py
executable file
·52 lines (40 loc) · 1.56 KB
/
demo_string_enumerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
import tyrell.spec as S
from tyrell.interpreter import PostOrderInterpreter
from tyrell.enumerator import SmtEnumerator
from tyrell.decider import Example, ExampleConstraintDecider
from tyrell.synthesizer import Synthesizer
from tyrell.logger import get_logger
logger = get_logger('tyrell')
class ToyInterpreter(PostOrderInterpreter):
def eval_const(self, node, args):
return args[0]
def eval_plus(self, node, args):
return args[0] + args[1]
def main():
logger.info('Parsing Spec...')
spec = S.parse_file('example/simplestring.tyrell')
logger.info('Parsing succeeded')
logger.info('Building synthesizer...')
synthesizer = Synthesizer(
# enumerator=SmtEnumerator(spec, depth=3, loc=1), # plus(@param1, @param0) / plus(@param0, @param1)
enumerator=SmtEnumerator(spec, depth=4, loc=3), # plus(plus(@param0, const(_apple_)), @param1)
decider=ExampleConstraintDecider(
spec=spec,
interpreter=ToyInterpreter(),
examples=[
# Example(input=["a", "b"], output="ab"), # plus(@param0, @param1)
# Example(input=["b", "a"], output="ab"), # plus(@param1, @param0)
Example(input=["a", "b"], output="a_apple_b"),
],
)
)
logger.info('Synthesizing programs...')
prog = synthesizer.synthesize()
if prog is not None:
logger.info('Solution found: {}'.format(prog))
else:
logger.info('Solution not found!')
if __name__ == '__main__':
logger.setLevel('DEBUG')
main()