Skip to content

Arrays: Array A as a primitive family of leaf places (SPEC §9, phase 1 of #8) - #73

Merged
empath-nirvana merged 9 commits into
mainfrom
feature/array
Sep 27, 2026
Merged

empath-nirvana merged 9 commits into
mainfrom
feature/array

Conversation

@empath-nirvana

Copy link
Copy Markdown
Owner

Phase 1 of #8: Array A, a primitive value-type former, is a run-time-length family of leaf places. This PR does not close #8.

Spec (SPEC 0.8, §9 "Entry 8 — arrays"; decision 29)

  • Array A = [Ref A] and ArrayImm 'a A = [Imm 'a A]. The new former [L] (VTy.fam) is the iterated separating conjunction ⊛_{i<n} L, and it is never persistent.
  • The machine gains three Prim steps:
    • tab (c, f) places len(c) fresh consecutive cells, then fills them from f.
    • idx ((ℓ,n), i) returns inl (ℓ+i) when i < n unsigned, and inr () otherwise, so it never gets stuck.
    • drop (f, (ℓ,n)) frees each cell after handing its contents to f.
  • Change from the issue's plan: an array is borrowed in a cell, with withbor1/withbor2 on Ref (Array A).
    • withload at Imm 'x (Array A) gives Imm̲ 'y (Array A) = [Imm 'y A] at a shorter 'y. This is sound by the fam cases of 6.131.
    • Mutation through Mut uses withswap with the owned array_replace.
    • A primitive array borrow with no cell, and Mut families, move to Open. The nested-frame proof needed a Q-stability fact that I did not establish; I have not verified that any obstruction there is real.
  • Constants: array_new, array_free, array_size, array_replace, array_len, array_get, array_copy, array_forget.
  • Buf/Str are withdrawn as externs. Vec, Str and u8 are recorded as library forms over arrays.

Why a same-lifetime leaf view is not the excluded view through a pointer

A family [Imm 'y A] holds its leaves' imm cells at the top level of the resource, and no cell owns them.

  • array_get hands out one leaf at 'y and forgets the rest (NoOwn). No view is taken through an owning cell.
  • The excluded view is the one taken at 'x directly from Imm 'x (Array A), where the leaves sit under the owning cell. That is experiments/FrameEnd's imm_view_rejected.
  • That view is avoided because withload produces the family at the shorter 'y (6.131's fam case, reborrow_sepRange).

The model did not forbid the family reading, so there was no STOP.

DoD

  • Spec first: §9 rewritten, version 0.8, changelog, Status, decision 29.
  • Former VTy.fam, never persistent, through relation, Imm̲, outlives, NoMut, shapes, invariant and destructor (HasDtor.array / arrayImm).
  • Machine steps via Prim. Out of range goes to inr, never stuck.
  • Constants with compat lemmas (Core/Arrays.lean, Core/ArrayWp.lean). fundamentalC and adequacyC are unchanged in statement. arrProg_runs (adequacy) and arrProg_eval (interpreter, by decide).
  • Family borrowing as a primitive rule: not added. Borrowing goes through the cell and withload (see above). The primitive borrow and Mut families are Open.
  • Checker: array A, array_imm 'a A, the eight constants, implicit array destructors. checkProgram_sound is unchanged in statement.
  • Backend: contiguous allocation of max(n,1) two-word slots, base + 16·i addressing with a bounds check, element destructors on free. The closure IR and flat IR interpreters are extended.
  • Corpus, all at live 0 including under Wasm:
    • array-sum: a 1000-element tabulate and sum, giving 500500.
    • array-get: includes an out-of-range get, which returns inr ().
    • array-replace: replace, then read back.
    • array-refs: owned Refs dropped implicitly.
    • array-empty.
    • Must-reject: array-view-escape, array-mut-while-viewed, array-use-after-free.
  • Book chapter "Arrays", with doc-tested examples. book-doctest gives 53/53.
  • Local checks: lake build is clean, and hygiene reports 0 forbidden keywords and 16522 declarations within [propext, Classical.choice, Quot.sound]. The parse, check and compile corpus scripts pass. mdbook build was not run locally because mdbook is not installed; CI runs it.
  • Experiments:
    • Grades gains the array constants.
    • FrameTheorems' StepsNA also excludes array redexes, because tab is nondeterministic.
    • TwoRun's renaming and fresh runs are restricted to runs with no array step, since ℓ + i is not preserved by a permutation. The READMEs say so. Whether they extend under a block renaming: not derived, not verified.
    • Optics and Affine build unchanged.
    • The HostIs-broken experiments were already broken on main.

Open

split_at, iter / iter_mut, Vec, Str, u8, Uninit, the primitive (cell-free) array borrow, and Mut leaf families.

Refs #8

🤖 Generated with Claude Code

https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK

John Thompson and others added 9 commits September 27, 2026 14:07
…ion 29

§9 is revised: [L] (VTy.fam) is the run-time family ⊛_{i<n} L of leaf places,
Array A = [Ref A], and a borrowed array is the family of its leaf borrows. The
entry states the values (base and length), the relation clause, the three
machine steps (tab, idx, drop), the constants of phase 1, the array borrows,
the LLVM-portable lowering, and what is library (Vec, Str, u8) and open
(split_at, iter, Uninit, sized types). Buffers and strings as externs are
withdrawn. Decision 29 records why arrays are primitive and records are not
arrays.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
Prim gains tab, idx and drop. BoLo.Head gains one step each: tab places
len(c) cells holding () at fresh consecutive locations and continues as
tabFill, which stores f i in cell i and returns (ℓ, n); idx answers inj₁ (ℓ+i)
when i < n unsigned and inj₂ () otherwise; drop continues as dropFill, which
frees each cell in order and hands its contents to f. The interpreter
allocates the block at next. The closure IR names the three operations.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
VTy gains fam L, the run-time family ⊛_{i<n} L of leaf places, whose value is
(ℓ, n) with leaf i at ℓ+i. The relation and the shape relation read it as the
iterated separating conjunction sepRange (Core/SepRange.lean) of L's clause at
the leaf locations; Imm̲ 'b [L] = [Imm̲ 'b L]; [L] outlives what L outlives;
NoMut, Cl, Occ, substitution and TyEq pass through; [L] is not persistent.
RefPos and MutPos gain the leaf positions of a family, and every lemma the
typed world proves by induction on types or positions gets its family case:
6.131 (reborrow_vShape), 6.60 (vC_outlives), the image, invariant and
relabelling lemmas, stability along Ext and the observable view. The checker's
deciders and printer read the former; it has no destructor yet.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
…es from withload

