ciao-lang / ciao

Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
https://ciao-lang.org
GNU Lesser General Public License v3.0
268 stars 20 forks source link

Feature request predicate divmod/4 #71

Open Jean-Luc-Picard-2021 opened 1 year ago

Jean-Luc-Picard-2021 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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?

jfmc commented 1 year ago

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 commented 1 year ago

What would be also useful, these evaluable functions:

Are they somewhere under a different name?

Jean-Luc-Picard-2021 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

does this terminate, i.e. model finder https://github.com/trealla-prolog/trealla/issues/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 commented 4 months ago

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.