I posted the code below as an answer to this question and user "repeat" answered and commented that it's not logically pure and "if you are interested in a minimal change to your code that makes it preserve logical-purity, I suggest posting a new question about that. I'd be glad to answer it :)".
% minset_one(1 in D1, 1 in D2, D1, D2, D1Len, D2Len, T).
minset_one_(true, false, D1, _, _, _, D1).
minset_one_(false, true, _, D2, _, _, D2).
minset_one_(true, true, _, D2, D1Len, D2Len, D2) :- D1Len >= D2Len.
minset_one_(true, true, D1, _, D1Len, D2Len, D1) :- D1Len < D2Len.
minset_one(D1, D2, T) :-
(member(1, D1) -> D1check = true ; D1check = false),
(member(1, D2) -> D2check = true ; D2check = false),
length(D1, D1Len),
length(D2, D2Len),
minset_one_(D1check, D2check, D1, D2, D1Len, D2Len, T).
e.g.
?- D1 = [X,Y,Z], D2 = [U,V], minset_one(D1,D2,T).
D1 = [1, Y, Z],
D2 = T, T = [1, V],
U = X, X = 1 ;
false
there are more solutions possible. member(1, D1) is not backtracking through [1, Y, Z], then [X, 1, Z] then [X, Y, 1].
The Problem with (->)/2 (and friends)
Consider the following goal:
(member(1,D1) -> D1check = true ; D1check = false)
(->)/2 commits to the first answer of member(1,D1)—other answers are disregarded.
Can alternatives to (->)/2—like (*->)/2 (SWI, GNU) or if/3 (SICStus)—help us here?
No. These do not ignore alternative answers to make member(1,D1) succeed, but they do not consider that the logical negation of member(1,D1) could also have succeeded.
Back to basics: "If P then Q else R" ≡ "(P ∧ Q) ∨ (¬P ∧ R)"
So let's rewrite (If -> Then ; Else) as (If, Then ; Not_If, Else):
(member(1,D1), D1check = true ; non_member(1,D1), D1check = false)
How should we implement non_member(X,Xs)—can we simply write \+ member(X,Xs)?
No! To preserve logical purity we better not build upon "negation as finite failure".
Luckily, combining maplist/2 and dif/2 does the job here:
non_member(X,Xs) :-
maplist(dif(X),Xs).
Putting it all together
So here's the minimum change I propose:
minset_one_(true, false, D1, _, _, _, D1).
minset_one_(false, true, _, D2, _, _, D2).
minset_one_(true, true, _, D2, D1Len, D2Len, D2) :- D1Len >= D2Len.
minset_one_(true, true, D1, _, D1Len, D2Len, D1) :- D1Len < D2Len.
non_member(X,Xs) :-
maplist(dif(X),Xs).
minset_one(D1, D2, T) :-
(member(1,D1), D1check = true ; non_member(1,D1), D1check = false),
(member(1,D2), D2check = true ; non_member(1,D2), D2check = false),
length(D1, D1Len),
length(D2, D2Len),
minset_one_(D1check, D2check, D1, D2, D1Len, D2Len, T).
Running the sample query we now get:
?- D1 = [X,Y,Z], D2 = [U,V], minset_one(D1,D2,T).
D1 = [1,Y,Z], X = U, U = 1, D2 = T, T = [1,V]
; D1 = [1,Y,Z], X = V, V = 1, D2 = T, T = [U,1]
; D1 = T, T = [1,Y,Z], X = 1, D2 = [U,V], dif(U,1), dif(V,1)
; D1 = [X,1,Z], Y = U, U = 1, D2 = T, T = [1,V]
; D1 = [X,1,Z], Y = V, V = 1, D2 = T, T = [U,1]
; D1 = T, T = [X,1,Z], Y = 1, D2 = [U,V], dif(U,1), dif(V,1)
; D1 = [X,Y,1], Z = U, U = 1, D2 = T, T = [1,V]
; D1 = [X,Y,1], Z = V, V = 1, D2 = T, T = [U,1]
; D1 = T, T = [X,Y,1], Z = 1, D2 = [U,V], dif(U,1), dif(V,1)
; D1 = [X,Y,Z], D2 = T, T = [1,V], U = 1, dif(X,1), dif(Y,1), dif(Z,1)
; D1 = [X,Y,Z], D2 = T, T = [U,1], V = 1, dif(X,1), dif(Y,1), dif(Z,1)
; false.
Better. Sure looks to me like there's nothing missing.
I think it would be:
add:
:- use_module(library(reif)).
... and replace:
%(member(1, D1) -> D1check = true ; D1check = false),
%(member(1, D2) -> D2check = true ; D2check = false),
memberd_t(1, D1, D1check),
memberd_t(1, D2, D2check),
Example of the difference between member and memberd_t:
?- member(X, [A, B, C]).
X = A ;
X = B ;
X = C.
?- memberd_t(X, [A, B, C], IsMember).
X = A,
IsMember = true ;
X = B,
IsMember = true,
dif(A,B) ;
X = C,
IsMember = true,
dif(A,C),
dif(B,C) ;
IsMember = false,
dif(A,X),
dif(B,X),
dif(C,X).
?- memberd_t(X, [A, B, C], IsMember), X = 5, A = 5, C = 5.
X = A, A = C, C = 5,
IsMember = true ;
false.
So, memberd_t is itself adding the dif/2 constraints. To aid performance slightly, it loops through the list only once.
The definition of memberd_t is at e.g. https://github.com/meditans/reif/blob/master/prolog/reif.pl#L194 and https://www.swi-prolog.org/pack/file_details/reif/prolog/reif.pl?show=src
i'm writing a simple function in prolog (beginer). Function calculates a specific type of quadratic equation (but this is not important, calculations are correct).
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0
-> false
; ( Delta == 0
-> X is -B/(2*A),
Y is A*X + B,
W = punkt(X, Y)
; X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
writeln(X1), writeln(Y1), writeln(X2), writeln(Y2),
W = punkt(X1, Y1) ; W = punkt(X2, Y2)
)
).
When i run this function, i receive warnings:
Singleton variable in branch: X2
Singleton variable in branch: Y2
In the result i receive strange things. In writeln(X2), writeln(Y2) everything is ok, but then there is sth strange in punkt(X2, Y2):
1.4142135623730951
1.4142135623730951
-1.4142135623730951
-1.4142135623730951
W = punkt(1.4142135623730951, 1.4142135623730951)
W = punkt(_1344, _1346)
What's happening? How should I make it?
The syntax of ->/2 is horrendous at the best of times. That no official better syntax has been developed in 30 years is a sad statement, but of what? I won't say anything more about this.
It is best to separate out the program parts into separate predicates.
(A further refinement could be to separate out the case "Delta == 0" into a dedicated delta_positive(A,B,W), thus avoiding having to make a decision on delta_positive/4 and thus cutting and guarding.
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0 -> false ; delta_positive(Delta,A,B,W) ).
delta_positive(0,A,B,W) :-
!, % commit to this clause
X is -B/(2*A),
Y is A*X + B,
W = punkt(X,Y).
delta_positive(Delta,A,B,W) :-
Delta \== 0, % superfluous, but makes things clear
!, % commit is also superfluous
X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
format("X1=~q, Y1=~q, Y2=~q, Y2=~q\n",[X1,Y1,X2,Y2]),
(W = punkt(X1, Y1) ; W = punkt(X2, Y2)).
This immediately shows that parantheses are missing around the expression:
(W = punkt(X1, Y1) ; W = punkt(X2, Y2)).
I tried solving it:-
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0
-> false
; ( Delta == 0
-> X is -B/(2*A),
Y is A*X + B,
W = punkt(X, Y)
; X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
writeln(X1), writeln(Y1), writeln(X2), writeln(Y2),
W = punkt(X1, Y1) ); X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B, W = punkt(X2, Y2)
).
?-liniowa(2, 3, 4, W).
0.9083123951776999
4.8166247903554
-2.4083123951777
-1.8166247903553998
W = punkt(0.9083123951776999, 4.8166247903554)
W = punkt(-2.4083123951777, -1.8166247903553998)
I believe that in your code the calculation is being done in the previous step, that's why it is unable to pick it in the last case.
I was making a prolog knowledge base to implement geometry rules. When testing if a rectangle had a right angle, I found two answers.
?- rect_tri(triangle(line(point(0,0),point(0,1)),line(point(0,1),point(1,0)),line(point(1,0),point(0,0)))).
true ;
false.
Here is the kwnoledge base:
point(X,Y).
line(X,Y) :- X = point(A,B), Y = point(C,D), not(X = Y).
len(X,R) :- X = line(P,Q), P = point(A,B), Q = point(C,D), not(P = Q),
R is sqrt((A - C) * (A - C) + (B - D) * (B - D)).
triangle(X,Y,Z) :- X = point(A,B), Y = point(C,D), Z = point(E,F),
not(X = Y), not(X = Z), not(Y = Z),
L1 = line(X,Y), L2 = line(X,Z), L3 = line(Y,Z),
len(L1,G), len(L2,H), len(L3,I),
G + H > I, G + I > H, H + I > G.
triangle(X,Y,Z) :- X = line(A,B), Y = line(B,C), line(A,C),
len(X,G), len(Y,H), len(Z,I),
G + H > I, G + I > H, H + I > G.
rect_tri(X) :- X = triangle(A,B,C), len(A,G), len(B,H), len(C,I),
(G is sqrt(H * H + I * I);
H is sqrt(G * G + I * I);
I is sqrt(H * H + G * G)).
When tracing, I found that the answer true comes when prolog hits the line H is sqrt(G * G + I * I), and false when it evaluates the last line.
I don't want the last evaluation to occur, because I want it to exit when a true has been found.
Daniel comment probably shows the most sensible way to solve your problem. Some other option...
in modern compilers there is the if/then/else construct:
rect_tri(X) :- X = triangle(A,B,C), len(A,G), len(B,H), len(C,I),
( G is sqrt(H * H + I * I)
-> true
; H is sqrt(G * G + I * I)
-> true
; I is sqrt(H * H + G * G)
).
You could as well use cuts (old fashioned way, somewhat more readable here):
rect_tri(X) :- X = triangle(A,B,C), len(A,G), len(B,H), len(C,I),
( G is sqrt(H * H + I * I), !
; H is sqrt(G * G + I * I), !
; I is sqrt(H * H + G * G)
).