-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Helpers.scala
166 lines (146 loc) · 5.08 KB
/
Helpers.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
/*
* 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.tests
import cats.kernel._
import org.scalacheck.{Arbitrary, Cogen}
import org.scalacheck.Arbitrary.arbitrary
/**
* Helpers provides new concrete types where we control exactly which
* type class instances are available. For example, the SL type has:
*
* - Semilattice[SL]
* - Arbitrary[SL]
* - Eq[SL]
*
* (All types in Helpers have Arbitrary and Eq instances.)
*
* These are useful when a type constructor (e.g. Function0) can
* produce many different instances depending on which instances are
* available for its type parameter.
*/
object Helpers {
abstract class N { def n: Int }
abstract class Arb[E <: N](f: Int => E) {
implicit val earb: Arbitrary[E] = Arbitrary(arbitrary[Int].map(f))
implicit val ccog: Cogen[E] = Cogen[Int].contramap(_.n)
}
trait Q[E] {
implicit val eeq: Eq[E] = Eq.fromUniversalEquals
}
abstract class Companion[E <: N](f: Int => E) extends Arb[E](f) with Q[E]
// Eq
case class Eqed(n: Int) extends N
object Eqed extends Companion(new Eqed(_))
// PartialOrder
case class POrd(n: Int) extends N
object POrd extends Arb(new POrd(_)) {
implicit object O extends PartialOrder[POrd] {
def partialCompare(x: POrd, y: POrd): Double =
if (x.n >= 0 && y.n >= 0) x.n.compare(y.n).toDouble
else if (x.n <= 0 && y.n <= 0) y.n.compare(x.n).toDouble
else Double.NaN
}
}
// Order
case class Ord(n: Int) extends N
object Ord extends Arb(new Ord(_)) {
implicit object O extends Order[Ord] {
def compare(x: Ord, y: Ord): Int = x.n.compare(y.n)
}
}
case class Hsh(n: Int) extends N
object Hsh extends Arb(new Hsh(_)) {
implicit object O extends Hash[Hsh] {
def hash(x: Hsh): Int = x.hashCode()
def eqv(x: Hsh, y: Hsh): Boolean = x.n == y.n
}
}
// Band
case class Bnd(n: Int) extends N
object Bnd extends Companion(new Bnd(_)) {
implicit object Alg extends Band[Bnd] {
def combine(x: Bnd, y: Bnd): Bnd = Bnd(x.n & y.n)
}
}
// Semilattice
case class SL(n: Int) extends N
object SL extends Companion(new SL(_)) {
implicit object Alg extends Semilattice[SL] {
def combine(x: SL, y: SL): SL = SL(x.n & y.n)
}
}
// BoundedSemilattice
case class BSL(n: Int) extends N
object BSL extends Companion(new BSL(_)) {
implicit object Alg extends BoundedSemilattice[BSL] {
def empty: BSL = BSL(0)
def combine(x: BSL, y: BSL): BSL = BSL(x.n | y.n)
}
}
// Semigroup
case class Semi(n: Int) extends N
object Semi extends Companion(new Semi(_)) {
implicit object Alg extends Semigroup[Semi] {
def combine(x: Semi, y: Semi): Semi = Semi(x.n ^ y.n)
}
}
// CommutativeSemigroup
case class CSemi(n: Int) extends N
object CSemi extends Companion(new CSemi(_)) {
implicit object Alg extends CommutativeSemigroup[CSemi] {
def combine(x: CSemi, y: CSemi): CSemi = CSemi(x.n ^ y.n)
}
}
// Monoid
case class Mono(n: Int) extends N
object Mono extends Companion(new Mono(_)) {
implicit object Alg extends Monoid[Mono] {
def empty: Mono = Mono(Int.MaxValue)
def combine(x: Mono, y: Mono): Mono = Mono(x.n.min(y.n))
}
}
// CommutativeMonoid
case class CMono(n: Int) extends N
object CMono extends Companion(new CMono(_)) {
implicit object Alg extends CommutativeMonoid[CMono] {
def empty: CMono = CMono(Int.MaxValue)
def combine(x: CMono, y: CMono): CMono = CMono(x.n.min(y.n))
}
}
// Group
case class Grp(n: Int) extends N
object Grp extends Companion(new Grp(_)) {
implicit object Alg extends Group[Grp] {
def empty: Grp = Grp(0)
def combine(x: Grp, y: Grp): Grp = Grp(x.n + y.n)
def inverse(x: Grp): Grp = Grp(-x.n)
}
}
// CommutativeGroup
case class CGrp(n: Int) extends N
object CGrp extends Companion(new CGrp(_)) {
implicit object Alg extends CommutativeGroup[CGrp] {
def empty: CGrp = CGrp(0)
def combine(x: CGrp, y: CGrp): CGrp = CGrp(x.n + y.n)
def inverse(x: CGrp): CGrp = CGrp(-x.n)
}
}
}