summaryrefslogtreecommitdiff
path: root/Test/dafny0/AutoReq.dfy
blob: 684de262d36996e80abe6a60c37233f7661025e0 (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

function f(x:int) : bool
  requires x > 3;
{
  x > 7
}

// Should cause a function-requires failure
function g(y:int) : bool    
{
  f(y)
}

// Should succeed thanks to auto-generation based on f's requirements
function {:autoReq} h(y:int) : bool  
{
  f(y)
}

// Should fail based on h's inferred requirements
function h_tester() : bool
{
  h(2)
}

// Should succeed thanks to auto_reqs
function {:autoReq} i(y:int, b:bool) : bool
{
  if b then f(y + 2) else f(2*y)
}

method test() 
{
  // Function req violations (and assertion violations)
  if (*) {
    assert i(1, true);
  } else if (*) {
    assert i(0, false);
  }

  // Make sure function i's reqs can be satisfied
  if (*) {
    assert i(3, true);    // False assertion
  }
  assert i(7, false);     // True  assertion
}

module {:autoReq} TwoLayersOfRequires {
  function f(x:int) : bool
    requires x > 4;
  {
    x > 5
  }

  function g(y:int) : bool
    requires y < 50;
  {
    f(y)
  }

  function h(z:int) : bool
  {
    g(z)
  }
}

module {:autoReq} QuantifierTestsSimple {
  function byte(x:int) : bool
  {
    0 <= x < 256
  }

  function f_forall(s:seq<int>) : bool
  {
    forall i :: 0 <= i < |s| ==> byte(s[i])
  }

  function f_exists(s:seq<int>) : bool
  {
    exists i :: 0 <= i < |s| && byte(s[i])
  }

  function g_forall(s:seq<int>) : bool
    requires f_forall(s);
  {
    |s| > 2
  }

  function g_exists(s:seq<int>) : bool
    requires f_exists(s);
  {
    |s| > 2
  }

  function h(s:seq<int>) : bool
  {
    g_forall(s) && g_exists(s)
  }
}

module {:autoReq} QuantifierTestsHard {
  function byte(x:int) : bool
    requires 0 <= x < 256;
  { true }

  function f_forall(s:seq<int>) : bool
  {
    forall i :: 0 <= i < |s| ==> byte(s[i])
  }

  function f_exists(s:seq<int>) : bool
  {
    exists i :: 0 <= i < |s| && byte(s[i])
  }

  function g_forall(s:seq<int>) : bool
    requires forall j :: 0 <= j < |s| ==> byte(s[j]);
  {
    |s| > 2
  }

  function h_forall(s:seq<int>) : bool
  {
    f_forall(s)
  }

  function h(s:seq<int>) : bool
  {
    g_forall(s) 
  }

  function i(s:seq<int>) : bool
    requires forall k :: 0 <= k < |s| ==> 0 <= s[k] < 5;
  {
    true
  }

  function j(s:seq<int>) : bool
  {
    i(s)
  }

  function m(x:int) : bool
  function n(x:int) : bool
  
  function f1(x:int) : bool
	requires x > 3;
	requires x < 16;

  function variable_uniqueness_test(x:int) : bool
  {
    (forall y:int :: m(y))
	&&
	f1(x)
  }
}

module CorrectReqOrdering {
	function f1(x:int) : bool
		requires x > 3;

	function f2(b:bool) : bool
		requires b;

	// Should pass if done correctly.
	// However, if Dafny incorrectly put the requires for f2 first,
	// then the requires for f1 won't be satisfied
	function {:autoReq} f3(z:int) : bool
	{
		f2(f1(z))
	} 		

}

module ShortCircuiting {	
	function f1(x:int) : bool
		requires x > 3;

	function f2(y:int) : bool
		requires y < 10;

	function {:autoReq} test1(x':int, y':int) : bool
	{
		f1(x') && f2(y')
	}

	function {:autoReq} test2(x':int, y':int) : bool
	{
		f1(x') ==> f2(y')
	}

	function {:autoReq} test3(x':int, y':int) : bool
	{
		f1(x') || f2(y')
	}
}

module Lets {
	function f1(x:int) : bool
		requires x > 3;

	function {:autoReq} test1(x':int) : bool
	{
		var y' := 3*x'; f1(y')
	}
}

// Test nested module specification of :autoReq attribute
module {:autoReq} M1 {
    module M2 {
        function f(x:int) : bool
          requires x > 3;
        {
          x > 7
        }

        // Should succeed thanks to auto-generation based on f's requirements
        function {:autoReq} h(y:int) : bool  
        {
          f(y)
        }
    }
}

module Datatypes {
	datatype TheType = TheType_builder(x:int) | TheType_copier(t:TheType);

	function f1(t:TheType):bool
		requires t.TheType_builder? && t.x > 3;

	function {:autoReq} test(t:TheType) : bool
	{
		f1(t)
	}

	function f2(x:int) : bool
		requires forall t:TheType :: t.TheType_builder? && t.x > x;
	{
		true
	}

	// Should cause a function-requirement violation without autoReq
	function f3(y:int) : bool
	{
		f2(y)
	}

	function {:autoReq} test2(z:int) : bool
	{
		f2(z)
	}

}

module Matches {
	datatype TheType = TheType_builder(x:int) | TheType_copier(t:TheType);

	function f1(x:int):bool
		requires x > 3;

	function {:autoReq} basic_test(t:TheType) : bool
	{
		match t
			case TheType_builder(x) => f1(x)
			case TheType_copier(t) => true
	}
}

// Make sure :autoReq works with static functions
module StaticTest {
    static function f(x:int) : bool
      requires x > 3;
    {
      x > 7
    }

	static function {:autoReq} g(z:int) : bool
        requires f(z);
    {
        true
    }
	
    // Should succeed thanks to auto-generation based on f's requirements
    static function {:autoReq} h(y:int) : bool  
    {
      g(y)
    }
}