如何在不求值的情况下将表达式作为参数传递给函数

时间:2020-04-11 15:39:39

标签: python-3.x z3 z3py

首先,请参阅运行z3-solver的相关用户代码。这可以正常工作,并且求解器在计算表达式之后返回一些值:

# see source at https://github.com/Z3Prover/z3
# the relevant code is at: z3-master/src/api/python/z3/z3.py
# pip install z3-solver
from z3 import *
x = Int('x')
#print(type(x)) # <class 'z3.z3.ArithRef'>
y = Int('y')
s = Solver()
# print(type(s)) # <class 'z3.z3.Solver'>
s.add(x > 10)  # this works
s.add(y == x + 2)
print(s)
print(s.check())
print(s.model())

现在,我简短地尝试编写像z3这样的评估器:

class Int():
    def __init__(self,name):
        self.name = name
        self.value = None
    def do_print(self):
        print('Rand({})'.format(self.name))
    def __str__(self):
        return(str(self.value))

class Solver():
    def add(self, *argv):
        self.args = []
        for arg in argv:
            self.args.append(arg)
    def do_print(self):
        print('Constr(',end='')
        for arg in self.args:
            print(arg)
        print(')')

x = Int('x')
s=Solver()
s.add(x > 10) # this does not work - but why is same kind of code in z3 working?
# err: TypeError: '<' not supported between instances of 'Int' and 'int'
x.do_print()
s.do_print()

我在s.add()行上遇到错误。我理解错误,即对象“ x”不能与整数进行比较。之所以发生这种情况,是因为在调用s.add()之前对这一行中的表达式进行了求值。

但是我的问题是-z3-solver如何绕过此错误?怎样在不先评估的情况下将args传递给add()?

如何解决代码中的问题,并将表达式传递给s.add()而不先对其求值?

0 个答案:

没有答案
相关问题