Solving CNF using Prolog - prolog

While learning Prolog, I tried to write a program solving CNF problem (the performance is not an issue), so I ended up with the following code to solve (!x||y||!z)&&(x||!y||z)&&(x||y||z)&&(!x||!y||z):
vx(t).
vx(f).
vy(t).
vy(f).
vz(t).
vz(f).
x(X) :- X=t; \+ X=f.
y(Y) :- Y=t; \+ Y=f.
z(Z) :- Z=t; \+ Z=f.
nx(X) :- X=f; \+ X=t.
ny(Y) :- Y=f; \+ Y=t.
nz(Z) :- Z=f; \+ Z=t.
cnf :-
(nx(X); y(Y); nz(Z)),
(x(X); ny(Y); z(Z)),
(x(X); y(Y); z(Z)),
(nx(X); ny(Y); z(Z)),
write(X), write(Y), write(Z).
Is there any simpler and more direct way to solve CNF using this declarative language?

Consider using the built-in predicates true/0 and false/0 directly, and use the toplevel to display results (independently, instead of several subsequent write/1 calls, consider using format/2):
boolean(true).
boolean(false).
cnf(X, Y, Z) :-
maplist(boolean, [X,Y,Z]),
(\+ X; Y ; \+ Z),
( X ; \+ Y ; Z),
( X ; Y ; Z),
( \+ X ; \+ Y ; Z).
Example:
?- cnf(X, Y, Z).
X = true,
Y = true,
Z = true .
EDIT: As explained by #repeat, also take a serious look at CLP(B): Constraint Solving over Booleans.
With CLP(B), you can write the whole program above as:
:- use_module(library(clpb)).
cnf(X, Y, Z) :-
sat(~X + Y + ~Z),
sat(X + ~Y + Z),
sat(X + Y + Z),
sat(~X + ~Y + Z).
Please see the answer by #repeat for more information about this.

Look up "lean theorem prover" (such as leanTAP or leanCoP) for simple, short theorem provers in Prolog. Those are designed to use Prolog features to the best possible advantage. Although provers like that use first-order logic, CNF is a subset of that. There are dedicated SAT solvers for Prolog as well, such as this one.

