Skip to content

Commit

Permalink
internal/core: extract sorted features from Vertex
Browse files Browse the repository at this point in the history
Given:

```
a: {
  z: 4
  y: 3
}
b: {
  x: 2
  w: 1
}
```

I want the following behaviour:

```
c1: a & b
c2: b & a
```

c1 and c2 should have fields in the same order. I.e. order of arguments
to unification does not matter (essential for associativity).

```
d1: {
  a
  b
}
d2: {
  b
  a
}
```

d1 and d2 should not have fields in the same order. I.e. order of
embedding dictates order of fields in output.

```
e1: {
  zz: 14
  a&b
  t: 13
}
e2: {
  zz: 14
  b&a
  t: 13
}
```

e1 and e2 should have fields in the same order, both with zz first and t
last. This means we need to understand the embedding of expressions, We
also need to be able to add edges between nodes from plain fields and
nodes from embedded structs.

```
f: {
  g: b | a
  h: a | b
}
```

The expansion of the values of g and h should match the order of the
branches in the disjunctions.

```
z: d: _
z: c: _
z: {
  b: _
  f: _
}
```

Within z, the output fields should be ordered d, c, b, f.

In order to do this, I need to modify StructInfo so that it contains a
pointer back to the declaration from which it came. This is sufficient
to be able to construct the graph correctly, by traversing the vertex
structs themselves in a topological order.

This change only supports evalv3. Support for evalv2 is added in the
next commit.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: Ic47d380f25b74f7633475091c34ff193dc547e01
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202400
Reviewed-by: Marcel van Lohuizen <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
cuematthew committed Nov 5, 2024
1 parent c6feea9 commit 65a15e2
Show file tree
Hide file tree
Showing 5 changed files with 557 additions and 3 deletions.
12 changes: 12 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ func (v *Vertex) rootCloseContext(ctx *OpContext) *closeContext {
parent: nil,
src: v,
parentConjuncts: v,
decl: v,
}
v._cc.incDependent(ctx, ROOT, nil) // matched in REF(decrement:nodeDone)
}
Expand Down Expand Up @@ -541,6 +542,9 @@ type StructInfo struct {
Disable bool

Embedding bool

// Decl contains this Struct
Decl Decl
}

// TODO(perf): this could be much more aggressive for eliminating structs that
Expand Down Expand Up @@ -1388,6 +1392,14 @@ func (v *Vertex) AddStruct(s *StructLit, env *Environment, ci CloseInfo) *Struct
Env: env,
CloseInfo: ci,
}
if env.Vertex != nil {
// be careful to avoid promotion of nil env.Vertex to non-nil
// info.Decl
info.Decl = env.Vertex
}
if cc := ci.cc; cc != nil && cc.decl != nil {
info.Decl = cc.decl
}
for _, t := range v.Structs {
if *t == info { // TODO: check for different identity.
return t
Expand Down
2 changes: 2 additions & 0 deletions internal/core/adt/conjunct.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ loop2:

case *Comprehension:
ci, cc := ci.spawnCloseContext(n.ctx, closeEmbed)
cc.decl = x
cc.incDependent(n.ctx, DEFER, nil)
defer cc.decDependent(n.ctx, DEFER, nil)
n.insertComprehension(childEnv, x, ci)
Expand Down Expand Up @@ -348,6 +349,7 @@ loop2:
case Expr:
// TODO: perhaps special case scalar Values to avoid creating embedding.
ci, cc := ci.spawnCloseContext(n.ctx, closeEmbed)
cc.decl = x

// TODO: do we need to increment here?
cc.incDependent(n.ctx, DEFER, nil) // decrement deferred below
Expand Down
4 changes: 4 additions & 0 deletions internal/core/adt/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ type closeContext struct {
// context has been completed, but it can be used for initial checking
// once isClosed is true.
Expr Value

// decl is the declaration which contains the conjuct which gave
// rise to this closeContext.
decl Decl
}

// Label is a convenience function to return the label of the associated Vertex.
Expand Down
7 changes: 4 additions & 3 deletions internal/core/toposort/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ type Graph struct {
}

type Node struct {
Feature adt.Feature
Outgoing Nodes
Incoming Nodes
Feature adt.Feature
Outgoing Nodes
Incoming Nodes
structMeta *structMeta
// temporary state for calculating the Strongly Connected
// Components of a graph.
sccNodeState *sccNodeState
Expand Down
Loading

0 comments on commit 65a15e2

Please sign in to comment.