def _dummy_spec(self, synth_fun): func = semantics_types.SynthFunction( 'pred_indicator_' + str(random.randint(1, 10000000)), synth_fun.function_arity, synth_fun.domain_types, exprtypes.BoolType()) args = [ exprs.FormalParameterExpression(func, argtype, i) for i, argtype in enumerate(synth_fun.domain_types) ] indicator_expr = exprs.FunctionExpression(func, tuple(args)) eval_ctx = evaluation.EvaluationContext() def compute_indicator(term, points): eval_ctx.set_interpretation(func, term) retval = [] for point in points: eval_ctx.set_valuation_map(point) try: retval.append( evaluation.evaluate_expression_raw( indicator_expr, eval_ctx)) except (basetypes.UnboundLetVariableError, basetypes.PartialFunctionError): # Can't mess up on predicates return [False] * len(points) return retval return func, compute_indicator
def __init__(self, term_signature, spec): super().__init__() self.term_signature = term_signature self.synth_funs = spec.synth_funs self.spec = spec self.syn_ctx = self.spec.syn_ctx self.point_var_exprs = [ exprs.VariableExpression(v) for v in spec.point_vars ] self.smt_ctx = z3smt.Z3SMTContext() self.eval_ctx = evaluation.EvaluationContext() self.canon_apps = [ self.spec.canon_application[sf] for sf in self.synth_funs ] self.outvars = [] for fn in self.synth_funs: self.outvars.append( exprs.VariableExpression(exprs.VariableInfo( exprtypes.IntType(), 'outvar_' + fn.function_name, len(self.point_var_exprs) + len(self.outvars)))) self.all_vars = self.point_var_exprs + self.outvars self.all_vars_z3 = [ _expr_to_smt(v, self.smt_ctx) for v in self.all_vars ] # self.clauses = spec.get_canon_clauses() self.lia_clauses = [ [ LIAInequality.from_expr(exprs.substitute_all(disjunct, list(zip(self.canon_apps, self.outvars)))) for disjunct in clause ] for clause in spec.get_canon_clauses() ] self.rewritten_spec = exprs.substitute_all( self.spec.get_canonical_specification(), list(zip(self.canon_apps, self.outvars)))
def __init__(self, spec): super().__init__() self.points = [] self.signatures = {} self.cache = {} self.base_generators = {} self.finished_generators = {} self.eval_ctx = evaluation.EvaluationContext() self.cache_sizes = [] self.all_caches = [] if spec.is_multipoint: assert len(spec.synth_funs) == 1 self.applications = spec.get_applications()[spec.synth_funs[0]] for app in self.applications: for child in app.children: if exprs.find_application( child, spec.synth_funs[0].function_name) is not None: raise Exception( "Unable to form point out of forall variables") self.point_profiles = [] else: self.applications = None self.point_profiles = None
def fetchop_func_pbe(specification, grammar, expr): if (exprs.is_constant_expression(expr)): if specification.theory == 'SLIA': eval_context = evaluation.EvaluationContext() value = evaluation.evaluate_expression_raw(expr, eval_context) if isinstance(value, int): result = fetchop(expr) else: const_category = get_constant_category( value, specification.valuations) result = aconst_to_string(const_category) else: result = fetchop(expr) elif (exprs.is_formal_parameter_expression(expr)): # if isinstance(specification, specifications.PBESpec): # n_params = len(specification.synth_fun.domain_types) # else: # formula spec # n_params = len(specification.synth_funs[0].domain_types) # param_category = get_parameter_category(expr.parameter_position, n_params) # # print(exprs.expression_to_string(expr), ' ', param_category) # only a single parameter result = aparam_to_string(0) else: result = fetchop(expr) # print(exprs.expression_to_string(expr), ' ', result) return result
def preprocess_operators(term_exprs, pred_exprs): eval_context = evaluation.EvaluationContext() bitsize = 64 bvlshr = semantics_bv.BVLShR(bitsize) new_term_exprs = set([]) new_pred_exprs = set([]) for term_expr, f in term_exprs: subst_pairs = set([]) all_exprs = exprs.get_all_exprs(term_expr) for e in all_exprs: if exprs.is_function_expression(e): if e.function_info.function_name == 'bvudiv': if exprs.is_constant_expression(e.children[1]): value = evaluation.evaluate_expression_raw( e.children[1], eval_context) new_right_child = exprs.ConstantExpression( exprs.Value( BitVector(int(math.log2(value.value)), bitsize), exprtypes.BitVectorType(bitsize))) subst_pairs.add( (e, exprs.FunctionExpression( bvlshr, (e.children[0], new_right_child)))) new_term_expr = term_expr for (old_term, new_term) in subst_pairs: new_term_expr = exprs.substitute(new_term_expr, old_term, new_term) new_term_exprs.add((new_term_expr, f)) for pred_expr, f in pred_exprs: subst_pairs = set([]) all_exprs = exprs.get_all_exprs(pred_expr) for e in all_exprs: if exprs.is_function_expression(e): if e.function_info.function_name == 'bvudiv': if exprs.is_constant_expression(e.children[1]): value = evaluation.evaluate_expression_raw( e.children[1], eval_context) new_right_child = exprs.ConstantExpression( exprs.Value( BitVector(int(math.log2(value.value)), bitsize), exprtypes.BitVectorType(bitsize))) subst_pairs.add( (e, exprs.FunctionExpression( bvlshr, (e.children[0], new_right_child)))) new_pred_expr = pred_expr for (old_term, new_term) in subst_pairs: new_pred_expr = exprs.substitute(new_pred_expr, old_term, new_term) new_pred_exprs.add((new_pred_expr, f)) return (new_term_exprs, new_pred_exprs)
def __init__(self, expr_valuations, synth_fun, theory): self.synth_fun = synth_fun self.eval_ctx = evaluation.EvaluationContext() self.theory = theory self._initialize_valuations(expr_valuations) args = [ exprs.FormalParameterExpression(synth_fun, argtype, i) for i, argtype in enumerate(synth_fun.domain_types)] self.synth_fun_expr = exprs.FunctionExpression(synth_fun, tuple(args)) self.is_multipoint = False
def __init__(self, pred_generator, term_solver, synth_funs, syn_ctx, spec): self.pred_generator = pred_generator self.term_solver = term_solver self.points = [] self.spec = spec self.synth_funs = synth_funs self.syn_ctx = syn_ctx self.eval_ctx = evaluation.EvaluationContext() self.clauses = spec.get_canon_clauses() self.intro_vars = spec.get_intro_vars()
def get_pbespec_kind_bv(specification, grammar): generator_factory = enumerators.RecursiveGeneratorFactory() term_generator = grammar.to_generator(generator_factory) max_size = 3 term_generator.set_size(max_size) smallexprs = [exp for exp in term_generator.generate()] inputs2values = {} valuations = specification.valuations # value : str | int | utils.BitVector | bool # valuations: exprs.Value tuple -> str | int | utils.BitVector | bool # we make inputs be of primitive type flags = set([]) eval_context = evaluation.EvaluationContext() for inputs, output in valuations.items(): eval_context.set_valuation_map(inputs) values = [] for se in smallexprs: try: values.append( evaluation.evaluate_expression_raw(se, eval_context)) except: continue inputs2values[inputs] = values if (any(is_sub(v, output) for v in values)): flags.add(PBESpecKind.SOME_INPUT_BELONG_TO_OUTPUT) if (any(is_sub(output, v) for v in values)): flags.add(PBESpecKind.SOME_OUTPUT_BELONG_TO_INPUT) if (any(is_intersection_nonempty(output, v) for v in values)): flags.add(PBESpecKind.SOME_INPUT_INTERSECT_OUTPUT) # if (any(i1 != i2 and is_sub(i1, i2) for (i1, i2) in itertools.product(inputs, inputs))): # flags.add(PBESpecKind.SOME_INPUT_BELONG_TO_SOME_INPUT) if (all( any(is_sub(value, output) for value in inputs2values[inputs]) for inputs, output in valuations.items())): flags.add(PBESpecKind.ALL_INPUT_BELONG_TO_OUTPUT) if (all( any(is_sub(output, value) for value in inputs2values[inputs]) for inputs, output in valuations.items())): flags.add(PBESpecKind.ALL_OUTPUT_BELONG_TO_INPUT) if (all( any( is_intersection_nonempty(output, value) for value in inputs2values[inputs]) for inputs, output in valuations.items())): flags.add(PBESpecKind.ALL_INPUT_INTERSECT_OUTPUT) if len(flags) == 0: flags.add(PBESpecKind.NO_INTERSECTION) return tuple(flags)
def reset(self): self.eval_ctx = evaluation.EvaluationContext() self.points = [] self.point_set = set()
def __init__(self, spec_expr, syn_ctx, synth_funs): self.syn_ctx = syn_ctx self.eval_ctx = evaluation.EvaluationContext() self.synth_funs = synth_funs self.spec_expr = spec_expr
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