Skip to content

Commit

Permalink
Refactor instances to avoid conflicts/ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed May 30, 2021
1 parent b0c1ced commit 44eb36c
Show file tree
Hide file tree
Showing 24 changed files with 74 additions and 119 deletions.
6 changes: 1 addition & 5 deletions algebra-core/src/main/scala/cats/algebra/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package algebra
package instances

package object all extends AllInstances
object all extends AllInstances

trait AllInstances
extends ArrayInstances
Expand All @@ -11,16 +11,12 @@ trait AllInstances
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import scala.{specialized => sp}
package object array extends ArrayInstances

trait ArrayInstances {
implicit def arrayEq[@sp A: Eq]: Eq[Array[A]] =
implicit def catsAlgebraStdEqForArray[@sp A: Eq]: Eq[Array[A]] =
new ArrayEq[A]
implicit def arrayOrder[@sp A: Order]: Order[Array[A]] =
implicit def catsAlgebraStdOrderForArray[@sp A: Order]: Order[Array[A]] =
new ArrayOrder[A]
implicit def arrayPartialOrder[@sp A: PartialOrder]: PartialOrder[Array[A]] =
implicit def catsAlgebraStdPartialOrderForArray[@sp A: PartialOrder]: PartialOrder[Array[A]] =
new ArrayPartialOrder[A]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import java.math.MathContext

import cats.algebra.ring._

package object bigDecimal extends BigDecimalInstances
object bigDecimal extends BigDecimalInstances

trait BigDecimalInstances extends cats.kernel.instances.BigDecimalInstances {
implicit val bigDecimalAlgebra: BigDecimalAlgebra = new BigDecimalAlgebra()
trait BigDecimalInstances {
implicit val catsAlgebraStdAlgebraForBigDecimal: BigDecimalAlgebra = new BigDecimalAlgebra()
}

class BigDecimalAlgebra(mc: MathContext) extends Field[BigDecimal] with Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package instances

import cats.algebra.ring._

package object bigInt extends BigIntInstances
object bigInt extends BigIntInstances

trait BigIntInstances extends cats.kernel.instances.BigIntInstances {
trait BigIntInstances {
private val instance: BigIntAlgebra = new BigIntAlgebra
implicit def bigIntAlgebra: EuclideanRing[BigInt] = instance
implicit def bigIntTruncatedDivision: TruncatedDivision[BigInt] = instance
implicit def catsAlgebraStdAlgebraForBigInt: EuclideanRing[BigInt] = instance
implicit def catsAlgebraStdTruncatedDivisionForBigInt: TruncatedDivision[BigInt] = instance
}

class BigIntAlgebra extends EuclideanRing[BigInt] with TruncatedDivision.forCommutativeRing[BigInt] with Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import scala.collection.immutable.BitSet

import cats.algebra.lattice._

package object bitSet extends BitSetInstances
object bitSet extends BitSetInstances

trait BitSetInstances extends cats.kernel.instances.BitSetInstances {
implicit val bitSetAlgebra: BitSetAlgebra =
trait BitSetInstances {
implicit val catsAlgebraStdAlgebraForBitSet: BitSetAlgebra =
new BitSetAlgebra
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import cats.algebra.lattice.Bool
import cats.algebra.ring.BoolRing
import cats.algebra.ring.CommutativeRig

package object boolean extends BooleanInstances
object boolean extends BooleanInstances

trait BooleanInstances extends cats.kernel.instances.BooleanInstances {
implicit val booleanAlgebra: BooleanAlgebra =
trait BooleanInstances {
implicit val catsAlgebraStdAlgebraForBoolean: BooleanAlgebra =
new BooleanAlgebra

val booleanRing = new BoolRing[Boolean] {
val catsAlgebraStdRingForBoolean: BoolRing[Boolean] = new BoolRing[Boolean] {
def zero: Boolean = false
def one: Boolean = true
def plus(x: Boolean, y: Boolean): Boolean = x ^ y
Expand Down
8 changes: 4 additions & 4 deletions algebra-core/src/main/scala/cats/algebra/instances/byte.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package instances
import cats.algebra.lattice._
import cats.algebra.ring._

package object byte extends ByteInstances
object byte extends ByteInstances

trait ByteInstances extends cats.kernel.instances.ByteInstances {
implicit val byteAlgebra: ByteAlgebra = new ByteAlgebra
trait ByteInstances {
implicit val catsAlgebraStdAlgebraForByte: ByteAlgebra = new ByteAlgebra

val ByteMinMaxLattice: BoundedDistributiveLattice[Byte] =
val catsAlgebraStdMinMaxLatticeForByte: BoundedDistributiveLattice[Byte] =
BoundedDistributiveLattice.minMax[Byte](Byte.MinValue, Byte.MaxValue)
}

Expand Down
7 changes: 0 additions & 7 deletions algebra-core/src/main/scala/cats/algebra/instances/char.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import cats.algebra.ring.Field

import java.lang.Math

trait DoubleInstances extends cats.kernel.instances.DoubleInstances {
implicit val doubleAlgebra: Field[Double] =
object double extends DoubleInstances

trait DoubleInstances {
implicit val catsAlgebraStdAlgebraForDouble: Field[Double] =
new DoubleAlgebra

// This is not Bounded due to the presence of NaN
val DoubleMinMaxLattice: DistributiveLattice[Double] =
val catsAlgebraStdMinMaxLatticeForDouble: DistributiveLattice[Double] =
DistributiveLattice.minMax[Double]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import cats.algebra.lattice.DistributiveLattice
import cats.algebra.ring.Field
import java.lang.Math

trait FloatInstances extends cats.kernel.instances.FloatInstances {
implicit val floatAlgebra: Field[Float] =
object float extends FloatInstances

trait FloatInstances {
implicit val catsAlgebraStdAlgebraForFloat: Field[Float] =
new FloatAlgebra

// Not bounded due to the presence of NaN
val FloatMinMaxLattice: DistributiveLattice[Float] =
val catsAlgebraStdMinMaxLatticeForFloat: DistributiveLattice[Float] =
DistributiveLattice.minMax[Float]
}

Expand Down
8 changes: 4 additions & 4 deletions algebra-core/src/main/scala/cats/algebra/instances/int.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package instances
import cats.algebra.lattice._
import cats.algebra.ring._

package object int extends IntInstances
object int extends IntInstances

trait IntInstances extends cats.kernel.instances.IntInstances {
implicit val intAlgebra: IntAlgebra =
trait IntInstances {
implicit val catsAlgebraStdAlgebraForInt: IntAlgebra =
new IntAlgebra

val IntMinMaxLattice: BoundedDistributiveLattice[Int] =
val catsAlgebraStdBoundedDistributiveLatticeForInt: BoundedDistributiveLattice[Int] =
BoundedDistributiveLattice.minMax[Int](Int.MinValue, Int.MaxValue)
}

Expand Down
7 changes: 0 additions & 7 deletions algebra-core/src/main/scala/cats/algebra/instances/list.scala

This file was deleted.

8 changes: 4 additions & 4 deletions algebra-core/src/main/scala/cats/algebra/instances/long.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package instances
import cats.algebra.lattice._
import cats.algebra.ring._

package object long extends LongInstances
object long extends LongInstances

trait LongInstances extends cats.kernel.instances.LongInstances {
implicit val longAlgebra: LongAlgebra =
trait LongInstances {
implicit val catsAlgebraStdAlgebraForLong: LongAlgebra =
new LongAlgebra

val LongMinMaxLattice: BoundedDistributiveLattice[Long] =
val catsAlgebraStdBoundedDistributiveLatticeForLong: BoundedDistributiveLattice[Long] =
BoundedDistributiveLattice.minMax[Long](Long.MinValue, Long.MaxValue)
}

Expand Down
16 changes: 5 additions & 11 deletions algebra-core/src/main/scala/cats/algebra/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ import scala.collection.mutable

import cats.algebra.ring._

package object map extends MapInstances
object map extends MapInstances

trait MapInstances extends cats.kernel.instances.MapInstances with MapInstances3

trait MapInstances3 extends MapInstances2 {}

trait MapInstances2 extends MapInstances1 {
implicit def mapSemiring[K, V: Semiring]: MapSemiring[K, V] =
trait MapInstances extends LowPriorityMapInstances {
implicit def catsAlgebraStdSemiringForMap[K, V: Semiring]: MapSemiring[K, V] =
new MapSemiring[K, V]
}

trait MapInstances1 extends MapInstances0 {}

trait MapInstances0 {
implicit def mapAdditiveMonoid[K, V: AdditiveSemigroup]: MapAdditiveMonoid[K, V] =
trait LowPriorityMapInstances {
implicit def catsAlgebraStdAdditiveMonoidForMap[K, V: AdditiveSemigroup]: MapAdditiveMonoid[K, V] =
new MapAdditiveMonoid[K, V]
}

Expand Down

This file was deleted.

10 changes: 5 additions & 5 deletions algebra-core/src/main/scala/cats/algebra/instances/set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ package instances
import cats.algebra.lattice.GenBool
import cats.algebra.ring.{BoolRng, Semiring}

package object set extends SetInstances
object set extends SetInstances

trait SetInstances extends cats.kernel.instances.SetInstances {
trait SetInstances {

implicit def setLattice[A]: GenBool[Set[A]] = new SetLattice[A]
implicit def setSemiring[A]: Semiring[Set[A]] = new SetSemiring[A]
implicit def catsAlgebraStdLatticeForSet[A]: GenBool[Set[A]] = new SetLattice[A]
implicit def catsAlgebraStdSemiringForSet[A]: Semiring[Set[A]] = new SetSemiring[A]

// this instance is not compatible with setSemiring, so it is not
// marked as implicit to avoid an ambiguity.
def setBoolRng[A]: BoolRng[Set[A]] = new SetBoolRng[A]
def catsAlgebraStdBoolRngForSet[A]: BoolRng[Set[A]] = new SetBoolRng[A]
}

class SetLattice[A] extends GenBool[Set[A]] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package instances
import cats.algebra.lattice._
import cats.algebra.ring._

package object short extends ShortInstances
object short extends ShortInstances

trait ShortInstances extends cats.kernel.instances.ShortInstances {
implicit val shortAlgebra: ShortAlgebra =
trait ShortInstances {
implicit val catsAlgebraStdAlgebraForShort: ShortAlgebra =
new ShortAlgebra

val ShortMinMaxLattice: BoundedDistributiveLattice[Short] =
val catsAlgebraStdMinMaxLatticeForShort: BoundedDistributiveLattice[Short] =
BoundedDistributiveLattice.minMax[Short](Short.MinValue, Short.MaxValue)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package cats
package algebra
package instances

package object tuple extends TupleInstances
object tuple extends TupleInstances
6 changes: 3 additions & 3 deletions algebra-core/src/main/scala/cats/algebra/instances/unit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package instances

import cats.algebra.ring.CommutativeRing

package object unit extends UnitInstances
object unit extends UnitInstances

trait UnitInstances extends cats.kernel.instances.UnitInstances {
implicit val unitRing: CommutativeRing[Unit] =
trait UnitInstances {
implicit val catsAlgebraStdAlgebraForUnit: UnitAlgebra =
new UnitAlgebra
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.typelevel.discipline.Laws
import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Prop._

import cats.algebra.instances.boolean._

object LatticePartialOrderTests {
def apply[A: Eq: Arbitrary] = new LatticePartialOrderTests[A] {
def Equ = Eq[A]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package cats.algebra.laws

import cats.algebra.ring.{
AdditiveCommutativeGroup,
CommutativeRing,
GCDRing,
Signed,
TruncatedDivision
}
import cats.algebra.ring.{AdditiveCommutativeGroup, CommutativeRing, GCDRing, Signed, TruncatedDivision}
import cats.kernel._
import org.scalacheck.{Arbitrary, Cogen, Prop}
import org.scalacheck.Prop._
Expand Down
23 changes: 10 additions & 13 deletions algebra-laws/shared/src/test/scala/cats/algebra/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import scala.collection.immutable.BitSet

class LawTests extends munit.DisciplineSuite {

implicit val byteLattice: Lattice[Byte] = ByteMinMaxLattice
implicit val shortLattice: Lattice[Short] = ShortMinMaxLattice
implicit val intLattice: BoundedDistributiveLattice[Int] = IntMinMaxLattice
implicit val longLattice: BoundedDistributiveLattice[Long] = LongMinMaxLattice

case class HasEq[A](a: A)

object HasEq {
Expand All @@ -45,17 +40,19 @@ class LawTests extends munit.DisciplineSuite {
checkAll("SimpleDeMorgan", LogicTests[SimpleDeMorgan].deMorgan)
checkAll("Boolean", LogicTests[Boolean].deMorgan(DeMorgan.fromBool(Bool[Boolean])))
checkAll("Boolean", LatticePartialOrderTests[Boolean].boundedLatticePartialOrder)
checkAll("Boolean", RingTests[Boolean].boolRing(booleanRing))
checkAll("Boolean", RingTests[Boolean].boolRing(catsAlgebraStdRingForBoolean))

// ensure that Bool[A].asBoolRing is a valid BoolRing
checkAll("Boolean-ring-from-bool", RingTests[Boolean].boolRing(new BoolRingFromBool[Boolean](Bool[Boolean])))

// ensure that BoolRing[A].asBool is a valid Bool
checkAll("Boolean- bool-from-ring", LogicTests[Boolean].bool(new BoolFromBoolRing(booleanRing)))
checkAll("Boolean- bool-from-ring", LogicTests[Boolean].bool(new BoolFromBoolRing(catsAlgebraStdRingForBoolean)))

checkAll("Set[Byte]", LogicTests[Set[Byte]].generalizedBool)
checkAll("Set[Byte]", RingTests[Set[Byte]].boolRng(setBoolRng[Byte]))
checkAll("Set[Byte]-bool-from-rng", LogicTests[Set[Byte]].generalizedBool(new GenBoolFromBoolRng(setBoolRng)))
checkAll("Set[Byte]", RingTests[Set[Byte]].boolRng(catsAlgebraStdBoolRngForSet))
checkAll("Set[Byte]-bool-from-rng",
LogicTests[Set[Byte]].generalizedBool(new GenBoolFromBoolRng(catsAlgebraStdBoolRngForSet))
)
checkAll("Set[Byte]-rng-from-bool", RingTests[Set[Byte]].boolRng(new BoolRngFromGenBool(GenBool[Set[Byte]])))
checkAll("Set[Int]", RingTests[Set[Int]].semiring)
checkAll("Set[String]", RingTests[Set[String]].semiring)
Expand All @@ -64,20 +61,20 @@ class LawTests extends munit.DisciplineSuite {
checkAll("Map[Int, BigInt]", RingTests[Map[Int, BigInt]].semiring)

checkAll("Byte", RingTests[Byte].commutativeRing)
checkAll("Byte", LatticeTests[Byte].lattice)
checkAll("Byte", LatticeTests[Byte].lattice(catsAlgebraStdMinMaxLatticeForByte))

checkAll("Short", RingTests[Short].commutativeRing)
checkAll("Short", LatticeTests[Short].lattice)
checkAll("Short", LatticeTests[Short].lattice(catsAlgebraStdMinMaxLatticeForShort))

checkAll("Int", RingTests[Int].commutativeRing)
checkAll("Int", LatticeTests[Int].boundedDistributiveLattice)
checkAll("Int", LatticeTests[Int].boundedDistributiveLattice(catsAlgebraStdBoundedDistributiveLatticeForInt))

{
checkAll("Int", RingTests[Int].commutativeRig)
}

checkAll("Long", RingTests[Long].commutativeRing)
checkAll("Long", LatticeTests[Long].boundedDistributiveLattice)
checkAll("Long", LatticeTests[Long].boundedDistributiveLattice(catsAlgebraStdBoundedDistributiveLatticeForLong))

checkAll("BigInt", RingTests[BigInt].euclideanRing)
checkAll("BigInt", SignedTests[BigInt].truncatedDivision)
Expand Down
Loading

0 comments on commit 44eb36c

Please sign in to comment.