algebraeon_sets/
structure.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::{borrow::Borrow, fmt::Debug, marker::PhantomData, rc::Rc};

use malachite_nz::{integer::Integer, natural::Natural};
use malachite_q::Rational;

pub trait Structure: Clone + Debug + PartialEq + Eq {
    type Set: Clone + Debug;
}

#[derive(Debug, Clone)]
pub struct CannonicalStructure<T: MetaType> {
    _ghost: std::marker::PhantomData<T>,
}
impl<T: MetaType> PartialEq for CannonicalStructure<T> {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}
impl<T: MetaType> Eq for CannonicalStructure<T> {}
pub trait MetaType: Clone + Debug {
    type Structure: Structure<Set = Self>;
    fn structure() -> Rc<Self::Structure>;
}

impl<T: MetaType> CannonicalStructure<T> {
    pub fn new() -> Self {
        Self {
            _ghost: PhantomData::default(),
        }
    }
}
impl<T: MetaType> Structure for CannonicalStructure<T> {
    type Set = T;
}

pub fn common_structure<S: Structure>(
    structure1: impl Borrow<Rc<S>>,
    structure2: impl Borrow<Rc<S>>,
) -> Rc<S> {
    if structure1.borrow() == structure2.borrow() {
        structure1.borrow().clone()
    } else {
        panic!("Unequal ring structures")
    }
}

pub trait ToStringStructure: Structure {
    fn to_string(&self, elem: &Self::Set) -> String;
}
impl<T: MetaType + ToString> ToStringStructure for CannonicalStructure<T> {
    fn to_string(&self, elem: &Self::Set) -> String {
        elem.to_string()
    }
}

pub trait PartialEqStructure: Structure {
    fn equal(&self, a: &Self::Set, b: &Self::Set) -> bool;
}
impl<T: MetaType + PartialEq> PartialEqStructure for CannonicalStructure<T> {
    fn equal(&self, a: &T, b: &T) -> bool {
        a == b
    }
}

pub trait EqStructure: PartialEqStructure {}
impl<T: MetaType + Eq> EqStructure for CannonicalStructure<T> {}

impl MetaType for Natural {
    type Structure = CannonicalStructure<Natural>;

    fn structure() -> Rc<Self::Structure> {
        CannonicalStructure::new().into()
    }
}

impl MetaType for Integer {
    type Structure = CannonicalStructure<Integer>;

    fn structure() -> Rc<Self::Structure> {
        CannonicalStructure::new().into()
    }
}

impl MetaType for Rational {
    type Structure = CannonicalStructure<Rational>;

    fn structure() -> Rc<Self::Structure> {
        CannonicalStructure::new().into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cannonical_structure() {
        #[derive(Debug, Clone, PartialEq, Eq)]
        struct A {
            x: i32,
        }

        impl MetaType for A {
            type Structure = CannonicalStructure<A>;

            fn structure() -> Rc<Self::Structure> {
                CannonicalStructure::new().into()
            }
        }

        impl ToString for A {
            fn to_string(&self) -> String {
                self.x.to_string()
            }
        }

        let a = A { x: 3 };
        let b = A { x: 4 };
        let v = A::structure().equal(&a, &b);
        assert_eq!(v, false);
        println!("{}", A::structure().to_string(&a));
    }

    #[test]
    fn foo() {
        #[derive(Debug, Clone, PartialEq, Eq)]
        struct Scary {
            t: usize,
        }

        impl Structure for Scary {
            type Set = usize;
        }

        impl ToStringStructure for Scary {
            fn to_string(&self, elem: &Self::Set) -> String {
                elem.to_string()
            }
        }
    }
}