Skip to main content

Semiring

Trait Semiring 

Source
pub trait Semiring: Clone + Eq {
    // Required methods
    fn zero() -> Self;
    fn one() -> Self;
    fn add(&self, rhs: &Self) -> Self;
    fn mul(&self, rhs: &Self) -> Self;

    // Provided methods
    fn is_zero(&self) -> bool { ... }
    fn is_one(&self) -> bool { ... }
}
Expand description

Semiring-like annotation domain for additive relation semantics.

This trait captures the smallest G4 foundation currently needed by the crate:

  • zero models absence or additive identity;
  • one models multiplicative identity;
  • add models union-like combination;
  • mul models composition-like chaining.

In later annotated relation layers, materializing exact support should keep tuples whose annotation is not zero and forget the annotation value itself.

§Examples

use relmath::annotated::{BooleanSemiring, Semiring};

fn reachable_via_two_hop_or_direct(
    direct: BooleanSemiring,
    first_leg: BooleanSemiring,
    second_leg: BooleanSemiring,
) -> BooleanSemiring {
    direct.add(&first_leg.mul(&second_leg))
}

let reachable = reachable_via_two_hop_or_direct(
    BooleanSemiring::FALSE,
    BooleanSemiring::TRUE,
    BooleanSemiring::TRUE,
);

assert!(reachable.is_one());
assert!(bool::from(reachable));

Required Methods§

Source

fn zero() -> Self

Returns the additive identity for the annotation domain.

Source

fn one() -> Self

Returns the multiplicative identity for the annotation domain.

Source

fn add(&self, rhs: &Self) -> Self

Combines two annotations with union-like or choice-like semantics.

Source

fn mul(&self, rhs: &Self) -> Self

Combines two annotations with chaining or composition-like semantics.

Provided Methods§

Source

fn is_zero(&self) -> bool

Returns true when this annotation is the additive identity.

Examples found in repository?
examples/semiring.rs (line 22)
13fn main() {
14    let direct = BooleanSemiring::FALSE;
15    let first_leg = BooleanSemiring::TRUE;
16    let second_leg = BooleanSemiring::TRUE;
17
18    let reachable = reachable_via_two_hop_or_direct(direct, first_leg, second_leg);
19
20    assert_eq!(reachable, BooleanSemiring::TRUE);
21    assert!(reachable.is_one());
22    assert!(!reachable.is_zero());
23    assert!(bool::from(reachable));
24    assert_eq!(BooleanSemiring::zero(), BooleanSemiring::FALSE);
25    assert_eq!(BooleanSemiring::one(), BooleanSemiring::TRUE);
26
27    println!(
28        "reachable after direct-or-two-hop combination: {}",
29        bool::from(reachable)
30    );
31}
Source

fn is_one(&self) -> bool

Returns true when this annotation is the multiplicative identity.

Examples found in repository?
examples/semiring.rs (line 21)
13fn main() {
14    let direct = BooleanSemiring::FALSE;
15    let first_leg = BooleanSemiring::TRUE;
16    let second_leg = BooleanSemiring::TRUE;
17
18    let reachable = reachable_via_two_hop_or_direct(direct, first_leg, second_leg);
19
20    assert_eq!(reachable, BooleanSemiring::TRUE);
21    assert!(reachable.is_one());
22    assert!(!reachable.is_zero());
23    assert!(bool::from(reachable));
24    assert_eq!(BooleanSemiring::zero(), BooleanSemiring::FALSE);
25    assert_eq!(BooleanSemiring::one(), BooleanSemiring::TRUE);
26
27    println!(
28        "reachable after direct-or-two-hop combination: {}",
29        bool::from(reachable)
30    );
31}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§