Wiki Agenda Contact Version française

VerifyThis @ FM 2012, problem 2

Computes prefix sums of an array, in-place


Authors: Claude Marché / François Bobot

Topics: Array Data Structure / Trees / Ghost code

Tools: Why3

References: VerifyThis @ FM2012

see also the index (by topic, by tool, by reference, by year)


download ZIP archive

The VerifyThis competition at FM2012: Problem 2

see this web page

module PrefixSumRec

   use int.Int
   use int.ComputerDivision
   use int.Power

   use map.Map
   use array.Array
   use array.ArraySum

Preliminary lemmas on division by 2 and power of 2

  lemma Div_mod_2:
    forall x:int. x >= 0 -> x >= 2 * div x 2 >= x-1

Needed for the proof of phase1_frame and phase1_frame2

  predicate is_power_of_2 (x:int) = exists k:int. (k >= 0 /\ x = power 2 k)

The step must be a power of 2

  (* needed *)
  lemma is_power_of_2_1:
    forall x:int. is_power_of_2 x -> x > 1 -> 2 * div x 2 = x

Modeling the "upsweep" phase

  function go_left (left right:int) : int =
    let space = right - left in left - div space 2

Shortcuts

  function go_right (left right:int) : int =
    let space = right - left in right - div space 2

Description in a purely logic way the effect of the first phase "upsweep" of the algorithm. The second phase "downsweep" then traverses the array in the same way than the first phase. Hence, the inductive nature of this definition is not an issue.

  inductive phase1 (left:int) (right:int) (a0: array int) (a: array int) =
    | Leaf : forall left right:int, a0 a : array int.
       right = left + 1 ->
       a[left] = a0[left] ->
       phase1 left right a0 a
    | Node : forall left right:int, a0 a : array int.
       right > left + 1 ->
       phase1 (go_left left right) left a0 a ->
       phase1 (go_right left right) right a0 a ->
       a[left] = sum a0 (left-(right-left)+1) (left+1) ->
       phase1 left right a0 a

