Count function in constraint

In reply to Huzefa Halolwala:
You have to be very careful when calling a user defined function in a constraint. A function call breaks the constraint set in half. The inputs to the function are solved first before calling the function. Then the result of the function call is used as a state variable to solve the rest of the constraints involving that result.

In your example, you have A as an input to a function, and there are no constraints on A. The return value from count_ones could be between 0 and 3. You did not declare length as a rand variable, so it is also a state variable whose initial value is 0. So if the return value of count_ones was not 0, you will get a constraint failure.

There is a built-in function, $countones, that can be used and will avoid this problem

constraint c1 { length == $countones(A); }