def show_used_relations(self, clauses, both=False): self.current_concept_graph.clear_edges() rels = self.current_concept_graph.g.relations used = set( il.normalize_symbol(s) for s in lu.used_constants(clauses.to_formula())) for rel in rels: fmla = rel.formula if any(c in used and not c.name.startswith('@') for c in lu.used_constants(fmla)): self.current_concept_graph.show_relation(rel, '+', update=False) if both and not il.is_enumerated(fmla): self.current_concept_graph.show_relation(rel, '-', update=False) need_update_relations = False for app in ilu.apps_clauses(clauses): if len(app.args) == 3 and il.is_numeral(app.args[0]): fmla = app.rep(app.args[0], il.Variable('X', app.args[1].sort), il.Variable('Y', app.args[2].sort)) concept = self.current_concept_graph.g.formula_to_concept(fmla) self.current_concept_graph.g.new_relation(concept) need_update_relations = True self.current_concept_graph.show_relation(concept, '+', update=False) if both: self.current_concept_graph.show_relation(concept, '-', update=False) if need_update_relations: self.current_concept_graph.update_relations() self.current_concept_graph.update()
def formula_to_z3_int(fmla): # print "formula_to_z3_int: {} : {}".format(fmla,type(fmla)) if ivy_logic.is_atom(fmla): return atom_to_z3(fmla) if isinstance(fmla, ivy_logic.Definition) and ivy_logic.is_enumerated(fmla.args[0]) and not use_z3_enums: return encode_equality(*fmla.args) args = [formula_to_z3_int(arg) for arg in fmla.args] if isinstance(fmla, ivy_logic.And): return z3.And(args) if isinstance(fmla, ivy_logic.Or): return z3.Or(args) if isinstance(fmla, ivy_logic.Not): return z3.Not(args[0]) if isinstance(fmla, ivy_logic.Definition): return my_eq(args[0], args[1]) if isinstance(fmla, ivy_logic.Iff): return my_eq(args[0], args[1]) if isinstance(fmla, ivy_logic.Implies): return z3.Implies(args[0], args[1]) if isinstance(fmla, ivy_logic.Ite): return z3.If(args[0], args[1], args[2]) if ivy_logic.is_quantifier(fmla): variables = ivy_logic.quantifier_vars(fmla) q = z3.ForAll if ivy_logic.is_forall(fmla) else z3.Exists res = q([term_to_z3(v) for v in variables], args[0]) # print "res = {}".format(res) return res if ivy_logic.is_individual(fmla): return term_to_z3(fmla) print "bad fmla: {!r}".format(fmla) assert False
def formula_to_z3_int(fmla): # print "formula_to_z3_int: {} : {}".format(fmla,type(fmla)) if ivy_logic.is_atom(fmla): return atom_to_z3(fmla) if isinstance(fmla, ivy_logic.Definition) and ivy_logic.is_enumerated( fmla.args[0]): return encode_equality(*fmla.args) args = [formula_to_z3_int(arg) for arg in fmla.args] if isinstance(fmla, ivy_logic.And): return z3.And(args) if isinstance(fmla, ivy_logic.Or): return z3.Or(args) if isinstance(fmla, ivy_logic.Not): return z3.Not(args[0]) if isinstance(fmla, ivy_logic.Definition): return my_eq(args[0], args[1]) if isinstance(fmla, ivy_logic.Iff): return my_eq(args[0], args[1]) if isinstance(fmla, ivy_logic.Implies): return z3.Implies(args[0], args[1]) if isinstance(fmla, ivy_logic.Ite): return z3.If(args[0], args[1], args[2]) if ivy_logic.is_quantifier(fmla): variables = ivy_logic.quantifier_vars(fmla) q = z3.forall if ivy_logic.is_forall(fmla) else z3.Exists res = q([term_to_z3(v) for v in variables], args[0]) # print "res = {}".format(res) return res if ivy_logic.is_individual(fmla): return term_to_z3(fmla) print "bad fmla: {!r}".format(fmla) assert False
def term_to_z3(term): if ivy_logic.is_boolean(term): return formula_to_z3_int(term) if not term.args: if isinstance(term, ivy_logic.Variable): sorted = hasattr(term, 'sort') sksym = term.rep + ':' + str(term.sort) if sorted else term.rep res = z3_constants.get(sksym) if res is not None: return res # print str(term.sort) sig = lookup_native(term.sort, sorts, "sort") if sorted else S if sig == None: sig = term.sort.to_z3() # if sorted: # print type(term.sort) # print term.sort # print type(sksym) # print sksym # print sig res = z3.Const(sksym, sig) z3_constants[sksym] = res return res res = z3_constants.get(term.rep) if res is None: # if isinstance(term.rep,str): # print "{} : {}".format(term,term.rep) if term.is_numeral(): res = numeral_to_z3(term.rep) elif ivy_logic.is_enumerated( term) and ivy_logic.is_interpreted_sort(term.sort): res = enumerated_to_numeral(term) else: iso = term.rep.sort # TODO: this is dangerous sig = iso.to_z3() if iso is not None else S # print "term: {}, iso : {}, sig = {}".format(term,iso,sig) res = z3.Const(term.rep.name, sig) z3_constants[term.rep] = res elif isinstance(term, ivy_logic.Ite): return z3.If(formula_to_z3_int(term.args[0]), term_to_z3(term.args[1]), term_to_z3(term.args[2])) else: if not hasattr(term, 'rep'): print term print term.lineno fun = z3_functions.get(term.rep) if fun is None: fun = lookup_native(term.rep, functions, "function") if fun is None: sig = term.rep.sort.to_z3() fun = z3.Function(term.rep.name, *sig) z3_functions[term.rep] = fun args = [term_to_z3(arg) for arg in term.args] res = apply_z3_func(fun, args) return res
def term_to_z3(term): if ivy_logic.is_boolean(term): return formula_to_z3_int(term) if not term.args: if isinstance(term, ivy_logic.Variable): sorted = hasattr(term, "sort") sksym = term.rep + ":" + str(term.sort) if sorted else term.rep res = z3_constants.get(sksym) if res is not None: return res # print str(term.sort) sig = lookup_native(term.sort, sorts, "sort") if sorted else S if sig == None: sig = term.sort.to_z3() # if sorted: # print type(term.sort) # print term.sort # print type(sksym) # print sksym # print sig res = z3.Const(sksym, sig) z3_constants[sksym] = res return res res = z3_constants.get(term.rep) if res is None: # if isinstance(term.rep,str): # print "{} : {}".format(term,term.rep) if term.is_numeral(): res = numeral_to_z3(term.rep) elif ivy_logic.is_enumerated(term) and ivy_logic.is_interpreted_sort(term.sort): res = enumerated_to_numeral(term) else: iso = term.rep.sort # TODO: this is dangerous sig = iso.to_z3() if iso != None else S # print "term: {}, iso : {}, sig = {}".format(term,iso,sig) res = z3.Const(term.rep.name, sig) z3_constants[term.rep] = res elif isinstance(term, ivy_logic.Ite): return z3.If(formula_to_z3_int(term.args[0]), term_to_z3(term.args[1]), term_to_z3(term.args[2])) else: fun = z3_functions.get(term.rep) if fun is None: fun = lookup_native(term.rep, functions, "function") if fun is None: sig = term.rep.sort.to_z3() fun = z3.Function(term.rep.name, *sig) z3_functions[term.rep] = fun args = [term_to_z3(arg) for arg in term.args] res = apply_z3_func(fun, args) return res
def atom_to_z3(atom): if ivy_logic.is_equals(atom.rep) and ivy_logic.is_enumerated(atom.args[0]) and not use_z3_enums: return encode_equality(*atom.args) if atom.relname not in z3_predicates: rel = lookup_native(atom.relname,relations,"relation") # print "atom: {}, rep: {}, rep.sort: {}".format(atom,atom.rep,atom.rep.sort) if not rel: sig = atom.rep.sort.to_z3() rel = z3.Function(solver_name(atom.rep), *sig) z3_predicates[atom.relname] = rel # return z3_predicates[atom.relname]( # *[term_to_z3(t) for t in atom.args]) pred = z3_predicates[atom.relname] tup = [term_to_z3(t) for t in atom.args] return apply_z3_func(pred,tup)
def atom_to_z3(atom): if ivy_logic.is_equals(atom.rep) and ivy_logic.is_enumerated(atom.args[0]): return encode_equality(*atom.args) if atom.relname not in z3_predicates: rel = lookup_native(atom.relname, relations, "relation") # print "atom: {}, rep: {}, rep.sort: {}".format(atom,atom.rep,atom.rep.sort) if not rel: sig = atom.rep.sort.to_z3() rel = z3.Function(atom.rep.name, *sig) z3_predicates[atom.relname] = rel # return z3_predicates[atom.relname]( # *[term_to_z3(t) for t in atom.args]) pred = z3_predicates[atom.relname] tup = [term_to_z3(t) for t in atom.args] return apply_z3_func(pred, tup)
def atom_to_z3(atom): if ivy_logic.is_equals(atom.rep) and ivy_logic.is_enumerated(atom.args[0]) and not use_z3_enums: return encode_equality(*atom.args) if atom.relname not in z3_predicates: rel = lookup_native(atom.relname,relations,"relation") # print "atom: {}, rep: {}, rep.sort: {}".format(atom,atom.rep,atom.rep.sort) if not rel: if atom.rep.name in polymacs and iu.ivy_use_polymorphic_macros: rel = get_polymacs(atom.rep) else: sig = atom.rep.sort.to_z3() rel = z3.Function(solver_name(atom.rep), *sig) if isinstance(sig,list) else z3.Const(solver_name(atom.rep),sig) z3_predicates[atom.relname] = rel # return z3_predicates[atom.relname]( # *[term_to_z3(t) for t in atom.args]) pred = z3_predicates[atom.relname] tup = [term_to_z3(t) for t in atom.args] return apply_z3_func(pred,tup)
def atom_to_z3(atom): if ivy_logic.is_equals(atom.rep) and ivy_logic.is_enumerated(atom.args[0]) and not use_z3_enums: return encode_equality(*atom.args) if atom.relname not in z3_predicates: rel = lookup_native(atom.relname, relations, "relation") # print "atom: {}, rep: {}, rep.sort: {}".format(atom,atom.rep,atom.rep.sort) if not rel: if atom.rep.name in polymacs and iu.ivy_use_polymorphic_macros: rel = get_polymacs(atom.rep) else: sig = atom.rep.sort.to_z3() rel = ( z3.Function(solver_name(atom.rep), *sig) if isinstance(sig, list) else z3.Const(solver_name(atom.rep), sig) ) z3_predicates[atom.relname] = rel # return z3_predicates[atom.relname]( # *[term_to_z3(t) for t in atom.args]) pred = z3_predicates[atom.relname] tup = [term_to_z3(t) for t in atom.args] return apply_z3_func(pred, tup)
def show_used_relations(self,clauses,both=False): self.current_concept_graph.clear_edges() rels = self.current_concept_graph.g.relations used = set(il.normalize_symbol(s) for s in lu.used_constants(clauses.to_formula())) for rel in rels: fmla = rel.formula if any(c in used and not c.name.startswith('@') for c in lu.used_constants(fmla)): self.current_concept_graph.show_relation(rel,'+',update=False) if both and not il.is_enumerated(fmla): self.current_concept_graph.show_relation(rel,'-',update=False) need_update_relations = False for app in ilu.apps_clauses(clauses): if len(app.args) == 3 and il.is_numeral(app.args[0]): fmla = app.rep(app.args[0],il.Variable('X',app.args[1].sort),il.Variable('Y',app.args[2].sort)) concept = self.current_concept_graph.g.formula_to_concept(fmla) self.current_concept_graph.g.new_relation(concept) need_update_relations = True self.current_concept_graph.show_relation(concept,'+',update=False) if both: self.current_concept_graph.show_relation(concept,'-',update=False) if need_update_relations: self.current_concept_graph.update_relations() self.current_concept_graph.update()