HoldFastPlayground

Modal unification and two-level staged polymorphism

Design for unifying HoldFast's ownership, capture, handler, and grade modes and eliminating duplicated APIs through compile-time mode and effect-row polymorphism beyond the first-release subset.

Scope and boundary

The modal design gives loan modes, locality, aliasing, contention and portability a common order. It pairs that order with a capture set for each binding. The staged design uses mode and effect-row parameters to share a single definition across several concrete interfaces.

Specialization resolves these parameters before runtime and produces a monomorphic signature. The prototype checks the specialization and records its inputs in a digest. Represented runtime lowering requires extension target transport. Exclusive access still requires the resource evidence carried in Delta.

Order the five modal axes componentwise. The relation a <= b means a value of mode a can be used where mode b is expected.

Axis Subtyping order (from <= to) Role in HoldFast
Locality Global <= Local Global values can be used in region-scoped blocks. Local values require a crossing proof to escape.
Uniqueness Unique <= Aliased Unique values can satisfy an aliased interface. A loan still requires its own resource evidence.
Linearity Many <= Once A repeatable value can satisfy a single-use interface.
Contention Contended <= Shared <= Uncontended Thread-safe contended access coerces down to single-task uncontended use
Portability Portable <= Nonportable Crossing a spawn boundary requires portable captures and the task's transfer premises.

Each binding also carries C : CaptureSet, the reachable capability and place identities. A closure crossing to Global or Portable needs every capture to satisfy the destination mode. Handler capture rules

The prototype projects synchronous modes into the following coordinates.

Surface mode Locality Uniqueness Linearity Contention Portability
owned T Global Unique Once Uncontended Portable for pure values, else Nonportable
in T Local if region-scoped, else Global Aliased Many Shared Portable if global and pure, else Nonportable
ref T or out T Local if region-scoped, else Global Unique Once Uncontended Nonportable

The modal checker compares concrete coordinates. The staged collection templates each check one mode parameter against an explicitly enumerated set. The symbolic staged collection trial compares direct propagation, a reduced decision diagram, and a solver that saturates order constraints and prunes partial assignments using lattice bounds. All three solve inequalities with meet and join over finite declared modes. A control for an empty elimination keeps the two ways of discharging a provisional binding as an explicit disjunction. The admitted choices replay through the concrete staging and capture checks. A separate synchronous control checks loan restoration before a write. OxCaml mode crossing · Adjoint mode inference · Staged collection workload

Mode crossing and adjoint boundaries

Mode crossing uses the representation to justify a mode change beyond ordinary subtyping. The scalar model admits crossing on all five axes. The flat immutable model admits crossing on locality, linearity, contention and portability while still enforcing the uniqueness order. Mutable references and capabilities follow ordinary subtyping. An admitted representation and resource theorem must justify classifying an actual value as an authority-free scalar or immutable value.

In the Rocq model, lock_mode sets locality to Local and unlock_mode sets it to Global. The other four axes stay fixed. lock_unlock_galois proves the adjunction, and the unit and counit are lock_unlock_unit and unlock_lock_counit. Connecting this model to region-delimited code and handler capabilities requires the source and resource proofs.

Two-level compile-time mode and effect-row polymorphism

A staged template declares compile-time mode and effect parameters. This schematic signature uses an explicit set of modes and an effect upper bound.

HoldFast
template fn Get<mode m in [In, Ref], effect E <= {reads(self)}>
  (m Buffer self, usize idx) -> m Element ~> E;
  • Stage discipline: Stage variables (SMVar, SEVar) exist strictly in StageExpr/StageTy. After specialization (specialize_ty, specialize_expr), the emitted ObjTy/ObjExpr contain zero stage variables (specialize_ty_stage_free, erased_object_stage_free, contrasted with stage_var_not_stage_free).
  • Symbolic body soundness: Template checking (TemplateTyped) rejects any body that uses an exclusive/mutating operation (SEMutWrite) unless every admitted mode instantiation allows mutation (unconstrained_mode_cannot_mutate), and rejects any effect not permitted by max_eff.
  • Pure specialization. A specialization at E = {} succeeds only when the body requires no effects. pure_instantiation_has_no_effects checks the modeled result, and the Python prototype records the accepted specialization in a deterministic SHA-256 SpecializationReceipt.

Proof obligations

Obligation Required artifact and refusal predicate
MA-lattice Partial-order (refl, antisym, trans) and join/meet semilattice laws over the five mode axes, lock_unlock_galois, and CaptureSet inclusion; reject any non-monotonic coercion.
MA-crossing Prove ShapeScalar / ShapeImmutableFlat safe Local -> Global crossing (mode_cross_scalar_global, mode_cross_immutable_flat_local_to_global), Aliased -> Unique refusal for ShapeImmutableFlat, and Local -> Global refusal for ShapeMutableRef and ShapeCapability.
MA-capture-escape Prove modal_typed_closure_valid and local_capture_cannot_escape_global so no Local capture identifier escapes into a Global-moded closure; reject escape at the binding/return span.
SP-staging Prove stage_var_not_stage_free, specialize_ty_stage_free, and erased_object_stage_free so runtime code never depends on unresolved stage variables.
SP-preservation Prove symbolic template well-typedness implies well-typedness of every constraint-satisfying monomorphic instance (instantiate_preserves_typing); reject unconstrained Unique/mutation access.
SP-effect-row Prove effect-row monotonicity (effect_row_sub_monotonic) and exact pure-instantiation effect absence (pure_instantiation_has_no_effects); reject any hidden effect exceeding the declared upper bound.

Qualification and implementation artifacts

Component Artifact Role
Rocq modal mechanization modal_adjoint.v Zero-axiom ModalAdjoint semilattice, capture set, crossing, and escape-freedom proofs (rocqchk)
Modal checker prototype modal_algebra.py Five-axis Mode, CaptureSet, crossing rules, and surface mode lowering
Rocq 2LTT mechanization staged_polymorphism.v Zero-axiom StagedPolymorphism template typing, stage erasure, and safety theorems (rocqchk)
Staged specializer prototype staged_poly.py Symbolic template checker, monomorphic specializer, and SHA-256 specialization receipts
Qualification records Q25en · Q25eo Modal and staged polymorphism