SystemVerilog: Solve before constraint on random child classes

Consider the below code.


class b;
    rand bit A;
    rand bit [1:0] B;
endclass
class c;
    rand b arr[];
    constraint constraints_c {
        foreach(arr[i]) {
            if(i>0) {
                arr[i].B != retlastB(i);
            }
        }
        foreach(arr[i]) {
            if(i>0) {
                //solve arr[i-1].B before arr[i].B;
                <font size=20>solve arr[i-1] before arr[i]</font>;
            }
        }
    }
    
    function bit[1:0] retlastB(int idx);
        for(int i = idx - 1; i >= 0; i--) begin
            if(arr[idx].A == arr[i].A)
                return arr[i].B;
        end
        return 0;
    endfunction

    function new;
        arr = new[10];
        foreach(arr[i])
            arr[i] = new;
    endfunction
endclass


The highlighed code doesnt compile. Without which anyways I get all incorrect randomization, without any warning of randomization fail from simulator. Each index of the random class (arr) depends on a previous history. Does anybody know a better way to accomplish this? I do not want to create a class with just one instance and iterate it 10 times preserving hostory thro postrandomize calls.

In reply to sharatk:

I relaxed my constraint a little bit. Now the next index only depends on previous index and I do not have to use the function. It seems to work well.


class b;
    rand bit A;
    rand bit [1:0] B;
endclass
class c;
    rand b arr[];
    constraint constraints_c {
        foreach(arr[i]) {
            if(i>0) {
                if(arr[i].A == arr[i-1].A)
                    arr[i].B != arr[i-1].B;
            }
            arr[i].A == 1;
        }
    }
    
    function new;
        arr = new[10];
        foreach(arr[i])
            arr[i] = new;
    endfunction
endclass



What is it with the function? Is it tool limitation or LRM/language quirk?

In reply to sharatk:

The
solve before
construct only works with integral rand variables, not class handles.

You need to explain what “incorrect randomization” means. What are you expecting? Also please give an example.

Your second example has the constraint
arr[i].A == 1;
, which means it is no longer random.

In reply to dave_59:

Thanks Dave for the reply.

1> You need to explain what “incorrect randomization” means.
I mean the randomization outputs were incorrect. What I was trying to do was value of B must not be equal to prev value of B for same A. Its like saying for a protocol(add and write) Value of address must not be equal to last address for same kind of access.

2> Your second example has the constraint arr[i].A == 1;
I did this to get more results since now I am comparing with just one previous iteration. Previously I was scanning all previous iterations.