I am wondering if z3 supports associative arrays (aka maps)? If not is there a simple way to model such data structures using the current version of z3?
Z3 has support for Arrays as defined by SMT-Lib and it also has support for datatypes, either of them should allow you to model maps. A detailed example of using datatypes is found in the answer to this question: a datatype contains a set in Z3. The Z3 Guide also contains sections on how to use arrays as well as datatypes.
Related
I'm trying to solve a question answering task. There are several approaches to this like Deep Learning methods, querying Knowledge Graphs, Semantic Search etc. But I thought if it would be possible to use Z3 theorem prover for that task as well? For example, if we can present knowledge as a set of axioms, each axiom consists of the predicates (relations), subjects and objects and is expressed in FOL clauses, then we can traverse through them and find an answer to the query (which can be expressed as axiom as well). For example, I can encode a simple knowledge "English is language" in FOL clause:
exists l.(language(l) & exists n.(name(n) & :op1(n,"English") & :name(l,n)))
How can I translate it into Z3? And how can I extract an answer to the query "{unknown} is language" to find an {unknown} variable or clause? Note that the {unknown} can be anything. It can be an atom or logical clause depending on the match with the query.
I don't think an SMT solver is very suitable for this task. Not because you can't do it using z3, but a system like Prolog or a custom-program you build will work just as fine as well. SMT solvers shine when you have combination of theories (numbers, arithmetic, arrays, data-structures, etc.); for your problem domain, all you need is a Prolog like simple-database and a query engine.
Encoding your suggested statement really depends on what sort of predicates you have in mind. Note that SMTLib is a "typed" language; so a predicate like language(l) is redundant: You'd have a value of the type language only when that call type-checks; i.e., you can't pass the predicate language anything that's not a language anyways. (This is similar to programming in a typed-language like Haskell/O'Caml etc., vs. in a dynamically typed language like Lisp/Scheme/Python etc.)
See Solving predicate calculus problems with Z3 SMT for an example of how to use an SMT solver to deal with first-order-logic modeling problems.
I wrote a large-ish library in the Z3 dialect of SMT-LIB. Unfortunately, my use of (declare-datatypes) to create tuples means that I cannot set the logic to QF_AUFBV as I desire. This has the side effect of making my scripts slower (sometimes timing out) than when I manually create the formulas programmatically and solve using QF_ABV. Thus, I want to eliminate (declare-datatypes) from my script. Most of the data types can be encoded as bit vectors. However, the most important sort in the library is a tuple of a bitvector term and three arrays. Is there a solution where I can make a sort like this, while still using QF_AUFBV logic?
You can always concatenate the bitvectors of the tuple and extract the relevant half whenever needed.
when you are going to use array module - and when to use arrays generally in functional programming - erlang in this case.
Thanks.
The rationale is that if you do want a functional (nondestructive) data structure using integer keys, then the array module is significantly more efficient than a dict, gb_tree or similar (which can use any kinds of values as keys). And the indexing is zero-based because that's generally more useful for the kind of problems you'd want an array for.
I find the note in Learn You Some Erlang to be quite off the mark.
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.*.
Is there a collection which is similar to a Java map with a key and value?
Hashtable would be the closest general purpose collection. If you are working with primitive keys or keys and values, there are some variations that do a better job of preserving types, and can be a little more efficient too:
IntHashtable
IntIntHashtable
IntLongHashtable
LongHashtable
LongIntHashtable