Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ways around if-then-else #3

Open
triska opened this issue Oct 19, 2016 · 2 comments
Open

Ways around if-then-else #3

triska opened this issue Oct 19, 2016 · 2 comments

Comments

@triska
Copy link

triska commented Oct 19, 2016

In some places, the following pattern currently occurs:

Goal -> false

This can always be rewritten to the negation of Goal, if a pure negation is available.

For example:

    (   0 #= N mod D ->
        false

This can be rewritten to:

0 #\= N mod D

This is because (#\=)/2 is true iff (#=)/2 isn't. Importantly, the rewritten version is more general.

@JCumin
Copy link
Owner

JCumin commented Oct 20, 2016

@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.

@triska
Copy link
Author

triska commented Oct 20, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants