3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-02-02 23:36:17 +00:00

Add Java APIs for polymorphic datatypes (#8438)

* Initial plan

* Add Java APIs for polymorphic datatypes and type variables

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Fix code review issue and add documentation

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Add TypeVarSort.java to CMakeLists.txt for Java bindings

The CMake build was failing because TypeVarSort.java was not included in the Z3_JAVA_JAR_SOURCE_FILES list in src/api/java/CMakeLists.txt. Added it in alphabetical order between TupleSort.java and UninterpretedSort.java.

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
Copilot 2026-01-31 15:31:36 -08:00 committed by GitHub
parent b008b5e926
commit db6e15361b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 290 additions and 0 deletions

View file

@ -1,5 +1,13 @@
A small example using the Z3 Java bindings.
## Examples
- **JavaExample.java** - General examples demonstrating various Z3 features
- **JavaGenericExample.java** - Examples using generic Z3 types
- **PolymorphicDatatypeExample.java** - Examples of parametric/polymorphic datatypes with type variables
- **SeqOperationsExample.java** - Examples of sequence operations
- **RCFExample.java** - Examples using real closed fields
## IDE Setup
For detailed instructions on setting up Z3 Java bindings in Eclipse, IntelliJ IDEA,
@ -26,3 +34,37 @@ In certain environments, depending on the developing process, the Z3 library is
To disable the automated loading process, the user can set the environment variable "z3.skipLibraryLoad=true".
In that case, the calling application should directly load the corresponding libraries before any interaction with Z3.
## Polymorphic Datatypes
Z3's Java API now supports polymorphic (parametric) datatypes, similar to generic types in Java or templates in C++.
These allow you to define datatypes that are parameterized by type variables.
### Creating Type Variables
```java
Context ctx = new Context();
TypeVarSort T = ctx.mkTypeVariable("T");
TypeVarSort U = ctx.mkTypeVariable("U");
```
### Creating Polymorphic Datatypes
Example: Polymorphic List[T]
```java
// Create type variable
TypeVarSort T = ctx.mkTypeVariable("T");
// Define constructors
Constructor<Object> nil = ctx.mkConstructor("nil", "is_nil", null, null, null);
Constructor<Object> cons = ctx.mkConstructor("cons", "is_cons",
new String[]{"head", "tail"},
new Sort[]{T, null}, // null means recursive reference to List[T]
new int[]{0, 0});
// Create the polymorphic datatype
DatatypeSort<Object> listSort = ctx.mkPolymorphicDatatypeSort("List",
new Sort[]{T}, new Constructor[]{nil, cons});
```
See `PolymorphicDatatypeExample.java` for complete working examples.