예제 #1
0
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!')
예제 #2
0
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!')
예제 #3
0
def test_component(component):
    def get_type(c):
        if c == "S":
            return "String"
        elif c == "B":
            return "Bool"
        elif c == "I":
            return "Int"
        else:
            raise Execption()

    def gen_program_type(S):
        types = [get_type(c) for c in S]
        return f"({', '.join(types[:-1])}) -> {types[-1]}"

    def gen_eval_program(name, S):
        return f"({name} {' '.join([f'(@param {i})' for i in range(len(S)-1)])})"

    def gen_synth_program(name, S):
        return f"{name}\\({', '.join([f'@param[0-9]' for i in range(len(S)-1)])}\\)"

    synth = SQLSynth("spec.tyrell.jinja2",
                     program_type=gen_program_type(component[1]))
    eval_p = gen_eval_program(component[0], component[1])
    synth_p = gen_synth_program(component[0], component[1])

    for (i, o) in component[2]:
        assert (synth.eval(eval_p, i) == o)

    examples = [Example(input=i, output=o) for (i, o) in component[2]]
    assert (re.match(synth_p,
                     str(synth.synthesize(depth=1, loc=1, examples=examples))))
예제 #4
0
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!')
예제 #5
0
def complete_c_couterexample(counterexample):
    repr_input(counterexample)
    ce = " ".join([f"'{ce}'" for ce in counterexample])
    LOG.debug(f"Running udf with {ce}")
    ce = os.popen(f"./udf run {ce}").read()
    ce = unhex_example(json.loads(ce))
    LOG.debug(f"Nex example {ce}")
    return Example(input=ce['input'], output=ce['output'])
예제 #6
0
def pbe_from_example_file(udf, f):
    FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
    logging.basicConfig(format=FORMAT, level=logging.DEBUG)

    examples = open(f).read().strip()
    LOG.debug(f"loaded examples: {examples}")
    examples = [json.loads(l) for l in examples.split('\n')]
    examples = [Example(input=e['input'], output=e['output']) for e in examples]
    return pbe(udf, examples)
예제 #7
0
def gen_examples_from_c_udf_prog():
    N = 1
    examples = os.popen(f"./udf gen {N}").read().strip()
    LOG.debug(f"initial examples: [{examples}]")
    if not examples:
        return []
    examples = [unhex_example(json.loads(l)) for l in examples.split('\n')]
    with open('../examples', 'w') as f:
        for e in examples:
            print(json.dumps(e), file=f)
    LOG.debug(f"{examples}")
    return [Example(input=e['input'], output=e['output']) for e in examples]
예제 #8
0
def gen_examples_from_prod():
    N = 3
    examples = os.popen(f"scala -cp .:{SCAFFOLD_JAR} cegis.product.Product gen {N}").read().strip()
    LOG.debug(f"initial examples: [{examples}]")
    if not examples:
        return []
    examples = [unhex_example(json.loads(l)) for l in examples.split('\n')]
    with open('../examples', 'w') as f:
        for e in examples:
            print(json.dumps(e), file=f)
    LOG.debug(f"{examples}")
    return [Example(input=e['input'], output=e['output']) for e in examples]
예제 #9
0
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()
예제 #10
0
def test_row():
    synth = SQLSynth("spec.tyrell.jinja2",
                     program_type="(String, Int, Bool) -> Row",
                     columns=["String", "Int", "Bool"])
    eval_p = "(row (@param 0) (@param 1) (@param 2))"
    synth_p = "row(@param0, @param1, @param2)"

    example = (["abc", 1, True], ("abc", 1, True))
    assert (synth.eval(eval_p, example[0]) == example[1])

    examples = [Example(input=example[0], output=example[1])]
    assert (synth_p == str(synth.synthesize(depth=1, loc=1,
                                            examples=examples)))
예제 #11
0
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!')
예제 #12
0
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()
예제 #13
0
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!')
예제 #14
0
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!')
예제 #15
0
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!')
예제 #16
0
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
예제 #17
0
    parser.add_argument("-col", action='append')
    parser.add_argument("-e", help='evaluate expression')
    parser.add_argument("--loc", type=int, help='number of calls')
    parser.add_argument("--depth", type=int, help='maximum of depth')
    args = parser.parse_args()

    print(args)
    if args.d:
        logging.basicConfig(level=logging.DEBUG,
                            format="[%(asctime)s] %(levelname)s %(message)s")
    examples = [eval(e) for e in args.examples]
    if args.ex:
        for ex in load_examples(args.ex):
            examples.append((ex['input'], ex['output']))
    print(examples)
    examples = [Example(input=e[0], output=e[1]) for e in examples]

    constrings = None
    if args.cs:
        constrings = ', '.join([f'"{cs}"' for cs in args.cs])
    synth = SQLSynth(f"{directory}/spec.tyrell.jinja2",
                     program_type=gen_program_type(args.signature),
                     const_string=constrings,
                     columns=args.col)

    if args.e:
        for ex in examples:
            output = synth.eval(args.e, ex.input)
            print(f"evaluated {ex.input}, return {output}, expect {ex.output}")
        sys.exit()
예제 #18
0
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 []
예제 #19
0
def make_examples(pairs):
    return list(
        Example(input=inputs, output=output) for inputs, output in pairs)
예제 #20
0
def complete_couterexample(counterexample):
    repr_input(counterexample)
    ce = " ".join([f"'{ce}'" for ce in counterexample])
    ce = os.popen(f"scala -cp .:{SCAFFOLD_JAR} cegis.product.Product run {ce}").read()
    ce = unhex_example(json.loads(ce))
    return Example(input=ce['input'], output=ce['output'])