summaryrefslogtreecommitdiff
path: root/Test/dafny0/CoResolution.dfy
blob: ff4c3dbf7a641fa1ef47b4f4b0da37670b75417d (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
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

module TestModule {
  class TestClass {
    copredicate P(b: bool)
    {
      !b && Q(null)
    }

    copredicate Q(a: array<int>)
    {
      a == null && P(true)
    }

    copredicate S(d: set<int>)
    {
      this.Undeclared#[5](d) &&  // error: 'Undeclared#' is undeclared
      Undeclared#[5](d) &&  // error: 'Undeclared#' is undeclared
      this.S#[5](d) &&
      S#[5](d) &&
      S#[_k](d)  // error: _k is not an identifier in scope
    }

    colemma CM(d: set<int>)
    {
      var b;
      b := this.S#[5](d);
      b := S#[5](d);
      this.CM#[5](d);
      CM#[5](d);
    }
  }
}

module GhostCheck0 {
  codatatype Stream<G> = Cons(head: G, tail: Stream)
  method UseStream0(s: Stream)
  {
    var x := 3;
    if (s == s.tail) {  // error: this operation is allowed only in ghost contexts
      x := x + 2;
    }
  }
}
module GhostCheck1 {
  codatatype Stream<G> = Cons(head: G, tail: Stream)
  method UseStream1(s: Stream)
  {
    var x := 3;
    if (s ==#[20] s.tail) {  // this seems innocent enough, but it's currently not supported by the compiler, so...
      x := x + 7;  // error: therefore, this is an error
    }
  }
}
module GhostCheck2 {
  codatatype Stream<G> = Cons(head: G, tail: Stream)
  ghost method UseStreamGhost(s: Stream)
  {
    var x := 3;
    if (s == s.tail) {  // fine
      x := x + 2;
    }
  }
}

module Mojul0 {
  class MyClass {
    copredicate D()
      reads this;  // error: copredicates are not allowed to have a reads clause -- WHY NOT?
    {
      true
    }

    copredicate NoEnsuresPlease(m: nat)
      ensures NoEnsuresPlease(m) ==> m < 100;  // error: a copredicate is not allowed to have an 'ensures' clause
    {
      m < 75
    }

    // Note, 'decreases' clauses are also disallowed on copredicates, but the parser takes care of that
  }
}

module Mojul1 {
  copredicate A() { B() }  // error: SCC of a copredicate must include only copredicates
  predicate B() { A() }

  copredicate X() { Y() }
  copredicate Y() { X#[10]() }  // error: X is not allowed to depend on X#

  colemma M()
  {
    N();
  }
  colemma N()
  {
    Z();
    W();  // error: not allowed to make co-recursive call to non-colemma
  }
  ghost method Z() { }
  ghost method W() { M(); }

  colemma G() { H(); }
  colemma H() { G#[10](); }  // fine for colemma/prefix-lemma
}

module CallGraph {
  // colemma -> copredicate -> colemma
  // colemma -> copredicate -> prefix lemma
  colemma CoLemma(n: nat)
  {
    var q := Q(n);  // error
    var r := R(n);  // error
  }

  copredicate Q(n: nat)
  {
    calc { 87; { CoLemma(n); } }  // error: this recursive call not allowed
    false
  }

  copredicate R(n: nat)
  {
    calc { 87; { CoLemma#[n](n); } }  // error: this recursive call not allowed
    false
  }

  // colemma -> prefix predicate -> colemma
  // colemma -> prefix predicate -> prefix lemma
  colemma CoLemma_D(n: nat)
  {
    var q := Q_D#[n](n);  // error
    var r := R_D#[n](n);  // error
  }

  copredicate Q_D(n: nat)
  {
    calc { 88; { CoLemma_D(n); } }  // error: this recursive call not allowed
    false
  }

  copredicate R_D(n: nat)
  {
    calc { 89; { CoLemma_D#[n](n); } }  // error: this recursive call not allowed
    false
  }

  // copredicate -> function -> copredicate
  // copredicate -> function -> prefix predicate
  copredicate P(n: nat)
  {
    G0(n)  // error
    <
    G1(n)  // error
  }

  function G0(n: nat): int
  {
    calc { true; { assert P(n); } }
    100
  }
  function G1(n: nat): int
  {
    calc { true; { assert P#[n](n); } }
    101
  }

  colemma J()
  {
    var f := JF();  // error: cannot call non-colemma recursively from colemma
  }
  function JF(): int
  {
    J();
    5
  }
}

module CrashRegression {
  codatatype Stream = Cons(int, Stream)

  // The following functions (where A ends up being the representative in the
  // SCC and B, which is also in the same SCC, has no body) once crashed the
  // resolver.
  function A(): Stream
  {
    B()
  }
  function B(): Stream
    ensures A() == S();

  function S(): Stream
}

module AmbiguousTypeParameters {
  codatatype Stream<T> = Cons(T, Stream)

  function A(): Stream
  {
    B()
  }

  // Here, the type arguments to A and S cannot be resolved
  function B(): Stream
    ensures A() == S();

  function S(): Stream
}