|
| 1 | +package algebra.ring |
| 2 | + |
| 3 | +import algebra.{CommutativeMonoid, Eq, Order} |
| 4 | + |
| 5 | +import scala.{specialized => sp} |
| 6 | + |
| 7 | +/** |
| 8 | + * A trait that expresses the existence of signs and absolute values on linearly ordered additive commutative monoids |
| 9 | + * (i.e. types with addition and a zero). |
| 10 | + * |
| 11 | + * The following laws holds: |
| 12 | + * |
| 13 | + * (1) if `a <= b` then `a + c <= b + c` (linear order), |
| 14 | + * (2) `signum(x) = -1` if `x < 0`, `signum(x) = 1` if `x > 0`, `signum(x) = 0` otherwise, |
| 15 | + * |
| 16 | + * Negative elements only appear when the scalar is taken from a additive abelian group. Then: |
| 17 | + * |
| 18 | + * (3) `abs(x) = -x` if `x < 0`, or `x` otherwise, |
| 19 | + * |
| 20 | + * Laws (1) and (2) lead to the triange inequality: |
| 21 | + * |
| 22 | + * (4) `abs(a + b) <= abs(a) + abs(b)` |
| 23 | + * |
| 24 | + * Signed should never be extended in implementations, rather the [[Signed.forAdditiveCommutativeMonoid]] and |
| 25 | + * [[Signed.forAdditiveCommutativeGroup subtraits]]. |
| 26 | + * |
| 27 | + * It's better to have the Signed hierarchy separate from the Ring/Order hierarchy, so that |
| 28 | + * we do not end up with duplicate implicits. |
| 29 | + */ |
| 30 | +trait Signed[@sp(Byte, Short, Int, Long, Float, Double) A] extends Any { |
| 31 | + |
| 32 | + def additiveCommutativeMonoid: AdditiveCommutativeMonoid[A] |
| 33 | + def order: Order[A] |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns Zero if `a` is 0, Positive if `a` is positive, and Negative is `a` is negative. |
| 37 | + */ |
| 38 | + def sign(a: A): Signed.Sign = Signed.Sign(signum(a)) |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns 0 if `a` is 0, 1 if `a` is positive, and -1 is `a` is negative. |
| 42 | + */ |
| 43 | + def signum(a: A): Int |
| 44 | + |
| 45 | + /** |
| 46 | + * An idempotent function that ensures an object has a non-negative sign. |
| 47 | + */ |
| 48 | + def abs(a: A): A |
| 49 | + |
| 50 | + def isSignZero(a: A): Boolean = signum(a) == 0 |
| 51 | + def isSignPositive(a: A): Boolean = signum(a) > 0 |
| 52 | + def isSignNegative(a: A): Boolean = signum(a) < 0 |
| 53 | + |
| 54 | + def isSignNonZero(a: A): Boolean = signum(a) != 0 |
| 55 | + def isSignNonPositive(a: A): Boolean = signum(a) <= 0 |
| 56 | + def isSignNonNegative(a: A): Boolean = signum(a) >= 0 |
| 57 | +} |
| 58 | + |
| 59 | +trait SignedFunctions[S[T] <: Signed[T]] extends cats.kernel.OrderFunctions[Order] { |
| 60 | + def sign[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Signed.Sign = |
| 61 | + ev.sign(a) |
| 62 | + def signum[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Int = |
| 63 | + ev.signum(a) |
| 64 | + def abs[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): A = |
| 65 | + ev.abs(a) |
| 66 | + def isSignZero[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 67 | + ev.isSignZero(a) |
| 68 | + def isSignPositive[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 69 | + ev.isSignPositive(a) |
| 70 | + def isSignNegative[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 71 | + ev.isSignNegative(a) |
| 72 | + def isSignNonZero[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 73 | + ev.isSignNonZero(a) |
| 74 | + def isSignNonPositive[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 75 | + ev.isSignNonPositive(a) |
| 76 | + def isSignNonNegative[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: S[A]): Boolean = |
| 77 | + ev.isSignNonNegative(a) |
| 78 | +} |
| 79 | + |
| 80 | +object Signed extends SignedFunctions[Signed] { |
| 81 | + |
| 82 | + /** |
| 83 | + * Signed implementation for additive commutative monoids |
| 84 | + */ |
| 85 | + trait forAdditiveCommutativeMonoid[A] extends Any with Signed[A] with AdditiveCommutativeMonoid[A] { |
| 86 | + final override def additiveCommutativeMonoid = this |
| 87 | + def signum(a: A): Int = { |
| 88 | + val c = order.compare(a, zero) |
| 89 | + if (c < 0) -1 |
| 90 | + else if (c > 0) 1 |
| 91 | + else 0 |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Signed implementation for additive commutative groups |
| 97 | + */ |
| 98 | + trait forAdditiveCommutativeGroup[A] |
| 99 | + extends Any |
| 100 | + with forAdditiveCommutativeMonoid[A] |
| 101 | + with AdditiveCommutativeGroup[A] { |
| 102 | + def abs(a: A): A = if (order.compare(a, zero) < 0) negate(a) else a |
| 103 | + } |
| 104 | + |
| 105 | + def apply[A](implicit s: Signed[A]): Signed[A] = s |
| 106 | + |
| 107 | + /** |
| 108 | + * A simple ADT representing the `Sign` of an object. |
| 109 | + */ |
| 110 | + sealed abstract class Sign(val toInt: Int) { |
| 111 | + def unary_- : Sign = this match { |
| 112 | + case Positive => Negative |
| 113 | + case Negative => Positive |
| 114 | + case Zero => Zero |
| 115 | + } |
| 116 | + |
| 117 | + def *(that: Sign): Sign = Sign(this.toInt * that.toInt) |
| 118 | + |
| 119 | + def **(that: Int): Sign = this match { |
| 120 | + case Positive => Positive |
| 121 | + case Zero if that == 0 => Positive |
| 122 | + case Zero => Zero |
| 123 | + case Negative if (that % 2) == 0 => Positive |
| 124 | + case Negative => Negative |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + case object Zero extends Sign(0) |
| 129 | + case object Positive extends Sign(1) |
| 130 | + case object Negative extends Sign(-1) |
| 131 | + |
| 132 | + object Sign { |
| 133 | + implicit def sign2int(s: Sign): Int = s.toInt |
| 134 | + |
| 135 | + def apply(i: Int): Sign = |
| 136 | + if (i == 0) Zero else if (i > 0) Positive else Negative |
| 137 | + |
| 138 | + private val instance: CommutativeMonoid[Sign] with MultiplicativeCommutativeMonoid[Sign] with Eq[Sign] = |
| 139 | + new CommutativeMonoid[Sign] with MultiplicativeCommutativeMonoid[Sign] with Eq[Sign] { |
| 140 | + def eqv(x: Sign, y: Sign): Boolean = x == y |
| 141 | + def empty: Sign = Positive |
| 142 | + def combine(x: Sign, y: Sign): Sign = x * y |
| 143 | + def one: Sign = Positive |
| 144 | + def times(x: Sign, y: Sign): Sign = x * y |
| 145 | + } |
| 146 | + |
| 147 | + implicit final def signMultiplicativeMonoid: MultiplicativeCommutativeMonoid[Sign] = instance |
| 148 | + implicit final def signMonoid: CommutativeMonoid[Sign] = instance |
| 149 | + implicit final def signEq: Eq[Sign] = instance |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments