On this page
Build a bounded stockroom application
This application manages three bins, each holding 0 through 100 items. A restock must fit the bin's remaining capacity (a precondition); a reservation that asks for more than the bin holds is declined (a normal outcome). The main program proves the exact total after each request and that a declined request leaves the stock unchanged.
Choose one syntax and read its route below; you don't need the other two.
Run the complete project
From the root of the Verada source tree, use your syntax's project:
veradic check --project docs/manuals/examples/stockroom/vada/verada.toml
veradic prove --project docs/manuals/examples/stockroom/vada/verada.toml
veradic run --project docs/manuals/examples/stockroom/vada/verada.tomlReplace vada with varc or vibe. The first two commands succeed; the run prints:
total: 61
last request: declined
bin 0 : 16
bin 1 : 15
bin 2 : 30
Build the same selected project for native delivery:
veradic build --project docs/manuals/examples/stockroom/vada/verada.toml -o stockroom-demo
./stockroom-demoAgain choose varc or vibe. The native program prints the same result.
Establish the data model
Bin is the index range 0 through 2. Quantity is Natural constrained to 0 through 100. A Shelf holds one Quantity for every Bin. From these types alone Verada proves that any Bin is a valid index and that adding three quantities cannot overflow.
Start at [10,20,30], whose total is 60. Restock bin 0 by 6, obtaining [16,20,30]. Reserve 5 from bin 1, obtaining [16,15,30], total 61. Request 99 from bin 2: the amount is a valid Quantity, but that bin has only 30. The operation declines and preserves the shelf.
The difference matters: asking for more than is in stock is a valid request that the application declines, while a restock that exceeds the capacity is an invalid call. A service taking deliveries from outside would check them, or report a handled outcome, before calling restock.
Specify both update and frame
restock requires amount <= 100 - current quantity. Writing the subtraction, rather than current + amount <= 100, keeps the precondition itself from computing a sum that could overflow. With it, the body's addition is proved to fit in a Quantity.
The postcondition gives the selected bin's exact new quantity and says that every other bin keeps its value. That second part, the frame, lets the caller keep what it knew about the other bins. The reservation has the same frame and also relates its Boolean result to the quantity on entry.
total is an expression function: its body is part of its declaration, so callers know exactly what it computes. A larger, reusable stock package would hide its representation and state its contracts through public observations instead.
Vada route
stock.vads is the public interface and stock.vadb its implementation. Items'Old is the shelf on entry, so Items'Old (Position) is the selected bin's quantity on entry. Reserve reports its outcome through an out parameter.
package Stock is
subtype Quantity is Natural range 0 .. 100;
subtype Bin is Natural range 0 .. 2;
type Shelf is array (Bin) of Quantity;
procedure Restock (Items : in out Shelf; Position : Bin; Amount : Quantity)
with Pre => Amount <= Quantity'Last - Items(Position),
Post => Items(Position) = Items'Old (Position) + Amount
and (for all I in Bin => (I = Position or else Items(I) = Items'Old (I)));
procedure Reserve (Items : in out Shelf; Position : Bin; Amount : Quantity;
Accepted : out Boolean)
with Post => Accepted = (Amount <= Items'Old (Position))
and Items(Position) = (if Accepted then Items'Old (Position) - Amount else Items'Old (Position))
and (for all I in Bin => (I = Position or else Items(I) = Items'Old (I)));
function Total (Items : Shelf) return Natural is
(Items(0) + Items(1) + Items(2));
end Stock;package body Stock is
procedure Restock (Items : in out Shelf; Position : Bin; Amount : Quantity) is
begin
Items(Position) := Items(Position) + Amount;
end Restock;
procedure Reserve (Items : in out Shelf; Position : Bin; Amount : Quantity;
Accepted : out Boolean) is
begin
if Amount <= Items(Position) then
Items(Position) := Items(Position) - Amount;
Accepted := True;
else
Accepted := False;
end if;
end Reserve;
end Stock;The main program asserts the outcome and the total after each request; the prover proves those assertions from the contracts alone. The final loop runs over Bin, so it needs no assumption about where the array starts.
with Ada.Text_IO;
with Stock;
procedure Main is
Items : Stock.Shelf := [10, 20, 30];
Accepted : Boolean;
begin
Stock.Restock (Items, Position => 0, Amount => 6);
Stock.Reserve (Items, Position => 1, Amount => 5, Accepted => Accepted);
pragma Assert (Accepted);
pragma Assert (Stock.Total (Items) = 61);
Stock.Reserve (Items, Position => 2, Amount => 99, Accepted => Accepted);
pragma Assert (not Accepted);
pragma Assert (Stock.Total (Items) = 61);
Ada.Text_IO.Put_Line ("total:" & Natural'Image (Stock.Total (Items)));
if Accepted then
Ada.Text_IO.Put_Line ("last request: accepted");
else
Ada.Text_IO.Put_Line ("last request: declined");
end if;
for I in Stock.Bin loop
Ada.Text_IO.Put_Line ("bin" & Natural'Image (I) & " :" & Stock.Quantity'Image (Items(I)));
end loop;
end Main;Varc route
The public declarations and their bodies share stock.va. pub marks the API, and the inout and out modes mark parameters the operation writes; the caller passes those with &. old(items) is the shelf on entry, and old(items)[position] the selected bin's quantity on entry.
package Stock {
pub subtype Quantity = Natural range 0..=100;
pub subtype Bin = Natural range 0..=2;
pub type Shelf = [Bin]Quantity;
pub fn restock(items: inout Shelf, position: Bin, amount: Quantity)
requires amount <= Quantity::Last - items[position]
ensures (items[position] == old(items)[position] + amount)
&& (forall i in Bin => i == position || items[i] == old(items)[i])
{
items[position] += amount;
}
pub fn reserve(items: inout Shelf, position: Bin, amount: Quantity, accepted: out Boolean)
ensures (accepted == (amount <= old(items)[position]))
&& (items[position] == (if accepted { old(items)[position] - amount } else { old(items)[position] }))
&& (forall i in Bin => i == position || items[i] == old(items)[i])
{
if amount <= items[position] {
items[position] -= amount;
accepted = True;
} else {
accepted = False;
}
}
pub fn total(items: Shelf) -> Natural = items[0] + items[1] + items[2];
}
Named arguments use the parameter names, with & on the argument. As in Vada, the assertions are proved from the public contracts, not from the bodies.
import Ada.Text_IO;
import Stock;
fn main() {
var items: Stock.Shelf = [10, 20, 30];
var accepted: Boolean;
Stock.restock(&items, position: 0, amount: 6);
Stock.reserve(&items, position: 1, amount: 5, accepted: &accepted);
assert accepted;
assert Stock.total(items) == 61;
Stock.reserve(&items, position: 2, amount: 99, accepted: &accepted);
assert !accepted;
assert Stock.total(items) == 61;
Ada.Text_IO.Put_Line("total:" ++ Natural::Image(Stock.total(items)));
if accepted {
Ada.Text_IO.Put_Line("last request: accepted");
} else {
Ada.Text_IO.Put_Line("last request: declined");
}
for i in Stock.Bin {
Ada.Text_IO.Put_Line("bin" ++ Natural::Image(i) ++ " :" ++ Stock.Quantity::Image(items[i]));
}
}
Vibe route
The module declares the constrained types as annotated aliases and the shelf as an Array class, and imports the contract functions from verada. reserve changes the shelf it is given and returns whether it accepted the request.
from verada import Annotated, Array, Interval, Natural, ensures, old, requires, result
type Quantity = Annotated[Natural, Interval(ge=0, le=100)]
type Bin = Annotated[Natural, Interval(ge=0, le=2)]
class Shelf(Array[Quantity], index=Bin):
pass
def restock(items: Shelf, position: Bin, amount: Quantity) -> None:
requires(amount <= 100 - items[position])
ensures(items[position] == old(items)[position] + amount)
ensures(all(i == position or items[i] == old(items)[i] for i in range(3)))
items[position] += amount
def reserve(items: Shelf, position: Bin, amount: Quantity) -> bool:
ensures(result == (amount <= old(items)[position]))
ensures(items[position] == (old(items)[position] - amount if result else old(items)[position]))
ensures(all(i == position or items[i] == old(items)[i] for i in range(3)))
if amount <= items[position]:
items[position] -= amount
return True
return False
def total(items: Shelf) -> Natural:
return items[0] + items[1] + items[2]The main program gives each returned outcome a declared type, and the call that changes the shelf is the whole right-hand side of that declaration.
from stock import Shelf, reserve, restock, total
def main() -> None:
items = Shelf([10, 20, 30])
restock(items, position=0, amount=6)
accepted: bool = reserve(items, 1, 5)
assert accepted
assert total(items) == 61
last_accepted: bool = reserve(items, 2, 99)
assert not last_accepted
assert total(items) == 61
print("total:", total(items))
if last_accepted:
print("last request: accepted")
else:
print("last request: declined")
for i in range(3):
print("bin", i, ":", items[i])
if __name__ == "__main__":
main()Make an invalid caller deliberately
In a copy of the project, change the restock amount from 6 to 91. Bin 0 starts with 10, so it has room for 90. 91 is a valid Quantity, but it breaks the precondition, and prove fails with B5101 in each syntax.
Repair it with an amount that fits, by checking the delivery before the call, or by changing the operation to report a capacity outcome. Don't weaken the precondition without changing the body and its postcondition with it.
Review what the application proves
The proved assertions establish that the first reservation succeeds, that the total is exactly 61, that the second request is declined and that the total is unchanged. The contracts prove each operation's update and frame for every permitted call, not just for these three requests.
The application reads no input, stores nothing and runs no tasks. Adding any of those brings new validation and design questions. Its proof relies on the library's contracts and on the compiler; veradic trust lists what it trusts.
Extend it with a requirement
The exercises try a zero-sized request, a fourth bin, a missing frame and an invalid index. Decide the required behavior before editing the source; then check, prove and run the changed project, and keep its expected output and a failing case next to it.