polydat 0.6.2

Polydat — a variates construction engine
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
---
type: specification
title: Cursor Partitions
timestamp: 2026-09-27
description: Partition values, the partition-spec language, resolution and rounding, ordering, metadata wires, cursor narrowing with over, and the source factories that hand a cursor's ordinals to fibers.
tags: [iteration]
---

# Cursor Partitions

This document specifies Polydat cursor-partition values, the partition-spec
language, how a spec is resolved to ordinal ranges and rounded, partition
ordering, narrowing a cursor to one partition with `over`, the partition
metadata wires, and the source factories that hand a cursor's ordinals to
fibers and rewind them between rounds.

**Related specifications:** [Comprehension Forms](comprehension_forms.md),
[Polydat Grammar](polydat_grammar.md#sec-fields-cursors),
[Runtime Model](runtime_model.md), and the node reference
([nodes.md](../reference/nodes.md)); §11 states what each covers.

## 1. Purpose

A cursor describes an ordinal domain such as `[0, 1_000_000)`. A partition
describes a fixed sub-domain of that cursor that is assigned to one scope or
fiber. Partitioning is separate from traversal:

```text
base cursor domain       [0 ................................ 1_000_000)
partition resolution      [------ p0 ------)[-- p1 --)[----- p2 -----)
comprehension order                  chooses which p is visited next
cursor `over p`                      narrows iteration to p's interval
```

Because of this separation, the same domain can be split by percentages,
absolute sizes, recipes, windows, or deterministic ordering without special
operators in the comprehension algebra. A comprehension reads a
`PartitionList` as an ordinary typed list source, and the cursor materializer
applies one selected `Partition` to a cursor.

## 2. Data model

### 2.1 `PartitionSpec`

`PartitionSpec` is the parsed but unresolved request. It contains:

- one `Chunking` shape;
- an optional `in start..end` window; and
- one `PartitionOrder`.

A spec has no concrete ordinals until it is resolved against a base half-open
range `[base_start, base_end)`.

### 2.2 `Partition`

A resolved `Partition` contains:

| Field | Meaning |
| --- | --- |
| `idx` | Zero-based generation position. Reordering never changes it. |
| `count` | Number of siblings produced by the same resolution. |
| `start_ord` | Inclusive absolute start ordinal. |
| `end_ord` | Exclusive absolute end ordinal. |
| `start_pct` | Start position in the full base frame. |
| `end_pct` | End position in the full base frame. |
| `base_extent` | Extent of the full base frame used for percentage labels. |

Its cardinality is exactly `end_ord - start_ord`.

### 2.3 `PartitionList`

`PartitionList` is the ordered result of resolving one spec. `Partition`,
`PartitionSpec`, and `PartitionList` are passed along Polydat wires as typed
`Value::Ext` reflected values. A resolved partition does not change within one
scope activation (CP8).

## 3. Spec language

The complete outer grammar is:

```text
partition-spec := chunking [ "in" window ] [ order ]
window         := sized-bound ".." sized-bound
order          := unchanged | smallest_first | largest_first | random
```

Whitespace outside tokens is ignored. Brackets and parentheses around ranges
are accepted as advisory notation, but every resolved interval is half-open:
`[start, end)`.

### 3.1 Sized bounds

Three spellings identify the unit without a separate suffix declaration:

| Spelling | Interpretation | Example |
| --- | --- | --- |
| Bare integer | Ordinal count or position | `1000` |
| Decimal in `[0.0, 1.0]` | Fraction of the active extent | `0.25` |
| Number followed by `%` | Percentage of the active extent | `25%` |

Percentage and fraction forms are equivalent at resolution. Ordinal bounds are
relative to the active domain's start and are clamped to its end.

### 3.2 Form 1: one range

A range creates exactly one partition:

```text
0..53%
[25%..75%)
100..1000
0.05..0.5
```

Both endpoints must be sized bounds. Tail tokens and gaps are invalid inside a
range. A range that is reversed or rounds to zero ordinals is an error rather
than a silent empty workload.

### 3.3 Form 2: a delta list

A comma-separated list walks left to right from the active domain's start. Each
sized entry advances by that amount and emits one partition:

```text
20%,30%,*
1000,5000,*
0.02,0.10,*
```

Against `[0, 100)`, `20%,30%,*` resolves to `[0,20)`, `[20,50)`, and
`[50,100)`.

The following list-only forms control the remainder or modify entries:

| Form | Meaning |
| --- | --- |
| `*` or `*%` | Emit the remaining extent as one partition. |
| `...` | Repeat the preceding sized delta to the end; truncate, but do not drop, the last chunk. |
| `*/N` | Split the remainder into `N` non-empty, near-equal partitions. |
| `*/recipe:args` | Split the remainder according to normalized recipe weights. |
| `<delta>xN` | Expand one sized delta into `N` consecutive copies. |
| `~<delta>` | Consume a sized gap without emitting a partition. |

At most one remainder-consuming tail is allowed. `...`, `*/N`, and
`*/recipe:args` must be last. `...` requires an immediately preceding sized,
non-gap delta. A list containing only gaps is an error.

If a list has no tail and sums to less than the domain, the trailing gap is
intentionally unassigned. If its sized entries exceed the domain, resolution is
an error.

### 3.4 Form 3: recipes

A recipe expands to normalized percentage deltas and otherwise follows the
same resolution rules as a delta list:

```text
linear:5
ratios:1,3,6
fib:7
geom:8,1.5
zipf:1.2,10
front_heavy:6
```

The recipe families are:

- `linear:N` and `ratios:a,b,c,...`;
- `mul:R` and `mul:S,R`;
- `bin:N`, `fib:N`, and `ln:N`;
- `geom:N,R`, `zipf:s,N`, and `pareto:alpha,N`; and
- `front_heavy:N` and `back_heavy:N`.

Recipes may also shape only the unallocated remainder, for example
`90%,*/fib:5`. Equal-count remainder splitting uses the canonical `*/N` form;
`*/linear:N` is rejected with that spelling as guidance.

## 4. Windows

An `in` clause narrows the domain before chunking:

```text
linear:5 in 25%..75%
10%,20%,* in 100..900
```

Window endpoints resolve against the full base range. Chunking then resolves
against the window, so percentages inside the chunking are window-relative.
Resolved partition ordinals remain absolute.

Percentage metadata uses the full base frame, not a window-local frame. For
example, a partition beginning at the start of `25%..75%` reports
`start_pct = 25`, not zero. This is required when a partition is later applied
to a different cursor extent through `over`.

## 5. Ordering and identity

Ordering is applied only after all partitions have been resolved:

| Order | Behavior |
| --- | --- |
| `unchanged` | Generation order; the default. |
| `smallest_first` | Stable ascending sort by cardinality. |
| `largest_first` | Stable descending sort by cardinality. |
| `random` | Deterministic permutation seeded from the complete spec. |

The ambiguous words `ascending` and `descending` are rejected because they do
not say whether position or size is the sort key. Use `unchanged`,
`smallest_first`, or `largest_first`.

Reordering changes visitation order only. `idx` always identifies the original
generation position, and `count` remains the total sibling count.

## 6. Resolution and rounding

Resolution is a pure operation:

```text
resolve(spec, base_start, base_end) -> ordered PartitionList
```

The resolver follows these steps:

1. Validate the base half-open range.
2. Resolve the optional window against the full base.
3. Resolve chunking against that active domain.
4. Assign generation indices and the sibling count.
5. Apply the requested visitation order without changing indices.

Fractional boundaries use exact cumulative positions and round each boundary
once. Partition sizes are differences between adjacent rounded boundaries. This
prevents rounding error from accumulating across a list: an equal three-way
split of 1,000 ordinals covers all 1,000 rather than producing three 333-item
chunks and silently losing one ordinal.

`*/N` and `subdivide(partition, N)` share the same boundary calculation.
Their non-empty chunk sizes differ by at most one ordinal.

## 7. DSL and comprehension integration

### 7.1 Producing partition lists

The standard `partitions(spec[, extent])` node parses a string and resolves it
against `[0, extent)`; the default extent is 100. Its result is a
`PartitionList` and can be used as a normal comprehension source:

```text
p in partitions("20%,30%,*", 1000000)
```

An existing partition can be divided again with `subdivide(p, n)`, which is
also a valid comprehension source.

### 7.2 Narrowing a cursor

The cursor declaration grammar is:

```text
cursor <name> = <constructor> [over <partition-expression>]
```

For example:

```polydat
cursor q = range(0, 1000000) over p
partition_index := q.cursor.idx
partition_start := q.cursor.start_ordinal
```

Narrowing a cursor to one partition writes the partition into the cursor's
`Ext` slot and its scalar projections into six more, all of them externs of
the program. The partition remains fixed for that scope activation (CP8).

The compiler resolves a literal `over` clause against a known extent at build
and records the partitions on the cursor's schema
(`SourceSchema::partitions`). Every kernel, on all four engines (the
interpreter, the closure tier, native, and pure native), reports its
cursors and their resolved partitions through `Kernel::cursor_schemas`.

How a cursor is narrowed depends on what its `over` clause denotes:

- **Exactly one partition.** The partition is written into the cursor's
  slots at build, so the program runs with no host call.
- **Several partitions.** The host chooses one, or, for a cursor in a
  `for` body, the traversal runtime does. It calls
  `Kernel::set_cursor(name, &partition)`, where `name` is the cursor's
  name and `partition` the chosen `Partition`; the call writes the seven
  slots on any kernel and returns an error if the kernel rejects the
  write.
- **A computed `over` value, or an extent known only at run time.** The
  caller resolves it with `cursor_over_partitions_on(kernel, schema)`,
  passing the kernel and the cursor's `SourceSchema`. It pulls the
  clause's value and the extent from the kernel, which may be on any of
  the four engines, resolves them as §6 does, and returns the resulting
  partitions, one of which the caller passes to `set_cursor`.

Available cursor metadata wires are:

```text
<cursor>.cursor
<cursor>.cursor.idx
<cursor>.cursor.partition_count
<cursor>.cursor.start_pct
<cursor>.cursor.end_pct
<cursor>.cursor.start_ordinal
<cursor>.cursor.end_ordinal
```

Chained field syntax is flattened to internal wire names by the normal Polydat
field-access rules; it is not a separate runtime object lookup.

### 7.3 Partition value functions

Typed library nodes expose partition values without relying on field syntax:
`cardinality`, `start_of`, `end_of`, `idx_of`, `count_of`, `at`, `mod_in`,
`clamp_in`, `random_in`, and `subdivide`. Their signatures and error behavior
are in the node reference ([nodes.md](../reference/nodes.md), the
`partition` section).

## 8. Source factories

This section owns the source-factory contract. It belongs with cursor
partitions because a factory serves a cursor's ordinal domain, including the
domain an `over` clause narrows (§7.2), to the fibers that run a scope. The
replay contract each source reports is specified in
[SIMD ISA Selection](simd_isa_autopromotion.md) §4.1 and is not repeated here.

A **source factory** (`DataSourceFactory`,
`polydat-core/src/iteration/source.rs`) holds the state every reader of one
cursor shares: the cursor position and the end of the domain. `create_reader()`
returns a per-fiber **reader** (`DataSource`). A reader's `reserve(stride)`
claims the next `stride` ordinals as a half-open range, claims fewer at the end
of the domain, and returns `None` when the domain is exhausted.

A **round** is one pass over the factory's domain, from the factory's
construction or a rewind until reservations return `None`. Within a round the
readers of one factory never claim the same ordinal. `rewind_for_poll()`
starts a new round:

- It returns `false` when the factory cannot rewind and changes nothing. The
  trait's default returns `false`. A host that re-runs a source between poll
  rounds rejects a factory that returns `false` rather than completing
  silently after the first round.
- When it returns `true`, every reservation linearized after the call returns
  belongs to the new round, and the new round hands out each of its ordinals
  at most once. The rule is safe under any concurrency: readers may reserve
  while the rewind runs, and a reservation that overlaps the rewind belongs to
  either the old round or the new one.

```text
range(0, 4), stride 3:  reserve -> [0, 3)   reserve -> [3, 4)   reserve -> None
rewind_for_poll() -> true
                        reserve -> [0, 3)   reserve -> [3, 4)   reserve -> None
```

The built-in factories both rewind:

| Factory | Rewind |
|---|---|
| `RangeSourceFactory`, for `range(a, b)` | The cursor returns to `a`, and the round serves `[a, b)` again. The cursor is the factory's only shared state and every reservation is one atomic add on it, so the rewind is a single atomic store in the same modification order as the reservations. |
| `ExtendingRangeSourceFactory`, for the `until_*` constructors | The cursor returns to the start, and the end returns to the start plus the base chunk, capped by the partition bound `max_end` when `over` narrows the cursor. The extension policy grows the end again as the round reaches it. The rewind and every decision to extend or end the round run under the factory's round lock, and the rewind writes the end before the cursor, so no reader acts on a partly rewound pair. A reservation below the end takes no lock. |

An extension policy reads the elapsed time from the start of the current
round: the factory's construction for the first round, and the latest
`rewind_for_poll()` for every round after it. The rewind resets this baseline
under the round lock, together with the cursor and the end, and every reader
of the factory measures from the same baseline. A time-based policy therefore
grows each round as it grew the first: `until_elapsed(64, 40)` extends past
its base chunk of 64 ordinals for 40 ms after the factory is built, and again
for 40 ms after each rewind.

## 9. Error policy

The parser and resolver fail with an explanatory diagnostic for at least:

- an empty spec, unknown recipe, or unknown order;
- a missing or malformed `in` window;
- tail tokens in a range or more than one tail in a list;
- a misplaced fill, split, shaped remainder, repetition, or gap;
- a reversed or zero-width explicit range;
- sized deltas that exceed the active extent;
- a remainder split with no remainder or more chunks than ordinals; and
- shaped weights that would create an empty partition.

Errors are not converted into empty partition lists. A spec that would silently
schedule no useful work is rejected at its earliest authoritative boundary.

## 10. Normative invariants

**CP1 — Half-open ranges.** Every resolved partition denotes
`[start_ord, end_ord)`.

**CP2 — Exact ownership.** A partition's cardinality is
`end_ord - start_ord`; emitted siblings do not overlap. Gaps and an intentionally
unassigned trailing suffix are not contained in any emitted partition.

**CP3 — Cumulative rounding.** Fractional delta boundaries are rounded from the
cumulative exact position, never by summing independently rounded sizes.

**CP4 — No silent oversubscription.** Sized entries may not consume more than
the active domain.

**CP5 — Stable generation identity.** Ordering never rewrites `idx` or `count`.

**CP6 — Stable random order.** The same complete spec produces the same random
permutation.

**CP7 — Full-frame labels.** Windows affect sizing and placement; percentage
metadata continues to describe the full base range.

**CP8 — Scope stability.** A resolved cursor partition is immutable within one
scope activation.

**CP9 — Algebra separation.** Partition lists enter comprehensions through the
ordinary list-source contract. Cursor partitioning does not add a new
comprehension operator.

**CP10 — Round exclusivity.** A source factory hands out each ordinal of a
round at most once, and a reservation linearized after a rewind returns
belongs to the new round, under any concurrency (§8).

## 11. Related specifications

- [Comprehension Forms](comprehension_forms.md) specifies list-source
  composition, traversal, filtering, ordering, and consumption surfaces.
- [Polydat Grammar](polydat_grammar.md#sec-fields-cursors) specifies general
  cursor declaration and field-access syntax.
- The node reference ([nodes.md](../reference/nodes.md)) lists the typed
  nodes that consume `Partition` values.
- [Runtime Model](runtime_model.md) specifies scope activation, state
  ownership, caching, and invalidation semantics.