source: CIVL/examples/verifyThisProblems/relaxedPrefix.c@ 05f3dc42

1.23 2.0 main test-branch
Last change on this file since 05f3dc42 was 76f18de, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

format changes

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

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