def fetchop(expr): if (exprs.is_function_expression(expr)): fkind = expr.function_info.function_name if dummy_pred_name in fkind: return dummy_pred_name else: return fkind elif (exprs.is_constant_expression(expr)): # return str(exprs.get_expression_type(expr)) return exprs.expression_to_string(expr) elif (exprs.is_formal_parameter_expression(expr)): return exprs.expression_to_string(expr) else: return 'Var'
def to_string(self, expr_object): ret = "(let (" ret += ' '.join([ "(%s %s %s)" % (bn, bt.print_string(), exprs.expression_to_string(e)) for bn, bt, e in zip(self.binding_names, self.binding_types, expr_object.children[:-1]) ]) ret += ') ' ret += exprs.expression_to_string(expr_object.children[-1]) ret += ')' return ret
def test_generators(): start_generator = _generate_test_generators() start_generator.set_size(5) for exp in start_generator.generate(): print(exprs.expression_to_string(exp)) # tests for bunched generators print('Testing bunched generators....') bunch_generator = BunchedGenerator(start_generator, 10, 5) for bunch in bunch_generator.generate(): print('Bunch:') for exp in bunch: print(' %s' % exprs.expression_to_string(exp))
def __str__(self): ret = "" for var, val in self.coeffs.items(): if var == 1: ret += "+ " + str(val) ret += "+ " + str(val) + "*" + exprs.expression_to_string(var) return ret
def str(self): ret = "" for nt in self.non_terminals: ret = ret + nt + "[" + str(self.nt_type[nt]) + "] ->\n" for rule in self.rules[nt]: ph_vars, nts, expr_template = rule.to_template_expr() ret = ret + "\t" + exprs.expression_to_string(expr_template) + "\n" return ret
def print_caches(self): print('++++++++++++') for placeholder, size in self.cache: print(placeholder, size, '->') # pass for term in self.cache[(placeholder, size)]: print(exprs.expression_to_string(term)) print('++++++++++++')
def print_caches(self): for i, cache in enumerate(self.all_caches): print('++++++++++++', i) for placeholder, size in cache: print(placeholder, size, '->') for term in cache[(placeholder, size)]: print("\tscore=", term.score, "\t", exprs.expression_to_string(term)) print('++++++++++++', "fin")
def solve(self): if len(self.points) == 0: print("Trivial solve!") return self._trivial_solve() print("-----------------") print("Nontrivial solve!") for point in self.points: print("POINT:", [ p.value_object for p in point]) for sig, term in self.signature_to_term.items(): print('SIGTOTERM:', str(sig), _expr_to_str(term)) intro_var_signature = [] for point in self.points: ivs = point[:len(self.spec.intro_vars)] intro_var_signature.append((ivs, point)) # Nobody can understand what python groupby returns!!! # ivs_groups = itertools.groupby( # sorted(intro_var_signature, key=lambda a: a[0]), # key=lambda a: a[0]) curr_ivs = None ivs_groups = [] for ivs, point in intro_var_signature: if ivs == curr_ivs: ivs_groups[-1][1].append(point) else: ivs_groups.append((ivs, [point])) curr_ivs = ivs for ivs, points in ivs_groups: print("A:") terms = self._single_solve(ivs, points) print("C:", [ exprs.expression_to_string(t) for t in terms ]) new_terms = [] for term, sf in zip(terms, self.synth_funs): new_terms.append(exprs.substitute_all(term, list(zip(self.spec.intro_vars, self.spec.formal_params[sf])))) terms = new_terms print([ _expr_to_str(t) for t in terms ]) sig = self.signature_factory() if len(self.synth_funs) > 1: domain_types = tuple([exprtypes.IntType()] * len(self.synth_funs)) single_term = exprs.FunctionExpression(semantics_core.CommaFunction(domain_types), tuple(terms)) else: single_term = terms[0] for i, t in enumerate(self.term_signature(single_term, self.points)): if t: sig.add(i) self.signature_to_term[sig] = single_term print("-----------------") return True
def fetchop_func(expr): key = exprs.expression_to_string(expr) if key in fetchop_func.cache: return fetchop_func.cache[key] if isinstance(specification, specifications.PBESpec): result = fetchop_func_pbe(specification, grammar, expr) else: result = fetchop_func_formula(specification, grammar, expr) fetchop_func.cache[key] = result return result
def check_expr_binding_to_context(expr, syn_ctx): kind = expr.expr_kind if (kind == exprs.ExpressionKinds.variable_expression): if (not syn_ctx.validate_variable(expr.variable_info)): raise TypeError( ('Expression %s was not formed using the given ' + 'context!') % exprs.expression_to_string(expr)) elif (kind == exprs.ExpressionKinds.function_expression): if (not syn_ctx.validate_function(expr.function_info)): raise TypeError( ('Expression %s was not formed using the given ' + 'context!') % exprs.expression_to_string(expr)) for child in expr.children: check_expr_binding_to_context(child, syn_ctx) elif (kind == exprs.ExpressionKinds.formal_parameter_expression): raise TypeError( ('Expression %s contains a formal parameter! Specifications ' + 'are not allowed to contain formal ' + 'parameters!') % (exprs.expression_to_string(expr))) else: return
def fetch_prod(partial_ast, curr_addr): curr_node = partial_ast for idx in curr_addr: if exprs.is_function_expression(curr_node) and len( curr_node.children) > 0: if (len(curr_node.children) > idx): curr_node = curr_node.children[idx] else: print(exprs.expression_to_string(curr_node), ' ', idx, ' ', len(curr_node.children)) assert False return curr_node
def gen(gens): for product_tuple in product_fun(*gens): retval = exprs.substitute_all( self.expr_template, list(zip(self.place_holder_vars, product_tuple))) word = rule_to_word(exprs.expression_to_string(retval)) score = self.pref[word] children_score = score_children_combiner( x.score for x in product_tuple) score = score_expr_combiner(score, children_score) retval = retval._replace(score=score) yield retval
def print_solutions(synth_funs, final_solutions): for sf, sol in zip(synth_funs, final_solutions): fp_infos = [] for v in sf.get_named_vars(): v_name = v.variable_info.variable_name v_type = v.variable_info.variable_type.print_string() fp_infos.append((v_name, v_type)) fp_infos_strings = ['(%s %s)' % (n, t) for (n, t) in fp_infos] fp_string = ' '.join(fp_infos_strings) print('(define-fun %s (%s) %s\n %s)' % (sf.function_name, fp_string, sf.range_type.print_string(), exprs.expression_to_string(sol)), flush=True)
def leaf_expr_to_word(expr): s = exprs.expression_to_string(expr) if parsable_as_int(s): num = parse_as_int(s) if num > 1: return "__bigint" else: return str(num) elif s.startswith("_arg_"): return "__arg" elif s == "z": return "z" print(s) assert False
def rewrite_expr_to_string(expr): kind = expr.expr_kind if (kind == exprs._function_expression): # child_strs = [] str = '(' + expr.function_info.function_name for child in expr.children: str += ' ' child_str = rewrite_expr_to_string(child) # child_strs.append(child_str) str += child_str str += ')' return str else: str = exprs.expression_to_string(expr) # non-terminal symbol if kind == exprs._variable_expression: tokens = str.split('_ph_') str = tokens[0] return str else: return str
def _default_generate_more_terms(self, transform_term=None): signature_to_term = self.signature_to_term bunch_generator_state = self.bunch_generator_state try: bunch = next(bunch_generator_state) except StopIteration: return False for term in bunch: print("BUNCH", exprs.expression_to_string(term)) if transform_term is not None: term = transform_term(term) sig = self._compute_term_signature(term) if (sig in signature_to_term or sig.is_empty()): continue signature_to_term[sig] = term self.full_signature = self.full_signature | sig if sig.is_full(): self.one_full_signature = True return True
def str(self): return exprs.expression_to_string(self.expr)
def solve(self, generator_factory, term_solver, unifier, verifier, verify_term_solve=True): import time import verifiers.verifiers time_origin = time.clock() while (True): print('________________') # iterate until we have terms that are "sufficient" success = term_solver.solve() if not success: return None # we now have a sufficient set of terms print('Term solve complete!') print( "TERMS", "\n".join([ "\t" + _expr_to_str(term) for sig, term in term_solver.get_signature_to_term().items() ])) # Check term solver for completeness if verify_term_solve: cexs = verifier.verify_term_solve( list(term_solver.get_signature_to_term().values())) else: cexs = None # print('Term solve checked!') if cexs is None: unifier_state = unifier.unify() unification = next(unifier_state) print('Unification done!') #print(unification) # FFS, docs! if unification[0] == "TERM": dt = unification[1].expr_id else: dt = unification[1][-1] if dt is None: print("AFTER_UU", None) else: uu = verifiers.verifiers.naive_dt_to_expr( self.syn_ctx, dt, unification[1][2], unification[1][0]) print("AFTER_UU", exprs.expression_to_string(uu)) sol_or_cex = verifier.verify(unification) print('Verification done!') if _is_expr(sol_or_cex): print("FOUND_FINAL", exprs.expression_to_string(sol_or_cex)) else: print("%d cex found" % len(sol_or_cex)) else: print('Term solve incomplete!') sol_or_cex = cexs if _is_expr(sol_or_cex): solution_found_at = time.clock() - time_origin if self.report_additional_info: yield (sol_or_cex, unifier.last_dt_size, term_solver.get_num_distinct_terms(), unifier.get_num_distinct_preds(), term_solver.get_largest_term_size_enumerated(), unifier.get_largest_pred_size_enumerated(), len(self.points), solution_found_at) else: yield sol_or_cex return for cex in sol_or_cex: print('ADDING POINT:', [p.value_object for p in cex]) term_solver.add_points( sol_or_cex) # Term solver can add all points at once unifier.add_points(sol_or_cex) self.add_points(sol_or_cex) generator_factory.add_points(sol_or_cex)
def to_string(self, expr_object): return "(not (= %s %s))" % tuple( [exprs.expression_to_string(c) for c in expr_object.children])
def get_exprs_info(expr_str_set): global exprstr2expr expr_set = [exprstr2expr[exprstr] for exprstr in expr_str_set] def get_all_addrs(expr): addrs = set() addrs.add(()) stack = [(expr, [])] while len(stack) > 0: v, addr = stack.pop(0) if exprs.is_function_expression(v): for i, child in enumerate(v.children): new_addr = list(addr) new_addr.append(i) addrs.add(tuple(new_addr)) stack.append((child, new_addr)) return addrs expr2history = {} for expr, fetchop_func in expr_set: history = get_history(expr) # print('------------------------------') # print('expr : ', exprs.expression_to_string(expr)) # for e, addr in history: # print('\t', exprs.expression_to_string(e), '\t', fetchop(fetch_prod(e, addr))) # history = get_history_new(expr) # print('expr : ', exprs.expression_to_string(expr)) # for e, addr in history: # print('\t', exprs.expression_to_string(e), '\t', fetchop(fetch_prod(e, addr))) # print('------------------------------') expr2history[expr] = (history, fetchop_func) expr2cache = {} for _, (history, fetchop_func) in expr2history.items(): for (expr, target_addr) in history: addrs = get_all_addrs(expr) write_cache = {} move_cache = {} for addr in addrs: for instr in move_instrs: new_addr, _ = get_ctxt(fetchop_func, expr, addr, [instr], training=True) move_cache[(addr, instr)] = new_addr _, ctxt = get_ctxt(fetchop_func, expr, addr, [Tcond.WRITE_VALUE], training=True) value = ctxt[0] # we do not use types for now # _, ctxt = get_ctxt(expr, addr, [Tcond.WRITE_TYPE], training=True) type = ctxt[0] # store value, type at addr into write_cache write_cache[addr] = (value, type) expr2cache[exprs.expression_to_string(expr)] = (move_cache, write_cache) # print(move_cache) # print(write_cache) # transform history into expr_string -> history_only_with_exprstrings expr2history_ = {} for (expr, (history, _)) in expr2history.items(): history_ = [] for (expr, target_addr) in history: history_.append((exprs.expression_to_string(expr), target_addr)) history = history_ expr2history_[exprs.expression_to_string(expr)] = history expr2history = expr2history_ return (expr2history, expr2cache)
def canonicalize_specification(expr, syn_ctx, theory): """Performs a bunch of operations: 1. Checks that the expr is "well-bound" to the syn_ctx object. 2. Checks that the specification has the single-invocation property. 3. Gathers the set of synth functions (should be only one). 4. Gathers the variables used in the specification. 5. Converts the specification to CNF (as part of the single-invocation test) 6. Given that the spec is single invocation, rewrites the CNF spec (preserving and sat) by introducing new variables that correspond to a uniform way of invoking the (single) synth function Returns a tuple containing: 1. A list of 'variable_info' objects corresponding to the variables used in the spec 2. A list of synth functions (should be a singleton list) 3. A list of clauses corresponding to the CNF specification 4. A list of NEGATED clauses 5. A list containing the set of formal parameters that all appearances of the synth functions are invoked with. """ check_expr_binding_to_context(expr, syn_ctx) clauses, cnf_expr = to_cnf(expr, theory, syn_ctx) synth_function_set = gather_synth_functions(expr) synth_function_list = list(synth_function_set) num_funs = len(synth_function_list) orig_variable_set = gather_variables(expr) orig_variable_list = [x.variable_info for x in orig_variable_set] orig_variable_list.sort(key=lambda x: x.variable_name) # check single invocation/separability properties if (not check_single_invocation_property(clauses, syn_ctx)): raise basetypes.ArgumentError('Spec:\n%s\nis not single-invocation!' % exprs.expression_to_string(expr)) (intro_clauses, intro_vars) = _intro_new_universal_vars(clauses, syn_ctx, synth_function_list[0]) # ensure that the intro_vars at the head of the list # Arjun: Why? Most likely not necessary variable_list = [x.variable_info for x in intro_vars] + orig_variable_list num_vars = len(variable_list) for i in range(num_vars): variable_list[i].variable_eval_offset = i num_funs = len(synth_function_list) for i in range(num_funs): synth_function_list[i].synth_function_id = i if len(intro_clauses) == 1: canon_spec = intro_clauses[0] else: canon_spec = syn_ctx.make_function_expr('and', *intro_clauses) canon_clauses = [] for ic in intro_clauses: if exprs.is_application_of(ic, 'or'): disjuncts = ic.children else: disjuncts = [ic] canon_clauses.append(disjuncts) return (variable_list, synth_function_list, canon_spec, canon_clauses, intro_vars)
(str(ret_type), '(for eusolver)' if eusolver else '')) (term_exprs, pred_exprs) = get_all_atomic_exprs(fun_exprs) if eusolver else ( fun_exprs, set([])) print('Collected term exprs for training : ', len(term_exprs)) # for expr, f in term_exprs: # print(exprs.expression_to_string(expr)) print('Collected pred exprs for training : ', len(pred_exprs)) # for expr, f in pred_exprs: # print(exprs.expression_to_string(expr)) # collecting pre-computed information to expedite learning total_exprs = list(term_exprs) + list(pred_exprs) for e, f in total_exprs: estr = exprs.expression_to_string(e) exprstr2expr[estr] = e, f exprs_info = get_exprs_info(list(exprstr2expr.keys())) # freeze exprs to strings term_exprs = [exprs.expression_to_string(e) for e, f in term_exprs] pred_exprs = [exprs.expression_to_string(e) for e, f in pred_exprs] term_exprs_wo_dup = sorted(list(set(term_exprs))) pred_exprs_wo_dup = sorted(list(set(pred_exprs))) print('Collected term exprs for training (w/o duplication) : ', len(term_exprs_wo_dup)) # for expr in term_exprs_wo_dup: # print(expr) print('Collected pred exprs for training (w/o duplication) : ', len(pred_exprs_wo_dup)) # for expr in pred_exprs_wo_dup: