Skip to content

Commit ffc020a

Browse files
author
davidcok
committed
More editing
1 parent 3f4a476 commit ffc020a

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

tutorial/AssertStatement.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: JML Tutorial - Assert statements (assert
2+
title: JML Tutorial - Assert statements (assert and check)
33
---
44
A JML *assert* statement states a condition that is expected to hold at a point
55
within the body of a method. Such statements are not part of a method's interface
@@ -55,4 +55,36 @@ produces similar output:
5555
{% include_relative T_assert3.out %}
5656
```
5757

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+
5890
## **[Assert Statements Problem Set](https://www.openjml.org/tutorial/exercises/AssertEx.html)**

tutorial/Loops.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Here is a typical for-each loop:
7070
{% include_relative T_foreach.java %}
7171
```
7272
Note that the (second) loop invariant states that so far (up to array index `\count`) all array elements are positive. That is because at the beginning of any loop iteration,
73-
all elements seen have been positive. As soon as a non-positive element is seen, the loop exists prematurely, but the verifier can follow this control flow to prove that
73+
all elements seen have been positive. As soon as a non-positive element is seen, the loop exits prematurely, but the verifier can follow this control flow to prove that
7474
the postcondition is valid for either exit.
7575

7676
Note also the use of `\count` as a stand-in for a loop counter and the use of `\nothing` to say that nothing is modified in the loop body, other than `\count`, which is always included as a modified variable.

tutorial/SpecStatements.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ The basic unit of specification and verification in JML is the method,
66
with the method body being compared to the method's specification.
77
However, sometimes specifications within the body of a method can
88
assist or are required to be able to prove that the body meets the
9-
method specification.
9+
method specification, and they can also be helpful in documenting
10+
a method body in a machine-checkable way.
11+
Such specification statements are not, however,
12+
part of the method's specification -- for example, they are not visible to
13+
calling methods.
1014

1115
The most common such specification statements are `assert`, `assume`,
1216
and `ghost` declaration statements. In addition, loops in a method
1317
body require a loop specification to be able to reason about the
14-
effect of that method body.s
18+
effect of that method body.

tutorial/T_assert4.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// openjml --esc T_assert4.java
2+
3+
public class T_assert4 {
4+
public int f;
5+
6+
public void example(/*@ nullable */ T_assert4 t) {
7+
//@ check t != null; // ERROR, because not necessarily so
8+
int k = t.f; // ERROR because t might be null
9+
}
10+
11+
public void example2(/*@ nullable */ T_assert4 t) {
12+
//@ assert t != null; // ERROR, because not necessarily so
13+
// but subsequently assumes it is true
14+
int k = t.f; // OK
15+
}
16+
17+
}

tutorial/T_assert4.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
T_assert4.java:8: verify: The prover cannot establish an assertion (PossiblyNullDeReference) in method example
2+
int k = t.f; // ERROR because t might be null
3+
^
4+
T_assert4.java:7: verify: The prover cannot establish an assertion (Assert) in method example
5+
//@ check t != null; // ERROR, because not necessarily so
6+
^
7+
T_assert4.java:12: verify: The prover cannot establish an assertion (Assert) in method example2
8+
//@ assert t != null; // ERROR, because not necessarily so
9+
^
10+
3 verification failures

0 commit comments

Comments
 (0)