Skip to content

feat(stdlib): Aggregate.affine — SQL group-by + aggregation primitives — db-theory #3 (7 externs)#527

Merged
hyperpolymath merged 1 commit into
feat/db-2-transactionsfrom
feat/db-3-aggregation
Jun 1, 2026
Merged

feat(stdlib): Aggregate.affine — SQL group-by + aggregation primitives — db-theory #3 (7 externs)#527
hyperpolymath merged 1 commit into
feat/db-2-transactionsfrom
feat/db-3-aggregation

Conversation

@hyperpolymath
Copy link
Copy Markdown
Owner

Summary

  • New stdlib/Aggregate.affine module with 7 externs: db_count, db_sum, db_min_int, db_max_int, db_avg, db_group_by (whole result-set as JSON), db_group_count (key/count buckets as JSON).
  • Codegen + Deno-ESM JS prelude wired; smoke + harness witness all 7 externs with both single-row aggregates and grouped buckets.
  • Cross-doc proof obligation at docs/academic/proofs/db-theory-3-aggregation-as-fold.md — each aggregator is a commutative monoid, group-by is the indexed sum (Green/Karvounarakis/Tannen).
  • Stacked on #526 (Transactions) — base will flip to main once feat(stdlib): Transaction.affine — affine-bounded write-set isolation — db-theory #2 (8 externs) #526 merges; auto-rebase will pick up cleanly.

Echo-types audit cross-doc

Per owner directive 2026-06-01, mandatory audit step. Found: no existing Monoid/Semiring infrastructure in echo-types. Closest scaffolding: EchoCost.CostAlgebra (left-identity only), OmegaPow.additive-principal. Filed hyperpolymath/echo-types#175 proposing new EchoAggregation.agda module with Monoid + GroupAggregator carriers + signature for aggregation-as-fold lemma. Sibling: echo-types#174 (Transaction safety).

Aggregator monoid table

Aggregator Elem ε Comm? Idemp?
COUNT 0 +
SUM ℕ (or ℤ, ℝ) 0 +
MIN ℕ ∪ {∞} min
MAX ℕ ∪ {-∞} -∞ max
AVG derived: SUM/COUNT (not a monoid — no identity)

Test plan

🤖 Generated with Claude Code

…s — db-theory #3 (7 externs)

New `Aggregate` module exposing SQL aggregation as typed scalars:
db_count / db_sum / db_min_int / db_max_int (Int monoids), db_avg
(Float, derived as SUM/COUNT), db_group_by (full grouped result-set
as JSON), and db_group_count (convenience over (key, count) buckets).

Codegen prelude adds `__as_db{Count,Sum,MinInt,MaxInt,Avg,GroupBy,
GroupCount}` helpers delegating to `globalThis.__as_sqlite.agg*` /
`groupBy` / `groupCount`. Deno-ESM smoke witnesses all 7 externs
across 5 fold-shaped assertions + 2 group-bucket shape assertions.

Cross-doc: docs/academic/proofs/db-theory-3-aggregation-as-fold.md
states the headline safety property #DB-3.1 (aggregation-as-fold):
each aggregator is a commutative monoid and group-by is the
indexed sum (Green/Karvounarakis/Tannen provenance-semiring framing).
Aggregator monoid table in cross-doc shows COUNT/SUM/MIN/MAX are
proper monoids; AVG derived as SUM/COUNT.

Echo-types upstream extension tracked at hyperpolymath/echo-types#175
— proposes new EchoAggregation.agda module with `Monoid` +
`GroupAggregator` carriers + signature for `aggregation-as-fold`
lemma. The proof reduces to existing closure properties (EchoCost as
existence witness; OmegaPow.additive-principal for ω^n monoid
closure). Sibling: echo-types#174 (Transaction safety).

