-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LowerBounded and UpperBounded typeclasses (#2913)
* Add LowerBounded and UpperBounded typeclasses * Add instances for LowerBounded and UpperBounded typeclasses
- Loading branch information
1 parent
e2c255e
commit 22a8cc6
Showing
18 changed files
with
283 additions
and
30 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
33 changes: 33 additions & 0 deletions
33
kernel-laws/shared/src/main/scala/cats/kernel/laws/BoundedLaws.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,33 @@ | ||
package cats.kernel.laws | ||
|
||
import cats.kernel.{LowerBounded, PartialOrder, UpperBounded} | ||
|
||
trait LowerBoundedLaws[A] extends PartialOrderLaws[A] { | ||
implicit def B: LowerBounded[A] | ||
|
||
def boundLteqv(x: A): IsEq[Boolean] = | ||
E.lteqv(B.minBound, x) <-> true | ||
} | ||
|
||
object LowerBoundedLaws { | ||
def apply[A](implicit ev: LowerBounded[A]): LowerBoundedLaws[A] = | ||
new LowerBoundedLaws[A] { | ||
def B: LowerBounded[A] = ev | ||
def E: PartialOrder[A] = ev.partialOrder | ||
} | ||
} | ||
|
||
trait UpperBoundedLaws[A] extends PartialOrderLaws[A] { | ||
implicit def B: UpperBounded[A] | ||
|
||
def boundGteqv(x: A): IsEq[Boolean] = | ||
E.gteqv(B.maxBound, x) <-> true | ||
} | ||
|
||
object UpperBoundedLaws { | ||
def apply[A](implicit ev: UpperBounded[A]): UpperBoundedLaws[A] = | ||
new UpperBoundedLaws[A] { | ||
def B: UpperBounded[A] = ev | ||
def E: PartialOrder[A] = ev.partialOrder | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
kernel-laws/shared/src/main/scala/cats/kernel/laws/discipline/BoundedTests.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,40 @@ | ||
package cats | ||
package kernel | ||
package laws | ||
package discipline | ||
|
||
import cats.kernel.instances.boolean._ | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Prop.forAll | ||
|
||
trait LowerBoundedTests[A] extends PartialOrderTests[A] { | ||
def laws: LowerBoundedLaws[A] | ||
|
||
def lowerBounded(implicit arbA: Arbitrary[A], arbF: Arbitrary[A => A], eqOA: Eq[Option[A]], eqA: Eq[A]): RuleSet = | ||
new DefaultRuleSet( | ||
"lowerBounded", | ||
Some(partialOrder), | ||
"bound is less than or equals" -> forAll(laws.boundLteqv _) | ||
) | ||
} | ||
|
||
object LowerBoundedTests { | ||
def apply[A: LowerBounded]: LowerBoundedTests[A] = | ||
new LowerBoundedTests[A] { def laws: LowerBoundedLaws[A] = LowerBoundedLaws[A] } | ||
} | ||
|
||
trait UpperBoundedTests[A] extends PartialOrderTests[A] { | ||
def laws: UpperBoundedLaws[A] | ||
|
||
def upperBounded(implicit arbA: Arbitrary[A], arbF: Arbitrary[A => A], eqOA: Eq[Option[A]], eqA: Eq[A]): RuleSet = | ||
new DefaultRuleSet( | ||
"upperBounded", | ||
Some(partialOrder), | ||
"bound is greater than or equals" -> forAll(laws.boundGteqv _) | ||
) | ||
} | ||
|
||
object UpperBoundedTests { | ||
def apply[A: UpperBounded]: UpperBoundedTests[A] = | ||
new UpperBoundedTests[A] { def laws: UpperBoundedLaws[A] = UpperBoundedLaws[A] } | ||
} |
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,43 @@ | ||
package cats.kernel | ||
|
||
import scala.{specialized => sp} | ||
|
||
/** | ||
* A type class used to name the lower limit of a type. | ||
*/ | ||
trait LowerBounded[@sp A] { | ||
def partialOrder: PartialOrder[A] | ||
|
||
/** | ||
* Returns the lower limit of a type. | ||
*/ | ||
def minBound: A | ||
} | ||
|
||
trait LowerBoundedFunctions[L[T] <: LowerBounded[T]] { | ||
def minBound[@sp A](implicit ev: L[A]): A = ev.minBound | ||
} | ||
|
||
object LowerBounded extends LowerBoundedFunctions[LowerBounded] { | ||
@inline def apply[A](implicit l: LowerBounded[A]): LowerBounded[A] = l | ||
} | ||
|
||
/** | ||
* A type class used to name the upper limit of a type. | ||
*/ | ||
trait UpperBounded[@sp A] { | ||
def partialOrder: PartialOrder[A] | ||
|
||
/** | ||
* Returns the upper limit of a type. | ||
*/ | ||
def maxBound: A | ||
} | ||
|
||
trait UpperBoundedFunctions[U[T] <: UpperBounded[T]] { | ||
def maxBound[@sp A](implicit ev: U[A]): A = ev.maxBound | ||
} | ||
|
||
object UpperBounded extends UpperBoundedFunctions[UpperBounded] { | ||
@inline def apply[A](implicit u: UpperBounded[A]): UpperBounded[A] = u | ||
} |
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
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
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
Oops, something went wrong.