Use clpb!
:- use_module(library(clpb)).
To check if some Boolean expression is satisfiable, use sat/1:
% OP: "(!x||y||!z) && (x||!y||z) && (x||y||z) && (!x||!y||z)"
?- sat((~X + Y + ~Z)*(X + ~Y + Z)*(X + Y + Z)*(~X + ~Y + Z)).
sat(X=\=X*Y#Z).
No concrete solution(s) yet... but a residue that's a lot simpler than the term we started with!
To get to concrete truth values, use labeling/1:
?- sat(X=\=X*Y#Z), labeling([X,Y,Z]).
X = 0, Y = 0, Z = 1
; X = 0, Y = 1, Z = 1
; X = 1, Y = 0, Z = 0
; X = 1, Y = 1, Z = 1.

Related

Prolog program to enumerate all possible solution over a countable set

I am writing a prolog program with can perform Peano arithmetics.
I have standard definitions for natural numbers.
nat(n).
nat(s(N)) :-
nat(N).
Because I want to enumerate all possible relation of addition between natural numbers, I defined an auxiliary function (in order for defining total ordering over the set).
cmp_n(X, Y, lt) :-
nat(Y), % generate a stream : n s(n) s(s(n)) ...
cmp_n_lt_helper(X, Y). % gives all XS smaller than Y
cmp_n_lt_helper(s(X), s(Y)) :-
cmp_n_lt_helper(X, Y).
cmp_n_lt_helper(n, s(Y)) :-
nat(Y).
Then, I defined addition
% need to use a wrapper because I want to generate (n, n, n) first
% if I don't use this warper, it would start from (n, s(n), s(n))
add_n(X, Y, R) :-
nat(R), % same reason as above
cmp_n(X, R, lt),
add_n_helper(X, Y, R).
add_n_helper(s(X), Y, s(R)):-
add_n_helper(X, Y, R).
add_n_helper(n, Y, Y).
If I enumerate all possible relations over this definition of addition, it worked fine. And when outputting a finite set of answers, it can halt.
?- add_n(X, Y, R).
X = Y, Y = R, R = n ;
X = R, R = s(n),
Y = n ;
X = n,
Y = R, R = s(n) ;
X = R, R = s(s(n)),
Y = n ;
X = Y, Y = s(n),
R = s(s(n)) ;
X = n,
Y = R, R = s(s(n)) .
?- add_n(X, Y, s(s(s(s(n))))).
X = s(s(s(s(n)))),
Y = n ;
X = s(s(s(n))),
Y = s(n) ;
X = Y, Y = s(s(n)) ;
X = s(n),
Y = s(s(s(n))) ;
X = n,
Y = s(s(s(s(n)))) ;
false.
These worked fine.
However, if I do the regular forward evaluation,
?- add_n(s(s(s(n))), s(s(n)), R).
R = s(s(s(s(s(n)))))
this program cannot halt.
I am wondering : is there a way to
for any finite answer, give a finite result.
for any infinite answer, fix a specific valid answer, give this specified answer in finite time
As spot properly in the comments and by you as well, you've got a problem in a specific case, when X and Y are defined and R is not.
So let's just solve this case separately without the R generator in that case.
In my implementation (similar to yours)
nat(n).
nat(s(N)) :-
nat(N).
eq_n(n, n) :- !.
eq_n(s(X), s(Y)) :-
eq_n(X, Y), !.
leq_n(n, n).
leq_n(n, Y) :-
nat(Y).
leq_n(s(X), s(Y)) :-
leq_n(X, Y).
movel_n(X, n, X) :- !.
movel_n(X, s(Y), Z) :-
movel_n(s(X), Y, Z), !.
add_n(X, Y, R) :-
( ( var(X)
; var(Y)
),
nat(R),
leq_n(X, R),
leq_n(Y, R)
; \+ var(X),
\+ var(Y), !
),
movel_n(X, Y, Xn),
eq_n(Xn, R).
The most important part for you is the first big or statement of add_n/3.
We're checking there with the var/1 if the variables are instantiated.
If not, we're creating the variables generator,
otherwise, we're just going forward to calculations.

Prolog predecessor math

I have an add2 predicate which resolves like this where s(0) is the successor of 0 i.e 1
?- add2(s(0)+s(s(0)), s(s(0)), Z).
Z = s(s(s(s(s(0)))))
?- add2(0, s(0)+s(s(0)), Z).
Z = s(s(s(0)))
?- add2(s(s(0)), s(0)+s(s(0)), Z).
Z = s(s(s(s(s(0)))))
etc..
I'm trying to do add in a predecessor predicate which will work like so
?- add2(p(s(0)), s(s(0)), Z).
Z = s(s(0))
?- add2(0, s(p(0)), Z).
Z = 0
?- add2(p(0)+s(s(0)),s(s(0)),Z).
Z = s(s(s(0)))
?- add2(p(0), p(0)+s(p(0)), Z).
Z = p(p(0))
I can't seem to find a way to do this. My code is below.
numeral(0).
numeral(s(X)) :- numeral(X).
numeral(X+Y) :- numeral(X), numeral(Y).
numeral(p(X)) :- numeral(X).
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
add(p(X),Y,p(Z)) :- add(X,Y,Z).
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(p(X),p(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
add2(A,B,C) :-
resolve(A,RA),
resolve(B,RB),
add(RA,RB,C).
In general, adding with successor arithmetic means handling successor terms, which have the shape 0 or s(X) where X is also a successor term. This is addressed completely by this part of your code:
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
Now you have to make a decision; you can either handle the predecessors and the addition terms here, in add/3, or you can wrap this predicate in another one that will handle them. You appear to have chosen to wrap add/3 with add2/3. In that case, you will definitely need to create a reducing term, such as you've built here with resolve/2, and I agree with your implementation of part of it:
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
This is all good. What you're missing now is a way to handle p(X) terms. The right way to do this is to notice that you already have a way of deducting by one, by using add/3 with s(0):
resolve(p(X), R) :-
resolve(X, X1),
add(s(0), R, X1).
In other words, instead of computing X using X = Y - 1, we are computing X using X + 1 = Y.
Provided your inputs are never negative, your add2/3 predicate will now work.

Can I use integers as parameters?

How can I know if a person X is descendant of a person Y given the descendancy degree?
I've tried this:
descendant(X, Y, 1) :- son(X, Y).
descendant(X, Y, Degree) :- son(X, Z) , descendant(Z, Y, Degree-1).
Where son(X, Y) returns yes if X is son of Y. If Degree == 1 it returns the correct answer but for descendant(X, Y, 2), for instance, should return yes if X is grandson of Y but returns no.
1) Naming: Does son(X,Y) mean "X is the son of Y"—or vice-versa?
son_of(X,Y) is better.
2) Exploit successor-arithmetics: We don't need to do general arithmetics here... we only need to count.
So let's start in the beginning...
child_of(abel, adam). % from source
child_of(abel, eve).
child_of(cain, adam).
child_of(cain, eve).
child_of(enoch, cain).
child_of(irad, enoch).
child_of(mehujael, irad).
child_of(methushael, mehujael).
child_of(lamech, methushael).
child_of(jabal, lamech).
child_of(jabal, adah).
child_of(jubal, lamech).
child_of(jubal, adah).
child_of(tubal_cain, lamech).
child_of(tubal_cain, zillah).
child_of(naamah, lamech).
child_of(naamah, zillah).
child_of(seth, adam).
child_of(seth, eve).
child_of(enos, seth).
child_of(kenan, enos).
child_of(mahalalel, kenan).
child_of(jared, mahalalel).
child_of(enoch, jared).
child_of(methuselah, enoch).
child_of(lamech, methuselah).
child_of(noah, lamech).
child_of(shem, noah).
child_of(ham, noah).
child_of(japheth, noah).
Based on child_of/2 we first define ancestor_of/2—this should be nothing new to you!
ancestor_of(Y, Z) :-
child_of(Z, Y). % If Z is a child of Y ...
% then Y is an ancestor of Z.
ancestor_of(X, Z) :-
child_of(Z, Y), % If Z is a child of Y ...
ancestor_of(X, Y). % and X is an ancestor of Y ...
% then X is an ancestor of Z.
Next, we add an additional parameter indicating the distance.
We use s/1 terms to represent natural numbers and add a new argument to ancestor_of/2:
ancestor_of_dist(Y, Z, s(0)) :-
child_of(Z, Y). % If Z is a child of Y ...
% then Y is an ancestor of Z with distance = 1."
ancestor_of_dist(X, Z, s(N)) :-
child_of(Z, Y), % If Z is a child of Y ...
ancestor_of_dist(X, Y, N). % and X is an ancestor of Y with distance N ...
% then X is an ancestor of Z with distance N+1.
So ... who is grandparent of whom?
?- ancestor_of_dist(X, Z, s(s(0))).
X = adam, Z = enoch
; X = eve, Z = enoch
; X = cain, Z = irad
; X = jared, Z = irad
; X = enoch, Z = mehujael
; ...
; X = lamech, Z = japheth
; false.
Prolog is not a functional language. Thus, the Degree-1 term is not interpreted and evaluated as an expression. For Prolog, Degree-1 is just a compound term with two arguments. That can be made clear using the standard write_canonical/1 predicate, which writes a term without using operator notation:
?- write_canonical(Degree-1).
-(_,1)
true.
Write instead:
descendant(X, Y, 1) :-
son(X, Y).
descendant(X, Y, Degree) :-
son(X, Z) ,
descendant(Z, Y, Degree0),
Degree is Degree0 + 1.
The is/2 standard built-in predicate unifies the left argument with the value of the arithmetic expression in the right argument.
P.S. Note that the alternative descendant/3 predicate definition I suggest will solve the problem you described but it is not an efficient definition as it is not a tail-recursive definition. I.e. the recursive call in the second clause is not the last call in the clause body. This issue can be easily solved, however, by using an accumulator.

Predicate in prolog which is true if M and N differ more than X

First of all I am completely new to prolog and I am trying to write a predicate length(M,X,N) which is true, if M differs from N more than X.
I wrote the following testcase which is true if M(=dec.5) and N(=dec.2) differ more than X(=dec.2). And it is true in this case because 5 and 2 have a difference of 3 which is more than 2:
?- length(s(s(s(s(s(0))))), s(s(0)), s(s(0))).
true .
I know that prolog works recursively so I am wondering if I can construct such a predicate with conditions (for example <,>) like in languages like C, or if there is another way to do this in prolog. Sorry for this simple question but I just started with prolog.
You could construct predicates for greater or less. For example:
greater_than(s(_), 0).
greater_than(s(X), s(Y)) :-
greater_than(X, Y).
And similarly:
less_than(0, s(_)).
less_than(s(X), s(Y)) :-
less_than(X, Y).
If you want to find the absolute difference, you could do something like this:
abs_diff(0, 0, 0).
abs_diff(s(X), 0, s(X)).
abs_diff(0, s(X), s(X)).
abs_diff(s(X), s(Y), D) :-
abs_diff(X, Y, D).
Those concepts should help kick start some ideas for how to solve the rest of the problem.
This answer follows up on #lurker's fine answer and improves the determinism of the auxiliary predicate abs_diff/3 by utilizing
first argument clause indexing.
Introducing x_y_dist/3:
x_y_dist(0, Y, Y).
x_y_dist(s(X), Y, Z) :-
y_sx_dist(Y, X, Z).
y_sx_dist(0, X, s(X)).
y_sx_dist(s(Y), X, Z) :-
x_y_dist(X, Y, Z).
Sample query:
?- x_y_dist(X, Y, s(s(0))). % |X-Y| = 2
( X = 0 , Y = s(s(0)) % |0-2| = 2
; X = s(s(0)) , Y = 0 % |2-0| = 2
; X = s(0) , Y = s(s(s(0))) % |1-3| = 2
; X = s(s(s(0))) , Y = s(0) % |3-1| = 2
; X = s(s(0)) , Y = s(s(s(s(0)))) % |2-4| = 2
; X = s(s(s(s(0)))) , Y = s(s(0)) % |4-2| = 2
; X = s(s(s(0))) , Y = s(s(s(s(s(0))))) % |3-5| = 2
; X = s(s(s(s(s(0))))), Y = s(s(s(0))) % |5-3| = 2
; X = s(s(s(s(0)))) , Y = s(s(s(s(s(s(0)))))) % |4-6| = 2
; .........
)
Try this:
?- length(s(s(s(s(s(0))))), s(s(0)), s(s(0))).
length(s(_),0,0).
length(s(M),s(X),s(N)) :- length(M,X,N).
Do keep in mind that Prolog's predicates do not return values - so they don't return true or false. They either succeed or they don't. The interpreter is just telling you if your program succeeds or not.

Euclidean recursive algorithm

Ok, I know it's really a stupid question, but I can't get it.
There is a task where I should find a recursive algorithm of
Euclid (gcd). I've done it for one case, here:
nondeterm nod (integer,integer,integer)
CLAUSES
nod (X,0,X):- !.
nod (0,X,X):- !.
nod (X,0,X):-X>0.
nod (X,Y,G):-Y>0, Z = X mod Y, nod (Y,Z,G).
I need to do another case, where recursion is beginnig from х0, when Xi then calling for function counting Xi+1.
It should be sort of it:
PREDICATES
nondeterm nod (integer,integer,integer)
nondeterm nod1 (integer,integer,integer,integer,integer)
CLAUSES
nod(X,Y,Z):- nod1(X,Y,Z,0,0).
nod1 (X,Y,Z,X,Y):- Otvet = Z, write("Otvet=", Otvet, "\n"), !.
nod1 (X,Y,X,Y):- nod1 (X,Y,X,Y).
nod1 (X,Y,Z,X1,Y1):-
X1>Y1, X>0, Y>0,
Y2 = X1 mod Y1,
X2 = Y1,
nod1(X,Y,Z,X2,Y2).
But it doesn't work. Please, help me with that.
The following code works for me. Please note the use of
rem, but I guess you could also use mod:
% sys_gcd(+Integer, +Integer, -Integer)
sys_gcd(X, 0, X) :- !.
sys_gcd(X, Y, Z) :-
H is X rem Y,
sys_gcd(Y, H, Z).
Here are some example runs with SWI-Prolog:
?- sys_gcd(20,30,X).
X = 10.
?- sys_gcd(-20,30,X).
X = 10.
?- sys_gcd(20,-30,X).
X = -10.
?- sys_gcd(-20,-30,X).
X = -10.
If you want a particular sign of the result, you
need additional code around it.
Bye

Resources