-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregate.eph
More file actions
304 lines (273 loc) · 9.62 KB
/
Copy pathAggregate.eph
File metadata and controls
304 lines (273 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//
// Ephapax stdlib — Monoidal aggregates (D18 from the DB-theory inventory).
//
// Citations:
// - Gray, Bosworth, Layman, Pirahesh. "Data Cube: A Relational Aggregation
// Operator." ICDE 1996. — establishes aggregation as the central
// dimensional operation in OLAP.
// - Cohen, S. "User-Defined Aggregates in Database Languages." DBPL 1999.
// — generalises SQL aggregates to user-defined monoid operations.
// - Bird, R. "Algebra of Programming." 1996. — the monoidal fold as the
// canonical right-fold-with-identity.
//
// What this module provides
// -------------------------
//
// A `Monoid<A>` is a pair `(empty, combine)` where `combine` is associative
// and `empty` is a left+right identity. This is the type-theoretic essence
// of an aggregate function:
//
// sum, product, max, min, count, set-union, list-concatenation, AND, OR,
// string-concatenation — all are monoids.
//
// Once your data carrier carries an `aggregate_with` ability, EVERY
// well-typed fold over a collection is provably terminating + produces a
// result whose meaning is uniquely determined by the monoid laws below.
//
// The three monoid laws
// ---------------------
//
// (L1) Left identity: combine(empty(), x) == x
// (L2) Right identity: combine(x, empty()) == x
// (L3) Associativity: combine(a, combine(b, c))
// == combine(combine(a, b), c)
//
// These laws are SHIPPED AS DOCUMENTATION here; the future
// `formal/Aggregate.v` will mechanise them per-instance. Until then,
// instances asserted via `aggregate_with` rely on programmer discipline.
//
// Why ephapax-shaped
// ------------------
//
// Aggregates are AFFINE: the result can be discarded freely; intermediate
// "running totals" need not be threaded linearly. (Compare: `Transaction`
// from `stdlib/Transactions.eph` is LINEAR because of its commit-or-abort
// obligation.) `Monoid<A>` carries no resource obligation at L1; its values
// drop freely under L2's Affine discipline.
//
// Future work (separate PRs)
// --------------------------
//
// - D19 — Windowed aggregation: takes a `Monoid<A>` + a window-size and
// produces a streaming reducer. Depends on this module + D17's
// `Stream` module.
// - `formal/Aggregate.v` — Coq mechanisation of the three monoid laws,
// case-analysed per stock instance below. See the durable owner
// directive 2026-06-01: any L3-flavoured Coq side here must
// check `hyperpolymath/echo-types` for prior art first (the
// `EchoCategorical` + `EchoCanonicalIdentitySuite` Agda modules
// speak the categorical-monoid language we'd be mirroring).
// - Bag / SetAggregate: idempotent monoids (associativity + commutativity
// + idempotence) for set-valued aggregation. Belongs in
// `stdlib/SetAggregate.eph` once `stdlib/Set.eph` exists.
module Aggregate
// =============================================================
// Sum (the canonical i64 monoid)
// =============================================================
//
// empty = 0
// combine = +
//
// Laws (informal proofs):
// (L1) 0 + x = x — definition of integer addition
// (L2) x + 0 = x — definition of integer addition
// (L3) a + (b + c) = (a + b) + c — associativity of integer addition
fn sum_empty() -> i64 {
0
}
fn sum_combine(a: i64, b: i64) -> i64 {
a + b
}
// Fold a list of i64s as a sum. Terminates structurally on the list length.
fn sum_fold(xs: &List<i64>) -> i64 {
list_foldl_i64(xs, sum_empty(), sum_combine)
}
// =============================================================
// Product (the multiplicative i64 monoid)
// =============================================================
//
// empty = 1
// combine = *
//
// Laws:
// (L1) 1 * x = x
// (L2) x * 1 = x
// (L3) a * (b * c) = (a * b) * c
fn product_empty() -> i64 {
1
}
fn product_combine(a: i64, b: i64) -> i64 {
a * b
}
fn product_fold(xs: &List<i64>) -> i64 {
list_foldl_i64(xs, product_empty(), product_combine)
}
// =============================================================
// Max (over i64)
// =============================================================
//
// empty = INT_MIN (i64::MIN, used as the bottom element)
// combine = max
//
// Laws (informal):
// (L1) max(INT_MIN, x) = x — INT_MIN is the bottom of the order
// (L2) max(x, INT_MIN) = x
// (L3) max(a, max(b, c)) = max(max(a, b), c) — total order
fn max_empty() -> i64 {
// i64 minimum — used as the identity of `max`.
-9223372036854775808
}
fn max_combine(a: i64, b: i64) -> i64 {
if a >= b then a else b
}
fn max_fold(xs: &List<i64>) -> i64 {
list_foldl_i64(xs, max_empty(), max_combine)
}
// =============================================================
// Min (over i64)
// =============================================================
//
// empty = INT_MAX
// combine = min
//
// Laws: dual of Max — same shape.
fn min_empty() -> i64 {
9223372036854775807
}
fn min_combine(a: i64, b: i64) -> i64 {
if a <= b then a else b
}
fn min_fold(xs: &List<i64>) -> i64 {
list_foldl_i64(xs, min_empty(), min_combine)
}
// =============================================================
// Count (cardinality monoid; counts via the unit input)
// =============================================================
//
// Count is the i64-valued monoid where each element contributes 1.
// It's a "tag-and-sum" over a `unit -> i64` mapping:
//
// empty() = 0
// contribute() = 1
// combine(a, b) = a + b // re-uses the Sum monoid
//
// Counting a list of A's: tag-each-with-1, then sum. The fold below
// is parameterised over A to avoid forcing a specific element type.
fn count_empty() -> i64 {
0
}
fn count_contribute() -> i64 {
1
}
fn count_combine(a: i64, b: i64) -> i64 {
sum_combine(a, b)
}
// =============================================================
// And / Or (the boolean lattice monoids)
// =============================================================
//
// And: empty = true, combine = &&
// Or: empty = false, combine = ||
//
// Both are commutative + idempotent (so they're semilattice monoids,
// satisfying laws L1-L3 plus a, combine(a, a) = a).
fn and_empty() -> bool {
true
}
fn and_combine(a: bool, b: bool) -> bool {
a && b
}
fn or_empty() -> bool {
false
}
fn or_combine(a: bool, b: bool) -> bool {
a || b
}
// =============================================================
// String concatenation
// =============================================================
//
// Strings under concatenation form a (non-commutative) monoid:
//
// empty = ""
// combine = string_concat
//
// Laws:
// (L1) "" ++ s = s
// (L2) s ++ "" = s
// (L3) a ++ (b ++ c) = (a ++ b) ++ c
//
// NOTE: string_concat is at AFFINE-arg consumption per
// `stdlib/Prelude.eph::string_concat`; the combine here mirrors that
// affine signature.
fn string_empty() -> String {
""
}
fn string_combine(a: String, b: String) -> String {
string_concat(a, b)
}
// =============================================================
// Generic fold over List<i64>
// =============================================================
//
// The recursion engine for the i64 monoids above. Parameterised over
// the (empty, combine) pair so that callers can express custom i64
// monoids without rewriting the structural recursion.
//
// Termination: structural on `xs` (list length strictly decreases on
// each recursive call). `@total` annotation would let the compiler
// emit a fuel-free machine-checked totality witness; see the v2
// grammar §4 for annotation syntax. Omitted here pending @total
// implementation in the typechecker.
fn list_foldl_i64(
xs: &List<i64>,
acc: i64,
op: fn(i64, i64) -> i64
) -> i64 {
// Implementation defers to the Prelude's list recursor.
__intrinsic_list_foldl_i64(xs, acc, op)
}
// =============================================================
// Pattern guide
// =============================================================
//
// Example 1 — sum a list of payments:
//
// let payments: List<i64> = ...;
// let total = sum_fold(&payments);
//
// Example 2 — count distinct items via the Count monoid:
//
// let items: List<Item> = ...;
// let n = list_foldl_i64(
// &items.map(|_| count_contribute()),
// count_empty(),
// count_combine);
//
// Example 3 — running max across a stream:
//
// let observations: List<i64> = ...;
// let peak = max_fold(&observations);
//
// Example 4 — concatenate string parts:
//
// let parts: List<String> = ...;
// // (sequential fold; not via list_foldl_i64 since strings ≠ i64)
// let report = parts.iter().fold(string_empty(), string_combine);
//
// =============================================================
// Echo-types audit (per durable owner directive 2026-06-01)
// =============================================================
//
// This `Aggregate.eph` is a **stdlib slice** — surface-language API
// only, no proof obligation attached at landing time. Echo-types audit
// for the stdlib slice itself: **not relevant** — pure type signatures.
//
// However, the FUTURE `formal/Aggregate.v` Coq mechanisation of the
// monoid laws IS echo-types territory: `EchoCategorical.agda`,
// `EchoCanonicalIdentitySuite.agda`, and `EchoMonoidal.agda` (if it
// exists in the upstream) speak the categorical-monoid language we
// would be mirroring. The mechanisation PR must start with an
// echo-types upstream audit per `feedback_proofs_must_check_and_cross_doc_echo_types.md`.