python - 如何在 Z3Py 的列表中使用 Empty 方法?
问题描述
在 z3py 中,我想在 Z3py 中使用 Empty 函数(https://z3prover.github.io/api/html/z3py_8py_source.html#l09944)
我试着这样做:
s = Solver()
# declare a sequence of integers
iseq = Const('iseq', SeqSort(IntSort()))
solve(Empty(iseq)!= True)
# get a model and print it:
if s.check() == sat:
print (s.model())
但我返回“Z3Exception:传递给 Empty 的非序列、非正则表达式排序”
我还尝试 Empty(iseq) 只支持我一个空序列,但它对我没有用
解决方案
这里发生了一些事情:
您正在通过 声明求解器对象
s = Solver ()
,但随后您正在调用该solve
函数。solve
创建自己的求解器。只需使用即可s.add
。Empty
给定排序,创建一个序列。你不能调用它iseq
。这就是您收到的错误消息。
我猜你想说的是: declare iseq
,确保它不是空的。您将编写如下代码:
from z3 import *
s = Solver()
# declare a sequence of integers
iseq = Const('iseq', SeqSort(IntSort()))
# assert it's not empty
s.add (Length(iseq) != 0)
# get a model and print it:
if s.check() == sat:
print (s.model())
z3 说:
$ python a.py
[iseq = Unit(2)]
所以,它给了你一个模型,其中iseq
包含数字的单例序列2
;它不是空的,它满足我们提出的约束。
Empty
这是一个用于创建空序列的示例:
from z3 import *
s = Solver()
# Give a name to integer sequences
ISeq = SeqSort(IntSort())
# declare a sequence of integers
iseq = Const('iseq', ISeq)
# make sure it's empty!
s.add (iseq == Empty(ISeq))
# get a model and print it:
if s.check() == sat:
print (s.model())
z3 说:
[iseq = Empty(Seq(Int))]
请注意,z3py 本质上是一种函数式语言;一旦您断言某事等于其他某事,您就无法修改该值,就像您在命令式语言(例如 Python)中所做的那样。希望有帮助!
推荐阅读
- express - 我应该在生产中使用代理中间件吗?
- javascript - WordPress Ajax Uncaught ReferenceError: url is not defined
- azure-devops - 仅在 ARM 模板部署后才知道 ConnectionString 时如何生成 EF Core 迁移脚本?
- javascript - 重要的 npm install @babel/plugin-transform-react-jsx 不起作用
- ansible - 如何将我的文件的包含与ansible进行比较?
- react-native - React Native Calendars:如何使用 Redux 仅更改一项的背景颜色
- sql - 如何使用两列转置表格
- mongodb - 有没有办法在删除集合后释放被 mongodb 索引占用的 RAM?
- node.js - Visual Studio 2017 中未出现的建议
- javascript - 加载到节点画布模块中的JPG文件在操作和导出后丢失质量