This commit is contained in:
parent
6b0d72c3a1
commit
2b7373698b
97
src/main.rs
97
src/main.rs
|
@ -47,7 +47,7 @@ impl Incrementable for AtomSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Lattice<I: Incrementable + Default, Ext = ()> {
|
struct Lattice<I: Incrementable + Default, Ext> {
|
||||||
ext: Ext,
|
ext: Ext,
|
||||||
order: Vec<I>,
|
order: Vec<I>,
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ trait LatticeAbstract {
|
||||||
fn is_lattice(&self) -> bool;
|
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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
for (i, atom) in self.order.iter().enumerate() {
|
for (i, atom) in self.order.iter().enumerate() {
|
||||||
let s = atom
|
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 {
|
fn increment(&mut self) -> IncrementResult {
|
||||||
for atom in &mut self.order {
|
for atom in &mut self.order {
|
||||||
if let Good = atom.increment() {
|
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
|
where
|
||||||
Self: LatticeAbstract,
|
Self: LatticeAbstract,
|
||||||
{
|
{
|
||||||
|
@ -140,29 +140,32 @@ where
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
fn monotone<Inc, Ext>(&self, other: &Lattice<Inc, Ext>) -> bool
|
fn monotone<IncB, ExtB>(&self, other: &Lattice<IncB, ExtB>) -> bool
|
||||||
where
|
where
|
||||||
Inc: Incrementable + Default,
|
IncB: Incrementable + Default,
|
||||||
Lattice<Inc, Ext>: LatticeAbstract,
|
Lattice<IncB, ExtB>: LatticeAbstract,
|
||||||
{
|
{
|
||||||
assert_eq!(self.order.len(), other.order.len());
|
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) {
|
if self.le(a, b) && !other.le(a, b) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
fn equal<Inc, Ext>(&self, other: &Lattice<Inc, Ext>) -> bool
|
fn equal<IncB, ExtB>(&self, other: &Lattice<IncB, ExtB>) -> bool
|
||||||
where
|
where
|
||||||
Inc: Incrementable + Default,
|
IncB: Incrementable + Default,
|
||||||
Lattice<Inc, Ext>: LatticeAbstract,
|
Lattice<IncB, ExtB>: LatticeAbstract,
|
||||||
{
|
{
|
||||||
self.monotone(other) && other.monotone(self)
|
self.monotone(other) && other.monotone(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Lattice<Atom> {
|
impl Lattice<Atom, ()> {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
fn is_transitive(&self) -> bool {
|
fn is_transitive(&self) -> bool {
|
||||||
for (i, j) in (0..self.order.len()).cartesian_product(0..self.order.len()) {
|
for (i, j) in (0..self.order.len()).cartesian_product(0..self.order.len()) {
|
||||||
if !self.le(i, j) && self.le_transitive(i, j) {
|
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 {
|
fn le(&self, a: usize, b: usize) -> bool {
|
||||||
if a == b {
|
if a == b {
|
||||||
return true;
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod lattice_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
@ -292,10 +286,9 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct Mapping<'a> {
|
struct Mapping<'a> {
|
||||||
domain: &'a Lattice<Atom>,
|
domain: &'a Lattice<Atom, ()>,
|
||||||
image: &'a Lattice<Atom>,
|
image: &'a Lattice<Atom, ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// function: Lattice<AtomSet>,
|
// function: Lattice<AtomSet>,
|
||||||
|
@ -303,7 +296,7 @@ struct Mapping<'a> {
|
||||||
type Operator<'a> = Lattice<AtomSet, Mapping<'a>>;
|
type Operator<'a> = Lattice<AtomSet, Mapping<'a>>;
|
||||||
|
|
||||||
impl<'a> Operator<'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 {
|
Lattice {
|
||||||
ext: Mapping { domain, image },
|
ext: Mapping { domain, image },
|
||||||
order: vec![AtomSet { included: vec![] }; image.order.len()],
|
order: vec![AtomSet { included: vec![] }; image.order.len()],
|
||||||
|
@ -344,6 +337,54 @@ impl<'a> LatticeAbstract for Operator<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_lattice(&self) -> bool {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue