From c33bc784b766aabe6064c95e4c916cb11c63fc0c Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 10 Jan 2023 08:33:22 -0800 Subject: [PATCH] Add refined module to root (#417) * Add refined module to root * Formatting * Add formatting commit to `.git-blame-ignore-revs` --- .git-blame-ignore-revs | 3 + build.sbt | 7 +- .../coulomb/conversion/standard/unit.scala | 14 +- .../coulomb/conversion/standard/value.scala | 32 +- .../coulomb/ops/algebra/refined/all.scala | 66 ++-- .../coulomb/ops/resolution/refined.scala | 28 +- .../main/scala/coulomb/syntax/refined.scala | 2 +- refined/src/test/scala/coulomb/quantity.scala | 309 +++++++++++++----- 8 files changed, 323 insertions(+), 138 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 623e88485..6dd934ccb 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,2 +1,5 @@ # Scala Steward: Reformat with scalafmt 3.6.1 5a32ba1fe51ead22fc92c76e3394ee6dcccc00a5 + +# Format refined +804037b709af9fa40c135b8e8c9f44dbcd4119a1 diff --git a/build.sbt b/build.sbt index a3564e82d..8d548423f 100644 --- a/build.sbt +++ b/build.sbt @@ -29,7 +29,7 @@ def commonSettings = Seq( ) lazy val root = tlCrossRootProject - .aggregate(core, units, spire, testkit, unidocs) + .aggregate(core, units, spire, refined, testkit, unidocs) lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform) .crossType(CrossType.Pure) @@ -65,7 +65,10 @@ lazy val refined = crossProject(JVMPlatform, JSPlatform, NativePlatform) .settings(name := "coulomb-refined") .dependsOn(core % "compile->compile;test->test", units % Test) .settings(commonSettings: _*) - .settings(libraryDependencies += "eu.timepit" %%% "refined" % "0.10.1") + .settings( + libraryDependencies += "eu.timepit" %%% "refined" % "0.10.1", + tlVersionIntroduced := Map("3" -> "0.7.2") + ) lazy val testkit = crossProject(JVMPlatform, JSPlatform, NativePlatform) .crossType(CrossType.Pure) diff --git a/refined/src/main/scala/coulomb/conversion/standard/unit.scala b/refined/src/main/scala/coulomb/conversion/standard/unit.scala index f804974e4..de8f7831c 100644 --- a/refined/src/main/scala/coulomb/conversion/standard/unit.scala +++ b/refined/src/main/scala/coulomb/conversion/standard/unit.scala @@ -18,7 +18,7 @@ package coulomb.conversion.refined import scala.util.{Try, Success, Failure} -import coulomb.conversion.* +import coulomb.conversion.* import eu.timepit.refined.* import eu.timepit.refined.api.* @@ -29,8 +29,7 @@ object unit: uc: UnitConversion[V, UF, UT], vld: Validate[V, Positive] ): UnitConversion[Refined[V, Positive], UF, UT] = - (v: Refined[V, Positive]) => - refineV[Positive].unsafeFrom(uc(v.value)) + (v: Refined[V, Positive]) => refineV[Positive].unsafeFrom(uc(v.value)) given ctx_UC_Refined_NonNegative[V, UF, UT](using uc: UnitConversion[V, UF, UT], @@ -43,8 +42,7 @@ object unit: uc: TruncatingUnitConversion[V, UF, UT], vld: Validate[V, Positive] ): TruncatingUnitConversion[Refined[V, Positive], UF, UT] = - (v: Refined[V, Positive]) => - refineV[Positive].unsafeFrom(uc(v.value)) + (v: Refined[V, Positive]) => refineV[Positive].unsafeFrom(uc(v.value)) given ctx_TUC_Refined_NonNegative[V, UF, UT](using uc: TruncatingUnitConversion[V, UF, UT], @@ -57,8 +55,7 @@ object unit: uc: DeltaUnitConversion[V, B, UF, UT], vld: Validate[V, Positive] ): DeltaUnitConversion[Refined[V, Positive], B, UF, UT] = - (v: Refined[V, Positive]) => - refineV[Positive].unsafeFrom(uc(v.value)) + (v: Refined[V, Positive]) => refineV[Positive].unsafeFrom(uc(v.value)) given ctx_DUC_Refined_NonNegative[V, B, UF, UT](using uc: DeltaUnitConversion[V, B, UF, UT], @@ -71,8 +68,7 @@ object unit: uc: TruncatingDeltaUnitConversion[V, B, UF, UT], vld: Validate[V, Positive] ): TruncatingDeltaUnitConversion[Refined[V, Positive], B, UF, UT] = - (v: Refined[V, Positive]) => - refineV[Positive].unsafeFrom(uc(v.value)) + (v: Refined[V, Positive]) => refineV[Positive].unsafeFrom(uc(v.value)) given ctx_TDUC_Refined_NonNegative[V, B, UF, UT](using uc: TruncatingDeltaUnitConversion[V, B, UF, UT], diff --git a/refined/src/main/scala/coulomb/conversion/standard/value.scala b/refined/src/main/scala/coulomb/conversion/standard/value.scala index 20f67ebd1..336dd5bff 100644 --- a/refined/src/main/scala/coulomb/conversion/standard/value.scala +++ b/refined/src/main/scala/coulomb/conversion/standard/value.scala @@ -18,7 +18,7 @@ package coulomb.conversion.refined import scala.util.{Try, Success, Failure} -import coulomb.conversion.* +import coulomb.conversion.* import eu.timepit.refined.* import eu.timepit.refined.api.* @@ -29,8 +29,7 @@ object value: vc: ValueConversion[VF, VT], vld: Validate[VT, Positive] ): ValueConversion[Refined[VF, Positive], Refined[VT, Positive]] = - (v: Refined[VF, Positive]) => - refineV[Positive].unsafeFrom(vc(v.value)) + (v: Refined[VF, Positive]) => refineV[Positive].unsafeFrom(vc(v.value)) given ctx_VC_Refined_NonNegative[VF, VT](using vc: ValueConversion[VF, VT], @@ -43,29 +42,36 @@ object value: vc: TruncatingValueConversion[VF, VT], vld: Validate[VT, Positive] ): TruncatingValueConversion[Refined[VF, Positive], Refined[VT, Positive]] = - (v: Refined[VF, Positive]) => - refineV[Positive].unsafeFrom(vc(v.value)) + (v: Refined[VF, Positive]) => refineV[Positive].unsafeFrom(vc(v.value)) given ctx_TVC_Refined_NonNegative[VF, VT](using vc: TruncatingValueConversion[VF, VT], vld: Validate[VT, NonNegative] - ): TruncatingValueConversion[Refined[VF, NonNegative], Refined[VT, NonNegative]] = + ): TruncatingValueConversion[Refined[VF, NonNegative], Refined[ + VT, + NonNegative + ]] = (v: Refined[VF, NonNegative]) => refineV[NonNegative].unsafeFrom(vc(v.value)) given ctx_VC_Refined_Either[VF, VT, P](using vc: ValueConversion[Refined[VF, P], Refined[VT, P]] - ): ValueConversion[Either[String, Refined[VF, P]], Either[String, Refined[VT, P]]] = + ): ValueConversion[Either[String, Refined[VF, P]], Either[ + String, + Refined[VT, P] + ]] = (v: Either[String, Refined[VF, P]]) => Try(v.map(vc)) match - case Success(x) => x - case Failure(e) => Left(e.getMessage) + case Success(x) => x + case Failure(e) => Left(e.getMessage) given ctx_TVC_Refined_Either[VF, VT, P](using vc: TruncatingValueConversion[Refined[VF, P], Refined[VT, P]] - ): TruncatingValueConversion[Either[String, Refined[VF, P]], Either[String, Refined[VT, P]]] = + ): TruncatingValueConversion[Either[String, Refined[VF, P]], Either[ + String, + Refined[VT, P] + ]] = (v: Either[String, Refined[VF, P]]) => Try(v.map(vc)) match - case Success(x) => x - case Failure(e) => Left(e.getMessage) - + case Success(x) => x + case Failure(e) => Left(e.getMessage) diff --git a/refined/src/main/scala/coulomb/ops/algebra/refined/all.scala b/refined/src/main/scala/coulomb/ops/algebra/refined/all.scala index f390abcb1..d1c7ebc4c 100644 --- a/refined/src/main/scala/coulomb/ops/algebra/refined/all.scala +++ b/refined/src/main/scala/coulomb/ops/algebra/refined/all.scala @@ -79,33 +79,40 @@ object all: new infra.MMRE[V, P] object infra: - class ASGR[V, P](using alg: AdditiveSemigroup[V], vld: Validate[V, P]) extends - AdditiveSemigroup[Refined[V, P]]: + class ASGR[V, P](using alg: AdditiveSemigroup[V], vld: Validate[V, P]) + extends AdditiveSemigroup[Refined[V, P]]: def plus(x: Refined[V, P], y: Refined[V, P]): Refined[V, P] = refineV[P].unsafeFrom(alg.plus(x.value, y.value)) - class MSGR[V, P](using alg: MultiplicativeSemigroup[V], vld: Validate[V, P]) extends - MultiplicativeSemigroup[Refined[V, P]]: + class MSGR[V, P](using + alg: MultiplicativeSemigroup[V], + vld: Validate[V, P] + ) extends MultiplicativeSemigroup[Refined[V, P]]: def times(x: Refined[V, P], y: Refined[V, P]): Refined[V, P] = refineV[P].unsafeFrom(alg.times(x.value, y.value)) - class MMR[V, P](using alg: MultiplicativeMonoid[V], vld: Validate[V, P]) extends - MSGR[V, P] with MultiplicativeMonoid[Refined[V, P]]: + class MMR[V, P](using alg: MultiplicativeMonoid[V], vld: Validate[V, P]) + extends MSGR[V, P] + with MultiplicativeMonoid[Refined[V, P]]: def one: Refined[V, P] = refineV[P].unsafeFrom(alg.one) - class MGR[V, P](using alg: MultiplicativeGroup[V], vld: Validate[V, P]) extends - MMR[V, P] with MultiplicativeGroup[Refined[V, P]]: + class MGR[V, P](using alg: MultiplicativeGroup[V], vld: Validate[V, P]) + extends MMR[V, P] + with MultiplicativeGroup[Refined[V, P]]: def div(x: Refined[V, P], y: Refined[V, P]): Refined[V, P] = refineV[P].unsafeFrom(alg.div(x.value, y.value)) - class FPR[V, P](using alg: FractionalPower[V], vld: Validate[V, P]) extends - FractionalPower[Refined[V, P]]: + class FPR[V, P](using alg: FractionalPower[V], vld: Validate[V, P]) + extends FractionalPower[Refined[V, P]]: def pow(v: Refined[V, P], e: Double): Refined[V, P] = refineV[P].unsafeFrom(alg.pow(v.value, e)) - class ASGRE[V, P](using alg: AdditiveSemigroup[Refined[V, P]]) extends - AdditiveSemigroup[Either[String, Refined[V, P]]]: - def plus(x: Either[String, Refined[V, P]], y: Either[String, Refined[V, P]]) = + class ASGRE[V, P](using alg: AdditiveSemigroup[Refined[V, P]]) + extends AdditiveSemigroup[Either[String, Refined[V, P]]]: + def plus( + x: Either[String, Refined[V, P]], + y: Either[String, Refined[V, P]] + ) = (x, y) match case (Left(xe), Left(ye)) => Left(s"($xe)($ye)") case (Left(xe), Right(_)) => Left(xe) @@ -115,9 +122,12 @@ object all: case Success(z) => Right(z) case Failure(e) => Left(e.getMessage) - class MSGRE[V, P](using alg: MultiplicativeSemigroup[Refined[V, P]]) extends - MultiplicativeSemigroup[Either[String, Refined[V, P]]]: - def times(x: Either[String, Refined[V, P]], y: Either[String, Refined[V, P]]) = + class MSGRE[V, P](using alg: MultiplicativeSemigroup[Refined[V, P]]) + extends MultiplicativeSemigroup[Either[String, Refined[V, P]]]: + def times( + x: Either[String, Refined[V, P]], + y: Either[String, Refined[V, P]] + ) = (x, y) match case (Left(xe), Left(ye)) => Left(s"($xe)($ye)") case (Left(xe), Right(_)) => Left(xe) @@ -127,16 +137,21 @@ object all: case Success(z) => Right(z) case Failure(e) => Left(e.getMessage) - class MMRE[V, P](using alg: MultiplicativeMonoid[Refined[V, P]]) extends - MSGRE[V, P] with MultiplicativeMonoid[Either[String, Refined[V, P]]]: + class MMRE[V, P](using alg: MultiplicativeMonoid[Refined[V, P]]) + extends MSGRE[V, P] + with MultiplicativeMonoid[Either[String, Refined[V, P]]]: def one: Either[String, Refined[V, P]] = Try(alg.one) match case Success(z) => Right(z) case Failure(e) => Left(e.getMessage) - class MGRE[V, P](using alg: MultiplicativeGroup[Refined[V, P]]) extends - MMRE[V, P] with MultiplicativeGroup[Either[String, Refined[V, P]]]: - def div(x: Either[String, Refined[V, P]], y: Either[String, Refined[V, P]]) = + class MGRE[V, P](using alg: MultiplicativeGroup[Refined[V, P]]) + extends MMRE[V, P] + with MultiplicativeGroup[Either[String, Refined[V, P]]]: + def div( + x: Either[String, Refined[V, P]], + y: Either[String, Refined[V, P]] + ) = (x, y) match case (Left(xe), Left(ye)) => Left(s"($xe)($ye)") case (Left(xe), Right(_)) => Left(xe) @@ -146,9 +161,12 @@ object all: case Success(z) => Right(z) case Failure(e) => Left(e.getMessage) - class FPRE[V, P](using alg: FractionalPower[Refined[V, P]]) extends - FractionalPower[Either[String, Refined[V, P]]]: - def pow(v: Either[String, Refined[V, P]], e: Double): Either[String, Refined[V, P]] = + class FPRE[V, P](using alg: FractionalPower[Refined[V, P]]) + extends FractionalPower[Either[String, Refined[V, P]]]: + def pow( + v: Either[String, Refined[V, P]], + e: Double + ): Either[String, Refined[V, P]] = Try(v.map(alg.pow(_, e))) match case Success(z) => z case Failure(e) => Left(e.getMessage) diff --git a/refined/src/main/scala/coulomb/ops/resolution/refined.scala b/refined/src/main/scala/coulomb/ops/resolution/refined.scala index fb96ca221..49ff8d0cd 100644 --- a/refined/src/main/scala/coulomb/ops/resolution/refined.scala +++ b/refined/src/main/scala/coulomb/ops/resolution/refined.scala @@ -25,17 +25,33 @@ import coulomb.ops.ValueResolution import coulomb.policy.priority.* object refined: - transparent inline given ctx_VR_Refined_Positive[VL, VR, Positive](using Prio0)(using + transparent inline given ctx_VR_Refined_Positive[VL, VR, Positive](using + Prio0 + )(using vres: ValueResolution[VL, VR] ): ValueResolution[Refined[VL, Positive], Refined[VR, Positive]] = - new ValueResolution.NC[Refined[VL, Positive], Refined[VR, Positive], Refined[vres.VO, Positive]] + new ValueResolution.NC[Refined[VL, Positive], Refined[ + VR, + Positive + ], Refined[vres.VO, Positive]] - transparent inline given ctx_VR_Refined_NonNegative[VL, VR, NonNegative](using Prio1)(using + transparent inline given ctx_VR_Refined_NonNegative[VL, VR, NonNegative]( + using Prio1 + )(using vres: ValueResolution[VL, VR] ): ValueResolution[Refined[VL, NonNegative], Refined[VR, NonNegative]] = - new ValueResolution.NC[Refined[VL, NonNegative], Refined[VR, NonNegative], Refined[vres.VO, NonNegative]] + new ValueResolution.NC[Refined[VL, NonNegative], Refined[ + VR, + NonNegative + ], Refined[vres.VO, NonNegative]] transparent inline given ctx_VR_Refined_Either[VL, VR, P](using vres: ValueResolution[Refined[VL, P], Refined[VR, P]] - ): ValueResolution[Either[String, Refined[VL, P]], Either[String, Refined[VR, P]]] = - new ValueResolution.NC[Either[String, Refined[VL, P]], Either[String, Refined[VR, P]], Either[String, vres.VO]] + ): ValueResolution[Either[String, Refined[VL, P]], Either[ + String, + Refined[VR, P] + ]] = + new ValueResolution.NC[Either[String, Refined[VL, P]], Either[ + String, + Refined[VR, P] + ], Either[String, vres.VO]] diff --git a/refined/src/main/scala/coulomb/syntax/refined.scala b/refined/src/main/scala/coulomb/syntax/refined.scala index c67aab126..756c5d574 100644 --- a/refined/src/main/scala/coulomb/syntax/refined.scala +++ b/refined/src/main/scala/coulomb/syntax/refined.scala @@ -44,7 +44,7 @@ extension [V](v: V) Validate[V, P] ): Quantity[Either[String, Refined[V, P]], U] = refineV[P](v).withUnit[U] - + object infra: class ApplyRefineVU[P, U]: def apply[V](v: V)(using diff --git a/refined/src/test/scala/coulomb/quantity.scala b/refined/src/test/scala/coulomb/quantity.scala index 402a1e3ea..39a6878e9 100644 --- a/refined/src/test/scala/coulomb/quantity.scala +++ b/refined/src/test/scala/coulomb/quantity.scala @@ -38,17 +38,36 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: type RefinedE[V, P] = Either[String, Refined[V, P]] test("lift") { - 1d.withRP[Positive].withUnit[Meter].assertQ[Refined[Double, Positive], Meter](1d.withRP[Positive]) - 1f.withRP[Positive].withUnit[Meter].assertQ[Refined[Float, Positive], Meter](1f.withRP[Positive]) - 1L.withRP[Positive].withUnit[Meter].assertQ[Refined[Long, Positive], Meter](1L.withRP[Positive]) - 1.withRP[Positive].withUnit[Meter].assertQ[Refined[Int, Positive], Meter](1.withRP[Positive]) + 1d.withRP[Positive] + .withUnit[Meter] + .assertQ[Refined[Double, Positive], Meter](1d.withRP[Positive]) + 1f.withRP[Positive] + .withUnit[Meter] + .assertQ[Refined[Float, Positive], Meter](1f.withRP[Positive]) + 1L.withRP[Positive] + .withUnit[Meter] + .assertQ[Refined[Long, Positive], Meter](1L.withRP[Positive]) + 1.withRP[Positive] + .withUnit[Meter] + .assertQ[Refined[Int, Positive], Meter](1.withRP[Positive]) - 1d.withRP[NonNegative].withUnit[Meter].assertQ[Refined[Double, NonNegative], Meter](1d.withRP[NonNegative]) - 1f.withRP[NonNegative].withUnit[Meter].assertQ[Refined[Float, NonNegative], Meter](1f.withRP[NonNegative]) - 1L.withRP[NonNegative].withUnit[Meter].assertQ[Refined[Long, NonNegative], Meter](1L.withRP[NonNegative]) - 1.withRP[NonNegative].withUnit[Meter].assertQ[Refined[Int, NonNegative], Meter](1.withRP[NonNegative]) + 1d.withRP[NonNegative] + .withUnit[Meter] + .assertQ[Refined[Double, NonNegative], Meter]( + 1d.withRP[NonNegative] + ) + 1f.withRP[NonNegative] + .withUnit[Meter] + .assertQ[Refined[Float, NonNegative], Meter](1f.withRP[NonNegative]) + 1L.withRP[NonNegative] + .withUnit[Meter] + .assertQ[Refined[Long, NonNegative], Meter](1L.withRP[NonNegative]) + 1.withRP[NonNegative] + .withUnit[Meter] + .assertQ[Refined[Int, NonNegative], Meter](1.withRP[NonNegative]) - refineV[Positive](1d).withUnit[Meter] + refineV[Positive](1d) + .withUnit[Meter] .assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](1d)) refineVU[Positive, Meter](1d) .assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](1d)) @@ -57,9 +76,13 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: } test("value") { - 1.withRP[Positive].withUnit[Meter].value.assertVT[Refined[Int, Positive]](1.withRP[Positive]) + 1.withRP[Positive] + .withUnit[Meter] + .value + .assertVT[Refined[Int, Positive]](1.withRP[Positive]) - refineVU[Positive, Meter](1).value.assertVT[RefinedE[Int, Positive]](refineV[Positive](1)) + refineVU[Positive, Meter](1).value + .assertVT[RefinedE[Int, Positive]](refineV[Positive](1)) } test("show") { @@ -68,7 +91,10 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: } test("showFull") { - assertEquals(1.withRP[NonNegative].withUnit[Second].showFull, "1 second") + assertEquals( + 1.withRP[NonNegative].withUnit[Second].showFull, + "1 second" + ) assertEquals(refineVU[Positive, Meter](1).showFull, "Right(1) meter") } @@ -76,68 +102,112 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.strict.given import coulomb.policy.overlay.refined.algebraic.given - 1.withRP[Positive].withUnit[Meter].toValue[Refined[Double, Positive]] + 1.withRP[Positive] + .withUnit[Meter] + .toValue[Refined[Double, Positive]] .assertQ[Refined[Double, Positive], Meter](1d.withRP[Positive]) - 1L.withRP[NonNegative].withUnit[Meter].toValue[Refined[Float, NonNegative]] + 1L.withRP[NonNegative] + .withUnit[Meter] + .toValue[Refined[Float, NonNegative]] .assertQ[Refined[Float, NonNegative], Meter](1f.withRP[NonNegative]) - assertCE("1d.withRP[Positive].withUnit[Meter].toValue[Refined[Int, Positive]]") + assertCE( + "1d.withRP[Positive].withUnit[Meter].toValue[Refined[Int, Positive]]" + ) - 1.5.withRP[Positive].withUnit[Meter].tToValue[Refined[Int, Positive]] + 1.5.withRP[Positive] + .withUnit[Meter] + .tToValue[Refined[Int, Positive]] .assertQ[Refined[Int, Positive], Meter](1.withRP[Positive]) - 1.5f.withRP[NonNegative].withUnit[Meter].tToValue[Refined[Int, NonNegative]] + 1.5f.withRP[NonNegative] + .withUnit[Meter] + .tToValue[Refined[Int, NonNegative]] .assertQ[Refined[Int, NonNegative], Meter](1.withRP[NonNegative]) - refineVU[Positive, Meter](1).toValue[RefinedE[Double, Positive]] + refineVU[Positive, Meter](1) + .toValue[RefinedE[Double, Positive]] .assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](1d)) - assert(refineVU[Positive, Meter](Double.MinPositiveValue) - .toValue[RefinedE[Float, Positive]].value.isLeft) + assert( + refineVU[Positive, Meter](Double.MinPositiveValue) + .toValue[RefinedE[Float, Positive]] + .value + .isLeft + ) } test("toUnit") { import coulomb.policy.strict.given import coulomb.policy.overlay.refined.algebraic.given - 1d.withRP[Positive].withUnit[Kilo * Meter].toUnit[Meter] + 1d.withRP[Positive] + .withUnit[Kilo * Meter] + .toUnit[Meter] .assertQ[Refined[Double, Positive], Meter](1000d.withRP[Positive]) - 1f.withRP[NonNegative].withUnit[Kilo * Meter].toUnit[Meter] - .assertQ[Refined[Float, NonNegative], Meter](1000f.withRP[NonNegative]) + 1f.withRP[NonNegative] + .withUnit[Kilo * Meter] + .toUnit[Meter] + .assertQ[Refined[Float, NonNegative], Meter]( + 1000f.withRP[NonNegative] + ) assertCE("1.withRP[Positive].withUnit[Kilo * Meter].toUnit[Meter]") - 1.withRP[Positive].withUnit[Meter].tToUnit[Yard] + 1.withRP[Positive] + .withUnit[Meter] + .tToUnit[Yard] .assertQ[Refined[Int, Positive], Yard](1.withRP[Positive]) - 1.withRP[NonNegative].withUnit[Meter].tToUnit[Yard] + 1.withRP[NonNegative] + .withUnit[Meter] + .tToUnit[Yard] .assertQ[Refined[Int, NonNegative], Yard](1.withRP[NonNegative]) - refineVU[Positive, Kilo * Meter](1d).toUnit[Meter] - .assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](1000d)) - assert(refineVU[Positive, Meter](Double.MinPositiveValue) - .toUnit[Kilo * Meter].value.isLeft) + refineVU[Positive, Kilo * Meter](1d) + .toUnit[Meter] + .assertQ[RefinedE[Double, Positive], Meter]( + refineV[Positive](1000d) + ) + assert( + refineVU[Positive, Meter](Double.MinPositiveValue) + .toUnit[Kilo * Meter] + .value + .isLeft + ) } test("add strict") { import coulomb.policy.strict.given import coulomb.policy.overlay.refined.algebraic.given - (1d.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]) + (1d.withRP[Positive] + .withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Double, Positive], Meter](3d.withRP[Positive]) - (1.withRP[Positive].withUnit[Meter] + 2.withRP[Positive].withUnit[Meter]) + (1.withRP[Positive] + .withUnit[Meter] + 2.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Int, Positive], Meter](3.withRP[Positive]) - (1f.withRP[NonNegative].withUnit[Meter] + 2f.withRP[NonNegative].withUnit[Meter]) + (1f.withRP[NonNegative] + .withUnit[Meter] + 2f.withRP[NonNegative].withUnit[Meter]) .assertQ[Refined[Float, NonNegative], Meter](3f.withRP[NonNegative]) - (1L.withRP[NonNegative].withUnit[Meter] + 2L.withRP[NonNegative].withUnit[Meter]) + (1L.withRP[NonNegative] + .withUnit[Meter] + 2L.withRP[NonNegative].withUnit[Meter]) .assertQ[Refined[Long, NonNegative], Meter](3L.withRP[NonNegative]) // differing value or unit types are not supported in strict policy - assertCE("1.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]") - assertCE("1d.withRP[NonNegative].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]") - assertCE("1d.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Yard]") + assertCE( + "1.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]" + ) + assertCE( + "1d.withRP[NonNegative].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]" + ) + assertCE( + "1d.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Yard]" + ) val x = refineVU[Positive, Meter](1d) val z = refineVU[Positive, Meter](0d) - (x + x).assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](2d)) + (x + x).assertQ[RefinedE[Double, Positive], Meter]( + refineV[Positive](2d) + ) assert((x + z).value.isLeft) assert((z + x).value.isLeft) assert((z + z).value.isLeft) @@ -153,27 +223,36 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.overlay.refined.algebraic.given // same unit and value type - (1d.withRP[Positive].withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]) + (1d.withRP[Positive] + .withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Double, Positive], Meter](3d.withRP[Positive]) // same unit, differing value types - (1L.withRP[NonNegative].withUnit[Meter] + 2f.withRP[NonNegative].withUnit[Meter]) + (1L.withRP[NonNegative] + .withUnit[Meter] + 2f.withRP[NonNegative].withUnit[Meter]) .assertQ[Refined[Float, NonNegative], Meter](3f.withRP[NonNegative]) // same value, differing units - (1f.withRP[Positive].withUnit[Meter] + 1f.withRP[Positive].withUnit[Kilo * Meter]) + (1f.withRP[Positive] + .withUnit[Meter] + 1f.withRP[Positive].withUnit[Kilo * Meter]) .assertQ[Refined[Float, Positive], Meter](1001f.withRP[Positive]) // value and unit type are different - (1.withRP[Positive].withUnit[Meter] + 1d.withRP[Positive].withUnit[Kilo * Meter]) + (1.withRP[Positive] + .withUnit[Meter] + 1d.withRP[Positive].withUnit[Kilo * Meter]) .assertQ[Refined[Double, Positive], Meter](1001d.withRP[Positive]) - (1f.withRP[NonNegative].withUnit[Meter] + 1L.withRP[NonNegative].withUnit[Kilo * Meter]) - .assertQ[Refined[Float, NonNegative], Meter](1001f.withRP[NonNegative]) + (1f.withRP[NonNegative] + .withUnit[Meter] + 1L.withRP[NonNegative].withUnit[Kilo * Meter]) + .assertQ[Refined[Float, NonNegative], Meter]( + 1001f.withRP[NonNegative] + ) val x = refineVU[Positive, Meter](1d) val y = refineVU[Positive, Kilo * Meter](1) val z = refineVU[Positive, Kilo * Meter](0) - (x + y).assertQ[RefinedE[Double, Positive], Meter](refineV[Positive](1001d)) + (x + y).assertQ[RefinedE[Double, Positive], Meter]( + refineV[Positive](1001d) + ) assert((x + z).value.isLeft) assert((z + x).value.isLeft) assert((z + z).value.isLeft) @@ -183,18 +262,28 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.strict.given import coulomb.policy.overlay.refined.algebraic.given - (2d.withRP[Positive].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]) + (2d.withRP[Positive] + .withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Double, Positive], Meter ^ 2](6d.withRP[Positive]) - (2.withRP[NonNegative].withUnit[Meter] * 3.withRP[NonNegative].withUnit[Meter]) - .assertQ[Refined[Int, NonNegative], Meter ^ 2](6.withRP[NonNegative]) + (2.withRP[NonNegative] + .withUnit[Meter] * 3.withRP[NonNegative].withUnit[Meter]) + .assertQ[Refined[Int, NonNegative], Meter ^ 2]( + 6.withRP[NonNegative] + ) // differing value types are not supported in strict policy - assertCE("2.withRP[Positive].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]") - assertCE("2d.withRP[NonNegative].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]") + assertCE( + "2.withRP[Positive].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]" + ) + assertCE( + "2d.withRP[NonNegative].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]" + ) val x = refineVU[Positive, Meter](2d) val z = refineVU[Positive, Meter](0d) - (x * x).assertQ[RefinedE[Double, Positive], Meter ^ 2](refineV[Positive](4d)) + (x * x).assertQ[RefinedE[Double, Positive], Meter ^ 2]( + refineV[Positive](4d) + ) assert((x * z).value.isLeft) assert((z * x).value.isLeft) assert((z * z).value.isLeft) @@ -209,20 +298,30 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.standard.given import coulomb.policy.overlay.refined.algebraic.given - (2f.withRP[Positive].withUnit[Meter] * 3f.withRP[Positive].withUnit[Meter]) + (2f.withRP[Positive] + .withUnit[Meter] * 3f.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Float, Positive], Meter ^ 2](6f.withRP[Positive]) - (2L.withRP[NonNegative].withUnit[Meter] * 3L.withRP[NonNegative].withUnit[Meter]) - .assertQ[Refined[Long, NonNegative], Meter ^ 2](6L.withRP[NonNegative]) - - (2.withRP[Positive].withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]) + (2L.withRP[NonNegative] + .withUnit[Meter] * 3L.withRP[NonNegative].withUnit[Meter]) + .assertQ[Refined[Long, NonNegative], Meter ^ 2]( + 6L.withRP[NonNegative] + ) + + (2.withRP[Positive] + .withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter]) .assertQ[Refined[Double, Positive], Meter ^ 2](6d.withRP[Positive]) - (2f.withRP[NonNegative].withUnit[Meter] * 3L.withRP[NonNegative].withUnit[Meter]) - .assertQ[Refined[Float, NonNegative], Meter ^ 2](6f.withRP[NonNegative]) + (2f.withRP[NonNegative] + .withUnit[Meter] * 3L.withRP[NonNegative].withUnit[Meter]) + .assertQ[Refined[Float, NonNegative], Meter ^ 2]( + 6f.withRP[NonNegative] + ) val x = refineVU[Positive, Meter](2d) val y = refineVU[Positive, Meter](2) val z = refineVU[Positive, Meter](0) - (x * y).assertQ[RefinedE[Double, Positive], Meter ^ 2](refineV[Positive](4d)) + (x * y).assertQ[RefinedE[Double, Positive], Meter ^ 2]( + refineV[Positive](4d) + ) assert((x * z).value.isLeft) assert((z * x).value.isLeft) assert((z * z).value.isLeft) @@ -232,17 +331,29 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.strict.given import coulomb.policy.overlay.refined.algebraic.given - (12d.withRP[Positive].withUnit[Meter] / 3d.withRP[Positive].withUnit[Second]) - .assertQ[Refined[Double, Positive], Meter / Second](4d.withRP[Positive]) - (12f.withRP[Positive].withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]) - .assertQ[Refined[Float, Positive], Meter / Second](4f.withRP[Positive]) + (12d.withRP[Positive] + .withUnit[Meter] / 3d.withRP[Positive].withUnit[Second]) + .assertQ[Refined[Double, Positive], Meter / Second]( + 4d.withRP[Positive] + ) + (12f.withRP[Positive] + .withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]) + .assertQ[Refined[Float, Positive], Meter / Second]( + 4f.withRP[Positive] + ) // differing values types not supported - assertCE("12d.withRP[Positive].withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]") + assertCE( + "12d.withRP[Positive].withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]" + ) // NonNegative is not multiplicative group - assertCE("12d.withRP[NonNegative].withUnit[Meter] / 3d.withRP[NonNegative].withUnit[Second]") + assertCE( + "12d.withRP[NonNegative].withUnit[Meter] / 3d.withRP[NonNegative].withUnit[Second]" + ) // Integrals are not a multiplicative group - assertCE("12.withRP[Positive].withUnit[Meter] / 3.withRP[Positive].withUnit[Second]") + assertCE( + "12.withRP[Positive].withUnit[Meter] / 3.withRP[Positive].withUnit[Second]" + ) val x = refineVU[Positive, Meter](2d) val z = refineVU[Positive, Meter](0d) @@ -260,18 +371,31 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.standard.given import coulomb.policy.overlay.refined.algebraic.given - (12d.withRP[Positive].withUnit[Meter] / 3d.withRP[Positive].withUnit[Second]) - .assertQ[Refined[Double, Positive], Meter / Second](4d.withRP[Positive]) + (12d.withRP[Positive] + .withUnit[Meter] / 3d.withRP[Positive].withUnit[Second]) + .assertQ[Refined[Double, Positive], Meter / Second]( + 4d.withRP[Positive] + ) + + (12d.withRP[Positive] + .withUnit[Meter] / 3.withRP[Positive].withUnit[Second]) + .assertQ[Refined[Double, Positive], Meter / Second]( + 4d.withRP[Positive] + ) + (12.withRP[Positive] + .withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]) + .assertQ[Refined[Float, Positive], Meter / Second]( + 4f.withRP[Positive] + ) - (12d.withRP[Positive].withUnit[Meter] / 3.withRP[Positive].withUnit[Second]) - .assertQ[Refined[Double, Positive], Meter / Second](4d.withRP[Positive]) - (12.withRP[Positive].withUnit[Meter] / 3f.withRP[Positive].withUnit[Second]) - .assertQ[Refined[Float, Positive], Meter / Second](4f.withRP[Positive]) - // NonNegative is not multiplicative group - assertCE("12d.withRP[NonNegative].withUnit[Meter] / 3d.withRP[NonNegative].withUnit[Second]") + assertCE( + "12d.withRP[NonNegative].withUnit[Meter] / 3d.withRP[NonNegative].withUnit[Second]" + ) // Integrals are not a multiplicative group - assertCE("12.withRP[Positive].withUnit[Meter] / 3.withRP[Positive].withUnit[Second]") + assertCE( + "12.withRP[Positive].withUnit[Meter] / 3.withRP[Positive].withUnit[Second]" + ) val x = refineVU[Positive, Meter](6d) val y = refineVU[Positive, Meter](2) @@ -287,26 +411,45 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite: import coulomb.policy.overlay.refined.algebraic.given // FractionalPower (algebras supporting rational exponents) - 2d.withRP[Positive].withUnit[Meter].pow[0] + 2d.withRP[Positive] + .withUnit[Meter] + .pow[0] .assertQ[Refined[Double, Positive], 1](1d.withRP[Positive]) - 2d.withRP[Positive].withUnit[Meter].pow[2] + 2d.withRP[Positive] + .withUnit[Meter] + .pow[2] .assertQ[Refined[Double, Positive], Meter ^ 2](4d.withRP[Positive]) - 2d.withRP[Positive].withUnit[Meter].pow[-1] - .assertQ[Refined[Double, Positive], 1 / Meter](0.5d.withRP[Positive]) - 4d.withRP[Positive].withUnit[Meter].pow[1 / 2] - .assertQ[Refined[Double, Positive], Meter ^ (1 / 2)](2d.withRP[Positive]) + 2d.withRP[Positive] + .withUnit[Meter] + .pow[-1] + .assertQ[Refined[Double, Positive], 1 / Meter]( + 0.5d.withRP[Positive] + ) + 4d.withRP[Positive] + .withUnit[Meter] + .pow[1 / 2] + .assertQ[Refined[Double, Positive], Meter ^ (1 / 2)]( + 2d.withRP[Positive] + ) // non-negative integer exponents allowed by multiplicative monoid - 2.withRP[Positive].withUnit[Meter].pow[0] + 2.withRP[Positive] + .withUnit[Meter] + .pow[0] .assertQ[Refined[Int, Positive], 1](1.withRP[Positive]) - 2.withRP[Positive].withUnit[Meter].pow[2] + 2.withRP[Positive] + .withUnit[Meter] + .pow[2] .assertQ[Refined[Int, Positive], Meter ^ 2](4.withRP[Positive]) assertCE("2.withRP[Positive].withUnit[Meter].pow[-1]") val x = refineVU[Positive, Meter](2d) val z = refineVU[Positive, Meter](0d) x.pow[0].assertQ[RefinedE[Double, Positive], 1](refineV[Positive](1d)) - x.pow[2].assertQ[RefinedE[Double, Positive], Meter ^ 2](refineV[Positive](4d)) + x.pow[2] + .assertQ[RefinedE[Double, Positive], Meter ^ 2]( + refineV[Positive](4d) + ) assert(z.pow[2].value.isLeft) val v = refineVU[Positive, Meter](Double.MinPositiveValue)