Skip to Relationships + arrays questions

Part 2 · Relationships and arrays

Model equations, implication, solve ordering, inheritance, shape, uniqueness, and reductions as one legal solution space.

Question directory

Questions for Relationships + arrays

Open an authored answer, then use the surrounding visual and code example to test the rule.

  1. Relationships and orderingWhy are SystemVerilog constraints described as declarative and bidirectional?SystemVerilog constraints
  2. Relationships and orderingWhat does solve ... before change, and what does it not change?SystemVerilog constraints
  3. Relationships and orderingHow are constraints inherited and overridden in a derived class?SystemVerilog constraints
  4. Arrays and collectionsHow do you constrain a dynamic array's size and elements?SystemVerilog constraints
  5. Arrays and collectionsWhy can sum() produce a surprising constraint result?SystemVerilog constraints
  6. Arrays and collectionsWhen should you use unique, randc, or shuffle()?SystemVerilog constraints
  7. Relationships and orderingWhat is the difference between equality, implication, and biconditional constraints?SystemVerilog constraints
  8. Relationships and orderingWhen does randomizing a parent object also randomize a nested object?SystemVerilog constraints
  9. Relationships and orderingHow do you prove a small constraint system is satisfiable?SystemVerilog constraints
  10. Arrays and collectionsWhat does sum() with (...) actually compute?SystemVerilog constraints
  11. Arrays and collectionsHow do you constrain one-hot, m-hot, or Hamming-distance patterns?SystemVerilog constraints
  12. Arrays and collectionsHow do you generate every ID from 0 through 255 without repeats, then reset?SystemVerilog constraints
  13. Arrays and collectionsHow do you constrain a 4 by 4 Latin square?SystemVerilog constraints
  14. Arrays and collectionsHow do you generate a constrained permutation instead of merely unique values?SystemVerilog constraints

SystemVerilog Relationships and orderingEncode relationships, not procedures.

Reason about equations, implication, inheritance, and solve ordering as one shared solution space rather than a sequence of assignments.

Concept model

Relationships carve one shared solution space

Legal-pair gridImplication removes exactly one Boolean state

A -> Bis false only when the condition is true and its rule is false.

Relationship strengthPick the rule that matches the requirement
A == BAlways equalLegal states: 00, 11
A -> BOne-way conditionLegal states: 00, 01, 11
A <-> BBoth conditions agreeLegal states: 00, 11

All active relationships are solved together.Source order never turns them into assignments.

Equality, implication, and biconditional rules admit different legal pairs. Written order never turns those declarative relationships into assignments.

Strong answer

A constraint describes relationships that must hold in the final assignment. It does not execute statements from top to bottom or designate the left side of == as an output.

Reason it through

  • length == beats * 4 relates both variables. If either value is fixed, the other is constrained by the same equation.
  • The order of constraint expressions does not change their logical intersection.
  • condition -> rule activates the consequent only when the antecedent is true. It neither creates execution order nor implies the reverse relationship.
  • If one field is purely derived and does not need to influence legality, calculating it in post_randomize() can make intent clearer and reduce solver work.

SystemVerilog Arrays and collectionsConstrain shape before contents.

Build collection constraints in layers: size, per-element domains, cross-element relationships, uniqueness, and width-safe reductions.

Concept model

Array constraints build from shape to aggregate

01 · Shapedata.size() == 6
02 · Element domainforeach (data[i]) data[i] inside {[1:8]}
03 · Cross-element ruleunique {data}
04 · Width-safe aggregatedata.sum() with (int'(item))

Map each contribution at the intended width, then reduce the completed collection.

The solver establishes the index domain, expands foreach rules over those indices, then applies uniqueness and aggregate relationships to the complete collection.