-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Move typelevel/algebra into cats repo #3877
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 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 66b8074
Rename package algebra -> cats.algebra
armanbilge a6ec3b8
Merge DeMorganLaws into LogicLaws per TODO
armanbilge 8ddf2a4
Merge in algebra#246
armanbilge 1ce0efd
Remove unimplemented stubs
armanbilge 39716dc
Formatting
armanbilge 5130867
Add kernel-laws dependency and refactor
armanbilge 7ff1809
Delete priority
armanbilge 048ce2e
Implement algebra#108
armanbilge b81e1fb
Make Field inherit from DivisionRing
armanbilge 844670d
Rename laws -> tests, remove redundancies
armanbilge 7ab3d90
Add Signed and TruncatedDivision typeclasses
armanbilge b0c1ced
More laws for Signed
armanbilge 44eb36c
Refactor instances to avoid conflicts/ambiguities
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 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
23 changes: 23 additions & 0 deletions
23
algebra-core/src/main/scala/cats/algebra/ring/DivisionRing.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package cats | ||
| package algebra | ||
| package ring | ||
|
|
||
| import scala.{specialized => sp} | ||
|
|
||
| trait DivisionRing[@sp(Byte, Short, Int, Long, Float, Double) A] extends Any with Ring[A] with MultiplicativeGroup[A] { | ||
| self => | ||
|
|
||
| def fromDouble(a: Double): A = Field.defaultFromDouble[A](a)(self, self) | ||
|
|
||
| } | ||
|
|
||
| trait DivisionRingFunctions[F[T] <: DivisionRing[T]] extends RingFunctions[F] with MultiplicativeGroupFunctions[F] { | ||
| def fromDouble[@sp(Int, Long, Float, Double) A](n: Double)(implicit ev: F[A]): A = | ||
| ev.fromDouble(n) | ||
| } | ||
|
|
||
| object DivisionRing extends DivisionRingFunctions[DivisionRing] { | ||
|
|
||
| @inline final def apply[A](implicit f: DivisionRing[A]): DivisionRing[A] = f | ||
|
|
||
| } |
60 changes: 60 additions & 0 deletions
60
algebra-core/src/main/scala/cats/algebra/ring/EuclideanRing.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package cats | ||
| package algebra | ||
| package ring | ||
|
|
||
| import scala.annotation.tailrec | ||
| import scala.{specialized => sp} | ||
|
|
||
| /** | ||
| * EuclideanRing implements a Euclidean domain. | ||
| * | ||
| * The formal definition says that every euclidean domain A has (at | ||
| * least one) euclidean function f: A -> N (the natural numbers) where: | ||
| * | ||
| * (for every x and non-zero y) x = yq + r, and r = 0 or f(r) < f(y). | ||
| * | ||
| * This generalizes the Euclidean division of integers, where f represents | ||
| * a measure of length (or absolute value), and the previous equation | ||
| * represents finding the quotient and remainder of x and y. So: | ||
| * | ||
| * quot(x, y) = q | ||
| * mod(x, y) = r | ||
| */ | ||
| trait EuclideanRing[@sp(Int, Long, Float, Double) A] extends Any with GCDRing[A] { self => | ||
| def euclideanFunction(a: A): BigInt | ||
| def equot(a: A, b: A): A | ||
| def emod(a: A, b: A): A | ||
| def equotmod(a: A, b: A): (A, A) = (equot(a, b), emod(a, b)) | ||
| def gcd(a: A, b: A)(implicit ev: Eq[A]): A = | ||
| EuclideanRing.euclid(a, b)(ev, self) | ||
| def lcm(a: A, b: A)(implicit ev: Eq[A]): A = | ||
| if (isZero(a) || isZero(b)) zero else times(equot(a, gcd(a, b)), b) | ||
| // def xgcd(a: A, b: A)(implicit ev: Eq[A]): (A, A, A) = | ||
| } | ||
|
|
||
| trait EuclideanRingFunctions[R[T] <: EuclideanRing[T]] extends GCDRingFunctions[R] { | ||
| def euclideanFunction[@sp(Int, Long, Float, Double) A](a: A)(implicit ev: R[A]): BigInt = | ||
| ev.euclideanFunction(a) | ||
| def equot[@sp(Int, Long, Float, Double) A](a: A, b: A)(implicit ev: R[A]): A = | ||
| ev.equot(a, b) | ||
| def emod[@sp(Int, Long, Float, Double) A](a: A, b: A)(implicit ev: R[A]): A = | ||
| ev.emod(a, b) | ||
| def equotmod[@sp(Int, Long, Float, Double) A](a: A, b: A)(implicit ev: R[A]): (A, A) = | ||
| ev.equotmod(a, b) | ||
| } | ||
|
|
||
| object EuclideanRing extends EuclideanRingFunctions[EuclideanRing] { | ||
|
|
||
| @inline final def apply[A](implicit e: EuclideanRing[A]): EuclideanRing[A] = e | ||
|
|
||
| /** | ||
| * Simple implementation of Euclid's algorithm for gcd | ||
| */ | ||
| @tailrec final def euclid[@sp(Int, Long, Float, Double) A: Eq: EuclideanRing](a: A, b: A): A = { | ||
| if (EuclideanRing[A].isZero(b)) a else euclid(b, EuclideanRing[A].emod(a, b)) | ||
| } | ||
|
|
||
| /* @tailrec final def extendedEuclid[@sp(Int, Long, Float, Double) A: Eq: EuclideanRing](a: A, b: A): (A, A, A) = { | ||
| if (EuclideanRing[A].isZero(b)) a else euclid(b, EuclideanRing[A].emod(a, b))*/ | ||
|
|
||
| } | ||
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
42 changes: 42 additions & 0 deletions
42
algebra-core/src/main/scala/cats/algebra/ring/GCDRing.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package cats | ||
| package algebra | ||
| package ring | ||
|
|
||
| import scala.{specialized => sp} | ||
|
|
||
| /** | ||
| * GCDRing implements a GCD ring. | ||
| * | ||
| * For two elements x and y in a GCD ring, we can choose two elements d and m | ||
| * such that: | ||
| * | ||
| * d = gcd(x, y) | ||
| * m = lcm(x, y) | ||
| * | ||
| * d * m = x * y | ||
| * | ||
| * Additionally, we require: | ||
| * | ||
| * gcd(0, 0) = 0 | ||
| * lcm(x, 0) = lcm(0, x) = 0 | ||
| * | ||
| * and commutativity: | ||
| * | ||
| * gcd(x, y) = gcd(y, x) | ||
| * lcm(x, y) = lcm(y, x) | ||
| */ | ||
| trait GCDRing[@sp(Int, Long, Float, Double) A] extends Any with CommutativeRing[A] { | ||
| def gcd(a: A, b: A)(implicit ev: Eq[A]): A | ||
| def lcm(a: A, b: A)(implicit ev: Eq[A]): A | ||
| } | ||
|
|
||
| trait GCDRingFunctions[R[T] <: GCDRing[T]] extends RingFunctions[R] { | ||
| def gcd[@sp(Int, Long, Float, Double) A](a: A, b: A)(implicit ev: R[A], eqA: Eq[A]): A = | ||
| ev.gcd(a, b)(eqA) | ||
| def lcm[@sp(Int, Long, Float, Double) A](a: A, b: A)(implicit ev: R[A], eqA: Eq[A]): A = | ||
| ev.lcm(a, b)(eqA) | ||
| } | ||
|
|
||
| object GCDRing extends GCDRingFunctions[GCDRing] { | ||
| @inline final def apply[A](implicit ev: GCDRing[A]): GCDRing[A] = ev | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.