More or fewer variables

Nothing above is tied to three variables.

Make the variable count data too, and size var and assignment from it:

extensional num_vars(n: integer).

var(V)        :- num_vars(N), V in [0 .. N - 1].
assignment(A) :- num_vars(N), A in [0 .. (1 << N) - 1].
With num_vars(N) there are N variables and 2^N assignments (1 << N of them).

The interpreter rules stay the same; only the formula and num_vars are supplied as input.