1

This message is going to be a bit long, but that is because I want to explain it as best as possible.

In Dafny, I had the next problem: Given an array, count the number of segments of length k in which this happens; the number of positives in the left-half of the segment is bigger or equal to the right-half.

As an example (imagine segments can only be even, so that there is no discussion about what a half is):

k=2 ---> count(array[-4,-2,2,1],k) ---> 2, as [-4,-2] fulfills and also [2,1]
k=4 ---> count(array[-4,-2,2,1],k) ---> 0, as [-4,-2,2,1] does not fulfil.
k=6 ---> count(array[-4,-2,2,1],k) ---> 0, as there are not length 6 segments.

I have implemented this in two ways: one with a function, using high-order and polymorphism (but it is not linear) and the second one with an iterative implementation which does it linearly.

The function works this way:

  1. We define a Count function, which counts a property on an array.
  2. We define a way of, given an array, producing all the segments of the given length.
  3. We define a predicate that, using Count, tells us if the property is fulfilled.
function method CountAux<T>(P: T -> bool, sequ: seq<T>, i: int): int 
//the warning just warns about how it is implemented, as when it warns about the triggers' choice
  requires 0 <= i <= |sequ|
  decreases |sequ| - i //necessary to prove termination
  ensures CountAux(P,sequ,i)>=0; //trivial
{
  if i == |sequ| then 0
  else (if P(sequ[i]) then 1 else 0) + CountAux(P, sequ, i+1)
}

function method Count<T>(P: T -> bool, sequ: seq<T>): int
  ensures CountAux(P, sequ, 0)>=0; //trivial
{
  CountAux(P, sequ, 0)
}

function method produce_segments (sequ:seq<int>, seg_length:int) : seq<seq<int>>
  requires |sequ| >= seg_length >= 0
   decreases sequ 
  {
    if |sequ| == 0 then [] //if the list is empty
    else if |sequ|-1 < seg_length then [sequ[0..seg_length]] //if the list cannot have any more segments
    else [sequ[0..seg_length]]+produce_segments(sequ[1..],seg_length)
  }

function method segment_LeftMoreSegmentRight (sequ:seq<int>) : bool
  {
    if (Count(x => x >= 0, sequ[0..|sequ|/2])) >= (Count(x => x >= 0, sequ[|sequ|/2..|sequ|])) then true
    else false
  }

Now, the method works this way:

  1. We initialize an array of length k where we have counted all the positives at the left and in the right.
  2. Now, from k on, on each iteration, we will increment, decrement or leave the same number of positives on each side: for instance, if v[i]>=0 then we add 1 to the number of positives on the right (and, obviously, we decrement this number in v[i-k/2] if it was positive).
  3. To count that, we define a predicate that tells us if the number of positives on the right is higher or equal than on the left.
//returns if the number of positives left is higher than the right or not
function method segmentL_isMore_right (pos_left:int,pos_right:int) : bool
  {
    if pos_left>=pos_right then true
    else false
  }
 
 method initialize(sequ:seq<int>, seg_length:int) returns (sequ_new:seq<int>,pos_left: int,pos_right:int)
  requires |sequ| >= seg_length >= 0
{
  var length := |sequ|;
  var pos_left_local := 0;
  var pos_right_local :=0;
  var i:=0;
  while i<(length/2) 
  {
    if sequ[i]>=1 {pos_left_local:=pos_left_local+1;}
    i:=i+1;
  }
  while i<(length) 
  {
    if sequ[i]>=1 {pos_right_local:=pos_right_local+1;}
    i:=i+1;
  }
  
  return sequ[0..],pos_left_local,pos_right_local;
}

//Linear implementation

