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
//! Proc macro crate for the UOR Foundation.
//!
//! Provides the `uor!` macro (since v0.2.0) and the v0.2.1 ergonomics
//! macros: `uor_ground!`, `#[derive(ConstrainedType)]`, `#[derive(CompileUnit)]`,
//! and the `#[uor_grounded]` attribute. The v0.2.1 macros wire downstream code
//! into the foundation's sealed-constructor minting path so the consumer-facing
//! one-liners in `uor_foundation::enforcement::prelude` work end-to-end at
//! compile time.
//!
//! # Usage
//!
//! ```rust,ignore
//! use uor_foundation::uor;
//!
//! // Type declaration (named-argument constraint syntax)
//! let pixel = uor! { type Pixel { ResidueConstraint(modulus: 256, residue: 255); } };
//!
//! // Term expression
//! let sum = uor! { add(mul(3, 5), 7) };
//!
//! // Assertion (ground — checked at compile time)
//! uor! { assert add(1, 2) = 3; };
//! ```
use TokenStream;
/// The UOR term language DSL macro.
///
/// Parses EBNF surface syntax at compile time and produces typed `Term` ASTs
/// in the `uor_foundation::enforcement` module. The macro handles:
///
/// - **Term expressions**: `add(mul(3, 5), 7)` — operation applications
/// - **Type declarations**: `type Pixel { ResidueConstraint(modulus: 256, residue: 255); }` — constrained types
/// - **Bindings**: `let x : Pixel = add(0, 0);` — named term bindings
/// - **Assertions**: `assert lhs = rhs;` — ground assertions checked at compile time
/// - **Effect declarations**: `effect Name { target: {0,1}; delta: 0; commutes: true; }` — generic props
/// - **Boundary declarations**: `source name : Type via grounding;`
/// - **Quantum literals**: `42@Q7` — level-annotated integers
/// - **Lift/Project**: `lift(x, Q3)`, `project(y, Q0)` — level transitions
/// - **Match**: `match x { pred => expr; otherwise => expr; }`
/// - **Recursion**: `recurse f(n) measure n base is_zero => 1 step => mul(n, f(pred(n)))`
/// - **Streams**: `unfold nat : Successor from 0`
///
/// # Examples
///
/// ```rust,ignore
/// use uor_foundation::uor;
///
/// // Term expressions produce a TermArena with the expression tree.
/// let sum = uor! { add(mul(3, 5), 7) };
///
/// // Quantum-annotated literals tag a value at a specific ring width.
/// let wide = uor! { 144115188075855617@Q7 };
///
/// // Type declarations define constrained types.
/// let pixel = uor! {
/// type Pixel {
/// ResidueConstraint(modulus: 256, residue: 255);
/// HammingConstraint(hammingBound: 8);
/// DepthConstraint(minDepth: 0, maxDepth: 1);
/// }
/// };
///
/// // Bindings carry surface syntax and content addresses.
/// let origin = uor! { let origin : Pixel = add(0, 0); };
///
/// // Ground assertions are checked at COMPILE TIME.
/// uor! { assert add(1, 2) = 3; };
/// uor! { assert mul(3, 5) = 15; };
///
/// // Effect declarations register fiber-targeted effects.
/// let blit = uor! {
/// effect Blit {
/// target: {0, 1, 2, 3};
/// delta: 0;
/// commutes: true;
/// }
/// };
///
/// // Boundary declarations define data sources and sinks.
/// uor! { source pixel_in : Pixel via sRGB; };
/// uor! { sink pixel_out : Pixel via DisplayP3; };
///
/// // Lift/Project handle level transitions explicitly.
/// let widened = uor! { lift(x, Q3) };
/// let narrowed = uor! { project(y, Q0) };
///
/// // Match expressions with pattern arms and a required otherwise arm.
/// let clamped = uor! {
/// match x {
/// is_negative => 0;
/// exceeds_max => 255;
/// otherwise => x;
/// }
/// };
///
/// // Bounded recursion with a descent measure and base case.
/// let factorial = uor! {
/// recurse fact(n)
/// measure n
/// base is_zero => 1
/// step => mul(n, fact(pred(n)))
/// };
///
/// // Stream construction via unfold (coinductive).
/// let naturals = uor! { unfold nat : Successor from 0 };
/// ```
///
/// # Errors
///
/// Parse errors produce `compile_error!()` at the macro call site.
/// The error message includes the unexpected token:
///
/// ```text
/// uor! { add(1, ) }
/// // error: uor! parse error: Unexpected token in expression: RParen
/// ```
///
/// Ground assertions that fail ring evaluation also produce compile errors:
///
/// ```text
/// uor! { assert add(1, 2) = 4; }
/// // error: assertion failed at compile time
/// ```
/// The v0.2.1 ground-state DSL macro.
///
/// Parses a `conformance-program` production of `uor.conformance.ebnf` whose
/// `compile_unit` body contains a term-language program. Lowers through the
/// reduction pipeline at build time and expands to a `Grounded<T>` value via
/// the foundation crate's back-door minting API.
///
/// In v0.2.1 the in-process pipeline driver is a stub: it parses the
/// conformance grammar, validates the keyword set, and emits a back-door
/// `Grounded<T>` constructor call for inputs in the smoke-test corpus.
/// Non-corpus inputs produce a `compile_error!` citing the
/// `reduction:ConvergenceStall` IRI for downstream tooling to surface.
///
/// # Examples
///
/// ```rust,ignore
/// use uor_foundation::enforcement::prelude::*;
///
/// #[derive(ConstrainedType)]
/// #[uor(residue = 65535, hamming = 16)]
/// struct MatVec<const M: usize, const K: usize>;
///
/// let unit: Grounded<MatVec<64, 2048>> = uor_ground! {
/// compile_unit matvec_q32 {
/// root_term: { /* ... */ };
/// witt_level_ceiling: W32;
/// thermodynamic_budget: 2048.0;
/// target_domains: { ComposedAlgebraic, ArithmeticValuation };
/// }
/// };
/// ```
/// Derive `ConstrainedType` for a struct.
///
/// Generates a `GroundedShape` impl so the struct can appear as the type
/// parameter of `Grounded<T>`. v0.2.1 emits the impl unconditionally; the
/// `#[uor(residue = …, hamming = …, …)]` attributes are recorded as constants
/// for downstream introspection.
///
/// # Examples
///
/// ```rust,ignore
/// use uor_foundation_macros::ConstrainedType;
///
/// #[derive(ConstrainedType)]
/// #[uor(residue = 255, hamming = 8)]
/// struct Pixel(u8);
/// ```
/// Derive `CompileUnit` for a struct whose fields name the v0.2.1 builder
/// inputs (`builder_root_term`, `builder_witt_level_ceiling`,
/// `builder_thermodynamic_budget`, `builder_target_domains`).
///
/// Generates a `build_compile_unit(&self) -> CompileUnit` method that calls
/// the v0.2.0 `CompileUnitBuilder` chain.
/// Attribute marker asserting that the function body lowers cleanly at the
/// named Witt level.
///
/// v0.2.1 parses `level = "W8" | "W16" | "W24" | "W32"` and emits a
/// `const _: ::uor_foundation::WittLevel = ::uor_foundation::WittLevel::WN;`
/// static assertion after the function. Any undefined level name fails
/// const evaluation at type-check time.
///
/// # Examples
///
/// ```rust,ignore
/// #[uor_foundation_macros::uor_grounded(level = "W32")]
/// fn matvec() -> Grounded<MatVec<64, 2048>> { /* ... */ }
/// ```