def process_defs_v2(self): for lf in self.definitions: # print(type(lf.formula)) # print(lf.formula) sym = lf.formula.defines() label = "def_" + str(sym) lhs = lf.formula.lhs() rhs = lf.formula.rhs() self.add_new_constants(rhs) args = {} vargs = [] if isinstance(lhs, lg.Apply): for arg in lhs.terms: name = "V" + str(len(vargs)) varg = lg.Var(name, arg.sort) args[arg] = varg vargs.append(varg) lhs = lgu.substitute(lhs, args) rhs = lgu.substitute(rhs, args) f = lg.Eq(lhs, rhs) if len(vargs) != 0: f = lg.ForAll(vargs, f) res = (f, label, "definition", str(sym)) self.vmt[label] = res self.defn_labels.append(label) sym = lgu.substitute(sym, self.nex2pre) label = "def_" + str(sym) pref = lgu.substitute(f, self.nex2pre) res = (pref, label, "definition", str(sym)) self.vmt[label] = res self.defn_labels.append(label)
def process_conj(self): fmlas = [] helpers = [] for lf in self.mod.labeled_conjs: label = str(lf.label) if label.startswith("help_"): helpers.append(lf) else: fmlas.append(lf.formula) cl = lut.Clauses(fmlas) f = self.get_formula(cl) pref = lgu.substitute(f, self.nex2pre) self.add_new_constants(pref) res = (pref, "prop", "invar-property", "0") self.vmt["$prop"] = res for lf in helpers: label = str(lf.label) self.helpers[label] = lf.formula cl = lut.Clauses([lf.formula]) f = self.get_formula(cl) pref = lgu.substitute(f, self.nex2pre) self.add_new_constants(pref) res = (pref, label, "help", label) self.vmt[label] = res
def process_defs_v1(self): for lf in self.definitions: # print(type(lf.formula)) # print(lf.formula) sym = lf.formula.defines() label = str(sym) lhs = lf.formula.lhs() rhs = lf.formula.rhs() self.add_new_constants(rhs) args = [] if isinstance(lhs, lg.Apply): for arg in lhs.terms: args.append(arg) self.add_definition(label, sym, args, rhs) sym = lgu.substitute(sym, self.nex2pre) label = str(sym) lhs = lgu.substitute(lf.formula.lhs(), self.nex2pre) rhs = lgu.substitute(lf.formula.rhs(), self.nex2pre) self.add_new_constants(rhs) args = [] if isinstance(lhs, lg.Apply): for arg in lhs.terms: args.append(arg) self.add_definition(label, sym, args, rhs) self.pre.remove(sym) self.defs.add(sym)
def __call__(self, *terms): if len(terms) != self.arity: raise ArityError(self.arity, terms) return substitute(self.formula, dict( (v, concretize_sorts(t, v.sort)) for v, t in zip(self.variables, terms) ))
def standardize_action(self, f, nexvars, name): nexSet = set() for n in nexvars: nexSet.add(n) self.updated.add(n) cons = lgu.used_constants(f) subs = dict() evars = [] for c in cons: if c in self.nex: if c not in nexSet: subs[c] = self.nex2pre[c] # elif False and (c not in self.pre): elif c not in self.pre: if (not c.sort.dom) and (c.sort != lg.Boolean): vname = "Vtmp" + c.name # vname = vname.replace(":", "") qv = lg.Var(vname, c.sort) subs[c] = qv evars.append(qv) action = f if len(subs) != 0: # for k, v in subs.iteritems(): # print("\treplacing %s -> %s in %s" % (k, v, name)) action = lgu.substitute(f, subs) if len(evars) != 0: action = lg.Exists(evars, action) return action
def rename_vars_no_clash(fmlas1,fmlas2): """ Rename the free variables in formula list fmlas1 so they occur nowhere in fmlas2, avoiding capture """ uvs = lu.used_variables(*fmlas2) uvs = lu.union(uvs,lu.bound_variables(*fmlas1)) rn = iu.UniqueRenamer('',(v.name for v in uvs)) vs = lu.free_variables(*fmlas1) vmap = dict((v,Variable(rn(v.name),v.sort)) for v in vs) return [lu.substitute(f,vmap) for f in fmlas1]
def rename_vars_no_clash(fmlas1, fmlas2): """ Rename the free variables in formula list fmlas1 so they occur nowhere in fmlas2, avoiding capture """ uvs = lu.used_variables(*fmlas2) uvs = lu.union(uvs, lu.bound_variables(*fmlas1)) rn = iu.UniqueRenamer('', (v.name for v in uvs)) vs = lu.free_variables(*fmlas1) vmap = dict((v, Variable(rn(v.name), v.sort)) for v in vs) return [lu.substitute(f, vmap) for f in fmlas1]
def process_init(self): init_cl = [] for name, action in self.mod.initializers: # print ("init: ", str(action)) ag = ivy_art.AnalysisGraph(initializer=lambda x: None) history = ag.get_history(ag.states[0]) post = lut.and_clauses(history.post) init_cl.append(post) clauses = lut.and_clauses(*init_cl) f = self.get_formula(clauses) pref = lgu.substitute(f, self.nex2pre) self.add_new_constants(pref) res = (pref, "init", "init", "true") self.vmt["$init"] = res
def to_constraint(self): if isinstance(self.args[1],Some): if self.args[1].if_value() != None: return And(Implies(self.args[1].args[1], lg.Exists([self.args[1].args[0]], And(self.args[1].args[1],Equals(self.args[0],self.args[1].if_value())))), Or(lg.Exists([self.args[1].args[0]],self.args[1].args[1]), Equals(self.args[0],self.args[1].else_value()))) return lg.ForAll([self.args[1].args[0]], Implies(self.args[1].args[1], lu.substitute(self.args[1].args[1],{self.args[1].args[0]:self.args[0]}))) if is_individual(self.args[0]): return Equals(*self.args) return Iff(*self.args)
def process_defs_v0(self): for lf in self.definitions: f = lf.formula.to_constraint() self.add_new_constants(f) label = str(lf.label) res = (f, label, "axiom", "true") self.vmt[label] = res self.defn_labels.append(label) pref = lgu.substitute(f, self.nex2pre) self.add_new_constants(pref) label = "__" + str(lf.label) res = (pref, label, "axiom", "true") self.vmt[label] = res self.defn_labels.append(label)
def get_selected_conjecture(self): """ Return a positive universal conjecture based on the selected facts. The result is a Clauses object """ from logic_util import used_constants, free_variables, substitute from ivy_logic_utils import negate, Clauses, simplify_clauses facts = self.get_active_facts() assert len(free_variables( * facts)) == 0, "conjecture would contain existential quantifiers..." sig_symbols = frozenset(il.sig.symbols.values()) facts_consts = used_constants(*facts) subs = {} rn = iu.VariableGenerator() for c in sorted(facts_consts, key=lambda c: c.name): if c.is_numeral() and il.is_uninterpreted_sort(c.sort): # prefix = str(c.sort)[:2].upper() + c.name subs[c] = lg.Var(rn(c.sort.name), c.sort) literals = [negate(substitute(f, subs)) for f in facts] result = Clauses([lg.Or(*literals)]) result = simplify_clauses(result) # now rename again to get a pretty clause, since some # variables have been eliminated by simplify_clauses # assert len(result.fmlas) == 1 # clause = result.fmlas[0] # subs = {} # count = defaultdict(int) # for c in free_variables(clause): # prefix = str(c.sort)[0].upper() # count[prefix] += 1 # subs[c] = lg.Var(prefix + str(count[prefix]), c.sort) # result = Clauses([substitute(clause, subs)]) # change to negation of conjunction rather than disjunction assert len(result.fmlas) == 1 if type(result.fmlas[0]) is lg.Or: result = Clauses( [lg.Not(lg.And(*(negate(lit) for lit in result.fmlas[0])))]) return result
def standardize_action(self, f, nexvars, name): nexSet = set() for n in nexvars: nexSet.add(n) self.updated.add(n) cons = lgu.used_constants(f) subs = dict() for c in cons: if c in self.nex: if c not in nexSet: subs[c] = self.nex2pre[c] if len(subs) == 0: return f else: # for k, v in subs.iteritems(): # print("\treplacing %s -> %s in %s" % (k, v, name)) return lgu.substitute(f, subs)
def process_global(self): for n in self.nex: if n not in self.updated: self.glo.add(n) subs = {} for n in self.glo: p = self.nex2pre[n] subs[p] = n self.pre.remove(p) self.str.pop(str(p)) self.set_global(n, str(p) + str(n)) # print("\treplacing %s -> %s globally" % (p, n)) if len(subs) != 0: for k, v in self.vmt.iteritems(): f, name, suffix, value = v newF = lgu.substitute(f, subs) self.vmt[k] = (newF, name, suffix, value)
def get_selected_conjecture(self): """ Return a positive universal conjecture based on the selected facts. The result is a Clauses object """ from logic_util import used_constants, free_variables, substitute from ivy_logic_utils import negate, Clauses, simplify_clauses facts = self.get_active_facts() assert len(free_variables(*facts)) == 0, "conjecture would contain existential quantifiers..." sig_symbols = frozenset(il.sig.symbols.values()) facts_consts = used_constants(*facts) subs = {} rn = iu.UniqueRenamer() for c in sorted(facts_consts, key=lambda c: c.name): if c.is_numeral() and il.is_uninterpreted_sort(c.sort): prefix = str(c.sort)[:2].upper() + str(c) subs[c] = lg.Var(rn(prefix), c.sort) literals = [negate(substitute(f, subs)) for f in facts] result = Clauses([lg.Or(*literals)]) result = simplify_clauses(result) # now rename again to get a pretty clause, since some # variables have been eliminated by simplify_clauses # assert len(result.fmlas) == 1 # clause = result.fmlas[0] # subs = {} # count = defaultdict(int) # for c in free_variables(clause): # prefix = str(c.sort)[0].upper() # count[prefix] += 1 # subs[c] = lg.Var(prefix + str(count[prefix]), c.sort) # result = Clauses([substitute(clause, subs)]) # change to negation of conjunction rather than disjunction assert len(result.fmlas) == 1 if type(result.fmlas[0]) is lg.Or: result = Clauses([lg.Not(lg.And(*(negate(lit) for lit in result.fmlas[0])))]) return result
def get_projections(self, node): """ Return a list of (name, binary_concept) with all possible projections at node """ witnesses = self._get_witnesses(node) if len(witnesses) == 0: return [] w = witnesses[0] result = [] n_concept = self.domain.concepts[node] for t_name in self.domain.concepts_by_arity(3): t_concept = self.domain.concepts[t_name] for v in t_concept.variables: if v.sort == w.sort: variables = [x for x in t_concept.variables if x is not v] formula = substitute(t_concept.formula, {v: w}) name = str(formula) concept = Concept(name, variables, formula) result.append((name, concept)) return result
def get_projections(self, node): """ Return a list of (name, binary_concept) with all possible projections at node """ witnesses = self._get_witnesses(node) if len(witnesses) == 0: return [] w = witnesses[0] result = [] n_concept = self.domain.concepts[node] for t_name in self.domain.concepts_by_arity(3): t_concept = self.domain.concepts[t_name] for v in t_concept.variables: if v.sort == w.sort: variables = [x for x in t_concept.variables if x is not v] formula = substitute(t_concept.formula, {v: w}) name = str(formula) concept = Concept(name,variables, formula) result.append((name, concept)) return result
def lambda_apply(self,args): assert len(args) == len(self.variables) return lu.substitute(self.body,dict(zip(self.variables,args)))
def lambda_apply(self, args): assert len(args) == len(self.variables) return lu.substitute(self.body, dict(zip(self.variables, args)))