Skip to content

Commit

Permalink
Convert NumericInference
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Feb 8, 2018
1 parent 7a3c712 commit 3c546f6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ lazy val moduleJvmSettings = Def.settings(
ProblemFilters.exclude[MissingClassProblem]("eu.timepit.refined.GenericValidate"),
ProblemFilters.exclude[MissingTypesProblem]("eu.timepit.refined.generic$*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("eu.timepit.refined.numeric.*"),
ProblemFilters.exclude[MissingClassProblem]("eu.timepit.refined.NumericInference"),
ProblemFilters.exclude[MissingClassProblem]("eu.timepit.refined.NumericValidate"),
ProblemFilters.exclude[MissingTypesProblem]("eu.timepit.refined.numeric*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("eu.timepit.refined.string.*"),
Expand Down
86 changes: 41 additions & 45 deletions modules/core/shared/src/main/scala/eu/timepit/refined/numeric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package eu.timepit.refined
import eu.timepit.refined.api.{Inference, Validate}
import eu.timepit.refined.api.Inference.==>
import eu.timepit.refined.boolean.{And, Not}
import eu.timepit.refined.numeric._
import shapeless.{Nat, Witness}
import shapeless.nat.{_0, _2}
import shapeless.ops.nat.ToInt
Expand All @@ -29,7 +28,7 @@ import shapeless.ops.nat.ToInt
*
* Note: `[[generic.Equal]]` can also be used for numeric types.
*/
object numeric extends NumericInference {
object numeric {

/** Predicate that checks if a numeric value is less than `N`. */
final case class Less[N](n: N)
Expand Down Expand Up @@ -98,6 +97,26 @@ object numeric extends NumericInference {
nt: Numeric[T]
): Validate.Plain[T, Less[N]] =
Validate.fromPredicate(t => nt.toDouble(t) < tn(), t => s"($t < ${tn()})", Less(wn.value))

implicit def lessInferenceWit[C, A <: C, B <: C](
implicit wa: Witness.Aux[A],
wb: Witness.Aux[B],
nc: Numeric[C]
): Less[A] ==> Less[B] =
Inference(nc.lt(wa.value, wb.value), s"lessInferenceWit(${wa.value}, ${wb.value})")

implicit def lessInferenceNat[A <: Nat, B <: Nat](
implicit ta: ToInt[A],
tb: ToInt[B]
): Less[A] ==> Less[B] =
Inference(ta() < tb(), s"lessInferenceNat(${ta()}, ${tb()})")

implicit def lessInferenceWitNat[C, A <: C, B <: Nat](
implicit wa: Witness.Aux[A],
tb: ToInt[B],
nc: Numeric[C]
): Less[A] ==> Less[B] =
Inference(nc.lt(wa.value, nc.fromInt(tb())), s"lessInferenceWitNat(${wa.value}, ${tb()})")
}

object Greater {
Expand All @@ -113,6 +132,26 @@ object numeric extends NumericInference {
nt: Numeric[T]
): Validate.Plain[T, Greater[N]] =
Validate.fromPredicate(t => nt.toDouble(t) > tn(), t => s"($t > ${tn()})", Greater(wn.value))

implicit def greaterInferenceWit[C, A <: C, B <: C](
implicit wa: Witness.Aux[A],
wb: Witness.Aux[B],
nc: Numeric[C]
): Greater[A] ==> Greater[B] =
Inference(nc.gt(wa.value, wb.value), s"greaterInferenceWit(${wa.value}, ${wb.value})")

implicit def greaterInferenceNat[A <: Nat, B <: Nat](
implicit ta: ToInt[A],
tb: ToInt[B]
): Greater[A] ==> Greater[B] =
Inference(ta() > tb(), s"greaterInferenceNat(${ta()}, ${tb()})")

implicit def greaterInferenceWitNat[C, A <: C, B <: Nat](
implicit wa: Witness.Aux[A],
tb: ToInt[B],
nc: Numeric[C]
): Greater[A] ==> Greater[B] =
Inference(nc.gt(wa.value, nc.fromInt(tb())), s"greaterInferenceWitNat(${wa.value}, ${tb()})")
}

object Modulo {
Expand Down Expand Up @@ -153,46 +192,3 @@ object numeric extends NumericInference {
)
}
}

private[refined] trait NumericInference {

implicit def lessInferenceWit[C, A <: C, B <: C](
implicit wa: Witness.Aux[A],
wb: Witness.Aux[B],
nc: Numeric[C]
): Less[A] ==> Less[B] =
Inference(nc.lt(wa.value, wb.value), s"lessInferenceWit(${wa.value}, ${wb.value})")

implicit def greaterInferenceWit[C, A <: C, B <: C](
implicit wa: Witness.Aux[A],
wb: Witness.Aux[B],
nc: Numeric[C]
): Greater[A] ==> Greater[B] =
Inference(nc.gt(wa.value, wb.value), s"greaterInferenceWit(${wa.value}, ${wb.value})")

implicit def lessInferenceNat[A <: Nat, B <: Nat](
implicit ta: ToInt[A],
tb: ToInt[B]
): Less[A] ==> Less[B] =
Inference(ta() < tb(), s"lessInferenceNat(${ta()}, ${tb()})")

implicit def greaterInferenceNat[A <: Nat, B <: Nat](
implicit ta: ToInt[A],
tb: ToInt[B]
): Greater[A] ==> Greater[B] =
Inference(ta() > tb(), s"greaterInferenceNat(${ta()}, ${tb()})")

implicit def lessInferenceWitNat[C, A <: C, B <: Nat](
implicit wa: Witness.Aux[A],
tb: ToInt[B],
nc: Numeric[C]
): Less[A] ==> Less[B] =
Inference(nc.lt(wa.value, nc.fromInt(tb())), s"lessInferenceWitNat(${wa.value}, ${tb()})")

implicit def greaterInferenceWitNat[C, A <: C, B <: Nat](
implicit wa: Witness.Aux[A],
tb: ToInt[B],
nc: Numeric[C]
): Greater[A] ==> Greater[B] =
Inference(nc.gt(wa.value, nc.fromInt(tb())), s"greaterInferenceWitNat(${wa.value}, ${tb()})")
}

0 comments on commit 3c546f6

Please sign in to comment.