A Julia library for semirings.
This provides implementations for several important semirings as well as a small interface so that you can define your own.
Provided semirings:
- Real Semiring:
RealSemiringElement
-(R, +, *, 0, 1)
- Max Plus:
MaxPlusSemiringElement
-(R, max, +, -∞, 0)
- Min Plus:
MinPlusSemiringElement
-(R, min, +, ∞, 0)
- Boolean:
BooleanSemiringElement
-({False, True}, OR, AND, False, True)
Suppose we wish to define the MySemiring
semiring. First make the type:
MySemiringElement{T} <: AbstractSemiringElement{T}
Then define the following methods for your semiring:
val(x::MySemiringElement)
- Get the value of the element+(l::MySemiringElement, r::MySemiringElement)
- Addition for your semiring*(l::MySemiringElement, r::MySemiringElement)
- Multiplication for your semiringzero(::Type{MySemiringElement})
- The0
element in your semiringone(::Type{MySemiringElement})
- The1
element in your semiring
Implementing these gives access to all of the other operations that are present in all semirings as well as several helpful methods concering promotion, conversion, semiring type inspection, etc.
Assuming the eltype
of your semiring has the appropriate conversion and promotion rules, everything else should just work. This includes using your MySemiringElement
in vectors and matrices (although some additional methods must be defined in order to have access to all of the LinearAlgebra
functionality).
If your semiring has additional structure (a division, complete, star, etc. semiring), you can define a few more operations to access a variety of new functionality.
When applicable, one can implement:
-(x::MySemiringElement)
- additive inversion/(x::MySemiringElement, y::MySemiringElement)
- semiring divisionstar(x::MySemiringElement)
- semiring star operation
and the associated semiring functionality will then be defined automatically.
We use SimpleTraits to add additional dispatch capabilities for semirings. These traits encode structural properties of the semiring (commutative, idempotent, star, etc). We currently have support for:
- Commutative -
IsCommutative
/iscommutative
- semirings with commutative multiplication - Idempotent -
IsIdempotent
/isidempotent
- semirings with idempotent addition - Star -
IsStar
/isstar
- semirings with a defined star operator
If your semiring has one of these traits, you can connect them by defining (for example) isidempotent(::Type{<:MySemiringElement}) = true
. New traits can be defined by following the template in src/traits.jl
.
- Adding
SimpleTraits
to denote semiring properties (idempotent, nilpotent, complete, division, etc) - Better operator for
star
- Include
conj
in the docs or work around it forRealSemiringElements