On this page
Proof enclaves in Vibe
A proof enclave is a module whose code may share mutable objects, through references that point at each other, while its clients see only values. Verada proves the module's code against the model its contracts state, and proves that each operation changes only its own objects' cells.
A linked stack
from verada import *
enclave()
@dataclass
class _Node:
value: int
next: Ref[_Node] | None = None
@dataclass(limited=True)
class Stack:
_repr: Cells[Ref[_Node]]
_nodes: Seq[Ref[_Node]]
_top: Ref[_Node] | None = None
_count: int = 0
def __invariant__(self) -> bool:
return (0 <= self._count and self._count <= 1000
and len(self._nodes) >= self._count
and ...)
def __initial__(self) -> bool:
return size(self) == 0
@ghost
def model(self) -> int:
return self._count
def size(self) -> int:
ensures(result == self.model())
return self._count
def push(self, x: int) -> None:
requires(self.size() < 1000)
ensures(self.size() == old(self.size()) + 1)
n = Ref(_Node(value=x, next=self._top))
...The complete module, which proves whole and runs the same under CPython, is test/programs/run/enclave-vibe-stack.
The forms
| Vibe | Meaning |
|---|---|
enclave(), first in the module |
The module is a proof enclave |
Ref[T], Ref[T] | None |
A reference to an object of a class T of the module, which may be shared; only in an enclave (elsewhere Owned[T]) |
Ref(T(...)), del r |
A new object and its reference; the object freed |
x = self._next |
Copies the reference (Python's sharing exactly): x is no view of the field |
_-named class |
In the module's private part: its references are the enclave's own, and never reach a client |
@dataclass(limited=True) with _ fields |
A class whose values are not copied, its fields private |
def __invariant__(self) -> bool |
The class's invariant, true outside the module's operations |
def __initial__(self) -> bool |
What is true of a new object (s = Stack()) |
@ghost |
A function for contracts and proof only, never run |
Seq[T] (.add, .get, .set, len) |
A model's sequence, its positions from 1; it takes no storage |
Cells[Ref[T]] (.add, .remove, in, for p in) |
A footprint: the cells an object owns; it takes no storage |
def __del__(self) |
The class's finalizer, run when an object ends |
A client creates an object with s = Stack() and calls its methods; it reasons about it through the model its contracts state, never through its references.
Translation
veradic translate --to vada writes the enclave as the Vada package it means: a package with Annotate => (Verada, Enclave), the class as a limited private type, _ names as Private_ names (_top is Private_top), the references' access type and Free, the footprint and sequence instances, and the special methods as aspects (Type_Invariant, Default_Initial_Condition, Finalizable). --to vibe from that package gives back the forms above, which run and prove as the original does.