File size: 10,239 Bytes
6851d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// RUN: %verify --disable-nonlinear-arithmetic "%s"

/*******************************************************************************
 *  Original: Copyright (c) Microsoft Corporation
 *  SPDX-License-Identifier: MIT
 *  
 *  Modifications and Extensions: Copyright by the contributors to the Dafny Project
 *  SPDX-License-Identifier: MIT 
 *******************************************************************************/

/* lemmas and functions in this file are used in the proofs in DivMod.dfy

Specs/implements mathematical div and mod, not the C version.
(x div n) * n + (x mod n) == x, where 0 <= x mod n < n.
https://en.wikipedia.org/wiki/Modulo_operation

This may produce "surprising" results for negative values.
For example, -3 div 5 is -1 and -3 mod 5 is 2.
Note this is consistent: -3 * -1 + 2 == 5 */

include "GeneralInternals.dfy"
include "MulInternals.dfy"
include "../Mul.dfy"
include "ModInternalsNonlinear.dfy"
include "DivInternalsNonlinear.dfy"

module {:options "-functionSyntax:4"} ModInternals {

  import opened GeneralInternals
  import opened Mul
  import opened MulInternalsNonlinear
  import opened MulInternals
  import opened ModInternalsNonlinear
  import opened DivInternalsNonlinear

  /* Performs modulus recursively. */
  function {:opaque} ModRecursive(x: int, d: int): int
    requires d > 0
    decreases if x < 0 then (d - x) else x
  {
    if x < 0 then
      ModRecursive(d + x, d)
    else if x < d then
      x
    else
      ModRecursive(x - d, d)
  }

  /* performs induction on modulus */
  lemma LemmaModInductionForall(n: int, f: int -> bool)
    requires n > 0
    requires forall i :: 0 <= i < n ==> f(i)
    requires forall i {:trigger f(i), f(i + n)} :: i >= 0 && f(i) ==> f(i + n)
    requires forall i {:trigger f(i), f(i - n)} :: i < n  && f(i) ==> f(i - n)
    ensures  forall i :: f(i)
  {
    forall i ensures f(i) { LemmaInductionHelper(n, f, i); }
  }

  /* given an integer x and divisor n, the remainder of x%n is equivalent to the remainder of (x+m)%n
  where m is a multiple of n */
  lemma LemmaModInductionForall2(n: int, f:(int, int)->bool)
    requires n > 0
    requires forall i, j :: 0 <= i < n && 0 <= j < n ==> f(i, j)
    requires forall i, j {:trigger f(i, j), f(i + n, j)} :: i >= 0 && f(i, j) ==> f(i + n, j)
    requires forall i, j {:trigger f(i, j), f(i, j + n)} :: j >= 0 && f(i, j) ==> f(i, j + n)
    requires forall i, j {:trigger f(i, j), f(i - n, j)} :: i < n  && f(i, j) ==> f(i - n, j)
    requires forall i, j {:trigger f(i, j), f(i, j - n)} :: j < n  && f(i, j) ==> f(i, j - n)
    ensures  forall i, j :: f(i, j)
  {
    forall x, y
      ensures f(x, y)
    {
      forall i | 0 <= i < n
        ensures f(i, y)
      {
        var fj := j => f(i, j);
        LemmaModInductionForall(n, fj);
        assert fj(y);
      }
      var fi := i => f(i, y);
      LemmaModInductionForall(n, fi);
      assert fi(x);
    }
  }

  lemma LemmaDivAddDenominator(n: int, x: int)
    requires n > 0
    ensures (x + n) / n == x / n + 1
  {
    LemmaFundamentalDivMod(x, n);
    LemmaFundamentalDivMod(x + n, n);
    var zp := (x + n) / n - x / n - 1;
    forall ensures 0 == n * zp + ((x + n) % n) - (x % n) { LemmaMulAuto(); }
    if (zp > 0) { LemmaMulInequality(1, zp, n); }
    if (zp < 0) { LemmaMulInequality(zp, -1, n); }
  }

  lemma LemmaDivSubDenominator(n: int, x: int)
    requires n > 0
    ensures (x - n) / n == x / n - 1
  {
    LemmaFundamentalDivMod(x, n);
    LemmaFundamentalDivMod(x - n, n);
    var zm := (x - n) / n - x / n + 1;
    forall ensures 0 == n * zm + ((x - n) % n) - (x % n) { LemmaMulAuto(); }
    if (zm > 0) { LemmaMulInequality(1, zm, n); }
    if (zm < 0) { LemmaMulInequality(zm, -1, n); }
  }

  lemma LemmaModAddDenominator(n: int, x: int)
    requires n > 0
    ensures (x + n) % n == x % n
  {
    LemmaFundamentalDivMod(x, n);
    LemmaFundamentalDivMod(x + n, n);
    var zp := (x + n) / n - x / n - 1;
    forall ensures 0 == n * zp + ((x + n) % n) - (x % n) { LemmaMulAuto(); }
    if (zp > 0) { LemmaMulInequality(1, zp, n); }
    if (zp < 0) { LemmaMulInequality(zp, -1, n); }
  }

  lemma LemmaModSubDenominator(n: int, x: int)
    requires n > 0
    ensures (x - n) % n == x % n
  {
    LemmaFundamentalDivMod(x, n);
    LemmaFundamentalDivMod(x - n, n);
    var zm := (x - n) / n - x / n + 1;
    forall ensures 0 == n * zm + ((x - n) % n) - (x % n) { LemmaMulAuto(); }
    if (zm > 0) { LemmaMulInequality(1, zm, n); }
    if (zm < 0) { LemmaMulInequality(zm, -1, n); }
  }

  lemma LemmaModBelowDenominator(n: int, x: int)
    requires n > 0
    ensures 0 <= x < n <==> x % n == x
  {
    forall x: int
      ensures 0 <= x < n <==> x % n == x
    {
      if (0 <= x < n) { LemmaSmallMod(x, n); }
      LemmaModRange(x, n);
    }
  }

  /* proves the basics of the modulus operation */
  lemma LemmaModBasics(n: int)
    requires n > 0
    ensures  forall x: int {:trigger (x + n) % n} :: (x + n) % n == x % n
    ensures  forall x: int {:trigger (x - n) % n} :: (x - n) % n == x % n
    ensures  forall x: int {:trigger (x + n) / n} :: (x + n) / n == x / n + 1
    ensures  forall x: int {:trigger (x - n) / n} :: (x - n) / n == x / n - 1
    ensures  forall x: int {:trigger x % n} :: 0 <= x < n <==> x % n == x
  {
    forall x: int
      ensures (x + n) % n == x % n
      ensures (x - n) % n == x % n
      ensures (x + n) / n == x / n + 1
      ensures (x - n) / n == x / n - 1
      ensures 0 <= x < n <==> x % n == x
    {
      LemmaModBelowDenominator(n, x);
      LemmaModAddDenominator(n, x);
      LemmaModSubDenominator(n, x);
      LemmaDivAddDenominator(n, x);
      LemmaDivSubDenominator(n, x);
    }
  }

  /* proves the quotient remainder theorem */
  lemma {:vcs_split_on_every_assert} LemmaQuotientAndRemainder(x: int, q: int, r: int, n: int)
    requires n > 0
    requires 0 <= r < n
    requires x == q * n + r
    ensures  q == x / n
    ensures  r == x % n
    decreases if q > 0 then q else -q
  {
    LemmaModBasics(n);

    if q > 0 {
      MulInternalsNonlinear.LemmaMulIsDistributiveAdd(n, q - 1, 1);
      LemmaMulIsCommutativeAuto();
      assert q * n + r == (q - 1) * n + n + r;
      LemmaQuotientAndRemainder(x - n, q - 1, r, n);
    }
    else if q < 0 {
      Mul.LemmaMulIsDistributiveSub(n, q + 1, 1);
      LemmaMulIsCommutativeAuto();
      assert q * n + r == (q + 1) * n - n + r;
      LemmaQuotientAndRemainder(x + n, q + 1, r, n);
    }
    else {
      LemmaSmallDiv();
      assert r / n == 0;
    }
  }

  /* automates the modulus operator process */
  ghost predicate ModAuto(n: int)
    requires n > 0
  {
    && (n % n == (-n) % n == 0)
    && (forall x: int {:trigger (x % n) % n} :: (x % n) % n == x % n)
    && (forall x: int {:trigger x % n} :: 0 <= x < n <==> x % n == x)
    && (forall x: int, y: int {:trigger (x + y) % n} ::
          (var z := (x % n) + (y % n);
           (  (0 <= z < n     && (x + y) % n == z)
              || (n <= z < n + n && (x + y) % n == z - n))))
    && (forall x: int, y: int {:trigger (x - y) % n} ::
          (var z := (x % n) - (y % n);
           (   (0 <= z < n && (x - y) % n == z)
               || (-n <= z < 0 && (x - y) % n == z + n))))
  }

  /* ensures that ModAuto is true */
  lemma LemmaModAuto(n: int)
    requires n > 0
    ensures  ModAuto(n)
  {
    LemmaModBasics(n);
    LemmaMulIsCommutativeAuto();
    LemmaMulIsDistributiveAddAuto();
    LemmaMulIsDistributiveSubAuto();

    forall x: int, y: int {:trigger (x + y) % n}
      ensures var z := (x % n) + (y % n);
              || (0 <= z < n && (x + y) % n == z)
              || (n <= z < 2 * n && (x + y) % n == z - n)
    {
      var xq, xr := x / n, x % n;
      LemmaFundamentalDivMod(x, n);
      assert x == xq * n + xr;
      var yq, yr := y / n, y % n;
      LemmaFundamentalDivMod(y, n);
      assert y == yq * n + yr;
      if xr + yr < n {
        LemmaQuotientAndRemainder(x + y, xq + yq, xr + yr, n);
      }
      else {
        LemmaQuotientAndRemainder(x + y, xq + yq + 1, xr + yr - n, n);
      }
    }

    forall x: int, y: int {:trigger (x - y) % n}
      ensures var z := (x % n) - (y % n);
              || (0 <= z < n && (x - y) % n == z)
              || (-n <= z < 0 && (x - y) % n == z + n)
    {
      var xq, xr := x / n, x % n;
      LemmaFundamentalDivMod(x, n);
      assert x == xq * n + xr;
      var yq, yr := y / n, y % n;
      LemmaFundamentalDivMod(y, n);
      assert y == yq * n + yr;
      if xr - yr >= 0 {
        LemmaQuotientAndRemainder(x - y, xq - yq, xr - yr, n);
      }
      else {
        LemmaQuotientAndRemainder(x - y, xq - yq - 1, xr - yr + n, n);
      }
    }
  }

  /* performs auto induction for modulus */
  lemma LemmaModInductionAuto(n: int, x: int, f: int -> bool)
    requires n > 0
    requires ModAuto(n) ==> && (forall i {:trigger IsLe(0, i)} :: IsLe(0, i) && i < n ==> f(i))
                            && (forall i {:trigger IsLe(0, i)} :: IsLe(0, i) && f(i) ==> f(i + n))
                            && (forall i {:trigger IsLe(i + 1, n)} :: IsLe(i + 1, n) && f(i) ==> f(i - n))
    ensures  ModAuto(n)
    ensures  f(x)
  {
    LemmaModAuto(n);
    assert forall i :: IsLe(0, i) && i < n ==> f(i);
    assert forall i {:trigger f(i), f(i + n)} :: IsLe(0, i) && f(i) ==> f(i + n);
    assert forall i {:trigger f(i), f(i - n)} :: IsLe(i + 1, n) && f(i) ==> f(i - n);
    LemmaModInductionForall(n, f);
    assert f(x);
  }

  // not used in other files
  /* performs auto induction on modulus for all i s.t. f(i) exists */
  lemma LemmaModInductionAutoForall(n: int, f: int -> bool)
    requires n > 0
    requires ModAuto(n) ==> && (forall i {:trigger IsLe(0, i)} :: IsLe(0, i) && i < n ==> f(i))
                            && (forall i {:trigger IsLe(0, i)} :: IsLe(0, i) && f(i) ==> f(i + n))
                            && (forall i {:trigger IsLe(i + 1, n)} :: IsLe(i + 1, n) && f(i) ==> f(i - n))
    ensures  ModAuto(n)
    ensures  forall i {:trigger f(i)} :: f(i)
  {
    LemmaModAuto(n);
    assert forall i :: IsLe(0, i) && i < n ==> f(i);
    assert forall i {:trigger f(i), f(i + n)} :: IsLe(0, i) && f(i) ==> f(i + n);
    assert forall i {:trigger f(i), f(i - n)} :: IsLe(i + 1, n) && f(i) ==> f(i - n);
    LemmaModInductionForall(n, f);
  }

}