-
Notifications
You must be signed in to change notification settings - Fork 4
/
pow.dfy
72 lines (63 loc) · 1.38 KB
/
pow.dfy
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
/*
Uninteresting definition of pow (exponentiation)
*/
include "../nonlin/arith.dfy"
module Pow {
import opened Arith
function pow(x:nat, k:nat): (p:nat)
decreases k
{
if k == 0 then 1 else (
mul_positive(x, pow(x,k-1));
x * pow(x,k-1)
)
}
lemma {:induction k1} pow_plus(x: nat, k1: nat, k2: nat)
decreases k1
ensures pow(x, k1) * pow(x, k2) == pow(x, k1+k2)
{
if k1 == 0 {
calc {
pow(x, k1) * pow(x, k2);
1 * pow(x, k2);
pow(x, k2);
pow(x, k1+k2);
}
} else {
calc {
pow(x, k1) * pow(x, k2);
x * pow(x,k1-1) * pow(x, k2);
{ pow_plus(x, k1-1, k2); }
{ mul_assoc(x, pow(x,k1-1), pow(x, k2)); }
x * pow(x, (k1-1) + k2);
pow(x, k1+k2);
}
}
}
lemma {:induction k} pow_pos(x: nat, k: nat)
decreases k
requires 0 < x
ensures 0 < pow(x, k)
{
if k == 0 {
} else {
pow_pos(x, k-1);
mul_r_increasing(pow(x, k-1), x);
}
}
lemma {:induction false} pow_increasing(x: nat, k1: nat, k2: nat)
requires 0 < x
ensures pow(x, k1) <= pow(x, k1+k2)
{
pow_plus(x, k1, k2);
pow_pos(x, k2);
mul_r_increasing(pow(x, k1), pow(x, k2));
}
lemma pow_incr(x: nat, k1: nat, k2: nat)
requires 0 < x
requires k1 <= k2
ensures pow(x, k1) <= pow(x, k2)
{
pow_increasing(x, k1, k2-k1);
}
}