| [260762f] | 1 | /*
|
|---|
| [41eb9b6] | 2 | Author: Yihao
|
|---|
| [f3368e99] | 3 |
|
|---|
| [41eb9b6] | 4 | See challenge 1 of: http://etaps2015.verifythis.org/challenges
|
|---|
| 5 |
|
|---|
| 6 | -----------------
|
|---|
| 7 | Problem description:
|
|---|
| [260762f] | 8 |
|
|---|
| 9 | Verify a function isRelaxedPrefix determining if a list _pat_ (for
|
|---|
| 10 | pattern) is a relaxed prefix of another list _a_.
|
|---|
| 11 |
|
|---|
| 12 | The relaxed prefix property holds iff _pat_ is a prefix of _a_ after
|
|---|
| 13 | removing at most one element from _pat_.
|
|---|
| 14 |
|
|---|
| 15 | examples:
|
|---|
| 16 | pat = {1,3} is a relaxed prefix of a = {1,3,2,3} (standard prefix)
|
|---|
| 17 | pat = {1,2,3} is a relaxed prefix of a = {1,3,2,3} (remove 2 from pat)
|
|---|
| 18 | pat = {1,2,4} is not a relaxed prefix of a = {1,3,2,3}.
|
|---|
| 19 |
|
|---|
| [41eb9b6] | 20 | -----------------
|
|---|
| 21 | Verification Task:
|
|---|
| 22 |
|
|---|
| 23 | Implement the isRelaxedPrefix function which takes two arrays and their length
|
|---|
| 24 | as input and verify that it behaves as described.
|
|---|
| 25 |
|
|---|
| 26 | command: civl verify relaxedPrefix.c
|
|---|
| [260762f] | 27 |
|
|---|
| [41eb9b6] | 28 | result: the problem is solved
|
|---|
| [f7e5282] | 29 |
|
|---|
| [260762f] | 30 | */
|
|---|
| [0ae124a] | 31 |
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <stdbool.h>
|
|---|
| 34 | #include <civlc.cvh>
|
|---|
| 35 |
|
|---|
| [260762f] | 36 | $input int N1_BOUND = 4;
|
|---|
| 37 | $input int N2_BOUND = 3;
|
|---|
| 38 | $input int n1;
|
|---|
| 39 | $input int n2;
|
|---|
| 40 | $input int X2[n2];
|
|---|
| [f3368e99] | 41 | $input int X1[n1];
|
|---|
| 42 | $assume (n2>=0 && n2 < N2_BOUND);
|
|---|
| 43 | $assume (n1>=0 && n1 < N1_BOUND);
|
|---|
| [260762f] | 44 |
|
|---|
| [0ae124a] | 45 | bool isRelaxedPrefix(int* pat, int patLen, int* a, int aLen) {
|
|---|
| [260762f] | 46 | int shift = 0;
|
|---|
| 47 | int i;
|
|---|
| [f3368e99] | 48 |
|
|---|
| [260762f] | 49 | if(patLen > aLen+1) return false;
|
|---|
| [f3368e99] | 50 |
|
|---|
| [4b8f2689] | 51 | if(aLen == 0) return true;
|
|---|
| [f3368e99] | 52 |
|
|---|
| [0ae124a] | 53 | for(i=0; i<patLen; i++) {
|
|---|
| 54 | if(pat[i] != a[i-shift]) {
|
|---|
| [260762f] | 55 | if(shift == 0)
|
|---|
| 56 | shift = 1;
|
|---|
| 57 | else
|
|---|
| 58 | return false;
|
|---|
| [f3368e99] | 59 | }
|
|---|
| [4b8f2689] | 60 | if(i == aLen - 1 && shift == 0) return true;
|
|---|
| 61 | }
|
|---|
| [260762f] | 62 | return true;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| [0ae124a] | 65 | void main() {
|
|---|
| [4b8f2689] | 66 | bool result = isRelaxedPrefix(X1, n1, X2, n2);
|
|---|
| [f3368e99] | 67 |
|
|---|
| [0ae124a] | 68 | if(n1 > n2+1) {
|
|---|
| [260762f] | 69 | $assert(!result);
|
|---|
| [0ae124a] | 70 | } else if(n1 == n2+1) {
|
|---|
| [4b8f2689] | 71 | $assert( result ==
|
|---|
| 72 | ($exists {int k | k >= 0 && k < n1}
|
|---|
| [260762f] | 73 | (
|
|---|
| 74 | ($forall {int i | i >= 0 && i < k} X1[i] == X2[i]) &&
|
|---|
| 75 | ($forall {int i | i > k && i < n1} X1[i] == X2[i-1])
|
|---|
| 76 | )
|
|---|
| 77 | )
|
|---|
| 78 | );
|
|---|
| [0ae124a] | 79 | } else {
|
|---|
| [260762f] | 80 | $assert(result ==
|
|---|
| 81 | ($exists {int k | k >= 0 && k <=n1}
|
|---|
| 82 | (
|
|---|
| 83 | ($forall {int i | i >= 0 && i < k} X1[i] == X2[i]) &&
|
|---|
| 84 | ($forall {int i | i > k && i < n1} X1[i] == X2[i-1])
|
|---|
| 85 | )
|
|---|
| 86 | )
|
|---|
| 87 | );
|
|---|
| [f3368e99] | 88 | }
|
|---|
| [260762f] | 89 | }
|
|---|