-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJustified.scala
549 lines (472 loc) · 18.8 KB
/
Justified.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.rallyhealth.vapors.v1
package data
import algebra.{EqualComparable, SizeComparable}
import dsl._
import lens.{DataPath, VariantLens}
import logic.Logic
import math._
import time.CountTime
import cats.data.{NonEmptySeq, NonEmptySet}
import cats.implicits._
import cats.{Applicative, Eq, Eval, Foldable, Order, Semigroupal, Traverse}
import scala.annotation.nowarn
import scala.collection.Factory
import scala.util.matching.Regex
/**
* Represents a result that contains a tree of justified inputs and operations, as well as the value
* for each operation along the way.
*
* Values can be justified by:
* - Constants embedded into the expression itself
* - Config values that are embedded into the expression but have a known configuration key / description
* - Facts from the [[FactTable]] used to compute this output
* - Inference by an operation defined by the [[dsl.JustifiedBuildExprDsl]] with references to all the
* justified values used as inputs to the operation.
*
* By following the chain of justification, one can determine if there is any factual evidence for a value,
* or whether it is only supported by configs and constants (i.e. it is a default value and not one that is
* tailored based on the provided facts).
*
* @tparam V the type of value that is justified by this container
*/
sealed trait Justified[+V] extends Product {
/**
* The output value from the expression.
*/
def value: V
/**
* Keep the same justification, but adjust the lens.
*
* Use this with caution. It should only be used in situations where the value is an obvious isomorphic
* transformation, such as a translation between a List and Vector, a value class and its value, a
* known subtype / supertype, a temporary storage for a wrapper type that is later unwrapped, etc.
*/
def withView[U >: V, A](buildLens: VariantLens.FromTo[U, A]): Justified[A] = {
val thatLens = buildLens(VariantLens.id[U])
val thatValue = thatLens.get(value)
if (thatLens.path.isEmpty && thatValue == (this.value: Any)) this.asInstanceOf[Justified[A]]
else
this match {
case Justified.BySelection(_, thisPath, thisSource) =>
Justified.BySelection(thatValue, thisPath ++ thatLens.path, thisSource)
case _ =>
Justified.BySelection(thatValue, thatLens.path, this)
}
}
/**
* A short description for how the value is justified. Either a "const", "config", "fact", or the name of
* an operation based on other justified values.
*/
def reason: String
/**
* Collects all the configuration keys used to produce this [[value]]
*/
def configs: Seq[(String, Option[String])] // TODO: How should this handle duplicate keys?
/**
* Collects all the [[Fact]]s used to produce this [[value]]
*/
def evidence: Evidence
/**
* Visit the tree of justified values with a tagless-final visitor.
*/
def visit[G[+_]](v: Justified.Visitor[G]): G[V]
/**
* Justify the result of an operation by inference given the other justified value, a reason, and a function
* defining the operation.
*/
def zipWith[Y, Z](
that: Justified[Y],
reason: String,
)(
fn: (V, Y) => Z,
): Justified[Z] =
Justified.ByInference(
reason,
fn(this.value, that.value),
NonEmptySeq.of(this, that),
)
}
object Justified extends LowPriorityJustifiedImplicits {
trait Visitor[G[+_]] {
def visitConstant[V](justified: ByConst[V]): G[V]
def visitConfig[V](justified: ByConfig[V]): G[V]
def visitFact[V](justified: ByFact[V]): G[V]
def visitInference[V](justified: ByInference[V]): G[V]
def visitSelection[V](justified: BySelection[V]): G[V]
}
def byConst[V](value: V): Justified[V] = ByConst(value)
final case class ByConst[+V](value: V) extends Justified[V] {
override def productPrefix: String = "Justified.ByConst"
override val reason: String = s"[const: $value]"
override def configs: Seq[(String, Option[String])] = Vector.empty
override def evidence: NoEvidence.type = NoEvidence
override def visit[G[+_]](v: Visitor[G]): G[V] = v.visitConstant(this)
}
def byConfig[V](
value: V,
configKey: String,
configDescription: Option[String] = None,
): Justified[V] = ByConfig(value, configKey, configDescription)
final case class ByConfig[+V](
value: V,
configKey: String,
configDescription: Option[String] = None,
) extends Justified[V] {
override def productPrefix: String = "Justified.ByConfig"
override val reason: String = s"[config: $configKey=$value]"
override def configs: Seq[(String, Option[String])] = Vector(configKey -> configDescription)
override def evidence: NoEvidence.type = NoEvidence
override def visit[G[+_]](v: Visitor[G]): G[V] = v.visitConfig(this)
}
def byFact[V](fact: TypedFact[V]): Justified[V] = ByFact(fact)
final case class ByFact[V](fact: TypedFact[V]) extends Justified[V] {
override def productPrefix: String = "Justified.ByFact"
override val reason: String = s"[fact: ${fact.typeInfo.name}=${fact.value}]"
override def value: V = fact.value
override def configs: Seq[(String, Option[String])] = Vector.empty
override val evidence: SomeEvidence = SomeEvidence(NonEmptySet.of(fact))
override def visit[G[+_]](v: Visitor[G]): G[V] = v.visitFact(this)
}
def bySelection[V](
value: V,
path: DataPath,
source: Justified[Any],
): Justified[V] = BySelection(value, path, source)
final case class BySelection[+V](
value: V,
path: DataPath,
source: Justified[Any],
) extends Justified[V] {
override def productPrefix: String = "Justified.BySelection"
override val reason: String = s"[select: _${path.asString}]"
override def configs: Seq[(String, Option[String])] = source.configs
override def evidence: Evidence = source.evidence
override def visit[G[+_]](v: Visitor[G]): G[V] = v.visitSelection(this)
}
def byInference[V](
reason: String,
value: V,
sources: NonEmptySeq[Justified[Any]],
): Justified[V] = ByInference(reason, value, sources)
final case class ByInference[+V](
reason: String,
value: V,
sources: NonEmptySeq[Justified[Any]],
) extends Justified[V] {
override def productPrefix: String = "Justified.ByInference"
override lazy val configs: Seq[(String, Option[String])] =
sources.map(_.configs).reduce // TODO: avoid clobbering duplicate keys
override lazy val evidence: Evidence =
sources.foldLeft(Evidence.none)(_ | _.evidence) // TODO: Is this even valid?
override def visit[G[+_]](v: Visitor[G]): G[V] = v.visitInference(this)
}
def elements[C[_] : Traverse, A](collection: Justified[C[A]]): C[Justified[A]] = {
collection.value.mapWithIndex { (a, idx) =>
Justified.bySelection(a, DataPath.empty.atIndex(idx), collection)
}
}
def elements[C[a] <: Iterable[a], A](
collection: Justified[C[A]],
)(implicit
factory: Factory[Justified[A], C[Justified[A]]],
): C[Justified[A]] = {
collection.value.zipWithIndex
.map {
case (a, idx) =>
Justified.bySelection(a, DataPath.empty.atIndex(idx), collection)
}
.to(factory)
}
implicit def eq[V : Eq, OP[_]]: EqualComparable[Justified, V, OP] =
new EqualComparable[Justified, V, OP] {
override def isEqual(
left: Justified[V],
right: Justified[V],
)(implicit
opV: OP[Justified[V]],
opO: OP[Justified[Boolean]],
): Justified[Boolean] = {
val isEqual = Eq[V].eqv(left.value, right.value)
Justified.byInference("isEqual", isEqual, NonEmptySeq(left, Vector(right)))
}
}
implicit def orderingByValue[V : Ordering]: Ordering[Justified[V]] = Ordering.by(_.value)
implicit def orderByValue[V : Order]: Order[Justified[V]] = Order.by(_.value)
implicit def countTime[T, U, O](
implicit
underlying: CountTime[T, U, O],
): CountTime[Justified[T], Justified[U], Justified[O]] = { (start, end, truncateToUnit) =>
Justified.byInference(
"date_diff",
underlying.between(start.value, end.value, truncateToUnit.value),
NonEmptySeq.of(start, end, truncateToUnit),
)
}
private final case object ExtractJustified extends Extract[Justified] {
override def extract[A](fa: Justified[A]): A = fa.value
}
implicit def extract: Extract[Justified] = ExtractJustified
implicit def wrapSizeComparedIterable[C[a] <: Iterable[a], A, N, B](
implicit
sizeComparable: SizeComparable[C[Justified[A]], N, B],
): SizeComparable[C[Justified[A]], Justified[N], Justified[B]] = wrapSizeCompared(_.size)
private final case object WrapConstJustified extends WrapConst[Justified, Any] {
override def wrapConst[A](value: A)(implicit opA: Any): Justified[A] = Justified.byConst(value)
}
implicit def wrapConst[OP[_]]: WrapConst[Justified, OP] = WrapConstJustified.asInstanceOf[WrapConst[Justified, OP]]
private final case object WrapFactJustified extends WrapFact[Justified, Any] {
override def wrapFact[O](fact: TypedFact[O])(implicit opO: Any): Justified[O] = Justified.byFact(fact)
}
implicit def wrapContained[OP[_]]: WrapContained[Justified, OP] =
WrapContainedJustified.asInstanceOf[WrapContained[Justified, OP]]
private final case object WrapContainedJustified extends WrapContained[Justified, Any] {
override def wrapContained[C[_] : Foldable, V](
original: C[Justified[V]],
valid: Set[Justified[V]],
found: Seq[Justified[V]],
)(implicit
opV: Any,
): Justified[Boolean] = {
// TODO: Figure out how to avoid the Justified.byConst situation.
// One option is to wrap every collection as well as the elements to provide traceability for empty collections
val justifiedValidValues = NonEmptySeq
.fromSeq(valid.toSeq)
.map { nonEmpty =>
Justified.byInference("validValues", valid.map(_.value), nonEmpty)
}
.getOrElse {
Justified.byConst(Set())
}
val justifiedElements = NonEmptySeq
.fromSeq(found)
.map { nonEmpty =>
Justified.byInference("foundElements", found.map(_.value), nonEmpty)
}
.getOrElse {
Justified.byConst(Seq())
}
Justified.byInference("contains", found.nonEmpty, NonEmptySeq.of(justifiedValidValues, justifiedElements))
}
}
implicit def wrapFact[OP[_]]: WrapFact[Justified, OP] = WrapFactJustified.asInstanceOf[WrapFact[Justified, OP]]
private final case object WrapSelectedJustified extends WrapSelected[Justified, Any] {
override def wrapSelected[I, O](
container: Justified[I],
path: DataPath,
element: O,
)(implicit
opB: Any,
): Justified[O] = {
container.withView((_: Any) => VariantLens(path, (_: Any) => element))
}
}
implicit def wrapRegexMatches[OP[_]]: WrapRegexMatches[Justified, OP] =
WrapRegexMatchesJustified.asInstanceOf[WrapRegexMatches[Justified, OP]]
private final object WrapRegexMatchesJustified extends WrapRegexMatches[Justified, Any] {
override def wrapMatched[O](
in: Justified[String],
out: O,
pattern: Regex,
matches: LazyList[RegexMatch],
): Justified[O] =
Justified.byInference(
"regex_matches",
out,
NonEmptySeq.of(
in,
Justified.byConst(pattern),
),
)
}
implicit def wrapSelected[OP[_]]: WrapSelected[Justified, OP] =
WrapSelectedJustified.asInstanceOf[WrapSelected[Justified, OP]]
private final object WrapQuantifierJustified extends WrapQuantifier[Justified, Any] {
// TODO: Pull this from config somehow?
override def shortCircuit: Boolean = false
override def wrapFalseForAll(
falseResults: NonEmptySeq[Justified[Boolean]],
)(implicit
opB: Any,
): Justified[Boolean] =
Justified.byInference("forall", false, falseResults)
override def wrapTrueForAll(trueResults: Seq[Justified[Boolean]])(implicit opB: Any): Justified[Boolean] = {
NonEmptySeq
.fromSeq(trueResults)
.map { justified =>
Justified.byInference("forall", true, justified)
}
.getOrElse {
// forall in an empty collection is true
// TODO: Should I put a reason instead of just a const?
Justified.byConst(true)
}
}
override def wrapFalseExists(falseResults: Seq[Justified[Boolean]])(implicit opB: Any): Justified[Boolean] = {
NonEmptySeq
.fromSeq(falseResults)
.map { justified =>
Justified.byInference("exists", false, justified)
}
.getOrElse {
// exists in an empty collection is false
// TODO: Should I put a reason instead of just a const?
// Maybe I should pass the original value in these functions?
Justified.byConst(false)
}
}
override def wrapTrueExists(trueResults: NonEmptySeq[Justified[Boolean]])(implicit opB: Any): Justified[Boolean] =
Justified.byInference("exists", true, trueResults)
}
implicit def wrapQuantifier[OP[_]]: WrapQuantifier[Justified, OP] =
WrapQuantifierJustified.asInstanceOf[WrapQuantifier[Justified, OP]]
implicit val semigroupal: Semigroupal[Justified] = new Semigroupal[Justified] {
override def product[A, B](
fa: Justified[A],
fb: Justified[B],
): Justified[(A, B)] = {
Justified.byInference("product", (fa.value, fb.value), NonEmptySeq(fa, Vector(fb)))
}
}
implicit val traverse: Traverse[Justified] = new Traverse[Justified] {
override def map[A, B](fa: Justified[A])(f: A => B): Justified[B] = {
val derived = f(fa.value)
Justified.byInference("map", derived, NonEmptySeq.of(fa))
}
override def traverse[G[_] : Applicative, A, B](fa: Justified[A])(f: A => G[B]): G[Justified[B]] = {
val gb = f(fa.value)
Applicative[G].map(gb) { b =>
Justified.byInference("traverse", b, NonEmptySeq.of(fa))
}
}
override def foldLeft[A, B](
fa: Justified[A],
b: B,
)(
f: (B, A) => B,
): B =
f(b, fa.value)
override def foldRight[A, B](
fa: Justified[A],
lb: Eval[B],
)(
f: (A, Eval[B]) => Eval[B],
): Eval[B] =
f(fa.value, lb)
}
private val anyBool = new BooleanLogic[Boolean](identity)
implicit def bool[OP[_]]: Logic[Justified, Boolean, OP] = anyBool.asInstanceOf[Logic[Justified, Boolean, OP]]
final class BooleanLogic[B : ExtractValue.AsBoolean](fromBoolean: Boolean => B) extends Logic[Justified, B, Any] {
override def and(
left: Justified[B],
right: Justified[B],
)(implicit
opB: Any,
): Justified[B] = {
val outcome = left.value && right.value
Justified.byInference("and", fromBoolean(outcome), NonEmptySeq(left, Vector(right)))
}
override def or(
left: Justified[B],
right: Justified[B],
)(implicit
opB: Any,
): Justified[B] = {
val outcome = left.value || right.value
Justified.byInference("or", fromBoolean(outcome), NonEmptySeq(left, Vector(right)))
}
override def not(value: Justified[B])(implicit opB: Any): Justified[B] = {
val outcome = !value.value
Justified.byInference("not", fromBoolean(outcome), NonEmptySeq.of(value))
}
}
implicit def add[L, R, O](implicit add0: Add.Aux[L, R, O]): Add.Aux[Justified[L], Justified[R], Justified[O]] = {
new Add[Justified[L], Justified[R]] {
override type Out = Justified[O]
def add(
left: Justified[L],
right: Justified[R],
): Justified[O] = {
left.zipWith(right, "add")(add0.add(_, _): @nowarn)
}
}
}
implicit def subtract[L, R, O](
implicit
sub0: Subtract.Aux[L, R, O],
): Subtract.Aux[Justified[L], Justified[R], Justified[O]] = {
new Subtract[Justified[L], Justified[R]] {
override type Out = Justified[O]
def subtract(
left: Justified[L],
right: Justified[R],
): Justified[O] = {
left.zipWith(right, "subtract")(sub0.subtract(_, _): @nowarn)
}
}
}
implicit def multiply[L, R, O](
implicit
mult0: Multiply.Aux[L, R, O],
): Multiply.Aux[Justified[L], Justified[R], Justified[O]] =
new Multiply[Justified[L], Justified[R]] {
override type Out = Justified[O]
override def multiply(
left: Justified[L],
right: Justified[R],
): Justified[O] = left.zipWith(right, "multiply")(mult0.multiply(_, _): @nowarn)
}
implicit def divide[N, D, O](
implicit
div0: Divide.Aux[N, D, O],
): Divide.Aux[Justified[N], Justified[D], Justified[O]] =
new Divide[Justified[N], Justified[D]] {
override type Out = Justified[O]
override def divide(
numerator: Justified[N],
denominator: Justified[D],
): Justified[O] = numerator.zipWith(denominator, "divide")(div0.divide(_, _): @nowarn)
}
implicit def power[B, E, O](
implicit
pow0: Power.Aux[B, E, O],
): Power.Aux[Justified[B], Justified[E], Justified[O]] =
new Power[Justified[B], Justified[E]] {
override type Out = Justified[O]
override def power(
base: Justified[B],
exponent: Justified[E],
): Justified[O] = base.zipWith(exponent, "power")(pow0.power(_, _): @nowarn)
}
}
sealed class LowPriorityJustifiedImplicits {
protected def wrapSizeCompared[C[_], A, N, B](
getSize: C[Justified[A]] => Long,
)(implicit
sizeComparable: SizeComparable[C[Justified[A]], N, B],
): SizeComparable[C[Justified[A]], Justified[N], Justified[B]] = { (collection, comparison, comparedTo) =>
val actualSize = getSize(collection)
val result = sizeComparable.sizeCompare(collection, comparison, comparedTo.value)
Justified.byInference(
s"sizeIs ${comparison.symbol} ${comparedTo.value}",
result,
NonEmptySeq.of(
// TODO: This is not really a "const", but rather an inference, but we have lost the reference to the original
// wrapped collection. We could try to retrace the Justified tree from the first element, but what if
// the collection is empty? What if the justification for the element comes from a different collection
// than the other elements of the collection? Do we need to unify the justification of all the elements
// and have a special case when the collection is empty?
//
// One potential (but invasive) solution to this problem would be to always retain the wrapper of the entire
// collection and perform all operations on the types inside the wrapper (rather than strip the wrapper when
// operating on collections, as we do now). We should explore this option before the 1.0.0 release.
Justified.byConst(actualSize),
Justified.byConst(comparison),
comparedTo,
),
)
}
implicit def wrapSizeComparedFoldable[C[_] : Foldable, A, N, B](
implicit
sizeComparable: SizeComparable[C[Justified[A]], N, B],
): SizeComparable[C[Justified[A]], Justified[N], Justified[B]] = wrapSizeCompared(_.size)
}