ciao
ciao copied to clipboard
Feature request predicate divmod/4
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?
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
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.
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?
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?
What would be also useful, these evaluable functions:
- getbit/2: https://www.swi-prolog.org/pldoc/man?function=getbit/2
- msb/1: https://www.swi-prolog.org/pldoc/doc_for?object=f(msb/1)
- lsb/1: https://www.swi-prolog.org/pldoc/doc_for?object=f(lsb/1)
- What else?
Are they somewhere under a different name?
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?
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.
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?
Nope, div/2 is still not yet there:
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.