<!-- description: Explore HoldFast prototypes for amortized work budgets, guarded interaction trees for reactive services, and proofs for packed CHERI layouts. -->

# Linear resource credits, guarded interaction trees, and bit-level layouts

> Design for amortized time/resource credits in `Delta`, cofixpoint-free guarded
> Interaction Trees for reactive services, and Dargent-style declarative layout
> lenses beyond the [first-release subset](../../language/first-release/#release-boundary).

## Scope and boundary

The prototypes address three distinct obligations.

1. Linear credits and amortized potential account for work in the
   [resource context](../graded-foundation/#dimensions-and-lawful-domains).
2. Step-indexed interaction trees model reactive loops within the
   [minimal CIC profile](../../assurance/verification-strategy/#research-and-semantic-boundaries).
3. Declarative layout lenses describe packed field access, padding and CHERI
   capability alignment for representation proofs.

<a id="unified-linear-resource-credits"></a>

## Unified linear resource and amortized time credits

Resource credits extend `Delta` with tokens `$(n)` for nonnegative work
budgets. Each credit can be spent once.

- `credit_split` partitions a budget across subcomputations.
  `credit_conservation` accounts for spending and `credit_frame_rule` preserves
  credits outside a call's budget.
- When every loop iteration or recursive call spends at least one credit, a
  finite budget bounds the number of iterations or calls. The model checks
  this connection with `finite_positive_credit_loop_terminates` and
  `loop_exec_bounded_by_credits`.
- `PotentialJudg` combines credits with stored potential `Phi(state)`.
  The two-stack queue proofs use that potential to pay for occasional transfers
  under a constant amortized operation budget.

The affine inference trial chooses the least natural coefficient `k` for
`Phi(size) = k * size`. Each transition requires
`cost + k * after <= budget + k * before` for all natural input sizes.
The input gives `before`, `after`, `cost` and `budget` as nonnegative
`[constant, slope]` pairs in a `transitions` array. The producer computes
coefficient bounds and emits a Rocq proof of the inequalities over those
rows. The locked queue trial connects the rows to provider behavior and
checks an unchanged caller against a private representation edit. Its budget
uses a source operation count.
[Potential Functions as Types](https://arxiv.org/html/2607.08547v2) ·
[Command](https://github.com/symbiogenesis/HoldFast/blob/main/tools/README.md#commands) ·
[Queue replay](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/test_holdfast_resource_credits.py)

This input infers coefficient two. Enqueue adds one item of potential, and
refill spends two work units per pending item.

```json
{
  "transitions": [
    {"label": "enqueue", "before": [0, 1], "after": [1, 1], "cost": [1, 0], "budget": [3, 0]},
    {"label": "refill", "before": [0, 1], "after": [0, 0], "cost": [1, 2], "budget": [1, 0]}
  ]
}
```

<a id="guarded-interaction-trees"></a>

## Interaction trees and copattern guarded recursion

The reactive-service prototype uses finite interaction trees (`ITree E R`)
and a copattern guardedness check within the cofixpoint-free kernel profile.

- `ITree E R` exposes fuel exhaustion (`ITFuelOut`), return (`ITRet r`),
  an internal step (`ITTau k`) or a visible event (`ITVis e k`). The event
  signature is `E : Type -> Type`. The model relates runs across finite horizons
  with `approx` and `guarded_iter_coherent`.
- `guarded_iter` emits an internal step or a visible event before each recursive
  unfolding. `guarded_iter_productive` describes the emitted step and the
  structurally smaller fuel budget.
- **Trace and handler laws:** Monadic laws (`bind_ret_l`, `bind_ret_r`,
  `bind_assoc`), handler interpretation (`interp_ret`, `interp_bind`), and weak
  equivalence up to finite `ITTau` steps (`eutt_n`, `eutt_n_tau_l`,
  `eutt_n_tau_r`) discharge reactive protocol theorems such as bounded
  request-response packet echo services (`echo_server_responds`).

The finite copattern prototype visits sequence and branch children in their
listed order and checks guardedness separately for each branch alternative.
A reachable recursive call restarts the body. A finite body emits a return
state, with each internal or visible step consuming one unit of horizon fuel.
The handler must cover every reachable visible event, including events beyond
the simulated prefix. Rocq replays the emitted service's state equations and
independently specified traces. [Replay controls](https://github.com/symbiogenesis/HoldFast/blob/main/tools/tests/test_holdfast_guarded_itrees.py)

<a id="declarative-bit-level-layouts"></a>

## Declarative bit-level layout and lens algebra

The layout prototype takes field offsets and widths for packed wire headers
and CHERI-capable structures.

- **Disjointness and bounds:** Every declared field `BitRange { bit_off, bit_width }`
  must have positive width, fit within total layout size, and be pairwise
  disjoint from all sibling fields (`ranges_disjoint`).
- **CHERI capability alignment:** Fields holding hardware capabilities enforce
  a 128-bit (16-byte) alignment and width. The layout proof checks disjointness
  from scalar fields. The machine interpretation must still establish the
  behavior and tag effects of capability loads and stores.
- **Synthesized lens laws:** Bitwise `get_bits` / `set_bits` operations paired
  with `zero_padding` generate closed-context Rocq lemmas for round-trip
  (`get_set_same`), last-write behavior (`set_set_same`), disjoint frame
  preservation (`get_set_other`), and padding confidentiality
  (`zero_padding_clears_pad_bit`).

## Proof obligations

| Obligation | Required artifact and refusal predicate |
| --- | --- |
| `RC-soundness` | Prove `credit_soundness`, `credit_frame_rule`, `credit_conservation`, and `finite_positive_credit_loop_terminates`; reject credit underflow or forged/duplicated tokens. |
| `RC-amortized` | Prove potential-transfer invariants (including two-stack queue `enqueue`/`dequeue` constant amortized credits); reject unbounded loops without decreasing potential. |
| `IT-monad-eutt` | Prove `ITree` monad and `interp` laws in minimal CIC strictly without `CoInductive`/`CoFixpoint`/`cofix`; reject any coinductive kernel construct. |
| `IT-productive` | Prove `guarded_iter` advances horizon fuel across every loop path; reject unguarded silent recursion (`RecCall` not behind `Tau`/`Vis`). |
| `LL-lens-laws` | Prove `get_set_same`, `set_set_same`, and pairwise `get_set_other` over declared bit ranges; reject overlapping or out-of-bounds fields. |
| `LL-padding-cheri` | Prove `zero_padding_preserves_field`, `zero_padding_clears_pad_bit`, and CHERI 128-bit granule alignment/disjointness; reject misaligned capability fields. |

## Qualification and implementation artifacts

| Component | Artifact | Role |
| --- | --- | --- |
| Rocq credit mechanization | [resource_credits.v](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/resource_credits.v) | Zero-axiom `ResourceCredits` linear/amortized credit and two-stack queue proofs (`rocqchk`) |
| Credit checker prototype | [resource_credits.py](https://github.com/symbiogenesis/HoldFast/blob/main/tools/toolchain/resource_credits.py) | Linear credit/potential verifier and Gallina budget emitter |
| Rocq ITree mechanization | [guarded_itrees.v](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/guarded_itrees.v) | Cofixpoint-free step-indexed `GuardedITrees` monad, `guarded_iter`, and reactive echo proofs (`rocqchk`) |
| Guardedness/ITree prototype | [guarded_itrees.py](https://github.com/symbiogenesis/HoldFast/blob/main/tools/toolchain/guarded_itrees.py) | Copattern guardedness checker, ITree simulator, and minimal-CIC emitter |
| Rocq layout lens mechanization | [layout_lenses.v](https://github.com/symbiogenesis/HoldFast/blob/main/tools/rocq/layout_lenses.v) | Zero-axiom `LayoutLenses` bitfield get/set, frame, padding, and CHERI granule proofs (`rocqchk`) |
| Layout lens prototype | [layout_algebra.py](https://github.com/symbiogenesis/HoldFast/blob/main/tools/toolchain/layout_algebra.py) | Bit-layout validator, runtime lens, and Gallina `.Model` lemma emitter |
| Qualification tracks | [Q25es](../../assurance/completion-log/#q25es-unify-linear-resource-and-amortized-time-credits-in-delta) · [Q25et](../../assurance/completion-log/#q25et-qualify-cofixpoint-free-guarded-interaction-trees) · [Q25eu](../../assurance/completion-log/#q25eu-qualify-declarative-bit-level-layouts-and-cheri-lens-proofs) | Linear credits, guarded ITrees, and bit-level layouts |
