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!')
def main(seed=None): logger.info('Parsing Spec...') spec = S.parse(toy_spec_str) logger.info('Parsing succeeded') logger.info('Building synthesizer...') synthesizer = Synthesizer( # RandomEnumerator samples ASTs with depths of [0, max_depth) enumerator=RandomEnumerator(spec, max_depth=1, seed=seed), # RelaxedRandomEnumerator will sample ASTs with depths of [min_depth, max_depth] # enumerator=RelaxedRandomEnumerator( # spec, max_depth=2, min_depth=0, seed=seed), decider=ExampleDecider( 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!')
def main(sol_file): seed = None # assert False logger.info('Analyzing Input...') deps, refs = analyze(sol_file, "C", "foo()") lambdas = analyze_lambdas(sol_file, "C", "foo()") logger.info('Analysis Successful!') # print(deps.dependencies) # print(refs.pprint_refinement()) actual_spec, prog_decl, types, i_global, global_vars = instantiate_dsl( sol_file, refs.types, lambdas) # print(actual_spec) logger.info('Parsing Spec...') spec = S.parse(actual_spec) logger.info('Parsing succeeded') # Fetch other contract names slither = Slither(sol_file) other_contracts = list( filter(lambda x: x != 'C', map(str, slither.contracts))) logger.info('Building synthesizer...') synthesizer = Synthesizer( enumerator=DependencyEnumerator(spec, max_depth=4, seed=seed, analysis=deps.dependencies, types=types), decider=SymdiffDecider(interpreter=SymDiffInterpreter( prog_decl, other_contracts, i_global, global_vars), example=sol_file, equal_output=check_eq)) logger.info('Synthesizing programs...') prog = synthesizer.synthesize() if prog is not None: logger.info('Solution found: {}'.format(prog)) return True else: logger.info('Solution not found!') return False
def main(): logger.info('Parsing Spec...') spec = S.parse(toy_spec_str) logger.info('Parsing succeeded') logger.info('Building sample program...') prog = D.Builder(spec).from_sexp_string(toy_dsl_sexp) logger.info('Build program = {}'.format(prog)) interpreter = ToyInterpreter() logger.info('Executing program on inputs {}...'.format(input0)) out_value = execute(interpreter, prog, input0) logger.info('Execution finished with output = {}'.format(out_value)) assert out_value == toy_dsl_func(input0) logger.info('Executing program on inputs {}...'.format(input1)) out_value = execute(interpreter, prog, input1) logger.info('Execution finished with output = {}'.format(out_value)) assert out_value == toy_dsl_func(input1)
def __init__(self, spec_file, program_type=None, const_string=None, const_int=None, const_double=None, columns=None): with open(spec_file) as f: template = Template(f.read()) column_type = None if columns: column_type = ", ".join( [f"{t} c{i+1}" for (i, t) in enumerate(columns)]) spec = template.render(program_type=program_type, const_string=const_string, const_int=const_int, const_double=const_double, columns=column_type) # print(spec) self.spectext = spec self.spec = parse(spec) self.decider = None
def main(): logger.info('Parsing Spec...') spec = S.parse(toy_spec_str) logger.info('Parsing succeeded') logger.info('Building synthesizer...') # loc = 6 loc = 3 enumerator = SmtEnumerator( spec, depth=loc + 1, loc=loc) if argv[1] == "smt" else LinesEnumerator( spec, depth=loc + 1, loc=loc) synthesizer = Synthesizer( # enumerator=LinesEnumerator(spec, depth=loc+1, loc=loc), enumerator=enumerator, # enumerator=LinesEnumerator(spec, depth=4, loc=3), # enumerator=SmtEnumerator(spec, depth=4, loc=3), 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=[3, 2], output=5), # loc 4 # Example(input=[3, 2], output=6), # loc 3 res: 118 # Example(input=[3], output=5), # loc 4 res: 190 # Example(input=[3, 2, 1], output=6), # loc 4 Example(input=[4, 2], output=1), # loc 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!')
def main(seed=None): global getProgram, final_program if not debug: sys.stderr = open(dir+'output.err', 'w+') # os.close(sys.stderr.fileno()) warnings.filterwarnings("ignore", category=RRuntimeWarning) warnings.filterwarnings('ignore') logger.info('Parsing Spec...') dsl, input_tables, prog_out, loc = DSL() # print(dsl) spec = S.parse(dsl) logger.info('Parsing succeeded') # loc += 1 #select # logger.info("Lines of Code: "+str(loc)) logger.info('Building synthesizer...') loc = 1 while (True): logger.info("Lines of Code: "+str(loc)) if argv[1]=="tree": enumerator = SmtEnumerator(spec, depth=loc+1, loc=loc) else: if "-off" in argv: enumerator = LinesEnumerator(spec, depth=loc+1, loc=loc) elif "-on" in argv: enumerator = LinesEnumerator(spec, depth=loc+1, loc=loc, break_sym_online=True) else: enumerator = LinesEnumerator(spec, depth=loc+1, loc=loc, sym_breaker=False) synthesizer = Synthesizer( #loc: # of function productions enumerator=enumerator, # decider=ExampleConstraintDecider( decider=ExampleConstraintPruningDecider( spec=spec, interpreter=SquaresInterpreter(), examples=[ Example(input=input_tables, output='expected_output'), ], equal_output=eq_r ) ) logger.info('Synthesizing programs...') prog = synthesizer.synthesize() if prog is not None: logger.info('Solution found: {}'.format(prog)) # print(prog_out+"select("+str(prog).replace("@param", "table")+","+output_attrs+")") # print(prog_out+str(prog).replace("@param", "table")) getProgram = True interpreter=SquaresInterpreter() evaluation = interpreter.eval(prog, input_tables) if dir == "./": print() if "-nr" not in argv: print("------------------------------------- R Solution ---------------------------------------\n") print(prog_out) print(final_program) print();print() print("+++++++++++++++++++++++++++++++++++++ SQL Solution +++++++++++++++++++++++++++++++++++++\n") robjects.r('{rscript}'.format(rscript=prog_out+final_program)) sql_query = robjects.r('sql_render({result_table})'.format(result_table=evaluation)) if dir == "./": print(beautifier(str(sql_query)[6:])) print() return final_program,beautifier(str(sql_query)[6:]) else: logger.info('No more queries to be tested. Solution not found!') logger.info('Increasing the number of lines of code.') loc = loc + 1
spec_str = ''' enum BoolLit { "false", "true" } value BoolExpr; program Bool(BoolExpr, BoolExpr) -> BoolExpr; func const: BoolExpr -> BoolLit; func and: BoolExpr -> BoolExpr, BoolExpr; func or: BoolExpr -> BoolExpr, BoolExpr; func not: BoolExpr -> BoolExpr; func assertTrue: BoolExpr -> BoolExpr; ''' spec = S.parse(spec_str) class TestSimpleInterpreter(unittest.TestCase): def setUp(self): self._builder = D.Builder(spec) self._interp = BoolInterpreter() self._domain = [False, True] def test_interpreter0(self): b = self._builder p0 = b.make_param(0) p1 = b.make_param(1) p = b.make_apply('and', [p0, p1]) for x, y in product(self._domain, self._domain):