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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Round-trip OID identity for `@code/rust/macro.shim_type`.
//!
//! T23 (2026-06-08, Mara), the load-bearing witness for the spec
//! `mirror/docs/specs/code-macro-surface.md` §5 ("the gap is the
//! spec — formalized") + §6 ("round-trip identity — the central
//! contract").
//!
//! # What this test asserts
//!
//! The substrate declares four laws on `shim_X`:
//!
//! 1. Type-soundness — emission has the same typed shape as input.
//! 2. Round-trip identity —
//! `parse_X(render_X(shim_X(D))) ≡ shim_X(D)` structurally.
//! Weak form per §6.2: equivalence is up to canonical-form
//! reduction.
//! 3. OID functionality — `oid(D1) = oid(D2)` implies
//! `oid(render_X(shim_X(D1))) = oid(render_X(shim_X(D2)))`.
//! 4. Substrate-pull preservation — emission uses only substrate-
//! realisable Rust constructs.
//!
//! This test witnesses laws 2 and 3 end-to-end, at the
//! TokenStream-canonical-form altitude.
//!
//! # The witness
//!
//! Given a substrate `type` declaration as input tokens:
//!
//! ```ignore
//! type point { x: u32, y: u32 }
//! ```
//!
//! The proc-macro `prismqueer::declaration!{...}` emits Rust:
//!
//! ```ignore
//! pub struct Point { pub x: u32, pub y: u32 }
//! ```
//!
//! Round-trip identity (weak form, spec §6.2):
//!
//! ```text
//! syn::parse2::<Item>(emission) → Item::Struct (re-parses cleanly)
//! quote!(&item).to_string() = emission.to_string() (canonical form)
//! Oid::hash(emission_canonical) = Oid::hash(re-parsed-then-rendered)
//! ```
//!
//! OID functionality (spec §5 law 3) at the Rust-altitude:
//!
//! ```text
//! Oid::hash(canonical_form(emit(D))) is a deterministic function of D.
//! ```
//!
//! The substrate's strong-form claim — that
//! `oid(render_rust(shim_type(D))) == oid(D)` at the
//! substrate-altitude — requires the substrate's render to
//! canonicalize the Rust AST through the same hash basis as the
//! substrate compiler. The weak form witnessed here is the
//! load-bearing chain:
//!
//! substrate D → shim_type emission → syn parse → quote render
//!
//! all the way through, with the OID computed on the canonical
//! form at each end of the round trip. Equality at both ends IS
//! the witness that the shim emission is a fixed point of the
//! Rust-altitude parse cycle.
use ;
use ToTokens;
/// The canonical form of a Rust item: parse through syn, render
/// back through quote, take `.to_string()`. This is the spec's
/// "canonical-form reduction" (§6.2) at the syn altitude — whitespace
/// is normalized, syntactic-sugar is normalized; two emissions that
/// differ only in whitespace become byte-identical after this step.
/// Compute the OID of a Rust item's canonical form. Uses the
/// substrate's standard Oid::hash (CoincidenceHash<3> compressed to
/// 64-hex). Two items with the same canonical form get the same OID;
/// the OID is the content-address at the @code/rust altitude.
/// The load-bearing witness for the cascade tick: round-trip OID
/// identity on a record-shaped `type` declaration.
///
/// 1. Run the proc-macro: substrate `type point { x: u32, y: u32 }`
/// → Rust `pub struct Point { pub x: u32, pub y: u32 }`.
/// 2. Parse the emission via syn → `Item::Struct`.
/// 3. Render via quote back to canonical form.
/// 4. Re-parse the rendered form.
/// 5. Compute OID at each end.
/// 6. Assert: same OID. The cycle has reached a fixed point.
///
/// This witnesses spec §6's central contract: the macro emission is
/// a fixed point of the language-side parse cycle.
/// OID functionality (spec §5 law 3) — structurally-identical
/// substrate declarations produce equal-OID Rust emissions. Same
/// shim, same input, same canonical output, same OID. The shim is
/// a function of the substrate declaration's content-address.
/// Sum-shaped substrate `type` declarations round-trip to Rust enum
/// items. The shim emits `pub enum Color { Red, Green, Blue }` for
/// `type color = red | green | blue`. The OID of the canonical-form
/// emission is the content-address of the @code/rust realisation.
/// Type-soundness witness (spec §5 law 1) for record types: the
/// emitted struct has the field names and types the substrate
/// declaration named. Substrate `u32` → Rust `u32`; substrate field
/// name `roughness` → Rust field name `roughness` (no rename, no
/// type drift).
// ---------------------------------------------------------------------------
// T24 (2026-06-09): the `prism` cascade tick.
//
// Per spec §10.1 dispatch table:
//
// prism(@path, five_op_block) ->
// emit Rust struct + #[derive(Prism)] per §4.1.2
//
// And §4.1.2 spelled out:
//
// `prism` declarations → emit a Rust `struct` plus a
// `#[derive(Prism)]` annotation plus the `#[oid("@X")]` attribute.
// The five-op block at the substrate level becomes the
// `Prism` trait impl scaffolding; the `prism-derive` proc-macro
// fills in the optic accessors.
//
// Cascade order per `shards/code/rust/macro.mirror`:
// type (T23 ✓) → prism (T24, here) → action → grammar.
//
// Name conversion at this altitude:
// substrate path `@parse` → struct name `Parse`
// (drop leading `@`; the final segment is PascalCased; the same
// rule as `point` → `Point` in the type cascade).
//
// Multi-segment paths (`@code/parse`) are forward-promised — the
// smallest tick witnesses only the single-segment case.
//
// The five-op block (`focus parse project parse split parse shift
// parse settle parse`) is parsed-but-not-encoded-in-fields at this
// tick. It is the universal Prism algebra; `#[derive(Prism)]` fills
// it in via the existing prism-derive expansion. Cascade ticks that
// wire substrate-named optic accessors (fields with `#[lens]` /
// `#[prism]` etc.) come later.
// ---------------------------------------------------------------------------
/// Bare `prism @path` declaration emits a unit struct with the
/// `#[derive(Prism)]` annotation and `#[oid("@path")]` attribute.
/// Round-trip OID identity (spec §5 law 2 + §6) at the
/// canonical-form altitude.
///
/// Substrate input (the smallest prism declaration — bare path,
/// no body):
///
/// ```ignore
/// prism @parse
/// ```
///
/// Expected Rust emission:
///
/// ```ignore
/// #[derive(prismqueer::DerivePrism)]
/// #[oid("@parse")]
/// pub struct Parse;
/// ```
/// Explicit five-op block produces the same unit-struct emission as
/// the bare form. The five-op block is the universal Prism algebra;
/// `#[derive(Prism)]` fills it in. Declaring it explicitly at the
/// substrate altitude is documentation, not extra encoded state.
///
/// Substrate input:
///
/// ```ignore
/// prism @kernel { focus kernel project kernel split kernel
/// shift kernel settle kernel }
/// ```
///
/// Expected emission: same as the bare form, just with `@kernel`
/// instead of `@parse`.
/// OID functionality (spec §5 law 3) at the `prism` shape:
/// substrate paths uniquely determine the runtime address. Two
/// different prism declarations with different `@paths` produce
/// different runtime OIDs (the address discriminates).
///
/// This is the structural inverse of the type-shape's
/// `shim_is_a_function_of_substrate_oid`: there, identical input →
/// identical output. Here, distinct addresses → distinct OIDs (the
/// shim is injective on substrate paths).
// ---------------------------------------------------------------------------
// T25 (2026-06-09): the `action` cascade tick.
//
// Per spec §10.1 dispatch table (now at `@code/metalogue` ground):
//
// action(name, args, ret, \ body) ->
// match classify(action):
// | substrate(target_altitude) ->
// emit pub fn matching signature;
// body = lowered_target_altitude_grammar
// | boundary(@io/species) ->
// emit pub fn matching signature;
// body = @io.species.invocation
// | partial(opacity_map) ->
// emit pub fn matching signature;
// body = mixed (per-function refinement; audition tournament)
//
// And §4.1.3:
//
// `action` declarations with `\` body → emit a Rust `pub fn`
// matching the signature. The body is the shim's responsibility:
// the realisation discriminator (T21's @mirror/realisation.classify)
// chooses substrate / boundary / partial.
//
// FLOOR (this tick): substrate `action name(args) -> ret { \ }`
// emits Rust `pub fn name(args) -> ret { todo!() }`. The `\` IS the
// question; the body resolution is forward-promised. This tick
// lands the TYPED SIGNATURE ROUND-TRIP — the load-bearing claim
// that the substrate signature determines the Rust signature, the
// shim is signature-preserving, and the OID law discriminates on
// signatures.
//
// The three dispatch sub-cases (substrate / boundary / partial) are
// forward-promised. The floor witnesses the universal claim: the
// emission is a pub fn with the substrate's typed signature. The
// body fill-in is a separate cascade tick (T25.5? or T26+).
//
// Name conversion at the action altitude: substrate names stay
// lowercase (they are Rust function names, not types). Substrate
// `increment` → Rust `increment`. No PascalCase at this altitude.
//
// Cascade order per `shards/code/rust/macro.mirror` (now inheriting
// from `@code/metalogue` ground at `7503a1a`):
// type (T23 ✓) → prism (T24 ✓) → action (T25, here) → grammar.
//
// === The `\` → `{}` translation at the Rust altitude ===
//
// Substrate-pull recognition surfaced during T25 GREEN (2026-06-09):
// the substrate's `\` body token cannot appear in Rust source code
// outside of string / char / byte literals — rustc's lexer rejects
// it as "unknown start of token: \". The `\` IS the substrate's
// question marker (`[[architecture-prism-as-trait-as-everything]]`'s
// obligation block), and it survives lossily through every species
// whose lexer admits free backslashes. Rust does not. The
// Rust-altitude rendering of substrate `{ \ }` is the empty body
// `{ }`: same semantic ("unresolved"), Rust-lexer-realisable shape.
// The shim translates substrate `\`-body to Rust `todo!()`-body at
// emission; the proc-macro accepts `{ }` (empty body) as the
// substrate's `{ \ }` at the Rust call-site altitude.
//
// This IS a substrate-pull seam — the species' lexical surface
// constrains the substrate's surface form. The `\` semantic
// ("unresolved body") survives; the `\` glyph does not. Per
// `[[architecture-glass-wall-substrate-types]]`, the seam is
// where the typed semantic crosses the species-lexer boundary;
// the substrate-side `.mirror` declarations keep their `\`, the
// Rust-side proc-macro call sites use `{ }`. The translation is
// the shim's responsibility (it is on the Rust altitude side of
// the glass wall).
// ---------------------------------------------------------------------------
/// Single-argument substrate action with `\` body emits a Rust
/// `pub fn` matching the signature, with `todo!()` body. Witnesses
/// round-trip OID identity at the canonical-form altitude
/// (spec §5 law 2 + §6).
///
/// Substrate input (at the `.mirror` altitude):
///
/// ```ignore
/// action increment(x: u32) -> u32 { \ }
/// ```
///
/// Rust-altitude rendering (the proc-macro call site, per the
/// substrate-pull seam doc'd above): empty body `{ }`.
///
/// Expected Rust emission:
///
/// ```ignore
/// pub fn increment(x: u32) -> u32 { todo!() }
/// ```
/// Nullary substrate action emits a `pub fn` with no args; the
/// `() -> ()` floor case. Witnesses that the shim handles the
/// zero-arg / unit-return shape (the substrate's null morphism
/// at the action altitude).
///
/// Substrate input (at the `.mirror` altitude):
///
/// ```ignore
/// action effect() -> () { \ }
/// ```
///
/// Rust-altitude rendering (per the substrate-pull seam doc'd at
/// the section header above): empty body `{ }`.
///
/// Expected Rust emission:
///
/// ```ignore
/// pub fn effect() -> () { todo!() }
/// ```
/// OID functionality (spec §5 law 3) at the `action` shape: distinct
/// substrate signatures produce distinct emission OIDs. The shim is
/// injective on (name, args, return). Same signature → same OID;
/// different signature → different OID. The address discriminates on
/// the typed surface.
///
/// Two distinct action declarations (substrate altitude):
///
/// ```ignore
/// action add(x: u32, y: u32) -> u32 { \ }
/// action sub(x: u32, y: u32) -> u32 { \ }
/// ```
///
/// Rust-altitude rendering (per the substrate-pull seam doc'd at
/// the section header above): empty bodies `{ }`.
///
/// must produce different canonical-form OIDs (different fn names).
/// And a signature change (different arity) must produce a different
/// OID too.
// ---------------------------------------------------------------------------
// T26 (2026-06-11): the `grammar` cascade tick.
//
// Per spec §10.1 dispatch table (now at `@code/metalogue` ground):
//
// grammar(@path, extensions, body) ->
// emit Rust unit struct + #[derive(prismqueer::DerivePrism)]
// + #[oid("@path")] per §4.1.4
//
// And §4.1.4 spelled out:
//
// `grammar` declarations → emit a Rust unit struct with
// `#[derive(prismqueer::DerivePrism)]` and `#[oid("@path")]`. The
// extension list and body block at the substrate altitude are
// parsed-but-not-encoded at the floor — the substrate path IS the
// runtime address (Addressable::oid law). Body encoding (the
// `<op> <keyword>` keyword table) is forward-promised to T26.5+
// (consumer-pull).
//
// Cascade order per `shards/code/rust/macro.mirror` (inheriting from
// `@code/metalogue` ground):
// type (T23 ✓) → prism (T24 ✓) → action (T25 ✓) → grammar (T26, here).
//
// === Why grammar shares prism's emission shape at the floor ===
//
// Both `prism @X` and `grammar @X` are substrate altitudes addressed
// by a path. The substrate differentiates them by their family-root
// declaration (the `.mirror` source). At the Rust altitude the
// Addressable impl is structural: same shape, distinct OIDs by
// virtue of distinct substrate paths. The four laws hold identically.
//
// The substrate `grammar @path(ext1, ext2, ...) { body }` form
// claims the listed file extensions (the tokenizer routes those
// extensions through this grammar's keyword table) and the body
// block contains `<op> <keyword>` pairs that bootstrap's keyword
// harvester merges into the tokenizer's lookup. The Rust-altitude
// floor witnesses only the path; downstream consumer-pull ticks
// encode the extensions and body when needed.
// ---------------------------------------------------------------------------
/// Bare `grammar @path { }` declaration (no extension list, empty
/// body) emits a unit struct with `#[derive(Prism)]` + `#[oid("@path")]`.
/// Round-trip OID identity at the canonical-form altitude.
///
/// Substrate input:
///
/// ```ignore
/// grammar @parse_spec { }
/// ```
///
/// Expected Rust emission:
///
/// ```ignore
/// #[derive(prismqueer::DerivePrism)]
/// #[oid("@parse_spec")]
/// pub struct ParseSpec;
/// ```
/// `grammar @path("ext1") { body }` with a single extension claim and
/// a body block emits the same unit-struct shape as the bare form.
/// The extensions and body are parsed-but-not-encoded (forward-
/// promised). The substrate's `grammar @mirror/spec("spec") { ... }`
/// shape used in `shards/mirror/spec/keywords.mirror` exercises this
/// case.
/// OID functionality (spec §5 law 3) at the `grammar` shape: distinct
/// substrate paths produce distinct runtime OIDs. Two grammars with
/// different `@paths` produce different runtime addresses. The address
/// discriminates on the substrate path, regardless of extension claims.
/// Type-soundness witness (spec §5 law 1) for `grammar` declarations:
/// the substrate-pull-canonical form is a unit struct usable at the
/// Rust altitude. Construction succeeds (the emission is well-typed).
/// This is the structural-soundness counterpart of the type cascade's
/// `record_type_preserves_field_names_and_types`.