33. Module initialisation: registries, proved @init hooks, and opt-in dynamic registration¶
Date: 2026-11-21
Status: Accepted
Context¶
ADR 0024 keeps Pia images free of dyld-run static initializers, so the certificate check always runs before any guarded code. Dean accepted it on condition that Pia gets a supported hook and a standardised registration mechanism for initialisation. In C++ that is implementation-defined; in Pia it must be well-defined and proved in advance. He added that at dynamic load time a library should call a runtime-provided function that registers its initialisers, much like the REPL (ADR 0031) but without a JIT: newly loaded symbols and functions are registered with the runtime and can be looked up, including through an eval-like mechanism.
C++ static initialization has an unspecified cross-unit order, lets one initializer read another's unconstructed object, lets initializers throw, loop or start threads, and runs them inside the loader. Ada's elaboration checks, Go's dependency-ordered init, Rust's refusal of life-before-main, and linker sets are the main alternatives. The full design is in docs/init.md.
Decision¶
- Registries for registration without code.
let r : registry t where Pdeclares one;@register(M.r) let e = ...adds a compile-time constant entry, proved againsttwhere it is defined. The linker collects entries into a constant table ordered by module name then entry name. Whole-set properties such asunique (.name)are checked by the static linker and again by the loader when libraries add entries. - One
@initper module, for state that must be built at run time. It returns the module state's initial value and establishes its invariant; its signature and contract are in the interface hash. It must be proved to terminate, is infallible unless it declares an error set (which is then in the interface), gets only the capabilities ininit_env(root allocator, arguments, environment, clock), and cannot spawn, await or lock. Teardown is the state'sDrop, which cannot fail. - A proved order. Every function that reads module
M's state has an implicit preconditioninitialised M. Inits run in a topological order of the init dependency graph (an edge where an init reads another module's state, which must be an import), with ties broken by module name. Reading state that is not yet initialised is a verification error, and a cycle is a compile or link error. Dependency sets go in the certificate, so the order is computed from certificates alone. - The runtime runs inits, never dyld. Images carry
__TEXT,__pia_initand__DATA_CONST,__pia_regtables and no__mod_init_func. At launch, the Pia entry point runs the certificate check, the registry checks, the inits in order,main, then the drops in reverse. A failed init drops what already ran, andmainnever runs. - Opt-in dynamic registration,
Pia.Dyn, on the REPL's environment model without a JIT. Each image has one compiler-generated registration function,_pia$register$<image>, whichPia.Dyn.opencalls after the certificate check. It hands the runtime constant tables of symbols (name, interface hash, pointer, eval adapter), registry entries and inits. The runtime rejects name clashes, checks registries, runs the new inits in order, and undoes everything on failure.Pia.Rtdoes not containPia.Dyn, just as it does not containPia.Live. - Typed lookups.
Pia.Dyn.bind env I libreturns a record typed by interfaceIonly if the library registeredI's hash, or a version proved to refine it.findandbind_symbol<t>do the same for one symbol. So a lookup never returns a value whose contract differs from its certificate. evalondynvalues.Pia.Dyn.eval env name argschecks argument tags against the registered type, and checks the function'srequiresat run time with a compiled checker (the eval adapter), because no proof covered the call. Results satisfy theensures. Only exports marked@evalget adapters, and the certificate lists them.
Implementation¶
Issues: registries and @register (#100); @init, initialised M and the init order (#101); __pia_init/__pia_reg and the launch sequence in Pia.Rt (#102); Pia.Dyn with registration functions, open and close (#103); typed bind, find and eval with eval adapters (#104).
Consequences¶
- Initialisation order is a pure function of the program's modules, and the order fiasco becomes a proof goal with a counterexample.
- Most static-initializer uses need no run-time code at all: registries are constant data.
- Pia images still run nothing at dyld load, so ADR 0024's soundness argument holds.
- Programs that never load code dynamically carry none of
Pia.Dyn. - The REPL and dynamic loading share one environment representation and one notion of compatible change.
evaltrades proofs for run-time precondition checks, visibly, and only where a module opts in.