-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add algebra interop module * Add algebra instances for Rational * Headers * Add cats CommutativeGroup instance for Rational * Order for Rational * Setup algebra for testing * Create a testkit * Add Cogen[Rational] * Test laws for Rational instances * Fix unlawful pow * Make Rational#hashCode lawful * Add arbs for (Delta)Quantity * Add Cats instances for Rational, test laws
- Loading branch information
1 parent
4fe10f8
commit a7eef88
Showing
7 changed files
with
145 additions
and
4 deletions.
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
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
42 changes: 42 additions & 0 deletions
42
core/src/main/scala/coulomb/ops/algebra/cats/rational.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 @@ | ||
/* | ||
* Copyright 2022 Erik Erlandson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package coulomb.ops.algebra.cats | ||
|
||
import algebra.ring.* | ||
import cats.kernel.* | ||
import coulomb.ops.standard.* | ||
import coulomb.rational.* | ||
|
||
object rational: | ||
given Field[Rational] with | ||
def zero = Rational.const0 | ||
def one = Rational.const1 | ||
def plus(x: Rational, y: Rational) = x + y | ||
override def minus(x: Rational, y: Rational) = x - y | ||
def times(x: Rational, y: Rational) = x * y | ||
def div(x: Rational, y: Rational) = x / y | ||
def negate(x: Rational) = -x | ||
override def pow(x: Rational, n: Int) = x.pow(n) | ||
|
||
given CommutativeGroup[Rational] = summon[Field[Rational]].additive | ||
|
||
given Order[Rational] with Hash[Rational] with | ||
def hash(x: Rational) = x.hashCode | ||
def compare(x: Rational, y: Rational) = | ||
if x == y then 0 | ||
else if x > y then 1 | ||
else -1 |
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,41 @@ | ||
/* | ||
* Copyright 2022 Erik Erlandson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package coulomb | ||
package testkit | ||
|
||
import coulomb.rational.* | ||
import coulomb.syntax.* | ||
import org.scalacheck.* | ||
|
||
given Arbitrary[Rational] = Arbitrary( | ||
for | ||
n <- Arbitrary.arbitrary[BigInt] | ||
d <- Arbitrary.arbitrary[BigInt].suchThat(_ != 0) | ||
yield Rational(n, d) | ||
) | ||
|
||
given Cogen[Rational] = summon[Cogen[(BigInt, BigInt)]].contramap(r => (r.n, r.d)) | ||
|
||
given [V, U](using arbV: Arbitrary[V]): Arbitrary[Quantity[V, U]] = | ||
Arbitrary(arbV.arbitrary.map(_.withUnit[U])) | ||
|
||
given [V, U, B](using arbV: Arbitrary[V]): Arbitrary[DeltaQuantity[V, U, B]] = | ||
Arbitrary(arbV.arbitrary.map(_.withDeltaUnit[U, B])) | ||
|
||
given [V, U](using cogenV: Cogen[V]): Cogen[Quantity[V, U]] = cogenV.contramap(_.value) | ||
|
||
given [V, U, B](using cogenV: Cogen[V]): Cogen[DeltaQuantity[V, U, B]] = cogenV.contramap(_.value) |
41 changes: 41 additions & 0 deletions
41
testkit/src/test/scala/coulomb/testkit/RationalSuite.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,41 @@ | ||
/* | ||
* Copyright 2022 Erik Erlandson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package coulomb.testkit | ||
|
||
import algebra.laws.* | ||
import cats.kernel.laws.discipline.* | ||
import coulomb.ops.algebra.cats.all.given | ||
import coulomb.rational.* | ||
import munit.DisciplineSuite | ||
import org.scalacheck.Prop.* | ||
|
||
class RationalSuite extends DisciplineSuite: | ||
property("rational identity") { | ||
forAll { (r: Rational) => | ||
r == Rational.const0 || (Rational.const1 / r) * r == Rational.const1 | ||
} | ||
} | ||
|
||
property("functions of rationals are pure") { | ||
forAll { (x: Rational, y: Rational, f: Rational => Rational) => | ||
x != y || f(x) == f(y) | ||
} | ||
} | ||
|
||
checkAll("Rational", RingLaws[Rational].field) | ||
checkAll("Rational", OrderTests[Rational].order) | ||
checkAll("Rational", HashTests[Rational].hash) |