-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binary-compatibly move typelevel/algebra into cats repo #3918
Merged
johnynek
merged 17 commits into
typelevel:main
from
armanbilge:migrate-algebra-bincompat
Aug 25, 2021
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2c51138
Initial dump of algebra into cats
armanbilge 71f397b
Disable MiMa
armanbilge 080a539
@nowarn deprecations
armanbilge 700c0b7
Migrate docs
armanbilge c51b726
Reference cats-algebra in README/docs
armanbilge d609b7d
Re-enable MiMa for algebra (but not laws)
armanbilge d54ae94
Merge branch 'main' into migrate-algebra-bincompat
armanbilge a652b61
Merge in typelevel/algebra#246
armanbilge 591b733
Implement typelevel/algebra#108
armanbilge 1ec9e21
Add Signed and TruncatedDivision typeclasses
armanbilge 66a6231
Make Field inherit from DivisionRing
armanbilge 2943b54
Start migrating tests
armanbilge 5c4d170
Add tests for Signed/TruncatedDivision
armanbilge 716ec2b
Prefer deprecation to source incompatibility
armanbilge ec0ed75
Remove redundant tests
armanbilge 610d1e1
Fix reference to algebra in README
armanbilge 84c780a
Un-deprecate Priority
armanbilge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package algebra | ||
|
||
import scala.annotation.nowarn | ||
|
||
/** | ||
* Priority is a type class for prioritized implicit search. | ||
* | ||
* This type class will attempt to provide an implicit instance of `P` | ||
* (the preferred type). If that type is not available it will | ||
* fallback to `F` (the fallback type). If neither type is available | ||
* then a `Priority[P, F]` instance will not be available. | ||
* | ||
* This type can be useful for problems where multiple algorithms can | ||
* be used, depending on the type classes available. | ||
*/ | ||
@deprecated("No replacement", since = "2.7.0") | ||
sealed trait Priority[+P, +F] { | ||
|
||
import Priority.{Fallback, Preferred} | ||
|
||
def fold[B](f1: P => B)(f2: F => B): B = | ||
this match { | ||
case Preferred(x) => f1(x) | ||
case Fallback(y) => f2(y) | ||
} | ||
|
||
def join[U >: P with F]: U = | ||
fold(_.asInstanceOf[U])(_.asInstanceOf[U]) | ||
|
||
def bimap[P2, F2](f1: P => P2)(f2: F => F2): Priority[P2, F2] = | ||
this match { | ||
case Preferred(x) => Preferred(f1(x)) | ||
case Fallback(y) => Fallback(f2(y)) | ||
} | ||
|
||
def toEither: Either[P, F] = | ||
fold[Either[P, F]](p => Left(p))(f => Right(f)) | ||
|
||
def isPreferred: Boolean = | ||
fold(_ => true)(_ => false) | ||
|
||
def isFallback: Boolean = | ||
fold(_ => false)(_ => true) | ||
|
||
def getPreferred: Option[P] = | ||
fold[Option[P]](p => Some(p))(_ => None) | ||
|
||
def getFallback: Option[F] = | ||
fold[Option[F]](_ => None)(f => Some(f)) | ||
} | ||
|
||
@deprecated("No replacement", since = "2.7.0") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove this? |
||
object Priority extends FindPreferred { | ||
|
||
case class Preferred[P](get: P) extends Priority[P, Nothing] | ||
case class Fallback[F](get: F) extends Priority[Nothing, F] | ||
|
||
def apply[P, F](implicit ev: Priority[P, F]): Priority[P, F] = ev | ||
} | ||
|
||
private[algebra] trait FindPreferred extends FindFallback { | ||
@nowarn("msg=deprecated") | ||
implicit def preferred[P](implicit ev: P): Priority[P, Nothing] = | ||
Priority.Preferred(ev) | ||
} | ||
|
||
private[algebra] trait FindFallback { | ||
@nowarn("msg=deprecated") | ||
implicit def fallback[F](implicit ev: F): Priority[Nothing, F] = | ||
Priority.Fallback(ev) | ||
} |
27 changes: 27 additions & 0 deletions
27
algebra-core/src/main/scala/algebra/instances/StaticMethods.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package algebra.instances | ||
|
||
import scala.annotation.tailrec | ||
|
||
object StaticMethods { | ||
|
||
/** | ||
* Exponentiation function, e.g. x^y | ||
* | ||
* If base^ex doesn't fit in a Long, the result will overflow (unlike | ||
* Math.pow which will return +/- Infinity). | ||
*/ | ||
final def pow(base: Long, exponent: Long): Long = { | ||
@tailrec def loop(t: Long, b: Long, e: Long): Long = | ||
if (e == 0L) t | ||
else if ((e & 1) == 1) loop(t * b, b * b, e >>> 1L) | ||
else loop(t, b * b, e >>> 1L) | ||
|
||
if (exponent >= 0L) loop(1L, base, exponent) | ||
else { | ||
if (base == 0L) throw new ArithmeticException("zero can't be raised to negative power") | ||
else if (base == 1L) 1L | ||
else if (base == -1L) if ((exponent & 1L) == 0L) -1L else 1L | ||
else 0L | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package algebra | ||
package instances | ||
|
||
package object all extends AllInstances | ||
|
||
trait AllInstances | ||
extends ArrayInstances | ||
with BigDecimalInstances | ||
with BigIntInstances | ||
with BitSetInstances | ||
with BooleanInstances | ||
with ByteInstances | ||
with CharInstances | ||
with DoubleInstances | ||
with FloatInstances | ||
with IntInstances | ||
with ListInstances | ||
with LongInstances | ||
with MapInstances | ||
with OptionInstances | ||
with SetInstances | ||
with ShortInstances | ||
with StringInstances | ||
with TupleInstances | ||
with UnitInstances |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package algebra | ||
package instances | ||
|
||
import scala.{specialized => sp} | ||
|
||
package object array extends ArrayInstances | ||
|
||
trait ArrayInstances { | ||
implicit def arrayEq[@sp A: Eq]: Eq[Array[A]] = | ||
new ArrayEq[A] | ||
implicit def arrayOrder[@sp A: Order]: Order[Array[A]] = | ||
new ArrayOrder[A] | ||
implicit def arrayPartialOrder[@sp A: PartialOrder]: PartialOrder[Array[A]] = | ||
new ArrayPartialOrder[A] | ||
} | ||
|
||
private object ArraySupport { | ||
|
||
private def signum(x: Int): Int = | ||
if (x < 0) -1 | ||
else if (x > 0) 1 | ||
else 0 | ||
|
||
def eqv[@sp A](x: Array[A], y: Array[A])(implicit ev: Eq[A]): Boolean = { | ||
var i = 0 | ||
if (x.length != y.length) return false | ||
while (i < x.length && i < y.length && ev.eqv(x(i), y(i))) i += 1 | ||
i == x.length | ||
} | ||
|
||
def compare[@sp A](x: Array[A], y: Array[A])(implicit ev: Order[A]): Int = { | ||
var i = 0 | ||
while (i < x.length && i < y.length) { | ||
val cmp = ev.compare(x(i), y(i)) | ||
if (cmp != 0) return cmp | ||
i += 1 | ||
} | ||
signum(x.length - y.length) | ||
} | ||
|
||
def partialCompare[@sp A](x: Array[A], y: Array[A])(implicit ev: PartialOrder[A]): Double = { | ||
var i = 0 | ||
while (i < x.length && i < y.length) { | ||
val cmp = ev.partialCompare(x(i), y(i)) | ||
// Double.NaN is also != 0.0 | ||
if (cmp != 0.0) return cmp | ||
i += 1 | ||
} | ||
signum(x.length - y.length).toDouble | ||
} | ||
} | ||
|
||
final private class ArrayEq[@sp A: Eq] extends Eq[Array[A]] with Serializable { | ||
def eqv(x: Array[A], y: Array[A]): Boolean = | ||
ArraySupport.eqv(x, y) | ||
} | ||
|
||
final private class ArrayOrder[@sp A: Order] extends Order[Array[A]] with Serializable { | ||
override def eqv(x: Array[A], y: Array[A]): Boolean = | ||
ArraySupport.eqv(x, y) | ||
def compare(x: Array[A], y: Array[A]): Int = | ||
ArraySupport.compare(x, y) | ||
} | ||
|
||
final private class ArrayPartialOrder[@sp A: PartialOrder] extends PartialOrder[Array[A]] with Serializable { | ||
override def eqv(x: Array[A], y: Array[A]): Boolean = | ||
ArraySupport.eqv(x, y) | ||
override def partialCompare(x: Array[A], y: Array[A]): Double = | ||
ArraySupport.partialCompare(x, y) | ||
} |
31 changes: 31 additions & 0 deletions
31
algebra-core/src/main/scala/algebra/instances/bigDecimal.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package algebra | ||
package instances | ||
|
||
import java.math.MathContext | ||
|
||
import algebra.ring._ | ||
|
||
package object bigDecimal extends BigDecimalInstances | ||
|
||
trait BigDecimalInstances extends cats.kernel.instances.BigDecimalInstances { | ||
implicit val bigDecimalAlgebra: BigDecimalAlgebra = new BigDecimalAlgebra() | ||
} | ||
|
||
class BigDecimalAlgebra(mc: MathContext) extends Field[BigDecimal] with Serializable { | ||
def this() = this(MathContext.UNLIMITED) | ||
|
||
val zero: BigDecimal = BigDecimal(0, mc) | ||
val one: BigDecimal = BigDecimal(1, mc) | ||
|
||
def plus(a: BigDecimal, b: BigDecimal): BigDecimal = a + b | ||
def negate(a: BigDecimal): BigDecimal = -a | ||
override def minus(a: BigDecimal, b: BigDecimal): BigDecimal = a - b | ||
|
||
def times(a: BigDecimal, b: BigDecimal): BigDecimal = a * b | ||
def div(a: BigDecimal, b: BigDecimal): BigDecimal = a / b | ||
|
||
override def pow(a: BigDecimal, k: Int): BigDecimal = a.pow(k) | ||
|
||
override def fromInt(n: Int): BigDecimal = BigDecimal(n, mc) | ||
override def fromBigInt(n: BigInt): BigDecimal = BigDecimal(n, mc) | ||
} |
63 changes: 63 additions & 0 deletions
63
algebra-core/src/main/scala/algebra/instances/bigInt.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package algebra | ||
package instances | ||
|
||
import algebra.ring._ | ||
|
||
package object bigInt extends BigIntInstances | ||
|
||
trait BigIntInstances extends cats.kernel.instances.BigIntInstances { | ||
implicit val bigIntAlgebra: BigIntAlgebra = new BigIntTruncatedDivison | ||
implicit def bigIntTruncatedDivision: TruncatedDivision[BigInt] = | ||
bigIntAlgebra.asInstanceOf[BigIntTruncatedDivison] // Bin-compat hack to avoid allocation | ||
} | ||
|
||
class BigIntAlgebra extends EuclideanRing[BigInt] with Serializable { | ||
|
||
val zero: BigInt = BigInt(0) | ||
val one: BigInt = BigInt(1) | ||
|
||
def plus(a: BigInt, b: BigInt): BigInt = a + b | ||
def negate(a: BigInt): BigInt = -a | ||
override def minus(a: BigInt, b: BigInt): BigInt = a - b | ||
|
||
def times(a: BigInt, b: BigInt): BigInt = a * b | ||
|
||
override def pow(a: BigInt, k: Int): BigInt = a.pow(k) | ||
|
||
override def fromInt(n: Int): BigInt = BigInt(n) | ||
override def fromBigInt(n: BigInt): BigInt = n | ||
|
||
override def lcm(a: BigInt, b: BigInt)(implicit ev: Eq[BigInt]): BigInt = | ||
if (a.signum == 0 || b.signum == 0) zero else (a / a.gcd(b)) * b | ||
override def gcd(a: BigInt, b: BigInt)(implicit ev: Eq[BigInt]): BigInt = a.gcd(b) | ||
|
||
def euclideanFunction(a: BigInt): BigInt = a.abs | ||
|
||
override def equotmod(a: BigInt, b: BigInt): (BigInt, BigInt) = { | ||
val (qt, rt) = a /% b // truncated quotient and remainder | ||
if (rt.signum >= 0) (qt, rt) | ||
else if (b.signum > 0) (qt - 1, rt + b) | ||
else (qt + 1, rt - b) | ||
} | ||
|
||
def equot(a: BigInt, b: BigInt): BigInt = { | ||
val (qt, rt) = a /% b // truncated quotient and remainder | ||
if (rt.signum >= 0) qt | ||
else if (b.signum > 0) qt - 1 | ||
else qt + 1 | ||
} | ||
|
||
def emod(a: BigInt, b: BigInt): BigInt = { | ||
val rt = a % b // truncated remainder | ||
if (rt.signum >= 0) rt | ||
else if (b > 0) rt + b | ||
else rt - b | ||
} | ||
|
||
} | ||
|
||
class BigIntTruncatedDivison extends BigIntAlgebra with TruncatedDivision.forCommutativeRing[BigInt] { | ||
override def tquot(x: BigInt, y: BigInt): BigInt = x / y | ||
override def tmod(x: BigInt, y: BigInt): BigInt = x % y | ||
override def order: Order[BigInt] = cats.kernel.instances.bigInt.catsKernelStdOrderForBigInt | ||
} |
21 changes: 21 additions & 0 deletions
21
algebra-core/src/main/scala/algebra/instances/bitSet.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package algebra | ||
package instances | ||
|
||
import scala.collection.immutable.BitSet | ||
|
||
import algebra.lattice._ | ||
|
||
package object bitSet extends BitSetInstances | ||
|
||
trait BitSetInstances extends cats.kernel.instances.BitSetInstances { | ||
implicit val bitSetAlgebra: BitSetAlgebra = | ||
new BitSetAlgebra | ||
} | ||
|
||
class BitSetAlgebra extends GenBool[BitSet] with Serializable { | ||
val zero: BitSet = BitSet.empty | ||
def and(a: BitSet, b: BitSet): BitSet = a & b | ||
def or(a: BitSet, b: BitSet): BitSet = a | b | ||
def without(a: BitSet, b: BitSet): BitSet = a -- b | ||
override def xor(a: BitSet, b: BitSet): BitSet = a ^ b | ||
} |
42 changes: 42 additions & 0 deletions
42
algebra-core/src/main/scala/algebra/instances/boolean.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package algebra | ||
package instances | ||
|
||
import algebra.lattice.Bool | ||
import algebra.ring.BoolRing | ||
import algebra.ring.CommutativeRig | ||
|
||
package object boolean extends BooleanInstances | ||
|
||
trait BooleanInstances extends cats.kernel.instances.BooleanInstances { | ||
implicit val booleanAlgebra: BooleanAlgebra = | ||
new BooleanAlgebra | ||
|
||
val booleanRing = new BoolRing[Boolean] { | ||
def zero: Boolean = false | ||
def one: Boolean = true | ||
def plus(x: Boolean, y: Boolean): Boolean = x ^ y | ||
def times(x: Boolean, y: Boolean): Boolean = x && y | ||
} | ||
} | ||
|
||
/** | ||
* This commutative rig is different than the one obtained from GF(2). | ||
* | ||
* It uses || for plus, and && for times. | ||
*/ | ||
class BooleanAlgebra extends Bool[Boolean] with CommutativeRig[Boolean] { | ||
|
||
def zero: Boolean = false | ||
def one: Boolean = true | ||
|
||
override def isZero(x: Boolean)(implicit ev: Eq[Boolean]): Boolean = !x | ||
override def isOne(x: Boolean)(implicit ev: Eq[Boolean]): Boolean = x | ||
|
||
def and(x: Boolean, y: Boolean): Boolean = x && y | ||
def or(x: Boolean, y: Boolean): Boolean = x || y | ||
def complement(x: Boolean): Boolean = !x | ||
|
||
def plus(a: Boolean, b: Boolean): Boolean = a || b | ||
override def pow(a: Boolean, b: Int): Boolean = a | ||
override def times(a: Boolean, b: Boolean): Boolean = a && b | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this deprecated? It seems it is not deprecated in algebra?
The motivation is that some functions can be written that e.g. use either a Group or a Monoid. If you have a Group, it can be more efficient, or else you can fall back to Monoid (at a constant multiplicative cost).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: we could imagine pushing this onto
Either[A, B]
in the scala standard library (prefer Right, fallback to Left). Similarly withOption[A]
and tuples, but I think the standard library currently does not allow this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just replaying 7ff1809 which was suggested on the original PR since it's not used in algebra itself. My bad, now I see its intrinsic value. I'll bring it back.
Btw I did see your plans to submit it to Scala standard library in typelevel/algebra#67!