-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfactorial.v
86 lines (69 loc) · 2.25 KB
/
myfactorial.v
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
Require Import AutoSep WordLemmas.
(* ============================================================================
* specification
* ========================================================================= *)
Fixpoint fact (n : nat) :=
match n with
| 0 => 1
| S n' => fact n' * n
end.
Definition factS : spec := SPEC("n") reserving 1
PRE[V] Emp
POST[R] [| R = natToW (fact (wordToNat (V "n"))) |].
(* ============================================================================
* implementation
* ========================================================================= *)
Definition m := bmodule "factorial" {{
bfunction "fact" ("n", "r" ) [factS]
"r" <- 1;;
[ PRE[V] Emp
POST[R] [| R = (wordToNat (V "r")
* fact (wordToNat (V "n")))%nat |] ]
While ("n" > 1) {
"r" <- "r" * "n";;
"n" <- "n" - 1
};;
Return "r"
end
}}.
(* ============================================================================
* lemmas
* ========================================================================= *)
Lemma fact_gt_1 : forall (r n : W), natToW 1 < n
-> natToW (wordToNat (r ^* n)
* fact (wordToNat (n ^- natToW 1)))
= natToW (wordToNat r * fact (wordToNat n)).
intros; destruct_words; roundtrip.
destruct w0; simpl; try omega.
replace (w0 - 0) with w0 by omega.
rewrite Mult.mult_succ_r.
rewrite ! natToWord_mult.
roundtrip.
rewrite <- wmult_assoc.
f_equal.
replace (S w0) with (1 + w0) by omega.
rewrite ! natToWord_plus.
rewrite ! natToWord_mult.
words.
Qed.
Hint Resolve fact_gt_1.
Lemma fact_le_1 : forall r n : W, n <= natToW 1
-> r = natToW (wordToNat r * fact (wordToNat n)).
intros; destruct_words; roundtrip.
f_equal.
repeat (destruct w0; simpl; try omega).
Qed.
Hint Resolve fact_le_1.
(* ===========================================================================
* Proof
* ========================================================================= *)
Ltac finish :=
match goal with
| _ => solve [auto]
| H: Regs _ Rv = _ |- _ => rewrite H
end.
Hint Extern 1 (natToW _ = natToW _) => roundtrip; auto.
Theorem ok : moduleOk m.
vcgen; sep_auto; repeat finish.
Qed.
Print Assumptions ok.