summaryrefslogtreecommitdiff
path: root/Test/dafny4/SoftwareFoundations-Basics.dfy
blob: 1d2878241f67378c9f3e53d8bd3a9a7a84103102 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

// Basics.v done in Dafny

// Enumerated Types

// Days of the Week

datatype day =
    monday
  | tuesday
  | wednesday
  | thursday
  | friday
  | saturday
  | sunday

// See method test_next_weekday() below for an explanation of why this function
// is declared a non-ghost (by "function method").
function method next_weekday (d:day) : day
{
  match d
  case monday    => tuesday
  case tuesday   => wednesday
  case wednesday => thursday
  case thursday  => friday
  case friday    => monday
  case saturday  => monday
  case sunday    => monday
}

method test_next_weekday()
{
  assert next_weekday(friday) == monday;
  assert next_weekday(next_weekday(saturday)) == tuesday;
  // Note, if you're not sure what some expression will compute, you
  // can assert that it equals some arbitrary value of the correct type.
  // For example:
  var d := next_weekday(next_weekday(tuesday));
  assert d == friday;   // error (use the Verification Debugger to view the correct value)
  // You will then get an error message (unless you were lucky and
  // happened to pick the right value).  If you're running the Dafny IDE
  // in Visual Studio, you can then click on the red circle that signals
  // the error.  This will bring up the Verification Debugger, where you
  // can inspect the value of d.  Then try changing the assertion in
  // the program to assert d to equal it.  In some cases, depending on
  // how many Dafny knows about the value you're trying to get it to
  // compute, you may need to build a disjunction of values for d in the
  // assertion.
  // Or, just write a program that prints the value, compile the program,
  // and run it -- Dafny is a programming language after all.  (Note, by
  // default, functions in Dafny of "ghost", which means they don't turn
  // into compiled code.  If you want to compile code for next_weekday, so
  // that you can use it in a print statement, change "function" to
  // "function method" in the declaration of next_weekday.)  To try this
  // out on rise4fun.com/dafny:  (0) comment out (or fix!) the assert above
  // so that the program will verify, (1) fill in a body of the uninterpreted
  // function f on line 396 below (any body will do; for example, uncomment
  // line 400), and (2) change the name of this method to Main().
  print next_weekday(wednesday);
}



// Booleans

// Booleans are built into Dafny as primitives.  Nevertheless, here is
// another definition, using an enumeration.

datatype Bool = True | False

function negb (b:Bool) : Bool
{
  match b
  case True => False
  case False => True
}

function andb (b1:Bool, b2:Bool) : Bool
{
  match b1
  case True => b2 
  case False => False
}

function orb (b1:Bool, b2:Bool) : Bool
{
  match b1
  case True => True
  case False => b2
}

method test_orb()
{
  assert orb(True, False) == True;
  assert orb(False, False) == False;
  assert orb(False, True) == True;
  assert orb(True, True) == True;
}

// Exercise: 1 star (nandb)

function nandb (b1:Bool, b2:Bool) : Bool
{
  negb(andb(b1, b2))
}

method test_nandb()
{
  assert nandb(True, False) == True;
  assert nandb(False, False) == True;
  assert nandb(False, True) == True;
  assert nandb(True, True) == False;
}

// Exercise: 1 star (andb3)

function andb3 (b1:Bool, b2:Bool, b3:Bool) : Bool
{
  andb(andb(b1, b2), b3)
}

method test_andb3()
{
  assert andb3(True, True, True) == True;
  assert andb3(False, True, True) == False;
  assert andb3(True, False, True) == False;
  assert andb3(True, True, False) == False;
}

// Function Types

// Numbers

datatype Nat =
    O
  | S(Nat)

function pred (n : Nat) : Nat
{
  match n
  case O => O
  case S(n') => n'
}

function minustwo (n : Nat) : Nat
{
  match n
  case O => O
  case S(nn) =>
    // Note, Dafny currently does not support nested patterns in match expressions, but
    // this will change soon.
    match nn
    case O => O
    case S(n') => n'
}

function evenb (n:Nat) : Bool
{
  match n
  case O       => True
  case S(nn) =>
    match nn
    case O     => False
    case S(n') => evenb(n')
}

function oddb (n:Nat) : Bool
{
  negb(evenb(n))
}

method test_oddb()
{
  assert oddb(S(O)) == True;
  assert oddb(S(S(S(S(O))))) == False;
}

function plus (n : Nat, m : Nat) : Nat
{
  match n
  case O => m
  case S(n') => S(plus(n', m))
}

method test_plus()
{
  assert plus(S(S(S(O))), S(S(O))) == S(S(S(S(S(O)))));
}

function mult (n: Nat, m: Nat) : Nat
{
  match n
  case O => O
  case S(n') => plus(m, mult(n', m))
}

