3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-02-01 06:47:56 +00:00
z3/examples/java
Copilot db6e15361b
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>
2026-01-31 15:31:36 -08:00
..
JavaExample.java fix #6665 2023-04-02 10:58:21 -07:00
JavaGenericExample.java fix generic example 2024-02-21 08:16:01 -08:00
PolymorphicDatatypeExample.java Add Java APIs for polymorphic datatypes (#8438) 2026-01-31 15:31:36 -08:00
RCFExample.java Add RCF (Real Closed Field) bindings to C++, Java, C#, and TypeScript (#8171) 2026-01-12 16:34:42 -08:00
README Add Java APIs for polymorphic datatypes (#8438) 2026-01-31 15:31:36 -08:00
SeqOperationsExample.java Add sequence higher-order functions to Java API (#8226) 2026-01-17 13:02:54 -08:00

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, 
or Visual Studio Code, see:
   ../../doc/JAVA_IDE_SETUP.md

## Building and Running Examples

To build the example, configure Z3 with the --java option to scripts/mk_make.py, build via  
   make examples
in the build directory.

It will create JavaExample.class in the build directory,
which can be run on Windows via 
   java -cp com.microsoft.z3.jar;. JavaExample

On Linux and FreeBSD, we must use
   LD_LIBRARY_PATH=. java -cp com.microsoft.z3.jar:. JavaExample
On macOS, the corresponding option is DYLD_LIBRARY_PATH:
   DYLD_LIBRARY_PATH=. java -cp com.microsoft.z3.jar:. JavaExample  

By default, Z3 Java bindings are automatically loading the required native library for Z3 from the default library path.
In certain environments, depending on the developing process, the Z3 library is not available in the given library path.
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.