简体   繁体   English

z3py为有效的SMT2文件引发解析器错误

[英]z3py throws parser error for a valid SMT2 file

 1  (set-logic UFLIA)
 2  (set-info :source | Simple list theorem |)
 3  (set-info :smt-lib-version 2.0)
 4  (set-info :category "crafted")
 5  (set-info :status unsat)
 6  (declare-sort List 0)
 7  (declare-sort Elem 0)
 8  (declare-fun cons (Elem List) List)
 9  (declare-fun nil () List)
10  (declare-fun car (List) Elem)
11  (declare-fun cdr (List) List)
12  (declare-fun len (List) Int)
13  (assert (forall ((?x Elem) (?y List)) (= (car (cons ?x ?y)) ?x)))
14  (assert (forall ((?x Elem) (?y List)) (= (cdr (cons ?x ?y)) ?y)))
15  (assert (= (len nil) 0))
16  (assert (forall ((?x Elem) (?y List)) (= (len (cons ?x ?y)) (+ (len ?y) 1))))
17  (declare-fun append (List List) List)
18  (assert (forall ((?y List)) (= (append nil ?y) ?y)))
19  (assert (forall ((?x Elem) (?y1 List) (?y2 List)) (= (append (cons ?x ?y1) ?y2) (cons ?x (append ?y1 ?y2)))))
20  (assert (not (forall ((?x Elem) (?y List)) (= (append (cons ?x nil) ?y) (cons ?x ?y)))))
21  (check-sat)
22  (exit)

For the above formula, f = z3.parse_smt2_file("UFLIA/misc/list3.smt2") results in the following errors. 对于上述公式, f = z3.parse_smt2_file("UFLIA/misc/list3.smt2")导致以下错误。

(error "line 6 column 14: invalid sort declaration, sort already declared/defined")
(error "line 8 column 24: sort constructor expects parameters")
(error "line 9 column 20: sort constructor expects parameters")
(error "line 10 column 18: sort constructor expects parameters")
(error "line 11 column 18: sort constructor expects parameters")
(error "line 12 column 18: sort constructor expects parameters")
(error "line 13 column 31: sort constructor expects parameters")
(error "line 14 column 31: sort constructor expects parameters")
(error "line 15 column 16: unknown constant nil")
(error "line 16 column 31: sort constructor expects parameters")
(error "line 17 column 21: sort constructor expects parameters")
(error "line 18 column 21: sort constructor expects parameters")
(error "line 19 column 32: sort constructor expects parameters")
(error "line 20 column 36: sort constructor expects parameters")

But processing the same file with z3-4.3.2.bb56885147e4-x64-osx-10.9.2 CLI provides unsat result. 但是,使用z3-4.3.2.bb56885147e4-x64-osx-10.9.2 CLI处理同一文件会导致结果不稳定。

Using traceback in Python, I found the following as the root cause of the exception TypeError: unorderable types: int() < Z3Exception() Based on the exception stack, it seems the error is stemming from within native Z3 code. 在Python中使用traceback ,我发现以下内容是引发TypeError: unorderable types: int() < Z3Exception()异常的根本原因TypeError: unorderable types: int() < Z3Exception()基于异常堆栈,似乎错误源于本机Z3代码。

Any idea why is this happening and how to resolve this? 知道为什么会这样以及如何解决吗?

You will have to rename "List" to something else. 您将不得不将“列表”重命名为其他名称。 From the API, Z3 pre-loads a built-in definition for "List". Z3从API中预加载了“列表”的内置定义。 It ignores the logic directive that otherwise narrows the pre-loaded sort. 它忽略了逻辑指令,否则该逻辑指令会缩小预加载的排序范围。

Based on @nikolaj comment, I used the following function to transform SMT2 files by renaming identifiers that clashed with definitions pre-loaded by Z3 when using Z3 API. 基于@nikolaj注释,我使用以下函数通过重命名与使用Z3 API时Z3预加载的定义冲突的标识符来转换SMT2文件。 [In addition to List , the function renames identifiers that are commonly used in SMT-LIB formulae.] [除了List ,该函数还重命名了SMT-LIB公式中常用的标识符。]

import z3
import importlib

def get_formula(src_file_name):
  try:
    f = z3.parse_smt2_file(src_file_name)
    return f
  except z3.Z3Exception as e:
    lines = open(src_file_name, 'rt').readlines()
    tmp1 = ' '.join(lines).replace("max", "c_max")
    tmp1 = tmp1.replace("sin", "c_sin")
    tmp1 = tmp1.replace("cos", "c_cos")
    tmp1 = tmp1.replace("tan", "c_tan")
    tmp1 = tmp1.replace("tanh", "c_tanh")
    tmp1 = tmp1.replace("atan", "c_atan")
    tmp1 = tmp1.replace("min", "c_min")
    tmp1 = tmp1.replace("max", "c_max")
    tmp1 = tmp1.replace("pi", "c_pi")
    tmp1 = tmp1.replace("List", "c_List")
    tmp1 = tmp1.replace("subset", "c_subset")
    tmp1 = tmp1.replace("difference", "c_difference")
    tmp1 = tmp1.replace("union", "c_union")
    tmp1 = tmp1.replace("fp", "c_fp")
    tmp1 = tmp1.replace("repeat", "c_repeat")
    importlib.reload(z3)
    f = z3.parse_smt2_string(tmp1)
    return f

With this function, I was able to successfully parse the formula but the program seg-faulted upon exit . 使用此功能,我能够成功解析公式,但是程序在退出时出现段错误 It seems that there are some references that aren't cleaned up even after reloading z3 module. 似乎有些参考即使重新加载z3模块后也无法清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM