/
PartialOrder.scala
188 lines (163 loc) · 6.13 KB
/
PartialOrder.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
184
185
186
187
188
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cats.kernel
import java.lang.Double.isNaN
import scala.{specialized => sp}
import compat.scalaVersionSpecific._
/**
* The `PartialOrder` type class is used to define a partial ordering on some type `A`.
*
* A partial order is defined by a relation <=, which obeys the following laws:
*
* - x <= x (reflexivity)
* - if x <= y and y <= x, then x = y (anti-symmetry)
* - if x <= y and y <= z, then x <= z (transitivity)
*
* To compute both <= and >= at the same time, we use a Double number
* to encode the result of the comparisons x <= y and x >= y.
* The truth table is defined as follows:
*
* | x <= y | x >= y | result | note |
* | :-- | :-- | --: | :-- |
* | true |true | 0.0 | (corresponds to x = y) |
* | false |false | NaN | (x and y cannot be compared) |
* | true |false | -1.0 | (corresponds to x < y) |
* | false |true | 1.0 | (corresponds to x > y) |
*/
trait PartialOrder[@sp A] extends Any with Eq[A] { self =>
/**
* Result of comparing `x` with `y`. Returns NaN if operands are not
* comparable. If operands are comparable, returns a Double whose
* sign is:
*
* - negative iff `x < y`
* - zero iff `x = y`
* - positive iff `x > y`
*/
def partialCompare(x: A, y: A): Double
/**
* Like `partialCompare`, but returns a [[cats.kernel.Comparison]] instead of an Double.
* Has the benefit of being able to pattern match on, but not as performant.
*/
def partialComparison(x: A, y: A): Option[Comparison] =
Comparison.fromDouble(partialCompare(x, y))
/**
* Result of comparing `x` with `y`. Returns None if operands are
* not comparable. If operands are comparable, returns Some[Int]
* where the Int sign is:
*
* - negative iff `x < y`
* - zero iff `x = y`
* - positive iff `x > y`
*/
def tryCompare(x: A, y: A): Option[Int] = {
val c = partialCompare(x, y).sign
if (isNaN(c)) None else Some(c.toInt)
}
/**
* Returns Some(x) if x <= y, Some(y) if x > y, otherwise None.
*/
def pmin(x: A, y: A): Option[A] = {
val c = partialCompare(x, y)
if (c <= 0) Some(x)
else if (c > 0) Some(y)
else None
}
/**
* Returns Some(x) if x >= y, Some(y) if x < y, otherwise None.
*/
def pmax(x: A, y: A): Option[A] = {
val c = partialCompare(x, y)
if (c >= 0) Some(x)
else if (c < 0) Some(y)
else None
}
// The following may be overridden for performance:
/**
* Returns true if `x` = `y`, false otherwise.
*/
def eqv(x: A, y: A): Boolean = partialCompare(x, y) == 0
/**
* Returns true if `x` <= `y`, false otherwise.
*/
def lteqv(x: A, y: A): Boolean = partialCompare(x, y) <= 0
/**
* Returns true if `x` < `y`, false otherwise.
*/
def lt(x: A, y: A): Boolean = partialCompare(x, y) < 0
/**
* Returns true if `x` >= `y`, false otherwise.
*/
def gteqv(x: A, y: A): Boolean = partialCompare(x, y) >= 0
/**
* Returns true if `x` > `y`, false otherwise.
*/
def gt(x: A, y: A): Boolean = partialCompare(x, y) > 0
}
abstract class PartialOrderFunctions[P[T] <: PartialOrder[T]] extends EqFunctions[P] {
def partialCompare[@sp A](x: A, y: A)(implicit ev: P[A]): Double =
ev.partialCompare(x, y)
def tryCompare[@sp A](x: A, y: A)(implicit ev: P[A]): Option[Int] =
ev.tryCompare(x, y)
def pmin[@sp A](x: A, y: A)(implicit ev: P[A]): Option[A] =
ev.pmin(x, y)
def pmax[@sp A](x: A, y: A)(implicit ev: P[A]): Option[A] =
ev.pmax(x, y)
def lteqv[@sp A](x: A, y: A)(implicit ev: P[A]): Boolean =
ev.lteqv(x, y)
def lt[@sp A](x: A, y: A)(implicit ev: P[A]): Boolean =
ev.lt(x, y)
def gteqv[@sp A](x: A, y: A)(implicit ev: P[A]): Boolean =
ev.gteqv(x, y)
def gt[@sp A](x: A, y: A)(implicit ev: P[A]): Boolean =
ev.gt(x, y)
}
@suppressUnusedImportWarningForScalaVersionSpecific
object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrderToPartialOrderingConversion {
/**
* Access an implicit `PartialOrder[A]`.
*/
@inline final def apply[A](implicit ev: PartialOrder[A]): PartialOrder[A] = ev
/**
* Convert an implicit `PartialOrder[B]` to an `PartialOrder[A]` using the given
* function `f`.
*/
def by[@sp A, @sp B](f: A => B)(implicit ev: PartialOrder[B]): PartialOrder[A] =
(x, y) => ev.partialCompare(f(x), f(y))
/**
* Defines a partial order on `A` from p where all arrows switch direction.
*/
def reverse[@sp A](p: PartialOrder[A]): PartialOrder[A] =
(x, y) => p.partialCompare(y, x)
/**
* Define a `PartialOrder[A]` using the given function `f`.
*/
def from[@sp A](f: (A, A) => Double): PartialOrder[A] = f(_, _)
def fromPartialOrdering[A](implicit ev: PartialOrdering[A]): PartialOrder[A] =
ev.tryCompare(_, _).fold(Double.NaN)(_.toDouble)
}
trait PartialOrderToPartialOrderingConversion {
implicit def catsKernelPartialOrderingForPartialOrder[A](implicit ev: PartialOrder[A]): PartialOrdering[A] =
new PartialOrdering[A] {
def tryCompare(x: A, y: A): Option[Int] = ev.tryCompare(x, y)
def lteq(x: A, y: A): Boolean = ev.lteqv(x, y)
}
}