forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernelBoiler.scala
423 lines (400 loc) · 18.8 KB
/
KernelBoiler.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import KernelBoiler.TemplateVals
import sbt._
/**
* Generate a range of boilerplate classes that would be tedious to write and maintain by hand.
*
* Copied, with some modifications, from
* [[https://github.com/milessabin/shapeless/blob/master/project/Boilerplate.scala Shapeless]].
*
* @author Miles Sabin
* @author Kevin Wright
*/
object KernelBoiler {
import scala.StringContext._
implicit final class BlockHelper(private val sc: StringContext) extends AnyVal {
def block(args: Any*): String = {
val interpolated = sc.standardInterpolator(treatEscapes, args)
val rawLines = interpolated.split('\n')
val trimmedLines = rawLines.map(_.dropWhile(_.isWhitespace))
trimmedLines.map(_.dropWhile(_ == '|')).mkString("\n")
}
}
val templates: Seq[Template] = Seq(GenTupleInstances)
val header = "// auto-generated boilerplate"
val maxArity = 22
/**
* Return a sequence of the generated files.
*
* As a side-effect, it actually generates them...
*/
def gen(dir: File): Seq[File] =
templates.map { template =>
val tgtFile = template.filename(dir)
IO.write(tgtFile, template.body)
tgtFile
}
class TemplateVals(val arity: Int) {
val synTypes = (0 until arity).map(n => s"A$n")
val synVals = (0 until arity).map(n => s"a$n")
val `A..N` = synTypes.mkString(", ")
val `a..n` = synVals.mkString(", ")
val `_.._` = Seq.fill(arity)("_").mkString(", ")
val `(A..N)` = if (arity == 1) "Tuple1[A0]" else synTypes.mkString("(", ", ", ")")
val `(_.._)` = if (arity == 1) "Tuple1[_]" else Seq.fill(arity)("_").mkString("(", ", ", ")")
val `(a..n)` = if (arity == 1) "Tuple1(a)" else synVals.mkString("(", ", ", ")")
}
/**
* Blocks in the templates below use a custom interpolator, combined with post-processing to
* produce the body.
*
* - The contents of the `header` val is output first
* - Then the first block of lines beginning with '|'
* - Then the block of lines beginning with '-' is replicated once for each arity,
* with the `templateVals` already pre-populated with relevant relevant vals for that arity
* - Then the last block of lines prefixed with '|'
*
* The block otherwise behaves as a standard interpolated string with regards to variable
* substitution.
*/
trait Template {
def filename(root: File): File
def preBody: String
def instances: Seq[InstanceDef]
def range: IndexedSeq[Int] = 1 to maxArity
def body: String = {
val headerLines = header.split('\n')
val tvs = range.map(n => new TemplateVals(n))
(headerLines ++ Seq(preBody) ++ instances.flatMap(_.body(tvs))).mkString("\n")
}
}
final case class InstanceDef(start: String, methods: TemplateVals => TemplatedBlock, end: String = "}") {
def body(tvs: Seq[TemplateVals]): Seq[String] = Seq(start) ++ tvs.map(methods(_).content) ++ Seq(end)
}
abstract class TemplatedBlock(tv: TemplateVals) {
import tv._
def constraints(constraint: String) =
synTypes.map(tpe => s"${tpe}: ${constraint}[${tpe}]").mkString(", ")
def tuple(results: TraversableOnce[String]) = {
val resultsVec = results.toVector
val a = synTypes.size
val r = s"${0.until(a).map(i => resultsVec(i)).mkString(", ")}"
if (a == 1) "Tuple1(" ++ r ++ ")"
else s"(${r})"
}
def tupleNHeader = s"Tuple${synTypes.size}"
def binMethod(name: String) =
synTypes.zipWithIndex.iterator.map { case (tpe, i) =>
val j = i + 1
s"${tpe}.${name}(x._${j}, y._${j})"
}
def binTuple(name: String) =
tuple(binMethod(name))
def unaryTuple(name: String) = {
val m = synTypes.zipWithIndex.map { case (tpe, i) => s"${tpe}.${name}(x._${i + 1})" }
tuple(m)
}
def unaryMethod(name: String) =
synTypes.zipWithIndex.iterator.map { case (tpe, i) =>
s"$tpe.$name(x._${i + 1})"
}
def nullaryTuple(name: String) = {
val m = synTypes.map(tpe => s"${tpe}.${name}")
tuple(m)
}
def content: String
}
object GenTupleInstances extends Template {
override def range: IndexedSeq[Int] = 1 to maxArity
def filename(root: File): File = root / "cats" / "kernel" / "instances" / "TupleInstances.scala"
val preBody: String =
block"""
package cats.kernel
package instances"""
def instances: Seq[InstanceDef] =
Seq(
InstanceDef(
block"""
trait TupleInstances extends TupleInstances1 {""",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""
| implicit def catsKernelStdCommutativeGroupForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeGroup"
)}): CommutativeGroup[${`(A..N)`}] =
| new CommutativeGroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| def inverse(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("inverse")}
| }
| implicit def catsKernelStdOrderForTuple${arity}[${`A..N`}](implicit ${constraints("Order")}): Order[${`(A..N)`}] =
| new Order[${`(A..N)`}] {
| def compare(x: ${`(A..N)`}, y: ${`(A..N)`}): Int =
| ${binMethod("compare").mkString("Array(", ", ", ")")}.find(_ != 0).getOrElse(0)
| }
| implicit def catsKernelStdBoundedSemilatticeForTuple${arity}[${`A..N`}](implicit ${constraints(
"BoundedSemilattice"
)}): BoundedSemilattice[${`(A..N)`}] =
| new BoundedSemilattice[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }"""
}
),
InstanceDef(
block"""
private[instances] trait TupleInstances1 extends TupleInstances2 {""",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""
| implicit def catsKernelStdSemilatticeForTuple${arity}[${`A..N`}](implicit ${constraints(
"Semilattice"
)}): Semilattice[${`(A..N)`}] =
| new Semilattice[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }
| implicit def catsKernelStdCommutativeMonoidForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeMonoid"
)}): CommutativeMonoid[${`(A..N)`}] =
| new CommutativeMonoid[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }
| implicit def catsKernelStdGroupForTuple${arity}[${`A..N`}](implicit ${constraints("Group")}): Group[${`(A..N)`}] =
| new Group[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| def inverse(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("inverse")}
| }
| implicit def catsKernelStdHashForTuple${arity}[${`A..N`}](implicit ${constraints("Hash")}): Hash[${`(A..N)`}] =
| new Hash[${`(A..N)`}] {
| def hash(x: ${`(A..N)`}): Int = ${unaryMethod("hash")
.mkString(s"$tupleNHeader(", ", ", ")")}.hashCode()
| def eqv(x: ${`(A..N)`}, y: ${`(A..N)`}): Boolean = ${binMethod("eqv").mkString(" && ")}
| }
| implicit def catsKernelStdPartialOrderForTuple${arity}[${`A..N`}](implicit ${constraints(
"PartialOrder"
)}): PartialOrder[${`(A..N)`}] =
| new PartialOrder[${`(A..N)`}] {
| def partialCompare(x: ${`(A..N)`}, y: ${`(A..N)`}): Double =
| ${binMethod("partialCompare").mkString("Array(", ", ", ")")}.find(_ != 0.0).getOrElse(0.0)
| }"""
}
),
InstanceDef(
block"""
private[instances] trait TupleInstances2 extends TupleInstances3 {""",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""
| implicit def catsKernelStdBandForTuple${arity}[${`A..N`}](implicit ${constraints("Band")}): Band[${`(A..N)`}] =
| new Band[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }
| implicit def catsKernelStdCommutativeSemigroupForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeSemigroup"
)}): CommutativeSemigroup[${`(A..N)`}] =
| new CommutativeSemigroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }
| implicit def catsKernelStdMonoidForTuple${arity}[${`A..N`}](implicit ${constraints("Monoid")}): Monoid[${`(A..N)`}] =
| new Monoid[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }"""
}
),
InstanceDef(
block"""
private[instances] trait TupleInstances3 {""",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""
| implicit def catsKernelStdSemigroupForTuple${arity}[${`A..N`}](implicit ${constraints("Semigroup")}): Semigroup[${`(A..N)`}] =
| new Semigroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }
| implicit def catsKernelStdEqForTuple${arity}[${`A..N`}](implicit ${constraints("Eq")}): Eq[${`(A..N)`}] =
| new Eq[${`(A..N)`}] {
| def eqv(x: ${`(A..N)`}, y: ${`(A..N)`}): Boolean = ${binMethod("eqv").mkString(" && ")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleBandInstances extends TupleSemigroupInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelBandForTuple${arity}[${`A..N`}](implicit ${constraints("Band")}): Band[${`(A..N)`}] = new Band[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleBoundedSemilatticeInstances extends TupleGroupInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelBoundedSemilatticeForTuple${arity}[${`A..N`}](implicit ${constraints(
"BoundedSemilattice"
)}): BoundedSemilattice[${`(A..N)`}] = new BoundedSemilattice[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleCommutativeGroupInstances extends TupleBoundedSemilatticeInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelCommutativeGroupForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeGroup"
)}): CommutativeGroup[${`(A..N)`}] = new CommutativeGroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| def inverse(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("inverse")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleCommutativeMonoidInstances extends TupleSemilatticeInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelCommutativeMonoidForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeMonoid"
)}): CommutativeMonoid[${`(A..N)`}] = new CommutativeMonoid[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleCommutativeSemigroupInstances extends TupleBandInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelCommutativeSemigroupForTuple${arity}[${`A..N`}](implicit ${constraints(
"CommutativeSemigroup"
)}): CommutativeSemigroup[${`(A..N)`}] = new CommutativeSemigroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleEqInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelEqForTuple${arity}[${`A..N`}](implicit ${constraints("Eq")}): Eq[${`(A..N)`}] = new Eq[${`(A..N)`}] {
| def eqv(x: ${`(A..N)`}, y: ${`(A..N)`}): Boolean = ${binMethod("eqv").mkString(" && ")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleGroupInstances extends TupleCommutativeMonoidInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelGroupForTuple${arity}[${`A..N`}](implicit ${constraints("Group")}): Group[${`(A..N)`}] = new Group[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| def inverse(x: ${`(A..N)`}): ${`(A..N)`} = ${unaryTuple("inverse")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleHashInstances extends TupleEqInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelHashForTuple${arity}[${`A..N`}](implicit ${constraints("Hash")}): Hash[${`(A..N)`}] = new Hash[${`(A..N)`}] {
| def hash(x: ${`(A..N)`}): Int = ${unaryMethod("hash")
.mkString(s"$tupleNHeader(", ", ", ")")}.hashCode()
| def eqv(x: ${`(A..N)`}, y: ${`(A..N)`}): Boolean = ${binMethod("eqv").mkString(" && ")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleMonoidInstances extends TupleCommutativeSemigroupInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelMonoidForTuple${arity}[${`A..N`}](implicit ${constraints("Monoid")}): Monoid[${`(A..N)`}] = new Monoid[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| def empty: ${`(A..N)`} = ${nullaryTuple("empty")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleOrderInstances extends TuplePartialOrderInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelOrderForTuple${arity}[${`A..N`}](implicit ${constraints("Order")}): Order[${`(A..N)`}] = new Order[${`(A..N)`}] {
| def compare(x: ${`(A..N)`}, y: ${`(A..N)`}): Int =
| ${binMethod("compare").mkString("Array(", ", ", ")")}.find(_ != 0).getOrElse(0)
| }"""
}
),
InstanceDef(
"private[kernel] trait TuplePartialOrderInstances extends TupleHashInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelPartialOrderForTuple${arity}[${`A..N`}](implicit ${constraints(
"PartialOrder"
)}): PartialOrder[${`(A..N)`}] = new PartialOrder[${`(A..N)`}] {
| def partialCompare(x: ${`(A..N)`}, y: ${`(A..N)`}): Double =
| ${binMethod("partialCompare").mkString("Array(", ", ", ")")}.find(_ != 0.0).getOrElse(0.0)
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleSemigroupInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelSemigroupForTuple${arity}[${`A..N`}](implicit ${constraints(
"Semigroup"
)}): Semigroup[${`(A..N)`}] = new Semigroup[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }"""
}
),
InstanceDef(
"private[kernel] trait TupleSemilatticeInstances extends TupleMonoidInstances {",
tv =>
new TemplatedBlock(tv) {
import tv._
def content =
block"""| implicit def catsKernelSemilatticeForTuple${arity}[${`A..N`}](implicit ${constraints(
"Semilattice"
)}): Semilattice[${`(A..N)`}] = new Semilattice[${`(A..N)`}] {
| def combine(x: ${`(A..N)`}, y: ${`(A..N)`}): ${`(A..N)`} = ${binTuple("combine")}
| }"""
}
)
)
}
}