source: CIVL/examples/verifyThisProblems/relaxedPrefix.c@ 0e12c94

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 0e12c94 was f7e5282, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

minor changes

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

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