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
142
143
144
145
146
147
148
149
150
151
152
use crate::{DeepReveal, Lattice, LatticeBimorphism};

/// Pair compound lattice.
///
/// `LatA` and `LatB` specify the nested lattice types.
///
/// When merging, both sub-lattices are always merged.
#[derive(Copy, Clone, Debug, Default, Eq, Lattice)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Pair<LatA, LatB> {
    /// The "left" Lattice of the Pair lattice.
    pub a: LatA,

    /// The "right" Lattice of the Pair lattice.
    pub b: LatB,
}

impl<LatA, LatB> Pair<LatA, LatB> {
    /// Create a `Pair` from the given values.
    pub fn new(a: LatA, b: LatB) -> Self {
        Self { a, b }
    }

    /// Create a `Pair` from the given values, using `Into`.
    pub fn new_from(a: impl Into<LatA>, b: impl Into<LatB>) -> Self {
        Self::new(a.into(), b.into())
    }

    /// Reveal the inner value as a shared reference.
    pub fn as_reveal_ref(&self) -> (&LatA, &LatB) {
        (&self.a, &self.b)
    }

    /// Reveal the inner value as an exclusive reference.
    pub fn as_reveal_mut(&mut self) -> (&mut LatA, &mut LatB) {
        (&mut self.a, &mut self.b)
    }

    /// Gets the inner by value, consuming self.
    pub fn into_reveal(self) -> (LatA, LatB) {
        (self.a, self.b)
    }
}

impl<LatA, LatB> DeepReveal for Pair<LatA, LatB>
where
    LatA: DeepReveal,
    LatB: DeepReveal,
{
    type Revealed = (LatA::Revealed, LatB::Revealed);

    fn deep_reveal(self) -> Self::Revealed {
        (self.a.deep_reveal(), self.b.deep_reveal())
    }
}

/// Bimorphism which pairs up the two input lattices.
#[derive(Default)]
pub struct PairBimorphism;
impl<LatA, LatB> LatticeBimorphism<LatA, LatB> for PairBimorphism {
    type Output = Pair<LatA, LatB>;

    fn call(&mut self, lat_a: LatA, lat_b: LatB) -> Self::Output {
        Pair::new(lat_a, lat_b)
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashSet;

    use super::*;
    use crate::set_union::{SetUnionBTreeSet, SetUnionHashSet, SetUnionSingletonSet};
    use crate::test::{check_all, check_lattice_bimorphism};
    use crate::{Merge, WithTop};

    #[test]
    fn consistency() {
        let mut test_vec = Vec::new();

        for a in [vec![], vec![0], vec![1], vec![0, 1]] {
            for b in [vec![], vec![0], vec![1], vec![0, 1]] {
                test_vec.push(Pair::new(
                    SetUnionHashSet::new_from(HashSet::from_iter(a.clone())),
                    SetUnionHashSet::new_from(HashSet::from_iter(b.clone())),
                ));
            }
        }

        check_all(&test_vec);
    }

    #[test]
    fn consistency_withtop() {
        let mut test_vec = vec![];

        let sub_items = &[
            Some(&[] as &[usize]),
            Some(&[0]),
            Some(&[1]),
            Some(&[0, 1]),
            None,
        ];

        for a in sub_items {
            for b in sub_items {
                test_vec.push(Pair::new(
                    WithTop::new(
                        a.map(|x| SetUnionHashSet::new_from(HashSet::from_iter(x.iter().cloned()))),
                    ),
                    WithTop::new(
                        b.map(|x| SetUnionHashSet::new_from(HashSet::from_iter(x.iter().cloned()))),
                    ),
                ));
            }
        }

        check_all(&test_vec);
    }

    #[test]
    fn test_merge_direction() {
        let src = Pair::new(
            SetUnionSingletonSet::new_from(5),
            SetUnionSingletonSet::new_from("hello"),
        );
        let mut dst = Pair::new(
            SetUnionHashSet::new_from([1, 2]),
            SetUnionBTreeSet::new_from(["world"]),
        );
        dst.merge(src);
    }

    #[test]
    fn test_pair_bimorphism() {
        let items_a = &[
            SetUnionHashSet::new_from([]),
            SetUnionHashSet::new_from([0]),
            SetUnionHashSet::new_from([1]),
            SetUnionHashSet::new_from([0, 1]),
        ];
        let items_b = &[
            SetUnionBTreeSet::new("hello".chars().collect()),
            SetUnionBTreeSet::new("world".chars().collect()),
        ];

        check_lattice_bimorphism(PairBimorphism, items_a, items_a);
        check_lattice_bimorphism(PairBimorphism, items_a, items_b);
        check_lattice_bimorphism(PairBimorphism, items_b, items_a);
        check_lattice_bimorphism(PairBimorphism, items_b, items_b);
    }
}