|
| 1 | +--- |
| 2 | +title: Method Calls and Returns |
| 3 | +parent: Verification Features |
| 4 | +nav_order: 1 |
| 5 | +permalink: /verification/method-calls/ |
| 6 | +description: Learn how parameter and return refinements connect a method's implementation to its callers. |
| 7 | +--- |
| 8 | + |
| 9 | +# Method Calls and Returns |
| 10 | + |
| 11 | +A method contract has two sides: |
| 12 | + |
| 13 | +- **Parameters:** callers must establish each parameter's refinement. The method body can assume those refinements. |
| 14 | +- **Return value:** each return must satisfy the method's declared refinement. Callers can use that refinement for the result. |
| 15 | + |
| 16 | +```java |
| 17 | +import liquidjava.specification.Refinement; |
| 18 | + |
| 19 | +public class MethodExample { |
| 20 | + @Refinement("_ == value") |
| 21 | + public static int positiveIdentity( |
| 22 | + @Refinement("value > 0") int value) { |
| 23 | + return value; |
| 24 | + } |
| 25 | + |
| 26 | + public static void example() { |
| 27 | + @Refinement("_ == 3") int result = positiveIdentity(3); |
| 28 | + positiveIdentity(0); // Refinement Error: 0 is not positive |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +For `positiveIdentity(3)`, the verifier checks `3 > 0`. It then substitutes the argument into the return contract `_ == value`, so the result is known to equal `3`. The call with `0` fails the parameter check. |
| 34 | + |
| 35 | +The body is checked separately using its parameter contract. Returning a value that contradicts its return contract also produces an error: |
| 36 | + |
| 37 | +```java |
| 38 | +import liquidjava.specification.Refinement; |
| 39 | + |
| 40 | +public class ReturnExample { |
| 41 | + @Refinement("_ > 0") |
| 42 | + public static int positive() { |
| 43 | + return 0; // Refinement Error: 0 is not positive |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Write the return properties callers need explicitly. For example, without a return refinement on `positiveIdentity`, callers should not rely on the verifier inspecting its body to discover that the result equals the argument. |
| 49 | + |
| 50 | +## Object State at a Call |
| 51 | + |
| 52 | +For a method with [state refinements]({{ '/annotations/state-refinement/' | relative_url }}), the verifier also checks that the receiver satisfies `from` before the call and uses `to` to describe its state afterward. A constructor's `to` establishes the initial state. This is how a protocol can reject a `read()` call after `close()`. |
| 53 | + |
| 54 | +For library methods whose source is outside the checked code, use [external refinements]({{ '/annotations/external-refinements-for/' | relative_url }}) to supply contracts. Those contracts describe the library behavior the verifier relies on; they do not verify the library's implementation. |
0 commit comments