File: int_pow.v

package info (click to toggle)
coq-math-classes 9.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,120 kB
  • sloc: python: 22; makefile: 21; sh: 2
file content (429 lines) | stat: -rw-r--r-- 14,603 bytes parent folder | download | duplicates (2)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
Require
  MathClasses.theory.naturals MathClasses.orders.semirings MathClasses.orders.integers MathClasses.orders.dec_fields.
From Coq Require Import Ring Field.
Require Import
  MathClasses.interfaces.abstract_algebra MathClasses.interfaces.naturals MathClasses.interfaces.integers
  MathClasses.interfaces.additional_operations MathClasses.interfaces.orders
  MathClasses.theory.nat_pow MathClasses.theory.int_abs MathClasses.theory.dec_fields.

(* * Properties of Int Pow *)
Section int_pow_properties.
Context `{DecField A} `{∀ x y, Decision (x = y)} `{Integers B} `{!IntPowSpec A B ipw}.

Add Field A : (dec_fields.stdlib_field_theory A).
Add Ring B : (rings.stdlib_ring_theory B).

Global Instance: Proper ((=) ==> (=) ==> (=)) ((^) : A → B → A) | 0.
Proof int_pow_proper.

Global Instance int_pow_mor_1: ∀ x : A, Setoid_Morphism (x^) | 0.
Proof. split; try apply _. Qed.

Global Instance int_pow_mor_2: ∀ n : B, Setoid_Morphism (^n) | 0.
Proof. split; try apply _. solve_proper. Qed.

Lemma int_pow_S_nonneg `{Apart B} `{!TrivialApart B} `{!FullPseudoSemiRingOrder (A:=B) Ble Blt} (x : A) (n : B) :
  0 ≤ n → x ^ (1+n) = x * x ^ n.
Proof.
  intros En. destruct (decide (x = 0)) as [Ex | Ex].
   rewrite Ex. rewrite int_pow_base_0. ring.
   intros E. destruct semirings.not_le_1_0.
   rewrite <-E. now apply semirings.nonneg_plus_le_compat_r.
  now rewrite int_pow_S.
Qed.

Lemma int_pow_negate (x : A) (n : B) : x ^ (-n) = /(x ^ n).
Proof.
  destruct (decide (x = 0)) as [Ex | Ex].
   rewrite Ex.
   destruct (decide (n = 0)) as [En | En].
    now rewrite En, rings.negate_0, int_pow_0, dec_recip_1.
   rewrite 2!int_pow_base_0; trivial.
    now rewrite dec_recip_0.
   now apply rings.flip_negate_ne_0.
  revert n. apply biinduction.
    solve_proper.
   now rewrite rings.negate_0, int_pow_0, dec_recip_1.
  intros n.
  setoid_replace (-n) with (1 - (1 + n)) by ring.
  rewrite 2!int_pow_S, dec_recip_distr; trivial.
  split; intros E.
   rewrite <-E. now field.
  rewrite E, associativity, dec_recip_inverse; trivial.
  ring.
Qed.

Lemma int_pow_negate_alt (x : A) (n : B) : x ^ n = /(x ^ (-n)).
Proof.
  rewrite <-int_pow_negate.
  now rewrite rings.negate_involutive.
Qed.

Lemma int_pow_mult (x y : A) (n : B) : (x * y) ^ n = x ^ n * y ^ n.
Proof.
  destruct (decide (x * y = 0)) as [Exy | Exy].
   rewrite Exy.
   destruct (decide (n = 0)) as [En | En].
    rewrite En, 3!int_pow_0. ring.
   destruct (zero_product x y Exy) as [E|E]; rewrite E, int_pow_base_0; trivial; ring.
  revert n. apply biinduction.
    solve_proper.
   rewrite 3!int_pow_0. ring.
  intros n.
  rewrite 3!int_pow_S; trivial.
    split; intros E.
     rewrite E. ring.
    apply (rings.left_cancellation_ne_0 (.*.) (x * y)); trivial.
    rewrite E. ring.
   intros E. apply Exy. rewrite E. ring.
  intros E. apply Exy. rewrite E. ring.
Qed.

Lemma int_pow_recip (x : A) (n : B) : (/x) ^ n = /(x ^ n).
Proof.
  destruct (decide (x = 0)) as [Ex | Ex].
   rewrite Ex, dec_recip_0.
   destruct (decide (n = 0)) as [En | En].
    now rewrite En, int_pow_0, dec_recip_1.
   now rewrite int_pow_base_0, dec_recip_0.
  revert n. apply biinduction.
    solve_proper.
   now rewrite 2!int_pow_0, dec_recip_1.
  intros n.
  assert (/x ≠ 0) by now apply dec_recip_ne_0.
  rewrite 2!int_pow_S, dec_recip_distr; trivial.
  split; intros E.
   now rewrite E.
  now apply (rings.left_cancellation_ne_0 (.*.) (/x)).
Qed.

Lemma int_pow_nat_pow `{Naturals N} `{!NatPowSpec A N pw} {f : N → B} `{!SemiRing_Morphism f} (x : A) (n : N) :
  x ^ (f n) = x ^ n.
