Exemplo n.º 1
0
def _filter_to_intro_vars(clauses, intro_vars):
    only_intro_var_clauses = []
    for clause in clauses:
        new_clause = []
        for disjunct in clause:
            variables = exprs.get_all_variables(disjunct)
            for v in variables:
                if v not in intro_vars:
                    break
            else:
                new_clause.append(simplify_inequality(disjunct))
        only_intro_var_clauses.append(new_clause)
    return only_intro_var_clauses
Exemplo n.º 2
0
def get_pbe_valuations(constraints, synth_fun):
    valuations = []
    for constraint in constraints:
        if not exprs.is_application_of(constraint, 'eq') and \
                not exprs.is_application_of(constraint, '='):
            return None
        if len(exprs.get_all_variables(constraint)) > 0:
            return None
        arg_func, arg_other = None, None
        for a in constraint.children:
            if exprs.is_application_of(a, synth_fun):
                arg_func = a
            else:
                arg_other = a
        if arg_func is None or arg_other is None:
            return None
        valuations.append((arg_func.children, arg_other))
    return valuations
Exemplo n.º 3
0
    def decompose(self, macro_instantiator):
        start_nt = self.start
        reverse_mapping = []

        term_productions = []
        pred_productions = []
        for rewrite in self.rules[start_nt]:
            ph_vars, nts, orig_expr_template = rewrite.to_template_expr()
            ph_var_nt_map = dict(zip(ph_vars, nts))
            expr_template = macro_instantiator.instantiate_all(
                orig_expr_template)
            ifs = exprs.find_all_applications(expr_template, 'ite')

            # Either there are no ifs or it is an concrete expression
            if len(ifs) == 0 or len(nts) == 0:
                term_productions.append(rewrite)
                continue
            elif len(ifs) > 1 and ifs[0] != expr_template:
                return None

            [cond, thent, elset] = ifs[0].children
            cond_ph_vars = exprs.get_all_variables(cond) & set(ph_vars)
            then_ph_vars = exprs.get_all_variables(thent) & set(ph_vars)
            else_ph_vars = exprs.get_all_variables(elset) & set(ph_vars)
            if (len(cond_ph_vars & then_ph_vars) > 0
                    or len(cond_ph_vars & else_ph_vars) > 0
                    or len(else_ph_vars & then_ph_vars) > 0):
                return None

            if (
                    thent not in ph_vars or \
                    elset not in ph_vars or \
                    ph_var_nt_map[thent] != start_nt or \
                    ph_var_nt_map[elset] != start_nt):
                return None

            cond_rewrite = expr_template_to_rewrite(cond, ph_var_nt_map, self)

            # Make dummy function to recognize predicate
            arg_var = exprs.VariableExpression(
                exprs.VariableInfo(exprtypes.BoolType(), 'd', 0))
            dummy_macro_func = semantics_types.MacroFunction(
                'dummy_pred_id_' + str(random.randint(1, 1000000)), 1,
                (exprtypes.BoolType(), ), exprtypes.BoolType(), arg_var,
                [arg_var])
            pred_production = FunctionRewrite(dummy_macro_func, cond_rewrite)
            pred_productions.append(pred_production)

            reverse_mapping.append(
                (dummy_macro_func, cond, orig_expr_template, expr_template))

        if len(pred_productions) == 0:
            return None

        # Non-terminals
        [term_start, pred_start] = [x + start_nt for x in ['Term', 'Pred']]
        [term_nts, pred_nts
         ] = [self.non_terminals + [x] for x in [term_start, pred_start]]
        term_nts.remove(start_nt)
        pred_nts.remove(start_nt)

        # Non-terminal types
        term_nt_type, pred_nt_type = self.nt_type.copy(), self.nt_type.copy()
        term_nt_type.pop(start_nt)
        term_nt_type[term_start] = self.nt_type[start_nt]
        pred_nt_type.pop(start_nt)
        pred_nt_type[pred_start] = exprtypes.BoolType()

        # Rules
        term_rules = {}
        term_rules[term_start] = [
            rew.rename_nt(start_nt, term_start) for rew in term_productions
        ]
        for nt in self.non_terminals:
            if nt != start_nt:
                term_rules[nt] = [
                    rew.rename_nt(start_nt, term_start)
                    for rew in self.rules[nt]
                ]

        pred_rules = {}
        pred_rules[pred_start] = [
            rew.rename_nt(start_nt, term_start) for rew in pred_productions
        ]
        for nt in self.non_terminals:
            if nt != start_nt:
                pred_rules[nt] = [
                    rew.rename_nt(start_nt, term_start)
                    for rew in self.rules[nt]
                ]
        # pred_rules[term_start] = term_rules[term_start]
        # pred_nt_type[term_start] = term_nt_type[term_start]
        pred_rules = {**term_rules, **pred_rules}
        pred_nt_type = {**term_nt_type, **pred_nt_type}
        term_grammar = Grammar(term_nts, term_nt_type, term_rules, term_start)
        pred_grammar = Grammar(pred_nts + [term_start], pred_nt_type,
                               pred_rules, pred_start)
        # print(pred_grammar)
        return term_grammar, pred_grammar, reverse_mapping
Exemplo n.º 4
0
def dt_rewrite_boolean_combs(syn_ctx, sol, synth_fun):
    orig_sol = sol
    smt_ctx = z3smt.Z3SMTContext()
    vs = exprs.get_all_variables(sol)
    dummy_vars = [
        exprs.VariableExpression(
            exprs.VariableInfo(v.variable_info.variable_type,
                               "D" + v.variable_info.variable_name, i))
        for (i, v) in enumerate(vs)
    ]
    argvars = [
        semantics.semantics_types.expression_to_smt(v, smt_ctx)
        for v in dummy_vars
    ]
    sol = exprs.substitute_all(sol, list(zip(vs, dummy_vars)))
    preds = get_atomic_preds(sol)
    terms = get_terms(sol)

    points = []

    from exprs import evaluation
    eval_ctx = evaluation.EvaluationContext()

    def add_point(point, pred_sig_list, term_sig_list):
        points.append(point)
        eval_ctx.set_valuation_map(point)
        solv = evaluation.evaluate_expression_raw(sol, eval_ctx)
        new_pred_sig_list = [
            utils.bitset_extend(
                sig, evaluation.evaluate_expression_raw(pred, eval_ctx))
            for (sig, pred) in zip(pred_sig_list, preds)
        ]
        new_term_sig_list = [
            utils.bitset_extend(
                sig,
                solv == evaluation.evaluate_expression_raw(term, eval_ctx))
            for (sig, term) in zip(term_sig_list, terms)
        ]
        return (new_pred_sig_list, new_term_sig_list)

    pred_sig_list = [BitSet(0) for p in preds]
    term_sig_list = [BitSet(0) for t in terms]

    expr = terms[0]
    fsol = None
    while True:
        z3point = exprs.sample(syn_ctx.make_function_expr('ne', expr, sol),
                               smt_ctx, argvars)
        if z3point is None:
            fsol = expr
            break
        else:
            point = list(
                map(lambda v, d: z3smt.z3value_to_value(v, d.variable_info),
                    z3point, dummy_vars))
            (pred_sig_list, term_sig_list) = add_point(point, pred_sig_list,
                                                       term_sig_list)
            dt = eusolver.eus_learn_decision_tree_for_ml_data(
                pred_sig_list, term_sig_list)
            expr = verifiers.naive_dt_to_expr(syn_ctx, dt, preds, terms)
    sol = exprs.substitute_all(fsol, list(zip(dummy_vars, vs)))
    return sol