You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@triska I assume that you are talking about check_prime_1/5 and check_prime_2/5. You are indeed right that your version would be more general.
However, those predicates are supposed to fail as quickly as possible if the input is not prime. With your correction, I would still need to add some sort of or cut if 0 #\= N mod D is false. Since check_prime/1 is only used for ground values, trading generality for speed seems acceptable to me.
What do you mean with "some sort of or cut if 0 #\= N mod D is false"?
If 0 #\= N mod D is false, then the whole predicate fails, which is also what it would have done if you call false/0 if 0 #= N mod D holds.
Regarding performance, consider for example:
no_divisor(N, D) :-
\+ ( 0 #= N mod D ).
Here is the expanded source code:
?- listing(no_divisor/2).
%@ no_divisor(A, B) :-
%@ \+ ( integer(A),
%@ integer(B),
%@ B=\=0
%@ -> 0=:=A mod B
%@ ; clpfd:clpfd_equal(0, A mod B)
%@ ).
%@
%@ true.
From this, you see that no constraints are invoked when the arguments are integers.
In some places, the following pattern currently occurs:
This can always be rewritten to the negation of
Goal
, if a pure negation is available.For example:
This can be rewritten to:
This is because
(#\=)/2
is true iff(#=)/2
isn't. Importantly, the rewritten version is more general.The text was updated successfully, but these errors were encountered: