def decompose(self, pre, post, fail=False): v = self.get_callee() if not isinstance(v, Action): return [] actual_params = self.args[0].args actual_returns = self.args[1:] vocab = list(symbols_asts(actual_params + actual_returns)) formals = v.formal_params + v.formal_returns premap, pre = hide_state_map(formals, pre) postmap, post = hide_state_map(formals, post) actual_params = [rename_ast(p, premap) for p in actual_params] actual_returns = [rename_ast(p, postmap) for p in actual_returns] pre = constrain_state( pre, And(* [Equals(x, y) for x, y in zip(actual_params, v.formal_params)])) if not fail: post = constrain_state( post, And(*[ Equals(x, y) for x, y in zip(actual_returns, v.formal_returns) ])) ren = dict((x, x.prefix('__hide:')) for x in actual_returns) post = (post[0], rename_clauses(post[1], ren), post[2]) callee = v.clone(v.args) # drop the formals res = [(pre, [callee], post)] print "decompose call:" print "pre = {}".format(pre) print "callee = {}".format(callee) print "post = {}".format(post) return res
def concepts_from_sig(symbols, concepts): for c in sorted(symbols, key=str): dom, rng = c.sort.dom, c.sort.rng if il.is_enumerated_sort(rng): if len(dom) in [0, 1, 2]: if len(dom) == 0: if c in il.sig.constructors: continue # Hack: we treat enumerated constants as labels on the bogus sort "unit" vs, xvs = [], [Variable('X', create_unit_sort(concepts))] else: vs, xvs = [ Variable(n, s) for n, s in zip(['X', 'Y'], dom) ], None ec = concept_from_formula(c(*vs), vs=xvs) concepts['enum'].append(ec.name) concepts[ec.name] = enum_concepts(ec.name, vs, c(*vs)) for cons in rng.constructors: c1 = add_domain_concept_fmla(concepts, Equals(c(*vs), cons), kind='enum_case', vs=xvs) concepts[ec.name].append(c1.name) elif il.is_boolean_sort(rng): # TODO: we have no way to display boolean constants if len(dom) in [0, 1, 2, 3]: if len(dom) == 0: if c in il.sig.constructors: continue # Hack: we treat boolean constants as labels on the bogus sort "unit" vs, xvs = [], [Variable('X', create_unit_sort(concepts))] else: vs, xvs = [ Variable(n, s) for n, s in zip(['X', 'Y', 'Z'], dom) ], None add_domain_concept_fmla(concepts, c(*vs), vs=xvs) elif il.is_first_order_sort(rng): if len(dom) == 0: add_domain_concept_fmla(concepts, Equals(Variable('X', rng), c)) elif len(dom) in [1, 2]: vs = [ Variable(n, s) for n, s in zip(['X', 'Y', 'Z'], dom + (rng, )) ] fmla = Equals(c(*vs[0:-1]), vs[-1]) add_domain_concept_fmla(concepts, fmla)
def subactions(self) : if isinstance(self.args[0],ivy_ast.Some): ps = list(self.args[0].params()) fmla = self.args[0].fmla() vs = [Variable('V{}'.format(idx),x.sort) for idx,x in enumerate(ps)] subst = dict((c,v) for c,v in zip(ps,vs)) sfmla = substitute_constants_ast(fmla,subst) if isinstance(self.args[0],ivy_ast.SomeMinMax): idx = self.args[0].index() if idx not in ps: ltsym = Symbol('<',RelationSort([idx.sort,idx.sort])) operator = lambda x,y: Not(ltsym(x,y)) ivar = substitute_constants_ast(idx,subst) comp = operator(ivar,idx) if isinstance(self.args[0],ivy_ast.SomeMin) else operator(idx,ivar) fmla = And(fmla,Implies(sfmla,comp)) else : leqsym = Symbol('<=',RelationSort([idx.sort,idx.sort])) operator = lambda x,y: And(leqsym(x,y),Not(Equals(x,y))) ivar = next(v for p,v in zip(ps,vs) if p == idx) comp = operator(ivar,idx) if isinstance(self.args[0],ivy_ast.SomeMin) else operator(idx,ivar) fmla = And(fmla,Not(And(sfmla,comp))) if_part = LocalAction(*(ps+[Sequence(AssumeAction(fmla),self.args[1])])) else_action = self.args[2] if len(self.args) >= 3 else Sequence() else_part = Sequence(AssumeAction(Not(sfmla)),else_action) # iu.dbg('if_part') # iu.dbg('else_part') else: if not is_boolean(self.args[0]): raise IvyError(self,'condition must be boolean') if_part = Sequence(AssumeAction(self.args[0]),self.args[1]) else_action = self.args[2] if len(self.args) >= 3 else Sequence() else_part = Sequence(AssumeAction(dual_formula(self.args[0])),else_action) return if_part,else_part
def mk_variant_assign_clauses(lhs, rhs): n = 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) nondet = n.suffix("_nd").skolem() if eqs: nondet = Ite(And(*eqs), nondet, n(*dlhs.args)) lsort, rsort = lhs.sort, rhs.sort fmlas = [ Iff( pto(lsort, rsort)(dlhs, Variable('X', rsort)), Equals(Variable('X', rsort), drhs)) ] for s in ivy_module.module.variants[lsort.name]: if s != rsort: fmlas.append(Not(pto(lsort, s)(dlhs, Variable('X', s)))) new_clauses = Clauses(fmlas, [Definition(dlhs, nondet)]) return new_clauses
def initial_concept_domain(sorts): concepts = empty_concepts() # add one node for each sort for sort in sorts: X = Variable('X', sort) add_domain_concept_fmla(concepts, Equals(X, X), kind='nodes') return make_concept_domain(concepts)
def create_unit_sort(concepts): sort = il.UninterpretedSort('unit') if 'unit' not in il.sig.sorts: il.sig.sorts['unit'] = sort if 'has_unit' not in concepts: # HACK: will this damage anything? X = Variable('X', sort) add_domain_concept_fmla(concepts, Equals(X, X), kind='nodes') concepts['has_unit'] = [] return il.sig.sorts['unit']
def splatter(self, node, constants=None): if constants == None: constants = ilu.used_constants_clauses(self.constraints) concepts = self.concept_session.domain.concepts name = node.name + '.splatter' concepts[name] = enum_concepts(name, None, None) for cons in constants: c1 = concept_from_formula(Equals(Variable('X', node.sort), cons)) concepts[c1.name] = c1 concepts[name].append(c1.name) self.concept_session.domain.split(node.name, name) self.recompute()
def concepts_from_sig(symbols, concepts): for c in sorted(symbols, key=str): dom, rng = c.sort.dom, c.sort.rng if il.is_enumerated_sort(rng): # TODO: we have no way to display enumerated constants if len(dom) in [1, 2]: vs = [Variable(n, s) for n, s in zip(['X', 'Y'], dom)] ec = concept_from_formula(c(*vs)) concepts['enum'].append(ec.name) concepts[ec.name] = enum_concepts(ec.name, vs, c(*vs)) for cons in rng.constructors: c1 = add_domain_concept_fmla(concepts, Equals(c(*vs), cons), kind='enum_case') concepts[ec.name].append(c1.name) elif il.is_boolean_sort(rng): # TODO: we have no way to display boolean constants if len(dom) in [1, 2, 3]: vs = [Variable(n, s) for n, s in zip(['X', 'Y', 'Z'], dom)] add_domain_concept_fmla(concepts, c(*vs)) elif il.is_first_order_sort(rng): if len(dom) == 0: add_domain_concept_fmla(concepts, Equals(Variable('X', rng), c)) elif len(dom) in [1, 2]: vs = [ Variable(n, s) for n, s in zip(['X', 'Y', 'Z'], dom + (rng, )) ] fmla = Equals(c(*vs[0:-1]), vs[-1]) add_domain_concept_fmla(concepts, fmla)
def action_update(self,domain,pvars): l,f,r = self.args return make_field_update(self,l,f,lambda v: Equals(v,r),domain,pvars)
def witness_concept(c): return concept_from_formula(Equals(Variable('X', c.sort), c))