Proof.
  revert n. apply naturals.induction.
    solve_proper.
   now rewrite rings.preserves_0, int_pow_0, nat_pow_0.
  intros n E.
  rewrite rings.preserves_plus, rings.preserves_1.
  rewrite int_pow_S_nonneg, nat_pow_S.
   now rewrite E.
  apply semirings.preserves_nonneg.
  now apply naturals.nat_nonneg.
Qed.

Global Instance int_pow_1: RightIdentity (^) (1:B).
Proof.
  intro. assert ((1:B) = 1 + 0) as E by ring. rewrite E.
  rewrite int_pow_S_nonneg, int_pow_0; [ring | reflexivity].
Qed.

Lemma int_pow_2 x : x ^ (2:B) = x * x.
Proof. now rewrite int_pow_S_nonneg, int_pow_1 by solve_propholds. Qed.

Lemma int_pow_3 x : x ^ (3:B) = x * (x * x).
Proof. now rewrite int_pow_S_nonneg, int_pow_2 by solve_propholds. Qed.

Lemma int_pow_4 x : x ^ (4:B) = x * (x * (x * x)).
Proof. now rewrite int_pow_S_nonneg, int_pow_3 by solve_propholds. Qed.

Global Instance int_pow_base_1: LeftAbsorb (^) (1:A).
Proof.
  red. apply biinduction.
    solve_proper.
   now apply int_pow_0.
  intros n. rewrite int_pow_S, left_identity.
   easy.
  now apply (rings.is_ne_0 1).
Qed.

Lemma int_pow_exp_plus (n m : B) (x : A) :
  x ≠ 0 → x ^ (n + m) = x ^ n * x ^ m.
Proof.
  intros nonneg.
  revert n. apply biinduction.
    solve_proper.
   rewrite int_pow_0, left_identity. ring.
  intros n. rewrite <-associativity, 2!int_pow_S; trivial.
  split; intros E.
   rewrite E. ring.
  apply (rings.left_cancellation_ne_0 (.*.) x); trivial.
  rewrite E. ring.
Qed.

Instance int_pow_ne_0 (x : A) (n : B) : PropHolds (x ≠ 0) → PropHolds (x ^ n ≠ 0).
Proof.
  intros nonneg. unfold PropHolds.
  revert n. apply biinduction.
    solve_proper.
   rewrite int_pow_0. apply (rings.is_ne_0 1).
  intros n. rewrite int_pow_S; trivial.
  split; intros E1 E2; destruct E1.
   apply (left_cancellation (.*.) x).
   now rewrite right_absorb.
  rewrite E2. ring.
Qed.

Lemma int_pow_exp_mult (x : A) (n m : B) :
  x ^ (n * m) = (x ^ n) ^ m.
