prism/std_types.rs
1//! Standard type library.
2//!
3//! `std_types` is `prism`'s realization of the wiki's
4//! [Building Block View § Whitebox `prism`](https://github.com/UOR-Foundation/UOR-Framework/wiki/05-Building-Block-View#whitebox-prism)
5//! component named "standard type library" — the catalog of pre-declared
6//! types built from `uor-foundation`'s vocabulary, available so
7//! application authors do not have to derive common shape patterns
8//! from first principles. Per ADR-017 the catalog is **canonical**: it
9//! is the addressing surface that schema-import tools and applications
10//! target so traces and certificates address consistently across the
11//! ecosystem.
12//!
13//! The catalog is layered:
14//!
15//! - **Foundation-supplied surface (re-exports).** The ten morphism
16//! kinds (`BinaryGroundingMap`, …, `Utf8ProjectionMap`), the
17//! structural marker traits (`Total`, `Invertible`,
18//! `PreservesStructure`, `PreservesMetric`), the sealed
19//! `GroundedValue`/`GroundedShape` family, `ConstrainedTypeInput`,
20//! `CartesianProductShape` and its `kunneth_compose` helper, the
21//! partition-algebra families (`*Witness`, `*Evidence`,
22//! `*MintInputs`, `PartitionResolver`, `PartitionHandle`,
23//! `NullPartition`, `VerifiedMint`), and the `OntologyVerifiedMint`
24//! sealed mint trait.
25//! - **First-class prism-defined surface.** [`FixedSites<N>`],
26//! [`Bytes<N>`], and the byte-aligned numeric / character / boolean
27//! primitives (`U8` … `I256`, `F32`, `F64`, `Bool`, `Char`).
28//!
29//! ## IRI rule (closure under `uor-foundation`)
30//!
31//! The IRI of every prism-defined stdlib type is **derived from its
32//! constraint declaration, not from the Rust type name** — this is the
33//! direct quote from
34//! [Concepts § Closure Under uor-foundation][08-closure]
35//! and the binding rule of ADR-017. Concretely: every prism stdlib
36//! type with empty `CONSTRAINTS` shares the same IRI
37//! (`https://uor.foundation/type/ConstrainedType`, the foundation's
38//! ontology class for `ConstrainedTypeShape` instances). Instance
39//! identity flows through `(SITE_COUNT, CONSTRAINTS)`, so distinct
40//! site counts produce distinct content-addresses while same-shape
41//! Rust types (e.g., `U32` and `I32`) produce **identical**
42//! content-addresses by design — the Rust name is for the developer,
43//! the IRI is for content-addressing.
44//!
45//! See [AGENTS.md § 11](../../../AGENTS.md#11-standard-type-library-policy)
46//! for the inclusion / exclusion criteria, the catalog growth tracks
47//! (baseline vs. specialized), and the implementation pattern every
48//! stdlib type follows.
49//!
50//! [08-closure]: https://github.com/UOR-Foundation/UOR-Framework/wiki/08-Concepts#closure-under-uor-foundation
51//!
52//! # See also
53//!
54//! - [Wiki: 05 Building Block View § Whitebox `prism`](https://github.com/UOR-Foundation/UOR-Framework/wiki/05-Building-Block-View#whitebox-prism)
55//! - [Wiki: 09 Architecture Decisions § ADR-017](https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions)
56//! - [Wiki: 12 Glossary § Term Definitions](https://github.com/UOR-Foundation/UOR-Framework/wiki/12-Glossary#term-definitions)
57//!
58//! # Constraints
59//!
60//! - **TC-02** — the morphism-kind traits are sealed by foundation; no
61//! downstream extension is permitted
62//! - **TC-04** — the kind classification participates in compile-time
63//! UORassembly enforcement (a `Grounding` impl whose `Map` does not
64//! inhabit [`GroundingMapKind`] fails to compile)
65//! - **ADR-017** — addresses are content-deterministic; the catalog is
66//! operational, not declarative
67//!
68//! # C4 placement
69//!
70//! Component `standard type library` (Level 3) inside container `prism`
71//! (Level 2). It is consumed by application authors implementing
72//! [`uor_foundation::enforcement::Grounding`] or
73//! [`uor_foundation::enforcement::Sinking`].
74//!
75//! # Behavior
76//!
77//! ```rust
78//! // Given: the ten morphism-kind marker types
79//! // When: each is used as a phantom type parameter
80//! // Then: the foundation's sealed trait family classifies them
81//! // identically to the foundation's own use sites
82//! use prism::std_types::{
83//! BinaryGroundingMap, BinaryProjectionMap, DigestGroundingMap,
84//! DigestProjectionMap, IntegerGroundingMap, IntegerProjectionMap,
85//! JsonGroundingMap, JsonProjectionMap, Utf8GroundingMap,
86//! Utf8ProjectionMap,
87//! };
88//! fn _accepts_grounding<M: prism::std_types::GroundingMapKind>() {}
89//! fn _accepts_projection<M: prism::std_types::ProjectionMapKind>() {}
90//! _accepts_grounding::<BinaryGroundingMap>();
91//! _accepts_grounding::<DigestGroundingMap>();
92//! _accepts_grounding::<IntegerGroundingMap>();
93//! _accepts_grounding::<JsonGroundingMap>();
94//! _accepts_grounding::<Utf8GroundingMap>();
95//! _accepts_projection::<BinaryProjectionMap>();
96//! _accepts_projection::<DigestProjectionMap>();
97//! _accepts_projection::<IntegerProjectionMap>();
98//! _accepts_projection::<JsonProjectionMap>();
99//! _accepts_projection::<Utf8ProjectionMap>();
100//!
101//! // And: the structural marker traits classify each kind as the
102//! // ontology declares. `BinaryGroundingMap` is total and invertible;
103//! // `IntegerGroundingMap` additionally preserves structure; the
104//! // foundation rejects (at compile time) any attempt to claim a
105//! // structural property a kind does not carry.
106//! use prism::std_types::{Invertible, PreservesStructure, Total};
107//! fn _total_invertible<M: prism::std_types::GroundingMapKind + Total + Invertible>() {}
108//! fn _preserves_structure<M: prism::std_types::GroundingMapKind + PreservesStructure>() {}
109//! _total_invertible::<BinaryGroundingMap>();
110//! _total_invertible::<IntegerGroundingMap>();
111//! _preserves_structure::<IntegerGroundingMap>();
112//! _preserves_structure::<JsonGroundingMap>();
113//! _preserves_structure::<Utf8GroundingMap>();
114//! ```
115
116pub use uor_foundation::enforcement::{
117 BinaryGroundingMap, BinaryProjectionMap, DigestGroundingMap, DigestProjectionMap,
118 GroundingMapKind, IntegerGroundingMap, IntegerProjectionMap, JsonGroundingMap,
119 JsonProjectionMap, MorphismKind, ProjectionMapKind, Utf8GroundingMap, Utf8ProjectionMap,
120};
121
122// Sealed structural marker traits that classify morphism kinds. Authors
123// use these in trait bounds to require, for example, an invertible
124// grounding map without naming the concrete kind. The traits are
125// foundation-sealed; downstream cannot add new structural classes.
126pub use uor_foundation::enforcement::{Invertible, PreservesMetric, PreservesStructure, Total};
127
128// The two sealed `GroundedValue` variants returned by `Grounding` impls,
129// plus their sealed marker traits. `GroundedValue` is the closed set
130// of permitted intermediates (`GroundedCoord` and `GroundedTuple<N>`);
131// `GroundedShape` is the closed-set bound on the `T` of `Grounded<T>`.
132pub use uor_foundation::enforcement::{GroundedCoord, GroundedShape, GroundedTuple, GroundedValue};
133
134// `ConstrainedTypeInput` is the foundation's pre-declared canonical
135// constrained-type shape: a built-in `ConstrainedTypeShape` impl that
136// participates in the principal data path without the application
137// author having to declare a fresh shape. It is the closest thing the
138// standard type library has to a "prelude" type and is the canonical
139// example used in the trace-replay round-trip scenario.
140pub use uor_foundation::enforcement::ConstrainedTypeInput;
141
142// `CartesianProductShape` is the foundation's canonical
143// `ConstrainedTypeShape` for products of two component shapes (added in
144// uor-foundation 0.3.1). It routes nerve-Betti computation through
145// Künneth composition of component Betti profiles rather than flat
146// pair-enumeration. Selecting it in a `result_type::<P>()` call admits
147// a CartesianPartitionProduct unit through the principal data path.
148pub use uor_foundation::pipeline::kunneth_compose;
149pub use uor_foundation::pipeline::CartesianProductShape;
150
151// Partition-algebra evidence, witness, and mint-input families. These
152// are the cross-crate construction inputs and outputs for product,
153// coproduct, and Cartesian-product partitions added by foundation
154// 0.3.1's Product/Coproduct Completion Amendment. `PartitionResolver`,
155// `PartitionRecord`, `PartitionHandle`, and `NullPartition` are the
156// runtime-side carriers; `*Evidence`, `*Witness`, and `*MintInputs`
157// classify the verified-mint bundles.
158pub use uor_foundation::enforcement::{
159 CartesianProductEvidence, CartesianProductMintInputs, CartesianProductWitness, NullPartition,
160 PartitionCoproductEvidence, PartitionCoproductMintInputs, PartitionCoproductWitness,
161 PartitionHandle, PartitionProductEvidence, PartitionProductMintInputs, PartitionProductWitness,
162 PartitionRecord, PartitionResolver, VerifiedMint,
163};
164
165// `OntologyVerifiedMint` is the sealed mint trait introduced in 0.3.1
166// for ontology-derived Path-2 witnesses. It carries a `HostTypes`-
167// parameterized GAT `Inputs<H>` so witness inputs can hold
168// host-decimal and handle fields without leaking concrete types.
169pub use uor_foundation::OntologyVerifiedMint;
170
171// ---- First-class stdlib types (§ 11 of AGENTS.md) ----
172
173use uor_foundation::pipeline::{ConstrainedTypeShape, ConstraintRef};
174
175/// `FixedSites<N>` — admit exactly `N` sites, unconstrained per-site.
176///
177/// The simplest non-trivial standard-type-library citizen: a generic
178/// `ConstrainedTypeShape` that fixes a site count and imposes no
179/// per-site constraint. It is the parametric building block under any
180/// downstream shape that wants "this many sites, my own grounding
181/// admission decides what each site contains" — for example, a 32-byte
182/// hash output (32 sites at `WittLevel::W8`), an 80-byte Bitcoin block
183/// header (80 sites at `WittLevel::W8`), or a 16-element
184/// integer-vector at `WittLevel::W64`.
185///
186/// At any instantiation, `<FixedSites<N> as ConstrainedTypeShape>::SITE_COUNT == N`
187/// and `<FixedSites<N> as ConstrainedTypeShape>::CONSTRAINTS` is the empty
188/// slice (foundation reads "empty `CONSTRAINTS`" as "unconstrained" per
189/// the trait's normative documentation). The IRI is the foundation's
190/// `ConstrainedType` class IRI — shared across every empty-constraint
191/// stdlib type per [ADR-017][09-adr-017] and the closure rule documented
192/// in this module's header — so instance identity flows entirely
193/// through `(SITE_COUNT, CONSTRAINTS)`.
194///
195/// [09-adr-017]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
196///
197/// # See also
198///
199/// - [Wiki: 05 Building Block View § Whitebox `prism`](https://github.com/UOR-Foundation/UOR-Framework/wiki/05-Building-Block-View#whitebox-prism)
200/// - [Wiki: 09 Architecture Decisions § ADR-017](https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions)
201///
202/// # Constraints
203///
204/// - **TC-01** — admission is a compile-time activity; `SITE_COUNT` and
205/// `CONSTRAINTS` are `const`-evaluable
206/// - **TC-04** — bilateral compile-time enforcement: a downstream
207/// author who consumes `FixedSites<N>` cannot violate the contract
208/// without the toolchain rejecting their program
209/// - **ADR-013** — closure under `uor-foundation`: the body uses only
210/// foundation vocabulary (`ConstrainedTypeShape`, `ConstraintRef`)
211/// - **ADR-017** — content-addressed identity: the
212/// `(IRI, SITE_COUNT, CONSTRAINTS)` triple deterministically encodes
213/// each instantiation
214///
215/// # Behavior
216///
217/// ```rust
218/// // Given: a fixed-32-sites shape
219/// // When: its trait constants are read
220/// // Then: SITE_COUNT reflects N and CONSTRAINTS is empty
221/// use prism::pipeline::ConstrainedTypeShape;
222/// use prism::std_types::FixedSites;
223/// assert_eq!(<FixedSites<32> as ConstrainedTypeShape>::SITE_COUNT, 32);
224/// assert!(<FixedSites<32> as ConstrainedTypeShape>::CONSTRAINTS.is_empty());
225/// assert_eq!(
226/// <FixedSites<32> as ConstrainedTypeShape>::IRI,
227/// "https://uor.foundation/type/ConstrainedType",
228/// );
229/// // And: a different N produces a distinct content-address — same
230/// // IRI, different SITE_COUNT — so the (IRI, SITE_COUNT, CONSTRAINTS)
231/// // triple distinguishes the two instantiations.
232/// assert_eq!(<FixedSites<80> as ConstrainedTypeShape>::SITE_COUNT, 80);
233/// assert_eq!(
234/// <FixedSites<80> as ConstrainedTypeShape>::IRI,
235/// <FixedSites<32> as ConstrainedTypeShape>::IRI,
236/// );
237/// ```
238pub struct FixedSites<const N: usize>;
239
240impl<const N: usize> ConstrainedTypeShape for FixedSites<N> {
241 const IRI: &'static str = "https://uor.foundation/type/ConstrainedType";
242 const SITE_COUNT: usize = N;
243 const CONSTRAINTS: &'static [ConstraintRef] = &[];
244 // ADR-032: cardinality of the value-set under the discrete-clock
245 // model. Empty-constraint shapes at W8 semantics carry 256 values
246 // per site; `cartesian_product_shape` (homogeneous power) raises
247 // the per-site cycle to `SITE_COUNT` saturating. `FixedSites<0>`
248 // collapses to the identity (`CYCLE_SIZE = 1`), matching
249 // `ConstrainedTypeInput` and the foundation convention. The
250 // truncation of `N: usize` to `u32` is harmless: SITE_COUNT values
251 // that approach `u32::MAX` would already overflow `u64` and
252 // saturate to `u64::MAX` long before the cast loses information.
253 #[allow(clippy::cast_possible_truncation)]
254 const CYCLE_SIZE: u64 = 256u64.saturating_pow(N as u32);
255}
256
257/// `Bytes<N>` — byte-buffer admission intent of width `N`.
258///
259/// Structurally identical to [`FixedSites<N>`] and content-address-
260/// identical at equal `N` (closure rule: same constraint declaration ⇒
261/// same IRI ⇒ same UOR address). Use `Bytes<N>` when the unit's intent
262/// is "this is a byte sequence" and `FixedSites<N>` when the intent is
263/// "this is a generic site container of width N"; the Rust type name
264/// distinguishes intent at the call site, the IRI does not.
265///
266/// # See also
267///
268/// - [`crate::std_types`] — the family contract and IRI namespace
269/// - [Wiki: 05 Building Block View § Whitebox `prism`](https://github.com/UOR-Foundation/UOR-Framework/wiki/05-Building-Block-View#whitebox-prism)
270/// - [AGENTS.md § 11](../../../AGENTS.md#11-standard-type-library-policy)
271///
272/// # Constraints
273///
274/// - **TC-01** — admission is compile-time
275/// - **TC-04** — bilateral compile-time enforcement
276/// - **ADR-013** — closure under `uor-foundation`
277/// - **ADR-017** — content-addressed identity via the IRI
278///
279/// # Behavior
280///
281/// ```rust
282/// use prism::pipeline::ConstrainedTypeShape;
283/// use prism::std_types::{Bytes, FixedSites};
284/// // Same SITE_COUNT and same IRI as FixedSites<N> per closure.
285/// assert_eq!(<Bytes<32> as ConstrainedTypeShape>::SITE_COUNT, 32);
286/// assert_eq!(
287/// <Bytes<32> as ConstrainedTypeShape>::IRI,
288/// "https://uor.foundation/type/ConstrainedType",
289/// );
290/// assert_eq!(
291/// <Bytes<32> as ConstrainedTypeShape>::IRI,
292/// <FixedSites<32> as ConstrainedTypeShape>::IRI,
293/// );
294/// ```
295pub struct Bytes<const N: usize>;
296
297impl<const N: usize> ConstrainedTypeShape for Bytes<N> {
298 const IRI: &'static str = "https://uor.foundation/type/ConstrainedType";
299 const SITE_COUNT: usize = N;
300 const CONSTRAINTS: &'static [ConstraintRef] = &[];
301 // ADR-032: per closure, identical to `FixedSites<N>`. Truncation
302 // bounded as in `FixedSites<N>` above.
303 #[allow(clippy::cast_possible_truncation)]
304 const CYCLE_SIZE: u64 = 256u64.saturating_pow(N as u32);
305}
306
307// ---- Typed primitives (baseline per AGENTS.md § 11.4) ----
308//
309// Each typed primitive is a unit struct that impls `ConstrainedTypeShape`
310// with a stable IRI under `uor.foundation/prism/std_types/<TypeName>`,
311// `SITE_COUNT` set to its byte width when used at `WittLevel::W8`, and
312// empty `CONSTRAINTS`. Value-level invariants (IEEE 754 well-formedness,
313// `Bool ∈ {0, 1}`, UTF-32 codepoint validity) are host-side decisions
314// enforced by the application's `Grounding` impl per the family contract
315// laid out in this module's docs and in AGENTS.md § 11.
316
317macro_rules! typed_primitive {
318 (
319 $(#[$brief:meta])*
320 $name:ident, $iri:literal, $sites:literal
321 ) => {
322 $(#[$brief])*
323 ///
324 /// # See also
325 ///
326 /// - [`crate::std_types`] for the family contract and IRI namespace.
327 /// - [Wiki: 05 Building Block View § Whitebox `prism`](https://github.com/UOR-Foundation/UOR-Framework/wiki/05-Building-Block-View#whitebox-prism)
328 /// - [AGENTS.md § 11](../../../AGENTS.md#11-standard-type-library-policy)
329 ///
330 /// # Constraints
331 ///
332 /// - **TC-01** — admission is compile-time
333 /// - **TC-04** — bilateral compile-time enforcement
334 /// - **ADR-013** — closure under `uor-foundation`
335 /// - **ADR-017** — content-addressed identity via the IRI
336 ///
337 /// # Behavior
338 ///
339 /// ```rust
340 /// use prism::pipeline::ConstrainedTypeShape;
341 #[doc = concat!("use prism::std_types::", stringify!($name), ";")]
342 #[doc = concat!(
343 "assert_eq!(<", stringify!($name), " as ConstrainedTypeShape>::SITE_COUNT, ",
344 stringify!($sites), ");"
345 )]
346 #[doc = concat!(
347 "assert_eq!(<", stringify!($name), " as ConstrainedTypeShape>::IRI, \"", $iri, "\");"
348 )]
349 #[doc = concat!(
350 "assert!(<", stringify!($name), " as ConstrainedTypeShape>::CONSTRAINTS.is_empty());"
351 )]
352 /// ```
353 pub struct $name;
354
355 impl ConstrainedTypeShape for $name {
356 const IRI: &'static str = $iri;
357 const SITE_COUNT: usize = $sites;
358 const CONSTRAINTS: &'static [ConstraintRef] = &[];
359 // ADR-032: 256-per-site at W8 raised to SITE_COUNT, saturating.
360 const CYCLE_SIZE: u64 = 256u64.saturating_pow($sites as u32);
361 }
362 };
363}
364
365// Unsigned integers — byte-aligned widths from 8 to 256 bits.
366typed_primitive!(
367 /// Unsigned 8-bit integer (1 byte at `WittLevel::W8`).
368 U8, "https://uor.foundation/type/ConstrainedType", 1
369);
370typed_primitive!(
371 /// Unsigned 16-bit integer (2 bytes at `WittLevel::W8`).
372 U16, "https://uor.foundation/type/ConstrainedType", 2
373);
374typed_primitive!(
375 /// Unsigned 32-bit integer (4 bytes at `WittLevel::W8`).
376 /// Width of a Bitcoin block-header nonce.
377 U32, "https://uor.foundation/type/ConstrainedType", 4
378);
379typed_primitive!(
380 /// Unsigned 64-bit integer (8 bytes at `WittLevel::W8`).
381 U64, "https://uor.foundation/type/ConstrainedType", 8
382);
383typed_primitive!(
384 /// Unsigned 128-bit integer (16 bytes at `WittLevel::W8`).
385 U128, "https://uor.foundation/type/ConstrainedType", 16
386);
387typed_primitive!(
388 /// Unsigned 256-bit integer (32 bytes at `WittLevel::W8`).
389 /// Width of a SHA-256 output and a Bitcoin difficulty target.
390 U256, "https://uor.foundation/type/ConstrainedType", 32
391);
392
393// Signed integers — same byte widths, distinct IRIs to self-document
394// signed admission intent.
395typed_primitive!(
396 /// Signed 8-bit integer (1 byte at `WittLevel::W8`).
397 I8, "https://uor.foundation/type/ConstrainedType", 1
398);
399typed_primitive!(
400 /// Signed 16-bit integer (2 bytes at `WittLevel::W8`).
401 I16, "https://uor.foundation/type/ConstrainedType", 2
402);
403typed_primitive!(
404 /// Signed 32-bit integer (4 bytes at `WittLevel::W8`).
405 I32, "https://uor.foundation/type/ConstrainedType", 4
406);
407typed_primitive!(
408 /// Signed 64-bit integer (8 bytes at `WittLevel::W8`).
409 I64, "https://uor.foundation/type/ConstrainedType", 8
410);
411typed_primitive!(
412 /// Signed 128-bit integer (16 bytes at `WittLevel::W8`).
413 I128, "https://uor.foundation/type/ConstrainedType", 16
414);
415typed_primitive!(
416 /// Signed 256-bit integer (32 bytes at `WittLevel::W8`).
417 I256, "https://uor.foundation/type/ConstrainedType", 32
418);
419
420// IEEE 754 floating-point — IEEE well-formedness (NaN, subnormal
421// handling) is the application's `Grounding` impl's responsibility.
422typed_primitive!(
423 /// IEEE 754 binary32 floating-point (4 bytes at `WittLevel::W8`).
424 /// Well-formedness (NaN, subnormal, and infinity policy) is enforced
425 /// host-side by the application's `Grounding` impl.
426 F32, "https://uor.foundation/type/ConstrainedType", 4
427);
428typed_primitive!(
429 /// IEEE 754 binary64 floating-point (8 bytes at `WittLevel::W8`).
430 /// Well-formedness is enforced host-side.
431 F64, "https://uor.foundation/type/ConstrainedType", 8
432);
433
434// Boolean — value-in-{0, 1} contract is enforced host-side; the
435// distinct IRI separates `Bool` from `U8` at the content-address level.
436typed_primitive!(
437 /// Boolean (1 byte at `WittLevel::W8`). The value-in-{0, 1} contract
438 /// is enforced host-side by the application's `Grounding` impl;
439 /// the distinct IRI separates `Bool` from `U8` at the content-address
440 /// level.
441 Bool, "https://uor.foundation/type/ConstrainedType", 1
442);
443
444// Character — UTF-32 codepoint width; Unicode validity is host-side.
445typed_primitive!(
446 /// Unicode codepoint (4 bytes at `WittLevel::W8`, UTF-32 width).
447 /// Unicode validity (codepoint range, surrogate exclusion) is
448 /// enforced host-side by the application's `Grounding` impl.
449 Char, "https://uor.foundation/type/ConstrainedType", 4
450);