コード例 #1
0
def std_unification_solver(theory,
                           syn_ctx,
                           synth_funs,
                           grammar_map,
                           specification,
                           verifier,
                           phog_file,
                           options=options):
    if len(synth_funs) > 1:
        raise UnsuitableSolverException(
            "DT Unification Solver: Multi-function unification not supported")
    if specification.is_multipoint:
        raise UnsuitableSolverException("Multi point specification")

    synth_fun = synth_funs[0]
    grammar = grammar_map[synth_fun]

    decomposed_grammar = grammar.decompose(syn_ctx.macro_instantiator)
    if decomposed_grammar == None:
        raise UnsuitableSolverException(
            "DT Unification Solver: Unable to decompose grammar")
    term_grammar, pred_grammar, reverse_mapping = decomposed_grammar
    generator_factory = enumerators.PointDistinctGeneratorFactory(
        specification)
    term_generator = term_grammar.to_generator(generator_factory)
    pred_generator = pred_grammar.to_generator(generator_factory)
    solver = solvers.Solver(syn_ctx)
    term_phog = SPhog(term_grammar, phog_file, synth_fun.range_type, specification, for_eusolver=True, for_pred_exprs=False) if options.use_sphog() else \
        Phog(term_grammar, phog_file, synth_fun.range_type, for_eusolver=True, for_pred_exprs=False)
    pred_phog = SPhog(pred_grammar, phog_file, synth_fun.range_type, specification, for_eusolver=True, for_pred_exprs=True, ref_grammar=term_grammar) if options.use_sphog() else \
        Phog(pred_grammar, phog_file, synth_fun.range_type, for_eusolver=True, for_pred_exprs=True)

    if term_phog.stat_map is None:
        print(
            'No model available for this problem. We use the classic EUSolver ...'
        )
        term_phog = None
        pred_phog = None
    if pred_phog is not None and pred_phog.stat_map is None:
        pred_phog = None

    term_solver = termsolvers.PointDistinctTermSolver(
        specification.term_signature, term_generator, stat_model=term_phog)
    unifier = unifiers.PointDistinctDTUnifier(pred_generator,
                                              term_solver,
                                              synth_fun,
                                              syn_ctx,
                                              stat_model=pred_phog)
    solver = solvers.Solver(syn_ctx)
    solutions = solver.solve(generator_factory,
                             term_solver,
                             unifier,
                             verifier,
                             verify_term_solve=True)
    solution = next(solutions)
    final_solution = rewrite_solution([synth_fun], solution, reverse_mapping)
    return final_solution
コード例 #2
0
def classic_esolver(theory, syn_ctx, synth_funs, grammar_map, specification,
                    verifier, phog_file):
    if len(synth_funs) != 1:
        raise UnsuitableSolverException(
            "Classic esolver for multi-function disable due to bugs")
    assert len(synth_funs) == 1
    try:
        generator_factory = enumerators.PointDistinctGeneratorFactory(
            specification)
    except:
        raise UnsuitableSolverException("Enumerator problems")
    TermSolver = termsolvers.PointDistinctTermSolver
    grammar = grammar_map[synth_funs[0]]
    term_generator = grammar.to_generator(generator_factory)

    # init for using PHOG
    phog = SPhog(grammar, phog_file, synth_funs[0].range_type, specification) if options.use_sphog() else \
        Phog(grammar, phog_file, synth_funs[0].range_type)
    if phog.stat_map is None:
        print(
            'No model available for this problem. We use the classic ESolver ...'
        )
        phog = None

    term_solver = TermSolver(specification.term_signature,
                             term_generator,
                             stat_model=phog)
    term_solver.stopping_condition = termsolvers.StoppingCondition.one_term_sufficiency
    unifier = unifiers.NullUnifier(None, term_solver, synth_funs, syn_ctx,
                                   specification)

    solver = solvers.Solver(syn_ctx)
    solutions = solver.solve(generator_factory,
                             term_solver,
                             unifier,
                             verifier,
                             verify_term_solve=False)
    try:
        solution = next(solutions)
    except StopIteration:
        return "NO SOLUTION"
    rewritten_solutions = rewrite_solution(synth_funs,
                                           solution,
                                           reverse_mapping=None)
    return rewritten_solutions
コード例 #3
0
ファイル: benchmarks.py プロジェクト: shraddhabarke/euphony
def print_stat(benchmark_files, phog_file):
    from os.path import basename
    for benchmark_file in benchmark_files:
        # print('loading: ', benchmark_file)
        file_sexp = parser.sexpFromFile(benchmark_file)
        benchmark_tuple = parser.extract_benchmark(file_sexp)
        (theories, syn_ctx, synth_instantiator, macro_instantiator,
         uf_instantiator, constraints, grammar_map, forall_vars_map,
         default_grammar_sfs) = benchmark_tuple
        assert len(theories) == 1
        theory = theories[0]
        specification = get_specification(file_sexp)
        synth_funs = list(synth_instantiator.get_functions().values())
        grammar = grammar_map[synth_funs[0]]

        phog = SPhog(grammar, phog_file, synth_funs[0].range_type, specification) if options.use_sphog() else \
            Phog(grammar, phog_file, synth_funs[0].range_type)

        defs, _ = parser.filter_sexp_for('define-fun', file_sexp)
        if defs is None or len(defs) == 0:
            print('cannot find a solution!')
            exit(0)
        [name, args_data, ret_type_data, interpretation] = defs[-1]

        ((arg_vars, arg_types, arg_var_map),
         return_type) = parser._process_function_defintion(
             args_data, ret_type_data)
        expr = parser.sexp_to_expr(interpretation, syn_ctx, arg_var_map)
        i = 0
        subs_pairs = []
        for (var_expr, ty) in zip(arg_vars, arg_types):
            param_expr = exprs.FormalParameterExpression(None, ty, i)
            subs_pairs.append((var_expr, param_expr))
            i += 1
        expr = exprs.substitute_all(expr, subs_pairs)

        score = phog.get_score(expr)
        print(basename(benchmark_file), ' \t', score)