Semiring

public protocol Semiring : Monoid

A semiring is an algebraic structure that has the same properties as a commutative monoid for addition, with multiplication.

  • An associative operation to combine values of the implementing type.

    This operation must satisfy the semigroup laws:

    a.multiply(b).multiply(c) == a.multiply(b.multiply(c))
    

    Declaration

    Swift

    func multiply(_ other: Self) -> Self

    Parameters

    other

    Value to multipy with the receiver.

    Return Value

    Multiplication of the receiver value with the parameter value.

  • zero() Default implementation

    Zero element.

    The zero element must obey the semirings laws:

    a.multiply(zero()) == zero().multiply(a) == zero()
    

    That is, multiplying any element with zero must return the zero. It is also an alias for empty of Monoid.

    Default Implementation

    Declaration

    Swift

    static func zero() -> Self

    Return Value

    A value of the implementing type satisfying the semiring laws.

  • One element.

    The one element must obey the semirings laws:

    a.multiply(one()) == one().multiply(a) == a
    

    That is, multiplying any element with one must return the original element.

    Declaration

    Swift

    static func one() -> Self

    Return Value

    A value of the implementing type satisfying the semiring laws.