Skip to main content

uor_foundation/
blanket_impls.rs

1// @codegen-exempt — Phase 16 hand-written per-class observable view newtypes.
2// emit::write_file's banner check preserves this file across `uor-crate` runs.
3//
4// Phase 11 emitted blanket Observable<H> + leaf-trait impls directly on
5// `Validated<T, Phase>`, but Rust forbids multiple `Observable<H>`
6// impls per type — the bare `value()` returned `H::EMPTY_DECIMAL` for
7// every kind, hiding the kind-specific scalar from callers.
8//
9// Phase 16 introduces five per-kind newtype wrappers, each with its
10// own `Observable<H>::value()` body delegating to the appropriate
11// primitive. The bare `Validated<T, Phase>` no longer impls
12// `Observable<H>` — consumers reach for an explicit view via the
13// inherent accessors `Validated::as_landauer()`, `as_jacobian()`,
14// `as_carry_depth()`, `as_derivation_depth()`, `as_free_rank()`.
15//
16// Coherence with Phase 7 + Phase 8:
17//   - `impl {Foo}<H> for Null{Foo}<H>`               — Phase 7 (resolver-absent)
18//   - `impl {Foo}<H> for Resolved{Foo}<'r, R, H>`     — Phase 8 (content-addressed)
19//   - `impl {Foo}<H> for Validated{Foo}View<T, Phase>` — Phase 16 (primitive-backed)
20// `Null{Foo}<H>`, `Resolved{Foo}<'r, R, H>`, and the Validated*View
21// newtypes are mutually disjoint concrete types, so each impl closes
22// the orphan without overlapping.
23
24#![allow(clippy::module_name_repetitions)]
25
26use crate::bridge::derivation::DerivationDepthObservable;
27use crate::bridge::observable::{JacobianObservable, LandauerBudget, Observable, ThermoObservable};
28use crate::bridge::partition::FreeRankObservable;
29use crate::enforcement::{Validated, ValidationPhase};
30use crate::enums::MeasurementUnit;
31use crate::kernel::carry::CarryDepthObservable;
32use crate::pipeline::ConstrainedTypeShape;
33use crate::{DecimalTranscendental, HostTypes};
34
35// ── Per-class observable views ─────────────────────────────────────
36//
37// Each view is a zero-cost newtype carrying `PhantomData<(fn() -> T,
38// Phase)>` so the wrapper is `Send + Sync` regardless of `T` and
39// works with `T: ?Sized`. Construct via the inherent
40// `Validated::as_*` accessors.
41
42/// Observable view: Landauer-cost projection of `Validated<T, Phase>`.
43/// `value()` returns the Landauer-cost in nats (delegates to
44/// `landauer_nats(self)`); the leaf trait `LandauerBudget<H>` is
45/// implemented with the same primitive-derived value.
46pub struct ValidatedLandauerView<T, Phase: ValidationPhase>(
47    core::marker::PhantomData<(fn() -> T, Phase)>,
48);
49
50/// Observable view: per-site Jacobian projection of `Validated<T, Phase>`.
51/// `value()` returns the L1-norm of the Jacobian row computed by
52/// `primitive_curvature_jacobian::<T>()`.
53pub struct ValidatedJacobianView<T, Phase: ValidationPhase>(
54    core::marker::PhantomData<(fn() -> T, Phase)>,
55);
56
57/// Observable view: carry-depth projection of `Validated<T, Phase>`.
58/// `value()` returns the dihedral orbit size from
59/// `primitive_dihedral_signature::<T>()`.
60pub struct ValidatedCarryDepthView<T, Phase: ValidationPhase>(
61    core::marker::PhantomData<(fn() -> T, Phase)>,
62);
63
64/// Observable view: derivation-depth projection of `Validated<T, Phase>`.
65/// `value()` returns the reduction-step count from
66/// `primitive_terminal_reduction::<T>(W8)`.
67pub struct ValidatedDerivationDepthView<T, Phase: ValidationPhase>(
68    core::marker::PhantomData<(fn() -> T, Phase)>,
69);
70
71/// Observable view: free-rank projection of `Validated<T, Phase>`.
72/// `value()` returns the free-rank residual from
73/// `primitive_descent_metrics::<T>(&nerve_betti).0`.
74pub struct ValidatedFreeRankView<T, Phase: ValidationPhase>(
75    core::marker::PhantomData<(fn() -> T, Phase)>,
76);
77
78// Manual trait impls — `derive` would propagate spurious bounds
79// (`T: Eq`, `T: Hash`, etc.) onto consumers' shape markers. The
80// PhantomData inside is unconditionally `Copy + Eq + Hash + Default`,
81// so the impls hold for every `T: ?Sized` and `Phase: ValidationPhase`.
82
83macro_rules! impl_view_traits {
84    ($name:ident, $label:literal) => {
85        impl<T, Phase: ValidationPhase> core::fmt::Debug for $name<T, Phase> {
86            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87                f.write_str($label)
88            }
89        }
90        impl<T, Phase: ValidationPhase> Default for $name<T, Phase> {
91            #[inline]
92            fn default() -> Self {
93                Self(core::marker::PhantomData)
94            }
95        }
96        impl<T, Phase: ValidationPhase> Clone for $name<T, Phase> {
97            #[inline]
98            fn clone(&self) -> Self {
99                *self
100            }
101        }
102        impl<T, Phase: ValidationPhase> Copy for $name<T, Phase> {}
103        impl<T, Phase: ValidationPhase> PartialEq for $name<T, Phase> {
104            #[inline]
105            fn eq(&self, _other: &Self) -> bool {
106                true
107            }
108        }
109        impl<T, Phase: ValidationPhase> Eq for $name<T, Phase> {}
110        impl<T, Phase: ValidationPhase> core::hash::Hash for $name<T, Phase> {
111            #[inline]
112            fn hash<S: core::hash::Hasher>(&self, _state: &mut S) {}
113        }
114    };
115}
116
117impl_view_traits!(ValidatedLandauerView, "ValidatedLandauerView");
118impl_view_traits!(ValidatedJacobianView, "ValidatedJacobianView");
119impl_view_traits!(ValidatedCarryDepthView, "ValidatedCarryDepthView");
120impl_view_traits!(ValidatedDerivationDepthView, "ValidatedDerivationDepthView");
121impl_view_traits!(ValidatedFreeRankView, "ValidatedFreeRankView");
122
123// Constructors — all `pub const fn new()` returning `PhantomData::default`.
124
125impl<T, Phase: ValidationPhase> ValidatedLandauerView<T, Phase> {
126    /// Construct a zero-cost Landauer view. Equivalent to
127    /// `Validated::<T, Phase>::as_landauer()`.
128    #[inline]
129    #[must_use]
130    pub const fn new() -> Self {
131        Self(core::marker::PhantomData)
132    }
133}
134
135impl<T, Phase: ValidationPhase> ValidatedJacobianView<T, Phase> {
136    /// Construct a zero-cost Jacobian view. Equivalent to
137    /// `Validated::<T, Phase>::as_jacobian()`.
138    #[inline]
139    #[must_use]
140    pub const fn new() -> Self {
141        Self(core::marker::PhantomData)
142    }
143}
144
145impl<T, Phase: ValidationPhase> ValidatedCarryDepthView<T, Phase> {
146    /// Construct a zero-cost carry-depth view. Equivalent to
147    /// `Validated::<T, Phase>::as_carry_depth()`.
148    #[inline]
149    #[must_use]
150    pub const fn new() -> Self {
151        Self(core::marker::PhantomData)
152    }
153}
154
155impl<T, Phase: ValidationPhase> ValidatedDerivationDepthView<T, Phase> {
156    /// Construct a zero-cost derivation-depth view. Equivalent to
157    /// `Validated::<T, Phase>::as_derivation_depth()`.
158    #[inline]
159    #[must_use]
160    pub const fn new() -> Self {
161        Self(core::marker::PhantomData)
162    }
163}
164
165impl<T, Phase: ValidationPhase> ValidatedFreeRankView<T, Phase> {
166    /// Construct a zero-cost free-rank view. Equivalent to
167    /// `Validated::<T, Phase>::as_free_rank()`.
168    #[inline]
169    #[must_use]
170    pub const fn new() -> Self {
171        Self(core::marker::PhantomData)
172    }
173}
174
175// Inherent accessors on `Validated<T, Phase>` to construct each view
176// without forcing callers to spell out the newtype path.
177
178impl<T, Phase: ValidationPhase> Validated<T, Phase>
179where
180    T: ConstrainedTypeShape,
181{
182    /// Phase 16 — Landauer-cost view of `self`.
183    #[inline]
184    #[must_use]
185    pub const fn as_landauer(&self) -> ValidatedLandauerView<T, Phase> {
186        ValidatedLandauerView::new()
187    }
188
189    /// Phase 16 — Jacobian view of `self`.
190    #[inline]
191    #[must_use]
192    pub const fn as_jacobian(&self) -> ValidatedJacobianView<T, Phase> {
193        ValidatedJacobianView::new()
194    }
195
196    /// Phase 16 — carry-depth view of `self`.
197    #[inline]
198    #[must_use]
199    pub const fn as_carry_depth(&self) -> ValidatedCarryDepthView<T, Phase> {
200        ValidatedCarryDepthView::new()
201    }
202
203    /// Phase 16 — derivation-depth view of `self`.
204    #[inline]
205    #[must_use]
206    pub const fn as_derivation_depth(&self) -> ValidatedDerivationDepthView<T, Phase> {
207        ValidatedDerivationDepthView::new()
208    }
209
210    /// Phase 16 — free-rank view of `self`.
211    #[inline]
212    #[must_use]
213    pub const fn as_free_rank(&self) -> ValidatedFreeRankView<T, Phase> {
214        ValidatedFreeRankView::new()
215    }
216}
217
218// ── Observable<H> + leaf-trait impls per view ──────────────────────
219
220// Default Observable supertrait helpers — every view returns the same
221// host-empty reference for source/target and the enum default for
222// has_unit. Only `value()` differs per view.
223#[inline]
224fn empty_source<H: HostTypes>() -> &'static H::HostString {
225    H::EMPTY_HOST_STRING
226}
227
228#[inline]
229fn empty_target<H: HostTypes>() -> &'static H::HostString {
230    H::EMPTY_HOST_STRING
231}
232
233// ── ValidatedLandauerView — primitive-backed entropy_nats ──────────
234impl<T, Phase, H> Observable<H> for ValidatedLandauerView<T, Phase>
235where
236    T: ConstrainedTypeShape,
237    Phase: ValidationPhase,
238    H: HostTypes,
239{
240    #[inline]
241    fn value(&self) -> H::Decimal {
242        <Self as LandauerBudget<H>>::landauer_nats(self)
243    }
244    #[inline]
245    fn source(&self) -> &H::HostString {
246        empty_source::<H>()
247    }
248    #[inline]
249    fn target(&self) -> &H::HostString {
250        empty_target::<H>()
251    }
252    #[inline]
253    fn has_unit(&self) -> MeasurementUnit {
254        MeasurementUnit::default()
255    }
256}
257
258impl<T, Phase, H> ThermoObservable<H> for ValidatedLandauerView<T, Phase>
259where
260    T: ConstrainedTypeShape,
261    Phase: ValidationPhase,
262    H: HostTypes,
263{
264    #[inline]
265    fn hardness_estimate(&self) -> H::Decimal {
266        H::EMPTY_DECIMAL
267    }
268}
269
270impl<T, Phase, H> LandauerBudget<H> for ValidatedLandauerView<T, Phase>
271where
272    T: ConstrainedTypeShape,
273    Phase: ValidationPhase,
274    H: HostTypes,
275{
276    #[inline]
277    fn landauer_nats(&self) -> H::Decimal {
278        let nerve = match crate::enforcement::primitive_simplicial_nerve_betti::<T>() {
279            Ok(b) => b,
280            Err(_) => return H::EMPTY_DECIMAL,
281        };
282        let (_residual, entropy_bits) = crate::enforcement::primitive_descent_metrics::<T>(&nerve);
283        <H::Decimal as DecimalTranscendental>::from_bits(entropy_bits)
284    }
285}
286
287// ── ValidatedJacobianView — L1 of the Jacobian row ─────────────────
288impl<T, Phase, H> Observable<H> for ValidatedJacobianView<T, Phase>
289where
290    T: ConstrainedTypeShape,
291    Phase: ValidationPhase,
292    H: HostTypes,
293{
294    #[inline]
295    fn value(&self) -> H::Decimal {
296        let jac = crate::enforcement::primitive_curvature_jacobian::<T>();
297        let mut sum: u64 = 0;
298        let mut i = 0;
299        while i < jac.len() {
300            sum = sum.saturating_add(u64::from(jac[i].unsigned_abs()));
301            i += 1;
302        }
303        <H::Decimal as DecimalTranscendental>::from_u64(sum)
304    }
305    #[inline]
306    fn source(&self) -> &H::HostString {
307        empty_source::<H>()
308    }
309    #[inline]
310    fn target(&self) -> &H::HostString {
311        empty_target::<H>()
312    }
313    #[inline]
314    fn has_unit(&self) -> MeasurementUnit {
315        MeasurementUnit::default()
316    }
317}
318
319impl<T, Phase, H> JacobianObservable<H> for ValidatedJacobianView<T, Phase>
320where
321    T: ConstrainedTypeShape,
322    Phase: ValidationPhase,
323    H: HostTypes,
324{
325}
326
327// ── ValidatedCarryDepthView — dihedral orbit size ──────────────────
328impl<T, Phase, H> Observable<H> for ValidatedCarryDepthView<T, Phase>
329where
330    T: ConstrainedTypeShape,
331    Phase: ValidationPhase,
332    H: HostTypes,
333{
334    #[inline]
335    fn value(&self) -> H::Decimal {
336        let (orbit_size, _period) = crate::enforcement::primitive_dihedral_signature::<T>();
337        <H::Decimal as DecimalTranscendental>::from_u32(orbit_size)
338    }
339    #[inline]
340    fn source(&self) -> &H::HostString {
341        empty_source::<H>()
342    }
343    #[inline]
344    fn target(&self) -> &H::HostString {
345        empty_target::<H>()
346    }
347    #[inline]
348    fn has_unit(&self) -> MeasurementUnit {
349        MeasurementUnit::default()
350    }
351}
352
353impl<T, Phase, H> CarryDepthObservable<H> for ValidatedCarryDepthView<T, Phase>
354where
355    T: ConstrainedTypeShape,
356    Phase: ValidationPhase,
357    H: HostTypes,
358{
359}
360
361// ── ValidatedDerivationDepthView — terminal-reduction step count ───
362impl<T, Phase, H> Observable<H> for ValidatedDerivationDepthView<T, Phase>
363where
364    T: ConstrainedTypeShape,
365    Phase: ValidationPhase,
366    H: HostTypes,
367{
368    #[inline]
369    fn value(&self) -> H::Decimal {
370        // W8 (8-bit Witt level) — the standard baseline.
371        match crate::enforcement::primitive_terminal_reduction::<T>(8u16) {
372            Ok((_witt, length, _sat)) => <H::Decimal as DecimalTranscendental>::from_u32(length),
373            Err(_) => H::EMPTY_DECIMAL,
374        }
375    }
376    #[inline]
377    fn source(&self) -> &H::HostString {
378        empty_source::<H>()
379    }
380    #[inline]
381    fn target(&self) -> &H::HostString {
382        empty_target::<H>()
383    }
384    #[inline]
385    fn has_unit(&self) -> MeasurementUnit {
386        MeasurementUnit::default()
387    }
388}
389
390impl<T, Phase, H> DerivationDepthObservable<H> for ValidatedDerivationDepthView<T, Phase>
391where
392    T: ConstrainedTypeShape,
393    Phase: ValidationPhase,
394    H: HostTypes,
395{
396}
397
398// ── ValidatedFreeRankView — free-rank residual ─────────────────────
399impl<T, Phase, H> Observable<H> for ValidatedFreeRankView<T, Phase>
400where
401    T: ConstrainedTypeShape,
402    Phase: ValidationPhase,
403    H: HostTypes,
404{
405    #[inline]
406    fn value(&self) -> H::Decimal {
407        let nerve = match crate::enforcement::primitive_simplicial_nerve_betti::<T>() {
408            Ok(b) => b,
409            Err(_) => return H::EMPTY_DECIMAL,
410        };
411        let (residual, _entropy_bits) = crate::enforcement::primitive_descent_metrics::<T>(&nerve);
412        <H::Decimal as DecimalTranscendental>::from_u32(residual)
413    }
414    #[inline]
415    fn source(&self) -> &H::HostString {
416        empty_source::<H>()
417    }
418    #[inline]
419    fn target(&self) -> &H::HostString {
420        empty_target::<H>()
421    }
422    #[inline]
423    fn has_unit(&self) -> MeasurementUnit {
424        MeasurementUnit::default()
425    }
426}
427
428impl<T, Phase, H> FreeRankObservable<H> for ValidatedFreeRankView<T, Phase>
429where
430    T: ConstrainedTypeShape,
431    Phase: ValidationPhase,
432    H: HostTypes,
433{
434}