7.9.2. Loop Examples

The examples in this section contain loops, and thus require in general that users write suitable Loop Invariants. We start by explaining the need for a loop invariant, and we continue with a description of the most common patterns of loops and their loop invariant. We summarize each pattern in a table of the following form:

Loop Pattern

Loop Over Data Structure

Proof Objective

Establish property P.

Loop Behavior

Loops over the data structure and establishes P.

Loop Invariant

Property P is established for the part of the data structure looped over so far.

The examples in this section use the types defined in package Loop_Types:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
with Ada.Containers.Formal_Doubly_Linked_Lists;
with Ada.Containers.Formal_Vectors;

package Loop_Types
  with SPARK_Mode
is
   subtype Index_T is Positive range 1 .. 1000;
   subtype Opt_Index_T is Natural range 0 .. 1000;
   subtype Component_T is Natural;

   type Arr_T is array (Index_T) of Component_T;

   package Vectors is new Ada.Containers.Formal_Vectors (Index_T, Component_T);
   subtype Vec_T is Vectors.Vector;

   package Lists is new Ada.Containers.Formal_Doubly_Linked_Lists (Component_T);
   subtype List_T is Lists.List;

   type List_Cell;
   type List_Acc is access List_Cell;
   type List_Cell is record
      Value : Component_T;
      Next  : List_Acc;
   end record;

   function At_End
     (L : access constant List_Cell) return access constant List_Cell
   is (L)
   with Ghost,
     Annotate => (GNATprove, At_End_Borrow);

   type Property is access function (X : Component_T) return Boolean;

   function For_All_List
     (L : access constant List_Cell;
      P : not null Property) return Boolean
   is
     (L = null or else (P (L.Value) and then For_All_List (L.Next, P)))
   with Annotate => (GNATprove, Terminating);
   pragma Annotate (GNATprove, False_Positive, "is recursive",
                    "The recursive call occurs on a strictly smaller list");
   pragma Annotate (GNATprove, False_Positive, "call via access-to-subprogram",
                    "We only call For_All_List on terminating functions");

   type Relation is access function (X, Y : Component_T) return Boolean;

   function For_All_List
     (L1, L2 : access constant List_Cell;
      P      : not null Relation) return Boolean
   is
     ((L1 = null) = (L2 = null)
      and then
        (if L1 /= null
         then P (L1.Value, L2.Value)
         and then For_All_List (L1.Next, L2.Next, P)))
   with Annotate => (GNATprove, Terminating);
   pragma Annotate (GNATprove, False_Positive, "is recursive",
                    "The recursive call occurs on a strictly smaller lists");
   pragma Annotate (GNATprove, False_Positive, "call via access-to-subprogram",
                    "We only call For_All_List on terminating functions");

end Loop_Types;

As there is no built-in way to iterate over the elements of a recursive data structure, the first function For_All_List can be used to state that all elements of a list have a given property. The second variant of For_All_List takes two lists and states that both lists have the same number of elements and that the corresponding elements of both lists are related by the given relation. The function At_End is used to refer to the value of a borrowed list or a local borrower at the end of the borrow, see Referring to a Value at the End of a Local Borrow for more explanations.

Note

We cannot currently prove the termination of For_All_List for two reasons. First, as it is a recursive function, we would need to provide a Subprogram_Variant to prove that the call chain is bounded. Currently, structural variants are not supported and we have not defined a notion of length on lists. The second is that we have no way for now to state on the access-to-subprogram type Property that all elements of this type must terminate. Therefore, we justify these checks, see section on Justifying Check Messages.

7.9.2.1. The Need for a Loop Invariant

Consider a simple procedure that increments its integer parameter X a number N of times:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procedure Increment_Loop (X : in out Integer; N : Natural) with
  SPARK_Mode,
  Pre  => X <= Integer'Last - N,
  Post => X = X'Old + N
is
begin
   for I in 1 .. N loop
      X := X + 1;
   end loop;
end Increment_Loop;

The precondition of Increment_Loop ensures that there is no overflow when incrementing X in the loop, and its postcondition states that X has been incremented N times. This contract is a generalization of the contract given for a single increment in Increment. GNATprove does not manage to prove either the absence of overflow or the postcondition of Increment_Loop:


increment_loop.adb:4:11: medium: postcondition might fail
    4 |  Post => X = X'Old + N
      |          ^~~~~~~~~~~~~
  e.g. when N = 1
        and X = 1
        and X'Old = 1
  possible fix: loop at line 7 should mention X in a loop invariant
    7 |   for I in 1 .. N loop
      |                   ^ here

increment_loop.adb:8:14: medium: overflow check might fail, cannot prove upper bound for X + 1
    8 |      X := X + 1;
      |           ~~^~~
  e.g. when X = Integer'Last
  reason for check: result of addition must fit in a 32-bits machine integer
  possible fix: loop at line 7 should mention X in a loop invariant
    7 |   for I in 1 .. N loop
      |                   ^ here

As described in How to Write Loop Invariants, this is because variable X is modified in the loop, hence GNATprove knows nothing about it unless it is stated in a loop invariant. If we add such a loop invariant, as suggested by the possible explanation in the message issued by GNATprove, that describes precisely the value of X in each iteration of the loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
procedure Increment_Loop_Inv (X : in out Integer; N : Natural) with
  SPARK_Mode,
  Pre  => X <= Integer'Last - N,
  Post => X = X'Old + N
is
begin
   for I in 1 .. N loop
      X := X + 1;
      pragma Loop_Invariant (X = X'Loop_Entry + I);
   end loop;
end Increment_Loop_Inv;

then GNATprove proves both the absence of overflow and the postcondition of Increment_Loop_Inv:

increment_loop_inv.adb:3:29: info: overflow check proved
increment_loop_inv.adb:4:11: info: postcondition proved
increment_loop_inv.adb:4:21: info: overflow check proved
increment_loop_inv.adb:8:14: info: overflow check proved
increment_loop_inv.adb:9:30: info: loop invariant preservation proved
increment_loop_inv.adb:9:30: info: loop invariant initialization proved
increment_loop_inv.adb:9:47: info: overflow check proved

Fortunately, many loops fall into some broad categories for which the loop invariant is known. In the following sections, we describe these common patterns of loops and their loop invariant, which involve in general iterating over the content of a collection (either an array, a container from the Formal Containers Library, or a pointer-based linked list).

7.9.2.2. Initialization Loops

This kind of loops iterates over a collection to initialize every element of the collection to a given value:

Loop Pattern

Separate Initialization of Each Element

Proof Objective

Every element of the collection has a specific value.

Loop Behavior

Loops over the collection and initializes every element of the collection.

Loop Invariant

Every element initialized so far has its specific value.

In the simplest case, every element is assigned the same value. For example, in procedure Init_Arr_Zero below, value zero is assigned to every element of array A:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
with Loop_Types; use Loop_Types;

procedure Init_Arr_Zero (A : out Arr_T) with
  SPARK_Mode,
  Post => (for all J in A'Range => A(J) = 0)
is
   pragma Annotate (GNATprove, False_Positive, """A"" might not be initialized",
                    "Entire array is initialized element-by-element in a loop");
begin
   for J in A'Range loop
      A(J) := 0;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = 0);
      pragma Annotate (GNATprove, False_Positive, """A"" might not be initialized",
                       "Part of array up to index J is initialized at this point");
   end loop;
end Init_Arr_Zero;

The loop invariant expresses that all elements up to the current loop index J have the value zero. With this loop invariant, GNATprove is able to prove the postcondition of Init_Arr_Zero, namely that all elements of the array have value zero:

init_arr_zero.adb:5:11: info: postcondition proved
init_arr_zero.adb:12:30: info: loop invariant initialization proved
init_arr_zero.adb:12:30: info: loop invariant preservation proved
init_arr_zero.adb:12:61: info: index check proved

In the example above, pragma Annotate is used in Init_Arr_Zero to justify a message issued by flow analysis, about the possible read of uninitialized value A(K) in the loop invariant. Indeed, flow analysis is not currently able to infer that all elements up to the loop index J have been initialized, hence it issues a message that "A" might not be initialized. For more details, see section on Justifying Check Messages.

To verify this loop completely, it is possible to annotate A with the Relaxed_Initialization aspect to use proof to verify its correct initialization (see Aspect Relaxed_Initialization and Attribute Initialized for more details). In this case, the loop invariant should be extended to state that the elements of A have been initialized by the loop up to the current index:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
with Loop_Types; use Loop_Types;

procedure Init_Arr_Zero (A : out Arr_T) with
  SPARK_Mode,
  Relaxed_Initialization => A,
  Post => A'Initialized and then (for all J in A'Range => A(J) = 0)
is
begin
   for J in A'Range loop
      A(J) := 0;
      pragma Loop_Invariant (for all K in A'First .. J => A(K)'Initialized);
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = 0);
   end loop;
end Init_Arr_Zero;

Remark that the postcondition of Init_Arr_Zero also needs to state that A is entirely initialized by the call.

Consider now a variant of the same initialization loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;

procedure Init_Vec_Zero (V : in out Vec_T) with
  SPARK_Mode,
  Post => (for all J in First_Index (V) .. Last_Index (V) => Element (V, J) = 0)
is
begin
   for J in First_Index (V) .. Last_Index (V) loop
      Replace_Element (V, J, 0);
      pragma Loop_Invariant (Last_Index (V) = Last_Index (V)'Loop_Entry);
      pragma Loop_Invariant (for all K in First_Index (V) .. J => Element (V, K) = 0);
   end loop;
end Init_Vec_Zero;

Like before, the loop invariant expresses that all elements up to the current loop index J have the value zero. Another loop invariant is needed here to express that the length of the vector does not change in the loop: as variable V is modified in the loop, GNATprove does not know its length stays the same (for example, calling procedure Append or Delete_Last would change this length) unless the user says so in the loop invariant. This is different from arrays whose length cannot change. With this loop invariant, GNATprove is able to prove the postcondition of Init_Vec_Zero, namely that all elements of the vector have value zero:

init_vec_zero.adb:5:11: info: postcondition proved
init_vec_zero.adb:5:62: info: precondition proved
init_vec_zero.adb:5:74: info: range check proved
init_vec_zero.adb:9:07: info: precondition proved
init_vec_zero.adb:9:27: info: range check proved
init_vec_zero.adb:10:30: info: loop invariant initialization proved
init_vec_zero.adb:10:30: info: loop invariant preservation proved
init_vec_zero.adb:11:30: info: loop invariant initialization proved
init_vec_zero.adb:11:30: info: loop invariant preservation proved
init_vec_zero.adb:11:67: info: precondition proved
init_vec_zero.adb:11:79: info: range check proved

Similarly, consider a variant of the same initialization loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Init_List_Zero (L : in out List_T) with
  SPARK_Mode,
  Post => (for all E of L => E = 0)
is
   Cu : Cursor := First (L);
begin
   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
                               Element (Model (L), I) = 0);
      Replace_Element (L, Cu, 0);
      Next (L, Cu);
   end loop;
