forked from fredfeng/Trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_smt_enumerator.py
executable file
·106 lines (85 loc) · 2.91 KB
/
demo_smt_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
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
#!/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')
toy_spec_str = '''
enum SmallInt {
"-1", "-2", "0", "1", "2"
}
value Int {
is_positive: bool;
}
value Empty;
program Toy(Int, Int) -> Int;
func const: Int -> SmallInt;
func sqrt_const: Int -> SmallInt;
func plus: Int -> Int, Int;
func minus: Int -> Int, Int;
func mult: Int r -> Int a, Int b {
is_positive(a) && is_positive(b) ==> is_positive(r);
is_positive(a) && !is_positive(b) ==> !is_positive(r);
!is_positive(a) && is_positive(b) ==> !is_positive(r);
}
func empty: Empty -> Empty;
# You can use any number larger than 0 as weight (does not need to be bounded at 100)
# predicate occurs(minus, 80);
predicate occurs(mult, 10);
predicate is_parent(minus, mult, 99);
predicate is_not_parent(mult, minus, 500);
# Since the program we want is mult(@param1, minus(@param0, @param1))
# The following 2 constraints would find that program very quickly
# predicate occurs(mult, 999);
# predicate is_parent(mult, minus, 999);
'''
class ToyInterpreter(PostOrderInterpreter):
def eval_SmallInt(self, v):
return int(v)
def eval_const(self, node, args):
return args[0]
def eval_sqrt_const(self, node, args):
self.assertArg(node, args,
index=0,
cond=lambda x: x >= 0,
capture_indices=[])
return int(args[0] ** 0.5)
def eval_plus(self, node, args):
return args[0] + args[1]
def eval_minus(self, node, args):
return args[0] - args[1]
def eval_mult(self, node, args):
return args[0] * args[1]
def apply_is_positive(self, val):
return val > 0
def main():
logger.info('Parsing Spec...')
spec = S.parse(toy_spec_str)
logger.info('Parsing succeeded')
logger.info('Building synthesizer...')
synthesizer = Synthesizer(
enumerator=SmtEnumerator(spec, depth=3, loc=2),
decider=ExampleConstraintDecider(
spec=spec,
interpreter=ToyInterpreter(),
examples=[
# we want to synthesize the program (x-y)*y (depth=3, loc=2)
# which is also equivalent to x*y-y*y (depth=3, loc=3)
Example(input=[4, 3], output=3),
Example(input=[6, 3], output=9),
Example(input=[1, 2], output=-2),
Example(input=[1, 1], output=0),
]
)
)
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()