Z3 Java APIs for getting values of bit vectors - z3

I have written a program that automatically generates smt2 file and use Z3 Java APIs to parse the file, solve the constraints and get assignments for each constants.
Now I have some constraints in SMT2 file that are specified in bit-vector and would like to get values out of after SMT solving. What is the right Java APIs for getting the values from those bit-vectors.
I have tried model.getConstDecls() and use Z3_sort_kind to determine the type of each constant in a loop. It seems I cannot get any values from bit-vectors. I notice that parseSMTLIB2File has changed somehow. Here is a piece of my sample code:
FuncDecl c[] = model.getConstDecls();
for (int i=0;i<cons.length;i++){
Z3_sort_kind sort = c[i].getRange().getSortKind();
Expr expr = model.getConstInterp(c[i]);
if (expr.isBV())
//my own code to extract values from bit-vector.
}
It seems that model.getConstDecls() is not getting any bit-vectors. I know there is a JavaExample to get model from bit-vector. However, I don't want to rewrite the program because it has quite large and has other things. It will be very time consuming.

Related

How to read a string for a number starting with a decimal point in Haskell?

I'm a total Haskell beginner who just discovered that read spits out an exception when given a decimal number starting with . rather than a digit. For example, in ghci:
Prelude> read ".7" :: Float
*** Exception: Prelude.read: no parse
I found one discussion and it makes sense why surrounding . in numbers with digits is required in Haskell. Another discussion is also somewhat helpful, but no one provides a solution of how to actually convert ".7" to 0.7.
So, I'm trying to extract data from a fixed-width format file containing fields with values like .7---is there a standard function or approach I can use to clean this up to a float 0.7?
(Before I hit this issue, my basic ideas was to define a custom type for my data, use splitWidth in Data.List.Split to split each line into its fields, and then use read to convert each field into its correct type, trying to apply the functional goodness in this answer in the actual implementation.)
As Thomas M. DuBuisson answered in a comment above, the obvious thing to do is myRead = read . ('0':) :: String -> Float. This works for me --- I won't ever be trying to read negative numbers, and I know which fields should be read as float. Thanks!

Z3 Java API: FuncDecl and Expr

I have an existing smt2 file, and used z3 java api parsed this and solved the problem. But I have a question about how to cast a FuncDecl to an Expr because I want to build some simple formula by using z3 java apis. Since my original formulas is purely written in smt2 text file and everything gets parsed into a single BoolExpr. Now, I successfully extracted the consts, and need to manipulate them with new formula. How can i do it? Basically, what I am looking for is how to build an Expr from FuncDecl Or is there a way I can cast it to Expr? Is there any official java api document available? I know there is an example of using z3 java api, but it's pretty painful to look for a specific api description in such a large example.
FuncDecl's are function declarations, they can not be cast directly to expressions, but the function they represent can be applied (to some arguments) to yield an expression. This is what FuncDecl.apply(...) does. Constants are of course a special case, where the function doesn't take any arguments.

how to get the number of declare-funs in smt2 instance in z3(api)

I used the *Z3_parse_smtlib2_file(c,Z3_string,0,0,0,num_decl,&decl_names,&decls)* to try to get the variables and the quantity of variables. But the value of *num_decl* still be zero.
What I consider the value will be become as the different smt2 files. Thanks
The parameters num_decls, decl_names and decls are input parameters. They are used to initialize the SMT 2.0 parser symbol table with declarations created using the C API.
The current Z3 API does not provide procedures for extracting the sorts and functions declared in a file/string in SMT 2.0 format. This information is available internally. See the files in the following directories in the Z3 distribution src/parsers/smt2 and src/cmd_context/cmd_context.*.

use variables in Lemon parser?

I want to allow mathematical variables in my Lemon parser-driven app. For example, if the user enters x^2+y, I want to then be able to evaluate this for 100000 different pairs of values of x and y, hopefully without having to reparse each time. The only way I can think of to do that is to have the parser generate a tree of objects, which then evaluates the expression when given the input. Is there a better/simpler/faster way?
Performance may be an issue here. But I also care about ease of coding and code upkeep.
If you want the most maintainable code, evaluate the expression as you parse. Don't build a tree.
If you want to re-execute the expression a lot, and the expression is complicated, you'll need to avoid reparsing (in order of most to least maintainable): build tree and evaluate, generate threaded code and evaluate, generate native code and evaluate.
If the expressions are generally as simple as your example, a recursive descent hand-coded parser that evaluates on the fly will likely be very fast, and work pretty well, even for 100,000 iterations. Such parsers will likely take much less time to execute than Lemon.
That is indeed how you would typically do it, unless you want to generate actual (real or virtual) code. x and y would just be variables in your case so you would fill in the actual values and then call your Evaluate function to evaluate the expression. The tree node would then contain pointers to the variables x and y, and so on. No need to parse it for each pair of test values.

Z3 naming let bindings in API

I am using Z3 from the API and I'm looking for a way to debug my constraints. My code compiles and Z3 runs on my constraints, but something is wrong with my constraints. I'm hoping to look at the constraints that I gave to Z3 to determine what is wrong or missing, but I'm not sure how to do this in a way that is very readable. The problem is that using facilities like SMTLIB_DUMP_ASSERTIONS does not provide meaningful names in any let bound variables. Since I have many reuses of the same expressions, nearly everything is let-bound with a generated variable.
Is there any way to dump a file of the input constraints, where let-bound variables have a name that I have assigned? I don't particularly care what the format is, but SMTLIB 1 or 2 would be nice.
No, you cannot provide names to let variables automatically created by Z3 AST printers.
One possible solution is to write your own AST printer. In the Z3 distribution, we have an example application examples/c/test_capi.c. It contains the function:
void display_ast(Z3_context c, FILE * out, Z3_ast v)
It shows how to implement a simple AST printer. This example is very simple, but it is a starting point.

Resources