Frame properties of the "phase1" inductive

  let rec lemma phase1_frame (left right:int) (a0 a a' : array int) : unit
    requires { forall i:int. left-(right-left) < i < right ->
               a[i] = a'[i]}
    requires { phase1 left right a0 a }
    variant { right-left }
    ensures { phase1 left right a0 a' } =
    if right > left + 1 then begin
       phase1_frame (go_left left right) left a0 a a';
       phase1_frame (go_right left right) right a0 a a'
    end

frame lemma for "phase1" on fourth argument. needed to prove both upsweep, downsweep and compute_sums

  let rec lemma phase1_frame2 (left right:int) (a0 a0' a : array int) : unit
    requires { forall i:int. left-(right-left) < i < right ->
               a0[i] = a0'[i]}
    requires { phase1 left right a0 a }
    variant { right-left }
    ensures { phase1 left right a0' a } =
    if right > left + 1 then begin
       phase1_frame2 (go_left left right) left a0 a0' a;
       phase1_frame2 (go_right left right) right a0 a0' a
    end

frame lemma for "phase1" on third argument. needed to prove upsweep and compute_sums

The upsweep phase

First function: modify partially the table and compute some intermediate partial sums

  let rec upsweep (left right: int) (a: array int)
    requires { 0 <= left < right < a.length }
    requires { -1 <= left - (right - left) }
    requires { is_power_of_2 (right - left) }
    variant { right - left }
    ensures { phase1 left right (old a) a }
    ensures { let space = right - left in
      a[right] = sum (old a) (left-space+1) (right+1) /\
      (forall i: int. i <= left-space -> a[i] = (old a)[i]) /\
      (forall i: int. i > right -> a[i] = (old a)[i]) }
  = let space = right - left in
    if right > left+1 then begin
      upsweep (left - div space 2) left a;
      upsweep (right - div space 2) right a;
      assert { phase1 (left - div space 2) left (old a) a };
      assert { phase1 (right - div space 2) right (old a) a };
      assert { a[left] = sum (old a) (left-(right-left)+1) (left+1) };
      assert { a[right] = sum (old a) (left+1) (right+1) }
    end;
    a[right] <- a[left] + a[right];
    assert {
      right > left+1 -> phase1 (left - div space 2) left (old a) a };
    assert {
      right > left+1 -> phase1 (right - div space 2) right (old a) a }

The downsweep phase

  predicate partial_sum (left:int) (right:int) (a0 a : array int) =
    forall i : int. (left-(right-left)) < i <= right -> a[i] = sum a0 0 i

The property we ultimately want to prove

  let rec downsweep (left right: int) (ghost a0 : array int) (a : array int)
    requires { 0 <= left < right < a.length }
    requires { -1 <= left - (right - left) }
    requires { is_power_of_2 (right - left) }
    requires { a[right] = sum a0 0 (left-(right-left) + 1) }
    requires { phase1 left right a0 a }
    variant  { right - left }
    ensures { partial_sum left right a0 a }
    ensures { forall i: int. i <= left-(right-left) -> a[i] = (old a)[i] }
    ensures { forall i: int. i > right -> a[i] = (old a)[i] }
  = let tmp = a[right] in
    assert { a[right] = sum a0 0 (left-(right-left) + 1) };
    assert { a[left] = sum a0 (left-(right-left)+1) (left+1) };
    a[right] <- a[right] + a[left];
    a[left] <- tmp;
    assert { a[right] = sum a0 0 (left + 1) };
    if right > left+1 then
    let space = right - left in
    assert { phase1 (go_left left right) left a0 (old a) };
    assert { phase1 (go_right left right) right a0 (old a) };
    assert { phase1 (go_right left right) right a0 a };
    downsweep (left - div space 2) left a0 a;
    assert { phase1 (go_right left right) right a0 a };
    downsweep (right - div space 2) right a0 a;
    assert { partial_sum (left - div space 2) left a0 a };
    assert { partial_sum (right - div space 2) right a0 a }

Second function: complete the partial using the remaining intial value and the partial sum already computed

The main procedure

  let compute_sums a
    requires { a.length >= 2 }
    requires { is_power_of_2 a.length }
    ensures { forall i : int. 0 <= i < a.length -> a[i] = sum (old a) 0 i }
  = let a0 = ghost (copy a) in
    let l = a.length in
    let left = div l 2 - 1 in
    let right = l - 1 in
    upsweep left right a;
    (* needed for the precondition of downsweep *)
    assert { phase1 left right a0 a };
    a[right] <- 0;
    downsweep left right a0 a;
    (* needed to prove the post-condition *)
    assert { forall i : int.
      left-(right-left) < i <= right -> a[i] = sum a0 0 i }

A simple test

  let test_harness () =
    let a = make 8 0 in
    (* needed for the precondition of compute_sums *)
    assert { power 2 3 = a.length };
    a[0] <- 3; a[1] <- 1; a[2] <- 7; a[3] <- 0;
    a[4] <- 4; a[5] <- 1; a[6] <- 6; a[7] <- 3;
    compute_sums a;
    assert { a[0] = 0 };
    assert { a[1] = 3 };
    assert { a[2] = 4 };
    assert { a[3] = 11 };
    assert { a[4] = 11 };
    assert { a[5] = 15 };
    assert { a[6] = 16 };
    assert { a[7] = 22 }

  exception BenchFailure

  let bench () raises { BenchFailure -> true } =
    let a = make 8 0 in
    (* needed for the precondition of compute_sums *)
    assert { power 2 3 = a.length };
    a[0] <- 3; a[1] <- 1; a[2] <- 7; a[3] <- 0;
    a[4] <- 4; a[5] <- 1; a[6] <- 6; a[7] <- 3;
    compute_sums a;
    if a[0] <> 0 then raise BenchFailure;
    if a[1] <> 3 then raise BenchFailure;
    if a[2] <> 4 then raise BenchFailure;
    if a[3] <> 11 then raise BenchFailure;
    if a[4] <> 11 then raise BenchFailure;
    if a[5] <> 15 then raise BenchFailure;
    if a[6] <> 16 then raise BenchFailure;
    if a[7] <> 22 then raise BenchFailure;
    a

end

Why3 Proof Results for Project "verifythis_PrefixSumRec"

Theory "verifythis_PrefixSumRec.PrefixSumRec": fully verified

ObligationsAlt-Ergo 2.0.0CVC3 2.4.1CVC4 1.4CVC4 1.5Z3 3.2Z3 4.3.2Z3 4.6.0
Div_mod_20.010.020.03---0.020.02---
is_power_of_2_10.010.030.02------------
VC phase1_frame---------------------
split_goal_right
VC phase1_frame.00.01---0.02------0.01---
VC phase1_frame.10.01---0.02------0.02---
VC phase1_frame.20.02---0.02------------
VC phase1_frame.30.01---0.03------0.01---
VC phase1_frame.40.02---0.03------0.01---
VC phase1_frame.50.01---0.02------------
VC phase1_frame.6---0.04---------------
remove real,bool,tuple0,unit,map,container,zero,one,( * ),(<=),(>=),abs,mod,power,get,make,is_power_of_2,Assoc,Unit_def_l,Unit_def_r,Inv_def_l,Inv_def_r,Comm,Assoc1,Mul_distr_l,Mul_distr_r,Comm1,Unitary,NonTrivialRing,Refl,Trans,Antisymm,Total,ZeroLessOne,CompatOrderAdd,CompatOrderMult,Abs_le,Abs_pos,Div_mod,Div_bound,Mod_bound,Div_sign_pos,Div_sign_neg,Mod_sign_pos,Mod_sign_neg,Rounds_toward_zero,Div_1,Mod_1,Div_inf,Mod_inf,Div_mult,Mod_mult,Power_0,Power_s,Power_s_alt,Power_1,Power_sum,Power_mult,Power_comm1,Power_comm2,Power_non_neg,Power_monotonic,Sum_def_empty,Sum_def_non_empty,Sum_right_extension,Sum_transitivity,Sum_eq,array'invariant,make_spec,Div_mod_2,is_power_of_2_1
VC phase1_frame.6.0------------------0.06
VC phase1_frame2---------------------
split_goal_right
VC phase1_frame2.00.01---0.02------0.01---
VC phase1_frame2.10.01---0.02------0.01---
VC phase1_frame2.20.02---0.03------------
VC phase1_frame2.30.02---0.03------0.01---
VC phase1_frame2.40.02---0.04------0.01---
VC phase1_frame2.50.01---0.02------------
VC phase1_frame2.6---4.32------0.02------
VC upsweep---------------------
split_goal_right
VC upsweep.0---------0.03---------
VC upsweep.10.02------------------
VC upsweep.20.020.010.02---1.06------
VC upsweep.30.020.020.02---0.020.03---
VC upsweep.40.030.060.04------------
VC upsweep.5---------0.02---------
VC upsweep.60.02------------------
VC upsweep.70.020.020.02---0.020.03---
VC upsweep.80.010.010.02---0.020.03---
VC upsweep.90.010.170.02------------
VC upsweep.100.100.130.05------------
VC upsweep.110.040.120.05------------
VC upsweep.12---0.02---------------
VC upsweep.13---3.68---------------
VC upsweep.140.020.020.02---0.020.02---
VC upsweep.150.020.020.01---0.020.05---
VC upsweep.160.010.000.02---0.000.00---
VC upsweep.170.04---0.09------------
VC upsweep.180.03---0.11------------
VC upsweep.19---0.22------0.06------
VC upsweep.200.47---0.07------------
VC upsweep.210.000.000.01---0.020.02---
VC upsweep.220.010.030.02---0.000.00---
VC upsweep.230.010.020.01---0.010.03---
VC upsweep.240.000.010.02---0.020.03---
VC upsweep.250.010.010.01---0.020.04---
VC upsweep.260.020.01------0.090.11---
VC upsweep.270.460.040.07------------
VC downsweep---------------------
split_goal_right
VC downsweep.00.010.020.02---0.030.03---
VC downsweep.10.000.060.01---0.010.00---
VC downsweep.2------0.02---0.04------
VC downsweep.30.010.020.02---0.030.02---
VC downsweep.40.030.030.03---0.040.04---
VC downsweep.50.020.030.02---0.020.03---
VC downsweep.60.010.020.01---0.000.00---
VC downsweep.7------0.04------------
VC downsweep.80.040.020.02------------
VC downsweep.9---0.020.02------------
VC downsweep.100.12---0.09------------
VC downsweep.11---------0.02---------
VC downsweep.120.02------------------
VC downsweep.130.010.030.01---0.020.04---
VC downsweep.140.020.020.03---0.040.03---
VC downsweep.150.18---0.10------------
VC downsweep.160.100.090.06------------
VC downsweep.176.574.71------0.74------
VC downsweep.180.040.020.04------------
VC downsweep.19---------0.02---------
VC downsweep.200.06------------------
VC downsweep.210.130.020.01---0.000.00---
VC downsweep.220.020.060.02---0.020.03---
VC downsweep.230.110.350.17---9.17------
VC downsweep.240.030.020.16------------
VC downsweep.25---0.08------0.01------
VC downsweep.260.03---0.06---0.050.05---
VC downsweep.270.030.040.02---0.020.03---
VC downsweep.280.150.480.07------------
VC downsweep.290.08---1.07---3.34------
VC downsweep.300.15---0.02---0.030.04---
VC downsweep.310.020.12---------------
VC downsweep.320.110.030.04---0.080.11---
VC downsweep.330.020.030.03------------
VC compute_sums---------------------
split_goal_right
VC compute_sums.0---------0.02---------
VC compute_sums.10.020.010.01---1.12------
VC compute_sums.20.010.010.03---0.040.06---
VC compute_sums.3---0.030.04---9.70------
VC compute_sums.40.090.030.04------------
VC compute_sums.50.020.030.02---0.020.03---
VC compute_sums.60.020.020.01---0.020.03---
VC compute_sums.70.020.020.01---0.030.03---
VC compute_sums.8---0.130.02------------
VC compute_sums.90.020.030.02---0.070.05---
VC compute_sums.100.020.060.08------------
VC compute_sums.110.02---0.04---0.050.06---
VC compute_sums.12------0.36------------
VC test_harness---------------------
split_goal_right
VC test_harness.00.010.010.01---0.000.00---
VC test_harness.10.070.020.04---0.050.05---
VC test_harness.20.010.010.02---0.000.00---
VC test_harness.30.020.020.01---0.000.00---
VC test_harness.40.010.010.01---0.000.00---
VC test_harness.50.010.020.01---0.000.00---
VC test_harness.60.020.010.00---0.000.00---
VC test_harness.70.000.020.01---0.000.00---
VC test_harness.80.010.010.02---0.000.00---
VC test_harness.90.010.010.00---0.000.00---
VC test_harness.100.020.010.01---0.000.00---
VC test_harness.110.020.030.02---0.020.03---
VC test_harness.120.030.020.13---0.020.04---
VC test_harness.130.040.030.03------------
VC test_harness.140.040.020.03------------
VC test_harness.150.040.020.03------------
VC test_harness.160.040.020.05------------
VC test_harness.170.040.030.03---0.35------
VC test_harness.180.040.030.06---0.334.65---
VC test_harness.190.040.030.04---0.240.84---
VC bench3.47------------------