algebraeon_sets/structure/
structure.rs

1use std::{borrow::Borrow, fmt::Debug};
2
3pub trait Structure: Clone + Debug + PartialEq + Eq {}
4
5/// Instances of a type implementing this trait represent
6/// a set of elements of type `Self::Set` with some
7/// structure, for example, the structure of a ring.
8pub trait SetStructure: Structure {
9    type Set: Clone + Debug;
10
11    /// Some instances of Self::Set may not be valid to represent elements of this set.
12    /// Return `true` if `x` is a valid element and `false` if not.
13    fn is_element(&self, x: &Self::Set) -> bool;
14}
15
16pub trait MetaType: Clone + Debug {
17    type Structure: SetStructure<Set = Self>;
18    fn structure() -> Self::Structure;
19}
20
21pub fn common_structure<S: SetStructure>(
22    structure1: impl Borrow<S>,
23    structure2: impl Borrow<S>,
24) -> S {
25    if structure1.borrow() == structure2.borrow() {
26        structure1.borrow().clone()
27    } else {
28        panic!("Unequal ring structures")
29    }
30}
31
32pub trait ToStringStructure: SetStructure {
33    fn to_string(&self, elem: &Self::Set) -> String;
34}
35
36pub trait EqStructure: SetStructure {
37    fn equal(&self, a: &Self::Set, b: &Self::Set) -> bool;
38}
39
40pub trait CountableSetStructure: SetStructure {
41    /// Yield distinct elements of the set such that every element eventually appears.
42    /// Always yields elements in the same order.
43    fn generate_all_elements(&self) -> impl Iterator<Item = Self::Set>;
44}
45
46pub trait FiniteSetStructure: CountableSetStructure {
47    /// A list of all elements in the set.
48    /// Always returns elements in the same order.
49    fn list_all_elements(&self) -> Vec<Self::Set> {
50        self.generate_all_elements().collect()
51    }
52    fn size(&self) -> usize {
53        self.list_all_elements().len()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use algebraeon_macros::CanonicalStructure;
61
62    #[test]
63    fn canonical_structure() {
64        #[derive(Debug, Clone, PartialEq, Eq, CanonicalStructure)]
65        pub struct A {
66            x: i32,
67        }
68
69        impl ToString for A {
70            fn to_string(&self) -> String {
71                self.x.to_string()
72            }
73        }
74
75        impl ToStringStructure for ACanonicalStructure {
76            fn to_string(&self, elem: &Self::Set) -> String {
77                elem.to_string()
78            }
79        }
80
81        let a = A { x: 3 };
82        let b = A { x: 4 };
83        let v = A::structure().equal(&a, &b);
84        assert_eq!(v, false);
85        println!("{}", A::structure().to_string(&a));
86    }
87
88    #[test]
89    fn foo() {
90        #[derive(Debug, Clone, PartialEq, Eq)]
91        struct A {
92            t: usize,
93        }
94
95        impl Structure for A {}
96
97        impl SetStructure for A {
98            type Set = usize;
99
100            fn is_element(&self, _x: &Self::Set) -> bool {
101                true
102            }
103        }
104
105        impl ToStringStructure for A {
106            fn to_string(&self, elem: &Self::Set) -> String {
107                elem.to_string()
108            }
109        }
110    }
111}