-
Notifications
You must be signed in to change notification settings - Fork 156
/
numeric.scala
183 lines (149 loc) · 6.19 KB
/
numeric.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
/**
* Module for numeric predicates. Predicates that take type parameters
* support both shapeless' natural numbers (`Nat`) and numeric singleton
* types (which are made available by shapeless' `Witness` - abbreviated
* as `[[W]]` in refined) which include subtypes of `Int`, `Long`,
* `Double`, `Char` etc.
*
* Example: {{{
* scala> import eu.timepit.refined.api.Refined
* | import eu.timepit.refined.numeric.Greater
* | import shapeless.nat._5
*
* scala> refineMV[Greater[_5]](10)
* res1: Int Refined Greater[_5] = 10
*
* scala> refineMV[Greater[W.`1.5`.T]](1.6)
* res2: Double Refined Greater[W.`1.5`.T] = 1.6
* }}}
*
* Note: `[[generic.Equal]]` can also be used for numeric types.
*/
object numeric extends NumericValidate with NumericInference {
/** Predicate that checks if a numeric value is less than `N`. */
final case class Less[N](n: N)
/** Predicate that checks if a numeric value is greater than `N`. */
final case class Greater[N](n: N)
/** Predicate that checks if a numeric value modulo `N` is `O`. */
final case class Modulo[N, O](n: N, o: O)
/** Predicate that checks if a numeric value is less than or equal to `N`. */
type LessEqual[N] = Not[Greater[N]]
/** Predicate that checks if a numeric value is greater than or equal to `N`. */
type GreaterEqual[N] = Not[Less[N]]
/** Predicate that checks if a numeric value is positive (> 0). */
type Positive = Greater[_0]
/** Predicate that checks if a numeric value is zero or negative (<= 0). */
type NonPositive = Not[Positive]
/** Predicate that checks if a numeric value is negative (< 0). */
type Negative = Less[_0]
/** Predicate that checks if a numeric value is zero or positive (>= 0). */
type NonNegative = Not[Negative]
/** Predicate that checks if a numeric value is evenly divisible by `N`. */
type Divisible[N] = Modulo[N, _0]
/** Predicate that checks if a numeric value is not evenly divisible by `N`. */
type NonDivisible[N] = Not[Divisible[N]]
/** Predicate that checks if a numeric value is evenly divisible by 2. */
type Even = Divisible[_2]
/** Predicate that checks if a numeric value is not evenly divisible by 2. */
type Odd = Not[Even]
object Interval {
/** Predicate that checks if a numeric value is in the interval `(L, H)`. */
type Open[L, H] = Greater[L] And Less[H]
/** Predicate that checks if a numeric value is in the interval `(L, H]`. */
type OpenClosed[L, H] = Greater[L] And LessEqual[H]
/** Predicate that checks if a numeric value is in the interval `[L, H)`. */
type ClosedOpen[L, H] = GreaterEqual[L] And Less[H]
/** Predicate that checks if a numeric value is in the interval `[L, H]`. */
type Closed[L, H] = GreaterEqual[L] And LessEqual[H]
}
}
private[refined] trait NumericValidate {
implicit def lessValidateWit[T, N <: T](
implicit wn: Witness.Aux[N],
nt: Numeric[T]
): Validate.Plain[T, Less[N]] =
Validate.fromPredicate(t => nt.lt(t, wn.value), t => s"($t < ${wn.value})", Less(wn.value))
implicit def greaterValidateWit[T, N <: T](
implicit wn: Witness.Aux[N],
nt: Numeric[T]
): Validate.Plain[T, Greater[N]] =
Validate.fromPredicate(t => nt.gt(t, wn.value), t => s"($t > ${wn.value})", Greater(wn.value))
implicit def moduloValidateWit[T, N <: T, O <: T](
implicit wn: Witness.Aux[N],
wo: Witness.Aux[O],
nt: Numeric[T]
): Validate.Plain[T, Modulo[N, O]] =
Validate.fromPredicate(
t ⇒ nt.toDouble(t) % nt.toDouble(wn.value) == nt.toDouble(wo.value),
t ⇒ s"($t % ${wn.value} == ${wo.value})",
Modulo(wn.value, wo.value)
)
implicit def lessValidateNat[N <: Nat, T](
implicit tn: ToInt[N],
wn: Witness.Aux[N],
nt: Numeric[T]
): Validate.Plain[T, Less[N]] =
Validate.fromPredicate(t => nt.toDouble(t) < tn(), t => s"($t < ${tn()})", Less(wn.value))
implicit def greaterValidateNat[N <: Nat, T](
implicit tn: ToInt[N],
wn: Witness.Aux[N],
nt: Numeric[T]
): Validate.Plain[T, Greater[N]] =
Validate.fromPredicate(t => nt.toDouble(t) > tn(), t => s"($t > ${tn()})", Greater(wn.value))
implicit def moduloValidateNat[N <: Nat, O <: Nat, T](
implicit tn: ToInt[N],
to: ToInt[O],
wn: Witness.Aux[N],
wo: Witness.Aux[O],
nt: Numeric[T]
): Validate.Plain[T, Modulo[N, O]] =
Validate.fromPredicate(
t ⇒ nt.toDouble(t) % tn() == to(),
t ⇒ s"($t % ${tn()} == ${to()})",
Modulo(wn.value, wo.value)
)
}
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()})")
}