end Init_List_Zero;

Contrary to arrays and vectors, lists are not indexed. Instead, a cursor can be defined to iterate over the list. The loop invariant expresses that all elements up to the current cursor Cu have the value zero. To access the element stored at a given position in a list, we use the function Model which computes the mathematical sequence of the elements stored in the list. The position of a cursor in this sequence is retrieved using the Positions function. Contrary to the case of vectors, no loop invariant is needed to express that the length of the list does not change in the loop, because the postcondition remains provable here even if the length of the list changes. With this loop invariant, GNATprove is able to prove the postcondition of Init_List_Zero, namely that all elements of the list have value zero:

init_list_zero.adb:6:11: info: postcondition proved
init_list_zero.adb:6:12: info: precondition proved
init_list_zero.adb:11:30: info: loop invariant initialization proved
init_list_zero.adb:11:30: info: loop invariant preservation proved
init_list_zero.adb:11:49: info: precondition proved
init_list_zero.adb:12:32: info: precondition proved
init_list_zero.adb:13:07: info: precondition proved
init_list_zero.adb:14:07: info: precondition proved

The case of sets and maps is similar to the case of lists.

Note

The parameter of Init_Vec_Zero and Init_List_Zero is an in out parameter. This is because some components of the vector/list parameter are preserved by the initialization procedure (in particular the component corresponding to its length). This is different from Init_Arr_Zero which takes an out parameter, as all components of the array are initialized by the procedure (the bounds of an array are not modifiable, hence considered separately from the parameter mode).

