def extra_concepts(self): tn = self.text_name() X, Y = self.variable('X'), self.variable('Y') c = [ Literal(1, Atom(tn, [X])), ~eq_lit(X, Y), Literal(1, Atom(tn, [Y])) ] cs = [ProductSpace([NamedSpace(x) for x in c])] w = get_witness(self) if w: cls = [[~lit, eq_lit(X, w)] for lit in self.fmla] cs += [ProductSpace([NamedSpace(x) for x in c]) for c in cls] return cs
def update_frame_constraint(update, relations): """ Return a clause list constraining all updated symbols to keep their previous values """ clauses = [] for sym in update[0]: if sym in relations: arity = relations[sym] vs = [Variable("V{}".format(i)) for i in range(0, arity)] lit1 = Literal(1, Atom(sym, vs)) lit2 = Literal(1, Atom(new(sym), vs)) clauses += [[~lit1, lit2], [lit1, ~lit2]] else: clauses.append([eq_lit(Constant(sym), Constant(new(sym)))]) return Clauses(clauses)
def get_solver_clauses(self): # expr = self.state_as_z3_expr if use_ivy_alpha: d = ProgressiveDomain(verbose=True) self.post = d sorts = sorted(set(n.sort for n in self.all_nodes)) concepts = [] for s in sorts: nconcepts = [n.to_concept_space() for n in self.all_nodes] nconcepts.append( (node_concept(s, 'X').atom, SumSpace( [NamedSpace(Literal(1, x)) for x, y in nconcepts]))) nconcepts.append( (xtra_concept(s, 'X', 'Y').atom, SumSpace([ x for n in self.all_nodes for x in n.extra_concepts() ]))) concepts += nconcepts concepts = concepts + self.relation_concepts([ r for r in self.relations if r.name() in self.enabled_relations ]) d.concept_spaces = concepts # # print "concepts: %s" % concepts d.post_init(self.state, [], {}, []) clauses = self.compile_concepts(concepts) # # print "clauses: %s" % clauses else: clauses = self.state # print "graph solver clauses = {}".format(clauses) s_add(self.solver, clauses_to_z3(clauses)) self.solver_clauses = and_clauses(self.solver_clauses, clauses)
def check_node(self, n): x = var_to_skolem('__', Variable('X', n.sort)).suffix(str(n.sort)) y = var_to_skolem('__', Variable('Y', n.sort)).suffix(str(n.sort)) # print "x.sort: {}",format(x.sort) self.solver.push() s = self.solver # if we have a witness we can show node is definite (present in all models) wit = get_witness(n) # print "checking: {}".format(n.fmla) cube = substitute_clause(n.fmla, {'X': x}) # print "cube: {!r}".format(cube) # print wit # if wit != None: ## print "wit: {}, wit.sort: {}, x.sort: {}".format(wit,wit.sort,x.sort) res = s_check_cube(s, cube, (Atom(equals, [x, wit]) if wit != None else None)) ## print"check cube: %s = %s" % (cube,res) # res = s_check_cube(s,substitute_clause(n.fmla,{'X':x})) # print "status: {}".format(res) n.status = res s_add(s, cube_to_z3(substitute_clause(n.fmla, {'X': x}))) s_add(s, cube_to_z3(substitute_clause(n.fmla, {'X': y}))) s_add(s, cube_to_z3([Literal(0, Atom(equals, [x, y]))])) n.summary = s.check() != z3.unsat self.solver.pop()
def get_definite_facts(self, names, tvals): edges = self.get_edges() return [[ substitute_lit( Literal(1 if status == 'true' else 0, self.rel_lit.atom), {'X': get_witness(p1)}) ] for (p1, ), status in edges if status != 'undef' and status in tvals and p1.name in names]
def rela_fact(polarity, relname, n1, n2): return ([~lit for lit in fmla1] + [ ~lit for lit in substitute_clause(n2.fmla, {'X': Variable('Y', n2.sort)}) ] + [ Literal( polarity, Atom(relname, [Variable('X', n1.sort), Variable('Y', n2.sort)])) ])
def split(self, node, p): if isinstance(p, tuple): self.split_n_way(node, [eq_lit(p[0], x) for x in p[1]]) return label = make_lit_label(p) posname = node.name + "+" + label negname = node.name + "-" + label self.all_nodes = [n for n in self.all_nodes if n is not node] self.all_nodes.append(GraphNode(posname, node.fmla + [p], node.sort)) neg_p = Literal(1 - p.polarity, p.atom) self.all_nodes.append( GraphNode(negname, node.fmla + [neg_p], node.sort)) self.needs_recompute = True
def relation_concepts(self, relations): rcons = [r.to_concept_space() for r in relations] rprods = [ ProductSpace([ NamedSpace(x) for x in [ node_concept(r.sorts[0], 'X'), Literal(1, cs[0]), node_concept(r.sorts[1], 'Y') ] ]) for (r, cs) in zip(relations, rcons) ] rsp = to_atom('__rsp(X,Y)'), SumSpace(rprods) return rcons + [rsp]
def set_state(self, clauses, recomp=True): self.state = clauses self.solver_clauses = true_clauses() self.predicates = self.get_predicates(clauses) sig = self.parent_state.domain.sig ufs = [ x for x, arity in self.parent_state.domain.functions.iteritems() if arity == 1 and not has_enumerated_sort(sig, x) ] if not hasattr(self, 'brels'): self.brels = ([ self.make_rel_lit(r, ['X', 'Y']) for r, arity in self.parent_state.domain.all_relations if arity == 2 ] + [ Literal( 1, Atom(equals, [ App(f, Variable('X', f.sort.dom[0])), Variable('Y', f.sort.rng) ])) for f in ufs ]) # brels = list(used_binary_relations_clauses(clauses)) # brels = [r for r in brels if ((r != equals) and not r.startswith('__'))] ## print "brels: %s" % brels self.relations = [GraphRelation(self, rel) for rel in self.brels] self.relations += [ GraphRelationUnary(self, rel) for rel in self.predicates if not isinstance(rel, tuple) and used_variables_ast(rel) ] self.relations += [ GraphFunctionUnary(self, Literal(1, rel[0])) for rel in self.predicates if isinstance(rel, tuple) ] self.needs_recompute = True if recomp: self.recompute()
def get_predicates(self, clauses): # print "get_predicates: {}".format(clauses) d = self.parent_state.domain sig = d.sig urs = [ x for x in used_unary_relations_clauses(clauses) if not is_skolem(x) ] cs = [ x for x in used_constants_clauses(clauses) if not is_skolem(x) and not has_enumerated_sort(sig, x) and not x.is_numeral() ] ufs = [ x for x in used_unary_functions_clauses(clauses) if not is_skolem(x) and has_enumerated_sort(sig, x) ] nrs = [x for x, arity in d.relations.iteritems() if arity == 0] union_to_list( urs, [x for x, arity in d.relations.iteritems() if arity == 1]) union_to_list(cs, [ x for x, arity in d.functions.iteritems() if arity == 0 and not has_enumerated_sort(sig, x) ]) union_to_list(ufs, [ x for x, arity in d.functions.iteritems() if arity == 1 and has_enumerated_sort(sig, x) ]) # print "ufs: {}".format(ufs) ccs = [Constant(c) for c in cs] # print "sorts: {}".format([(c,c.get_sort()) for c in ccs]) return ([Literal(1, Atom(c, [])) for c in nrs] + [ Literal(1, Atom(equals, [Variable("X", c.get_sort()), c])) for c in ccs ] + [Literal(1, Atom(r, [Variable("X", r.sort.dom[0])])) for r in urs] + [(App(f, Variable('X', f.sort.dom[0])), [ Constant(Symbol(x, f.sort.rng)) for x in f.sort.rng.defines() ]) for f in ufs])
def make_rel_lit(self, rel, varnames): args = [Variable(v, t) for v, t in zip(varnames, rel.sort.dom)] return Literal(1, Atom(rel, args))
def xtra_concept(sort, vn1, vn2): return Literal( 1, Atom('__xtra:' + sort, [Variable(vn1, sort), Variable(vn2, sort)]))
def node_concept(sort, varname): return Literal(1, Atom('__node:' + sort, [Variable(varname, sort)]))
def status_lit(self, status): return Literal(1, Atom(equals, [self.fmla, status]))