简体   繁体   中英

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.

(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.

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.

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". 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. [In addition to List , the function renames identifiers that are commonly used in SMT-LIB formulae.]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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