summaryrefslogtreecommitdiff
path: root/Test/dafny0/Modules0.dfy
blob: b171a9c97c8681059bd012c1f4791dd6e7bec105 (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
// ---------------------- 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 imports N {  // fine, despite the fact that a class is called U,
                      // since module names are in their own name space
}

module UU imports N, U, N, N {  // duplicates are allowed
}

module N_left imports N { }
module N_right imports N { }
module Diamond imports N_left, N_right {  // this imports N.T twice, but that's okay
}

module A imports N, M {  // 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
  class X {
    var t: T;  // error: use of the ambiguous name T
    function F(x: T):  // error: use of the ambiguous name T
               T  // error: use of the ambiguous name T
    { x }
    method M(x: T)  // error: use of the ambiguous name T
      returns (y: T)  // error: use of the ambiguous name T
    { var g := new T; }  // error: use of the ambiguous name T
  }
}

module B0 imports A {
  class BadUse {
    var b0: T;  // error: T is not directly accessible
  }
}

module B1 imports A, N {
  class GoodUse {
    var b1: T;  // fine
  }
}

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

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

module X1 imports X0 {
  class MyClass1 {
    method Down(x0: MyClass0) {
      x0.Down();
    }
    method Up(x2: MyClass2) {  // error: class MyClass2 is not in scope
    }
  }
}

module X2 imports X0, X1, YY {
  class MyClass2 {
    method Down(x1: MyClass1, x0: MyClass0) {
      x1.Down(x0);
    }
    method WayDown(x0: MyClass0) {
      x0.Down();
    }
    method Up() {
    }
    method Somewhere(y: MyClassY) {
      y.M();
    }
  }
}

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

class ClassG {
  method T() { }
  function method TFunc(): int { 10 }
  method V(y: MyClassY) {  // Note, MyClassY is in scope, since we are in the _default
                           // module, which imports everything
    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) { }
}

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 imports ATr {
  class Y {
    method N() returns (x: X)
      ensures x != null;
    {
      x := new X;
    }
  }
}

module CTr imports BTr {
  class Z {
    var b: Y;  // fine
    var a: X;  // error: imports don't reach name name X explicitly
    method P() {
      var y := new 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 imports NonLocalA, 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() { }
  }
}