blob: 1610d8b8a07f8d9646a73ff1af405aa29b10f485 (
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
|
module A {
class C {
var x: int;
predicate P()
reads this;
{
x < 100
}
method M()
modifies this;
ensures P();
{
x := 28;
}
method N()
modifies this;
ensures P();
{ // error: in module B, the postcondition P() has been strengthened and no longer holds
x := -28;
}
}
}
module B refines A {
class C {
predicate P()
{
0 <= x
}
}
}
// ------------------------------------------------
module Loose {
class MyNumber {
var N: int;
ghost var Repr: set<object>;
predicate Valid()
reads this, Repr;
{
this in Repr && null !in Repr &&
0 <= N
}
constructor Init()
modifies this;
ensures Valid() && fresh(Repr - {this});
{
N, Repr := 0, {this};
}
method Inc()
requires Valid();
modifies Repr;
ensures old(N) < N;
ensures Valid() && fresh(Repr - old(Repr));
{
N := N + 2;
}
method Get() returns (n: int)
requires Valid();
ensures n == N;
{
n := N;
}
}
}
module Tight refines Loose {
class MyNumber {
predicate Valid()
{
N % 2 == 0
}
constructor Init()
ensures N == 0;
method Inc()
ensures N == old(N) + 2;
}
}
module UnawareClient {
import L = Loose;
method Main0() {
var n := new L.MyNumber.Init();
assert n.N == 0; // error: this is not known
n.Inc();
n.Inc();
var k := n.Get();
assert k == 4; // error: this is not known
}
}
module AwareClient {
import T = Tight;
method Main1() {
var n := new T.MyNumber.Init();
assert n.N == 0;
n.Inc();
n.Inc();
var k := n.Get();
assert k == 4;
}
}
// -------- Tricky refinement inheritance ----------------------------------------
module Tricky_Base {
class Tree {
var x: int;
predicate Constrained()
reads this;
{
x < 10
}
predicate Valid()
reads this;
{
x < 100
}
method Init()
modifies this;
ensures Valid();
{ // error: in module Tricky_Full, the strengthened postcondition Valid() no longer holds
x := 20;
}
}
}
module Tricky_Full refines Tricky_Base {
class Tree {
predicate Valid()
{
Constrained // this causes an error to be generated for the inherited Init
}
}
}
// -------- Quantifiers ----------------------------------------
module Q0 {
class C {
var x: int;
predicate P()
reads this;
{
true
}
method M()
modifies this;
ensures forall c: C :: c != null ==> c.P();
{ // error: in module Q1, the postcondition no longer holds
}
predicate Q()
reads this;
{
x < 100
}
method N()
modifies this;
ensures forall c :: c == this ==> c.Q();
{ // error: fails to establish postcondition (but this error should not be repeated in Q1 below)
x := 102;
}
predicate R() reads this; // a body-less predicate
}
}
module Q1 refines Q0 {
class C {
predicate P()
{
x == 18
}
predicate R() // no body yet
}
}
module Q2 refines Q1 {
class C {
predicate R() { x % 3 == 2 } // finally, give it a body
}
}
|