def int_update(self,domain,pvars): if determinize and len(self.args) == 2: cond = bool_const('___branch:' + str(self.unique_id)) ite = IfAction(cond,self.args[0],self.args[1]) return ite.update(domain,pvars) result = [], false_clauses(), false_clauses() for a in self.args: foo = a.update(domain, pvars) result = join_action(result, foo, domain.relations) return result
def top_bottom(node): # TODO: change this to use the concrete post already there preds, action = arg_get_preds_action(node) assert action != "join" assert len(preds) == 1 pred = preds[0] implied = implied_facts(forward_image(arg_get_fact(pred), action), [false_clauses()]) if len(implied) == 1: node.clauses = false_clauses() else: node.clauses = true_clauses()
def top_bottom(node): # TODO: change this to use the concrete post already there preds, action = arg_get_preds_action(node) assert action != 'join' assert len(preds) == 1 pred = preds[0] implied = implied_facts( forward_image(arg_get_fact(pred), action), [false_clauses()], ) if len(implied) == 1: node.clauses = false_clauses() else: node.clauses = true_clauses()
def testfunc(mod): # for testing/ debugging ns = logic.UninterpretedSort("node") ids = logic.UninterpretedSort('id') n1 = logic.Var('Node0', ns) n2 = logic.Var('Node1', ns) ineq = logic.Not(logic.Eq(n2, n1)) leadsorts = [ns, logic.BooleanSort()] leadfunc = logic.Const("leader", logic.FunctionSort(*leadsorts)) idsorts = [ns, logic.UninterpretedSort("id")] idfunc = logic.Const("idn", logic.FunctionSort(*idsorts)) lesorts = [ids, ids, logic.BooleanSort()] lefunc = logic.Const('le', logic.FunctionSort(*lesorts)) leadterm = logic.Apply(leadfunc, *[n1]) leterm = logic.Apply( lefunc, *[logic.Apply(idfunc, *[n1]), logic.Apply(idfunc, *[n2])]) fmla = logic.Not(logic.And(*[ineq, leadterm, leterm])) candInv, coincide = Clauses([fmla]), false_clauses() print "<plearn> CandInv", candInv for actname in sorted(mod.public_actions): spos, sneg = samplePos(mod, candInv, coincide, actname), sampleNeg(mod, candInv, actname) spos, sneg = Sample(spos, '1'), Sample(sneg, '0') if hasattr(spos, 'interp'): print "<plearn> + interp: ", spos.interp spos.displaySample() if hasattr(sneg, 'interp'): print "<plearn> - interp: ", sneg.interp sneg.displaySample() exit(0)
def learnWeekestInv(mod, clf, actname): ''' candInv and coincide will be of type ivy_logic_utils.Clauses. coincide is a Clause object representing samples which cannot be distinguish by feature set ''' global candInv, coincide candInv, coincide = true_clauses(), false_clauses() while True: spos = samplePos(mod, candInv, coincide, actname) # Generating positve sample sneg = sampleNeg(mod, candInv, actname) if spos is None and sneg is None: break spos, sneg = Sample(spos, '1'), Sample(sneg, '0') print "Pos Sample", spos.unv.unvsize() if hasattr(spos, 'interp') else None print "Neg Sample", sneg.unv.unvsize() if hasattr(sneg, 'interp') else None # if hasattr(spos, 'interp'): # spos is not None # print "<plearn> + sample interpretation : ",spos.interp, "\n" # for detailed information # spos.displaySample() # for displaying the sample # if hasattr(sneg, 'interp'): # sneg is not None # print "<plearn> - sample interpretation : ",sneg.interp, "\n" # for detailed information # sneg.displaySample() clf.addSample(spos) # adding Positive sample clf.addSample(sneg) print "<plearn> done adding samples" import sys sys.stdout.flush() candInv, coincide = clf.learn() print "<plearn> candidate Invariant", candInv sys.stdout.flush() # print "coincide Clause", coincide # a = raw_input('One iteration of loop completed, press enter:') return candInv
def int_update(self, domain, pvars): update = ([], true_clauses(), false_clauses()) axioms = domain.background_theory(pvars) for op in self.args: thing = op.int_update(domain, pvars) # print "op: {}, thing: {}".format(op,thing) update = compose_updates(update, axioms, thing) return update
def int_update(self,domain,pvars): update = ([],true_clauses(),false_clauses()) axioms = domain.background_theory(pvars) for op in self.args: thing = op.int_update(domain,pvars); # print "op: {}, thing: {}".format(op,thing) update = compose_updates(update,axioms,thing) return update
def action_update(self, domain, pvars): lhs, rhs = self.args n = lhs.rep # Handle the hierarchical case if n in domain.hierarchy: asgns = [ postfix_atoms_ast(self, Atom(x, [])) for x in domain.hierarchy[n] ] res = unzip_append( [asgn.action_update(domain, pvars) for asgn in asgns]) return res # If the lhs application is partial, make it total by adding parameters xtra = len(lhs.rep.sort.dom) - len(lhs.args) if xtra < 0: raise IvyError(self, "too many parameters in assignment to " + lhs.rep) if xtra > 0: extend = sym_placeholders(lhs.rep)[-xtra:] extend = variables_distinct_list_ast(extend, self) # get unused variables lhs = add_parameters_ast(lhs, extend) # Assignment of individual to a boolean is a special case if is_individual_ast(rhs) and not is_individual_ast(lhs): rhs = eq_atom(extend[-1], add_parameters_ast(rhs, extend[0:-1])) else: rhs = add_parameters_ast(rhs, extend) type_check(domain, rhs) if is_individual_ast(lhs) != is_individual_ast(rhs): # print type(lhs.rep) # print str(lhs.rep) # print type(lhs.rep.sort) # print "lhs: %s: %s" % (lhs,type(lhs)) # print "rhs: %s: %s" % (rhs,type(rhs)) raise IvyError(self, "sort mismatch in assignment to {}".format(lhs.rep)) new_n = new(n) args = lhs.args dlhs = new_n(*sym_placeholders(n)) vs = dlhs.args eqs = [ eq_atom(v, a) for (v, a) in zip(vs, args) if not isinstance(a, Variable) ] rn = dict( (a.rep, v) for v, a in zip(vs, args) if isinstance(a, Variable)) drhs = substitute_ast(rhs, rn) if eqs: drhs = Ite(And(*eqs), drhs, n(*dlhs.args)) new_clauses = Clauses([], [Definition(dlhs, drhs)]) # print "assign new_clauses = {}".format(new_clauses) return ([n], new_clauses, false_clauses())
def action_update(self,domain,pvars): type_check(domain,self.args[0]) # print type(self.args[0]) ca = checked_assert.get() if ca: if ca != self.lineno: return ([],formula_to_clauses(self.args[0]),false_clauses()) cl = formula_to_clauses(dual_formula(self.args[0])) # return ([],formula_to_clauses_tseitin(self.args[0]),cl) return ([],true_clauses(),cl)
def start(self): ivy_ui.AnalysisGraphUI.start(self) self.node(0).clauses = ilu.false_clauses() # just to make CG empty initially self.transitive_relations = [] self.transitive_relation_concepts = [] self.relations_to_minimize = Thing('relations to minimize') self.conjectures = im.module.conjs self.view_state(self.node(0)) with self.ui_parent.run_context(): self.autodetect_transitive() self.have_cti = False
def get_diagram(goal, weaken=False): axioms = _ivy_interp.background_theory() d = ivy_solver.clauses_model_to_diagram( and_clauses(goal.node.clauses, goal.formula), is_skolem, # None, false_clauses(), axioms=axioms, weaken=weaken, ) return goal_at_arg_node(d, goal.node)
def get_diagram(goal, weaken=False): axioms = _ivy_interp.background_theory() d = ivy_solver.clauses_model_to_diagram( and_clauses(goal.node.clauses, goal.formula), is_skolem, #None, false_clauses(), axioms=axioms, weaken=weaken, ) return goal_at_arg_node(d, goal.node)
def start(self): ivy_ui.AnalysisGraphUI.start(self) self.node( 0).clauses = ilu.false_clauses() # just to make CG empty initially self.transitive_relations = [] self.transitive_relation_concepts = [] self.relations_to_minimize = Thing('relations to minimize') self.conjectures = im.module.conjs self.view_state(self.node(0)) with self.ui_parent.run_context(): self.autodetect_transitive() self.have_cti = False
def get_update_axioms(self, updated, action): # print "get_update_axioms: {}, {}".format(map(str,updated),action) for x in updated: if x in self.dependencies.symbols: updated = updated + [y for y in self.defines.symbols if y not in updated] try: precond, postcond = next(y for y in (p.match(action) for p in self.patterns.args) if y != None) except StopIteration: raise IvyError(action, "No matching update axiom for " + str(x)) postcond = state_to_action((updated, postcond, precond)) # print "update axioms: {}, {}, {}".format(map(str,postcond[0]),postcond[1],postcond[2]) return (updated, postcond[1], precond) return (updated, true_clauses(), false_clauses())
def action_update(self,domain,pvars): lit = self.args[0] n = lit.atom.relname new_n = new(n) args = lit.atom.args vs = sym_placeholders(n) eqs = [Atom(equals,[v,a]) for (v,a) in zip(vs,args) if not isinstance(a,Variable)] new_clauses = And(*([Or(sign(lit.polarity,Atom(new_n,vs)),sign(1-lit.polarity,Atom(n,vs))), sign(lit.polarity,Atom(new_n,args))] + [Or(*([sign(0,Atom(new_n,vs)),sign(1,Atom(n,vs))] + [eq])) for eq in eqs] + [Or(*([sign(1,Atom(new_n,vs)),sign(0,Atom(n,vs))] + [eq])) for eq in eqs])) new_clauses = formula_to_clauses(new_clauses) return ([n], new_clauses, false_clauses())
def get_update_axioms(self,updated,action): # print "get_update_axioms: {}, {}".format(map(str,updated),action) for x in updated: if x in self.dependencies.symbols: updated = updated + [y for y in self.defines.symbols if y not in updated] try: precond,postcond = next(y for y in (p.match(action) for p in self.patterns.args) if y != None) except StopIteration: raise IvyError(action,'No matching update axiom for ' + str(x)) postcond = state_to_action((updated,postcond,precond)) # print "update axioms: {}, {}, {}".format(map(str,postcond[0]),postcond[1],postcond[2]) return (updated,postcond[1],precond) return (updated,true_clauses(),false_clauses())
def int_update(self, domain, pvars): inst = self.args[0] if hasattr(domain, "macros"): im = instantiate_macro(inst, domain.macros) # print "im: {}".format(im) if im: # print im res = im.compile().int_update(domain, pvars) ## print "res: {}".format(res) return res if inst.relname in domain.schemata: clauses = domain.schemata[inst.relname].get_instance(inst.args) return ([], clauses, false_clauses()) raise IvyError(inst, "instantiation of undefined: {}".format(inst.relname))
def int_update(self,domain,pvars): inst = self.args[0] if hasattr(domain,'macros'): im = instantiate_macro(inst,domain.macros) # print "im: {}".format(im) if im: # print im res = im.compile().int_update(domain,pvars) ## print "res: {}".format(res) return res if inst.relname in domain.schemata: clauses = domain.schemata[inst.relname].get_instance(inst.args) return ([],clauses, false_clauses()) raise IvyError(inst,"instantiation of undefined: {}".format(inst.relname))
def action_update(self,domain,pvars): lhs = type_ast(domain,self.args[0]) n = lhs.rep new_n = new(n) args = lhs.args vs = [Variable("X%d" % i,s) for i,s in enumerate(n.sort.dom)] eqs = [eq_atom(v,a) for (v,a) in zip(vs,args) if not isinstance(a,Variable)] if is_atom(lhs): clauses = And(*([Or(Not(Atom(new_n,vs)),Atom(n,vs),eq) for eq in eqs] + [Or(Atom(new_n,vs),Not(Atom(n,vs)),eq) for eq in eqs])) elif is_individual_ast(lhs.rep): clauses = And(*[Or(eq_atom(type(lhs)(new_n,vs),type(lhs)(n,vs)),eq) for eq in eqs]) else: # TODO: ??? clauses = And() clauses = formula_to_clauses(clauses) return ([n], clauses, false_clauses())
def action_update(self,domain,pvars): lhs = self.args[0] n = lhs.rep new_n = new(n) args = lhs.args vs = [Variable("X%d" % i,s) for i,s in enumerate(n.sort.dom)] eqs = [eq_atom(v,a) for (v,a) in zip(vs,args) if not isinstance(a,Variable)] if is_atom(lhs): clauses = And(*([Or(Not(Atom(new_n,vs)),Atom(n,vs),eq) for eq in eqs] + [Or(Atom(new_n,vs),Not(Atom(n,vs)),eq) for eq in eqs])) elif is_individual_ast(lhs.rep): clauses = And(*[Or(eq_atom(type(lhs)(new_n,vs),type(lhs)(n,vs)),eq) for eq in eqs]) else: # TODO: ??? clauses = And() clauses = formula_to_clauses(clauses) return ([n], clauses, false_clauses())
def post_step(self,concept_spaces): if self.unsat: return false_clauses() self.inferred = [] # print "cs: {}".format(concept_spaces) for atom,cs in concept_spaces: if log: print "concept space: %s" % atom concepts = cs.enumerate(self.memo,self.test_cube) if log: print "result: {}".format([str(c) for c in concepts]) self.memo[atom.relname] = ([t.rep for t in atom.args], concepts) res = self.inferred if log: print "inferred: {}".format([[str(c) for c in cls] for cls in res]) del self.inferred return Clauses(res)
def post_step(self, concept_spaces): if self.unsat: return false_clauses() self.inferred = [] # print "cs: {}".format(concept_spaces) for atom, cs in concept_spaces: if log: print "concept space: %s" % atom concepts = cs.enumerate(self.memo, self.test_cube) if log: print "result: {}".format([str(c) for c in concepts]) self.memo[atom.relname] = ([t.rep for t in atom.args], concepts) res = self.inferred if log: print "inferred: {}".format([[str(c) for c in cls] for cls in res]) del self.inferred return Clauses(res)
def action_update(self, domain, pvars): lhs, rhs = self.args n = lhs.rep # Handle the hierarchical case if n in domain.hierarchy: asgns = [postfix_atoms_ast(self, Atom(x, [])) for x in domain.hierarchy[n]] res = unzip_append([asgn.action_update(domain, pvars) for asgn in asgns]) return res # If the lhs application is partial, make it total by adding parameters xtra = len(lhs.rep.sort.dom) - len(lhs.args) if xtra < 0: raise IvyError(self, "too many parameters in assignment to " + lhs.rep) if xtra > 0: extend = sym_placeholders(lhs.rep)[-xtra:] extend = variables_distinct_list_ast(extend, self) # get unused variables lhs = add_parameters_ast(lhs, extend) # Assignment of individual to a boolean is a special case if is_individual_ast(rhs) and not is_individual_ast(lhs): rhs = eq_atom(extend[-1], add_parameters_ast(rhs, extend[0:-1])) else: rhs = add_parameters_ast(rhs, extend) type_check(domain, rhs) if is_individual_ast(lhs) != is_individual_ast(rhs): # print type(lhs.rep) # print str(lhs.rep) # print type(lhs.rep.sort) # print "lhs: %s: %s" % (lhs,type(lhs)) # print "rhs: %s: %s" % (rhs,type(rhs)) raise IvyError(self, "sort mismatch in assignment to {}".format(lhs.rep)) new_n = new(n) args = lhs.args dlhs = new_n(*sym_placeholders(n)) vs = dlhs.args eqs = [eq_atom(v, a) for (v, a) in zip(vs, args) if not isinstance(a, Variable)] rn = dict((a.rep, v) for v, a in zip(vs, args) if isinstance(a, Variable)) drhs = substitute_ast(rhs, rn) if eqs: drhs = Ite(And(*eqs), drhs, n(*dlhs.args)) new_clauses = Clauses([], [Definition(dlhs, drhs)]) # print "assign new_clauses = {}".format(new_clauses) return ([n], new_clauses, false_clauses())
def null_update(): return SemActionValue([],true_clauses(),false_clauses())
def int_update(self, domain, pvars): result = [], false_clauses(), false_clauses() for a in self.args: result = join_action(result, a.int_update(domain, pvars), domain.relations) return result
def null_update(): return SemActionValue([], true_clauses(), false_clauses())
def action_update(self, domain, pvars): type_check(domain, self.args[0]) return ([], formula_to_clauses_tseitin(self.args[0]), false_clauses())
def null_update(): return ([], true_clauses(), false_clauses())
def bottom_state(): return pure_state(false_clauses())
def pure_state(clauses): return (None, clauses, false_clauses())
def pure_state(clauses): return (None,clauses,false_clauses())
def action_update(self, domain, pvars): type_check(domain, self.args[0]) check_can_assume(self.args[0], self) return SemActionValue([], formula_to_clauses_tseitin(self.args[0]), false_clauses())
def get_update_axioms(self,updated,action): defines = self.defn.args[0].rep if defines not in updated and any(x in self.dependencies for x in updated): updated.append(defines) return (updated,true_clauses(),false_clauses())
def action_update(self,domain,pvars): type_check(domain,self.args[0]) check_can_assume(self.args[0],self) return SemActionValue([],formula_to_clauses_tseitin(self.args[0]),false_clauses())
def null_update(): return ([],true_clauses(),false_clauses())
def action_update(self,domain,pvars): type_check(domain,self.args[0]) clauses = formula_to_clauses_tseitin(skolemize_formula(self.args[0])) clauses = unfold_definitions_clauses(clauses) return ([],clauses,false_clauses())
def action_update(self,domain,pvars): lhs,rhs = self.args n = lhs.rep # Handle the hierarchical case if n in domain.hierarchy: asgns = [postfix_atoms_ast(self,Atom(x,[])) for x in domain.hierarchy[n]] res = unzip_append([asgn.action_update(domain,pvars) for asgn in asgns]) return res # If the lhs application is partial, make it total by adding parameters xtra = len(lhs.rep.sort.dom) - len(lhs.args) if xtra < 0: raise IvyError(self,"too many parameters in assignment to " + lhs.rep) if xtra > 0: extend = sym_placeholders(lhs.rep)[-xtra:] extend = variables_distinct_list_ast(extend,self) # get unused variables lhs = add_parameters_ast(lhs,extend) # Assignment of individual to a boolean is a special case if is_individual_ast(rhs) and not is_individual_ast(lhs): rhs = eq_atom(extend[-1],add_parameters_ast(rhs,extend[0:-1])) else: rhs = add_parameters_ast(rhs,extend) lhs_vars = used_variables_ast(lhs) if any(v not in lhs_vars for v in used_variables_ast(rhs)): print self raise IvyError(self,"multiply assigned: {}".format(lhs.rep)) type_check(domain,rhs) if is_individual_ast(lhs) != is_individual_ast(rhs): # print type(lhs.rep) # print str(lhs.rep) # print type(lhs.rep.sort) # print "lhs: %s: %s" % (lhs,type(lhs)) # print "rhs: %s: %s" % (rhs,type(rhs)) raise IvyError(self,"sort mismatch in assignment to {}".format(lhs.rep)) # For a destructor assignment, we actually mutate the first argument if n.name in ivy_module.module.destructor_sorts: mut = lhs.args[0] rest = list(lhs.args[1:]) mut_n = mut.rep nondet = mut_n.suffix("_nd").skolem() new_clauses = mk_assign_clauses(mut_n,nondet(*sym_placeholders(mut_n))) fmlas = [] nondet_lhs = lhs.rep(*([nondet(*mut.args)]+rest)) fmlas.append(equiv_ast(nondet_lhs,rhs)) vs = sym_placeholders(n) dlhs = n(*([nondet(*mut.args)] + vs[1:])) drhs = n(*([mut] + vs[1:])) eqs = [eq_atom(v,a) for (v,a) in zip(vs,lhs.args)[1:] if not isinstance(a,Variable)] if eqs: fmlas.append(Or(And(*eqs),equiv_ast(dlhs,drhs))) for destr in ivy_module.module.sort_destructors[mut.sort.name]: if destr != n: phs = sym_placeholders(destr) a1 = [nondet(*mut.args)] + phs[1:] a2 = [mut] + phs[1:] fmlas.append(eq_atom(destr(*a1),destr(*a2))) new_clauses = and_clauses(new_clauses,Clauses(fmlas)) return ([mut_n], new_clauses, false_clauses()) new_clauses = mk_assign_clauses(lhs,rhs) # print "assign new_clauses = {}".format(new_clauses) return ([n], new_clauses, false_clauses())
def pure_state(clauses): return SemStateValue(None,clauses,false_clauses())
def pure_state(clauses): return SemStateValue(None, clauses, false_clauses())
def get_update_axioms(self, updated, action): defines = self.defn.args[0].rep if defines not in updated and any(x in self.dependencies for x in updated): updated.append(defines) return (updated, true_clauses(), false_clauses())
def int_update(self,domain,pvars): return ([], true_clauses(), false_clauses())
import ivy_ast import display_sample as ds import ivy_utils as iu # <TODO> No such assumption as universe cannot be empty # <TODO> support for enumerated sort # <TODO> dictionary mapping Var to its assigned number # <TODO> action Clause for positive sample is different from Neg sample module = None featureset = [] numVarFS = { } # number of Variables(quant.) of each sort in feature set (invariant: equal to max number of quantified Var that could be present in CandInv) numVarInv = { } # number of variables of each sort in learned as well as previous invariant. Used for determining instance size candInv = true_clauses() coincide = false_clauses() silent = False # if True then silent the print statement in sat checker (ivy backend) enum = iu.BooleanParameter("enum", False) ''' :returns a tuple containing universe and pure state. pure state is again a tuple with 2nd element a clause repr that state ''' def sampleUtil(mod, preclause, fcs, actname): ''' a sample created by using z3 which satsfies preclauses and final condition(fcs) where transtion function is obtained from action corresponding to actname ''' action = mod.actions[actname] ag = ivy_art.AnalysisGraph()