Z3 Java API: FuncDecl and Expr - z3

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.

Related

Parsing SMTLIB using the Z3 C API without losing `push`, `pop` and `check-sat` commands

I'm using the Z3_parse_smtlib2_string function from the Z3 C API (via Haskell's Z3 lib) to parse an SMTLIB file and apply some tactics to simplify its content, however I notice that any push, pop and check-sat commands appear to be swallowed by this function and do not appear in the resulting AST.
Is there anyway that I can parse this without losing these commands (and then apply the required tactics, once again without losing them)?
I don't think it's possible to do this with Z3_parse_smtlib2_string. As you can see in the documentation "It returns a formula comprising of the conjunction of assertions in the scope (up to push/pop) at the end of the string." See: https://z3prover.github.io/api/html/group__capi.html#ga7905ebec9289b9fe5debcad965f6267e
Note that the reason for this is not just mere "not-implemented" or "buggy." Look at the return type of the function you're using. It returns a Z3_ast_vector, and Z3_ast only captures "expressions" in the SMTLib language. But push/pop etc. are not considered expressions by Z3, but rather commands; i.e., they are internally represented differently. (Whether this was a conscious choice or historical is something I'm not sure about.)
I don't think there's a function to do what you're asking; i.e., can return both expressions and commands. You can ask at https://github.com/Z3Prover/z3/discussions to see if the developers can provide an alternative API, or if they already have something exposed to the users that achieves this.

Z3 Java APIs for getting values of bit vectors

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.

Data Structure to store Token Properties

I am writing an interpreter for a mathematical language in Rust which is intended to be used to solve mathematical expressions.
When lexing, the program needs to know based on the characters used in a token, what type of token it is (for example is it a function or an operator).
Currently I use an enumeration to represent a type of token:
pub enum IdentifierType {
Function,
Variable,
Operator,
Integer,
}
To check the type of a token I use a function which takes an IdentifierType as input and matches based on input to return a bool. The data structures that could be used in this case are relatively simple as tokens only have a single property: allowed characters.
When parsing to an Abstract Syntax Tree (AST), I would like to know what specific operator or function is being used based on a token and to be able to add a reference to that operator and its associated functions to the AST.
When interpreting, I would like to be able to call execute on a node and have it know how to perform its own function.
I have tried to come up with a solution to store all of these related items, but none that I have encountered as felt satisfactory.
For example I stored all of the operators in a TOML file (a type of configuration file that maps to a hash table) but storing enumerations (values that are constrained) is difficult and there is no way to store an operators function. I also want to be able to search by multiple keys, such as operator associativity (e.g. get all operators that are right associative), which means storing within source code is not very satisfactory.
Other possible ideas I have had are using some kind of SQL hybrid system, however that seems tough to implement

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

how to convert z3 expression to infix expression?

I want to translate boolExpression in Z3 into infix representation. For example, there is a z3 expression (>= t 3), I want to get the infix string "t>=3", is any existing Z3 api to implement it in C# ?
No, the official API does not have support for displaying expressions in infix notation. This functionality can be implemented on top of the API for traversing expressions. The Z3 Python API implements an infix printer. Actually, it implements two: one for Python-like syntax, and one for HTML math-like syntax. The source code of these printers is included in the Z3 distribution. The code is written in python, but can be easily converted into any programming language. The code is located at python\z3printer.py.

Resources