On this page
Projects, packages and public APIs
A function is useful to other code only if that code can find it, understand what it requires and use it without depending on how it is written. The stockroom introduces that boundary with one stock API and one main program.
Lay out the project
Each syntax keeps the API, its implementation and the application in these files:
| Syntax | API | Implementation | Application |
|---|---|---|---|
| Vada | stock.vads |
stock.vadb |
main.vadb |
| Varc | pub items in stock.va |
The same file | main.va |
| Vibe | Public names in stock.vb |
The same file | main.vb |
Each project has a verada.toml with source-dirs = ["."]. Verada finds units only in the project's source directories; it does not search your machine for imports.
In Vada, with Stock; makes the package specification visible, and the body implements it. In Varc, import Stock; makes the package's pub declarations visible. In Vibe, from stock import reserve binds reserve, import stock binds the module (stock.reserve), and as renames either; names starting with an underscore stay private to their module.
These are three spellings of one boundary. Every operation has the same parameter modes, types, visibility and contracts whichever syntax declares it.
Design an API around valid requests
restock has a capacity precondition: its caller must know enough about the stock to show that the delivery fits. reserve handles insufficient stock itself and reports the outcome. They divide responsibility differently because the failures belong to different parts of the application.
A caller can only use what the interface says. If a function's contract says only that it returns a Natural, the caller knows the result is nonnegative and nothing more, not that it is the number of items in stock. Write the postcondition, or make the function a visible expression function, so the caller can use its meaning.
An interface can also expose too much. The stockroom makes its shelf a public array so the example stays transparent. A reusable stock service would use a private type with public observations, such as the total quantity and the quantity in one bin, and state its contracts through those.
State what changes and what stays the same
The stockroom's update contracts give the selected bin's new quantity and also say that every other bin keeps its old value. The second part is a frame condition: it lets a caller keep what it knew about the bins the update did not touch.
Without it, a caller could prove that bin 1 changed correctly and still fail to prove the total. The body does leave the other bins alone, but the caller reasons only from the interface, never from the body.
For package-level state, Global says which state an operation reads and writes, and Depends says which inputs affect which outputs. They complement a postcondition; they don't replace it. A package that hides its state names it as abstract state and refines it in the body. See initialization, effects and abstract state.
Use visibility on purpose
A Vada package's private part completes hidden types and objects, and child packages can see it. A Varc package keeps everything not marked pub hidden, and pub(child) makes a declaration visible to child packages only. A Varc struct is either wholly public (every field pub) or a private type (no field pub).
A project has one main program: one parameterless library-level procedure (a main function in Vibe). See main programs.
Reuse generics without losing type identity
A generic takes compile-time parameters such as an element type, a capacity or an ordering function. The formal vector example instantiates a bounded vector of Natural with capacity 4 and creates, appends, replaces, deletes and clears in Vada and Varc.
Each instance is its own type. Two instances declared separately are different types even when their parameters are the same, so share one instance across modules: declare it once and import it everywhere. Generic declarations and instances explains instance identity when an apparently identical value fails a type check.
Mix syntaxes in one project
A project can contain units in different syntaxes: a Vada package can be called from Varc or Vibe, and the other way round. Treat the boundary as an API:
- Keep one definition of each package or module in the project.
- Keep its names, types, parameter modes and contracts; callers in every syntax see the same declarations.
- Write each call in the caller's syntax: Varc marks writable arguments with
&, and Vibe receives a procedure'soutand scalarin outparameters as the call's results, in order:value = stock.increment(value),low, high = stock.split(7)(imported scalar outputs). - Check and prove the whole project, then run it.
The compiler's mixed-syntax test project is a complete example. The three stockroom directories are separate projects in different syntaxes; putting them in one directory would define Stock three times.
veradic translate rewrites units into another syntax. Check, prove and run the translated project, and compare its output with the original's; see translation.
Share an API
Keep the source, verada.toml and a few representative tests together. veradic doc writes reference pages from the specifications and their comments, and veradic stubs writes .pyi stubs of the verada module and of the Vada and Varc packages you give it, so Python type checkers understand the Vibe modules that import them. Neither checks the implementation; prove does.
When you change a public contract, review its callers. A stronger precondition can reject calls that used to be valid; a weaker postcondition can remove facts that callers rely on. A change to a body alone should keep the public contract, the proof and the output the same. The automation recipe makes that review repeatable.