/
disjunct.go
588 lines (509 loc) · 15.5 KB
/
disjunct.go
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adt
import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
)
// Nodes man not reenter a disjunction.
//
// Copy one layer deep; throw away items on failure.
// DISJUNCTION ALGORITHM
//
// The basic concept of the algorithm is to use backtracking to find valid
// disjunctions. The algorithm can stop if two matching disjuncts are found
// where one does not subsume the other.
//
// At a later point, we can introduce a filter step to filter out possible
// disjuncts based on, say, discriminator fields or field exclusivity (oneOf
// fields in Protobuf).
//
// To understand the details of the algorithm, it is important to understand
// some properties of disjunction.
//
//
// EVALUATION OF A DISJUNCTION IS SELF CONTAINED
//
// In other words, fields outside of a disjunction cannot bind to values within
// a disjunction whilst evaluating that disjunction. This allows the computation
// of disjunctions to be isolated from side effects.
//
// The intuition behind this is as follows: as a disjunction is not a concrete
// value, it is not possible to lookup a field within a disjunction if it has
// not yet been evaluated. So if a reference within a disjunction that is needed
// to disambiguate that disjunction refers to a field outside the scope of the
// disjunction which, in turn, refers to a field within the disjunction, this
// results in a cycle error. We achieve this by not removing the cycle marker of
// the Vertex of the disjunction until the disjunction is resolved.
//
// Note that the following disjunct is still allowed:
//
// a: 1
// b: a
//
// Even though `a` refers to the root of the disjunction, it does not _select
// into_ the disjunction. Implementation-wise, it also doesn't have to, as the
// respective vertex is available within the Environment. Referencing a node
// outside the disjunction that in turn selects the disjunction root, however,
// will result in a detected cycle.
//
// As usual, cycle detection should be interpreted marked as incomplete, so that
// the referring node will not be fixed to an error prematurely.
//
//
// SUBSUMPTION OF AMBIGUOUS DISJUNCTS
//
// A disjunction can be evaluated to a concrete value if only one disjunct
// remains. Aside from disambiguating through unification failure, disjuncts
// may also be disambiguated by taking the least specific of two disjuncts.
// For instance, if a subsumes b, then the result of disjunction may be a.
//
// NEW ALGORITHM NO LONGER VERIFIES SUBSUMPTION. SUBSUMPTION IS INHERENTLY
// IMPRECISE (DUE TO BULK OPTIONAL FIELDS). OTHER THAN THAT, FOR SCALAR VALUES
// IT JUST MEANS THERE IS AMBIGUITY, AND FOR STRUCTS IT CAN LEAD TO STRANGE
// CONSEQUENCES.
//
// USE EQUALITY INSTEAD:
// - Undefined == error for optional fields.
// - So only need to check exact labels for vertices.
type envDisjunct struct {
env *Environment
cloneID CloseInfo
expr *DisjunctionExpr
value *Disjunction
hasDefaults bool
// These are used for book keeping, tracking whether any of the
// disjuncts marked with a default marker remains after unification.
// If no default is used, all other elements are treated as "maybeDefault".
// Otherwise, elements are treated as is.
parentDefaultUsed bool
childDefaultUsed bool
}
func (n *nodeContext) addDisjunction(env *Environment, x *DisjunctionExpr, cloneID CloseInfo) {
// TODO: precompute
numDefaults := 0
for _, v := range x.Values {
isDef := v.Default // || n.hasDefaults(env, v.Val)
if isDef {
numDefaults++
}
}
n.disjunctions = append(n.disjunctions,
envDisjunct{env, cloneID, x, nil, numDefaults > 0, false, false})
}
func (n *nodeContext) addDisjunctionValue(env *Environment, x *Disjunction, cloneID CloseInfo) {
n.disjunctions = append(n.disjunctions,
envDisjunct{env, cloneID, nil, x, x.HasDefaults, false, false})
}
func (n *nodeContext) expandDisjuncts(
state VertexStatus,
parent *nodeContext,
parentMode defaultMode, // default mode of this disjunct
recursive, last bool) {
n.ctx.stats.DisjunctCount++
node := n.node
defer func() {
n.node = node
}()
for n.expandOne() {
}
// save node to snapShot in nodeContex
// save nodeContext.
if recursive || len(n.disjunctions) > 0 {
n.snapshot = clone(*n.node)
} else {
n.snapshot = *n.node
}
defaultOffset := len(n.usedDefault)
switch {
default: // len(n.disjunctions) == 0
m := *n
n.postDisjunct(state)
switch {
case n.hasErr():
// TODO: consider finalizing the node thusly:
// if recursive {
// n.node.Finalize(n.ctx)
// }
x := n.node
err, ok := x.BaseValue.(*Bottom)
if !ok {
err = n.getErr()
}
if err == nil {
// TODO(disjuncts): Is this always correct? Especially for partial
// evaluation it is okay for child errors to have incomplete errors.
// Perhaps introduce an Err() method.
err = x.ChildErrors
}
if err.IsIncomplete() {
break
}
if err != nil {
parent.disjunctErrs = append(parent.disjunctErrs, err)
}
if recursive {
n.free()
}
return
}
if recursive {
*n = m
n.result = *n.node // XXX: n.result = snapshotVertex(n.node)?
n.node = &n.result
n.disjuncts = append(n.disjuncts, n)
}
if n.node.BaseValue == nil {
n.node.BaseValue = n.getValidators()
}
n.usedDefault = append(n.usedDefault, defaultInfo{
parentMode: parentMode,
nestedMode: parentMode,
origMode: parentMode,
})
case len(n.disjunctions) > 0:
// Process full disjuncts to ensure that erroneous disjuncts are
// eliminated as early as possible.
state = Finalized
n.disjuncts = append(n.disjuncts, n)
n.refCount++
defer n.free()
for i, d := range n.disjunctions {
a := n.disjuncts
n.disjuncts = n.buffer[:0]
n.buffer = a[:0]
last := i+1 == len(n.disjunctions)
skipNonMonotonicChecks := i+1 < len(n.disjunctions)
if skipNonMonotonicChecks {
n.ctx.inDisjunct++
}
for _, dn := range a {
switch {
case d.expr != nil:
for _, v := range d.expr.Values {
cn := dn.clone()
*cn.node = clone(dn.snapshot)
cn.node.state = cn
c := MakeConjunct(d.env, v.Val, d.cloneID)
cn.addExprConjunct(c)
newMode := mode(d.hasDefaults, v.Default)
cn.expandDisjuncts(state, n, newMode, true, last)
}
case d.value != nil:
for i, v := range d.value.Values {
cn := dn.clone()
*cn.node = clone(dn.snapshot)
cn.node.state = cn
cn.addValueConjunct(d.env, v, d.cloneID)
newMode := mode(d.hasDefaults, i < d.value.NumDefaults)
cn.expandDisjuncts(state, n, newMode, true, last)
}
}
}
if skipNonMonotonicChecks {
n.ctx.inDisjunct--
}
if len(n.disjuncts) == 0 {
n.makeError()
}
if recursive || i > 0 {
for _, x := range a {
x.free()
}
}
if len(n.disjuncts) == 0 {
break
}
}
// Annotate disjunctions with whether any of the default disjunctions
// was used.
for _, d := range n.disjuncts {
for i, info := range d.usedDefault[defaultOffset:] {
if info.parentMode == isDefault {
n.disjunctions[i].parentDefaultUsed = true
}
if info.origMode == isDefault {
n.disjunctions[i].childDefaultUsed = true
}
}
}
// Combine parent and child default markers, considering that a parent
// "notDefault" is treated as "maybeDefault" if none of the disjuncts
// marked as default remain.
//
// NOTE for a parent marked as "notDefault", a child is *never*
// considered as default. It may either be "not" or "maybe" default.
//
// The result for each disjunction is conjoined into a single value.
for _, d := range n.disjuncts {
m := maybeDefault
orig := maybeDefault
for i, info := range d.usedDefault[defaultOffset:] {
parent := info.parentMode
used := n.disjunctions[i].parentDefaultUsed
childUsed := n.disjunctions[i].childDefaultUsed
hasDefaults := n.disjunctions[i].hasDefaults
orig = combineDefault(orig, info.parentMode)
orig = combineDefault(orig, info.nestedMode)
switch {
case childUsed:
// One of the children used a default. This is "normal"
// mode. This may also happen when we are in
// hasDefaults/notUsed mode. Consider
//
// ("a" | "b") & (*(*"a" | string) | string)
//
// Here the doubly nested default is called twice, once
// for "a" and then for "b", where the second resolves to
// not using a default. The first does, however, and on that
// basis the "ot default marker cannot be overridden.
m = combineDefault(m, info.parentMode)
m = combineDefault(m, info.origMode)
case !hasDefaults, used:
m = combineDefault(m, info.parentMode)
m = combineDefault(m, info.nestedMode)
case hasDefaults && !used:
Assertf(parent == notDefault, "unexpected default mode")
}
}
d.defaultMode = m
d.usedDefault = d.usedDefault[:defaultOffset]
d.usedDefault = append(d.usedDefault, defaultInfo{
parentMode: parentMode,
nestedMode: m,
origMode: orig,
})
}
// TODO: this is an old trick that seems no longer necessary for the new
// implementation. Keep around until we finalize the semantics for
// defaults, though. The recursion of nested defaults is not entirely
// proper yet.
//
// A better approach, that avoids the need for recursion (semantically),
// would be to only consider default usage for one level, but then to
// also allow a default to be passed if only one value is remaining.
// This means that a nested subsumption would first have to be evaluated
// in isolation, however, to determine that it is not previous
// disjunctions that cause the disambiguation.
//
// HACK alert: this replaces the hack of the previous algorithm with a
// slightly less worse hack: instead of dropping the default info when
// the value was scalar before, we drop this information when there is
// only one disjunct, while not discarding hard defaults. TODO: a more
// principled approach would be to recognize that there is only one
// default at a point where this does not break commutativity. if
// if len(n.disjuncts) == 1 && n.disjuncts[0].defaultMode != isDefault {
// n.disjuncts[0].defaultMode = maybeDefault
// }
}
// Compare to root, but add to this one.
switch p := parent; {
case p != n:
p.disjunctErrs = append(p.disjunctErrs, n.disjunctErrs...)
n.disjunctErrs = n.disjunctErrs[:0]
outer:
for _, d := range n.disjuncts {
for k, v := range p.disjuncts {
if !d.done() || !v.done() {
break
}
flags := CheckStructural
if last {
flags |= IgnoreOptional
}
if Equal(n.ctx, &v.result, &d.result, flags) {
m := maybeDefault
for _, u := range d.usedDefault {
m = combineDefault(m, u.nestedMode)
}
if m == isDefault {
p.disjuncts[k] = d
v.free()
} else {
d.free()
}
continue outer
}
}
p.disjuncts = append(p.disjuncts, d)
}
n.disjuncts = n.disjuncts[:0]
}
}
func (n *nodeContext) makeError() {
code := IncompleteError
if len(n.disjunctErrs) > 0 {
code = EvalError
for _, c := range n.disjunctErrs {
if c.Code > code {
code = c.Code
}
}
}
b := &Bottom{
Code: code,
Err: n.disjunctError(),
}
n.node.SetValue(n.ctx, Finalized, b)
}
func mode(hasDefault, marked bool) defaultMode {
var mode defaultMode
switch {
case !hasDefault:
mode = maybeDefault
case marked:
mode = isDefault
default:
mode = notDefault
}
return mode
}
// clone makes a shallow copy of a Vertex. The purpose is to create different
// disjuncts from the same Vertex under computation. This allows the conjuncts
// of an arc to be reset to a previous position and the reuse of earlier
// computations.
//
// Notes: only Arcs need to be copied recursively. Either the arc is finalized
// and can be used as is, or Structs is assumed to not yet be computed at the
// time that a clone is needed and must be nil. Conjuncts no longer needed and
// can become nil. All other fields can be copied shallowly.
func clone(v Vertex) Vertex {
v.state = nil
if a := v.Arcs; len(a) > 0 {
v.Arcs = make([]*Vertex, len(a))
for i, arc := range a {
switch arc.status {
case Finalized:
v.Arcs[i] = arc
case 0:
a := *arc
v.Arcs[i] = &a
a.Conjuncts = make([]Conjunct, len(arc.Conjuncts))
copy(a.Conjuncts, arc.Conjuncts)
default:
a := *arc
a.state = arc.state.clone()
a.state.node = &a
a.state.snapshot = clone(a)
v.Arcs[i] = &a
}
}
}
if a := v.Structs; len(a) > 0 {
v.Structs = make([]*StructInfo, len(a))
copy(v.Structs, a)
}
return v
}
// Default rules from spec:
//
// U1: (v1, d1) & v2 => (v1&v2, d1&v2)
// U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2)
//
// D1: (v1, d1) | v2 => (v1|v2, d1)
// D2: (v1, d1) | (v2, d2) => (v1|v2, d1|d2)
//
// M1: *v => (v, v)
// M2: *(v1, d1) => (v1, d1)
//
// NOTE: M2 cannot be *(v1, d1) => (v1, v1), as this has the weird property
// of making a value less specific. This causes issues, for instance, when
// trimming.
//
// The old implementation does something similar though. It will discard
// default information after first determining if more than one conjunct
// has survived.
//
// def + maybe -> def
// not + maybe -> def
// not + def -> def
type defaultMode int
const (
maybeDefault defaultMode = iota
isDefault
notDefault
)
// combineDefaults combines default modes for unifying conjuncts.
//
// Default rules from spec:
//
// U1: (v1, d1) & v2 => (v1&v2, d1&v2)
// U2: (v1, d1) & (v2, d2) => (v1&v2, d1&d2)
func combineDefault(a, b defaultMode) defaultMode {
if a > b {
return a
}
return b
}
// disjunctError returns a compound error for a failed disjunction.
//
// TODO(perf): the set of errors is now computed during evaluation. Eventually,
// this could be done lazily.
func (n *nodeContext) disjunctError() (errs errors.Error) {
ctx := n.ctx
disjuncts := selectErrors(n.disjunctErrs)
if disjuncts == nil {
errs = ctx.Newf("empty disjunction") // XXX: add space to sort first
} else {
disjuncts = errors.Sanitize(disjuncts)
k := len(errors.Errors(disjuncts))
// prefix '-' to sort to top
errs = ctx.Newf("%d errors in empty disjunction:", k)
}
errs = errors.Append(errs, disjuncts)
return errs
}
func selectErrors(a []*Bottom) (errs errors.Error) {
// return all errors if less than a certain number.
if len(a) <= 2 {
for _, b := range a {
errs = errors.Append(errs, b.Err)
}
return errs
}
// First select only relevant errors.
isIncomplete := false
k := 0
for _, b := range a {
if !isIncomplete && b.Code >= IncompleteError {
k = 0
isIncomplete = true
}
a[k] = b
k++
}
a = a[:k]
// filter errors
positions := map[token.Pos]bool{}
add := func(b *Bottom, p token.Pos) bool {
if positions[p] {
return false
}
positions[p] = true
errs = errors.Append(errs, b.Err)
return true
}
for _, b := range a {
// TODO: Should we also distinguish by message type?
if add(b, b.Err.Position()) {
continue
}
for _, p := range b.Err.InputPositions() {
if add(b, p) {
break
}
}
}
return errs
}