My logic teacher said in passing that Quines algorithm can be also used to count valuations. Unfortunately I cannot get my head around how this is done in Prolog?
The program would for example give, using
the syntax from the answer in Quines algorithm:
?- sat_count(X+Y, C).
C = 3
Since the truth table for the disjunction X+Y
has 3 rows that valuate to true:
X Y X+Y
0 0 0
0 1 1
1 0 1
1 1 1
Point of departure is Quines algorithm with its core predicate eval/2 which has the following specification. The source code of the Quine algorithm and the solution to the question can be found here.
We first experimented with a labeling predicate, that will list all valuations without counting them. The predicate has a fast fail feature, if the partially evaluated formula is false (0) then labeling needs not to proceed, otherwise we simply probe the boolean values:
Here is an example run:
From the labeling predicate we derived a counting predicate using findall/3 from the ISO core standard. Instead of succeeding at the end we return 1, inbetween we sum the counts. This does the job and also profits from fast failing:
Here is an example run:
The implementation might profit from some optimizations that we didn't implement. For example assigning values to a variable that does not anymore occure in the formula could be optimized away.