method test_mult()
{
  var n3 := S(S(S(O)));
  var n9 := S(S(S(S(S(S(S(S(S(O)))))))));
  assert mult(n3, n3) == n9;
}

function minus (n: Nat, m: Nat) : Nat
{
  match n
  case O => O
  case S(n') =>
    match m
    case O => n
    case S(m') => minus(n', m')
}

function exp (base: Nat, power: Nat) : Nat
{
  match power
  case O => S(O)
  case S(p) => mult(base, exp(base, p))
}

// Exercise: 1 star (factorial)

function factorial(n: Nat): Nat
{
  match n
  case O => S(O)
  case S(n') => mult(n, factorial(n'))
}

method test_factorial1()
{
  var n2 := S(S(O));
  var n3 := S(n2);
  var n5 := S(S(n3));
  var n6 := S(n5);
  assert factorial(n3) == n6;

  var n10 := S(S(S(S(n6))));
  var n12 := S(S(n10));

  assert factorial(n5)==mult(n10, n12);
}

method test_factorial1_old()
{
  var n2 := S(S(O));
  var n3 := S(n2);
  var n5 := S(S(n3));
  var n6 := S(n5);
  assert factorial(n3) == n6;

  var n10 := S(S(S(S(n6))));
  var n12 := S(S(n10));

  // proving that 5! == 10*12 _used to_ take some effort...
  calc {
    factorial(n5);
    { mult_lemma(n2, n6); }
    mult(n5, plus(plus(n6, n6), plus(plus(n6, n6), O)));
    { mult_lemma(n5, plus(n6, n6)); }
    mult(n10, n12);
  }
}

// This lemma expresses:  m*(2*n) == (2*m)*n
lemma mult_lemma(m: Nat, n: Nat)
  ensures mult(m, plus(n, n)) == mult(plus(m, m), n);
{
  match m {
    case O =>
    case S(m') =>
      calc {
        mult(m, plus(n, n));
        plus(plus(n, n), mult(m', plus(n, n)));
        // induction hypothesis
        plus(plus(n, n), mult(plus(m', m'), n));
        { assert forall a,b,c :: plus(plus(a, b), c) == plus(a, plus(b, c)); }
        plus(n, plus(n, mult(plus(m', m'), n)));
        mult(S(S(plus(m', m'))), n);
        mult(S(plus(S(m'), m')), n);
        { assert forall a,b :: S(plus(a, b)) == plus(a, S(b)); }
        mult(plus(S(m'), S(m')), n);
      }
  }
}

function beq_nat (n: Nat, m: Nat) : Bool
{
  match n
  case O => (
         match m
         case O => True
         case S(m') => False
         )
  case S(n') =>
         match m
         case O => False
         case S(m') => beq_nat(n', m')
}

function ble_nat (n: Nat, m: Nat) : Bool
{
  match n
  case O => True
  case S(n') =>
      match m
      case O => False
      case S(m') => ble_nat(n', m')
}

method test_ble_nat1()
{
  var n2 := S(S(O));
  var n4 := S(S(n2));
  assert ble_nat(n2, n2) == True;
  assert ble_nat(n2, n4) == True;
  assert ble_nat(n4, n2) == False;
}

// Exercise: 2 stars (blt_nat)

function blt_nat (n: Nat, m: Nat) : Bool
{
  andb(ble_nat(n, m), negb(beq_nat(n, m)))
}

method test_blt_nat1()
{
  var n2 := S(S(O));
  var n4 := S(S(n2));
  assert blt_nat(n2, n2) == False;
  assert blt_nat(n2, n4) == True;
  assert blt_nat(n4, n2) == False;
}

// Proof by Simplification

lemma plus_O_n (n: Nat)
  ensures plus(O, n) == n;
{
}

lemma plus_1_l (n: Nat)
  ensures plus(S(O), n) == S(n);
{
}

lemma mult_0_l (n: Nat)
  ensures mult(O, n) == O;
{
}

// Proof by Rewriting

lemma plus_id_example (n: Nat, m: Nat)
  ensures n == m ==> plus(n, n) == plus(m, m);
{
}

// Exercise: 1 star (plus_id_exercise)

lemma plus_id_exercise (n: Nat, m: Nat, o: Nat)
  ensures n == m ==> m == o ==> plus(n, m) == plus(m, o);
{
}

lemma mult_0_plus (n: Nat, m: Nat)
  ensures mult(plus(O, n), m) == mult(n, m);
{
}

// Exercise: 2 stars (mult_S_1)

lemma mult_S_1 (n: Nat, m: Nat)
  ensures m == S(n) ==> mult(m, plus(S(O), n)) == mult(m, m);
{
}


// Proof by Case Analysis

lemma plus_1_neq_0_firsttry (n: Nat)
  ensures beq_nat(plus(n, S(O)), O) == False;
{
}

lemma plus_1_neq_0 (n: Nat)
  ensures beq_nat(plus(n, S(O)), O) == False;
{
}

lemma negb_involutive (b: Bool)
  ensures negb(negb(b)) == b;
{
}

// Exercise: 1 star (zero_nbeq_plus_1)

lemma zero_nbeq_plus_1 (n: Nat)
  ensures beq_nat(O, plus(n, S(O))) == False;
{
}

// More Exercises

// Exercise: 2 stars (boolean functions)

// Since Dafny currently does not allow functions as parameters, we instead declare
// a global function f for use in the following lemmas.  Since f is given no body,
// it will be treated as an uninterpreted function.
function f(x: Bool): Bool
// Note:  If you want to compile and run the program, as suggested above in method
// test_next_weekday, then you must supply some body for f.  Here is one way to do
// that:
//      { x }

lemma identity_fn_applied_twice(b: Bool)
  requires forall x :: f(x) == x;
  ensures f(f(b)) == b;
{
}
  
    
lemma negation_fn_applied_twice(b: Bool)
  requires forall x :: f(x) == negb(x);
  ensures f(f(b)) == b;
{
}

// Exercise: 2 stars (andb_eq_orb)

lemma andb_true (b : Bool)
  ensures andb(True, b) == b;
{
}

lemma orb_false (b : Bool)
  ensures orb(False, b) == b;
{
}

lemma andb_eq_orb (b: Bool, c: Bool)
  ensures andb(b, c) == orb(b, c) ==> b == c;
{
}

// Exercise: 3 stars (binary)

datatype bin = Zero | Twice(bin) | TwicePlusOne(bin)

function increment(b: bin): bin
{
  match b
  case Zero => TwicePlusOne(Zero)
  case Twice(b') => TwicePlusOne(b')
  case TwicePlusOne(b') => Twice(increment(b'))
}

function BinToUnary(b: bin): Nat
{
  match b
  case Zero => O
  case Twice(b') =>
    var t := BinToUnary(b');
    plus(t, t)
  case TwicePlusOne(b') =>
    var t := BinToUnary(b');
    plus(t, plus(t, S(O)))
}

method test_bin()
{
  var n6 := S(S(S(S(S(S(O))))));
  var n13 := S(S(S(S(S(S(S(n6)))))));
  assert BinToUnary(Twice(TwicePlusOne(TwicePlusOne(Zero)))) == n6;
  assert BinToUnary(TwicePlusOne(Twice(TwicePlusOne(TwicePlusOne(Zero))))) == n13;
}

method test_increment()
{
  var b13 := TwicePlusOne(Twice(TwicePlusOne(TwicePlusOne(Zero))));
  var n13 := S(S(S(S(S(S(S(S(S(S(S(S(S(O)))))))))))));
  var n14 := S(n13);

  assert increment(Twice(TwicePlusOne(TwicePlusOne(Zero)))) == TwicePlusOne(TwicePlusOne(TwicePlusOne(Zero)));
  assert increment(b13) == Twice(TwicePlusOne(TwicePlusOne(TwicePlusOne(Zero))));

  assert BinToUnary(increment(b13)) == plus(BinToUnary(b13), S(O));
}

// Optional Material

// [Fixpoint]s and Structural Recursion

function plus' (n : Nat, m : Nat) : Nat
{
  match n
  case O => m
  case S(n') => S(plus'(n', m))
}

// Exercise: 2 stars, optional (decreasing)

function decreasingOnTwo (n: Nat, m: Nat, p: Nat) : Nat
{
  match p
  case O => (
      match n
      case O     => O
      case S(n') => decreasingOnTwo(n', m, S(O))
      )
  case S(_) =>
      match m
      case O     => S(O)
      case S(m') => decreasingOnTwo(n, m', O)
}