Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Z3 对数据类型进行量化_Z3 - Fatal编程技术网

Z3 对数据类型进行量化

Z3 对数据类型进行量化,z3,Z3,在使用Z3Py时,我定义了一个整数列表,如下所示: List = Datatype('List') List.declare('cons', ('head', IntSort()), ('tail', List)) List.declare('nil') List = List.create() 现在,我在该列表上定义了一些简单的函数,如下所示: len = Function('len', List, IntSort()) def len_defn(): ls = List('ls')

在使用Z3Py时,我定义了一个整数列表,如下所示:

List = Datatype('List')
List.declare('cons', ('head', IntSort()), ('tail', List))
List.declare('nil')
List = List.create()
现在,我在该列表上定义了一些简单的函数,如下所示:

len = Function('len', List, IntSort())

def len_defn():
  ls = List('ls')
  return And([
    len(List.nil) == 0,
    ForAll(ls, Implies(List.is_cons(ls), len(ls) == 1+len(List.tail(ls))))
  ])
不幸的是,这最终会失败,因为
ls=List('ls')
抛出错误:

AttributeError: DatatypeSortRef instance has no __call__ method
尝试使用
ls=Var(0,列表)
throw:

AttributeError: DatatypeRef instance has no attribute '__len__'

有人知道通常应该如何处理数据类型的量化吗?

要创建排序常量
列表
,我们应该使用过程
Const

ls = Const('ls', List)
在Z3Py中,所有和存在的过程都基于以Z3常量为参数的C API。大多数用户发现这些C API比基于de Bruijn索引的API更易于使用

另一个问题,我们不应该重新定义
len
len
是一个Python内置函数。 为了避免程序中出现问题,我们应该使用

Len = Function('len', List, IntSort())
下面是重写的示例()


谢谢你的回答。重新阅读Z3Py教程(特别是函数部分),我可以看出哪里出了问题。本节讨论将变量建模为未解释的常量,然后使用诸如
x=Int('x')
之类的语法来定义变量。我猜底层的apply方法在这里结束了对
Const('x',IntSort())
的调用?感谢您提醒我们查看底层C函数以获取更多信息。是的,您是正确的
Int('x')
只是
Const('x',IntSort())
的语法糖。
List = Datatype('List')
List.declare('cons', ('head', IntSort()), ('tail', List))
List.declare('nil')
List = List.create()
Len = Function('len', List, IntSort())

def len_defn():
  ls = Const('ls', List)
  return And([
    Len(List.nil) == 0,
    ForAll(ls, Implies(List.is_cons(ls), Len(ls) == 1+Len(List.tail(ls))))
  ])

print len_defn()