relmath/traits/relation_view.rs
1//! Shared read-only relation views for the deterministic exact core.
2//!
3//! This module is intentionally narrow. It gives generic code a deterministic
4//! read-only boundary over the shipped exact unary, binary, and n-ary
5//! relations without flattening their differences in tuple shape. When code
6//! needs to forget provenance, annotation, or temporal support first, pair
7//! this module with [`crate::ExactSupport`] or the `ToExact*Relation` traits.
8
9use crate::core::{BinaryRelation, NaryRelation, UnaryRelation};
10
11use super::FiniteRelation;
12
13/// A shared read-only view over a finite exact relation.
14///
15/// `RelationView` is the first G6 shared convergence boundary. It lets generic
16/// code inspect tuple count and iterate over stored tuples in deterministic
17/// order across the exact unary, binary, and n-ary relation types.
18///
19/// This first layer intentionally does not cover mutation, membership queries,
20/// domain/range inspection, schema access, support materialization,
21/// provenance, annotations, temporal support, or backend abstraction. Exact
22/// support materialization now lives in the separate G6 capability traits such
23/// as [`crate::ExactSupport`] and the `ToExact*Relation` family.
24///
25/// Iteration order follows the deterministic storage order documented by the
26/// implementing relation type. Tuple shapes remain concrete:
27///
28/// - unary relations yield `&T`;
29/// - binary relations yield `&(A, B)`;
30/// - n-ary relations yield `&[T]`.
31///
32/// # Examples
33///
34/// ```rust
35/// use relmath::{FiniteRelation, RelationView, UnaryRelation};
36///
37/// let values = UnaryRelation::from_values([3, 1, 2]);
38///
39/// assert_eq!(values.len(), 3);
40/// assert_eq!(values.tuples().copied().collect::<Vec<_>>(), vec![1, 2, 3]);
41/// ```
42pub trait RelationView: FiniteRelation {
43 /// Borrowed tuple item yielded by [`Self::tuples`].
44 type Tuple<'a>
45 where
46 Self: 'a;
47
48 /// Returns the stored tuples in deterministic order.
49 fn tuples(&self) -> impl Iterator<Item = Self::Tuple<'_>>;
50}
51
52impl<T: Ord> RelationView for UnaryRelation<T> {
53 type Tuple<'a>
54 = &'a T
55 where
56 Self: 'a;
57
58 fn tuples(&self) -> impl Iterator<Item = Self::Tuple<'_>> {
59 UnaryRelation::iter(self)
60 }
61}
62
63impl<A: Ord, B: Ord> RelationView for BinaryRelation<A, B> {
64 type Tuple<'a>
65 = &'a (A, B)
66 where
67 Self: 'a;
68
69 fn tuples(&self) -> impl Iterator<Item = Self::Tuple<'_>> {
70 BinaryRelation::iter(self)
71 }
72}
73
74impl<T: Ord> RelationView for NaryRelation<T> {
75 type Tuple<'a>
76 = &'a [T]
77 where
78 Self: 'a;
79
80 fn tuples(&self) -> impl Iterator<Item = Self::Tuple<'_>> {
81 NaryRelation::iter(self)
82 }
83}