different/2 – does a pure, determinate definition exist?

First try! The following code is based on the meta-predicates tfilter/3 and tpartition/4, the monotone if-then-else control construct if_/3, the reified unary logical connective not_t/3, and the reified term equality predicate (=)/3: different([],[_|_]). different([X|Xs0],Ys0) :- tpartition(=(X),Ys0,Es,Ys1), if_(Es=[], true, (tfilter(not_t(=(X)),Xs0,Xs1),different(Xs1,Ys1))). Sample query: ?- different([A,B],[X,Y]). A=Y , dif(B,Y), X=Y ; A=X , B=X , dif(X,Y) ; A=X … Read more

What use does if_/3 have?

In old-fashioned Prolog code, the following pattern arises rather frequently: predicate([], …). predicate([L|Ls], …) :- condition(L), then(Ls, …). predicate([L|Ls], …) :- \+ condition(L), else(Ls, …). I am using lists here as an example where this occurs (see for example include/3, exclude/3 etc.), although the pattern of course also occurs elsewhere. The tragic is the following: … Read more

‘if’ in prolog?

Yes, there is such a control construct in ISO Prolog, called ->. You use it like this: ( condition -> then_clause ; else_clause ) Here is an example that uses a chain of else-if-clauses: ( X < 0 -> writeln(‘X is negative. That’s weird! Failing now.’), fail ; X =:= 0 -> writeln(‘X is zero.’) … Read more

tech