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
//! This file contains proofs related to division. These are internal
//! functions used within the math standard library.
//!
//! It's based on the following file from the Dafny math standard library:
//! `Source/DafnyStandardLibraries/src/Std/Arithmetic/Internal/DivInternals.dfy`.
//! That file has the following copyright notice:
//! /*******************************************************************************
//! * Original: Copyright (c) Microsoft Corporation
//! * SPDX-License-Identifier: MIT
//! *
//! * Modifications and Extensions: Copyright by the contributors to the Dafny Project
//! * SPDX-License-Identifier: MIT
//! *******************************************************************************/
#[allow(unused_imports)]
use super::super::super::prelude::*;
verus! {
#[cfg(verus_keep_ghost)]
use super::super::super::arithmetic::internals::general_internals::is_le;
#[cfg(verus_keep_ghost)]
use super::super::super::arithmetic::internals::mod_internals::{
lemma_mod_induction_forall,
lemma_mod_induction_forall2,
mod_auto,
lemma_mod_auto,
lemma_mod_basics,
};
use super::super::super::arithmetic::internals::mod_internals_nonlinear;
#[cfg(verus_keep_ghost)]
#[cfg(verus_keep_ghost)]
use super::super::super::arithmetic::internals::div_internals_nonlinear;
#[cfg(verus_keep_ghost)]
use super::super::super::math::{add as add1, sub as sub1};
/// This function recursively computes the quotient resulting from
/// dividing two numbers `x` and `d`, in the case where `d > 0`
#[verifier::opaque]
pub open spec fn div_pos(x: int, d: int) -> int
recommends
d > 0,
decreases
(if x < 0 {
d - x
} else {
x
}),
when d > 0
{
if x < 0 {
-1 + div_pos(x + d, d)
} else if x < d {
0
} else {
1 + div_pos(x - d, d)
}
}
/// This function recursively computes the quotient resulting from
/// dividing two numbers `x` and `d`. It's only meaningful when `d !=
/// 0`, of course.
#[verifier::opaque]
pub open spec fn div_recursive(x: int, d: int) -> int
recommends
d != 0,
{
// reveal(div_pos);
if d > 0 {
div_pos(x, d)
} else {
-1 * div_pos(x, -1 * d)
}
}
/// Proof of basic properties of integer division when the divisor is
/// the given positive integer `n`
pub proof fn lemma_div_basics(n: int)
requires
n > 0,
ensures
(n / n) == 1 && -((-n) / n) == 1,
forall|x: int| 0 <= x < n <==> #[trigger] (x / n) == 0,
forall|x: int| #[trigger] ((x + n) / n) == x / n + 1,
forall|x: int| #[trigger] ((x - n) / n) == x / n - 1,
{
lemma_mod_auto(n);
lemma_mod_basics(n);
div_internals_nonlinear::lemma_small_div();
div_internals_nonlinear::lemma_div_by_self(n);
assert forall|x: int| #[trigger] (x / n) == 0 implies 0 <= x < n by {
mod_internals_nonlinear::lemma_fundamental_div_mod(x, n);
}
}
/// This function says that for any `x` and `y`, there are two
/// possibilities for the sum `x % n + y % n`: (1) It's in the range
/// `[0, n)` and `(x + y) / n == x / n + y / n`. (2) It's in the range
/// `[n, n + n)` and `(x + y) / n = x / n + y / n + 1`.
pub open spec fn div_auto_plus(n: int) -> bool {
forall|x: int, y: int|
#![trigger ((x + y) / n)]
{
let z = (x % n) + (y % n);
((0 <= z < n && ((x + y) / n) == x / n + y / n) || (n <= z < n + n && ((x + y) / n) == x
/ n + y / n + 1))
}
}
/// This function says that for any `x` and `y`, there are two
/// possibilities for the difference `x % n - y % n`: (1) It's in the
/// range `[0, n)` and `(x - y) / n == x / n - y / n`. (2) It's in the
/// range `[-n, 0)` and `(x - y) / n = x / n - y / n - 1`.
pub open spec fn div_auto_minus(n: int) -> bool {
forall|x: int, y: int|
#![trigger ((x - y) / n)]
{
let z = (x % n) - (y % n);
((0 <= z < n && ((x - y) / n) == x / n - y / n) || (-n <= z < 0 && ((x - y) / n) == x
/ n - y / n - 1))
}
}
/// This function states various properties of integer division when
/// the denominator is `n`, including the identity property, a fact
/// about when quotients are zero, and facts about adding and
/// subtracting integers over this common denominator
pub open spec fn div_auto(n: int) -> bool
recommends
n > 0,
{
&&& mod_auto(n)
&&& (n / n == -((-n) / n) == 1)
&&& forall|x: int| 0 <= x < n <==> #[trigger] (x / n) == 0
&&& div_auto_plus(n)
&&& div_auto_minus(n)
}
/// Proof of `div_auto_plus(n)`, not exported publicly because it's
/// just used as part of [`lemma_div_auto`] to prove `div_auto(n)`
proof fn lemma_div_auto_plus(n: int)
requires
n > 0,
ensures
div_auto_plus(n),
{
lemma_mod_auto(n);
lemma_div_basics(n);
assert forall|x: int, y: int|
{
let z = (x % n) + (y % n);
((0 <= z < n && #[trigger] ((x + y) / n) == x / n + y / n) || (n <= z < n + n && ((x
+ y) / n) == x / n + y / n + 1))
} by {
let f = |xx: int, yy: int|
{
let z = (xx % n) + (yy % n);
((0 <= z < n && ((xx + yy) / n) == xx / n + yy / n) || (n <= z < 2 * n && ((xx + yy)
/ n) == xx / n + yy / n + 1))
};
assert forall|i: int, j: int|
{
// changing this from j + n to mod's addition speeds up the verification
// otherwise you need higher rlimit
// might be a good case for profilers
&&& (j >= 0 && #[trigger] f(i, j) ==> f(i, add1(j, n)))
&&& (i < n && f(i, j) ==> f(i - n, j))
&&& (j < n && f(i, j) ==> f(i, j - n))
&&& (i >= 0 && f(i, j) ==> f(i + n, j))
} by {
assert(((i + n) + j) / n == ((i + j) + n) / n);
assert((i + (j + n)) / n == ((i + j) + n) / n);
assert(((i - n) + j) / n == ((i + j) - n) / n);
assert((i + (j - n)) / n == ((i + j) - n) / n);
}
assert forall|i: int, j: int| 0 <= i < n && 0 <= j < n implies #[trigger] f(i, j) by {
assert(((i + n) + j) / n == ((i + j) + n) / n);
assert((i + (j + n)) / n == ((i + j) + n) / n);
assert(((i - n) + j) / n == ((i + j) - n) / n);
assert((i + (j - n)) / n == ((i + j) - n) / n);
}
lemma_mod_induction_forall2(n, f);
assert(f(x, y));
}
}
/// Proof of `div_auto_mius(n)`, not exported publicly because it's
/// just used as part of [`lemma_div_auto`] to prove `div_auto(n)`
#[verifier::spinoff_prover]
proof fn lemma_div_auto_minus(n: int)
requires
n > 0,
ensures
div_auto_minus(n),
{
lemma_mod_auto(n);
lemma_div_basics(n);
assert forall|x: int, y: int|
{
let z = (x % n) - (y % n);
((0 <= z < n && #[trigger] ((x - y) / n) == x / n - y / n) || (-n <= z < 0 && ((x - y)
/ n) == x / n - y / n - 1))
} by {
let f = |xx: int, yy: int|
{
let z = (xx % n) - (yy % n);
((0 <= z < n && ((xx - yy) / n) == xx / n - yy / n) || (-n <= z < 0 && (xx - yy) / n
== xx / n - yy / n - 1))
};
assert forall|i: int, j: int|
{
&&& (j >= 0 && #[trigger] f(i, j) ==> f(i, add1(j, n)))
&&& (i < n && f(i, j) ==> f(sub1(i, n), j))
&&& (j < n && f(i, j) ==> f(i, sub1(j, n)))
&&& (i >= 0 && f(i, j) ==> f(add1(i, n), j))
} by {
assert(((i + n) - j) / n == ((i - j) + n) / n);
assert((i - (j - n)) / n == ((i - j) + n) / n);
assert(((i - n) - j) / n == ((i - j) - n) / n);
assert((i - (j + n)) / n == ((i - j) - n) / n);
}
assert forall|i: int, j: int| 0 <= i < n && 0 <= j < n implies #[trigger] f(i, j) by {
assert(((i + n) - j) / n == ((i - j) + n) / n);
assert((i - (j - n)) / n == ((i - j) + n) / n);
assert(((i - n) - j) / n == ((i - j) - n) / n);
assert((i - (j + n)) / n == ((i - j) - n) / n);
}
lemma_mod_induction_forall2(n, f);
assert(f(x, y));
}
}
/// Proof of `div_auto(n)`, which expresses many useful properties of
/// division when the denominator is the given positive integer `n`.
pub proof fn lemma_div_auto(n: int)
requires
n > 0,
ensures
div_auto(n),
{
lemma_mod_auto(n);
lemma_div_basics(n);
assert forall|x: int| 0 <= x < n <==> #[trigger] (x / n) == 0 by {
lemma_div_basics(n);
}
assert((0 + n) / n == 1);
assert((0 - n) / n == -1);
lemma_div_auto_plus(n);
lemma_div_auto_minus(n);
}
/// This utility function helps prove a mathematical property by
/// induction. The caller supplies an integer predicate, proves the
/// predicate holds in certain base cases, and proves correctness of
/// inductive steps both upward and downward from the base cases. This
/// lemma invokes induction to establish that the predicate holds for
/// the given arbitrary input `x`.
///
/// `f`: The integer predicate
///
/// `n`: Upper bound on the base cases. Specifically, the caller
/// establishes `f(i)` for every value `i` satisfying `is_le(0, i) &&
/// i < n`.
///
/// `x`: The desired case established by this lemma. Its postcondition
/// thus includes `f(x)`.
///
/// To prove inductive steps upward from the base cases, the caller
/// must establish that, for any `i`, `is_le(0, i) && f(i) ==> f(i +
/// n)`. `is_le(0, i)` is just `0 <= i`, but written in a functional
/// style so that it can be used where functional triggers are
/// required.
///
/// To prove inductive steps downward from the base cases, the caller
/// must establish that, for any `i`, `is_le(i + 1, n) && f(i) ==> f(i
/// - n)`. `is_le(i + 1, n)` is just `i + 1 <= n`, but written in a
/// functional style so that it can be used where functional triggers
/// are required.
pub proof fn lemma_div_induction_auto(n: int, x: int, f: spec_fn(int) -> bool)
requires
n > 0,
div_auto(n) ==> {
&&& (forall|i: int| #[trigger] is_le(0, i) && i < n ==> f(i))
&&& (forall|i: int| #[trigger] is_le(0, i) && f(i) ==> f(i + n))
&&& (forall|i: int| #[trigger] is_le(i + 1, n) && f(i) ==> f(i - n))
},
ensures
div_auto(n),
f(x),
{
lemma_div_auto(n);
assert(forall|i: int| is_le(0, i) && i < n ==> f(i));
assert(forall|i: int| is_le(0, i) && #[trigger] f(i) ==> #[trigger] f(add1(i, n)));
assert(forall|i: int| is_le(i + 1, n) && #[trigger] f(i) ==> #[trigger] f(sub1(i, n)));
assert forall|i: int| 0 <= i < n implies #[trigger] f(i) by {
assert(f(i)) by {
assert(forall|i: int| is_le(0, i) && i < n ==> f(i));
assert(is_le(0, i) && i < n);
};
};
lemma_mod_induction_forall(n, f);
assert(f(x));
}
/// This utility function helps prove a mathematical property by
/// induction. The caller supplies an integer predicate, proves the
/// predicate holds in certain base cases, and proves correctness of
/// inductive steps both upward and downward from the base cases. This
/// lemma invokes induction to establish that the predicate holds for
/// all integer values.
///
/// `f`: The integer predicate
///
/// `n`: Upper bound on the base cases. Specifically, the caller
/// establishes `f(i)` for every value `i` satisfying `is_le(0, i) &&
/// i < n`.
///
/// To prove inductive steps upward from the base cases, the caller
/// must establish that, for any `i`, `is_le(0, i) && f(i) ==> f(i +
/// n)`. `is_le(0, i)` is just `0 <= i`, but written in a functional
/// style so that it can be used where functional triggers are
/// required.
///
/// To prove inductive steps downward from the base cases, the caller
/// must establish that, for any `i`, `is_le(i + 1, n) && f(i) ==> f(i
/// - n)`. `is_le(i + 1, n)` is just `i + 1 <= n`, but written in a
/// functional style so that it can be used where functional triggers
/// are required.
pub proof fn lemma_div_induction_auto_forall(n: int, f: spec_fn(int) -> bool)
requires
n > 0,
div_auto(n) ==> {
&&& (forall|i: int| #[trigger] is_le(0, i) && i < n ==> f(i))
&&& (forall|i: int| #[trigger] is_le(0, i) && f(i) ==> f(i + n))
&&& (forall|i: int| #[trigger] is_le(i + 1, n) && f(i) ==> f(i - n))
},
ensures
div_auto(n),
forall|i| #[trigger] f(i),
{
assert(div_auto(n)) by {
lemma_div_induction_auto(n, 0, f);
}
assert forall|i| #[trigger] f(i) by {
lemma_div_induction_auto(n, i, f);
}
}
} // verus!