CRV - item.index not working

Hi,

I have the following code in which i am trying to make the 5 index to be a 6. and sum to be 20. But i see neither happening. Can you please tell me what is wrong with the code. Thanks

rand int unsigned fibo[8];
  
   constraint pop_1_c
   {
     foreach (fibo[i])
     {
       fibo[i] inside {[1:10]};
     }
       fibo.sum with ( (item.index==5)?6:item)==20;
   }

In reply to CRVAddict:

rand int unsigned fibo[8];
 
   constraint pop_1_c
   {
     foreach (fibo[i])
     {
       fibo[i] inside {[1:10]};
       i==5 -> fifo[i]==6;
     }
       fibo.sum()==20;
   }

In reply to dave_59:

Hi Dave, Yes i know how to do that, i was tryind to play with item and item.index

In reply to CRVAddict:

What you were doing leaves fifo[5] out of the sum expression.

(fifo[0] + fifo[1] + fifo[2] + fifo[3] + fifo[4] + 6 + fifo[6] + fifo[7]) == 20

In reply to dave_59:

This is the output i get

'{'h1, 'h1, 'h1, 'h1, 'h1, 'h4, 'h8, 'h1}

index 5 is not 6 nor the sum is 20

In reply to CRVAddict:
Do a $display(fibo.sum with ( (item.index==5)?6:item)) after calling randomize()

In reply to dave_59:
20
'{'h1, 'h1, 'h1, 'h1, 'h1, 'h4, 'h8, 'h1}

Thats interesting i printed in post_randomize and it prints sum is 20. But i dont see sum is 20. Am i missing anything?

In reply to CRVAddict:

When you are calling it after randomize in the $display or wherever, you are executing an array reduction operation on the array. It uses a value of 6 for index 5. So 'h4 will be replaced by 6 and that adds up to 20.

The exact same thing happens when you put it in a constraint. You are not asking the solver to constrain index 5 to be 6 at all. Refer to dave’s response above for the expansion of the array reduction operation.

In reply to kernalmode1:

Thanks Kernal that makes sense now.

One last question then what is this line doing in the constraint?

(item.index==5)?6:item

In reply to CRVAddict:

The constraint you wrote originally:

rand int unsigned fibo[8];
 
   constraint pop_1_c
   {
     foreach (fibo[i])
     {
       fibo[i] inside {[1:10]};
     }
       fibo.sum with ( (item.index==5)?6:item)==20;
   }

is equivalent to this constraint

rand int unsigned fibo[8];
 
   constraint pop_1_c
   {
       fibo[0] inside {[1:10]};
       fibo[1] inside {[1:10]};
       fibo[2] inside {[1:10]};
       fibo[3] inside {[1:10]};
       fibo[4] inside {[1:10]};
       fibo[5] inside {[1:10]};
       fibo[6] inside {[1:10]};
       fibo[7] inside {[1:10]};
      (fifo[0] + fifo[1] + fifo[2] + fifo[3] + fifo[4] + 6 + fifo[6] + fifo[7]) == 20
  }