Skip to content

Commit

Permalink
Add refined module to root (#417)
Browse files Browse the repository at this point in the history
* Add refined module to root

* Formatting

* Add formatting commit to `.git-blame-ignore-revs`
  • Loading branch information
armanbilge authored Jan 10, 2023
1 parent 0f9607d commit c33bc78
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 138 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Scala Steward: Reformat with scalafmt 3.6.1
5a32ba1fe51ead22fc92c76e3394ee6dcccc00a5

# Format refined
804037b709af9fa40c135b8e8c9f44dbcd4119a1
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions refined/src/main/scala/coulomb/conversion/standard/unit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand Down
32 changes: 19 additions & 13 deletions refined/src/main/scala/coulomb/conversion/standard/value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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],
Expand All @@ -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)
66 changes: 42 additions & 24 deletions refined/src/main/scala/coulomb/ops/algebra/refined/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
28 changes: 22 additions & 6 deletions refined/src/main/scala/coulomb/ops/resolution/refined.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
2 changes: 1 addition & 1 deletion refined/src/main/scala/coulomb/syntax/refined.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit c33bc78

Please sign in to comment.