def main(): logger.info('Parsing Spec...') spec = S.parse_file('example/toy.tyrell') 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(): 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!')
def main(seed=None): logger.info('Parsing Spec...') spec = S.parse(toy_spec_str) logger.info('Parsing succeeded') logger.info('Building synthesizer...') synthesizer = Synthesizer( enumerator=RandomEnumerator(spec, max_depth=4, 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(): global number parser = argparse.ArgumentParser() parser.add_argument('-i0', '--input0', type=str) parser.add_argument('-o', '--output', type=str) parser.add_argument('-l', '--length', type=int) parser.add_argument('-d', '--depth', type=int) parser.add_argument('-t', '--tyrellpath', type=str) parser.add_argument('-m', '--plotmax', type=float) args = parser.parse_args() loc_val = args.length depth = args.depth # Input and Output must be in CSV format. input0 = args.input0 output = args.output # This is required by Ruben. init_input_tbl('input0', input0) init_tbl('output', output) build_tab('output').max_input = args.plotmax logger.info('Parsing Spec...') spec = S.parse_file(args.tyrellpath) logger.info('Parsing succeeded') logger.info('Building synthesizer...') r_interpreter = RInterpreter() synthesizer = Synthesizer( #loc: # of function productions # enumerator=SmtEnumerator(spec, depth=2, loc=1), # enumerator=SmtEnumerator(spec, depth=3, loc=2), enumerator=SmtEnumerator(spec, depth=depth, loc=loc_val, evaluator=Evaluator(r_interpreter)), decider=SmtBasicDecider( spec=spec, interpreter=r_interpreter, examples=[ # Example(input=[DataFrame2(benchmark1_input)], output=benchmark1_output), Example(input=['input0'], output='output'), ], equal_output=eq_r ) ) logger.info('Synthesizing programs...') synthesizer.set_table(Table) prog = synthesizer.synthesize() logger.info('Number of candidates: {}'.format(number)) if prog is not None: logger.info('Selected solution: {}'.format(prog)) else: logger.info('Solution not found!') sys.stderr.flush()
def synthesize(enumerator, decider): logger.info('Building synthesizer...') synthesizer = Synthesizer(enumerator=enumerator, decider=decider) logger.info('Synthesizing programs...') prog = synthesizer.synthesize() if prog is not None: logger.info('Solution found: {}'.format(prog)) return 'SAT' else: logger.info('Solution not found!') return 'UNSAT'
def main(): ##### Input-output constraint benchmark1_input = robjects.r(''' dat <- read.table(text=" round var1 var2 nam val round1 22 33 foo 0.16912201 round2 11 44 foo 0.18570826 round1 22 33 bar 0.12410581 round2 11 44 bar 0.03258235 ", header=T) dat ''') benchmark1_output = robjects.r(''' dat2 <- read.table(text=" nam val_round1 val_round2 var1_round1 var1_round2 var2_round1 var2_round2 bar 0.1241058 0.03258235 22 11 33 44 foo 0.1691220 0.18570826 22 11 33 44 ", header=T) dat2 ''') logger.info('Parsing Spec...') spec = S.parse_file('example/morpheus.tyrell') logger.info('Parsing succeeded') logger.info('Building synthesizer...') synthesizer = Synthesizer( #loc: # of function productions # enumerator=SmtEnumerator(spec, depth=2, loc=1), # enumerator=SmtEnumerator(spec, depth=3, loc=2), enumerator=SmtEnumerator(spec, depth=4, loc=3), decider=ExampleConstraintDecider( spec=spec, interpreter=MorpheusInterpreter(), examples=[ # Example(input=[DataFrame2(benchmark1_input)], output=benchmark1_output), Example(input=['dat'], output='dat2'), ], equal_output=eq_r ) ) 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): global conn conn = create_connection("squares.db") # c = conn.cursor() # benchmark1_input = "" # c.execute("select * from faculty;") # rows = c.fetchall() # for r in rows: # # print(str(r)) # benchmark1_input += str(r) # print(benchmark1_input) benchmark1_output = "" c = conn.cursor() c.execute("SELECT fid, fname FROM faculty natural join class;") rows = c.fetchall() for r in rows: benchmark1_output += str(r) # print(r) # print(benchmark1_output) logger.info('Parsing Spec...') spec = S.parse_file('example/squares.tyrell') logger.info('Parsing succeeded') logger.info('Building synthesizer...') synthesizer = Synthesizer( #loc: # of function productions # enumerator=SmtEnumerator(spec, depth=2, loc=1), # enumerator=SmtEnumerator(spec, depth=3, loc=2), enumerator=SmtEnumerator(spec, depth=4, loc=3), # enumerator=RandomEnumerator( # spec, max_depth=4, seed=seed), decider=ExampleConstraintDecider( spec=spec, interpreter=SquaresInterpreter(), examples=[ Example(input=None, output=benchmark1_output), ], equal_output=eq_r ) ) logger.info('Synthesizing programs...') prog = synthesizer.synthesize() if prog is not None: logger.info('Solution found: {}'.format(prog)) else: logger.info('Solution not found!') conn.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('-i0', '--input0', type=str) parser.add_argument('-i1', '--input1', type=str) parser.add_argument('-o', '--output', type=str) parser.add_argument('-l', '--length', type=int) args = parser.parse_args() loc_val = args.length # Input and Output must be in CSV format. input0 = args.input0 input1 = args.input1 output = args.output # This is required by Ruben. depth_val = loc_val + 1 print(input0, input1, output, loc_val) init_tbl('input0', input0) #FIXME: ignore the second input table for now. init_tbl('output', output) logger.info('Parsing Spec...') spec = S.parse_file('example/morpheus.tyrell') logger.info('Parsing succeeded') # Reading the n-gram model. sketches = [line.strip() for line in open("./ngram.txt", 'r')] logger.info('Building synthesizer...') synthesizer = Synthesizer( #loc: # of function productions enumerator=BidirectEnumerator(spec, depth=depth_val, loc=loc_val, sk_queue=sketches), decider=ExampleConstraintPruningDecider( spec=spec, interpreter=MorpheusInterpreter(), examples=[ # Example(input=[DataFrame2(benchmark1_input)], output=benchmark1_output), Example(input=['input0'], output='output'), ], equal_output=eq_r)) 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 synthesize(self, depth, loc, examples=None): global g_enumerator_cache logger.debug("creating synthesizer") start_time = time.time() if (self.spectext, depth, loc) in g_enumerator_cache: enumerator = g_enumerator_cache[(self.spectext, depth, loc)] else: enumerator = SmtEnumerator( self.spec, depth=depth, loc=loc ) # loc is the number of function calls in the synthesized program # g_enumerator_cache[(self.spectext, depth, loc)] = enumerator # print(len(g_enumerator_cache), hashlib.md5(self.spectext).hexdigest(), depth, loc) enumerator_init_time = time.time() - start_time start_time = time.time() if examples: logger.debug("creating decider in synthesize()") decider = ExampleConstraintDecider( spec=self.spec, # spec is needed for this decider interpreter=Interpreter(), examples=examples) logger.debug("decider created") else: decider = self.decider synthesizer = Synthesizer(enumerator=enumerator, decider=decider) decider_init_time = time.time() - start_time logger.debug("synthesizer created") start_synth_time = time.time() # Do synthesis res = synthesizer.synthesize() self.enumerator_init_time = enumerator_init_time self.decider_init_time = decider_init_time self.time_elapsed = time.time() - start_synth_time self.num_attempts = synthesizer.num_attempts if res: logger.debug( f"synthesized program {res}, time_elapsed: {self.time_elapsed}" ) else: logger.debug( f"synthesis failed, time_elapsed: {self.time_elapsed}") return res
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(): logger.info('Parsing Spec...') spec = S.parse_file('example/deepcoder.tyrell') logger.info('Parsing succeeded') logger.info('Building synthesizer...') synthesizer = Synthesizer( enumerator=SmtEnumerator(spec, depth=5, loc=5), decider=ExampleConstraintDecider( spec=spec, interpreter=DeepCoderInterpreter(), examples=[ Example(input=[[6, 2, 4, 7, 9], [5, 3, 6, 1, 0]], output=27), ], # equal_output=eq_deepcoder )) 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
def synthesize(inputs, output, oracle_output, prune, extra_consts): global full_table full_table = oracle_output logger.setLevel('INFO') """ synthesizer table transformation programs from input-output examples Args: inputs: a list of input tables (represented as a list of named tuples) output: a symbolic table (of class symbolic.SymTable) full_output: the oracle output table, the task would need to generalize to it extra_consts: extra constants provided to the solver Returns: a list of transformation programs s.t. p(inputs) = output """ #print("output table:\n", output) #print("input table:\n", inputs[0]) loc_val = 2 output_data = json.dumps(output.instantiate()) full_data = json.dumps(oracle_output.instantiate()) input_data = json.dumps(inputs[0], default=default) init_tbl_json_str('input0', input_data) init_tbl_json_str('output', output_data) # if prune == 'morpheus': # init_tbl_json_str('output', full_data) # else: # init_tbl_json_str('output', output_data) print(robjects.r('input0')) print(robjects.r('output')) depth_val = loc_val + 1 logger.info('Parsing spec ...') # provide additional string constants to the solver grammar_base = "dsl/tidyverse.tyrell.base" grammar_file = "dsl/__tidyverse__.tyrell" synth_utils.update_search_grammar(extra_consts, grammar_base, grammar_file) spec = S.parse_file(grammar_file) logger.info('Parsing succeeded') logger.info('Building synthesizer ...') global iter_num iter_num = 0 for loc in range(1, loc_val + 1): eq_fun = subset_eq if prune == 'none': eq_fun = proj_eq enumerator = BidirectEnumerator(spec, depth=loc + 1, loc=loc) decider = BidirectionalDecider(spec=spec, interpreter=MorpheusInterpreter(), examples=[ Example(input=['input0'], output='output'), ], prune=prune, equal_output=eq_fun) if prune == 'morpheus': enumerator = SmtEnumerator(spec, depth=loc + 1, loc=loc) decider = ExampleConstraintPruningDecider( spec=spec, interpreter=MorpheusInterpreter(), examples=[ # Example(input=[DataFrame2(benchmark1_input)], output=benchmark1_output), Example(input=['input0'], output='output'), ], equal_output=eq_fun) synthesizer = Synthesizer(enumerator=enumerator, decider=decider) logger.info('Synthesizing programs ...') prog = synthesizer.synthesize() if prog is not None: logger.info('Solution found: {}'.format(prog)) return [prog] else: logger.info('Solution not found!') return []