-
Notifications
You must be signed in to change notification settings - Fork 1
/
107-even_odd_palindrome.dfy
69 lines (62 loc) · 1.88 KB
/
107-even_odd_palindrome.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
function is_palindrome(n: nat) : bool {
var s := natToString(n);
forall i | 0 <= i < |s| :: s[i] == s[|s|-1-i]
}
method even_odd_palindrome(n: nat) returns (even: nat, odd: nat)
// post-conditions-start
ensures even == |set i | 0 <= i <= n && i % 2 == 0 && is_palindrome(i)|
ensures odd == |set i | 0 <= i <= n && i % 2 == 1 && is_palindrome(i)|
{
// impl-start
even := 0;
odd := 0;
ghost var even_pal := {};
ghost var odd_pal := {};
var i := 0;
while i <= n
// invariants-start
invariant 0 <= i <= n + 1
invariant even_pal == set j | 0 <= j < i && j % 2 == 0 && is_palindrome(j)
invariant odd_pal == set j | 0 <= j < i && j % 2 == 1 && is_palindrome(j)
invariant even == |even_pal|
invariant odd == |odd_pal|
// invariants-end
{
if is_palindrome(i) {
if i % 2 == 0 {
even := even + 1;
even_pal := even_pal + {i};
} else {
odd := odd + 1;
odd_pal := odd_pal + {i};
}
}
i := i + 1;
}
assert even_pal == set i | 0 <= i <= n && i % 2 == 0 && is_palindrome(i);
assert odd_pal == set i | 0 <= i <= n && i % 2 == 1 && is_palindrome(i);
}
type stringNat = s: string |
|s| > 0 && (|s| > 1 ==> s[0] != '0') &&
forall i | 0 <= i < |s| :: s[i] in "0123456789"
witness "1"
function natToString(n: nat): stringNat {
match n
case 0 => "0" case 1 => "1" case 2 => "2" case 3 => "3" case 4 => "4"
case 5 => "5" case 6 => "6" case 7 => "7" case 8 => "8" case 9 => "9"
case _ => natToString(n / 10) + natToString(n % 10)
}
function stringToNat(s: stringNat): nat
decreases |s|
{
if |s| == 1 then
match s[0]
case '0' => 0 case '1' => 1 case '2' => 2 case '3' => 3 case '4' => 4
case '5' => 5 case '6' => 6 case '7' => 7 case '8' => 8 case '9' => 9
else
stringToNat(s[..|s|-1])*10 + stringToNat(s[|s|-1..|s|])
}
lemma natToStringThenStringToNatIdem(n: nat)
ensures stringToNat(natToString(n)) == n
{
}