-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path079-decimal_to_binary.dfy
40 lines (38 loc) · 1.02 KB
/
079-decimal_to_binary.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
type stringBin = s: string |
|s| > 0 && (|s| > 1 ==> s[0] != '0') &&
forall i | 0 <= i < |s| :: s[i] in "01"
witness "1"
method decimal_to_binary(n: nat) returns (s: string)
// post-conditions-start
ensures |s| == |decimal_to_binary_helper(n)| + 4
ensures s[..2] == "db"
ensures s[|s| - 2..] == "db"
ensures s[2..|s| - 2] == decimal_to_binary_helper(n)
// post-conditions-end
{
// impl-start
return "db" + decimal_to_binary_helper(n) + "db";
// impl-end
}
function decimal_to_binary_helper(n: nat): stringBin
// post-conditions-start
ensures binary_to_decimal(decimal_to_binary_helper(n)) == n
// post-conditions-end
{
// impl-start
match n
case 0 => "0" case 1 => "1"
case _ => decimal_to_binary_helper(n / 2) + decimal_to_binary_helper(n % 2)
// impl-end
}
// pure-end
function binary_to_decimal(s: stringBin): nat
decreases |s|
{
if |s| == 1 then
match s[0]
case '0' => 0 case '1' => 1
else
binary_to_decimal(s[..|s|-1])*2 + binary_to_decimal(s[|s|-1..|s|])
}
// pure-end