-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commutative_ring_binomial.v
326 lines (274 loc) · 8.03 KB
/
Commutative_ring_binomial.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
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
(*
(C) Copyright 2010, COQTAIL team
Project Info: http://sourceforge.net/projects/coqtail/
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
*)
Require Import Type_class_definition.
Require Import Type_class_instance.
Require Import Coq.Relations.Relation_Definitions.
Require Import Lia.
Require Import Arith.
Require Import Nbinomial.
Require Import Setoid.
Definition iter_l (A : Type) (op : operation A) (neutral : A) (x : A) :=
fix s n := match n with
| O => neutral
| S n' => op x (s n')
end.
Definition iter_r (A : Type) (op : operation A) (neutral : A) (x : A) :=
fix s n := match n with
| O => neutral
| S n' => op (s n') x
end.
Definition iter1 (A : Type) (op : operation A) (f : nat -> A) :=
fix s n := match n with
| O => f O
| S n' => op (f n) (s n')
end.
Section Commutative_Ring.
Variable X : Type.
Variable eqr : relation X.
Variable add mul : operation X.
Variable zero one : X.
Variable CR : Ring_Commutative X eqr add mul zero one.
Definition CRpow : X -> nat -> X := iter_r X mul one.
Definition CRsum : (nat -> X) -> nat -> X := iter1 X add.
Definition CRnatmul : nat -> X -> X := fun n x => iter_l X add zero x n.
Notation " a ^^ b " := (CRpow a b) (at level 30, right associativity).
Notation " a * b " := (mul a b).
Notation " a == b " := (eqr a b) (at level 90, no associativity).
Notation " a + b " := (add a b).
Notation " a ** b " := (CRnatmul a b) (at level 60).
Definition newton_sum n a b : X :=
CRsum (fun k => (Nbinomial n k) ** (a ^^ k) * (b ^^ (n - k))) n.
Definition opp : X -> X.
Proof.
intros x.
destruct (group_reverse x) as [y ?].
exact y.
Defined.
Definition sub : X -> X -> X := fun a b => add a (opp b).
Add Setoid X eqr monoid_setoid as setoid_X.
Add Morphism add with signature eqr ==> eqr ==> eqr as add_wd.
intros.
transitivity (x + y0).
apply monoid_eq_compat_l; assumption.
apply monoid_eq_compat_r; assumption.
Qed.
Add Morphism mul with signature eqr ==> eqr ==> eqr as mul_wd.
intros.
transitivity (x * y0).
apply monoid_eq_compat_l; assumption.
apply monoid_eq_compat_r; assumption.
Qed.
Add Morphism opp with signature eqr ==> eqr as opp_wd.
Proof.
intros.
unfold opp.
destruct (group_reverse x) as [x' ?].
destruct (group_reverse y) as [y' ?].
transitivity (zero + y'); [ | apply monoid_iden_l].
transitivity (x' + x + y'); [ | apply monoid_eq_compat_r; rewrite op_comm; assumption].
transitivity (x' + y + y'); [ | apply monoid_eq_compat_r; apply monoid_eq_compat_l; symmetry; assumption].
transitivity (zero + x').
symmetry; apply monoid_iden_l.
transitivity (x' + x + x').
apply monoid_eq_compat_r; symmetry; transitivity (x + x'); auto; apply op_comm.
transitivity (x' + (y + y')).
transitivity (x' + (x + x')).
symmetry; apply op_assoc.
apply monoid_eq_compat_l; transitivity zero; auto with relations.
apply op_assoc.
Qed.
Lemma Xring : @ring_theory X zero one add mul sub opp eqr.
Proof.
split; intros.
apply monoid_iden_l.
apply op_comm.
apply op_assoc.
apply monoid_iden_l.
apply op_comm.
apply op_assoc.
apply ring_distributive_l.
reflexivity.
unfold opp.
destruct (group_reverse x) as [y ?].
assumption.
Qed.
Add Ring X : Xring.
Lemma CRsum_simpl f n : CRsum f (S n) = f (S n) + CRsum f n.
Proof.
reflexivity.
Qed.
Lemma CRsum_simpl_r f n : CRsum f (S n) == CRsum f n + f (S n).
Proof.
intros; simpl; ring.
Qed.
Lemma CRsum_reindex : forall n f, f O + CRsum (fun k => f (S k)) n == CRsum f (S n).
Proof.
intros n f.
induction n.
simpl; ring.
do 2 rewrite CRsum_simpl.
rewrite <- IHn.
ring.
Qed.
Lemma CRsum_eq_compat_weak : forall a b n, (forall n, a n == b n) -> CRsum a n == CRsum b n.
Proof.
intros a b n H.
induction n.
simpl; apply H.
simpl.
rewrite IHn.
rewrite H.
reflexivity.
Qed.
Lemma CRsum_eq_compat : forall a b n, (forall i, i <= n -> a i == b i) -> CRsum a n == CRsum b n.
Proof.
intros a b n H.
induction n.
simpl; apply H.
constructor.
simpl.
rewrite IHn.
rewrite H.
reflexivity.
constructor.
intros; apply H; auto.
Qed.
Lemma CRsum_add_compat : forall a b n, CRsum (fun i => a i + b i) n == CRsum a n + CRsum b n.
Proof.
intros a b n.
induction n.
simpl; reflexivity.
simpl.
rewrite IHn.
ring.
Qed.
Lemma CRsum_scal_compat : forall x f n, x * CRsum f n == CRsum (fun n => x * f n) n.
Proof.
intros a b n.
induction n.
simpl; reflexivity.
simpl.
ring_simplify.
rewrite IHn.
reflexivity.
Qed.
Lemma CRpow_simpl : forall a n, a ^^ (S n) = a ^^ n * a.
Proof.
reflexivity.
Qed.
Lemma CRadd_eq_compat : forall a b c d, a == c -> b == d -> a + b == c + d.
Proof.
intros ? ? ? ? H H'.
rewrite H; rewrite H'.
ring.
Qed.
Lemma CRmul_scal_compat : forall a b n, n ** a * b == a * (n ** b).
Proof.
intros a b n.
induction n.
simpl; ring.
simpl.
rewrite IHn.
ring.
Qed.
Lemma CRscal_eq_compat : forall a b n, a == b -> n ** a == n ** b.
Proof.
intros a b n H.
induction n.
simpl; ring.
simpl.
rewrite IHn.
rewrite H.
ring.
Qed.
Lemma CRscal_mult_scal_one : forall a n, (n ** one) * a == n ** a.
Proof.
intros a n.
induction n.
simpl; ring.
simpl.
rewrite <- IHn.
ring.
Qed.
Lemma CRscal_add_eq_compat : forall a b n, (n ** a) + (n ** b) == n ** (a + b).
Proof.
intros a b n.
induction n.
simpl; ring.
simpl.
rewrite <- IHn.
ring.
Qed.
Lemma CRadd_scal_eq_compat : forall a n p, (n ** a) + (p ** a) == (n + p) ** a.
Proof.
intros a n p.
induction n.
simpl; ring.
simpl.
rewrite <- IHn.
ring.
Qed.
Theorem Newton : forall n a b, (a + b) ^^ n == newton_sum n a b.
Proof.
intros n a b.
induction n; [compute; ring | ].
destruct n; [compute; ring | ].
unfold newton_sum.
rewrite CRsum_simpl.
rewrite <- CRsum_reindex.
rewrite <- (CRsum_eq_compat (fun k =>
(Nbinomial (S n) k ** a ^^ S k * b ^^ (S (S n) - S k)) +
(Nbinomial (S n) (S k) ** a ^^ S k * b ^^ (S (S n) - S k)))).
rewrite CRsum_add_compat.
rewrite CRpow_simpl.
rewrite IHn.
rewrite ring_distributive_r.
unfold newton_sum.
do 2 (rewrite (op_comm (O:=mul)); rewrite CRsum_scal_compat).
assert (AP:forall a b c d e f, a == e + c -> b == d + f -> a + b == c + (d + (e + f)))
by (intros ? ? ? ? ? ? Hi Hj; rewrite Hi; rewrite Hj; ring);
apply AP; clear AP.
rewrite CRsum_simpl_r.
repeat rewrite Nbinomial_diag.
repeat rewrite Nat.sub_diag.
apply CRadd_eq_compat.
apply CRsum_eq_compat_weak.
intro.
rewrite <- CRmul_scal_compat.
apply CRscal_eq_compat.
rewrite CRpow_simpl.
simpl; ring.
simpl; ring.
rewrite <- CRsum_reindex.
apply CRadd_eq_compat.
simpl; repeat rewrite binomial_zero; ring.
apply CRsum_eq_compat; intros j Hj.
rewrite (Nat.sub_succ_l _ (S n)); [|lia].
simpl.
rewrite <- CRmul_scal_compat.
apply CRscal_eq_compat.
ring.
intros j Hj.
rewrite <- CRscal_mult_scal_one.
rewrite <- (CRscal_mult_scal_one _ (Nbinomial (S n) (S j))).
rewrite <- ring_distributive_l.
rewrite CRadd_scal_eq_compat.
rewrite Nbinomial_pascal; [ | lia].
rewrite CRscal_mult_scal_one.
reflexivity.
Qed.
End Commutative_Ring.