def filter_terms(terms, inps): assert isinstance(inps, set) and \ all(isinstance(s, str) for s in inps), inps if not inps: mlog.warning("Have not tested case with no inps") excludes = set() for term in terms: if isinstance(term, data.poly.mp.MP): a_symbs = list(map(str, Miscs.get_vars(term.a))) b_symbs = list(map(str, Miscs.get_vars(term.b))) inp_in_a = any(s in inps for s in a_symbs) inp_in_b = any(s in inps for s in b_symbs) if ((inp_in_a and inp_in_b) or (not inp_in_a and not inp_in_b)): excludes.add(term) continue t_symbs = set(a_symbs + b_symbs) else: t_symbs = set(map(str, term.symbols)) if len(t_symbs) <= 1: # ok for finding bound of single input val continue if (inps.issuperset(t_symbs) or all(s not in inps for s in t_symbs)): excludes.add(term) new_terms = [term for term in terms if term not in excludes] return new_terms
def get_postconds(cls, eqt): assert Miscs.is_expr(eqt), eqt assert eqt.operator() == operator.eq, eqt # tCtr symbols = [ s for s in Miscs.get_vars(eqt) if settings.CTR_VAR in str(s) ] if not symbols: return assert len(symbols) == 1, \ "should only have 1 tCtr symbol: {}, {}".format( symbols, settings.CTR_VAR) postconds = sage.all.solve(eqt, symbols[0]) return postconds if len(postconds) >= 1 else None
def instantiate(self, term, ntraces): assert Miscs.is_expr(term), term assert ntraces is None or ntraces >= 1, ntraces if ntraces is None: for t in self.mydicts: exprs = set(term.subs(t) for t in self.mydicts) else: ntracesExtra = ntraces * settings.TRACE_MULTIPLIER exprs = set() for t in self.mydicts: expr = term.subs(t) if expr not in exprs: exprs.add(expr) if len(exprs) >= ntracesExtra: break # instead of doing this, can find out the # 0's in traces # the more 0's , the better exprs = sorted(exprs, key=lambda expr: len(Miscs.get_vars(expr))) exprs = set(exprs[:ntraces]) return exprs
def get_conj_preconds(self, loc, preconds, postcond_expr): """ preconds => post can be strengthened by removing some preconds e.g., a&b => post is stronger than a&b&c => post """ assert all(isinstance(p, Inv) for p in preconds), preconds assert z3.is_expr(postcond_expr), postcond_expr if not preconds: return [] preconds = sorted(preconds, key=lambda p: len(Miscs.get_vars(p.inv))) preconds_exprs = [pc.expr(self.use_reals) for pc in preconds] if not self.check(preconds_exprs, postcond_expr, loc): return [] def _imply(js, _): jexprs = [preconds_exprs[j] for j in js] return self.check(jexprs, postcond_expr, loc) results = Miscs.simplify_idxs(list(range(len(preconds))), _imply) results = [preconds[i] for i in results] return results
def symbols(self): try: return self._symbols except AttributeError: self._symbols = Miscs.get_vars(self.poly) return self._symbols
def mysorted(ps): return sorted(ps, key=lambda p: len(Miscs.get_vars(p.inv)))
def symbols(self): try: return self._symbols except AttributeError: self._symbols = set(map(str, Miscs.get_vars(self.a + self.b))) return self._symbols