algebraeon_sets/structure/
structure.rs1use std::{borrow::Borrow, fmt::Debug};
2
3pub trait Structure: Clone + Debug + PartialEq + Eq {}
4
5pub trait SetStructure: Structure {
9 type Set: Clone + Debug;
10
11 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 fn generate_all_elements(&self) -> impl Iterator<Item = Self::Set>;
44}
45
46pub trait FiniteSetStructure: CountableSetStructure {
47 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}