Difficulty: Medium · Puzzle 3 of 6 · Topic: Distribution Puzzles (`dist`, `:=` vs `:/`)
Note: this one is about `soft` constraints, not `dist`; it sits here because it also changes which values are likely. Which len values are legal, and with what probability, for each of the four calls (A to D)? Assume equal probability among legal values.
class soft_len;
rand bit [3:0] len;
constraint c_hard { len inside {[1:12]}; }
constraint c_def { soft len inside {[1:4]}; }
endclass
soft_len s = new();
assert(s.randomize()); // Call A
assert(s.randomize() with { len inside {[3:9]}; }); // Call B
assert(s.randomize() with { len inside {[6:9]}; }); // Call C
assert(s.randomize() with { soft len inside {[8:10]}; }); // Call DA soft constraint is kept whenever it can be satisfied together with every hard constraint and every higher-priority soft constraint. It is not dropped just because an inline constraint exists.
1) Call A: nothing conflicts with c_def, so it is kept: len in 1..4, four values, 1/4 each.
2) Call B: the inline hard constraint [3:9] overlaps c_def in {3,4}, so the soft constraint can still be satisfied and is KEPT: len in {3,4}, 1/2 each.
3) Call C: [6:9] and [1:4] do not overlap, so c_def cannot be satisfied and is discarded: len in 6..9, 1/4 each.
4) Call D: both are soft; the inline soft has higher priority than the class soft. [8:10] and [1:4] conflict, so c_def is discarded: len in {8,9,10}, 1/3 each.
5) A hard constraint is never discarded; if the inline in Call C had been outside 1..12 the call would fail instead.Answer: A: 1..4 (1/4 each). B: {3,4} (1/2 each). C: 6..9 (1/4 each). D: {8,9,10} (1/3 each).
Checked by running a model of the rule in a Python model and Verilator 5. Exact enumeration of the constraint set per the LRM (the LRM defines the distribution; simulator solvers need not be uniform). Soft constraints are applied in priority order (inline soft, then class soft) and each is kept only if the remaining set stays non-empty. Verilator 5.048: 200 calls of each of A to D always succeed and stay inside the predicted legal set (frequencies are not asserted).
Call B is the trap: the inline constraint does not 'override' the soft default. Soft means 'satisfy if consistent with the hard constraints', so a partial overlap shrinks the range to the overlap {3,4} instead of discarding the default. It is discarded only on a full conflict (Call C) or against a higher-priority soft (Call D). Legal sets for all four calls were confirmed on Verilator 5.048 (B gave only 3 and 4, C only values within 6..9, D only 8 to 10).
What does Call D become if the inline is `soft len inside {[3:9]}`? Model answer: len in {3,4}. The two soft constraints are compatible (overlap {3,4}), so neither is dropped and both apply, 1/2 each. Soft constraints only fall away when they cannot be satisfied.
This is really about soft constraints: soft means satisfy it if it is consistent with the hard constraints. Call A has no conflict, so len is one to four. Call B overlaps in three and four, so the soft default is kept and len is three or four, half each. Call C does not overlap, so the default is discarded and len is six to nine; call D has a higher priority inline soft, so len is eight, nine or ten. The trap is thinking an inline constraint overrides the default in call B. Next I would check that the hard constraint is satisfiable, because a hard conflict fails the call.