source: CIVL/examples/verifyThis/pairInsertSort_sorted_loop-invariants-simple.cvl

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <stdio.h>
2#pragma CIVL ACSL
3$input int N = 6; // upper bound on n
4$input int n; // size of array
5$assume(1<=n && n<=N);
6$input int LEFT;
7$input int RIGHT;
8$assume(1<=LEFT && LEFT < RIGHT && RIGHT<n-1);
9
10int main() {
11 int a[n];
12
13 // assumption: a[LEFT-1] is less than every thing of right of LEFT:
14 $assume($forall (int j : LEFT .. RIGHT) a[j] >= a[LEFT-1]);
15 int left = LEFT, right = RIGHT;
16 int k = left;
17
18 left++;
19 /*@ loop invariant left <= right + 2; // initially left <= right, left <= right+2 at termination
20 @ loop invariant LEFT <= k && k <= RIGHT+1 && k == left - 1; // initially LEFT <= k < RIGHT, LEFT <= k <= RIGHT + 1 at termination
21 @ loop invariant \forall int i; LEFT <= i && i <= RIGHT ==>
22 @ a[i] >= a[LEFT-1];
23 @ loop invariant \forall int j; LEFT <= j && j < k ==>
24 @ a[j-1] <= a[j];
25 @*/
26 for (;left <= right;) {
27 int a1 = a[k], a2 = a[left];
28 if (a1 < a2) {
29 a2 = a1; a1 = a[left];
30 }
31 k--;
32 while (a1 < a[k]) {
33 a[k + 2] = a[k];
34 k--;
35 }
36 a[++k + 1] = a1;
37 k--;
38 while (a2 < a[k]) {
39 a[k + 1] = a[k];
40 k--;
41 }
42 a[k + 1] = a2;
43 k = ++left;
44 left++;
45 }
46 // [LEFT, RIGHT-1] is guaranteed to be sorted, but if element at [RIGHT] is
47 // sorted depends on if length of the sub-array is even.
48 $assert( $forall (int i : LEFT .. RIGHT-2) a[i] <= a[i+1] );
49}
Note: See TracBrowser for help on using the repository browser.