CProver manual
|
A requires clause specifies a precondition for a function, i.e., a property that must hold to properly execute the operation. Developers may see the requires clauses of a function as obligations on the caller when invoking the function. The precondition of a function is the conjunction of the requires clauses, or true
if none are specified.
An ensures clause specifies a postcondition for a function, i.e., a property over arguments or global variables that the function guarantees at the end of the operation. Developers may see the ensures clauses of a function as the obligations of that function to the caller. The postcondition of a function is the conjunction of the ensures clauses, or true
if none are specified.
A requires clause takes a Boolean expression over the arguments of a function and/or global variables, including CBMC primitive functions (e.g., Memory Predicates). Similarly, ensures clauses also accept Boolean expressions and CBMC primitives, but also History Variables and __CPROVER_return_value
.
Important. Developers may call functions inside requires and ensures clauses to better write larger specifications (e.g., predicates). However, at this point CBMC does not enforce such functions to be without side effects (i.e., do not modify any global state). This will be checked in future versions.
The semantics of ensures and requires clauses can be understood in two contexts: enforcement and replacement. To illustrate these two perspectives, consider the following implementation of the sum
function.
In order to determine whether requires and ensures clauses are a sound abstraction of the behavior of a function f, CBMC will try to check them as follows:
__CPROVER_requires
clauses;__CPROVER_ensures
clauses.In our example, the sum
function will be instrumented as follows:
Assuming requires and ensures clauses are a sound abstraction of the behavior of the function f, CBMC will use the function contract in place of the function implementation as follows:
__CPROVER_requires
clauses;__CPROVER_assigns
clause (see Assigns Clause for details);__CPROVER_ensures
clauses;In our example, consider that a function foo
may call sum
.
CBMC will use the function contract in place of the function implementation wherever the function is called.
Last modified: 2022-09-29 15:10:20 -0400