provably.types¶
Refinement type markers and Z3 sort utilities.
from provably.types import (
Gt, Ge, Lt, Le, Between, NotEq,
Positive, NonNegative, UnitInterval,
extract_refinements, python_type_to_z3_sort, make_z3_var,
)
Markers¶
Use with typing.Annotated to embed constraints in signatures.
| Marker | Constraint | Example |
|---|---|---|
Gt(bound) |
\(x > \text{bound}\) | Annotated[float, Gt(0)] |
Ge(bound) |
\(x \geq \text{bound}\) | Annotated[float, Ge(0)] |
Lt(bound) |
\(x < \text{bound}\) | Annotated[float, Lt(1)] |
Le(bound) |
\(x \leq \text{bound}\) | Annotated[float, Le(100)] |
Between(lo, hi) |
\(\text{lo} \leq x \leq \text{hi}\) | Annotated[float, Between(0, 1)] |
NotEq(val) |
\(x \neq \text{val}\) | Annotated[float, NotEq(0)] |
Multiple markers on the same parameter are ANDed. Any callable m(var) -> z3.BoolRef
is also accepted.
Convenience aliases¶
| Alias | Definition |
|---|---|
Positive |
Annotated[float, Gt(0)] |
NonNegative |
Annotated[float, Ge(0)] |
UnitInterval |
Annotated[float, Between(0, 1)] |
extract_refinements(typ, var)¶
Convert Annotated markers to Z3 constraints for a symbolic variable.
import z3
from provably.types import extract_refinements, Between
x = z3.Real('x')
extract_refinements(Annotated[float, Between(0, 1)], x)
# [x >= 0, x <= 1]
python_type_to_z3_sort(typ)¶
| Python type | Z3 sort |
|---|---|
int |
z3.IntSort() |
float |
z3.RealSort() |
bool |
z3.BoolSort() |
Strips Annotated wrappers. Raises TypeError for unsupported types.
make_z3_var(name, typ)¶
make_z3_var('x', float) # z3.Real('x')
make_z3_var('n', int) # z3.Int('n')
make_z3_var('b', bool) # z3.Bool('b')
provably.types ¶
Refinement types and Z3 sort mapping for Provably.
Maps Python type annotations to Z3 sorts, and provides refinement type markers compatible with typing.Annotated for embedding proof obligations directly in function signatures.
Convenience aliases¶
Positive — Annotated[float, Gt(0)]
NonNegative — Annotated[float, Ge(0)]
UnitInterval — Annotated[float, Between(0, 1)]
These compose with other annotations::
x: Annotated[Positive, Le(100)] # 0 < x <= 100
Classes¶
Between ¶
Ge ¶
Gt ¶
Le ¶
Lt ¶
NotEq ¶
Functions¶
extract_refinements ¶
Extract Z3 constraints from Annotated type markers.
Walks the metadata arguments of an Annotated type and converts
each Gt / Ge / Lt / Le / Between / NotEq
marker into a Z3 BoolRef constraint on var.
Also supports callable markers: marker(var) is called and the
result is included if it is a z3.BoolRef.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typ
|
type
|
A Python type, typically |
required |
var
|
Any
|
The Z3 variable to constrain. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
A list of |
Source code in src/provably/types.py
make_z3_var ¶
Create a Z3 variable from a name and Python type annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The variable name (used as the Z3 symbol name). |
required |
typ
|
type
|
The Python type annotation ( |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A Z3 |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the type cannot be mapped to a Z3 sort. |
Source code in src/provably/types.py
python_type_to_z3_sort ¶
Map a Python type annotation to a Z3 sort.
Strips Annotated wrappers and maps int → IntSort,
float → RealSort, bool → BoolSort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typ
|
type
|
A Python type, optionally wrapped in |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The corresponding Z3 sort. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If no Z3 sort exists for the given type. |