Proof.
  destruct (decide (x = 0)) as [Ex|Ex].
   rewrite Ex.
   destruct (decide (n = 0)) as [En|En].
    rewrite En, left_absorb, int_pow_0.
    now rewrite left_absorb.
   destruct (decide (m = 0)) as [Em|Em].
    now rewrite Em, right_absorb, 2!int_pow_0.
   rewrite 3!int_pow_base_0; try easy.
   intros E. now destruct (zero_product n m E).
  revert m. apply biinduction.
    solve_proper.
   rewrite right_absorb. now rewrite 2!int_pow_0.
  intros m. split; intros E.
   rewrite int_pow_S, <-E.
    rewrite distribute_l, right_identity.
    now rewrite int_pow_exp_plus.
   now apply int_pow_ne_0.
  rewrite int_pow_S in E.
  rewrite distribute_l, right_identity, int_pow_exp_plus in E.
    apply (rings.left_cancellation_ne_0 (.*.) (x ^ n)).
     now apply int_pow_ne_0.
    now rewrite E.
   easy.
  now apply int_pow_ne_0.
Qed.

Context `{Apart A} `{!TrivialApart A} `{!FullPseudoSemiRingOrder (A:=A) Ale Alt}.
Context `{Apart B} `{!TrivialApart B} `{!FullPseudoSemiRingOrder (A:=B) Ble Blt}.

Instance int_pow_pos (x : A) (n : B) : PropHolds (0 < x) → PropHolds (0 < x ^ n).
Proof.
  intros nonneg. unfold PropHolds.
  revert n. apply biinduction.
    solve_proper.
   intros. rewrite int_pow_0. now apply semirings.lt_0_1.
  intros n; split; intros E.
   rewrite int_pow_S.
    now apply pos_mult_compat.
   apply not_symmetry. now apply orders.lt_ne.
  apply (strictly_order_reflecting (x *.)).
  rewrite <-int_pow_S.
   now rewrite right_absorb.
  apply not_symmetry. now apply orders.lt_ne.
Qed.

Instance int_pow_nonneg (x : A) (n : B) : PropHolds (0 ≤ x) → PropHolds (0 ≤ x ^ n).
Proof.
  intros E1. red in E1.
  destruct (orders.le_equiv_lt _ _ E1) as [E2|E2].
   rewrite <-E2.
   destruct (decide (n = 0)) as [En|En].
    rewrite En.
    rewrite int_pow_0.
    apply semirings.le_0_1.
   unfold PropHolds. now rewrite int_pow_base_0.
  now apply orders.lt_le, int_pow_pos.
Qed.

Lemma int_pow_ge_1 (x : A) (n : B) : 1 ≤ x → 0 ≤ n → 1 ≤ x ^ n.
Proof.
  intros E1 E2. revert n E2. apply integers.induction_nonneg; trivial.
    solve_proper.
   now rewrite int_pow_0.
  intros.
  rewrite int_pow_S.
   rewrite <-rings.mult_1_r.
   apply semirings.mult_le_compat; try apply semirings.le_0_1; auto.
  apply orders.lt_ne_flip.
  apply orders.lt_le_trans with 1; trivial.
  now apply semirings.lt_0_1.
Qed.

Lemma int_pow_gt_1 (x : A) (n : B) : 1 < x → 0 < n → 1 < x ^ n.
Proof.
  intros Ex En.
  apply nat_int.lt_iff_S_le in En.
  destruct (semirings.decompose_le En) as [z [Ez1 Ez2]]. ring_simplify in Ez2.
  rewrite Ez2. clear En Ez2 n.
  revert z Ez1. apply integers.induction_nonneg; try assumption.
    solve_proper.
   now rewrite left_identity, right_identity.
  intros n En E2.
  rewrite <-associativity, int_pow_S.
   apply semirings.gt_1_mult_lt_compat_l; auto.
   transitivity (1:A); [apply semirings.lt_0_1 | assumption].
  apply orders.lt_ne_flip.
  apply orders.le_lt_trans with 1; trivial.
  now apply semirings.le_0_1.
Qed.

(* Making these instances Global is not useful, we don't have PropHolds (1 ≤ x)
  instances and it will only slow down instance resolution (it increases the
  compilation time of dyadics from 1:35 to 2:28). *)
Instance int_pow_exp_le:
  ∀ x : A, PropHolds (1 ≤ x) → OrderPreserving (x^).
Proof.
  repeat (split; try apply _).
  assert (0 < x) by (apply orders.lt_le_trans with 1; [solve_propholds | easy]).
  intros n m E.
  destruct (semirings.decompose_le E) as [z [Ea Eb]].
  rewrite Eb.
  rewrite int_pow_exp_plus by now apply orders.lt_ne_flip.
  rewrite <-rings.mult_1_r at 1.
  apply (order_preserving (x ^ n *.)).
  now apply int_pow_ge_1.
Qed.

Instance int_pow_exp_lt:
  ∀ x : A, PropHolds (1 < x) → StrictlyOrderPreserving (x^).
Proof.
  repeat (split; try apply _).
  assert (0 < x) by (apply orders.le_lt_trans with 1; [solve_propholds | easy]).
  intros n m E.
  apply nat_int.lt_iff_plus_1_le in E.
  destruct (semirings.decompose_le E) as [z [Ea Eb]].
  rewrite Eb.
  rewrite <-associativity, int_pow_exp_plus by now apply orders.lt_ne_flip.
  rewrite <-(rings.mult_1_r (x ^ n)) at 1.
  apply (strictly_order_preserving (x^n *.)).
  apply int_pow_gt_1; trivial.
  now apply nat_int.le_iff_lt_S.
Qed.

Instance int_pow_exp_le_back:
  ∀ x : A, PropHolds (1 < x) → OrderReflecting (x^).
Proof.
  split; try apply _. intros n m E1.
  destruct (total (≤) n m) as [E2|E2]; trivial.
  destruct (orders.le_equiv_lt _ _ E2) as [E3|E3].
   now rewrite E3.
  contradict E1.
  apply orders.lt_not_le_flip.
  now apply (strictly_order_preserving (x^)).
Qed.

Instance int_pow_exp_lt_back:
  ∀ x : A, PropHolds (1 < x) → StrictlyOrderReflecting (x^).
Proof. intros ? E1. apply _. Qed.

Instance int_pow_inj:
  ∀ x : A, PropHolds (1 < x) → Injective (x^).
Proof.
  repeat (split; try apply _). intros n m E.
  apply (antisymmetry (≤)); apply (order_reflecting (x^)); trivial; rewrite E; reflexivity.
Qed.
End int_pow_properties.

(* Due to bug #2528 *)
#[global]
Hint Extern 18 (PropHolds (_ ^ _ ≠ 0)) => eapply @int_pow_ne_0 : typeclass_instances.
#[global]
Hint Extern 18 (PropHolds (0 ≤ _ ^ _)) => eapply @int_pow_nonneg : typeclass_instances.
#[global]
Hint Extern 18 (PropHolds (0 < _ ^ _)) => eapply @int_pow_pos : typeclass_instances.

Section preservation.
  Context
    `{Integers B}
    `{DecField A1} `{∀ x y : A1, Decision (x = y)} `{!IntPowSpec A1 B ip1}
    `{DecField A2} `{∀ x y : A2, Decision (x = y)} `{!IntPowSpec A2 B ip2}
    {f : A1 → A2} `{!SemiRing_Morphism f}.

  Add Ring B2 : (rings.stdlib_ring_theory B).

  Lemma preserves_int_pow x (n : B) : f (x ^ n) = (f x) ^ n.
  Proof.
    destruct (decide (x = 0)) as [Ex | Ex].
     rewrite Ex, rings.preserves_0.
     destruct (decide (n = 0)) as [En|En].
      rewrite En, 2!int_pow_0.
      now apply rings.preserves_1.
     rewrite 2!int_pow_base_0; trivial.
     now apply rings.preserves_0.
    revert n. apply biinduction.
      solve_proper.
     rewrite int_pow_0, int_pow_0.
     now apply rings.preserves_1.
    intros n.
    assert (f x ≠ 0) by now apply rings.injective_ne_0.
    rewrite 2!int_pow_S, rings.preserves_mult; trivial.
    split; intros E.
     now rewrite E.
    now apply (left_cancellation (.*.) (f x)).
  Qed.
End preservation.

Section exp_preservation.
  Context `{Field A} `{∀ x y : A, Decision (x = y)}
   `{Integers B1} `{Integers B2} `{!IntPowSpec A B1 pw1} `{!IntPowSpec A B2 pw2}
    {f : B1 → B2} `{!SemiRing_Morphism f}.

  Lemma preserves_int_pow_exp x (n : B1) : x ^ (f n) = x ^ n.
  Proof.
    destruct (decide (x = 0)) as [Ex | Ex].
     rewrite Ex.
     destruct (decide (n = 0)) as [En|En].
      now rewrite En, rings.preserves_0, 2!int_pow_0.
     rewrite 2!int_pow_base_0; try easy.
     now apply rings.injective_ne_0.
    revert n. apply biinduction.
      solve_proper.
     rewrite rings.preserves_0.
     now rewrite 2!int_pow_0.
    intros n.
    rewrite rings.preserves_plus, rings.preserves_1.
    rewrite 2!int_pow_S by trivial.
    split; intros E.
     now rewrite E.
    now apply (rings.left_cancellation_ne_0 (.*.) x).
  Qed.
End exp_preservation.

(* Very slow default implementation by translation into Peano *)
Section int_pow_default.
  Context `{DecField A} `{∀ x y, Decision (x = y)}
    `{Integers B} `{Apart B} `{!TrivialApart B} `{!FullPseudoSemiRingOrder (A:=B) Ble Blt}.

  Add Ring B3 : (rings.stdlib_ring_theory B).

  Global Instance int_pow_default: Pow A B | 10 := λ x n,
    match (decide_rel (≤) 0 n) with
    | left _ => x ^ int_abs B nat n
    | right _ => /x ^ int_abs B nat n
    end.

  Global Instance: IntPowSpec A B int_pow_default.
  Proof.
    split; unfold pow, int_pow_default.
       intros ? ? E1 ? ? E2.
       now (case (decide_rel); case (decide_rel); rewrite E1, E2).
      intros x. case (decide_rel); intros E.
       now rewrite int_abs_0.
      now destruct E.
     intros n ?. case (decide_rel); intros E.
      now apply nat_pow_base_0, int_abs_ne_0.
     rewrite nat_pow_base_0.
     apply dec_recip_0.
     now apply int_abs_ne_0.
    intros x n E. case (decide_rel); case (decide_rel); intros E1 E2.
       now rewrite int_abs_nonneg_plus, int_abs_1 by (auto;solve_propholds).
      setoid_replace n with (-1 : B).
       rewrite rings.plus_negate_r, int_abs_0, nat_pow_0.
       rewrite int_abs_negate, int_abs_1, right_identity.
       symmetry. now apply dec_recip_inverse.
      apply (antisymmetry (≤)).
       apply orders.not_le_lt_flip in E1.
       apply nat_int.lt_iff_plus_1_le in E1.
       apply (order_reflecting (+1)).
       now ring_simplify.
      apply (order_reflecting (1+)). now rewrite rings.plus_negate_r.
     destruct E2. apply semirings.nonneg_plus_compat; [solve_propholds | assumption].
     rewrite <-int_abs_negate, <-(int_abs_negate n).
     setoid_replace (-n) with (1 - (1 + n)) by ring.
     rewrite (int_abs_nonneg_plus 1 (-(1 + n))), int_abs_1.
       rewrite nat_pow_S.
       rewrite dec_recip_distr, associativity.
       now rewrite dec_recip_inverse, left_identity.
      now apply (rings.is_nonneg 1).
     now apply rings.flip_nonpos_negate, orders.le_flip.
  Qed.
End int_pow_default.