source: CIVL/examples/verifyThis/pairInsertSort.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.3 KB
RevLine 
[d604c6f]1typedef int T;
2$input int n, N = 5, LEFT, RIGHT;
3$assume(1<=n && n<=N && 1<=LEFT && LEFT<=RIGHT && RIGHT<n);
4$input T A[n];
5int count(int n, T *p, int val) {
6 int result = 0;
7 for (int i=0; i < n; i++) if ((p[i]) == val) result++;
8 return result;
9}
10/* Asserts the sequence starting at p2 is the permutation of that
11 * starting at p1 with elements occurring in nondecreasing order */
12void assertSortOf(int n, T *p1, T *p2) {
13 $assert($forall (int i : 0..n-2) p2[i]<=p2[i+1]);
14 $assert($forall (int i : 0..n-1) $exists (int j : 0..n-1) p1[i]==p2[j]);
15 for (int i=0; i<n; i++) $assert(count(n, p1, p2[i]) == count(n, p2, p2[i]));
16}
[9ac73ac]17int main() {
[d604c6f]18 for (int i=LEFT; i<=RIGHT; i++) $assume(A[LEFT-1]<=A[i]);
[9ac73ac]19 int a[n];
20 for (int i=0; i<n; i++) a[i]=A[i];
21 int left = LEFT, right = RIGHT;
[d604c6f]22 for (int k = left; ++left <= right; k = ++left) {
[9ac73ac]23 int a1 = a[k], a2 = a[left];
[d604c6f]24 if (a1 < a2) { a2 = a1; a1 = a[left]; }
25 while (a1 < a[--k]) a[k + 2] = a[k];
[9ac73ac]26 a[++k + 1] = a1;
[d604c6f]27 while (a2 < a[--k]) a[k + 1] = a[k];
[9ac73ac]28 a[k + 1] = a2;
29 }
30 int last = a[right];
[d604c6f]31 while (last < a[--right]) a[right + 1] = a[right];
[9ac73ac]32 a[right + 1] = last;
[d604c6f]33 $assert($forall (int i:0..LEFT-1) a[i]==A[i]);
34 $assert($forall (int i:RIGHT+1..n-1) a[i]==A[i]);
35 assertSortOf(RIGHT-LEFT+1, A+LEFT, a+LEFT);
[9ac73ac]36}
Note: See TracBrowser for help on using the repository browser.