Skip to main content

Semiring

Trait Semiring 

Source
pub trait Semiring {
    // Required methods
    fn add(a: Self, b: Self) -> Self;
    fn zero() -> Self;
    fn multiply(a: Self, b: Self) -> Self;
    fn one() -> Self;
}
Expand description

A type class for types that form a semiring.

A semiring provides two binary operations (addition and multiplication) with their respective identity elements (zero and one).

§Laws

  • Commutative monoid under addition:
    • add(a, add(b, c)) = add(add(a, b), c) (associativity)
    • add(zero, a) = a and add(a, zero) = a (identity)
    • add(a, b) = add(b, a) (commutativity)
  • Monoid under multiplication:
    • multiply(a, multiply(b, c)) = multiply(multiply(a, b), c) (associativity)
    • multiply(one, a) = a and multiply(a, one) = a (identity)
  • Left distributivity: multiply(a, add(b, c)) = add(multiply(a, b), multiply(a, c))
  • Right distributivity: multiply(add(a, b), c) = add(multiply(a, c), multiply(b, c))
  • Annihilation: multiply(zero, a) = multiply(a, zero) = zero

Note: Integer types do not strictly satisfy these laws due to overflow.

§Examples

use fp_library::classes::Semiring;

// Distributivity: multiply(a, add(b, c)) = add(multiply(a, b), multiply(a, c))
let a = 2i32;
let b = 3i32;
let c = 4i32;
assert_eq!(
	i32::multiply(a, i32::add(b, c)),
	i32::add(i32::multiply(a, b), i32::multiply(a, c)),
);

Required Methods§

Source

fn add(a: Self, b: Self) -> Self

Adds two values.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum of the two values.

§Examples
use fp_library::classes::Semiring;

assert_eq!(i32::add(2, 3), 5);
Source

fn zero() -> Self

Returns the additive identity element.

§Type Signature

() -> Self

§Returns

The additive identity (zero).

§Examples
use fp_library::classes::Semiring;

assert_eq!(i32::zero(), 0);
Source

fn multiply(a: Self, b: Self) -> Self

Multiplies two values.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product of the two values.

§Examples
use fp_library::classes::Semiring;

assert_eq!(i32::multiply(2, 3), 6);
Source

fn one() -> Self

Returns the multiplicative identity element.

§Type Signature

() -> Self

§Returns

The multiplicative identity (one).

§Examples
use fp_library::classes::Semiring;

assert_eq!(i32::one(), 1);

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.

Implementations on Foreign Types§

Source§

impl Semiring for f32

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using the + operator.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f32>::add(2.0 as f32, 3.0 as f32), 5.0 as f32);
Source§

fn zero() -> Self

Returns the additive identity (0.0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f32>::zero(), 0.0 as f32);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using the * operator.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f32>::multiply(2.0 as f32, 3.0 as f32), 6.0 as f32);
Source§

fn one() -> Self

Returns the multiplicative identity (1.0).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f32>::one(), 1.0 as f32);
Source§

impl Semiring for f64

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using the + operator.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f64>::add(2.0 as f64, 3.0 as f64), 5.0 as f64);
Source§

fn zero() -> Self

Returns the additive identity (0.0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f64>::zero(), 0.0 as f64);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using the * operator.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f64>::multiply(2.0 as f64, 3.0 as f64), 6.0 as f64);
Source§

fn one() -> Self

Returns the multiplicative identity (1.0).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<f64>::one(), 1.0 as f64);
Source§

impl Semiring for i8

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i8>::add(2 as i8, 3 as i8), 5 as i8);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i8>::zero(), 0 as i8);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i8>::multiply(2 as i8, 3 as i8), 6 as i8);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i8>::one(), 1 as i8);
Source§

impl Semiring for i16

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i16>::add(2 as i16, 3 as i16), 5 as i16);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i16>::zero(), 0 as i16);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i16>::multiply(2 as i16, 3 as i16), 6 as i16);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i16>::one(), 1 as i16);
Source§

impl Semiring for i32

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i32>::add(2 as i32, 3 as i32), 5 as i32);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i32>::zero(), 0 as i32);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i32>::multiply(2 as i32, 3 as i32), 6 as i32);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i32>::one(), 1 as i32);
Source§

impl Semiring for i64

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i64>::add(2 as i64, 3 as i64), 5 as i64);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i64>::zero(), 0 as i64);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i64>::multiply(2 as i64, 3 as i64), 6 as i64);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i64>::one(), 1 as i64);
Source§

impl Semiring for i128

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i128>::add(2 as i128, 3 as i128), 5 as i128);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i128>::zero(), 0 as i128);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i128>::multiply(2 as i128, 3 as i128), 6 as i128);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<i128>::one(), 1 as i128);
Source§

impl Semiring for isize

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<isize>::add(2 as isize, 3 as isize), 5 as isize);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<isize>::zero(), 0 as isize);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<isize>::multiply(2 as isize, 3 as isize), 6 as isize);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<isize>::one(), 1 as isize);
Source§

impl Semiring for u8

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u8>::add(2 as u8, 3 as u8), 5 as u8);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u8>::zero(), 0 as u8);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u8>::multiply(2 as u8, 3 as u8), 6 as u8);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u8>::one(), 1 as u8);
Source§

impl Semiring for u16

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u16>::add(2 as u16, 3 as u16), 5 as u16);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u16>::zero(), 0 as u16);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u16>::multiply(2 as u16, 3 as u16), 6 as u16);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u16>::one(), 1 as u16);
Source§

impl Semiring for u32

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u32>::add(2 as u32, 3 as u32), 5 as u32);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u32>::zero(), 0 as u32);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u32>::multiply(2 as u32, 3 as u32), 6 as u32);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u32>::one(), 1 as u32);
Source§

impl Semiring for u64

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u64>::add(2 as u64, 3 as u64), 5 as u64);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u64>::zero(), 0 as u64);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u64>::multiply(2 as u64, 3 as u64), 6 as u64);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u64>::one(), 1 as u64);
Source§

impl Semiring for u128

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u128>::add(2 as u128, 3 as u128), 5 as u128);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u128>::zero(), 0 as u128);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u128>::multiply(2 as u128, 3 as u128), 6 as u128);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<u128>::one(), 1 as u128);
Source§

impl Semiring for usize

Source§

fn add(a: Self, b: Self) -> Self

Adds two values using wrapping addition.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The sum (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<usize>::add(2 as usize, 3 as usize), 5 as usize);
Source§

fn zero() -> Self

Returns the additive identity (0).

§Type Signature

() -> Self

§Returns

Zero.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<usize>::zero(), 0 as usize);
Source§

fn multiply(a: Self, b: Self) -> Self

Multiplies two values using wrapping multiplication.

§Type Signature

(Self, Self) -> Self

§Parameters
  • a: The first value.
  • b: The second value.
§Returns

The product (wrapping on overflow).

§Examples
use fp_library::classes::Semiring;

assert_eq!(<usize>::multiply(2 as usize, 3 as usize), 6 as usize);
Source§

fn one() -> Self

Returns the multiplicative identity (1).

§Type Signature

() -> Self

§Returns

One.

§Examples
use fp_library::classes::Semiring;

assert_eq!(<usize>::one(), 1 as usize);

Implementors§