|
1 | 1 | --- |
2 | | -title: JML Tutorial - Assert statements (assert |
| 2 | +title: JML Tutorial - Assert statements (assert and check) |
3 | 3 | --- |
4 | 4 | A JML *assert* statement states a condition that is expected to hold at a point |
5 | 5 | within the body of a method. Such statements are not part of a method's interface |
@@ -55,4 +55,36 @@ produces similar output: |
55 | 55 | {% include_relative T_assert3.out %} |
56 | 56 | ``` |
57 | 57 |
|
| 58 | +## Check statement |
| 59 | + |
| 60 | +The `check` statement (e.g. `check neg < 0;`) is similar to the `assert` statement. |
| 61 | +It also effects a test of whether the given predicate is always true. |
| 62 | +However, the two statements differ in their effect on the subsequent logic |
| 63 | +of the program: |
| 64 | + |
| 65 | +* A `check` statement tests the predicate but makes no assumption about whether the |
| 66 | +predicate is subsequently true or false. A `check` statement essentially says, |
| 67 | +please just check whether the given predicate is true or false. |
| 68 | +* An `assert` predicate tests the predicate and then _assumes that it is subsequently true_. |
| 69 | +An `assert` statement essentially says, this predicate is supposed to be true, so pleasae test it |
| 70 | +and assume it to be true for analyzing subsequent code. |
| 71 | + |
| 72 | +For example, |
| 73 | + |
| 74 | +``` |
| 75 | +{% include_relative T_assert4.java %} |
| 76 | +``` |
| 77 | + |
| 78 | +produces this output: |
| 79 | + |
| 80 | +``` |
| 81 | +{% include_relative T_assert4.out %} |
| 82 | +``` |
| 83 | + |
| 84 | +This explanation points to a potentially confusing point about `assert` statements. If |
| 85 | +the given predicate is always false, then the implicit assumption, after the assert check, |
| 86 | +that it is true causes a complete halt to the symbolic execution -- there is no pre-state |
| 87 | +that satisfies the assert/assume combination. A `check` would be better to be used in such cases. |
| 88 | + |
| 89 | + |
58 | 90 | ## **[Assert Statements Problem Set](https://www.openjml.org/tutorial/exercises/AssertEx.html)** |
0 commit comments