method Count_linear(sequ:seq<int>, seg_length:int) returns (num_segmts: int)
  requires |sequ| >= seg_length >= 0
  ensures num_segmts == Count(segment_LeftMoreSegmentRight, produce_segments(sequ,seg_length));
{
  var num_segmts_local := 0;
  var new_seq, pos_left_local, pos_right_local := initialize(sequ,seg_length);
  var i:=seg_length+1;
  while i<|sequ| 
    decreases |sequ|-i
  {
    if (sequ[i] >=0) {pos_right_local:=pos_right_local+1;}
    if (sequ[i-seg_length]>=0) {pos_left_local:=pos_left_local+1;}
    if (sequ[i-seg_length]/2>=0) 
        {
        pos_right_local:=pos_right_local-1;
        pos_left_local:=pos_left_local+1;
        }
    i:=i+1;
    if segmentL_isMore_right (pos_left_local,pos_right_local) 
      {
        num_segmts_local:=num_segmts_local+1;
      }
  }
  return num_segmts_local;
}


\\if want to prove it:
method Main()
{
  var aseq := [1,2,3,4,5,6,7,8,9,10];
  var seg2_leftRight := Count(segment_LeftMoreSegmentRight, produce_segments(aseq,4)); //high order and polymorphic
}

Now, the key is that ensures num_segmts == Count(segment_LeftMoreSegmentRight, produce_segments(sequ,seg_length)); does not verify. Obviously, I will have to prove some properties with lemmas about Count or Count_linear, but do not know where to start with this problem.

Any help? I hope I have explained the problem as best as possible.

Theo Deep
  • 666
  • 4
  • 15
  • 1
    I am working on a complete proof for you, but just a few quick comments for now: (1) your code has bugs :), you can try running adding some print statements and testing things first before trying to prove it; (2) ensures clauses on methods that have loops in their bodies will *always* need loop invariants. – James Wilcox Mar 10 '21 at 16:06
  • First of all, lots of thanks. (1) You are completely right, I proved it with `var bseq := [[1,2,3,4,5,6,7,8,9,10]];` over `var seg_leftRight := Count(segment_LeftMoreSegmentRight, bseq)` and did work (i.e. it works to apply the Count to one concrete segment). I see I had not proved with the one you say, but I thought I had. Now it makes much more sense that indeed the ensures is failing. (2) I thought that the current version of Dafny did not always need invariants, at least with simple algorithms, so I will work on that before asking this kind of questions again. Sorry for the loss of time. – Theo Deep Mar 10 '21 at 16:38
  • 1
    No problem! It's not a waste of time. I will get back to a full proof in a few hours. To answer your question about loop invariants, no, the only loop invariants that Dafny can automatically infer are things like `i <= e` where `i` is an integer variable and `e` is either a constant or the loop comparison expression. And even then, it can only *sometimes* infer these invariants automatically, so I prefer to approximate its behavior by saying it always requires loop invariants. – James Wilcox Mar 10 '21 at 16:47
  • Thanks a lot! I will take your advice and bear in mind invariants are always needed. I have been for a while in Dafny, but I almost forgot the iterative way of programming :( – Theo Deep Mar 10 '21 at 17:07
  • I have been testing the functions, and found no problem. I have tested, for instance, ```var aseq := [1,2,-1,4,5,6,7,-1,9,10]; var bseq := [[1,2,3,4,5,6,7,8,9,10]]; var seg_leftRight := Count(segment_LeftMoreSegmentRight, bseq); var seg2_leftRight := Count(segment_LeftMoreSegmentRight, produce_segments(aseq,5)); ``` with different ```aseq``` and ```bseq```s and also with different segment-lengths. I am probably wrong: which are the failing cases? – Theo Deep Mar 11 '21 at 17:36

1 Answers1

1

I am making this an answer just so I can get nice code formatting.

Your Count_linear has a bug.

  var aseq := [1,2,3,4,5,6,7,8,9,10];
  var slow := Count(segment_LeftMoreSegmentRight, produce_segments(aseq,4)); //high order and polymorphic
  var fast := Count_linear(aseq, 4);
  print slow, "\n";
  print fast, "\n";

prints

7
5
James Wilcox
  • 5,307
  • 16
  • 25