Last active
August 29, 2015 14:24
-
-
Save fthomas/5d13d36e8c6ff64685df to your computer and use it in GitHub Desktop.
Absorbing Semigroup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Semigroup[A] { | |
def op(a1: A, a2: A): A // op is associative | |
} | |
trait Monoid[A] extends Semigroup[A] { | |
def id: A // id is the identity of op: op(a, id) == op(id, a) == a | |
} | |
trait SemigroupZero[A] extends Semigroup[A] { | |
def zero: A // zero is the absorbing element of op: op(a, zero) == op(zero, a) == zero | |
// https://en.wikipedia.org/wiki/Absorbing_element | |
} | |
new Monoid[Boolean] { def id = true; def op(a1: Boolean, a2: Boolean) = a1 && a2 } // true, && | |
new SemigroupZero[Boolean] { def zero = false; def op(a1: Boolean, a2: Boolean) = a1 && a2 } // false, && | |
new Monoid[Boolean] { def id = false; def op(a1: Boolean, a2: Boolean) = a1 || a2 } // false, || | |
new SemigroupZero[Boolean] { def zero = true; def op(a1: Boolean, a2: Boolean) = a1 || a2 } // true, || |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment