python const无法使用_python – 如何检查z3中的const是变量还是值?

对整数执行此操作的一种方法是使用is_int和is_int_value:

x = Int('x')

print "x types"

print is_int(x) # true, is of sort int

print is_int_value(x) # false, not a "value"

x_ = IntVal(7)

print "x_ types"

print is_int(x_) # true, is also of sort int

print is_int_value(x_) # true, is a value

对于实数,你可以使用is_real来检查变量排序,并使用(分离)is_algebraic_value和is_rational_value作为值(我没有在API中看到像is_real_value这样的函数,但我认为这个析取将会这样做).对于bitvectors,可以使用is_bv_value作为值,使用is_bv检查变量sort.

.NET API具有Expr.IsNumeral,您可以在此处查看这些是如何在API中实现的(例如,Expr.IsIntNum [相当于Python is_int_value]检查Expr.IsNumeral和Expr.IsInt是否都为真):http://research.microsoft.com/en-us/um/redmond/projects/z3/_expr_8cs_source.html

我没有立即看到为自定义枚举排序执行此操作的方法.作为一种替代方法,您可以使用bitvectors对枚举进行编码,并使用is_bv_value比较变量/值.但是,作为更好的解决方法,您似乎需要使用更一般的代数数据类型及其自动创建的“识别器”.如果您将它们声明为枚举排序,Python API似乎无法正确创建识别器.这是实现有效枚举排序(但声明为更通用的数据类型)的一种方法.

ColorVal = Datatype('ColorVal')

ColorVal.declare('white')

ColorVal.declare('black')

ColorVal = ColorVal.create()

mycolor = Const("mycolor", ColorVal)

print ColorVal.recognizer(0) # is_white

print ColorVal.recognizer(1) # is_black

print simplify(ColorVal.is_white(mycolor)) # returns is_white(mycolor)

print simplify(ColorVal.is_black(mycolor)) # returns is_black(mycolor)

mycolorVal = ColorVal.white # set to value white

print simplify(ColorVal.is_white(mycolorVal)) # true

print simplify(ColorVal.is_black(mycolorVal)) # false

# compare "variable" versus "value" with return of is_white, is_black, etc.: if it gives a boolean value, it's a value, if not, it's a variable

print "var vs. value"

x = simplify(ColorVal.is_white(mycolor))

print is_true(x) or is_false(x) # returns false, since x is is_white(mycolor)

y = simplify(ColorVal.is_white(mycolorVal))

print is_true(y) or is_false(y) # true

ColorValEnum, (whiteEnum,blackEnum) = EnumSort("ColorValEnum",["whiteEnum","blackEnum"])

mycolorEnum = Const("mycolorEnum",ColorValEnum)

print ColorValEnum.recognizer(0) # is_whiteEnum

print ColorValEnum.recognizer(1) # is_blackEnum

# it appears that declaring an enum does not properly create the recognizers in the Python API:

#print simplify(ColorValEnum.is_whiteEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_whiteEnum'

#print simplify(ColorValEnum.is_blackEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_blackEnum'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值