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

maybe/2 would be a useful addition to library(random) #2670

Open
triska opened this issue Nov 30, 2024 · 6 comments
Open

maybe/2 would be a useful addition to library(random) #2670

triska opened this issue Nov 30, 2024 · 6 comments

Comments

@triska
Copy link
Contributor

triska commented Nov 30, 2024

A generalization of maybe/0 would be a useful addition to library(random):

maybe(+K, +N), succeeding with probability K/N.

This is needed for library(clpb), where we currently have:

?- sat(A+B), random_labeling(0, [A,B]).
   error(existence_error(procedure,maybe/2),maybe/2), unexpected.

@notoria and everyone who is interested in arithmetic and probabilistic functionality, I would greatly appreciate your help with this!

@UWN
Copy link

UWN commented Dec 1, 2024

Note that there are systems (the original CLP(R) comes to mind) that do not produce as a full leaf answer containing constraints but instead just say maybe. Not sure this is such a good idea to repurpose this name.

@triska
Copy link
Contributor Author

triska commented Dec 1, 2024

Yes indeed, very good point, a better name is also worth considering!

@hurufu
Copy link
Contributor

hurufu commented Dec 1, 2024

Can this be enough?

maybe(K, N) :-
    maplist(integer_si, [K, N]),
    random_integer(0, N, X),
    X < K.

@triska
Copy link
Contributor Author

triska commented Dec 1, 2024

Something in this direction would be perfect, if the bounds are correctly chosen to yield the correct probabilities!

@hurufu
Copy link
Contributor

hurufu commented Dec 2, 2024

Also just a comment. For any serious security based application Prolog language should support multiple RNG instances for different purposes. For example if a program uses the same RNG to both generate access tokens and for UI animation, then it is much easier to predict next access tokens just by observing UI animation.

@bakaq
Copy link
Contributor

bakaq commented Dec 2, 2024

Yes, ideally we need a way to explicitly generate and pass an "rng context" to rng functions so that we have control of this. It would also make things more declarative, depending on how the rng context works.

triska added a commit to triska/scryer-prolog that referenced this issue Dec 8, 2024
This is until library(random) provides this functionality,
and a good name is found for it. See mthom#2670.
triska added a commit to triska/scryer-prolog that referenced this issue Dec 8, 2024
Many thanks to @hurufu for the very nice definition!

This is until library(random) provides this functionality,
and a good name is found for it. See mthom#2670.
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

4 participants