1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/**************************************************************************/
/* */
/* The Why platform for program certification */
/* Copyright (C) 2002-2008 */
/* Romain BARDOU */
/* Jean-François COUCHOT */
/* Mehdi DOGGUY */
/* Jean-Christophe FILLIÂTRE */
/* Thierry HUBERT */
/* Claude MARCHÉ */
/* Yannick MOY */
/* Christine PAULIN */
/* Yann RÉGIS-GIANAS */
/* Nicolas ROUSSET */
/* Xavier URBAIN */
/* */
/* This software is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public */
/* License version 2, as published by the Free Software Foundation. */
/* */
/* This software is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* */
/* See the GNU General Public License version 2 for more details */
/* (enclosed in the file GPL). */
/* */
/**************************************************************************/
//@ lemma mean1 : (\forall integer x y ; x <= y ==> x <= (x + y) / 2) ;
//@ lemma mean2 : (\forall integer x y ; x < y ==> (x + y) / 2 < y) ;
//@ type intset ;
//@ predicate In(int n, intset s) ;
//@ logic intset emptyset() ;
//@ logic intset union(intset s1, intset s2) ;
//@ logic intset singleton(int n) ;
/*@ axiom union_empty_left :
@ \forall intset s ; union(emptyset(),s) == s;
@*/
/*@ axiom In_emptyset :
@ \forall int n ; ! In(n,emptyset()) ;
@*/
/*@ axiom In_singleton :
@ \forall int n k ;
@ In(n,singleton(k)) <==> n==k ;
@*/
/*@ axiom In_union :
@ \forall intset s1 s2, int n ;
@ In(n,union(s1,s2)) <==> In(n,s1) || In(n,s2) ;
@*/
/*@ axiom intset_ext :
@ \forall intset s1 s2;
@ (\forall int n ; In(n,s1) <==> In(n,s2)) ==> s1==s2 ;
@*/
/*@ predicate array_models(intset s, int t[], integer i, integer j) {
@ t != null &&
@ 0 <= i && j < t.length &&
@ (\forall int n; In(n,s) <==>
@ (\exists int k; i <= k <= j && n==t[k]))
@ }
@*/
/*@ predicate IntSetModelField_models(intset s, IntSetModelField i) {
@ i != null &&
@ array_models(s,i.t,0,i.size-1)
@ }
@*/
public class IntSetModelField {
int size;
int t[];
/*@ invariant private_inv:
@ t != null && 0 <= size <= t.length &&
@ (\forall integer i j; 0 <= i <= j < size ==> t[i] <= t[j]);
@*/
//@ model intset my_model;
//@ invariant repr_inv: IntSetModelField_models(this.my_model,this);
/*@ assigns my_model;
@ ensures my_model == emptyset();
@*/
IntSetModelField () {
t = new int[10];
size = 0;
}
/*@ assigns \nothing;
@ ensures
@ 0 <= \result <= size &&
@ (\forall integer i; 0 <= i < \result ==> t[i] < n) &&
@ (\forall integer i; \result <= i < size ==> t[i] >= n) ;
@*/
public int index(int n) {
int a = 0, b = size, m;
/*@ loop_invariant
@ 0 <= a <= b <= size &&
@ (\forall integer i; 0 <= i < a ==> t[i] < n) &&
@ (\forall integer i; b <= i < size ==> t[i] >= n) ;
@ decreases b-a;
@*/
while (a<b) {
m = (a+b)/2;
if (t[m] < n) a=m+1; else b=m;
}
return a;
}
/*@ assigns \nothing;
@ ensures \result == true <==> In(n,my_model);
@*/
public boolean mem(int n) {
int i = index(n);
return (i < size && t[i] == n);
}
/* add with intermediate annotations for automatic
* proof with simplify
*/
/*@ assigns t,size, my_model; // t[..], syntax error ?
@ ensures my_model == union(\old(my_model),singleton(n));
@*/
public void add(int n) {
int i = index(n);
if (i < size && t[i] == n) return;
/* @ // assigns t; // ,t[*]; syntax error ?
ensures (\forall int j; 0 <= j && j < i; t[j]==\old(t[j]))
&& (\forall int j; i+1 <= j && j < size + 1; t[j]==\old(t[j-1]))
&& (\forall int j; i <= j && j < size; \old(t[j])==t[j+1])
&& t !=null && t[i]==n && size < t.length && t instanceof int[];
*/
{if (size < t.length) {
copy(t,t,i,size-1,i+1);
}
else {
int old[] = t;
t = new int[2*size+1];
copy(old,t,0,i-1,0);
copy(old,t,i,size-1,i+1);
}
t[i] = n;};
size++;
}
/*@ ensures \result == true;
@*/
public static boolean test() {
IntSetModelField s = new IntSetModelField();
s.add(1);
s.add(2);
//@ assert s.my_model == union(singleton(1),singleton(2));
return s.mem(1);
}
/*
copy(src,dest,a,b,c) copies src[a..b] to dest[c..c+b-a]
if src and dest are the same, c is assumed greater than or equal to a.
*/
/*@ requires src != null && dest != null &&
@ 0 <= a && a-1 <= b < src.length && 0 <= c &&
@ c+b-a < dest.length && (dest == src ==> c >= a);
@ assigns dest[c..c+b-a];
@ ensures \forall integer i; c <= i <= c+b-a ==> dest[i] == \old(src[i+a-c]);
@*/
public static void copy(int src[], int dest[],int a, int b, int c) {
/*@ loop_invariant
@ a-1 <= j <= b &&
@ \forall integer i; j < i <= b ==> dest[i+c-a] == \old(src[i]) &&
@ \forall integer i; c <= i <= j+c-a ==> dest[i] == \old(dest[i]);
@ decreases j;
@*/
for (int j = b; j >= a; j--) {
dest[j+c-a] = src[j];
}
}
}
|