-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add getOnlyElement()
built in operator
#1525
Conversation
Perhaps, you could just use Please do not use |
This is a workaround, but it doesn't make the assumption that the set is a singleton explicit - and calling a fold on a set with many elements means that the result depends on the order of fold, and then we end up with the same problem as in I actually wrote this different hacky version:
But I think having the builtin support with proper error messages is a better idea. Also on this topic, I'm planning to make the Option type a builtin and adding
Oh right! I guess I can write \* S.getOnlyElement()
CHOOSE x \in S: S = {x} I was playing with some versions to see if Apalache would report an error for an empty |
Well, in this case, you could still use |
Good. Please do not add any kind of exceptions. This makes program analysis much harder. You can model |
Hello
Context
chooseSome
is a tricky operator to implement as, in theory, it should be deterministic. However, people are struggling with the lack ofchooseSome
even in scenarios with no room for non-determinism. So, we decided to at least give them something that only works in this scenarios:getOnlyElement
.This won't help with cases like choosing a base element for fold, but it will help in scenarios that involve taking the single element of a singleton (a set with exactly one element).
General example:
MySet.filter(e => e.name == "Gabriela")
. If I know that names are unique in this set, I know that this will result in a singleton. I should be able to extract the element out of it.Special case - when I want to evaluate
f(x)
for an element of the set, and I know thatf(x)
has the same exact result for all elements in the set (∀ x,y ∈ S : f(x) = f(y)
).MySet.map(f)
and obtain a singletonf(x)
for any elementx
, which is now the only element in this set.getOnlyElement()
is enough for these scenarios.Behavior
This operator only works in singletons. If it is called with a set of size different than 1, it will result in an error.
Apalache
I still haven't added this to the Apalache translation. We should somehow force a similar behavior to Quint* when it is called with a non-singleton (as opposed to allowing Apalache to do a not-fully-deterministic choice using it's own
CHOOSE
implementation). I'm thinking something like:CHANGELOG.md
for any new functionalityREADME.md
updated for any listed functionality