diff --git a/src/main.rs b/src/main.rs index 7772fd0..3589ed4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ impl Incrementable for AtomSet { } #[derive(Debug, Default)] -struct Lattice { +struct Lattice { ext: Ext, order: Vec, } @@ -58,7 +58,7 @@ trait LatticeAbstract { fn is_lattice(&self) -> bool; } -impl Display for Lattice { +impl Display for Lattice { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (i, atom) in self.order.iter().enumerate() { let s = atom @@ -72,7 +72,7 @@ impl Display for Lattice { } } -impl Incrementable for Lattice { +impl Incrementable for Lattice { fn increment(&mut self) -> IncrementResult { for atom in &mut self.order { if let Good = atom.increment() { @@ -83,7 +83,7 @@ impl Incrementable for Lattice { } } -impl Lattice +impl Lattice where Self: LatticeAbstract, { @@ -140,29 +140,32 @@ where } return true; } - fn monotone(&self, other: &Lattice) -> bool + fn monotone(&self, other: &Lattice) -> bool where - Inc: Incrementable + Default, - Lattice: LatticeAbstract, + IncB: Incrementable + Default, + Lattice: LatticeAbstract, { assert_eq!(self.order.len(), other.order.len()); - for (a, b) in (0..self.order.len()).cartesian_product((0..other.order.len())) { + for (a, b) in (0..self.order.len()).cartesian_product(0..other.order.len()) { if self.le(a, b) && !other.le(a, b) { return false; } } return true; } - fn equal(&self, other: &Lattice) -> bool + fn equal(&self, other: &Lattice) -> bool where - Inc: Incrementable + Default, - Lattice: LatticeAbstract, + IncB: Incrementable + Default, + Lattice: LatticeAbstract, { self.monotone(other) && other.monotone(self) } } -impl Lattice { +impl Lattice { + fn new() -> Self { + Self::default() + } fn is_transitive(&self) -> bool { for (i, j) in (0..self.order.len()).cartesian_product(0..self.order.len()) { if !self.le(i, j) && self.le_transitive(i, j) { @@ -185,7 +188,7 @@ impl Lattice { } } -impl LatticeAbstract for Lattice { +impl LatticeAbstract for Lattice { fn le(&self, a: usize, b: usize) -> bool { if a == b { return true; @@ -212,17 +215,8 @@ impl LatticeAbstract for Lattice { } } -fn main() { - let mut lattice = Lattice::default(); - for (i, _) in repeat(0).enumerate() { - print!("{}", lattice); - println!("iteration {}", i); - lattice.next_lattice(); - } -} - #[cfg(test)] -mod tests { +mod lattice_tests { use super::*; use std::error::Error; @@ -292,10 +286,9 @@ mod tests { } } - struct Mapping<'a> { - domain: &'a Lattice, - image: &'a Lattice, + domain: &'a Lattice, + image: &'a Lattice, } // function: Lattice, @@ -303,7 +296,7 @@ struct Mapping<'a> { type Operator<'a> = Lattice>; impl<'a> Operator<'a> { - fn new(domain: &'a Lattice, image: &'a Lattice) -> Operator<'a> { + fn new(domain: &'a Lattice, image: &'a Lattice) -> Operator<'a> { Lattice { ext: Mapping { domain, image }, order: vec![AtomSet { included: vec![] }; image.order.len()], @@ -344,6 +337,54 @@ impl<'a> LatticeAbstract for Operator<'a> { } fn is_lattice(&self) -> bool { - self.all_nonempty() && self.ext.image.equal() + self.all_nonempty() && self.ext.image.equal(self) + } +} + +impl<'a> Display for Operator<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for (i, atom) in self.order.iter().enumerate() { + let s = atom + .included + .iter() + .map(|&b| if b { "1 " } else { "0 " }) + .collect::(); + writeln!(f, "{}: {}", i, s)?; + } + Ok(()) + } +} + +#[cfg(test)] +mod operator_tests { + use super::*; + use std::error::Error; + + #[test] + fn test_operator() { + } +} + +fn main() { + let mut domain = Lattice::::new(); + let image = Lattice { + ext: (), + order: vec![ + Atom { smaller: vec![] }, + Atom { + smaller: vec![true], + }, + Atom { + smaller: vec![true, false], + }, + ], + }; + loop { + domain.next_lattice(); + println!("{}", domain); + let mut operator = Operator::new(&domain, &image); + while let Good = operator.next_lattice() { + println!("{}", operator); + } } }