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

findnsols/4 what is it supposed to do? #101

Open
Jean-Luc-Picard-2021 opened this issue Oct 30, 2024 · 2 comments
Open

findnsols/4 what is it supposed to do? #101

Jean-Luc-Picard-2021 opened this issue Oct 30, 2024 · 2 comments

Comments

@Jean-Luc-Picard-2021
Copy link

The Ciao Playground gives me:

?- findnsols(3, X, between(1,6,X), L).
L = [1,2,3] ? ;
no

On the other hand SWI-Prolog gives me:

?- findnsols(3, X, between(1,6,X), L).
L = [1, 2, 3] ;
L = [4, 5, 6].

Bug or feature?

@jfmc
Copy link
Member

jfmc commented Oct 31, 2024

I'd say that this is a feature and not a bug. According to SWI's documentation https://www.swi-prolog.org/pldoc/man?predicate=findnsols/4 the SWI version is based on Ciao but added the non-deterministic behavior to implement the chunk option in library(pengines).

Ciao's behavior sounds more natural to me for findnsols/3 but SWI's extension is definitively very useful, but adopting the full SWI semantics (e.g., allow nb_setarg/3 to change the chunk size) sounds a bit scary... I'd prefer a more controlled interface, like passing some reference to the query with some interface to modify the aggregation or search parameters?

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Nov 1, 2024

An interesting improvement over findall/3 in the wild,
would work for both SWI-Prolog and Ciao Prolog?

has_unique_answer(G):-findnsols(2,G,G,Sols),!,Sols=[G].

https://arxiv.org/pdf/1709.04302

But SWI-Prolog does copy the goal G before it runs
findnsols/4. Is this necessary? I don't find that the goal
G is copied in the ordinary findall/3. Only the template.

I think the above example gives a rational why copy is necessary.
Without copy the G might show a solution, depending on
implementation of findnsols/4 maybe the 2nd or 3rd solution,

this might prevent binding G with the 1st solution.
Does Ciao Prolog copy the 3rd argument of findnsols/4,
before it runs the 3rd argument? Or does it have other

means to prevent leaking some binding?

BTW: Such abominations are not really necessary if the Prolog
system has a library(sequence). One can combine limit/2
with findall/3. There is also no goal copy issue:

has_unique_answer(G):-findall(G,limit(2,G),Sols),!,Sols=[G].

In as far the Ciao Prolog behaviour of findnsols/4 becomes
also redundant if limit/2 and findall/3 are availablle. Does
Ciao Prolog have limit/2 ?

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