arrow-typeclasses / arrow.typeclasses / Semiring

Semiring

interface Semiring<A> : Monoid<A>

The Semiring type class for a given type A extends the Monoid type class by adding a combineMultiplicate and an one function. combineMultiplicate also takes two values and returns a value of type A and guarantees to be associative:

(a.combineMultiplicate(b)).combineMultiplicate(c) == a.combineMultiplicate(b.combineMultiplicate(c))

The one value serves exactly like the empty function for an additive Monoid, just adapted for the multiplicative version. This forms the following law:

combineMultiplicate(x, one) == combineMultiplicate(one, x) == x

Please note that the empty function has been renamed to zero to get a consistent naming style inside the semiring.

Currently, Semiring instances are defined for all available number types.

Examples

Here a some examples:


import arrow.core.extensions.*

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Int.semiring().run { 1.combine(2) }
  //sampleEnd
  println(result)
}

import arrow.core.extensions.*

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Int.semiring().run { 2.combineMultiplicate(3) }
  //sampleEnd
  println(result)
}

import arrow.core.Option
import arrow.core.extensions.*
import arrow.core.extensions.option.semiring.semiring

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Option.semiring(Int.semiring()).run {
     Option(1).combine(Option(2))
  }
  //sampleEnd
  println(result)
}

import arrow.core.Option
import arrow.core.extensions.*
import arrow.core.extensions.option.semiring.semiring

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Option.semiring(Int.semiring()).run {
     Option(2).combineMultiplicate(Option(3))
  }
  //sampleEnd
  println(result)
}

import arrow.core.Option
import arrow.core.None
import arrow.core.extensions.*
import arrow.core.extensions.option.semiring.semiring

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Option.semiring(Int.semiring()).run {
     Option(1).combine(None)
  }
  //sampleEnd
  println(result)
}

The type class Semiring also has support for the + * syntax:


import arrow.core.Option
import arrow.core.extensions.*
import arrow.core.extensions.option.semiring.semiring

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Option.semiring(Int.semiring()).run {
     Option(1) + Option(2)
  }
  //sampleEnd
  println(result)
}

import arrow.core.Option
import arrow.core.extensions.*
import arrow.core.extensions.option.semiring.semiring

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Option.semiring(Int.semiring()).run {
     Option(2) * Option(3)
  }
  //sampleEnd
  println(result)
}

Functions

combineMultiplicate abstract fun A.combineMultiplicate(b: A): A
Multiplicatively combine two A values.
empty open fun empty(): A
A zero value for this A
maybeCombineAddition open fun A?.maybeCombineAddition(b: A?): A
Maybe additively combine two A values.
maybeCombineMultiplicate open fun A?.maybeCombineMultiplicate(b: A?): A
Maybe multiplicatively combine two A values.
one abstract fun one(): A
A one value for this A
times open operator fun A.times(b: A): A
zero abstract fun zero(): A
A zero value for this A

Inherited Functions

combineAll open fun Collection<A>.combineAll(): A
Combine an Collection of A values.open fun combineAll(elems: List<A>): A
Combine an array of A values.

Extension Functions

const fun <A> A.const(): Const<A, Nothing>
fix fun <A> MonoidOf<A>.fix(): Monoid<A>
nest fun <F, G, A> UnnestedType<F, G, A>.nest(): NestedType<F, G, A>

Type Class Hierarchy