3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-03-15 17:49:59 +00:00
z3/examples/java
Angelica Moreira b8e15f2121
Add missing AST query methods to Java API (#8977)
* add Expr.isGround() to Java API

Expose Z3_is_ground as a public method on Expr. Returns true when the
expression contains no free variables.

* add Expr.isLambda() to Java API

Expose Z3_is_lambda as a public method on Expr. Returns true when the
expression is a lambda quantifier.

* add AST.getDepth() to Java API

Expose Z3_get_depth as a public method on AST. Returns the maximum
number of nodes on any path from root to leaf.

* add ArraySort.getArity() to Java API

Expose Z3_get_array_arity as a public method on ArraySort. Returns
the number of dimensions of a multi-dimensional array sort.

* add DatatypeSort.isRecursive() to Java API

Expose Z3_is_recursive_datatype_sort as a public method on
DatatypeSort. Returns true when the datatype refers to itself.

* add FPExpr.isNumeral() to Java API

Expose Z3_fpa_is_numeral as a public method on FPExpr. Returns true
when the expression is a concrete floating-point value.

* add isGroundExample test to JavaExample

Test Expr.isGround() on constants, variables, and compound
expressions.

* add astDepthExample test to JavaExample

Test AST.getDepth() on leaf nodes and nested expressions to verify
the depth computation.

* add arrayArityExample test to JavaExample

Test ArraySort.getArity() on single-domain and multi-domain array
sorts.

* add recursiveDatatypeExample test to JavaExample

Test DatatypeSort.isRecursive() on a recursive list datatype and a
non-recursive pair datatype.

* add fpNumeralExample test to JavaExample

Test FPExpr.isNumeral() on a floating point constant and a symbolic
variable.

* add isLambdaExample test to JavaExample

Test Expr.isLambda() on a lambda expression and a plain variable.
2026-03-14 10:13:42 -07:00
..
JavaExample.java Add missing AST query methods to Java API (#8977) 2026-03-14 10:13:42 -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.