source: CIVL/examples/verifyThis/relaxedPrefix.c

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: 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
26-----------------
27Result:
28
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. Therefore, the isRelaxedPrefix behaves correctly.
32
33-----------------
34command: civl verify relaxedPrefix.c
35
36*/
37
38#include <stdio.h>
39#include <stdbool.h>
40#include <civlc.cvh>
41
42$input int N1_BOUND = 4;
43$input int N2_BOUND = 3;
44$input int n1;
45$input int n2;
46$assume (n2>0 && n2 < N2_BOUND);
47$assume (n1>0 && n1 < N1_BOUND);
48$input int X2[n2];
49$input int X1[n1];
50
51bool isRelaxedPrefix(int* pat, int patLen, int* a, int aLen) {
52 int shift = 0;
53 int i;
54
55 if(patLen > aLen+1) return false;
56
57 if(aLen == 0) return true;
58
59 for(i=0; i<patLen; i++) {
60 if(pat[i] != a[i-shift]) {
61 if(shift == 0)
62 shift = 1;
63 else
64 return false;
65 }
66 if(i == aLen - 1 && shift == 0) return true;
67 }
68 return true;
69}
70
71void main() {
72 bool result = isRelaxedPrefix(X1, n1, X2, n2);
73
74 if(n1 > n2+1) {
75 $assert(!result);
76 } else if(n1 == n2+1) {
77 $assert( result ==
78 ($exists (int k: 0 .. n1-1)
79 (
80 ($forall (int i: 0 .. k-1) X1[i] == X2[i]) &&
81 ($forall (int i: k+1 .. n1-1) X1[i] == X2[i-1])
82 )
83 )
84 );
85 } else {
86 $assert(result ==
87 ($exists (int k: 0 .. n1)
88 (
89 ($forall (int i: 0 .. k-1) X1[i] == X2[i]) &&
90 ($forall (int i: k+1 .. n1-1) X1[i] == X2[i-1])
91 )
92 )
93 );
94 }
95}
Note: See TracBrowser for help on using the repository browser.