source: CIVL/examples/verifyThis/pairInsertSort_sorted_loop-invariants-simple_bad.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/* Prove the sorted property of the main loop in a pair-insertion
2 * sorting algorithm. The loop invariants INCORRECTLY express that the
3 * given array is sorted either from [LEFT, RIGHT] or [LEFT, RIGHT + 1].
4 **/
5#include <stdio.h>
6#pragma CIVL ACSL
7$input int N = 6; // upper bound on n
8$input int n; // size of array
9$assume(1<=n && n<=N);
10$input int LEFT;
11$input int RIGHT;
12$assume(1<=LEFT && LEFT < RIGHT && RIGHT<n-1);
13
14int main() {
15 int a[n];
16
17 // assumption: a[LEFT-1] is less than every thing of right of LEFT:
18 $assume($forall (int j : LEFT .. RIGHT) a[j] >= a[LEFT-1]);
19 int left = LEFT, right = RIGHT;
20 int k = left;
21
22 left++;
23 /*@ loop invariant left <= right;
24 @ loop invariant LEFT <= k && k <= RIGHT+1 && k == left - 1;
25 @ loop invariant \forall int i; LEFT <= i && i <= RIGHT ==>
26 @ a[i] >= a[LEFT-1];
27 @ loop invariant \forall int j; LEFT <= j && j < k ==>
28 @ a[j] <= a[j+1];
29 @*/
30 for (;left <= right;) {
31 int a1 = a[k], a2 = a[left];
32 if (a1 < a2) {
33 a2 = a1; a1 = a[left];
34 }
35 k--;
36 while (a1 < a[k]) {
37 a[k + 2] = a[k];
38 k--;
39 }
40 a[++k + 1] = a1;
41 k--;
42 while (a2 < a[k]) {
43 a[k + 1] = a[k];
44 k--;
45 }
46 a[k + 1] = a2;
47 k = ++left;
48 left++;
49 }
50 $assert( $forall (int i : LEFT .. RIGHT-2) a[i] <= a[i+1] );
51}
Note: See TracBrowser for help on using the repository browser.