relmath-rs 0.7.0

Relation-first mathematics and scientific computing in Rust.
Documentation
//! Shared exact-support capability boundaries across add-on relation surfaces.

use relmath::{
    BinaryRelation, ExactSupport, ToExactBinaryRelation,
    annotated::{AnnotatedRelation, BooleanSemiring},
    provenance::ProvenanceRelation,
    temporal::{Interval, ValidTimeRelation},
};

fn exact_pairs<R>(relation: &R) -> BinaryRelation<&'static str, &'static str>
where
    R: ExactSupport<(&'static str, &'static str)>
        + ToExactBinaryRelation<&'static str, &'static str>,
{
    assert_eq!(
        relation.exact_support().to_vec(),
        relation.to_binary_relation().to_vec()
    );
    relation.to_binary_relation()
}

fn main() {
    let evidence = ProvenanceRelation::from_facts([
        (("alice", "review"), "directory"),
        (("bob", "approve"), "policy"),
    ]);
    let permissions = AnnotatedRelation::from_facts([
        (("alice", "review"), BooleanSemiring::TRUE),
        (("bob", "approve"), BooleanSemiring::TRUE),
    ]);
    let schedule = ValidTimeRelation::from_facts([
        (
            ("alice", "review"),
            Interval::new(1, 3).expect("expected valid interval"),
        ),
        (
            ("bob", "approve"),
            Interval::new(2, 4).expect("expected valid interval"),
        ),
    ]);

    let exact_evidence = exact_pairs(&evidence);
    let exact_permissions = exact_pairs(&permissions);
    let exact_schedule = exact_pairs(&schedule);

    assert_eq!(
        exact_evidence.to_vec(),
        vec![("alice", "review"), ("bob", "approve")]
    );
    assert_eq!(exact_permissions.to_vec(), exact_evidence.to_vec());
    assert_eq!(exact_schedule.to_vec(), exact_evidence.to_vec());

    assert_eq!(
        evidence
            .why(&("alice", "review"))
            .expect("expected witness")
            .to_vec(),
        vec!["directory"]
    );
    assert_eq!(
        permissions.annotation_of(&("alice", "review")),
        Some(&BooleanSemiring::TRUE)
    );
    assert_eq!(
        schedule
            .valid_time_of(&("alice", "review"))
            .expect("expected interval support")
            .to_vec(),
        vec![Interval::new(1, 3).expect("expected valid interval")]
    );

    println!(
        "witness={:?}, annotation={:?}, interval_support={:?}, exact_support={:?}",
        evidence
            .why(&("alice", "review"))
            .expect("expected witness")
            .to_vec(),
        permissions.annotation_of(&("alice", "review")),
        schedule
            .valid_time_of(&("alice", "review"))
            .expect("expected interval support")
            .to_vec(),
        exact_schedule.to_vec()
    );
}