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

« back to all changes in this revision

Viewing changes to contrib/correctness/examples/exp.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
(*i $Id: exp.v 1577 2001-04-11 07:56:19Z filliatr $ i*)
 
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 Even.
 
22
Require Div2.
 
23
Require Correctness.
 
24
Require ArithRing.
 
25
Require ZArithRing.
 
26
 
 
27
(* The specification uses the traditional definition of X^n *)
 
28
 
 
29
Fixpoint power [x,n:nat] : nat :=
 
30
  Cases n of
 
31
    O      => (S O)
 
32
  | (S n') => (mult x (power x n'))
 
33
  end.
 
34
 
 
35
Definition square := [n:nat](mult n n).
 
36
 
 
37
 
 
38
(* Three lemmas are necessary to establish the forthcoming proof obligations *)
 
39
 
 
40
(* n = 2*(n/2) => (x^(n/2))^2 = x^n *)
 
41
 
 
42
Lemma exp_div2_0 : (x,n:nat)
 
43
     n=(double (div2 n)) 
 
44
  -> (square (power x (div2 n)))=(power x n).
 
45
Proof.
 
46
Unfold square.
 
47
Intros x n. Pattern n. Apply ind_0_1_SS.
 
48
Auto.
 
49
 
 
50
Intro. (Absurd (1)=(double (0)); Auto).
 
51
 
 
52
Intros. Simpl.
 
53
Cut n0=(double (div2 n0)).
 
54
Intro. Rewrite <- (H H1).
 
55
Ring.
 
56
 
 
57
Simpl in H0.
 
58
Unfold double in H0.
 
59
Simpl in H0.
 
60
Rewrite <- (plus_n_Sm (div2 n0) (div2 n0)) in H0.
 
61
(Injection H0; Auto).
 
62
Save.
 
63
 
 
64
(* n = 2*(n/2)+1 => x*(x^(n/2))^2 = x^n *)
 
65
 
 
66
Lemma exp_div2_1 : (x,n:nat) 
 
67
     n=(S (double (div2 n)))
 
68
  -> (mult x (square (power x (div2 n))))=(power x n).
 
69
Proof.
 
70
Unfold square.
 
71
Intros x n. Pattern n. Apply ind_0_1_SS.
 
72
 
 
73
Intro. (Absurd (0)=(S (double (0))); Auto).
 
74
 
 
75
Auto.
 
76
 
 
77
Intros. Simpl.
 
78
Cut n0=(S (double (div2 n0))).
 
79
Intro. Rewrite <- (H H1).
 
80
Ring.
 
81
 
 
82
Simpl in H0.
 
83
Unfold double in H0.
 
84
Simpl in H0.
 
85
Rewrite <- (plus_n_Sm (div2 n0) (div2 n0)) in H0.
 
86
(Injection H0; Auto).
 
87
Save.
 
88
 
 
89
(* x^(2*n) = (x^2)^n *)
 
90
 
 
91
Lemma power_2n : (x,n:nat)(power x (double n))=(power (square x) n).
 
92
Proof.
 
93
Unfold double. Unfold square.
 
94
Induction n.
 
95
Auto.
 
96
 
 
97
Intros.
 
98
Simpl.
 
99
Rewrite <- H.
 
100
Rewrite <- (plus_n_Sm n0 n0).
 
101
Simpl.
 
102
Auto with arith.
 
103
Save.
 
104
 
 
105
Hints Resolve exp_div2_0 exp_div2_1.
 
106
 
 
107
 
 
108
(* Functional version.
 
109
 * 
 
110
 * Here we give the functional program as an incomplete CIC term,
 
111
 * using the tactic Refine.
 
112
 *
 
113
 * On this example, it really behaves as the tactic Program.
 
114
 *)
 
115
 
 
116
(*
 
117
Lemma f_exp : (x,n:nat) { y:nat | y=(power x n) }.
 
118
Proof.
 
119
Refine [x:nat]
 
120
  (well_founded_induction nat lt lt_wf
 
121
    [n:nat]{y:nat | y=(power x n) }
 
122
    [n:nat]
 
123
    [f:(p:nat)(lt p n)->{y:nat | y=(power x p) }]
 
124
             Cases (zerop n) of 
 
125
               (left _) => (exist ? ? (S O) ?)
 
126
             | (right _) => 
 
127
                  let (y,H) = (f (div2 n) ?) in 
 
128
                  Cases (even_odd_dec n) of
 
129
                    (left _) => (exist ? ? (mult y y) ?)
 
130
                  | (right _) => (exist ? ? (mult x (mult y y)) ?)
 
131
                  end
 
132
             end).
 
133
Proof.
 
134
Rewrite a. Auto.
 
135
Exact (lt_div2 n a).
 
136
Change (square y)=(power x n). Rewrite H. Auto with arith.
 
137
Change (mult x (square y))=(power x n). Rewrite H. Auto with arith.
 
138
Save.
 
139
*)
 
140
 
 
141
(* Imperative version. *)
 
142
 
 
143
Definition even_odd_bool := [x:nat](bool_of_sumbool ? ? (even_odd_dec x)).
 
144
 
 
145
Correctness i_exp
 
146
  fun (x:nat)(n:nat) ->
 
147
    let y = ref (S O) in
 
148
    let m = ref x in
 
149
    let e = ref n in
 
150
    begin
 
151
      while (notzerop_bool !e) do
 
152
        { invariant (power x n)=(mult y (power m e)) as Inv
 
153
          variant e for lt }
 
154
        (if not (even_odd_bool !e) then y := (mult !y !m))
 
155
          { (power x n) = (mult y (power m (double (div2 e)))) as Q };
 
156
        m := (square !m);
 
157
        e := (div2 !e)
 
158
      done;
 
159
      !y
 
160
    end
 
161
    { result=(power x n) }
 
162
.
 
163
Proof.
 
164
Rewrite (odd_double e0 Test1) in Inv. Rewrite Inv. Simpl. Auto with arith.
 
165
 
 
166
Rewrite (even_double e0 Test1) in Inv. Rewrite Inv. Reflexivity.
 
167
 
 
168
Split.
 
169
Exact (lt_div2 e0 Test2).
 
170
 
 
171
Rewrite Q. Unfold double. Unfold square.
 
172
Simpl. 
 
173
Change (mult y1 (power m0 (double (div2 e0))))
 
174
     = (mult y1 (power (square m0) (div2 e0))).
 
175
Rewrite (power_2n m0 (div2 e0)). Reflexivity.
 
176
 
 
177
Auto with arith.
 
178
 
 
179
Decompose [and] Inv.
 
180
Rewrite H. Rewrite H0.
 
181
Auto with arith.
 
182
Save.
 
183
 
 
184
 
 
185
(* Recursive version. *)
 
186
 
 
187
Correctness r_exp
 
188
  let rec exp (x:nat) (n:nat) : nat { variant n for lt} =
 
189
    (if (zerop_bool n) then
 
190
       (S O)
 
191
     else
 
192
       let y = (exp x (div2 n)) in
 
193
       if (even_odd_bool n) then
 
194
         (mult y y)
 
195
       else
 
196
         (mult x (mult y y))
 
197
    ) { result=(power x n) }
 
198
.
 
199
Proof.
 
200
Rewrite Test2. Auto.
 
201
Exact (lt_div2 n0 Test2).
 
202
Change (square y)=(power x0 n0). Rewrite Post7. Auto with arith.
 
203
Change (mult x0 (square y))=(power x0 n0). Rewrite Post7. Auto with arith.
 
204
Save.