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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! Tables and the ways to refer to them: the trait [`Table`] that a table's
//! value type implements, the declaration [`TableDef`], the captured value type
//! [`TableType`], the registry-assigned number [`TableId`], and the typed table
//! keys [`TableName<T>`] and [`TableHandle<T>`].
//!
//! A table is typed data that one stage writes beside the frozen trees and other
//! stages read; its identity is its registered dotted name, never its Rust type,
//! and that name is also a [capability](crate::core::registry::CapabilityDef),
//! the capability of the table having been written ([`Table`]).
//!
//! # Names, value types and table numbers
//!
//! A feature declares a table ([`TableDef`]) under a dotted name that the
//! feature owns (`flm.labels`, `flm.lowered.html`), with one Rust value type.
//! When the registry is built, each declared table receives a [`TableId`], a
//! number that the store uses as the table's key.
//!
//! The name, not the Rust type, identifies a table, because one Rust type can
//! hold several tables: the type holding lowered trees is declared once per
//! backend, so the type alone cannot say which table is meant. The value type is
//! captured at declaration as a [`TableType`] (its Rust `TypeId` together with
//! its operations as plain function pointers), so that the store, its fold
//! ([`fold_outs`](crate::core::data::store::fold_outs)) and the inspection dumps
//! ([`crate::dump`]) can handle every table without naming its Rust type.
//!
//! When the registry is built, it compares the value type of every table
//! reference in a stage's declarations with the table's declared value type. At
//! every access through a typed table key,
//! [`ComputedView::get`](crate::core::data::store::ComputedView::get),
//! [`StageOut::table`](crate::core::data::store::StageOut::table) and
//! [`Run::table`](crate::core::run::Run::table) compare the key's value type
//! with the declared one and return
//! [`AccessError::TypeMismatch`](crate::core::data::store::AccessError::TypeMismatch)
//! on a mismatch, so the conversion back to the value type behind the key cannot
//! fail.
//!
//! # Typed table keys
//!
//! A reader or a writer refers to a table through a typed table key, which
//! carries the table's name and its value type, so that a read returns the
//! value already typed:
//!
//! - a [`TableName<T>`] constant, for a table with a name fixed at compile
//! time; the crate that defines the value type exports it beside the type
//! (`pub const LABELS: TableName<Labels> = TableName::new("flm.labels")`);
//! - a [`TableHandle<T>`], for a table declared under a name computed at run
//! time;
//! - the value type itself, when it implements [`SingletonTable`] because
//! exactly one table uses it: `T::NAME` is then the name that the store's
//! singleton methods use.
//!
//! The read and write methods of a stage
//! ([`ComputedView::get`](crate::core::data::store::ComputedView::get),
//! [`StageOut::table`](crate::core::data::store::StageOut::table),
//! [`StageCx::get`](crate::core::run::StageCx::get)), [`Run::table`](crate::core::run::Run::table)
//! and [`Externals::install`](crate::core::run::Externals::install) accept any
//! [`TableKey`]. Every typed table key has a
//! type-erased form, [`TableRef`] (the name plus the identity of the value
//! type), which stage declarations, plans and messages carry.
//!
//! # A table's name is a capability
//!
//! Every table's name is also a
//! [capability](crate::core::registry::CapabilityDef): the capability of the
//! table having been written. A stage that declares a write of the table therefore provides
//! that capability, and a stage that declares a read of it requires it; the
//! registry derives both. See [stages, capabilities, and
//! plans](crate::guide::concepts_overview#stages-capabilities-and-plans).
// This module uses the name newtypes of `crate::model::names` and techy's
// serialized-value type; it does not depend on the registry, on stages, on plan
// construction or on the runner.
use ;
use fmt;
use PhantomData;
use NodeTree;
use SerialValue;
use crateFlm;
use crate;
use crate;
use crateshort_type_name;
/// The context through which a table writes node addresses, tree identifiers
/// and trees when it serializes itself for inspection ([`Table::project`]).
///
/// A table that holds node addresses or tree identifiers calls these methods
/// rather than writing an address itself, so that the serialized form of every
/// table writes addresses the same way. The library's implementation is
/// [`TableProjection`](crate::dump::project::TableProjection), which resolves
/// each address against the trees it was built over.
///
/// A table that holds a [synthesized
/// value](crate::guide::concepts_overview#synthesized-values) writes that tree
/// in full through [`ProjectCx::tree`], never by its identifier: value catch-up
/// gives a synthesized value its identifier, numbered per stage call
/// ([`StageCx::resolve_value`](crate::core::run::StageCx::resolve_value)), so the
/// identifier means something only inside the stage call that brought the value
/// up to date, and a reader of the serialized table could not resolve it. See
/// [`TreeId`].
///
/// See also [`crate::dump::project`].
/// What a table piece holds: data keyed by node address, nothing, or a fact
/// about the document as a whole.
///
/// A table piece is the part of one table that one stage invocation produced;
/// the runner folds the pieces into the store ([`Table::absorb`]), and a
/// piece's class says whether it states a fact about the document as a whole.
/// A table's value type classifies its own table pieces with
/// [`Table::piece_class`].
///
/// The class matters only during [value
/// catch-up](crate::guide::concepts_overview#value-catch-up), which runs
/// per-tree stages (stages of
/// [`Scope::PerTree`](crate::core::stage::Scope::PerTree), run once for each
/// tree) again over a [synthesized
/// value](crate::guide::concepts_overview#synthesized-values) and folds their
/// table pieces into a catch-up store of its own. A table piece keyed by node
/// address, or an empty one, is accepted there. A table piece that states a
/// fact about the document as a whole (a label declared inside the synthesized
/// value, a numbered point) is refused with
/// [`StoreError::DocumentFactInValue`](crate::core::data::store::StoreError::DocumentFactInValue),
/// because a synthesized value is not part of the document and must not add to
/// the document's facts.
/// The trait of a table's value type.
///
/// A table is typed data that one stage writes beside the frozen trees and other
/// stages read; its identity is its registered dotted name, never its Rust type,
/// and that name is also a [capability](crate::core::registry::CapabilityDef).
/// See [frozen trees and computed
/// data](crate::guide::concepts_overview#frozen-trees-and-computed-data) for
/// why computed data is kept in tables, and the [module documentation](self) for
/// how names, value types and typed table keys relate.
///
/// A map keyed by node address and a map keyed by a symbolic name are the usual
/// shapes of a table, but any value type can be one: a document-wide count is a
/// table whose [`absorb`](Table::absorb) adds.
///
/// To add a table, implement this trait on its value type, declare the table
/// in a feature with
/// [`FeatureBuilder::table`](crate::core::registry::FeatureBuilder::table) (or
/// [`FeatureBuilder::external_table`](crate::core::registry::FeatureBuilder::external_table)
/// for a table no stage writes), declare it as a write of the stage that
/// produces it ([`StageDecl::writes`](crate::core::stage::StageDecl::writes)) and
/// as a read of each stage that reads it
/// ([`Needs::reads`](crate::core::stage::Needs::reads)), and export a
/// [`TableName`] constant for its readers. A stage
/// writes its part of the table through
/// [`StageOut::table`](crate::core::data::store::StageOut::table), and a later
/// stage reads the whole table through
/// [`StageCx::get`](crate::core::run::StageCx::get).
///
/// The value type must implement `Default`: a stage's table piece starts as the
/// default value, and a stage that declared a write but produced nothing still
/// installs a default (empty) table.
///
/// See also [`SingletonTable`] for a value type used by exactly one table, and
/// [`TableType`] for the type-erased form of a value type.
/// A table value type that exactly one table uses: [`NAME`](SingletonTable::NAME)
/// is that table's name.
///
/// A value type that implements this trait lets a reader or a writer refer to
/// its table by the Rust type alone: [`StageCx::get_singleton`],
/// [`ComputedView::get_singleton`] and [`StageOut::table_singleton`] find the
/// name through it.
///
/// A value type declared under several names, such as the type holding one
/// backend's lowered trees, must not implement this trait, since it has no
/// single name. For that reason `NAME` is defined on this separate trait rather
/// than on [`Table`]: on [`Table`], it would force such a value type to invent a
/// name.
///
/// [`StageCx::get_singleton`]: crate::core::run::StageCx::get_singleton
/// [`ComputedView::get_singleton`]: crate::core::data::store::ComputedView::get_singleton
/// [`StageOut::table_singleton`]: crate::core::data::store::StageOut::table_singleton
type ErasedTable = ;
// The three helpers below downcast a type-erased table back to `T`. The
// downcast is unreachable-by-construction — the store keys by `TableId` and the
// registry verified the value type at declaration — so they `expect` with a
// message naming the invariant rather than returning a result nobody could
// act on.
/// A table value type's identity and operations, captured when the table is
/// declared, so that the store, its fold
/// ([`fold_outs`](crate::core::data::store::fold_outs)) and the inspection dumps
/// ([`crate::dump`]) can handle every table without naming its Rust type.
///
/// A `TableType` holds the value type's `TypeId` (checked so that converting a
/// type-erased table back to its value type cannot fail), its Rust name (for
/// messages), a constructor of an empty value (so that a declared write with no
/// table piece still installs a table), and the three operations of [`Table`]:
/// [`absorb`](Table::absorb), [`project`](Table::project) and
/// [`piece_class`](Table::piece_class).
///
/// Create one with [`TableType::of`]. A [`TableDef`] carries one, and so do the
/// resolved reads and writes of a stage
/// ([`ReadEntry`](crate::core::data::store::ReadEntry),
/// [`WriteEntry`](crate::core::data::store::WriteEntry)).
///
/// Two `TableType`s are equal exactly when they capture the same Rust type.
/// A table as a feature declares it: its name, which is also its capability;
/// its value type; its description; and the feature that owns it.
///
/// [`FeatureBuilder::table`](crate::core::registry::FeatureBuilder::table) and
/// [`FeatureBuilder::external_table`](crate::core::registry::FeatureBuilder::external_table)
/// create it; the built registry holds it in the table's
/// [`TableEntry`](crate::core::registry::TableEntry).
///
/// It is written to data with `Serialize`; see [serialized
/// forms](crate::core::registry#serialized-forms).
/// The number of a declared table within one registry; the store keys its
/// tables by it.
///
/// The registry assigns the numbers when it is built, consecutively and in its
/// own deterministic order. A `TableId` identifies a table only within the
/// registry that assigned it: the same number in another registry may refer to
/// another table. Its `Display` form is `table#N`.
///
/// It is written to data with `Serialize`; see [serialized
/// forms](crate::core::registry#serialized-forms).
;
/// The type-erased form of a typed table key: the table's name together with
/// the identity of the value type the key expects.
///
/// Stage declarations, plans and messages carry a `TableRef`, because none of
/// them is generic over the value type. The registry checks every `TableRef`
/// of a stage declaration against the [`TableDef`] of the same name when it is
/// built; a typed table key used at an access site is checked again there.
/// Its `Display` form is the name followed by the value type's short name in
/// parentheses.
///
/// It is written to data with `Serialize`; see [serialized
/// forms](crate::core::registry#serialized-forms).
/// A typed table key: a value that refers to a table together with its value
/// type, namely a [`TableName`], a [`TableHandle`], or a reference to either.
///
/// The read and write methods of [`ComputedView`], [`StageOut`], [`StageCx`]
/// and [`Run`], and [`Externals::install`], are generic over this trait, so a
/// caller passes whichever key it has.
///
/// [`ComputedView`]: crate::core::data::store::ComputedView
/// [`StageOut`]: crate::core::data::store::StageOut
/// [`StageCx`]: crate::core::run::StageCx
/// [`Run`]: crate::core::run::Run
/// [`Externals::install`]: crate::core::run::Externals::install
/// A typed table key for a table whose name is fixed at compile time.
///
/// The crate that defines a table's value type exports one constant of this
/// type beside the value type for each table it declares under a fixed name:
///
/// ```
/// use flm_core::core::data::table::{Table, TableName};
///
/// #[derive(Default)]
/// struct Labels;
/// impl Table for Labels {
/// fn absorb(&mut self, _piece: Labels) {}
/// }
///
/// pub const LABELS: TableName<Labels> = TableName::new("example.labels");
/// assert_eq!(LABELS.name(), "example.labels");
/// ```
///
/// Use a [`TableHandle`] instead when the name is known only at run time;
/// [`TableName::handle`] converts a `TableName` into one.
/// A typed table key for a table whose name is known only at run time.
///
/// Use it for a table declared under a computed name, such as the table of
/// lowered trees of each backend, `flm.lowered.<backend>`, or under a name read
/// from configuration. Where the name is fixed at compile time, use a
/// [`TableName`] instead.
///
/// [`FeatureBuilder::table`](crate::core::registry::FeatureBuilder::table)
/// returns a handle for the table it declares. The registry checks the name and
/// the value type when it is built, so a handle returned by a declaration always
/// reads the table as its own value type. A handle created by hand, pairing a
/// name with a value type that the registry does not pair it with, is refused
/// at the access site with
/// [`AccessError::TypeMismatch`](crate::core::data::store::AccessError::TypeMismatch);
/// it does not cause a panic.