Consider now a variant of the same initialization loop over a pointer-based list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Is_Zero (X : Component_T) return Boolean is
     (X = 0);

   procedure Init_List_Zero (L : access List_Cell) with
     Post => For_All_List (L, Is_Zero'Access);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   procedure Init_List_Zero (L : access List_Cell) is
      B : access List_Cell := L;
   begin
      while B /= null loop
         pragma Loop_Invariant
           (if For_All_List (At_End (B), Is_Zero'Access)
            then For_All_List (At_End (L), Is_Zero'Access));
         B.Value := 0;
         B := B.Next;
      end loop;
   end Init_List_Zero;
end P;

Like in the other variants, the postcondition of Init_List_Zero states that the elements of the list L after the call are all 0. It uses the For_All_List function from Loop_Types to quantify over all the elements of the list. The loop iterates over the list L using a local borrower B which is a local variable which borrows the ownership of a part of a datastructure for the duration of its scope, see Borrowing for more details. The loop invariant uses the At_End function to express properties about the values of L and B at the end of the borrow. It states that the elements of L at the end of the borrow will all be 0 if the elements of B at the end of the borrow are all 0. This is provable because we know while verifying the invariant that the already traversed elements were all set to 0 and that they can no longer be changed during the scope of B. With this loop invariant, GNATprove is able to prove the postcondition of Init_List_Zero:

p.adb:11:13: info: loop invariant initialization proved
p.adb:11:13: info: loop invariant preservation proved
p.adb:11:49: info: null exclusion check proved
p.adb:12:51: info: null exclusion check proved
p.adb:13:11: info: pointer dereference check proved
p.adb:14:16: info: pointer dereference check proved
p.ads:10:14: info: postcondition proved
p.ads:10:38: info: null exclusion check proved

Consider now a case where the value assigned to each element is not the same. For example, in procedure Init_Arr_Index below, each element of array A is assigned the value of its index:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
with Loop_Types; use Loop_Types;

procedure Init_Arr_Index (A : out Arr_T) with
  SPARK_Mode,
  Post => (for all J in A'Range => A(J) = J)
is
   pragma Annotate (GNATprove, False_Positive, """A"" might not be initialized",
                    "Entire array is initialized element-by-element in a loop");
begin
   for J in A'Range loop
      A(J) := J;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = K);
      pragma Annotate (GNATprove, False_Positive, """A"" might not be initialized",
                       "Part of array up to index J is initialized at this point");
   end loop;
end Init_Arr_Index;

The loop invariant expresses that all elements up to the current loop index J have the value of their index. With this loop invariant, GNATprove is able to prove the postcondition of Init_Arr_Index, namely that all elements of the array have the value of their index:

init_arr_index.adb:5:11: info: postcondition proved
init_arr_index.adb:12:30: info: loop invariant initialization proved
init_arr_index.adb:12:30: info: loop invariant preservation proved
init_arr_index.adb:12:61: info: index check proved

As for Init_Arr_Zero above, it is possible to annotate A with the Relaxed_Initialization aspect to use proof to verify its correct initialization:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
with Loop_Types; use Loop_Types;

procedure Init_Arr_Index (A : out Arr_T) with
  SPARK_Mode,
  Relaxed_Initialization => A,
  Post => A'Initialized and then (for all J in A'Range => A(J) = J)
is
begin
   for J in A'Range loop
      A(J) := J;
      pragma Loop_Invariant (for all K in A'First .. J => A(K)'Initialized);
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = K);
   end loop;
end Init_Arr_Index;

Everything is proved by GNATprove:

init_arr_index.adb:6:11: info: postcondition proved
init_arr_index.adb:6:59: info: initialization check proved
init_arr_index.adb:11:30: info: loop invariant initialization proved
init_arr_index.adb:11:30: info: loop invariant preservation proved
init_arr_index.adb:11:61: info: index check proved
init_arr_index.adb:12:30: info: loop invariant preservation proved
init_arr_index.adb:12:30: info: loop invariant initialization proved
init_arr_index.adb:12:59: info: initialization check proved
init_arr_index.adb:12:61: info: index check proved

Similarly, variants of Init_Vec_Zero and Init_List_Zero that assign a different value to each element of the collection would be proved by GNATprove.

7.9.2.3. Mapping Loops

This kind of loops iterates over a collection to map every element of the collection to a new value:

Loop Pattern

Separate Modification of Each Element

Proof Objective

Every element of the collection has an updated value.

Loop Behavior

Loops over the collection and updates every element of the collection.

Loop Invariant

Every element updated so far has its specific value.

In the simplest case, every element is assigned a new value based only on its initial value. For example, in procedure Map_Arr_Incr below, every element of array A is incremented by one:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
with Loop_Types; use Loop_Types;

procedure Map_Arr_Incr (A : in out Arr_T) with
  SPARK_Mode,
  Pre  => (for all J in A'Range => A(J) /= Component_T'Last),
  Post => (for all J in A'Range => A(J) = A'Old(J) + 1)
is
begin
   for J in A'Range loop
      A(J) := A(J) + 1;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = A'Loop_Entry(K) + 1);
      --  The following loop invariant is generated automatically by GNATprove:
      --  pragma Loop_Invariant (for all K in J + 1 .. A'Last => A(K) = A'Loop_Entry(K));
   end loop;
end Map_Arr_Incr;

The loop invariant expresses that all elements up to the current loop index J have been incremented (using Attribute Loop_Entry). With this loop invariant, GNATprove is able to prove the postcondition of Map_Arr_Incr, namely that all elements of the array have been incremented:

map_arr_incr.adb:6:11: info: postcondition proved
map_arr_incr.adb:6:52: info: overflow check proved
map_arr_incr.adb:10:20: info: overflow check proved
map_arr_incr.adb:11:30: info: loop invariant initialization proved
map_arr_incr.adb:11:30: info: loop invariant preservation proved
map_arr_incr.adb:11:61: info: index check proved
map_arr_incr.adb:11:79: info: index check proved
map_arr_incr.adb:11:82: info: overflow check proved

Note that the commented loop invariant expressing that other elements have not been modified is not needed, as it is an example of Automatically Generated Loop Invariants.

Consider now a variant of the same initialization loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pragma Unevaluated_Use_Of_Old (Allow);
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;
use Loop_Types.Vectors.Formal_Model;

procedure Map_Vec_Incr (V : in out Vec_T) with
  SPARK_Mode,
  Pre  => (for all I in 1 .. Last_Index (V) =>
               Element (V, I) /= Component_T'Last),
  Post => Last_Index (V) = Last_Index (V)'Old
  and then (for all I in 1 .. Last_Index (V) =>
                 Element (V, I) = Element (Model (V)'Old, I) + 1)
is
begin
   for J in 1 .. Last_Index (V) loop
      pragma Loop_Invariant (Last_Index (V) = Last_Index (V)'Loop_Entry);
      pragma Loop_Invariant
        (for all I in 1 .. J - 1 =>
           Element (V, I) = Element (Model (V)'Loop_Entry, I) + 1);
      pragma Loop_Invariant
        (for all I in J .. Last_Index (V) =>
           Element (V, I) = Element (Model (V)'Loop_Entry, I));
      Replace_Element (V, J, Element (V, J) + 1);
   end loop;
end Map_Vec_Incr;

Like before, we need an additionnal loop invariant to state that the length of the vector is not modified by the loop. The other two invariants are direct translations of those used for the loop over arrays: the first one expresses that all elements up to the current loop index J have been incremented, and the second one expresses that other elements have not been modified. Note that, as formal vectors are limited, we need to use the Model function of vectors to express the set of elements contained in the vector before the loop (using attributes Loop_Entry and Old). With this loop invariant, GNATprove is able to prove the postcondition of Map_Vec_Incr, namely that all elements of the vector have been incremented:

map_vec_incr.adb:8:16: info: precondition proved
map_vec_incr.adb:8:28: info: range check proved
map_vec_incr.adb:9:11: info: postcondition proved
map_vec_incr.adb:11:18: info: precondition proved
map_vec_incr.adb:11:30: info: range check proved
map_vec_incr.adb:11:35: info: precondition proved
map_vec_incr.adb:11:59: info: range check proved
map_vec_incr.adb:11:62: info: overflow check proved
map_vec_incr.adb:15:30: info: loop invariant initialization proved
map_vec_incr.adb:15:30: info: loop invariant preservation proved
map_vec_incr.adb:17:10: info: loop invariant initialization proved
map_vec_incr.adb:17:10: info: loop invariant preservation proved
map_vec_incr.adb:18:12: info: precondition proved
map_vec_incr.adb:18:24: info: range check proved
map_vec_incr.adb:18:29: info: precondition proved
map_vec_incr.adb:18:60: info: range check proved
map_vec_incr.adb:18:63: info: overflow check proved
map_vec_incr.adb:20:10: info: loop invariant preservation proved
map_vec_incr.adb:20:10: info: loop invariant initialization proved
map_vec_incr.adb:21:12: info: precondition proved
map_vec_incr.adb:21:24: info: range check proved
map_vec_incr.adb:21:29: info: precondition proved
map_vec_incr.adb:21:60: info: range check proved
map_vec_incr.adb:22:07: info: precondition proved
map_vec_incr.adb:22:27: info: range check proved
map_vec_incr.adb:22:30: info: precondition proved
map_vec_incr.adb:22:42: info: range check proved
map_vec_incr.adb:22:45: info: overflow check proved

Similarly, consider a variant of the same mapping loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Map_List_Incr (L : in out List_T) with
  SPARK_Mode,
  Pre  => (for all E of L => E /= Component_T'Last),
  Post => Length (L) = Length (L)'Old
  and then (for all I in 1 .. Length (L) =>
                 Element (Model (L), I) = Element (Model (L'Old), I) + 1)
is
   Cu : Cursor := First (L);
begin
   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (Length (L) = Length (L)'Loop_Entry);
      pragma Loop_Invariant
        (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
           Element (Model (L), I) = Element (Model (L'Loop_Entry), I) + 1);
      pragma Loop_Invariant
        (for all I in P.Get (Positions (L), Cu) .. Length (L) =>
           Element (Model (L), I) = Element (Model (L'Loop_Entry), I));
      Replace_Element (L, Cu, Element (L, Cu) + 1);
      Next (L, Cu);
   end loop;
end Map_List_Incr;

Like before, we need to use a cursor to iterate over the list. The loop invariants express that all elements up to the current loop index J have been incremented and that other elements have not been modified. Note that it is necessary to state here that the length of the list is not modified during the loop. It is because the length is used to bound the quantification over the elements of the list both in the invariant and in the postcondition. With this loop invariant, GNATprove is able to prove the postcondition of Map_List_Incr, namely that all elements of the list have been incremented:

map_list_incr.adb:6:12: info: precondition proved
map_list_incr.adb:7:11: info: postcondition proved
map_list_incr.adb:9:18: info: precondition proved
map_list_incr.adb:9:43: info: precondition proved
map_list_incr.adb:9:70: info: overflow check proved
map_list_incr.adb:14:30: info: loop invariant initialization proved
map_list_incr.adb:14:30: info: loop invariant preservation proved
map_list_incr.adb:16:10: info: loop invariant initialization proved
map_list_incr.adb:16:10: info: loop invariant preservation proved
map_list_incr.adb:16:29: info: precondition proved
map_list_incr.adb:17:12: info: precondition proved
map_list_incr.adb:17:37: info: precondition proved
map_list_incr.adb:17:71: info: overflow check proved
map_list_incr.adb:19:10: info: loop invariant preservation proved
map_list_incr.adb:19:10: info: loop invariant initialization proved
map_list_incr.adb:19:24: info: precondition proved
map_list_incr.adb:20:12: info: precondition proved
map_list_incr.adb:20:32: info: range check proved
map_list_incr.adb:20:37: info: precondition proved
map_list_incr.adb:20:68: info: range check proved
map_list_incr.adb:21:07: info: precondition proved
map_list_incr.adb:21:31: info: precondition proved
map_list_incr.adb:21:47: info: overflow check proved
map_list_incr.adb:22:07: info: precondition proved

Finally, consider a variant of the same mapping loop over a pointer-based list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Small_Enough (X : Component_T) return Boolean is
     (X /= Component_T'Last);
   function Bigger_Than_First (X : Component_T) return Boolean is
     (X /= Component_T'First);

   procedure Map_List_Incr (L : access List_Cell) with
     Pre  => For_All_List (L, Small_Enough'Access),
     Post => For_All_List (L, Bigger_Than_First'Access);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   procedure Map_List_Incr (L : access List_Cell) is
      B : access List_Cell := L;
   begin
      while B /= null loop
         pragma Loop_Invariant (For_All_List (B, Small_Enough'Access));
         pragma Loop_Invariant
           (if For_All_List (At_End (B), Bigger_Than_First'Access)
            then For_All_List (At_End (L), Bigger_Than_First'Access));
         B.Value := B.Value + 1;
         B := B.Next;
      end loop;
   end Map_List_Incr;
end P;

Like in the other variants, the precondition of Map_List_Incr states that all elements of the input list L are less than Component_T'Last before the call. It uses the For_All_List function from Loop_Types to quantify over all the elements of the list. The postcondition is weaker than in other variants of the loop. Indeed, referring to the value of a pointer-based datastructure before the call is not allowed in the SPARK language. Therefore we changed the postcondition to state instead that all elements of the list are bigger than Component_T'First after the call.

The loop iterates over the list L using a local borrower B which is a local variable which borrows the ownership of a part of a datastructure for the duration of its scope, see Borrowing for more details. The loop invariant is made of two parts. The first one states that the initial property still holds on the elements of L accessible through B. The second uses the At_End function to express properties about the values of L and B at the end of the borrow. It states that the elements of L at the end of the borrow will have the Bigger_Than_First property if the elements of B at the end of the borrow have this property. This is provable because we know when verifying the invariant that the already traversed elements currently have the Bigger_Than_First property and that they can no longer be changed during the scope of B. With this loop invariant, GNATprove is able to prove the postcondition of Map_List_Incr:

p.adb:10:33: info: loop invariant preservation proved
p.adb:10:33: info: loop invariant initialization proved
p.adb:10:62: info: null exclusion check proved
p.adb:12:13: info: loop invariant initialization proved
p.adb:12:13: info: loop invariant preservation proved
p.adb:12:59: info: null exclusion check proved
p.adb:13:61: info: null exclusion check proved
p.adb:14:11: info: pointer dereference check proved
p.adb:14:22: info: pointer dereference check proved
p.adb:14:29: info: overflow check proved
p.adb:15:16: info: pointer dereference check proved
p.ads:12:43: info: null exclusion check proved
p.ads:13:14: info: postcondition proved
p.ads:13:48: info: null exclusion check proved

If we want to retain the most precise postcondition relating the elements of the structure before and after the loop, we need to introduce a way to store the values of the list before the call in a separate data structure. In the following example, it is done by declaring a Copy function which returns a copy of its input list. In its postcondition, we use the two-valued For_All_List function to state that the elements of the new structure are equal to the elements of the input structure. An alternative could be to store the elements in a structure not subjected to ownership like an array.

Note

The function Copy is marked as Import as it is not meant to be executed. It could be implemented in SPARK by returning a deep copy of the argument list, reallocating all cells of the list in the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Small_Enough (X : Component_T) return Boolean is
     (X /= Component_T'Last);

   function Equal (X, Y : Component_T) return Boolean is (X = Y);

   function Is_Incr (X, Y : Component_T) return Boolean is
     (X < Y and then Y = X + 1);

   function Copy (L : access List_Cell) return List_Acc with
     Ghost,
     Import,
     Post => For_All_List (L, Copy'Result, Equal'Access);

   procedure Map_List_Incr (L : access List_Cell) with
     Pre  => For_All_List (L, Small_Enough'Access),
     Post => For_All_List (Copy (L)'Old, L, Is_Incr'Access);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   procedure Map_List_Incr (L : access List_Cell) is
      L_Old : constant List_Acc := Copy (L) with Ghost;
      pragma Annotate (GNATprove, Intentional, "memory leak might occur",
                       "The code will be compiled with assertions disabled");
      B     : access List_Cell := L;
      B_Old : access constant List_Cell := L_Old with Ghost;
   begin
      while B /= null loop
         pragma Loop_Invariant (For_All_List (B, Small_Enough'Access));
         pragma Loop_Invariant (For_All_List (B, B_Old, Equal'Access));
         pragma Loop_Invariant
           (if For_All_List (B_Old, At_End (B), Is_Incr'Access)
            then For_All_List (L_Old, At_End (L), Is_Incr'Access));
         B.Value := B.Value + 1;
         B := B.Next;
         B_Old := B_Old.Next;
      end loop;
      pragma Assert
        (For_All_List (L_Old, At_End (L), Is_Incr'Access));
   end Map_List_Incr;
end P;

The postcondition of Map_List_Incr is similar to the postcondition of Copy. It uses the two-valued For_All_List function to relate the elements of L before and after the call. Like in the previous variant, the loop traverses L using a local borrower B. To be able to speak about the initial value of L in the invariant, we introduce a ghost constant L_Old storing a copy of this value. As we need to traverse both lists at the same time, we declare a ghost variable B_Old as a local observer of L_Old.

The loop invariant is made of three parts now. The first one is similar to the one in the previous example. The third loop invariant is a direct adaptation of the second loop invariant of the previous example. It states that if, at the end of the borrow, the values accessible through B are related to their equivalent element in B_Old through Is_Incr, then so are all the elements of L. The loop invariant in the middle states that the elements reachable through B have not been modified by the loop. GNATprove can verify these loop invariants as well as the postcondition of Map_List_Incr:

p.adb:14:33: info: loop invariant preservation proved
p.adb:14:33: info: loop invariant initialization proved
p.adb:14:62: info: null exclusion check proved
p.adb:15:33: info: loop invariant initialization proved
p.adb:15:33: info: loop invariant preservation proved
p.adb:15:62: info: null exclusion check proved
p.adb:17:13: info: loop invariant preservation proved
p.adb:17:13: info: loop invariant initialization proved
p.adb:17:56: info: null exclusion check proved
p.adb:18:58: info: null exclusion check proved
p.adb:19:11: info: pointer dereference check proved
p.adb:19:22: info: pointer dereference check proved
p.adb:19:29: info: overflow check proved
p.adb:20:16: info: pointer dereference check proved
p.adb:21:24: info: pointer dereference check proved
p.adb:24:10: info: assertion proved
p.adb:24:50: info: null exclusion check proved
p.ads:12:28: info: overflow check proved
p.ads:20:43: info: null exclusion check proved
p.ads:21:14: info: postcondition proved

p.ads:21:28: medium: memory leak might occur
   21 |     Post => For_All_List (Copy (L)'Old, L, Is_Incr'Access);
      |                           ^~~~~~~
  possible explanation: call to allocating function inside an assertion leaks memory
p.ads:21:52: info: null exclusion check proved

Note

The second loop invariant does not subsume the first. Indeed, proving that, if all elements of L_Old are small enough, so are all elements of an unknown observer B_Old of L_Old, is beyond the capacity of GNATprove.

7.9.2.4. Validation Loops

This kind of loops iterates over a collection to validate that every element of the collection has a valid value. The most common pattern is to exit or return from the loop if an invalid value if encountered:

Loop Pattern

Sequence Validation with Early Exit

Proof Objective

Determine (flag) if there are any invalid elements in a given collection.

Loop Behavior

Loops over the collection and exits/returns if an invalid element is encountered.

Loop Invariant

Every element encountered so far is valid.

Consider a procedure Validate_Arr_Zero that checks that all elements of an array A have value zero:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
with Loop_Types; use Loop_Types;

procedure Validate_Arr_Zero (A : Arr_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for all J in A'Range => A(J) = 0)
is
begin
   for J in A'Range loop
      if A(J) /= 0 then
         Success := False;
         return;
      end if;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = 0);
   end loop;

   Success := True;
end Validate_Arr_Zero;

The loop invariant expresses that all elements up to the current loop index J have value zero. With this loop invariant, GNATprove is able to prove the postcondition of Validate_Arr_Zero, namely that output parameter Success is True if-and-only-if all elements of the array have value zero:

validate_arr_zero.adb:3:41: info: initialization of "Success" proved
validate_arr_zero.adb:5:11: info: postcondition proved
validate_arr_zero.adb:13:30: info: loop invariant initialization proved
validate_arr_zero.adb:13:30: info: loop invariant preservation proved
validate_arr_zero.adb:13:61: info: index check proved

Consider now a variant of the same validation loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;

procedure Validate_Vec_Zero (V : Vec_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for all J in First_Index (V) .. Last_Index (V) => Element (V, J) = 0)
is
begin
   for J in First_Index (V) .. Last_Index (V) loop
      if Element (V, J) /= 0 then
         Success := False;
         return;
      end if;
      pragma Loop_Invariant (for all K in First_Index (V) .. J => Element (V, K) = 0);
   end loop;

   Success := True;
end Validate_Vec_Zero;

Like before, the loop invariant expresses that all elements up to the current loop index J have the value zero. Since variable V is not modified in the loop, no additional loop invariant is needed here for GNATprove to know that its length stays the same (this is different from the case of Init_Vec_Zero seen previously). With this loop invariant, GNATprove is able to prove the postcondition of Validate_Vec_Zero, namely that output parameter Success is True if-and-only-if all elements of the vector have value zero:

validate_vec_zero.adb:3:41: info: initialization of "Success" proved
validate_vec_zero.adb:5:11: info: postcondition proved
validate_vec_zero.adb:5:72: info: precondition proved
validate_vec_zero.adb:5:84: info: range check proved
validate_vec_zero.adb:9:10: info: precondition proved
validate_vec_zero.adb:9:22: info: range check proved
validate_vec_zero.adb:13:30: info: loop invariant initialization proved
validate_vec_zero.adb:13:30: info: loop invariant preservation proved
validate_vec_zero.adb:13:67: info: precondition proved
validate_vec_zero.adb:13:79: info: range check proved

Similarly, consider a variant of the same validation loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Validate_List_Zero (L : List_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for all E of L => E = 0)
is
   Cu : Cursor := First (L);
begin
   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
                               Element (Model (L), I) = 0);
      if Element (L, Cu) /= 0 then
         Success := False;
         return;
      end if;
      Next (L, Cu);
   end loop;

   Success := True;
end Validate_List_Zero;

Like in the case of Init_List_Zero seen previously, we need to define a cursor here to iterate over the list. The loop invariant expresses that all elements up to the current cursor Cu have the value zero. With this loop invariant, GNATprove is able to prove the postcondition of Validate_List_Zero, namely that output parameter Success is True if-and-only-if all elements of the list have value zero:

validate_list_zero.adb:4:43: info: initialization of "Success" proved
validate_list_zero.adb:6:11: info: postcondition proved
validate_list_zero.adb:6:22: info: precondition proved
validate_list_zero.adb:11:30: info: loop invariant initialization proved
validate_list_zero.adb:11:30: info: loop invariant preservation proved
validate_list_zero.adb:11:49: info: precondition proved
validate_list_zero.adb:12:32: info: precondition proved
validate_list_zero.adb:13:10: info: precondition proved
validate_list_zero.adb:17:07: info: precondition proved

The case of sets and maps is similar to the case of lists.

Consider now a variant of the same validation loop over a pointer-based list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Is_Zero (X : Component_T) return Boolean is
     (X = 0);

   procedure Validate_List_Zero
     (L       : access constant List_Cell;
      Success : out Boolean)
   with
     Post => Success = For_All_List (L, Is_Zero'Access);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   procedure Validate_List_Zero
     (L       : access constant List_Cell;
      Success : out Boolean)
   is
      C : access constant List_Cell := L;
   begin
      while C /= null loop
         pragma Loop_Invariant
           (For_All_List (L, Is_Zero'Access) = For_All_List (C, Is_Zero'Access));
         if C.Value /= 0 then
            Success := False;
            return;
         end if;
         C := C.Next;
      end loop;

      Success := True;
   end Validate_List_Zero;
end P;

The loop is implemented using a local observer (see Observing) which borrows a read-only permission over a part of a datastructure until the end of the scope of the observer. In the loop invariant, we cannot, like in the other versions of the algorithm, speak about the value of the elements which have already been traversed to say that they are all 0. Instead, we state that the list L only contains 0 iff C only contains 0. This is true since the loop exits as soon as a non-zero value is encountered. With this invariant, the postcondition can be proved by GNATprove:

p.adb:14:13: info: loop invariant initialization proved
p.adb:14:13: info: loop invariant preservation proved
p.adb:14:37: info: null exclusion check proved
p.adb:14:72: info: null exclusion check proved
p.adb:15:14: info: pointer dereference check proved
p.adb:19:16: info: pointer dereference check proved
p.ads:11:07: info: initialization of "Success" proved
p.ads:13:14: info: postcondition proved
p.ads:13:48: info: null exclusion check proved

A variant of the previous validation pattern is to continue validating elements even after an invalid value has been encountered, which allows for example logging all invalid values:

Loop Pattern

Sequence Validation that Validates Entire Collection

Proof Objective

Determine (flag) if there are any invalid elements in a given collection.

Loop Behavior

Loops over the collection. If an invalid element is encountered, flag this, but keep validating (typically logging every invalidity) for the entire collection.

Loop Invariant

If invalidity is not flagged, every element encountered so far is valid.

Consider a variant of Validate_Arr_Zero that keeps validating elements of the array after a non-zero element has been encountered:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
with Loop_Types; use Loop_Types;

procedure Validate_Full_Arr_Zero (A : Arr_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for all J in A'Range => A(J) = 0)
is
begin
   Success := True;

   for J in A'Range loop
      if A(J) /= 0 then
         Success := False;
         --  perform some logging here instead of returning
      end if;
      pragma Loop_Invariant (Success = (for all K in A'First .. J => A(K) = 0));
   end loop;
end Validate_Full_Arr_Zero;

The loop invariant has been modified to state that all elements up to the current loop index J have value zero if-and-only-if the output parameter Success is True. This in turn requires to move the assignment of Success before the loop. With this loop invariant, GNATprove is able to prove the postcondition of Validate_Full_Arr_Zero, which is the same as the postcondition of Validate_Arr_Zero, namely that output parameter Success is True if-and-only-if all elements of the array have value zero:

validate_full_arr_zero.adb:3:46: info: initialization of "Success" proved
validate_full_arr_zero.adb:5:11: info: postcondition proved
validate_full_arr_zero.adb:15:30: info: loop invariant initialization proved
validate_full_arr_zero.adb:15:30: info: loop invariant preservation proved
validate_full_arr_zero.adb:15:72: info: index check proved

Similarly, variants of Validate_Vec_Zero and Validate_List_Zero that keep validating elements of the collection after a non-zero element has been encountered would be proved by GNATprove.

7.9.2.5. Counting Loops

This kind of loops iterates over a collection to count the number of elements of the collection that satisfy a given criterion:

Loop Pattern

Count Elements Satisfying Criterion

Proof Objective

Count elements that satisfy a given criterion.

Loop Behavior

Loops over the collection. Increments a counter each time the value of an element satisfies the criterion.

Loop Invariant

The value of the counter is either 0 when no element encountered so far satisfies the criterion, or a positive number bounded by the current iteration of the loop otherwise.

Consider a procedure Count_Arr_Zero that counts elements with value zero in array A:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
with Loop_Types; use Loop_Types;

procedure Count_Arr_Zero (A : Arr_T; Counter : out Natural) with
  SPARK_Mode,
  Post => (Counter in 0 .. A'Length) and then
          ((Counter = 0) = (for all K in A'Range => A(K) /= 0))
is
begin
   Counter := 0;

   for J in A'Range loop
      if A(J) = 0 then
         Counter := Counter + 1;
      end if;
      pragma Loop_Invariant (Counter in 0 .. J);
      pragma Loop_Invariant ((Counter = 0) = (for all K in A'First .. J => A(K) /= 0));
   end loop;
end Count_Arr_Zero;

The loop invariant expresses that the value of Counter is a natural number bounded by the current loop index J, and that Counter is equal to zero exactly when all elements up to the current loop index have a non-zero value. With this loop invariant, GNATprove is able to prove the postcondition of Count_Arr_Zero, namely that output parameter Counter is a natural number bounded by the length of the array A, and that Counter is equal to zero exactly when all elements in A have a non-zero value:

count_arr_zero.adb:3:38: info: initialization of "Counter" proved
count_arr_zero.adb:5:11: info: postcondition proved
count_arr_zero.adb:13:29: info: overflow check proved
count_arr_zero.adb:15:30: info: loop invariant initialization proved
count_arr_zero.adb:15:30: info: loop invariant preservation proved
count_arr_zero.adb:16:30: info: loop invariant preservation proved
count_arr_zero.adb:16:30: info: loop invariant initialization proved
count_arr_zero.adb:16:78: info: index check proved

Consider now a variant of the same counting loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;

procedure Count_Vec_Zero (V : Vec_T; Counter : out Natural) with
  SPARK_Mode,
  Post => (Counter in 0 .. Natural (Length (V))) and then
          ((Counter = 0) = (for all K in First_Index (V) .. Last_Index (V) => Element (V, K) /= 0))
is
begin
   Counter := 0;

   for J in First_Index (V) .. Last_Index (V) loop
      if Element (V, J) = 0 then
         Counter := Counter + 1;
      end if;
      pragma Loop_Invariant (Counter in 0 .. J);
      pragma Loop_Invariant ((Counter = 0) = (for all K in First_Index (V) .. J => Element (V, K) /= 0));
   end loop;
end Count_Vec_Zero;

Like before, the loop invariant expresses that the value of Counter is a natural number bounded by the current loop index J, and that Counter is equal to zero exactly when all elements up to the current loop index have a non-zero value. With this loop invariant, GNATprove is able to prove the postcondition of Count_Vec_Zero, namely that output parameter Counter is a natural number bounded by the length of the vector V, and that Counter is equal to zero exactly when all elements in V have a non-zero value:

count_vec_zero.adb:3:38: info: initialization of "Counter" proved
count_vec_zero.adb:5:11: info: postcondition proved
count_vec_zero.adb:6:79: info: precondition proved
count_vec_zero.adb:6:91: info: range check proved
count_vec_zero.adb:12:10: info: precondition proved
count_vec_zero.adb:12:22: info: range check proved
count_vec_zero.adb:13:29: info: overflow check proved
count_vec_zero.adb:15:30: info: loop invariant initialization proved
count_vec_zero.adb:15:30: info: loop invariant preservation proved
count_vec_zero.adb:16:30: info: loop invariant initialization proved
count_vec_zero.adb:16:30: info: loop invariant preservation proved
count_vec_zero.adb:16:84: info: precondition proved
count_vec_zero.adb:16:96: info: range check proved

7.9.2.6. Search Loops

This kind of loops iterates over a collection to search an element of the collection that meets a given search criterion:

Loop Pattern

Search with Early Exit

Proof Objective

Find an element or position that meets a search criterion.

Loop Behavior

Loops over the collection. Exits when an element that meets the search criterion is found.

Loop Invariant

Every element encountered so far does not meet the search criterion.

Consider a procedure Search_Arr_Zero that searches an element with value zero in array A:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
with Loop_Types; use Loop_Types;

procedure Search_Arr_Zero (A : Arr_T; Pos : out Opt_Index_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for some J in A'Range => A(J) = 0) and then
          (if Success then A (Pos) = 0)
is
begin
   for J in A'Range loop
      if A(J) = 0 then
         Success := True;
         Pos := J;
         return;
      end if;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) /= 0);
   end loop;

   Success := False;
   Pos := 0;
end Search_Arr_Zero;

The loop invariant expresses that all elements up to the current loop index J have a non-zero value. With this loop invariant, GNATprove is able to prove the postcondition of Search_Arr_Zero, namely that output parameter Success is True if-and-only-if there is an element of the array that has value zero, and that Pos is the index of such an element:

search_arr_zero.adb:3:39: info: initialization of "Pos" proved
search_arr_zero.adb:3:62: info: initialization of "Success" proved
search_arr_zero.adb:5:11: info: postcondition proved
search_arr_zero.adb:6:31: info: index check proved
search_arr_zero.adb:15:30: info: loop invariant initialization proved
search_arr_zero.adb:15:30: info: loop invariant preservation proved
search_arr_zero.adb:15:61: info: index check proved

Consider now a variant of the same search loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;

procedure Search_Vec_Zero (V : Vec_T; Pos : out Opt_Index_T; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for some J in First_Index (V) .. Last_Index (V) => Element (V, J) = 0) and then
          (if Success then Element (V, Pos) = 0)
is
begin
   for J in First_Index (V) .. Last_Index (V) loop
      if Element (V, J) = 0 then
         Success := True;
         Pos := J;
         return;
      end if;
      pragma Loop_Invariant (for all K in First_Index (V) .. J => Element (V, K) /= 0);
   end loop;

   Success := False;
   Pos := 0;
end Search_Vec_Zero;

Like before, the loop invariant expresses that all elements up to the current loop index J have a non-zero value. With this loop invariant, GNATprove is able to prove the postcondition of Search_Vec_Zero, namely that output parameter Success is True if-and-only-if there is an element of the vector that has value zero, and that Pos is the index of such an element:

search_vec_zero.adb:3:39: info: initialization of "Pos" proved
search_vec_zero.adb:3:62: info: initialization of "Success" proved
search_vec_zero.adb:5:11: info: postcondition proved
search_vec_zero.adb:5:73: info: precondition proved
search_vec_zero.adb:5:85: info: range check proved
search_vec_zero.adb:6:28: info: precondition proved
search_vec_zero.adb:6:40: info: range check proved
search_vec_zero.adb:10:10: info: precondition proved
search_vec_zero.adb:10:22: info: range check proved
search_vec_zero.adb:12:17: info: range check proved
search_vec_zero.adb:15:30: info: loop invariant initialization proved
search_vec_zero.adb:15:30: info: loop invariant preservation proved
search_vec_zero.adb:15:67: info: precondition proved
search_vec_zero.adb:15:79: info: range check proved

Similarly, consider a variant of the same search loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Search_List_Zero (L : List_T; Pos : out Cursor; Success : out Boolean) with
  SPARK_Mode,
  Post => Success = (for some E of L => E = 0) and then
          (if Success then Element (L, Pos) = 0)
is
   Cu : Cursor := First (L);
begin
   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
                               Element (Model (L), I) /= 0);
      if Element (L, Cu) = 0 then
         Success := True;
         Pos := Cu;
         return;
      end if;
      Next (L, Cu);
   end loop;

   Success := False;
   Pos := No_Element;
end Search_List_Zero;

The loop invariant expresses that all elements up to the current cursor Cu have a non-zero value. With this loop invariant, GNATprove is able to prove the postcondition of Search_List_Zero, namely that output parameter Success is True if-and-only-if there is an element of the list that has value zero, and that Pos is the cursor of such an element:

search_list_zero.adb:4:41: info: initialization of "Pos" proved
search_list_zero.adb:4:59: info: initialization of "Success" proved
search_list_zero.adb:6:11: info: postcondition proved
search_list_zero.adb:6:22: info: precondition proved
search_list_zero.adb:7:28: info: precondition proved
search_list_zero.adb:12:30: info: loop invariant initialization proved
search_list_zero.adb:12:30: info: loop invariant preservation proved
search_list_zero.adb:12:49: info: precondition proved
search_list_zero.adb:13:32: info: precondition proved
search_list_zero.adb:14:10: info: precondition proved
search_list_zero.adb:19:07: info: precondition proved

The case of sets and maps is similar to the case of lists.

Consider a variant of the same search loop over a pointer-based list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Is_Non_Zero (X : Component_T) return Boolean is
     (X /= 0);

   function Search_List_Zero (L : access List_Cell) return access List_Cell with
     Post =>
       ((Search_List_Zero'Result = null) = For_All_List (L, Is_Non_Zero'Access)
        and then
            (if Search_List_Zero'Result /= null then Search_List_Zero'Result.Value = 0));
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   function Search_List_Zero (L : access List_Cell) return access List_Cell is
      B : access List_Cell := L;
   begin
      while B /= null and then B.Value /= 0 loop
         pragma Loop_Invariant
           (For_All_List (L, Is_Non_Zero'Access) =
                For_All_List (B, Is_Non_Zero'Access));
         B := B.Next;
      end loop;

      return B;
   end Search_List_Zero;
end P;

As our pointer-based lists do not support cursors, the result of the search is a pointer inside the list which can be used to access or even update the corresponding element. Storing such an object inside an OUT parameter would break the ownership model of SPARK by creating an alias. Instead, we use a traversal function (see Traversal Functions) to return this pointer as a local borrower of the input list. Since we now have a function, we can no longer have an explicit Success flag to encode whether or not the value was found. Instead, we simply return null in case of failure.

The loop iterates over the input list L using a local borrower B. The iteration stops when either B is null or B.Value is zero. In the loop invariant, we cannot speak directly about the elements of L that have been traversed to say that they are not 0. Instead, we write in the invariant that L contains only non-zero values iff B contains only non-zero values. Thanks to this loop invariant, GNATprove is able to verify the postcondition of Search_List_Zero:

p.adb:9:33: info: pointer dereference check proved
p.adb:11:13: info: loop invariant initialization proved
p.adb:11:13: info: loop invariant preservation proved
p.adb:11:41: info: null exclusion check proved
p.adb:12:45: info: null exclusion check proved
p.adb:13:16: info: pointer dereference check proved
p.adb:16:14: info: dynamic accessibility check proved
p.ads:11:08: info: postcondition proved
p.ads:11:72: info: null exclusion check proved
p.ads:13:77: info: pointer dereference check proved

For more complex examples of search loops, see the SPARK Tutorial as well as the section on How to Write Loop Invariants.

7.9.2.7. Maximize Loops

This kind of loops iterates over a collection to search an element of the collection that maximizes a given optimality criterion:

Loop Pattern

Search Optimum to Criterion

Proof Objective

Find an element or position that maximizes an optimality criterion.

Loop Behavior

Loops over the collection. Records maximum value of criterion so far and possibly index that maximizes this criterion.

Loop Invariant

Exactly one element encountered so far corresponds to the recorded maximum over other elements encountered so far.

Consider a procedure Search_Arr_Max that searches an element maximum value in array A:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
with Loop_Types; use Loop_Types;

procedure Search_Arr_Max (A : Arr_T; Pos : out Index_T; Max : out Component_T) with
  SPARK_Mode,
  Post => (for all J in A'Range => A(J) <= Max) and then
          (for some J in A'Range => A(J) = Max) and then
          A(Pos) = Max
is
begin
   Max := 0;
   Pos := A'First;

   for J in A'Range loop
      if A(J) > Max then
         Max := A(J);
         Pos := J;
      end if;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) <= Max);
      pragma Loop_Invariant (for some K in A'First .. J => A(K) = Max);
      pragma Loop_Invariant (A(Pos) = Max);
   end loop;
end Search_Arr_Max;

The loop invariant expresses that all elements up to the current loop index J have a value less than Max, and that Max is the value of one of these elements. The last loop invariant gives in fact this element, it is A(Pos), but this part of the loop invariant may not be present if the position Pos for the optimum is not recorded. With this loop invariant, GNATprove is able to prove the postcondition of Search_Arr_Max, namely that output parameter Max is the maximum of the elements in the array, and that Pos is the index of such an element:

search_arr_max.adb:3:38: info: initialization of "Pos" proved
search_arr_max.adb:3:57: info: initialization of "Max" proved
search_arr_max.adb:5:11: info: postcondition proved
search_arr_max.adb:18:30: info: loop invariant initialization proved
search_arr_max.adb:18:30: info: loop invariant preservation proved
search_arr_max.adb:18:61: info: index check proved
search_arr_max.adb:19:30: info: loop invariant preservation proved
search_arr_max.adb:19:30: info: loop invariant initialization proved
search_arr_max.adb:19:62: info: index check proved
search_arr_max.adb:20:30: info: loop invariant initialization proved
search_arr_max.adb:20:30: info: loop invariant preservation proved

Consider now a variant of the same search loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;

procedure Search_Vec_Max (V : Vec_T; Pos : out Index_T; Max : out Component_T) with
  SPARK_Mode,
  Pre  => not Is_Empty (V),
  Post => (for all J in First_Index (V) .. Last_Index (V) => Element (V, J) <= Max) and then
          (for some J in First_Index (V) .. Last_Index (V) => Element (V, J) = Max) and then
          Pos in First_Index (V) .. Last_Index (V) and then
          Element (V, Pos) = Max
is
begin
   Max := 0;
   Pos := First_Index (V);

   for J in First_Index (V) .. Last_Index (V) loop
      if Element (V, J) > Max then
         Max := Element (V, J);
         Pos := J;
      end if;
      pragma Loop_Invariant (for all K in First_Index (V) .. J => Element (V, K) <= Max);
      pragma Loop_Invariant (for some K in First_Index (V) .. J => Element (V, K) = Max);
      pragma Loop_Invariant (Pos in First_Index (V) .. J);
      pragma Loop_Invariant (Element (V, Pos) = Max);
   end loop;
end Search_Vec_Max;

Like before, the loop invariant expresses that all elements up to the current loop index J have a value less than Max, and that Max is the value of one of these elements, most precisely the value of Element (V, Pos) if the position Pos for the optimum is recorded. An additional loop invariant is needed here compared to the case of arrays to state that Pos remains within the bounds of the vector. With this loop invariant, GNATprove is able to prove the postcondition of Search_Vec_Max, namely that output parameter Max is the maximum of the elements in the vector, and that Pos is the index of such an element:

search_vec_max.adb:3:38: info: initialization of "Pos" proved
search_vec_max.adb:3:57: info: initialization of "Max" proved
search_vec_max.adb:6:11: info: postcondition proved
search_vec_max.adb:6:62: info: precondition proved
search_vec_max.adb:6:74: info: range check proved
search_vec_max.adb:7:63: info: precondition proved
search_vec_max.adb:7:75: info: range check proved
search_vec_max.adb:9:11: info: precondition proved
search_vec_max.adb:16:10: info: precondition proved
search_vec_max.adb:16:22: info: range check proved
search_vec_max.adb:17:17: info: precondition proved
search_vec_max.adb:17:29: info: range check proved
search_vec_max.adb:18:17: info: range check proved
search_vec_max.adb:20:30: info: loop invariant preservation proved
search_vec_max.adb:20:30: info: loop invariant initialization proved
search_vec_max.adb:20:67: info: precondition proved
search_vec_max.adb:20:79: info: range check proved
search_vec_max.adb:21:30: info: loop invariant initialization proved
search_vec_max.adb:21:30: info: loop invariant preservation proved
search_vec_max.adb:21:68: info: precondition proved
search_vec_max.adb:21:80: info: range check proved
search_vec_max.adb:22:30: info: loop invariant preservation proved
search_vec_max.adb:22:30: info: loop invariant initialization proved
search_vec_max.adb:23:30: info: precondition proved
search_vec_max.adb:23:30: info: loop invariant initialization proved
search_vec_max.adb:23:30: info: loop invariant preservation proved

Similarly, consider a variant of the same search loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Search_List_Max (L : List_T; Pos : out Cursor; Max : out Component_T) with
  SPARK_Mode,
  Pre  => not Is_Empty (L),
  Post => (for all E of L => E <= Max) and then
          (for some E of L => E = Max) and then
          Has_Element (L, Pos) and then
          Element (L, Pos) = Max
is
   Cu : Cursor := First (L);
begin
   Max := 0;
   Pos := Cu;

   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
                               Element (Model (L), I) <= Max);
      pragma Loop_Invariant (Has_Element (L, Pos));
      pragma Loop_Invariant (Max = 0 or else Element (L, Pos) = Max);

      if Element (L, Cu) > Max then
         Max := Element (L, Cu);
         Pos := Cu;
      end if;
      Next (L, Cu);
   end loop;
end Search_List_Max;

The loop invariant expresses that all elements up to the current cursor Cu have a value less than Max, and that Max is the value of one of these elements, most precisely the value of Element (L, Pos) if the cursor Pos for the optimum is recorded. Like for vectors, an additional loop invariant is needed here compared to the case of arrays to state that cursor Pos is a valid cursor of the list. A minor difference is that a loop invariant now starts with Max = 0 or else .. because the loop invariant is stated at the start of the loop (for convenience with the use of First_To_Previous) which requires this modification. With this loop invariant, GNATprove is able to prove the postcondition of Search_List_Max, namely that output parameter Max is the maximum of the elements in the list, and that Pos is the cursor of such an element:

search_list_max.adb:4:40: info: initialization of "Pos" proved
search_list_max.adb:4:58: info: initialization of "Max" proved
search_list_max.adb:7:11: info: postcondition proved
search_list_max.adb:7:12: info: precondition proved
search_list_max.adb:8:12: info: precondition proved
search_list_max.adb:10:11: info: precondition proved
search_list_max.adb:18:30: info: loop invariant preservation proved
search_list_max.adb:18:30: info: loop invariant initialization proved
search_list_max.adb:18:49: info: precondition proved
search_list_max.adb:19:32: info: precondition proved
search_list_max.adb:20:30: info: loop invariant initialization proved
search_list_max.adb:20:30: info: loop invariant preservation proved
search_list_max.adb:21:30: info: loop invariant initialization proved
search_list_max.adb:21:30: info: loop invariant preservation proved
search_list_max.adb:21:46: info: precondition proved
search_list_max.adb:23:10: info: precondition proved
search_list_max.adb:24:17: info: precondition proved
search_list_max.adb:27:07: info: precondition proved

The case of sets and maps is similar to the case of lists.

Consider a variant of the same search loop over a pointer-based list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function All_Smaller_Than_Max
     (L : access constant List_Cell; Max : Component_T) return Boolean
   is (L = null or else
         (L.Value <= Max and then All_Smaller_Than_Max (L.Next, Max)))
   with Annotate => (GNATprove, Terminating);
   pragma Annotate (GNATprove, False_Positive, "is recursive",
                    "The recursive call occurs on a strictly smaller list");

   function Search_List_Max
     (L : not null access List_Cell) return not null access List_Cell
   with
     Post => All_Smaller_Than_Max (L, Search_List_Max'Result.Value);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   function Search_List_Max
     (L : not null access List_Cell) return not null access List_Cell
   is
      B : access List_Cell := L;
   begin
      loop
         pragma Loop_Invariant (B /= null);
         pragma Loop_Invariant
           (for all M in B.Value .. Component_T'Last =>
              (if All_Smaller_Than_Max (B, M)
               then All_Smaller_Than_Max (L, M)));
         declare
            Prec : access List_Cell := B;
            Max  : constant Component_T := B.Value;
         begin
            while B /= null and then B.Value <= Max loop
               pragma Loop_Invariant
                 (for all M in Max .. Component_T'Last =>
                    (if All_Smaller_Than_Max (B, M)
                     then All_Smaller_Than_Max (L, M)));
               B := B.Next;
            end loop;
            if B = null then
               return Prec;
            end if;
         end;
      end loop;
   end Search_List_Max;
end P;

As our pointer-based lists do not support cursors, the result of the search is a pointer inside the list which can be used to access or even update the corresponding element. Storing such an object inside an OUT parameter would break the ownership model of SPARK by creating an alias. Instead, we use a traversal function (see Traversal Functions) to return this pointer as a local borrower of the input list. Since we now have a function, we can no longer explicitely return the value of the maximum. It is not a problem, as it can be accessed easily as the Value component of the returned pointer. In the postcondition of Search_List_Max, we cannot use For_All_List to express that the returned pointer designates the maximum value in the list. Indeed, the property depends on the value of this maximum. Instead, we create a specific recursive function taking the maximum as an additional parameter.

The iteration over the input list L uses a local borrower B. It is expressed as two nested loops. The inner loop declares a local borrower Prec to register the current value of the maximum. Then it iterates through the loop using B until a value bigger than the current maximum is found. The outer loop repeats this step as many times as necessary. This split into two loops is necessary as the SPARK language prevents borrowers from jumping into a different part of the data structure. As B is not syntactically a path rooted at Prec, Prec cannot be assigned the current value of B when a new maximal value is found. We therefore need to create a new variable to hold the current maximum each time it changes.

In the loop invariant of the outer loop, we cannot speak directly about the elements of L that have been traversed to say that they are smaller than the current maximum. Instead, we write in the invariant that the all values of L are smaller than any given value bigger than the current maximum iff the values of B are. A similar invariant is necessary on the inner loop. Thanks to these loop invariants, GNATprove is able to verify the postcondition of Search_List_Max:

p.adb:12:33: info: loop invariant initialization proved
p.adb:12:33: info: loop invariant preservation proved
p.adb:14:13: info: loop invariant preservation proved
p.adb:14:13: info: loop invariant initialization proved
p.adb:14:27: info: pointer dereference check proved
p.adb:15:44: info: range check proved
p.adb:16:46: info: range check proved
p.adb:19:45: info: pointer dereference check proved
p.adb:21:39: info: pointer dereference check proved
p.adb:23:19: info: loop invariant initialization proved
p.adb:23:19: info: loop invariant preservation proved
p.adb:24:50: info: range check proved
p.adb:25:52: info: range check proved
p.adb:26:22: info: pointer dereference check proved
p.adb:29:23: info: dynamic accessibility check proved
p.adb:29:23: info: null exclusion check proved
p.ads:9:12: info: pointer dereference check proved
p.ads:9:58: info: pointer dereference check proved
p.ads:17:14: info: postcondition proved
p.ads:17:61: info: pointer dereference check proved

For more complex examples of search loops, see the SPARK Tutorial as well as the section on How to Write Loop Invariants.

7.9.2.8. Update Loops

This kind of loops iterates over a collection to update individual elements based either on their value or on their position. The first pattern we consider is the one that updates elements based on their value:

Loop Pattern

Modification of Elements Based on Value

Proof Objective

Elements of the collection are updated based on their value.

Loop Behavior

Loops over a collection and assigns the elements whose value satisfies a given modification criterion.

Loop Invariant

Every element encountered so far has been assigned according to its value.

Consider a procedure Update_Arr_Zero that sets to zero all elements in array A that have a value smaller than a given Threshold:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
with Loop_Types; use Loop_Types;

procedure Update_Arr_Zero (A : in out Arr_T; Threshold : Component_T) with
  SPARK_Mode,
  Post => (for all J in A'Range => A(J) = (if A'Old(J) <= Threshold then 0 else A'Old(J)))
is
begin
   for J in A'Range loop
      if A(J) <= Threshold then
         A(J) := 0;
      end if;
      pragma Loop_Invariant (for all K in A'First .. J => A(K) = (if A'Loop_Entry(K) <= Threshold then 0 else A'Loop_Entry(K)));
      --  The following loop invariant is generated automatically by GNATprove:
      --  pragma Loop_Invariant (for all K in J + 1 .. A'Last => A(K) = A'Loop_Entry(K));
   end loop;
end Update_Arr_Zero;

The loop invariant expresses that all elements up to the current loop index J have been zeroed out if initially smaller than Threshold (using Attribute Loop_Entry). With this loop invariant, GNATprove is able to prove the postcondition of Update_Arr_Zero, namely that all elements initially smaller than Threshold have been zeroed out, and that other elements have not been modified:

update_arr_zero.adb:5:11: info: postcondition proved
update_arr_zero.adb:12:30: info: loop invariant initialization proved
update_arr_zero.adb:12:30: info: loop invariant preservation proved
update_arr_zero.adb:12:61: info: index check proved
update_arr_zero.adb:12:83: info: index check proved
update_arr_zero.adb:12:124: info: index check proved

Note that the commented loop invariant expressing that other elements have not been modified is not needed, as it is an example of Automatically Generated Loop Invariants.

Consider now a variant of the same update loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pragma Unevaluated_Use_Of_Old (Allow);
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;
use Loop_Types.Vectors.Formal_Model;

procedure Update_Vec_Zero (V : in out Vec_T; Threshold : Component_T) with
  SPARK_Mode,
  Post => Last_Index (V) = Last_Index (V)'Old
  and (for all I in 1 .. Last_Index (V) =>
            Element (V, I) =
             (if Element (Model (V)'Old, I) <= Threshold then 0
              else Element (Model (V)'Old, I)))
is
begin
   for J in First_Index (V) .. Last_Index (V) loop
      pragma Loop_Invariant (Last_Index (V) = Last_Index (V)'Loop_Entry);
      pragma Loop_Invariant
        (for all I in 1 .. J - 1 =>
             Element (V, I) =
              (if Element (Model (V)'Loop_Entry, I) <= Threshold then 0
               else Element (Model (V)'Loop_Entry, I)));
      pragma Loop_Invariant
        (for all I in J .. Last_Index (V) =>
             Element (V, I) = Element (Model (V)'Loop_Entry, I));
      if Element (V, J) <= Threshold then
         Replace_Element (V, J, 0);
      end if;
   end loop;
end Update_Vec_Zero;

Like for Map_Vec_Incr, we need to use the Model function over arrays to access elements of the vector before the loop as the vector type is limited. The loop invariant expresses that all elements up to the current loop index J have been zeroed out if initially smaller than Threshold, that elements that follow the current loop index have not been modified, and that the length of V is not modified (like in Init_Vec_Zero). With this loop invariant, GNATprove is able to prove the postcondition of Update_Vec_Zero:

update_vec_zero.adb:7:11: info: postcondition proved
update_vec_zero.adb:9:13: info: precondition proved
update_vec_zero.adb:9:25: info: range check proved
update_vec_zero.adb:10:18: info: precondition proved
update_vec_zero.adb:10:42: info: range check proved
update_vec_zero.adb:11:20: info: precondition proved
update_vec_zero.adb:11:44: info: range check proved
update_vec_zero.adb:15:30: info: loop invariant initialization proved
update_vec_zero.adb:15:30: info: loop invariant preservation proved
update_vec_zero.adb:17:10: info: loop invariant initialization proved
update_vec_zero.adb:17:10: info: loop invariant preservation proved
update_vec_zero.adb:17:30: info: overflow check proved
update_vec_zero.adb:18:14: info: precondition proved
update_vec_zero.adb:18:26: info: range check proved
update_vec_zero.adb:19:19: info: precondition proved
update_vec_zero.adb:19:50: info: range check proved
update_vec_zero.adb:20:21: info: precondition proved
update_vec_zero.adb:20:52: info: range check proved
update_vec_zero.adb:22:10: info: loop invariant preservation proved
update_vec_zero.adb:22:10: info: loop invariant initialization proved
update_vec_zero.adb:23:14: info: precondition proved
update_vec_zero.adb:23:26: info: range check proved
update_vec_zero.adb:23:31: info: precondition proved
update_vec_zero.adb:23:62: info: range check proved
update_vec_zero.adb:24:10: info: precondition proved
update_vec_zero.adb:24:22: info: range check proved
update_vec_zero.adb:25:10: info: precondition proved
update_vec_zero.adb:25:30: info: range check proved

Similarly, consider a variant of the same update loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Update_List_Zero (L : in out List_T; Threshold : Component_T) with
  SPARK_Mode,
  Post => Length (L) = Length (L)'Old
  and (for all I in 1 .. Length (L) =>
            Element (Model (L), I) =
             (if Element (Model (L'Old), I) <= Threshold then 0
              else Element (Model (L'Old), I)))
is
   Cu : Cursor := First (L);
begin
   while Has_Element (L, Cu) loop
      pragma Loop_Invariant (Length (L) = Length (L)'Loop_Entry);
      pragma Loop_Invariant
        (for all I in 1 .. P.Get (Positions (L), Cu) - 1 =>
            Element (Model (L), I) =
             (if Element (Model (L'Loop_Entry), I) <= Threshold then 0
              else Element (Model (L'Loop_Entry), I)));
      pragma Loop_Invariant
        (for all I in P.Get (Positions (L), Cu) .. Length (L) =>
            Element (Model (L), I) = Element (Model (L'Loop_Entry), I));
      if Element (L, Cu) <= Threshold then
         Replace_Element (L, Cu, 0);
      end if;
      Next (L, Cu);
   end loop;
end Update_List_Zero;

The loop invariant expresses that all elements up to the current cursor Cu have been zeroed out if initially smaller than Threshold (using function Model to access the element stored at a given position in the list and function Positions to query the position of the current cursor), and that elements that follow the current loop index have not been modified. Note that it is necessary to state here that the length of the list is not modified during the loop. It is because the length is used to bound the quantification over the elements of the list both in the invariant and in the postcondition.

With this loop invariant, GNATprove is able to prove the postcondition of Update_List_Zero, namely that all elements initially smaller than Threshold have been zeroed out, and that other elements have not been modified:

update_list_zero.adb:6:11: info: postcondition proved
update_list_zero.adb:8:13: info: precondition proved
update_list_zero.adb:9:18: info: precondition proved
update_list_zero.adb:10:20: info: precondition proved
update_list_zero.adb:15:30: info: loop invariant initialization proved
update_list_zero.adb:15:30: info: loop invariant preservation proved
update_list_zero.adb:17:10: info: loop invariant preservation proved
update_list_zero.adb:17:10: info: loop invariant initialization proved
update_list_zero.adb:17:29: info: precondition proved
update_list_zero.adb:18:13: info: precondition proved
update_list_zero.adb:19:18: info: precondition proved
update_list_zero.adb:20:20: info: precondition proved
update_list_zero.adb:22:10: info: loop invariant preservation proved
update_list_zero.adb:22:10: info: loop invariant initialization proved
update_list_zero.adb:22:24: info: precondition proved
update_list_zero.adb:23:13: info: precondition proved
update_list_zero.adb:23:33: info: range check proved
update_list_zero.adb:23:38: info: precondition proved
update_list_zero.adb:23:69: info: range check proved
update_list_zero.adb:24:10: info: precondition proved
update_list_zero.adb:25:10: info: precondition proved
update_list_zero.adb:27:07: info: precondition proved

The case of sets and maps is similar to the case of lists.

Consider now a variant of the same update loop over a pointer-based list. To express the postcondition relating the elements of the structure before and after the loop, we need to introduce a way to store the values of the list before the call in a separate data structure. Indeed, the Old attribute cannot be used on L directly has it would introduce an alias. In this example, it is done by declaring a Copy function which returns a copy of its input list. In its postcondition, we use the two-valued For_All_List function to state that the elements of the new structure are equal to the elements of its input structure. An alternative could be to store the elements in a structure not subjected to ownership like an array.

Note

The function Copy is marked as Import as it is not meant to be executed. It could be implemented in SPARK by returning a deep copy of the argument list, reallocating all cells of the list in the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
with Loop_Types; use Loop_Types;

package P with
  SPARK_Mode
is
   function Equal (X, Y : Component_T) return Boolean is (X = Y);

   function Copy (L : access List_Cell) return List_Acc with
     Ghost,
     Import,
     Post => For_All_List (L, Copy'Result, Equal'Access);

   function Updated_If_Less_Than_Threshold
     (L1, L2    : access constant List_Cell;
      Threshold : Component_T) return Boolean
   is
     ((L1 = null) = (L2 = null)
      and then
        (if L1 /= null then
             (if L1.Value <= Threshold then L2.Value = 0
              else L2.Value = L1.Value)
         and then Updated_If_Less_Than_Threshold (L1.Next, L2.Next, Threshold)))
   with Annotate => (GNATprove, Terminating);
   pragma Annotate (GNATprove, False_Positive, "is recursive",
                    "The recursive call occurs on a strictly smaller lists");

   procedure Update_List_Zero (L : access List_Cell; Threshold : Component_T) with
     Post => Updated_If_Less_Than_Threshold (Copy (L)'Old, L, Threshold);
end P;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
with Loop_Types; use Loop_Types;

package body P with
  SPARK_Mode
is
   procedure Update_List_Zero (L : access List_Cell; Threshold : Component_T) is
      L_Old : constant List_Acc := Copy (L) with Ghost;
      pragma Annotate (GNATprove, Intentional, "memory leak might occur",
                       "The code will be compiled with assertions disabled");
      B     : access List_Cell := L;
      B_Old : access constant List_Cell := L_Old with Ghost;
   begin
      while B /= null loop
         pragma Loop_Invariant (For_All_List (B, B_Old, Equal'Access));
         pragma Loop_Invariant
           (if Updated_If_Less_Than_Threshold (B_Old, At_End (B), Threshold)
            then Updated_If_Less_Than_Threshold (L_Old, At_End (L), Threshold));
         if B.Value <= Threshold then
            B.Value := 0;
         end if;
         B := B.Next;
         B_Old := B_Old.Next;
      end loop;
      pragma Assert
        (Updated_If_Less_Than_Threshold (L_Old, At_End (L), Threshold));
   end Update_List_Zero;
end P;

In the postcondition of Update_List_Zero, we cannot use For_All_List to express the relation between the values of the list before and after the call. Indeed, the relation depends on the value of the input Threshold. Instead, we create a specific recursive function taking the threshold as an additional parameter.

The loop traverses L using a local borrower B. To be able to speak about the initial value of L in the invariant, we introduce a ghost constant L_Old storing a copy of this value. As we need to traverse both lists at the same time, we declare a ghost variable B_Old as a local observer of L_Old.

The loop invariant is made of two parts. The first one states that the elements reachable through B have not been modified by the loop. In the second loop invariant, we want to use Updated_If_Less_Than_Threshold to relate the elements of L that were already traversed to the elements of L_Old. As we cannot speak specifically about the traversed elements of L, the invariant states that, if at the end of the borrow the values accessible through B are related to their equivalent element in B_Old through Updated_If_Less_Than_Threshold, then so are all the elements of L. GNATprove can verify these invariants as well as the postcondition of Update_List_Zero:

p.adb:14:33: info: loop invariant preservation proved
p.adb:14:33: info: loop invariant initialization proved
p.adb:14:62: info: null exclusion check proved
p.adb:16:13: info: loop invariant initialization proved
p.adb:16:13: info: loop invariant preservation proved
p.adb:18:14: info: pointer dereference check proved
p.adb:19:14: info: pointer dereference check proved
p.adb:21:16: info: pointer dereference check proved
p.adb:22:24: info: pointer dereference check proved
p.adb:25:10: info: assertion proved
p.ads:20:20: info: pointer dereference check proved
p.ads:20:47: info: pointer dereference check proved
p.ads:21:22: info: pointer dereference check proved
p.ads:21:33: info: pointer dereference check proved
p.ads:22:53: info: pointer dereference check proved
p.ads:22:62: info: pointer dereference check proved
p.ads:28:14: info: postcondition proved

p.ads:28:46: medium: memory leak might occur
   28 |     Post => Updated_If_Less_Than_Threshold (Copy (L)'Old, L, Threshold);
      |                                             ^~~~~~~
  possible explanation: call to allocating function inside an assertion leaks memory

The second pattern of update loops that we consider now is the one that updates elements based on their position:

Loop Pattern

Modification of Elements Based on Position

Proof Objective

Elements of the collection are updated based on their position.

Loop Behavior

Loops over a collection and assigns the elements whose position satisfies a given modification criterion.

Loop Invariant

Every element encountered so far has been assigned according to its position.

Consider a procedure Update_Range_Arr_Zero that sets to zero all elements in array A between indexes First and Last:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
with Loop_Types; use Loop_Types;

procedure Update_Range_Arr_Zero (A : in out Arr_T; First, Last : Index_T) with
  SPARK_Mode,
  Post => A = (A'Old with delta First .. Last => 0)
is
begin
   for J in First .. Last loop
      A(J) := 0;
      pragma Loop_Invariant (A = (A'Loop_Entry with delta First .. J => 0));
   end loop;
end Update_Range_Arr_Zero;

The loop invariant expresses that all elements between First and the current loop index J have been zeroed out, and that other elements have not been modified (using a combination of Attribute Loop_Entry and Delta Aggregates to express this concisely). With this loop invariant, GNATprove is able to prove the postcondition of Update_Range_Arr_Zero, namely that all elements between First and Last have been zeroed out, and that other elements have not been modified:

update_range_arr_zero.adb:5:11: info: postcondition proved
update_range_arr_zero.adb:10:30: info: loop invariant initialization proved
update_range_arr_zero.adb:10:30: info: loop invariant preservation proved

Consider now a variant of the same update loop over a vector:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
pragma Unevaluated_Use_Of_Old (Allow);
with Loop_Types; use Loop_Types; use Loop_Types.Vectors;
use Loop_Types.Vectors.Formal_Model;

procedure Update_Range_Vec_Zero (V : in out Vec_T; First, Last : Index_T) with
  SPARK_Mode,
  Pre  => Last <= Last_Index (V),
  Post => (for all J in 1 .. Last_Index (V) =>
               (if J in First .. Last then Element (V, J) = 0
                else Element (V, J) = Element (Model (V)'Old, J)))
is
begin
   for J in First .. Last loop
      Replace_Element (V, J, 0);
      pragma Loop_Invariant (Last_Index (V) = Last_Index (V)'Loop_Entry);
      pragma Loop_Invariant
        (for all I in 1 .. Last_Index (V) =>
               (if I in First .. J then Element (V, I) = 0
                else Element (V, I) = Element (Model (V)'Loop_Entry, I)));
   end loop;
end Update_Range_Vec_Zero;

Like for Map_Vec_Incr, we need to use the Model function over arrays to access elements of the vector before the loop as the vector type is limited. The loop invariant expresses that all elements between First and current loop index J have been zeroed, and that other elements have not been modified. With this loop invariant, GNATprove is able to prove the postcondition of Update_Range_Vec_Zero:

update_range_vec_zero.adb:8:11: info: postcondition proved
update_range_vec_zero.adb:9:44: info: precondition proved
update_range_vec_zero.adb:9:56: info: range check proved
update_range_vec_zero.adb:10:22: info: precondition proved
update_range_vec_zero.adb:10:34: info: range check proved
update_range_vec_zero.adb:10:39: info: precondition proved
update_range_vec_zero.adb:10:63: info: range check proved
update_range_vec_zero.adb:14:07: info: precondition proved
update_range_vec_zero.adb:15:30: info: loop invariant preservation proved
update_range_vec_zero.adb:15:30: info: loop invariant initialization proved
update_range_vec_zero.adb:17:10: info: loop invariant preservation proved
update_range_vec_zero.adb:17:10: info: loop invariant initialization proved
update_range_vec_zero.adb:18:41: info: precondition proved
update_range_vec_zero.adb:18:53: info: range check proved
update_range_vec_zero.adb:19:22: info: precondition proved
update_range_vec_zero.adb:19:34: info: range check proved
update_range_vec_zero.adb:19:39: info: precondition proved
update_range_vec_zero.adb:19:70: info: range check proved

Similarly, consider a variant of the same update loop over a list:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
with Loop_Types; use Loop_Types; use Loop_Types.Lists;
with Ada.Containers; use Ada.Containers; use Loop_Types.Lists.Formal_Model;

procedure Update_Range_List_Zero (L : in out List_T; First, Last : Cursor) with
  SPARK_Mode,
  Pre  => Has_Element (L, First) and then Has_Element (L, Last)
  and then P.Get (Positions (L), First) <= P.Get (Positions (L), Last),
  Post => Length (L) = Length (L)'Old
  and Positions (L) = Positions (L)'Old
  and (for all I in 1 .. Length (L) =>
            (if I in P.Get (Positions (L), First) .. P.Get (Positions (L), Last) then
               Element (Model (L), I) = 0
             else Element (Model (L), I) = Element (Model (L'Old), I)))
is
   Cu : Cursor := First;
begin
   loop
      pragma Loop_Invariant (Has_Element (L, Cu));
      pragma Loop_Invariant (P.Get (Positions (L), Cu) in P.Get (Positions (L), First) .. P.Get (Positions (L), Last));
      pragma Loop_Invariant (Length (L) = Length (L)'Loop_Entry);
      pragma Loop_Invariant (Positions (L) = Positions (L)'Loop_Entry);
      pragma Loop_Invariant (for all I in 1 .. Length (L) =>
                               (if I in P.Get (Positions (L), First) .. P.Get (Positions (L), Cu) - 1 then
                                   Element (Model (L), I) = 0
                                else Element (Model (L), I) = Element (Model (L'Loop_Entry), I)));
      Replace_Element (L, Cu, 0);
      exit when Cu = Last;
      Next (L, Cu);
   end loop;
end Update_Range_List_Zero;

Compared to the vector example, it requires three additional invariants. As the loop is done via a cursor, the first two loop invariants are necessary to know that the current cursor Cu stays between First and Last in the list. The fourth loop invariant states that the position of cursors in L is not modified during the loop. It is necessary to know that the two cursors First and Last keep designating the same range after the loop. With this loop invariant, GNATprove is able to prove the postcondition of Update_Range_List_Zero, namely that all elements between First and Last have been zeroed out, and that other elements have not been modified:

update_range_list_zero.adb:7:13: info: precondition proved
update_range_list_zero.adb:7:45: info: precondition proved
update_range_list_zero.adb:8:11: info: postcondition proved
update_range_list_zero.adb:11:23: info: precondition proved
update_range_list_zero.adb:11:55: info: precondition proved
update_range_list_zero.adb:12:16: info: precondition proved
update_range_list_zero.adb:13:19: info: precondition proved
update_range_list_zero.adb:13:44: info: precondition proved
update_range_list_zero.adb:18:30: info: loop invariant initialization proved
update_range_list_zero.adb:18:30: info: loop invariant preservation proved
update_range_list_zero.adb:19:30: info: loop invariant preservation proved
update_range_list_zero.adb:19:30: info: loop invariant initialization proved
update_range_list_zero.adb:19:31: info: precondition proved
update_range_list_zero.adb:19:60: info: precondition proved
update_range_list_zero.adb:19:92: info: precondition proved
update_range_list_zero.adb:20:30: info: loop invariant preservation proved
update_range_list_zero.adb:20:30: info: loop invariant initialization proved
update_range_list_zero.adb:21:30: info: loop invariant preservation proved
update_range_list_zero.adb:21:30: info: loop invariant initialization proved
update_range_list_zero.adb:22:30: info: loop invariant preservation proved
update_range_list_zero.adb:22:30: info: loop invariant initialization proved
update_range_list_zero.adb:23:42: info: precondition proved
update_range_list_zero.adb:23:74: info: precondition proved
update_range_list_zero.adb:24:36: info: precondition proved
update_range_list_zero.adb:25:38: info: precondition proved
update_range_list_zero.adb:25:63: info: precondition proved
update_range_list_zero.adb:26:07: info: precondition proved
update_range_list_zero.adb:28:07: info: precondition proved