Boolean operators should return true for successful evaluation #144
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A guard can be a boolean expression. As an example, here is a simple guard using the boolean operator for equals:
The value of this boolean expression is true, so the guard succeeds. But now consider:
The first expression,
(> 1 2)
evaluates to false. So equals should check false equals false, and the equals expression result should be true as before. But this is not the current behavior. The bug is in the code for all boolean operators.To show the cause of the bug, first consider the arithmetic operator for logarithm, which works correctly.
Note that the operator returns a boolean value which is true if the expression was evaluated successfully or false for a problem such as arguments of the wrong type. You can see that
log
returns false if the argument isn't a float. The actual result of the expression is placed in the context object.Now consider the "greater than" operator used above. It successfully evaluates the expression result
r
and places the result in the context object. Because it was successfully evaluated it should return true. But instead, it returnsr
, which is this case is false. The guard evaluator sees that an expression failed to be evaluated, so the guard fails, even though it should succeed.This pull request has two commits. The first commit updates the guard evaluator for programs and HLP (models and composite states) to independently check the result of the expression. If the result type is boolean, and the value is false, then the guard fails. (We have to check the type of the expression result because models and composite states can have assignment guards which are not boolean.)
The second commit updates the boolean operators to return true for successful evaluation, even if the expression value is false. Now, The code for the "greater than" operator evaluates
(> 1 2)
and returns true (for successful evaluation). The expression value of false is stored in the context object. Then the code for the "equals" operator evaluates(= (> 1 2) false)
. It gets the value of false from the context object of the first sub expression, compares it tofalse
and stores the expression result of true in the context, as expected.