Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating new expression from an existing expression in Z3
    primarykey
    data
    text
    <p>Given an expression f , I want to replace all variables <code>v</code> in <code>f</code> with something like <code>v_d</code> (<code>d</code> is a number). Currently I have to go through the following steps to achieve this </p> <ol> <li>Obtain all the variables in <code>f</code></li> <li>For each variable <code>v</code> in <code>f</code>, determine its sort, then generate the appropriate <code>v_d</code> var. </li> <li>do the substitution</li> </ol> <p>Since I am still trying to learn Z3 python so probably there is a much better way to do the above? </p> <p>Thanks.</p> <p>My full code is listed below:</p> <pre><code>def get_vars(f,rs=[]): """obtain variables from f""" if is_const(f): try: f.as_string() #to differentiate btw Int('x') and IntVal('3') return rs except AttributeError: return vset(rs + [f],str) else: for f_ in f.children(): rs = get_vars(f_,rs) return vset(rs,str) def gen_vars(vs,i): """ Generates a new set of variables E.g. v =&gt; v_i , v_next =&gt; v_(i+1) """ assert i&gt;=0 def mk_name(i,v): next_kw = '_next' if str(v).endswith(next_kw): astr = str(v)[:-len(next_kw)] + '_' + str(i+1) else: astr = str(v) + '_' + str(i) return astr def mk_var(name,vsort): if vsort.kind() == Z3_INT_SORT: v = Int(name) elif vsort.kind() == Z3_REAL_SORT: v = Real(name) elif vsort.kind() == Z3_BOOL_SORT: v = Bool(name) elif vsort.kind() == Z3_DATATYPE_SORT: v = Const(name,vsort) else: assert False, 'Cannot handle this sort yet (id: %d)'%vsort.kind() return v #so that the longest one is replaced first vs = sorted(vs,key=str,reverse=True) names = [mk_name(i,v) for v in vs] vs_ = [mk_var(ns,v.sort()) for ns,v in zip(names,vs)] vss = zip(vs,vs_) return vss def substitute_f(f,i,vs=None): """ Replaces all variables v in f with v_(i*_) vs: variables in f (could be automatically obtained from f) """ assert is_expr(f) assert isinstance(vs,list) or vs is None if vs is None: vs = get_vars(f) vss = gen_vars(vs,i) f_ = f for pvs in vss: f_ = substitute(f_,pvs) return f_ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload