4

数式で関数記号を(別の関数で)置き換えるための最良の方法は何ですか?Z3pysubstituteは式でのみ機能するようです。現在、関数を適用できるconsts / varsの可能なすべての組み合わせを推測し、それらを別の関数のアプリケーションに置き換えようとしています。それを行うためのより良い方法はありますか?

4

1 に答える 1

4

s用語を指定すると、関数fと用語がのすべてのアプリケーションをにt置き換える単純なボトムアップリライターを実装fできます。の自由変数を項、...、 。に置き換えて得られる項を表すために表記を使用しています。f(r_1, ..., r_n)st[r_1, ..., r_n]t[r_1, ..., r_n]tr_1r_n

リライターはZ3APIを実装できます。AstMap結果をキャッシュするためにを使用し、todoまだ処理する必要のある式を格納するためにリストを使用します。

fこれは、フォームの-applicationsf(t)g(t+1)inに置き換える簡単な例ですs

x = Var(0, IntSort())
print rewrite(s, f, g(x + 1))

これがコードとその他の例です。注意してください、私はいくつかの例のセットでのみコードをテストしました。

from z3 import *

def update_term(t, args):
    # Update the children of term t with args. 
    # len(args) must be equal to the number of children in t.
    # If t is an application, then len(args) == t.num_args()
    # If t is a quantifier, then len(args) == 1
    n = len(args)
    _args = (Ast * n)()
    for i in range(n):
        _args[i] = args[i].as_ast()
    return z3._to_expr_ref(Z3_update_term(t.ctx_ref(), t.as_ast(), n, _args), t.ctx)

def rewrite(s, f, t):
    """
    Replace f-applications f(r_1, ..., r_n) with t[r_1, ..., r_n] in s.
    """
    todo = [] # to do list
    todo.append(s)
    cache = AstMap(ctx=s.ctx)
    while todo:
        n = todo[len(todo) - 1]
        if is_var(n):
            todo.pop()
            cache[n] = n
        elif is_app(n):
            visited  = True
            new_args = []
            for i in range(n.num_args()):
                arg = n.arg(i)
                if not arg in cache:
                    todo.append(arg)
                    visited = False
                else:
                    new_args.append(cache[arg])
            if visited:
                todo.pop()
                g = n.decl()
                if eq(g, f):
                    new_n = substitute_vars(t, *new_args)
                else:
                    new_n = update_term(n, new_args)
                cache[n] = new_n
        else:
            assert(is_quantifier(n))
            b = n.body()
            if b in cache:
                todo.pop()
                new_n = update_term(n, [ cache[b] ])
                cache[n] = new_n
            else:
                todo.append(b)
    return cache[s]

f = Function('f', IntSort(), IntSort())
a, b = Ints('a b')
s = Or(f(a) == 0, f(a) == 1, f(a+a) == 2)
# Example 1: replace all f-applications with b
print rewrite(s, f, b)

# Example 2: replace all f-applications f(t) with g(t+1)
g = Function('g', IntSort(), IntSort())
x = Var(0, IntSort())
print rewrite(s, f, g(x + 1))

# Now, f and g are binary functions.
f = Function('f', IntSort(), IntSort(), IntSort())
g = Function('g', IntSort(), IntSort(), IntSort())

# Example 3: replace all f-applications f(t1, t2) with g(t2, t1)
s = Or(f(a, f(a, b)) == 0, f(b, a) == 1, f(f(1,0), 0) == 2)
# The first argument is variable 0, and the second is variable 1.
y = Var(1, IntSort())
print rewrite(s, f, g(y, x))

# Example 4: quantifiers
s = ForAll([a], f(a, b) >= 0)
print rewrite(s, f, g(y, x + 1))
于 2013-03-06T01:20:50.360 に答える