source: CIVL/examples/verifyThisProblems/relaxedPrefix.c@ 315d1ee

1.23 2.0 main test-branch
Last change on this file since 315d1ee was 41eb9b6, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

format changing

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

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/*
2Author: Yihao
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: the problem is solved
29
30*/
31
32#include <stdio.h>
33#include <stdbool.h>
34#include <civlc.cvh>
35
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];
41$input int X1[n1];
42$assume (n2>=0 && n2 < N2_BOUND);
43$assume (n1>=0 && n1 < N1_BOUND);
44
45bool isRelaxedPrefix(int* pat, int patLen, int* a, int aLen) {
46 int shift = 0;
47 int i;
48
49 if(patLen > aLen+1) return false;
50
51 if(aLen == 0) return true;
52
53 for(i=0; i<patLen; i++) {
54 if(pat[i] != a[i-shift]) {
55 if(shift == 0)
56 shift = 1;
57 else
58 return false;
59 }
60 if(i == aLen - 1 && shift == 0) return true;
61 }
62 return true;
63}
64
65void main() {
66 bool result = isRelaxedPrefix(X1, n1, X2, n2);
67
68 if(n1 > n2+1) {
69 $assert(!result);
70 } else if(n1 == n2+1) {
71 $assert( result ==
72 ($exists {int k | k >= 0 && k < n1}
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 );
79 } else {
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 );
88 }
89}
Note: See TracBrowser for help on using the repository browser.