~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to contrib/correctness/examples/exp_int.v

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(***********************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
 
3
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
 
4
(*   \VV/  *************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the      *)
 
6
(*         *       GNU Lesser General Public License Version 2.1       *)
 
7
(***********************************************************************)
 
8
 
 
9
(* Certification of Imperative Programs / Jean-Christophe Filli�tre *)
 
10
 
 
11
(* $Id: exp_int.v 1577 2001-04-11 07:56:19Z filliatr $ *)
 
12
 
 
13
(* Efficient computation of X^n using
 
14
 * 
 
15
 *    X^(2n)   =     (X^n) ^ 2
 
16
 *    X^(2n+1) = X . (X^n) ^ 2
 
17
 *
 
18
 * Proofs of both fonctional and imperative programs.
 
19
 *)
 
20
 
 
21
Require Zpower.
 
22
Require Zcomplements.
 
23
 
 
24
Require Correctness.
 
25
Require ZArithRing.
 
26
Require Omega.
 
27
 
 
28
Definition Zdouble := [n:Z]`2*n`.
 
29
 
 
30
Definition Zsquare := [n:Z](Zmult n n).
 
31
 
 
32
(* Some auxiliary lemmas about Zdiv2 are necessary *)
 
33
 
 
34
Lemma Zdiv2_ge_0 : (x:Z) `x >= 0` -> `(Zdiv2 x) >= 0`.
 
35
Proof.
 
36
Destruct x; Auto with zarith.
 
37
Destruct p; Auto with zarith.
 
38
Simpl. Omega.
 
39
Intros. (Absurd `(NEG p) >= 0`; Red; Auto with zarith).
 
40
Save.
 
41
 
 
42
Lemma Zdiv2_lt : (x:Z) `x > 0` -> `(Zdiv2 x) < x`.
 
43
Proof.
 
44
Destruct x.
 
45
Intro. Absurd `0 > 0`; [ Omega | Assumption ].
 
46
Destruct p; Auto with zarith.
 
47
 
 
48
Simpl.
 
49
Intro p0.
 
50
Replace (POS (xI p0)) with `2*(POS p0)+1`.
 
51
Omega.
 
52
Simpl. Auto with zarith.
 
53
 
 
54
Intro p0.
 
55
Simpl.
 
56
Replace (POS (xO p0)) with `2*(POS p0)`.
 
57
Omega.
 
58
Simpl. Auto with zarith.
 
59
 
 
60
Simpl. Omega.
 
61
 
 
62
Intros. 
 
63
Absurd `(NEG p) > 0`; Red; Auto with zarith.
 
64
Elim p; Auto with zarith.
 
65
Omega.
 
66
Save.
 
67
 
 
68
(* A property of Zpower:  x^(2*n) = (x^2)^n *)
 
69
 
 
70
Lemma Zpower_2n : 
 
71
  (x,n:Z)`n >= 0` -> (Zpower x (Zdouble n))=(Zpower (Zsquare x) n).
 
72
Proof.
 
73
Unfold Zdouble.
 
74
Intros x n Hn.
 
75
Replace `2*n` with `n+n`.
 
76
Rewrite Zpower_exp.
 
77
Pattern n.
 
78
Apply natlike_ind.
 
79
 
 
80
Simpl. Auto with zarith.
 
81
 
 
82
Intros.
 
83
Unfold Zs.
 
84
Rewrite Zpower_exp.
 
85
Rewrite Zpower_exp.
 
86
Replace (Zpower x `1`) with x.
 
87
Replace (Zpower (Zsquare x) `1`) with (Zsquare x).
 
88
Rewrite <- H0.
 
89
Unfold Zsquare.
 
90
Ring.
 
91
 
 
92
Unfold Zpower; Unfold Zpower_pos; Simpl. Omega.
 
93
 
 
94
Unfold Zpower; Unfold Zpower_pos; Simpl. Omega.
 
95
 
 
96
Omega.
 
97
Omega.
 
98
Omega.
 
99
Omega.
 
100
Omega.
 
101
Assumption.
 
102
Assumption.
 
103
Omega.
 
104
Save.
 
105
 
 
106
 
 
107
(* The program *)
 
108
 
 
109
Correctness i_exp
 
110
  fun (x:Z)(n:Z) ->
 
111
    { `n >= 0` }
 
112
   (let y = ref 1 in
 
113
    let m = ref x in
 
114
    let e = ref n in
 
115
    begin
 
116
      while !e > 0 do
 
117
        { invariant (Zpower x n)=(Zmult y (Zpower m e)) /\ `e>=0` as Inv
 
118
          variant e }
 
119
        (if not (Zeven_odd_bool !e) then y := (Zmult !y !m))
 
120
          { (Zpower x n) = (Zmult y (Zpower m (Zdouble (Zdiv2 e)))) as Q };
 
121
        m := (Zsquare !m);
 
122
        e := (Zdiv2 !e)
 
123
      done;
 
124
      !y
 
125
    end)
 
126
    { result=(Zpower x n) }
 
127
.
 
128
Proof.
 
129
(* Zodd *)
 
130
Decompose [and] Inv.
 
131
Rewrite (Zodd_div2 e0 H0 Test1) in H. Rewrite H.
 
132
Rewrite Zpower_exp.
 
133
Unfold Zdouble.
 
134
Replace (Zpower m0 `1`) with m0.
 
135
Ring.
 
136
Unfold Zpower; Unfold Zpower_pos; Simpl; Ring.
 
137
Generalize (Zdiv2_ge_0 e0); Omega.
 
138
Omega.
 
139
(* Zeven *)
 
140
Decompose [and] Inv.
 
141
Rewrite (Zeven_div2 e0 Test1) in H. Rewrite H.
 
142
Auto with zarith.
 
143
Split.
 
144
(* Zwf *)
 
145
Unfold Zwf.
 
146
Repeat Split.
 
147
Generalize (Zdiv2_ge_0 e0); Omega.
 
148
Omega.
 
149
Exact (Zdiv2_lt e0 Test2).
 
150
(* invariant *)
 
151
Split.
 
152
Rewrite Q. Unfold Zdouble. Unfold Zsquare.
 
153
Rewrite (Zpower_2n).
 
154
Trivial.
 
155
Generalize (Zdiv2_ge_0 e0); Omega.
 
156
Generalize (Zdiv2_ge_0 e0); Omega.
 
157
Split; [ Ring | Assumption ].
 
158
(* exit fo loop *)
 
159
Decompose [and] Inv.
 
160
Cut `e0 = 0`. Intro.
 
161
Rewrite H1. Rewrite H.
 
162
Simpl; Ring.
 
163
Omega.
 
164
Save.
 
165
 
 
166
 
 
167
(* Recursive version. *)
 
168
 
 
169
Correctness r_exp
 
170
  let rec exp (x:Z) (n:Z) : Z { variant n } =
 
171
    { `n >= 0` }
 
172
    (if n = 0 then
 
173
       1
 
174
     else
 
175
       let y = (exp x (Zdiv2 n)) in
 
176
       (if (Zeven_odd_bool n) then
 
177
          (Zmult y y)
 
178
        else
 
179
          (Zmult x (Zmult y y))) { result=(Zpower x n) as Q }
 
180
    ) 
 
181
    { result=(Zpower x n) }
 
182
.
 
183
Proof.
 
184
Rewrite Test2. Auto with zarith.
 
185
(* w.f. *)
 
186
Unfold Zwf.
 
187
Repeat Split.
 
188
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
189
Omega.
 
190
Generalize (Zdiv2_lt n0) ; Omega.
 
191
(* rec. call *)
 
192
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
193
(* invariant: case even *)
 
194
Generalize (Zeven_div2 n0 Test1).
 
195
Intro Heq. Rewrite Heq.
 
196
Rewrite Post4.
 
197
Replace `2*(Zdiv2 n0)` with `(Zdiv2 n0)+(Zdiv2 n0)`.
 
198
Rewrite Zpower_exp.
 
199
Auto with zarith.
 
200
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
201
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
202
Omega.
 
203
(* invariant: cas odd *)
 
204
Generalize (Zodd_div2 n0 Pre1 Test1).
 
205
Intro Heq. Rewrite Heq.
 
206
Rewrite Post4.
 
207
Rewrite Zpower_exp.
 
208
Replace `2*(Zdiv2 n0)` with `(Zdiv2 n0)+(Zdiv2 n0)`.
 
209
Rewrite Zpower_exp.
 
210
Replace `(Zpower x0 1)` with x0.
 
211
Ring.
 
212
Unfold Zpower; Unfold Zpower_pos; Simpl. Omega.
 
213
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
214
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
215
Omega.
 
216
Generalize (Zdiv2_ge_0 n0) ; Omega.
 
217
Omega.
 
218
Save.