provably.engine¶
Proof certificates, Z3 orchestration, and cache management.
from provably.engine import ProofCertificate, Status, verify_function, verify_module, clear_cache, configure
Status¶
| Value | Meaning |
|---|---|
VERIFIED |
Z3 returned unsat. Mathematical theorem. |
COUNTEREXAMPLE |
Z3 returned sat. Counterexample found. |
UNKNOWN |
Solver timed out. |
TRANSLATION_ERROR |
AST translator failed. |
SKIPPED |
Not attempted (no Z3, async, no postcondition). |
ProofCertificate¶
Immutable certificate attached to every @verified function as fn.__proof__.
cert = fn.__proof__
cert.verified # True iff status == VERIFIED
cert.status # Status.VERIFIED
cert.counterexample # {'x': 3, '__return__': 1} or None
cert.solver_time_ms # 1.4
cert.message # human-readable summary
str(cert) # "[Q.E.D.] fn_name"
| Field | Type | Description |
|---|---|---|
function_name |
str |
Verified function name |
source_hash |
str |
SHA-256 prefix (content-addressing) |
status |
Status |
Verification outcome |
preconditions |
tuple[str, ...] |
Z3 precondition strings |
postconditions |
tuple[str, ...] |
Z3 postcondition strings |
counterexample |
dict \| None |
Witness on COUNTEREXAMPLE |
message |
str |
Error/skip/counterexample summary |
solver_time_ms |
float |
Wall-clock ms in Z3 |
z3_version |
str |
Z3 version used |
Serialization¶
data = cert.to_json() # dict (JSON-serializable)
cert = ProofCertificate.from_json(data) # round-trip
verify_function()¶
Low-level entry point. Most users should use @verified instead.
from provably.engine import verify_function
def add(x: int, y: int) -> int:
return x + y
cert = verify_function(add, pre=lambda x, y: x >= 0, post=lambda x, y, result: result >= x)
cert.verified # True
verify_module()¶
Collect all @verified functions in a module, return {name: ProofCertificate}.
from provably.engine import verify_module
import mypackage.math as m
results = verify_module(m)
for name, cert in results.items():
print(cert) # [Q.E.D.] name
configure()¶
Set global defaults. Call before importing decorated modules.
| Key | Default | Description |
|---|---|---|
timeout_ms |
5000 |
Z3 timeout per proof (ms) |
raise_on_failure |
False |
Raise VerificationError on failure |
log_level |
"WARNING" |
Logging level for provably logger |
clear_cache()¶
Clear the global proof cache (content-addressed by source + contract hash).
provably.engine ¶
Verification engine — VC generation, Z3 solving, proof certificates.
Orchestrates the full pipeline
- Parse function source → AST
- Create Z3 symbolic variables from type annotations
- Translate function body via Translator (the TCB)
- Build verification condition: pre ∧ body → post
- Negate postcondition, check UNSAT with Z3
- Return ProofCertificate (cached, content-addressed)
Global configuration¶
Use :func:configure to set defaults that apply to every subsequent call::
from provably import configure
configure(timeout_ms=10_000, raise_on_failure=True)
These defaults can be overridden per-call via keyword arguments to
:func:verify_function or the :func:~provably.decorators.verified decorator.
Classes¶
ProofCertificate
dataclass
¶
ProofCertificate(function_name: str, source_hash: str, status: Status, preconditions: tuple[str, ...], postconditions: tuple[str, ...], counterexample: dict[str, Any] | None = None, message: str = '', solver_time_ms: float = 0.0, z3_version: str = '')
Immutable proof certificate for a verified function.
Attached to decorated functions as func.__proof__.
Attributes:
| Name | Type | Description |
|---|---|---|
function_name |
str
|
The name of the verified function. |
source_hash |
str
|
SHA-256 prefix of the function's source text. |
status |
Status
|
The verification outcome (see :class: |
preconditions |
tuple[str, ...]
|
Human-readable Z3 string representations of the applied preconditions. |
postconditions |
tuple[str, ...]
|
Human-readable Z3 string representations of the applied postconditions. |
counterexample |
dict[str, Any] | None
|
Input values that disprove the postcondition,
or |
message |
str
|
Human-readable explanation (error message, skip reason, etc.). |
solver_time_ms |
float
|
Wall-clock time spent in the Z3 solver. |
z3_version |
str
|
The Z3 version string used for this proof. |
Attributes¶
Functions¶
explain ¶
Human-readable explanation of the proof result.
Returns a multi-line string describing the outcome, any counterexample found, and the violated postcondition.
Example::
print(func.__proof__.explain())
# Q.E.D.: double
# or
# COUNTEREXAMPLE: bad_func
# Counterexample: {'x': -1}
# bad_func(x=-1) = -1
# Postcondition: 0 <= result
Source code in src/provably/engine.py
from_json
classmethod
¶
Deserialize a certificate from a JSON-compatible dict.
This is the inverse of :meth:to_json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dict with the same keys as produced by :meth: |
required |
Returns:
| Type | Description |
|---|---|
ProofCertificate
|
A reconstructed :class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a required field is missing from data. |
ValueError
|
If the |
Example::
cert = ProofCertificate.from_json(json.loads(json_string))
Source code in src/provably/engine.py
to_json ¶
Serialize the certificate to a JSON-compatible dict.
All values are JSON-native types (str, int, float, bool, None, dict).
The counterexample values are coerced to strings when they are
not already JSON-serializable.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict that can be passed directly to |
Example::
import json
cert = func.__proof__
print(json.dumps(cert.to_json(), indent=2))
Source code in src/provably/engine.py
to_prompt ¶
Format certificate for LLM consumption in repair loops.
Returns a single-paragraph string describing the verification result in a form suitable for inclusion in an LLM prompt.
Example::
prompt = func.__proof__.to_prompt()
# "Function `bad_func` DISPROVED. Counterexample: {'x': -1} → result=-1
# Violated: 0 <= result Fix the implementation or strengthen the precondition."
Source code in src/provably/engine.py
Status ¶
Bases: Enum
Verification result status.
Functions¶
clear_cache ¶
Clear the in-memory proof cache.
Does not delete disk-cached proofs. To clear disk cache, delete
the directory set via configure(cache_dir=...).
configure ¶
Set global verification defaults.
Supported keys:
timeout_ms(int): Z3 solver timeout in milliseconds (default 5000).raise_on_failure(bool): Raise :class:~provably.decorators.VerificationErrorwhen a proof fails (defaultFalse).log_level(str): Python logging level for theprovablylogger (default"WARNING").cache_dir(str | None): Directory for disk-persistent proof cache. Default:~/.provably/cache. Set toNoneto disable disk caching. Proofs are persisted across process restarts — no re-proving on import.
Example::
from provably import configure
configure(timeout_ms=10_000, cache_dir=".provably_cache")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Key-value pairs to update in the global config. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unknown configuration key is provided. |
Source code in src/provably/engine.py
verify_function ¶
verify_function(func: Callable[..., Any], pre: Callable[..., Any] | None = None, post: Callable[..., Any] | None = None, timeout_ms: int | None = None, verified_contracts: dict[str, dict[str, Any]] | None = None) -> ProofCertificate
Verify a Python function using Z3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to verify. |
required |
pre
|
Callable[..., Any] | None
|
Precondition lambda taking the same args as func.
Use |
None
|
post
|
Callable[..., Any] | None
|
Postcondition lambda taking |
None
|
timeout_ms
|
int | None
|
Z3 solver timeout in milliseconds. Defaults to
the global |
None
|
verified_contracts
|
dict[str, dict[str, Any]] | None
|
Contracts of called functions for composition. |
None
|
Returns:
| Type | Description |
|---|---|
ProofCertificate
|
class: |
ProofCertificate
|
|
Source code in src/provably/engine.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
verify_module ¶
Find all @verified functions in a module and return their certificates.
Walks the module's namespace looking for callables that have a
__proof__ attribute (i.e. functions decorated with
:func:~provably.decorators.verified).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType
|
A Python module object (e.g. from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ProofCertificate]
|
A dict mapping |
dict[str, ProofCertificate]
|
Functions without a |
Example::
import mymodule
from provably import verify_module
results = verify_module(mymodule)
for name, cert in results.items():
print(cert)