Stacked on feat/db-2-transactions (#526) — auto-rebase after #526
merges into main.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hyperpolymath hyperpolymath merged commit f08fcc7 into feat/db-2-transactions Jun 1, 2026
@hyperpolymath hyperpolymath deleted the feat/db-3-aggregation branch June 1, 2026 13:54
hyperpolymath added a commit that referenced this pull request Jun 2, 2026
…s — db-theory #3 (7 externs) (#527)

## Summary

- New `stdlib/Aggregate.affine` module with 7 externs: `db_count`,
`db_sum`, `db_min_int`, `db_max_int`, `db_avg`, `db_group_by` (whole
result-set as JSON), `db_group_count` (key/count buckets as JSON).
- Codegen + Deno-ESM JS prelude wired; smoke + harness witness all 7
externs with both single-row aggregates and grouped buckets.
- Cross-doc proof obligation at
`docs/academic/proofs/db-theory-3-aggregation-as-fold.md` — each
aggregator is a commutative monoid, group-by is the indexed sum
(Green/Karvounarakis/Tannen).
- **Stacked on
[#526](#526
(Transactions) — base will flip to `main` once #526 merges; auto-rebase
will pick up cleanly.

## Echo-types audit cross-doc

Per owner directive 2026-06-01, mandatory audit step. Found: no existing
`Monoid`/`Semiring` infrastructure in echo-types. Closest scaffolding:
`EchoCost.CostAlgebra` (left-identity only),
`OmegaPow.additive-principal`. Filed
[hyperpolymath/echo-types#175](hyperpolymath/echo-types#175)
proposing new `EchoAggregation.agda` module with `Monoid` +
`GroupAggregator` carriers + signature for `aggregation-as-fold` lemma.
Sibling:
[echo-types#174](hyperpolymath/echo-types#174)
(Transaction safety).

## Aggregator monoid table

| Aggregator | Elem            | ε  | ⊕   | Comm? | Idemp? |
|------------|-----------------|----|----|-------|--------|
| COUNT      | ℕ               | 0  | +   | ✓     | ✗     |
| SUM        | ℕ (or ℤ, ℝ)     | 0  | +   | ✓     | ✗     |
| MIN        | ℕ ∪ {∞}         | ∞  | min | ✓     | ✓     |
| MAX        | ℕ ∪ {-∞}        | -∞ | max | ✓     | ✓     |
| AVG        | derived: SUM/COUNT (not a monoid — no identity) |

## Test plan

- [x] `dune runtest test/test_main.exe` — 368 tests green (one more than
baseline since AOT smoke picked up `Aggregate.affine`).
- [x] `tools/run_codegen_deno_tests.sh` — `aggregate_smoke.harness.mjs`
passes all 8 assertions (5 single-row + 3 grouped).
- [x] Stack discipline: based on `feat/db-2-transactions`; will rebase
onto main once #526 lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
hyperpolymath added a commit that referenced this pull request Jun 2, 2026
Appends today's three db-theory stdlib PRs to the [Unreleased] section.
Repo's changelog-reusable.yml automation isn't wired yet (manual append
per the file's own header pointer).

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
hyperpolymath added a commit that referenced this pull request Jun 2, 2026
## Summary

Refresh the authoritative status surfaces in this repo to reflect the
stdlib **db-theory triplet** that landed yesterday and today:

- PR #525 — `Sqlite.affine` schema introspection + bulk I/O + error
inspection (db-theory #1c, 6 externs)
- PR #526 — `Transaction.affine` (db-theory #2, affine-bounded write-set
isolation, 8 externs)
- PR #527 — `Aggregate.affine` (db-theory #3, SQL group-by + aggregation
primitives, 7 externs)
- PR #528 — CHANGELOG sync (already merged)

`CHANGELOG.md` is already up to date via #528, so this PR touches only
the four files that ledger that work as status, not as history.

## What changed

- `docs/CAPABILITY-MATRIX.adoc` — replace the stale "19/19 stdlib files"
sentence (the AOT gate now discovers files dynamically) and call out the
db-theory triplet as the current authoring frontier alongside Http /
Json / Dict-Map.
- `docs/TECH-DEBT.adoc` §C STDLIB — add a `STDLIB-05` row capturing the
full Sqlite arc (#522 / #524 / #525), Transaction (#526), and Aggregate
(#527), with pointers to the two academic proofs the PRs already shipped
(`docs/academic/proofs/db-theory-{2,3}-*.md`).
- `docs/stdlib-roadmap.adoc` — promote `Sqlite.affine` from `partial /
Coverage audit needed` to `usable`, and add `Aggregate.affine` +
`Transaction.affine` rows to the inventory snapshot.
- `.machine_readable/6a2/STATE.a2ml` — bump `last-updated` to
`2026-06-02` (this file mirrors, it does not lead — per its own
drift-flag).

## What was deliberately *not* touched

- `docs/ROADMAP.adoc` — language/compiler progress; the db-theory PRs
are stdlib-side and don't move the language roadmap.
- A `TEST-NEEDS` file — none exists at repo root; the three PRs each
shipped their own Deno-ESM smoke harness under `tests/codegen-deno/`
(`sqlite_introspect_bulk` / `transaction_smoke` / `aggregate_smoke`) and
the AOT gate auto-picked the new modules up.
- License / SPDX headers — per the estate's no-automated-licence-edits
rule.

## Test plan

- [x] `bash tools/check-doc-truthing.sh` passes locally — banner /
primacy / mirror invariants intact, over-claim ratchet unchanged (no new
baseline entry needed).
- [x] Commit GPG-signed; verified `gpg: Good signature`.

Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant