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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
(** * DecimalR
Proofs that conversions between decimal numbers and [R]
are bijections. *)
Require Import Decimal DecimalFacts DecimalPos DecimalZ DecimalQ Rdefinitions.
Lemma of_IQmake_to_decimal num den :
match IQmake_to_decimal num den with
| None => True
| Some (DecimalExp _ _ _) => False
| Some (Decimal i f) =>
of_decimal (Decimal i f) = IRQ (QArith_base.Qmake num den)
end.
Proof.
unfold IQmake_to_decimal.
case (Pos.eq_dec den 1); [now intros->|intro Hden].
assert (Hf : match QArith_base.IQmake_to_decimal num den with
| Some (Decimal i f) => f <> Nil
| _ => True
end).
{ unfold QArith_base.IQmake_to_decimal; simpl.
generalize (Unsigned.nztail_to_uint den).
case Decimal.nztail as [den' e_den'].
case den'; [now simpl|now simpl| |now simpl..]; clear den'; intro den'.
case den'; [ |now simpl..]; clear den'.
case e_den' as [|e_den']; [now simpl; intros H _; apply Hden; injection H|].
intros _.
case Nat.ltb_spec; intro He_den'.
- apply del_head_nonnil.
revert He_den'; case nb_digits as [|n]; [now simpl|].
now intro H; simpl; apply Nat.lt_succ_r, Nat.le_sub_l.
- apply nb_digits_n0.
now rewrite nb_digits_iter_D0, Nat.sub_add. }
replace (match den with 1%positive => _ | _ => _ end)
with (QArith_base.IQmake_to_decimal num den); [|now revert Hden; case den].
generalize (of_IQmake_to_decimal num den).
case QArith_base.IQmake_to_decimal as [d'|]; [|now simpl].
case d' as [i f|]; [|now simpl].
unfold of_decimal; simpl.
injection 1 as H <-.
generalize (f_equal QArith_base.IZ_to_Z H); clear H.
rewrite !IZ_to_Z_IZ_of_Z; injection 1 as <-.
now revert Hf; case f.
Qed.
Lemma of_to (q:IR) : forall d, to_decimal q = Some d -> of_decimal d = q.
Proof.
intro d.
case q as [z|q|r r'|r r']; simpl.
- case z as [z p| |p|p].
+ now simpl.
+ now simpl; injection 1 as <-.
+ simpl; injection 1 as <-.
now unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to.
+ simpl; injection 1 as <-.
now unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to.
- case q as [num den].
generalize (of_IQmake_to_decimal num den).
case IQmake_to_decimal as [d'|]; [|now simpl].
case d' as [i f|]; [|now simpl].
now intros H; injection 1 as <-.
- case r as [z|q| |]; [|case q as[num den]|now simpl..];
(case r' as [z'| | |]; [|now simpl..]);
(case z' as [p e| | |]; [|now simpl..]).
+ case (Z.eq_dec p 10); [intros->|intro Hp].
2:{ revert Hp; case p; [now simpl|intro d0..];
(case d0; [intro d1..|]; [now simpl| |now simpl];
case d1; [intro d2..|]; [|now simpl..];
case d2; [intro d3..|]; [now simpl| |now simpl];
now case d3). }
case z as [| |p|p]; [now simpl|..]; injection 1 as <-.
* now unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to.
* unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to; simpl.
now rewrite Unsigned.of_to.
* unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to; simpl.
now rewrite Unsigned.of_to.
+ case (Z.eq_dec p 10); [intros->|intro Hp].
2:{ revert Hp; case p; [now simpl|intro d0..];
(case d0; [intro d1..|]; [now simpl| |now simpl];
case d1; [intro d2..|]; [|now simpl..];
case d2; [intro d3..|]; [now simpl| |now simpl];
now case d3). }
generalize (of_IQmake_to_decimal num den).
case IQmake_to_decimal as [d'|]; [|now simpl].
case d' as [i f|]; [|now simpl].
intros H; injection 1 as <-.
unfold of_decimal; simpl.
change (match f with Nil => _ | _ => _ end) with (of_decimal (Decimal i f)).
rewrite H; clear H.
now unfold Z.of_uint; rewrite Unsigned.of_to.
- case r as [z|q| |]; [|case q as[num den]|now simpl..];
(case r' as [z'| | |]; [|now simpl..]);
(case z' as [p e| | |]; [|now simpl..]).
+ case (Z.eq_dec p 10); [intros->|intro Hp].
2:{ revert Hp; case p; [now simpl|intro d0..];
(case d0; [intro d1..|]; [now simpl| |now simpl];
case d1; [intro d2..|]; [|now simpl..];
case d2; [intro d3..|]; [now simpl| |now simpl];
now case d3). }
case z as [| |p|p]; [now simpl|..]; injection 1 as <-.
* now unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to.
* unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to; simpl.
now rewrite Unsigned.of_to.
* unfold of_decimal; simpl; unfold Z.of_uint; rewrite Unsigned.of_to; simpl.
now rewrite Unsigned.of_to.
+ case (Z.eq_dec p 10); [intros->|intro Hp].
2:{ revert Hp; case p; [now simpl|intro d0..];
(case d0; [intro d1..|]; [now simpl| |now simpl];
case d1; [intro d2..|]; [|now simpl..];
case d2; [intro d3..|]; [now simpl| |now simpl];
now case d3). }
generalize (of_IQmake_to_decimal num den).
case IQmake_to_decimal as [d'|]; [|now simpl].
case d' as [i f|]; [|now simpl].
intros H; injection 1 as <-.
unfold of_decimal; simpl.
change (match f with Nil => _ | _ => _ end) with (of_decimal (Decimal i f)).
rewrite H; clear H.
now unfold Z.of_uint; rewrite Unsigned.of_to.
Qed.
Lemma to_of (d:decimal) : to_decimal (of_decimal d) = Some (dnorm d).
Proof.
case d as [i f|i f e].
- unfold of_decimal; simpl.
case (uint_eq_dec f Nil); intro Hf.
+ rewrite Hf; clear f Hf.
unfold to_decimal; simpl.
rewrite IZ_to_Z_IZ_of_Z, DecimalZ.to_of.
case i as [i|i]; [now simpl|]; simpl.
rewrite app_nil_r.
case (uint_eq_dec (nzhead i) Nil); [now intros->|intro Hi].
now rewrite (unorm_nzhead _ Hi); revert Hi; case nzhead.
+ set (r := IRQ _).
set (m := match f with Nil => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
unfold to_decimal; simpl.
unfold IQmake_to_decimal; simpl.
set (n := Nat.iter _ _ _).
case (Pos.eq_dec n 1); intro Hn.
exfalso; apply Hf.
{ now apply nb_digits_0; revert Hn; unfold n; case nb_digits. }
clear m; set (m := match n with 1%positive | _ => _ end).
replace m with (QArith_base.IQmake_to_decimal (Z.of_int (app_int i f)) n).
2:{ now unfold m; revert Hn; case n. }
unfold QArith_base.IQmake_to_decimal, n; simpl.
rewrite nztail_to_uint_pow10.
clear r; set (r := if _ <? _ then Some (Decimal _ _) else Some _).
clear m; set (m := match nb_digits f with 0 => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
rewrite DecimalZ.to_of, abs_norm, abs_app_int.
case Nat.ltb_spec; intro Hnf.
* rewrite (del_tail_app_int_exact _ _ Hnf).
rewrite (del_head_app_int_exact _ _ Hnf).
now rewrite (dnorm_i_exact _ _ Hnf).
* rewrite (unorm_app_r _ _ Hnf).
rewrite (iter_D0_unorm _ Hf).
now rewrite dnorm_i_exact'.
- unfold of_decimal; simpl.
rewrite <-(DecimalZ.to_of e).
case (Z.of_int e); clear e; [|intro e..]; simpl.
+ case (uint_eq_dec f Nil); intro Hf.
* rewrite Hf; clear f Hf.
unfold to_decimal; simpl.
rewrite IZ_to_Z_IZ_of_Z, DecimalZ.to_of.
case i as [i|i]; [now simpl|]; simpl.
rewrite app_nil_r.
case (uint_eq_dec (nzhead i) Nil); [now intros->|intro Hi].
now rewrite (unorm_nzhead _ Hi); revert Hi; case nzhead.
* set (r := IRQ _).
set (m := match f with Nil => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
unfold to_decimal; simpl.
unfold IQmake_to_decimal; simpl.
set (n := Nat.iter _ _ _).
case (Pos.eq_dec n 1); intro Hn.
exfalso; apply Hf.
{ now apply nb_digits_0; revert Hn; unfold n; case nb_digits. }
clear m; set (m := match n with 1%positive | _ => _ end).
replace m with (QArith_base.IQmake_to_decimal (Z.of_int (app_int i f)) n).
2:{ now unfold m; revert Hn; case n. }
unfold QArith_base.IQmake_to_decimal, n; simpl.
rewrite nztail_to_uint_pow10.
clear r; set (r := if _ <? _ then Some (Decimal _ _) else Some _).
clear m; set (m := match nb_digits f with 0 => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
rewrite DecimalZ.to_of, abs_norm, abs_app_int.
case Nat.ltb_spec; intro Hnf.
-- rewrite (del_tail_app_int_exact _ _ Hnf).
rewrite (del_head_app_int_exact _ _ Hnf).
now rewrite (dnorm_i_exact _ _ Hnf).
-- rewrite (unorm_app_r _ _ Hnf).
rewrite (iter_D0_unorm _ Hf).
now rewrite dnorm_i_exact'.
+ set (i' := match i with Pos _ => _ | _ => _ end).
set (m := match Pos.to_uint e with Nil => _ | _ => _ end).
replace m with (DecimalExp i' f (Pos (Pos.to_uint e))).
2:{ unfold m; generalize (Unsigned.to_uint_nonzero e).
now case Pos.to_uint; [|intro u; case u|..]. }
unfold i'; clear i' m.
case (uint_eq_dec f Nil); intro Hf.
* rewrite Hf; clear f Hf.
unfold to_decimal; simpl.
rewrite IZ_to_Z_IZ_of_Z, DecimalZ.to_of.
case i as [i|i]; [now simpl|]; simpl.
rewrite app_nil_r.
case (uint_eq_dec (nzhead i) Nil); [now intros->|intro Hi].
now rewrite (unorm_nzhead _ Hi); revert Hi; case nzhead.
* set (r := IRQ _).
set (m := match f with Nil => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
unfold to_decimal; simpl.
unfold IQmake_to_decimal; simpl.
set (n := Nat.iter _ _ _).
case (Pos.eq_dec n 1); intro Hn.
exfalso; apply Hf.
{ now apply nb_digits_0; revert Hn; unfold n; case nb_digits. }
clear m; set (m := match n with 1%positive | _ => _ end).
replace m with (QArith_base.IQmake_to_decimal (Z.of_int (app_int i f)) n).
2:{ now unfold m; revert Hn; case n. }
unfold QArith_base.IQmake_to_decimal, n; simpl.
rewrite nztail_to_uint_pow10.
clear r; set (r := if _ <? _ then Some (Decimal _ _) else Some _).
clear m; set (m := match nb_digits f with 0 => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
rewrite DecimalZ.to_of, abs_norm, abs_app_int.
case Nat.ltb_spec; intro Hnf.
-- rewrite (del_tail_app_int_exact _ _ Hnf).
rewrite (del_head_app_int_exact _ _ Hnf).
now rewrite (dnorm_i_exact _ _ Hnf).
-- rewrite (unorm_app_r _ _ Hnf).
rewrite (iter_D0_unorm _ Hf).
now rewrite dnorm_i_exact'.
+ case (uint_eq_dec f Nil); intro Hf.
* rewrite Hf; clear f Hf.
unfold to_decimal; simpl.
rewrite IZ_to_Z_IZ_of_Z, DecimalZ.to_of.
case i as [i|i]; [now simpl|]; simpl.
rewrite app_nil_r.
case (uint_eq_dec (nzhead i) Nil); [now intros->|intro Hi].
now rewrite (unorm_nzhead _ Hi); revert Hi; case nzhead.
* set (r := IRQ _).
set (m := match f with Nil => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
unfold to_decimal; simpl.
unfold IQmake_to_decimal; simpl.
set (n := Nat.iter _ _ _).
case (Pos.eq_dec n 1); intro Hn.
exfalso; apply Hf.
{ now apply nb_digits_0; revert Hn; unfold n; case nb_digits. }
clear m; set (m := match n with 1%positive | _ => _ end).
replace m with (QArith_base.IQmake_to_decimal (Z.of_int (app_int i f)) n).
2:{ now unfold m; revert Hn; case n. }
unfold QArith_base.IQmake_to_decimal, n; simpl.
rewrite nztail_to_uint_pow10.
clear r; set (r := if _ <? _ then Some (Decimal _ _) else Some _).
clear m; set (m := match nb_digits f with 0 => _ | _ => _ end).
replace m with r; [unfold r|now unfold m; revert Hf; case f].
rewrite DecimalZ.to_of, abs_norm, abs_app_int.
case Nat.ltb_spec; intro Hnf.
-- rewrite (del_tail_app_int_exact _ _ Hnf).
rewrite (del_head_app_int_exact _ _ Hnf).
now rewrite (dnorm_i_exact _ _ Hnf).
-- rewrite (unorm_app_r _ _ Hnf).
rewrite (iter_D0_unorm _ Hf).
now rewrite dnorm_i_exact'.
Qed.
(** Some consequences *)
Lemma to_decimal_inj q q' :
to_decimal q <> None -> to_decimal q = to_decimal q' -> q = q'.
Proof.
intros Hnone EQ.
generalize (of_to q) (of_to q').
rewrite <-EQ.
revert Hnone; case to_decimal; [|now simpl].
now intros d _ H1 H2; rewrite <-(H1 d eq_refl), <-(H2 d eq_refl).
Qed.
Lemma to_decimal_surj d : exists q, to_decimal q = Some (dnorm d).
Proof.
exists (of_decimal d). apply to_of.
Qed.
Lemma of_decimal_dnorm d : of_decimal (dnorm d) = of_decimal d.
Proof. now apply to_decimal_inj; rewrite !to_of; [|rewrite dnorm_involutive]. Qed.
Lemma of_inj d d' : of_decimal d = of_decimal d' -> dnorm d = dnorm d'.
Proof.
intro H.
apply (@f_equal _ _ (fun x => match x with Some x => x | _ => d end)
(Some (dnorm d)) (Some (dnorm d'))).
now rewrite <- !to_of, H.
Qed.
Lemma of_iff d d' : of_decimal d = of_decimal d' <-> dnorm d = dnorm d'.
Proof.
split. apply of_inj. intros E. rewrite <- of_decimal_dnorm, E.
apply of_decimal_dnorm.
Qed.
|