blob: 2c2fc52b269b6c14ecc111ed200fcde9e8239e69 (
plain)
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
|
module A {
class X { }
class T {
method M(x: int) returns (y: int)
requires 0 <= x;
ensures 0 <= y;
{
y := 2 * x;
}
method Q() returns (q: int, r: int, s: int)
ensures 0 <= q && 0 <= r && 0 <= s;
{ // error: failure to establish postcondition about q
r, s := 100, 200;
}
}
}
module B refines A {
class C { }
datatype Dt = Ax | Bx;
class T {
method P() returns (p: int)
{
p := 18;
}
method M(x: int) returns (y: int)
ensures y % 2 == 0; // add a postcondition
method Q ...
ensures 12 <= r;
ensures 1200 <= s; // error: postcondition is not established by
// inherited method body
}
}
// ------------------------------------------------
module A_AnonymousClass {
var x: int;
method Increment(d: int)
modifies this;
{
x := x + d;
}
}
module B_AnonymousClass refines A_AnonymousClass {
method Increment...
ensures x <= old(x) + d;
}
module C_AnonymousClass refines B_AnonymousClass {
method Increment(d: int)
ensures old(x) + d <= x;
method Main()
modifies this;
{
x := 25;
Increment(30);
assert x == 55;
Increment(12);
assert x == 66; // error: it's 67
}
}
// ------------------------------------------------
module BodyFree {
function F(x: int): int
ensures 0 <= F(x);
method TestF() {
assert F(6) == F(7); // error: no information about F so far
}
method M() returns (a: int, b: int)
ensures a == b;
}
module SomeBody refines BodyFree {
function F(x: int): int
{ if x < 0 then 2 else 3 }
method TestFAgain() {
assert F(6) == F(7);
}
method M() returns (a: int, b: int)
{
a := b; // good
}
}
module FullBodied refines BodyFree {
function F(x: int): int
{ x } // error: does not meet the inherited postcondition
method M() returns (a: int, b: int)
{ // error: does not establish postcondition
a := b + 1;
}
}
// ------------------------------------------------
module Abstract {
class MyNumber {
ghost var N: int;
ghost var Repr: set<object>;
predicate Valid
reads this, Repr;
{
this in Repr && null !in Repr
}
constructor Init()
modifies this;
ensures N == 0;
ensures Valid && fresh(Repr - {this});
{
N, Repr := 0, {this};
}
method Inc()
requires Valid;
modifies Repr;
ensures N == old(N) + 1;
ensures Valid && fresh(Repr - old(Repr));
{
N := N + 1;
}
method Get() returns (n: int)
requires Valid;
ensures n == N;
{
var k; assume k == N;
n := k;
}
}
}
module Concrete refines Abstract {
class MyNumber {
var a: int;
var b: int;
predicate Valid
{
N == a - b
}
constructor Init()
{
a := b;
}
method Inc()
{
if (*) { a := a + 1; } else { b := b - 1; }
}
method Get() returns (n: int)
{
var k := a - b;
assert ...;
}
}
}
module Client {
module C = Concrete;
class TheClient {
method Main() {
var n := new C.MyNumber.Init();
n.Inc();
n.Inc();
var k := n.Get();
assert k == 2;
}
}
}
module IncorrectConcrete refines Abstract {
class MyNumber {
var a: int;
var b: int;
predicate Valid
{
N == 2*a - b
}
constructor Init()
{ // error: postcondition violation
a := b;
}
method Inc()
{ // error: postcondition violation
if (*) { a := a + 1; } else { b := b - 1; }
}
method Get() returns (n: int)
{
var k := a - b;
assert ...; // error: assertion violation
}
}
}
|