summaryrefslogtreecommitdiff
path: root/Test/dafny0/Modules0.dfy
blob: 4b86d84802605171bbcf5e4f1ffc1d875190ec6e (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

// ---------------------- duplicate types within a module

module Wazzup {
  class WazzupA { }
  class WazzupA { }  // error: duplicate type
  datatype WazzupA = W_A_X  // error: duplicate type
  type WazzupA  // error: duplicate type

  type WazzupB
  type WazzupB  // error: duplicate type
  class WazzupB { }  // error: duplicate type
  datatype WazzupB = W_B_X  // error: duplicate type
}

// ---------------------- duplicate types across modules

module M {
  class T { }
  class U { }
}

module N {
  class T { }
}

module U {
  import NN = N
}


module A {  // Note, this has the effect of importing two different T's,
            // but that's okay as long as the module doesn't try to access
            // one of them
  import MM = M
  import NN = N
  class X {
    var t: MM.T;  // error: use of the ambiguous name T
    function F(x: MM.T):  // error: use of the ambiguous name T
               MM.T  // error: use of the ambiguous name T
    { x }
    method M(x: NN.T)  // error: use of the ambiguous name T
      returns (y: NN.T)  // error: use of the ambiguous name T
  }
}


// --------------- calls

module X0 {
  class MyClass0 {
    method Down() {
    }
    method Up<S>(x1: MyClass1,  // error: MyClass1 is not in scope
                 x2: MyClass2) {  // error: MyClass2 is not in scope
    }
  }
}

module X1 {
  import X0' = X0
  class MyClass1 {
    method Down(x0: X0'.MyClass0) {
      x0.Down();
    }
    method Up<T>(x2: MyClass2) {  // error: class MyClass2 is not in scope
    }
  }
}

module X2 {
  import opened X1
  class MyClass2 {
    method Down(x1: MyClass1, x0: X0'.MyClass0) {
      x1.Down(x0);
    }
    method WayDown(x0: X0'.MyClass0) {
      x0.Down();
    }
    method Up() {
    }
    method Somewhere(y: MyClassY) {  // error: no such type in scope
      y.M();
    }
  }
}

module YY {
  class MyClassY {
    method M() { }
    method P<R>(g: ClassG) {  // error: ClassG is not in scope
    }
  }
}

class ClassG {
  method T() { }
  function method TFunc(): int { 10 }
  method V(y: MyClassY) {
    y.M();
  }
}

method Ping() {
  Pong();  // allowed: intra-module call
}

method Pong() {
  Ping();  // allowed: intra-module call
}

method ProcG(g: ClassG) {
  g.T();  // allowed: intra-module call
  var t := g.TFunc();  // allowed: intra-module call
}

// ---------------------- some ghost stuff ------------------------

class Ghosty {
  method Caller() {
    var x := 3;
    ghost var y := 3;
    Callee(x, y);  // fine
    Callee(x, x);  // fine
    Callee(y, x);  // error: cannot pass in ghost to a physical formal
    Theorem(x);  // fine
    Theorem(y);  // fine, because it's a ghost method
  }
  method Callee(a: int, ghost b: int) { }
  ghost method Theorem(a: int) { }
}

class AClassWithSomeField {
  var SomeField: int;

  method SpecialFunctions()
    modifies this;
  {
    SomeField := SomeField + 4;
    var a := old(SomeField);  // error: old can only be used in ghost contexts
    var b := fresh(this);  // error: fresh can only be used in ghost contexts
//    var c := allocated(this);  // error: allocated can only be used in ghost contexts
    if (fresh(this)) {  // this guard makes the if statement a ghost statement
      ghost var x := old(SomeField);  // this is a ghost context, so it's okay
//      ghost var y := allocated(this);  // this is a ghost context, so it's okay
    }
  }
}

// ---------------------- illegal match expressions ---------------

datatype Tree = Nil | Cons(int, Tree, Tree)

function NestedMatch0(tree: Tree): int
{
  match tree
  case Nil => 0
  case Cons(h,l,r) =>
    match tree  // error: cannot match on "tree" again
    case Nil => 0
    case Cons(hh,ll,rr) => hh
}

function NestedMatch1(tree: Tree): int
{
  match tree
  case Nil => 0
  case Cons(h,l,r) =>
    match l
    case Nil => 0
    case Cons(h0,l0,r0) =>
      match r
      case Nil => 0
      case Cons(h1,l1,r1) => h + h0 + h1
}

function NestedMatch2(tree: Tree): int
{
  match tree
  case Nil => 0
  case Cons(h,l,r) =>
    match l
    case Nil => 0
    case Cons(h,l0,tree) =>  // fine to declare another "h" and "tree" here
      match r
      case Nil => 0
      case Cons(h1,l1,r1) => h + h1
}

function NestedMatch3(tree: Tree): int
{
  match tree
  case Nil => 0
  case Cons(h,l,r) =>
    match l
    case Nil => 0
    case Cons(h0,l0,r0) =>
      match l  // error: cannot match on "l" again
      case Nil => 0
      case Cons(h1,l1,r1) => h + h0 + h1
}

// ---------------------- direct imports are not transitive

module ATr {
  class X {
    method M() returns (q: int)
    {
      q := 16;
    }
    static method Q() returns (q: int)
    {
      q := 18;
    }
  }
}

module BTr {
  import A = ATr
  class Y {
    method N() returns (x: A.X)
      ensures x != null;
    {
      x := new X;
    }
  }
}

module CTr {
  import B = BTr
  class Z {
    var b: B.Y;  // fine
    var a: B.X;  // error: imports don't reach name X explicitly
  }
}
module CTs {
  import B = BTr
  method P() {
    var y := new B.Y;
    var x := y.N();  // this is allowed and will correctly infer the type of x to
                     // be X, but X could not have been mentioned explicitly
    var q := x.M();
    var r := X.Q();  // error: X is not in scope
    var s := x.DoesNotExist();  // error: method not declared in class X
  }
}

// ---------------------- module-local declarations override imported declarations

module NonLocalA {
  class A {
    method M() { }
  }
  class Common {
    method P() { }
  }
}

module NonLocalB {
  class B {
    method N() { }
  }
  class D {
    method K() returns (b: B)
      ensures b != null;
    {
      return new B;
    }
  }
  class Common {
    method P() { }
  }
}

module Local {
  import AA = NonLocalA
  import BB = NonLocalB
  class MyClass {
    method MyMethod()
    {
      var b := new B;
      var c := new Common;
      var d := new D;
      c.Q();  // this is fine, since c's type is the local class Common
      b.R();  // fine, since B refers to the locally declared class
      var nonLocalB := d.K();
      nonLocalB.N();
      nonLocalB.R();  // error: this is not the local type B
    }
  }
  class B {
    method R() { }
  }
  class Common {
    method Q() { }
  }
}

// ------ qualified type names ----------------------------------

module Q_Imp {
  class Node { }
  datatype List<T> = Nil | Cons(T, List)
  class Klassy {
    method Init()
  }
}

module Q_M {
  import Q_Imp
  method MyMethod(root: Q_Imp.Node, S: set<Node>)
    requires root in S;  // error: the element type of S does not agree with the type of root
  {
    var i := new Q_Imp.Node;
    var j := new Node;
    assert i != j;  // error: i and j have different types
    var k: LongLostModule.Node;  // error: undeclared module
    var l: Wazzup.WazzupA;  // error: undeclared module (it has not been imported)
    var m: Q_Imp.Edon;  // error: undeclared class in module Q_Imp
    var n: Q_Imp.List;
    var o := new Q_Imp.List;  // error: not a class declared in module Q_Imp
    var p := new Q_Imp.Klassy.Create();  // error: Create is not a method
    var q := new Q_Imp.Klassy.Init();
  }
  class Node { }
}

// ------- top-level statics -----------------

module TopLevelStatics {
  static function F(): int  // error/warning: static keyword does not belong here
  { 0 }
  static method M()  // error/warning: static keyword does not belong here
  { }
}

module Library {
  class T { }
}

module AA {
 import opened Library
}

module B refines AA {
  datatype T = MakeT(int)  // illegal
}