This commit is contained in:
Spencer Killen 2023-12-21 16:03:44 -07:00
parent 6b0d72c3a1
commit 2b7373698b
Signed by: sjkillen
GPG Key ID: 3AF3117BA6FBB75B
1 changed files with 69 additions and 28 deletions

View File

@ -47,7 +47,7 @@ impl Incrementable for AtomSet {
}
#[derive(Debug, Default)]
struct Lattice<I: Incrementable + Default, Ext = ()> {
struct Lattice<I: Incrementable + Default, Ext> {
ext: Ext,
order: Vec<I>,
}
@ -58,7 +58,7 @@ trait LatticeAbstract {
fn is_lattice(&self) -> bool;
}
impl Display for Lattice<Atom> {
impl Display for Lattice<Atom, ()> {
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<Atom> {
}
}
impl<I: Incrementable + Default> Incrementable for Lattice<I> {
impl<I: Incrementable + Default, Ext> Incrementable for Lattice<I, Ext> {
fn increment(&mut self) -> IncrementResult {
for atom in &mut self.order {
if let Good = atom.increment() {
@ -83,7 +83,7 @@ impl<I: Incrementable + Default> Incrementable for Lattice<I> {
}
}
impl<I: Incrementable + Default> Lattice<I>
impl<I: Incrementable + Default, Ext> Lattice<I, Ext>
where
Self: LatticeAbstract,
{
@ -140,29 +140,32 @@ where
}
return true;
}
fn monotone<Inc, Ext>(&self, other: &Lattice<Inc, Ext>) -> bool
fn monotone<IncB, ExtB>(&self, other: &Lattice<IncB, ExtB>) -> bool
where
Inc: Incrementable + Default,
Lattice<Inc, Ext>: LatticeAbstract,
IncB: Incrementable + Default,
Lattice<IncB, ExtB>: 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<Inc, Ext>(&self, other: &Lattice<Inc, Ext>) -> bool
fn equal<IncB, ExtB>(&self, other: &Lattice<IncB, ExtB>) -> bool
where
Inc: Incrementable + Default,
Lattice<Inc, Ext>: LatticeAbstract,
IncB: Incrementable + Default,
Lattice<IncB, ExtB>: LatticeAbstract,
{
self.monotone(other) && other.monotone(self)
}
}
impl Lattice<Atom> {
impl Lattice<Atom, ()> {
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<Atom> {
}
}
impl LatticeAbstract for Lattice<Atom> {
impl LatticeAbstract for Lattice<Atom, ()> {
fn le(&self, a: usize, b: usize) -> bool {
if a == b {
return true;
@ -212,17 +215,8 @@ impl LatticeAbstract for Lattice<Atom> {
}
}
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<Atom>,
image: &'a Lattice<Atom>,
domain: &'a Lattice<Atom, ()>,
image: &'a Lattice<Atom, ()>,
}
// function: Lattice<AtomSet>,
@ -303,7 +296,7 @@ struct Mapping<'a> {
type Operator<'a> = Lattice<AtomSet, Mapping<'a>>;
impl<'a> Operator<'a> {
fn new(domain: &'a Lattice<Atom>, image: &'a Lattice<Atom>) -> Operator<'a> {
fn new(domain: &'a Lattice<Atom, ()>, image: &'a Lattice<Atom, ()>) -> 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::<String>();
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::<Atom, ()>::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);
}
}
}