summaryrefslogtreecommitdiff
path: root/Test/dafny0/TypeParameters.dfy
blob: 705961a094baca8fb1bd4aa55e355a45f7f8a662 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
class C<U(==)> {
  method M<T>(x: T, u: U) returns (y: T)
    ensures x == y && u == u;
  {
    y := x;
  }

  function method F<X(==)>(x: X, u: U): bool
  {
    x == x && u == u
  }

  method Main(u: U)
  {
    var t := F(3,u) && F(this,u);
    var kz := M(t,u);
	var a := G();
    assert kz && (a || !a);
  }
  method G<Y>() returns (a: Y)
  {
    
  }
}

class SetTest {
  method Add<T>(s: set<T>, x: T) returns (t: set<T>)
    ensures t == s + {x};
  {
    t := s + {x};
  }

  method Good()
  {
    var s := {2, 5, 3};
    var t := Add(s, 7);
    assert {5,7,2,3} == t;
  }

  method Bad()
  {
    var s := {2, 50, 3};
    var t := Add(s, 7);
    assert {5,7,2,3} == t;  // error
  }
}

class SequenceTest {
  method Add<T>(s: seq<T>, x: T) returns (t: seq<T>)
    ensures t == s + [x];
  {
    t := s + [x];
  }

  method Good()
  {
    var s := [2, 5, 3];
    var t := Add(s, 7);
    assert [2,5,3,7] == t;
  }

  method Bad()
  {
    var s := [2, 5, 3];
    var t := Add(s, 7);
    assert [2,5,7,3] == t || [2,5,3] == t;  // error
  }
}

// -------------------------

class CC<T> {
  var x: T;
  method M(c: CC<T>, z: T) returns (y: T)
    requires c != null;
    modifies this;
    ensures y == c.x && x == z;
  {
    x := c.x;
    x := z;
    y := c.x;
  }
}

class CClient {
  method Main() {
    var c := new CC<int>;
    var k := c.x + 3;
    if (c.x == c.x) {
      k := k + 1;
    }
    var m := c.x;
    if (m == c.x) {
      k := k + 1;
    }
    c.x := 5;
    c.x := c.x;
    var z := c.M(c, 17);
    assert z == c.x;
  }
}

// -------------------------

static function IsCelebrity<Person>(c: Person, people: set<Person>): bool
  requires c == c || c in people;
{
  false
}

method FindCelebrity3(people: set<int>, ghost c: int)
  requires IsCelebrity(c, people);  // once upon a time, this caused the translator to produce bad Boogie
{
  ghost var b: bool;
  b := IsCelebrity(c, people);
  b := F(c, people);
}

static function F(c: int, people: set<int>): bool
  requires IsCelebrity(c, people);
{
  false
}
function RogerThat<G>(g: G): G
{
  g
}

function Cool(b: bool): bool
{
  b
}

function Rockin'<G>(g: G): G
{
  var h := g;
  h
}

function Groovy<G>(g: G, x: int): G
{
  var h := g;
  if x == 80 then
    RogerThat(h)
  else
    [h][0]
}

method IsRogerCool(n: int)
  requires RogerThat(true);  // once upon a time, this caused the translator to produce bad Boogie
{
  if (*) {
    assert Cool(2 < 3 && n < n && n < n+1);  // the error message here will peek into the argument of Cool
  } else if (*) {
    assert RogerThat(2 < 3 && n < n && n < n+1);  // same here; cool, huh?
  } else if (*) {
    assert Rockin'(false);  // error
  } else if (*) {
    assert Groovy(n < n, 80);  // error
  } else if (*) {
    assert Groovy(n + 1 <= n, 81);  // error
  }
}

method LoopyRoger(n: int)
{
  var i := 0;
  while (i < n)
    invariant RogerThat(0 <= n ==> i <= n);
  {
    i := i + 1;
  }
  i := 0;
  while (i < n)
    invariant RogerThat(0 <= n ==> i <= n);  // error: failure to maintain loop invariant
  {
    i := i + 2;
  }
}

// ----------------------

class TyKn_C<T> {
  var x: T;
  function G(): T
    reads this;
  {
     x
  }
  method M() returns (t: T)
  {

  }
}

class TyKn_K {
  function F(): int { 176 }
}

method TyKn_Main(k0: TyKn_K) {
  var c := new TyKn_C<TyKn_K>;
  var k1: TyKn_K;

  assert k0 != null ==> k0.F() == 176;
  assert k1 != null ==> k1.F() == 176;

  assert c.x != null ==> c.x.F() == 176;  // the Dafny encoding needs the canCall mechanism to verify this
  assert c.G() != null ==> c.G().F() == 176;  // ditto
  var k2 := c.M();
  assert k2 != null ==> k2.F() == 176;  // the canCall mechanism does the trick here, but so does the encoding
                                        // via k2's where clause
}

// ------------------- there was once a bug in the handling of the following example

module OneLayer
{
  datatype wrap<V> = Wrap(V);
}

module TwoLayers
{
  import OneLayer;
  datatype wrap2<T> = Wrap2(get: OneLayer.wrap<T>);
  
  function F<U>(w: wrap2<U>) : OneLayer.wrap<U>
  {
    match w
    case Wrap2(a) => a
  }
  function G<U>(w: wrap2<U>) : OneLayer.wrap<U>
  {
    match w
    case Wrap2(a) => w.get
  }
  function H<U>(w: wrap2<U>) : OneLayer.wrap<U>
  {
    w.get
  }
}

// ---------------------------------------------------------------------

datatype List<T> = Nil | Cons(T, List)
predicate InList<T>(x: T, xs: List<T>)
predicate Subset(xs: List, ys: List) 
{
  forall x :: InList(x, xs) ==> InList(x, ys)
}
ghost method ListLemma_T(xs: List, ys: List)
  requires forall x :: InList(x, xs) ==> InList(x, ys);
{
  assert Subset(xs, ys);
}
ghost method ammeLtsiL_T(xs: List, ys: List)
  requires Subset(xs, ys);
{
  assert forall x :: InList(x, xs) ==> InList(x, ys);
}
ghost method ListLemma_int(xs: List<int>, ys: List<int>)
  requires forall x :: InList(x, xs) ==> InList(x, ys);
{
  assert Subset(xs, ys);
}
ghost method ammeLtsiL_int(xs: List<int>, ys: List<int>)
  requires Subset(xs, ys);
{
  assert forall x :: InList(x, xs) ==> InList(x, ys);
}