I am about to implement a prover for logical terms in Prolog. My current code is not really presentable, therefore, I will just state, what I want my program to do and hopefully you can give me some good advice for that :)
It should take a list of variables (so to say the logical arguments) and secondly a logical formula containing these arguments (e.g. 'not'(A 'and' B) 'or' 'not'(B 'and' C) 'or' ... and so forth).
As output I would like my program to respond with the possible consistent assignments. The single arguments can either be true (1) or false (0).
So I aim for a return like A=0, B=0, C=0 ; A=1 and so forth.
I am happy for every help concerning my program :)
There are several ways one could approach this. One way that is convenient in terms of syntax would be to define operators, something like this:
(I am just guessing at reasonable precedence settings here, but just for illustration. See the
opdocumentation for details.)Having done that, you can write an expression such as:
A and Band Prolog will "see" it asand(A, B):From there, you need to have a way to evaluate an expression. There are lots of questions on SO here in this regard (do a search in this site on
[prolog] boolean expression evaluation), but I'll provide a simple example. It's now all about how you want to represent a result, and about recursion.When it comes to representing a result, you could use Prolog's success/fail mechanism since you are dealing with boolean results. Or, you can have an explicit result, such as 0 and 1. Let's try 0 and 1 since that's your representation for true and false.
Instead of calculating "boolean" 1/0 results as I've done above, you could, instead, assert them as facts like so:
And then, for example, instead of
Result #= (ResultA + ResultB + 1) // 2you could just have,bool_or(ResultA, ResultB, Result).Now that we can evaluate expressions, we want a solver:
Note that in the original problem statement, it's said that the variable list would be given as an argument, but you can use
term_variables/2to obtain the variables from an expression.Then you can run the solver:
I don't know what your representation is for an expression. But whatever it is, you can map it to the above solution. What I've shown is simple and clear. You could skip the
op/3stuff and use standard term expressions, like,or(not(and(A,B)), not(and(B,C)))using the above code. If you have your input as some kind of token sequence, like,[not, (, A, and, B, ...]then you'll have to do a little list processing.