-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Clarify potential uses for implies
#8615
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
Conversation
Clarify how `if .. then .. else` can be equivalent to `implies` in certain scenarios.
| implication. This is just a simplified notation: ``A implies B`` is the same as writing ``(not A) or B``. | ||
| implication. This is just a simplified notation: | ||
|
|
||
| * ``A implies B`` is the same as writing ``(not A) or B``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly right -- it is desugared in this way internally. 👍
| implication. This is just a simplified notation: | ||
|
|
||
| * ``A implies B`` is the same as writing ``(not A) or B``. | ||
| * ``A implies B`` is the same as writing ``if A then B else any()``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not quite right. It means the same, but is not operationally equivalent.
The desugaring for if A then B else C is in fact (A and B) or ((not A) and C). Thus, if C is any(), the if/then/else desugars to (A and B) or (not A). We can distribute the terms to see this is equivalent to (A or (not A)) and (B or (not A)), which simplifies to (not A) or B, so logically they are the same.
Practically, however, it may be that (A and B) is much faster to evaluate than just B -- perhaps A has very few tuples, while B has very many, and so it's faster to just check whether B holds of the tuples in A rather than enumerating all the tuples in B. Or, in other words, writing if A then B else any() evaluates the then part only for contexts that satisfy the condition, while this constraint does not exist in A implies B.
[You might argue that we should therefore desugar A implies B to (not A) or (A and B), but that can be worse if A in fact does not significantly restrict the values in B, and therefore it would be a potentially breaking change. Still, it might be less surprising. 🤔]
| * ``A implies B`` is the same as writing ``if A then B else any()``. | |
| * ``A implies B`` means the same as writing ``if A then B else any()``, though the latter is in fact evaluated as ``A implies (A and B)``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... I'm actually not sure whether we want this kind of operational detail in the language reference -- how do others feel?
|
|
||
| You can use these keywords to write a conditional formula. This is another way to simplify | ||
| notation: ``if A then B else C`` is the same as writing ``(A and B) or ((not A) and C)``. | ||
| ``if A then B else any()`` can also be represented as `A implies B`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ``if A then B else any()`` can also be represented as `A implies B`. | |
| ``if A then B else any()`` can also be represented as ``A implies (A and B)``, and it means the same as ``A implies B``. |
(See my other comment for explanation.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest not adding this line at all since it encourages the use of implies. Use of implies tends to be confusing to readers who weren't in exactly the same frame of mind as the author. I'm fine with giving hints in the other direction, suggesting how to replace implies with either or or if.
jbj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We sometimes rewrite implies to the if form manually for performance reasons. That trick is probably worth mentioning somewhere, but I'm not sure the QL language reference is the ideal place. If we should mention it here, it should be clearly marked as an optimisation trick that may be manually applied.
| implication. This is just a simplified notation: ``A implies B`` is the same as writing ``(not A) or B``. | ||
| implication. This is just a simplified notation: | ||
|
|
||
| * ``A implies B`` is the same as writing ``(not A) or B``. | ||
| * ``A implies B`` is the same as writing ``if A then B else any()``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While A implies B is logically equivalent to if A then B else any(), they're not equivalent as QL formulas for all purposes. The most important differences are:
- They generate different code and can therefore have different performance. The
ifexpansion duplicates theAformula (see line 333 of this file), while theorexpansion does not. Having an extra copy ofAcould make execution slower (because there is more code to run) or faster (because more optimisations may apply toBwhen it's in the context ofA and B). - The various checks applied to a QL program will behave slightly differently in the two cases. The
orexpansion is anti-monotonic inA, while theifexpansion is non-monotonic. I'm fairly confident that the two expansions can also give different binding check results in edge cases, and QL's checks for empty formulas might also apply slightly differently.
As an example of how monotonicity is different, consider this predicate, which is a convoluted way to write i = [1,2,3,4]:
predicate foo(int i) {
i < 5 and
(not foo(i-1) implies i = 1)
}If you use the if expansion of implies, then the program no longer compiles.
Co-authored-by: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com>
|
There seems to be a lot of debate on my PR about my update to the explanation around It doesn't seem clear to me what the "best" way to express Given this, what would you like to see the documentation changed to? |
The
I don't see the need to change the documentation here - both I suggest just closing this PR. |
|
Personally, I think that |
I'm not sure what else you'd expect in the documentation? |
|
I agree with @JLLeitschuh .
|
|
Right, I see what's missing now, we're not actually documenting the "true" ( |
|
If we still want to mention something like |
Clarify how
if .. then .. elsecan be equivalent toimpliesin certain scenarios.