3

I'm using Z3py and Strings to generate some new constraints from a recursive data type (list). In order to construct related constraints in Z3 engine, I used Z3 String to store it. For example, if we have a list (a,b,c), then we produce x1==1, x2==2, x3==3 by means of (a,b,c) in the engine. Then we need to add the new constraints into the engine again. eval() does not work because it need Python string rather than Z3 String. So, anyone can help me to convert Z3 String into Python String. Thank you very very much.

Kun
  • 101
  • 3
  • 1
    I think I've found the solution. For example, str(StringVal("abc")) can return a python string. However, it's very strange that str(StringVal("abc")) returns a string staring and ending with the quotation symbol. That is str(StringVal("abc")) =="abc" will return false, and str(StringVal("abc")).strip(' " ') == "abc" will return True. That's the reason I got wrong in the past. – Kun Jan 17 '18 at 00:43

1 Answers1

1

You can find the code for StringEval of z3 in this link: https://z3prover.github.io/api/html/namespacez3py.html

And about the extra quotations when you apply str on its returned value, you should note that z3 wants to follow the standards of SMT-LIB, see this link https://www.philipzucker.com/z3-rise4fun/sequences.html. In this standard, strings are denoted by a double quote and not a single quote (compare with Python, where a single quote is also allowed to denote a string). So they are hard-coding the double quote. So for example, when you run the following

from z3 import *
str( StringVal( "abc" ) )

You get '"abc"' as the output, no? Python used the single quote ' to show this value is a string, which z3 wants to avoid. Therefore, probably the best solution is what you found yourself: str( StringVal( "abc" ) ).strip(' " ')

Tpeazy1
  • 43
  • 4