def map_fmla(fmla, strat_map): if il.is_binder(fmla): return map_fmla(fmla.body, strat_map) if il.is_variable(fmla): if fmla not in strat_map: res = UFNode() res.variables.add(fmla) strat_map[fmla] = res return strat_map[fmla] nodes = [map_fmla(f, strat_map) for f in fmla.args] if il.is_eq(fmla): unify(*nodes) if il.is_interpreted_sort(fmla.args[0].sort): unify(strat_map[(fmla.rep, 0)], nodes[0]) return None if il.is_ite(fmla): unify(*nodes[1:]) return nodes[1] if il.is_app(fmla): func = fmla.rep if func in symbols_over_universals or True: for idx, node in enumerate(nodes): if node is not None: unify(strat_map[(func, idx)], node) return strat_map[func] return None
def map_fmla(fmla,strat_map): if il.is_binder(fmla): return map_fmla(fmla.body,strat_map) if il.is_variable(fmla): if fmla not in strat_map: res = UFNode() res.variables.add(fmla) strat_map[fmla] = res return strat_map[fmla] nodes = [map_fmla(f,strat_map) for f in fmla.args] if il.is_eq(fmla): unify(*nodes) if il.is_interpreted_sort(fmla.args[0].sort): unify(strat_map[(fmla.rep,0)],nodes[0]) return None if il.is_ite(fmla): unify(*nodes[1:]) return nodes[1] if il.is_app(fmla): func = fmla.rep if func in symbols_over_universals or True: for idx,node in enumerate(nodes): if node is not None: unify(strat_map[(func,idx)],node) return strat_map[func] return None
def apply_match_rec(match,fmla,env): args = [apply_match_rec(match,f,env) for f in fmla.args] if il.is_app(fmla): if fmla.rep in match: func = match[fmla.rep] return func(*args) return apply_match_func(match,fmla.rep)(*args) if il.is_variable(fmla) and fmla in match: return match[fmla] if il.is_binder(fmla): with il.BindSymbols(env,fmla.variables): fmla = fmla.clone_binder([apply_match_rec(match,v,env) for v in fmla.variables],args[0]) return fmla return fmla.clone(args)
def apply_match(match,fmla): """ apply a match to a formula. In effect, substitute all symbols in the match with the corresponding lambda terms and apply beta reduction """ args = [apply_match(match,f) for f in fmla.args] if il.is_app(fmla): if fmla.rep in match: func = match[fmla.rep] return func(*args) elif il.is_binder(fmla): vs = [apply_match(match,v) for v in fmla.variables] return fmla.clone_binder(vs,apply_match(match,fmla.body)) elif il.is_variable(fmla): return il.Variable(fmla.name,match.get(fmla.sort,fmla.sort)) return fmla.clone(args)
def apply_match_alt_rec(match,fmla,env): args = [apply_match_alt_rec(match,f,env) for f in fmla.args] if il.is_app(fmla): if fmla.rep in match: return apply_fun(match_get(match,fmla.rep,env),args) func = apply_match_func(match,fmla.rep) func = match_get(match,func,env,func) return func(*args) if il.is_variable(fmla): if fmla in match: return match_get(match,fmla,env) fmla = il.Variable(fmla.name,apply_match_sort(match,fmla.sort)) fmla = match_get(match,fmla,env,fmla) return fmla if il.is_binder(fmla): with il.BindSymbols(env,fmla.variables): fmla = fmla.clone_binder([apply_match_alt_rec(match,v,env) for v in fmla.variables],args[0]) return fmla return fmla.clone(args)
def map_fmla(lineno, fmla, pol): """ Add all of the subterms of `fmla` to the stratification graph. """ global universally_quantified_variables global macro_var_map global macro_dep_map global macro_map global macro_val_map global strat_map global arcs if il.is_binder(fmla): return map_fmla(lineno, fmla.body, pol) if il.is_variable(fmla): if fmla in universally_quantified_variables: if fmla not in strat_map: res = UFNode() strat_map[fmla] = res return strat_map[fmla], set() node, vs = macro_var_map.get(fmla, None), macro_dep_map.get(fmla, set()) return node, vs reses = [ map_fmla(lineno, f, il.polar(fmla, pos, pol)) for pos, f in enumerate(fmla.args) ] nodes, uvs = iu.unzip_pairs(reses) all_uvs = iu.union_of_list(uvs) all_uvs.update(n for n in nodes if n is not None) if il.is_eq(fmla): if not il.is_interpreted_sort(fmla.args[0].sort): S_sigma = strat_map[il.Symbol('=', fmla.args[0])] for x, uv in zip(nodes, uvs): if x is not None: unify(x, S_sigma) arcs.extend((v, S_sigma, fmla, lineno) for v in uv) else: check_interpreted(fmla, nodes, uvs, lineno, pol) return None, all_uvs if il.is_ite(fmla): # S_sigma = strat_map[il.Symbol('=',fmla.args[1])] # for x,uv in zip(nodes[1:],uvs[1:]): # if x is not None: # unify(x,S_sigma) # arcs.extend((v,S_sigma,fmla,lineno) for v in uv) # TODO: treat ite as pseudo-macro: does this work? if nodes[1] and nodes[2]: unify(*nodes[1:]) return nodes[1] or nodes[2], all_uvs if il.is_app(fmla): func = fmla.rep if not il.is_interpreted_symbol(func): if func in macro_value_map: return macro_value_map[func] if func in macro_map: defn, lf = macro_map[func] res = map_fmla(lf.lineno, defn.rhs(), None) macro_value_map[func] = res return res for idx, node in enumerate(nodes): anode = strat_map[(func, idx)] if node is not None: unify(anode, node) arcs.extend((v, anode, fmla, lineno) for v in uvs[idx]) else: check_interpreted(fmla, nodes, uvs, lineno, pol) return None, all_uvs return None, all_uvs