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

Feature request predicate divmod/4 #71

Open
Jean-Luc-Picard-2021 opened this issue Sep 21, 2022 · 9 comments
Open

Feature request predicate divmod/4 #71

Jean-Luc-Picard-2021 opened this issue Sep 21, 2022 · 9 comments

Comments

@Jean-Luc-Picard-2021
Copy link

Its quite customary that Prolog systems nowadays
have divmod/4, because many bigint algorithms can
produce both div and mod at the same time.

Trying to port a SWI-Porlog program I get:

?- use_module('/draft.pl').
{Compiling /draft.pl
ERROR: (lns 27-30) Predicate divmod/4 undefined in source
ERROR: Aborted module compilation
}

Could this be added to Ciao Prolog, especially the playground?

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Sep 21, 2022

Further problem, I cannot rewrite it into div and mod, I then get this error:

?- use_module('/draft.pl').
{Reading /draft.pl
ERROR: (lns 27-32) syntax error: operator expected after expression
cra_egcd( A , B , X , Y ) :- Q is A
** here **
div B , R is A mod B , cra_egcd( B , R , S , X ) , Y is S - Q * X .
}
{Compilation aborted}

div/2 came into ISO core Prolog in 2012:

DRAFT TECHNICAL CORRIGENDUM 2
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2#fdivplus

@jfmc
Copy link
Member

jfmc commented Sep 21, 2022

Thanks for the suggestion!

You may try this:

divmod(Dividend, Divisor, Quot, Rem) :-
    Rem is Dividend mod Divisor,
    Quot is (Dividend - Rem) // Divisor.

which actually gives an implementation for div/3 (before we introduce it as an arithmetic expression):

div(Dividend, Divisor, Quot) :-
    Rem is Dividend mod Divisor,
    Quot is (Dividend - Rem) // Divisor.

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Sep 21, 2022

It uses two bigint operations, mod/2 and (//)/2.
Its a little slow. Usually bigint libraries have something like:

divideAndRemainder
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#divideAndRemainder%28java.math.BigInteger%29

Which can be used to implement divmod/4. I don't know
what library Ciao Prolog is using, GMP maybe?

@Jean-Luc-Picard-2021 Jean-Luc-Picard-2021 changed the title Feature request divmod/4 Feature request optional evaluable function divmod/4 Sep 21, 2022
@jfmc
Copy link
Member

jfmc commented Sep 21, 2022

No, Ciao uses its own implementation of bignums. Of course we could add it, but is there any example where performance of this operation is critical?

@Jean-Luc-Picard-2021
Copy link
Author

What would be also useful, these evaluable functions:

Are they somewhere under a different name?

@Jean-Luc-Picard-2021 Jean-Luc-Picard-2021 changed the title Feature request optional evaluable function divmod/4 Feature request predicate divmod/4 Oct 28, 2022
@jfmc jfmc reopened this Jul 29, 2023
@jfmc jfmc reopened this May 3, 2024
@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented May 4, 2024

This ticket is already 3 years old. It has the rotten smell
that Ciao doesn't want to implement any of the built-ins.

So why not close it with won't fix?

@mherme
Copy link

mherme commented May 4, 2024

It is better not to close it because it is not solved. Open issues are reminders of things that need to done or would be good to do, for which we thank you. Unfortunately time is limited, snd thus when to get something done is indeed a matter of priorities, as @jfmc implied.

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented May 5, 2024

Ciao Prolog is an excellent Prolog system. For example
it is the fastest in this problem here:

does this terminate, i.e. model finder
trealla-prolog/trealla#533

But I remember that I was accused on SWI-Prolog
discourse not working towards the Prolog cause.
In my opinion Prolog providers that would behave

a little bit more client oriented would surely help
the Prolog cause. Client orientation could mean
increasing the priority to at least implement the ISO

core standard, which has div already more than 12 years:
div/2 came into ISO core Prolog in 2012:

DRAFT TECHNICAL CORRIGENDUM 2
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2#fdivplus

but have to check, maybe div/2 is already there?

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented May 5, 2024

Nope, div/2 is still not yet there:

image

Since div/2 is not there, a straight forward bootstrapping of divmod/4
is also out of reach. One could of course work around via rem/2 for
divmod/4. But this still leaves the residual problem of div/2 itself.

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

No branches or pull requests

3 participants