§9 amended before the array constants land: an array is a value and is
borrowed as any value is, in a cell (Imm/Mut @A (Array A)). withload at
Imm @A (Array A) gives the family [Imm 'x A] at a shorter lifetime, sound by
6.131 at [L]; mutation through Mut is withswap over the owned operations. The
primitive array borrows and mutable families move to Open. The constants are
array_new, array_free, array_size, array_replace (owned), and on families
array_len, array_get, array_copy, array_forget. Decision 29 follows.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
…tor [spec §9]

CDerives gains array_new (tab), array_free (drop), array_size and
array_replace on owned arrays, and array_len, array_get (idx), array_copy and
array_forget on the family of leaf views; fundamentalC covers each, so
adequacyC is unchanged in statement. tab is n of TW.alloc at a fresh run of
locations (alloc_block, wpTS_tab), and the fill and the drain are proved by
induction over the cells (tabFill_wp, dropFill_wp). array_get hands out one
leaf's imm cell at the family's lifetime and forgets the others, which hold no
own cell. The family itself comes from withload at Imm @A (Array A), sound by
the [L] cases of 6.131. HasDtor gains Array A (array_free with the element's
destructor) and ArrayImm @A A (array_forget). ArrayPrograms.lean runs one
program by adequacy and by the interpreter.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
The core syntax gains the types array A ([ref A]) and array_imm @ A
([imm @ A]), and the constants array_new, array_free, array_size,
array_replace, array_len, array_get, array_copy and array_forget, each read
with its bracket arguments and checked by one CDerives constructor at a dead
context. The destructor decider knows arrays: array A when A has one,
array_imm @A A always. checkProgram_sound is unchanged. syntax-core §8b
documents them; a round trip is checked by the kernel.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
The closure IR interprets tab, idx and drop as the source machine does: an
array is consecutive cells, tab calls a copy of the tabulator per element and
drop hands each element to a copy of the consumer. The flat IR lowers them to
runtime functions: an array is one range of max(n, 1) two-word slots (a cell
header and the contents), element i at base + 16·i, so an element's location
is an ordinary cell; idx is an unsigned compare and the address computation;
the fill and the drain are tail-recursive loops that call the closure through
the table; drop frees the range once. Wasm is lowered from the flat IR
unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
Corpus programs, each run by the interpreter of every stage and compiled to
Wasm, all ending with live 0: array-sum (1000 elements built by array_new and
summed by a 1024-step loop, 500500), array-get (the family of leaf views from
withload: len, get in range, get out of range), array-replace (replace, read
back, out of range, and through a mut borrow by withswap), array-refs (an
array of owned cells dropped implicitly), array-empty (a negative length).
Must-rejects: a leaf view escaping its family's lifetime, mutating while a
view is held, an array used after array_free. The book gains the chapter
Arrays with doc-tested examples, and the reference includes the constants.
The introduction, the proved page, the overview, the READMEs and SPEC Status
say what is implemented.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
Grades: GDerives gains the eight array constants, translated to and from
CDerives and sound by their compat lemmas.

FrameTheorems: tab is nondeterministic like alloc, so the determinism of
StepsNA also excludes array redexes (ArrRedex). TwoRun: idx, drop and tab
compute locations, which a permutation of Loc does not preserve, so
renaming and fresh runs are stated for runs with no array step (StepsR,
Step1F). READMEs record the restriction.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxc29PmGiszpa6ndt21EuK
@empath-nirvana

Copy link
Copy Markdown
Owner Author

CI did not run: both jobs were refused before starting ("recent account payments have failed or your spending limit needs to be increased"). Local results at 3e4503c: lake build clean; check-hygiene 0 forbidden, 16522 declarations within the allowed axioms; parse/check/compile-corpus 0 failures, live 0; book-doctest 53/53. mdbook build not run locally.

@empath-nirvana
empath-nirvana merged commit cc32efb into main Sep 27, 2026
0 of 2 checks passed
@empath-nirvana
empath-nirvana deleted the feature/array branch September 27, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SPEC §9: implement buffers and strings

1 participant