summaryrefslogtreecommitdiff
path: root/plugins/setoid_ring/Ncring.v
blob: 7789ba3eb3a094341735fbbbecb7cda9dd41cd20 (plain)
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
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2012     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

(* non commutative rings *)

Require Import Setoid.
Require Import BinPos.
Require Import BinNat.
Require Export Morphisms Setoid Bool.
Require Export ZArith_base.
Require Export Algebra_syntax.

Set Implicit Arguments.

Class Ring_ops(T:Type)
   {ring0:T}
   {ring1:T}
   {add:T->T->T}
   {mul:T->T->T}
   {sub:T->T->T}
   {opp:T->T}
   {ring_eq:T->T->Prop}.

Instance zero_notation(T:Type)`{Ring_ops T}:Zero T:= ring0. 
Instance one_notation(T:Type)`{Ring_ops T}:One T:= ring1.
Instance add_notation(T:Type)`{Ring_ops T}:Addition T:= add.
Instance mul_notation(T:Type)`{Ring_ops T}:@Multiplication T T:= mul.
Instance sub_notation(T:Type)`{Ring_ops T}:Subtraction T:= sub.
Instance opp_notation(T:Type)`{Ring_ops T}:Opposite T:= opp.
Instance eq_notation(T:Type)`{Ring_ops T}:@Equality T:= ring_eq.

Class Ring `{Ro:Ring_ops}:={
 ring_setoid: Equivalence _==_;
 ring_plus_comp: Proper (_==_ ==> _==_ ==>_==_) _+_;
 ring_mult_comp: Proper (_==_ ==> _==_ ==>_==_) _*_;
 ring_sub_comp: Proper (_==_ ==> _==_ ==>_==_) _-_;
 ring_opp_comp: Proper (_==_==>_==_) -_;
 ring_add_0_l    : forall x, 0 + x == x;
 ring_add_comm   : forall x y, x + y == y + x;
 ring_add_assoc  : forall x y z, x + (y + z) == (x + y) + z;
 ring_mul_1_l    : forall x, 1 * x == x;
 ring_mul_1_r    : forall x, x * 1 == x;
 ring_mul_assoc  : forall x y z, x * (y * z) == (x * y) * z;
 ring_distr_l    : forall x y z, (x + y) * z == x * z + y * z;
 ring_distr_r    : forall x y z, z * ( x + y) == z * x + z * y;
 ring_sub_def    : forall x y, x - y == x + -y;
 ring_opp_def    : forall x, x + -x == 0
}.
(* inutile! je sais plus pourquoi j'ai mis ca...
Instance ring_Ring_ops(R:Type)`{Ring R}
  :@Ring_ops R 0 1 addition multiplication subtraction opposite equality.
*)
Existing Instance ring_setoid.
Existing Instance ring_plus_comp.
Existing Instance ring_mult_comp.
Existing Instance ring_sub_comp.
Existing Instance ring_opp_comp.

Section Ring_power.

Context {R:Type}`{Ring R}.

 Fixpoint pow_pos (x:R) (i:positive) {struct i}: R :=
  match i with
  | xH => x
  | xO i => let p := pow_pos x i in p * p
  | xI i => let p := pow_pos x i in x * (p * p)
  end.

 Definition pow_N (x:R) (p:N) :=
  match p with
  | N0 => 1
  | Npos p => pow_pos x p
  end.

End Ring_power.

Definition ZN(x:Z):=
  match x with
    Z0 => N0
    |Zpos p | Zneg p => Npos p
end.

Instance power_ring {R:Type}`{Ring R} : Power:=
  {power x y := pow_N x (ZN y)}.

(** Interpretation morphisms definition*)

Class Ring_morphism (C R:Type)`{Cr:Ring C} `{Rr:Ring R}`{Rh:Bracket C R}:= {
    ring_morphism0    : [0] == 0;
    ring_morphism1    : [1] == 1;
    ring_morphism_add : forall x y, [x + y] == [x] + [y];
    ring_morphism_sub : forall x y, [x - y] == [x] - [y];
    ring_morphism_mul : forall x y, [x * y] == [x] * [y];
    ring_morphism_opp : forall  x, [-x] == -[x];
    ring_morphism_eq  : forall x y, x == y -> [x] == [y]}.

Section Ring.

Context {R:Type}`{Rr:Ring R}.

(* Powers *)

Lemma pow_pos_comm : forall  x j,  x * pow_pos x j == pow_pos x j * x.
Proof.
induction j; simpl. rewrite <- ring_mul_assoc.
rewrite <- ring_mul_assoc.
rewrite <- IHj. rewrite (ring_mul_assoc (pow_pos x j) x (pow_pos x j)).
rewrite <- IHj. rewrite <- ring_mul_assoc. reflexivity.
rewrite <- ring_mul_assoc. rewrite <- IHj.
rewrite ring_mul_assoc. rewrite IHj.
rewrite <- ring_mul_assoc. rewrite IHj. reflexivity. reflexivity.
Qed.

Lemma pow_pos_succ : forall  x j, pow_pos x (Pos.succ j) == x * pow_pos x j.
Proof.
induction j; simpl.
  rewrite IHj.
rewrite <- (ring_mul_assoc x (pow_pos x j) (x * pow_pos x j)).
rewrite (ring_mul_assoc (pow_pos x j) x  (pow_pos x j)).
  rewrite <- pow_pos_comm.
rewrite <- ring_mul_assoc. reflexivity.
reflexivity. reflexivity.
Qed.

Lemma pow_pos_add : forall  x i j,
  pow_pos x (i + j) == pow_pos x i * pow_pos x j.
Proof.
  intro x;induction i;intros.
  rewrite Pos.xI_succ_xO;rewrite <- Pos.add_1_r.
  rewrite <- Pos.add_diag;repeat rewrite <- Pos.add_assoc.
  repeat rewrite IHi.
  rewrite Pos.add_comm;rewrite Pos.add_1_r;
  rewrite pow_pos_succ.
  simpl;repeat rewrite ring_mul_assoc. reflexivity.
  rewrite <- Pos.add_diag;repeat rewrite <- Pos.add_assoc.
  repeat rewrite IHi. rewrite ring_mul_assoc. reflexivity.
  rewrite Pos.add_comm;rewrite Pos.add_1_r;rewrite pow_pos_succ.
   simpl. reflexivity.
 Qed.

 Definition id_phi_N (x:N) : N := x.

 Lemma pow_N_pow_N : forall  x n, pow_N x (id_phi_N n) == pow_N x n.
 Proof.
  intros; reflexivity.
 Qed.

 (** Identity is a morphism *)
 (*
 Instance IDmorph : Ring_morphism _ _ _  (fun x => x).
 Proof.
  apply (Build_Ring_morphism H6 H6 (fun x => x));intros;
  try reflexivity. trivial.
 Qed.
*)
 (** rings are almost rings*)
 Lemma ring_mul_0_l : forall  x, 0 * x == 0.
 Proof.
  intro x. setoid_replace (0*x) with ((0+1)*x + -x). 
  rewrite ring_add_0_l. rewrite ring_mul_1_l .
  rewrite ring_opp_def . fold zero. reflexivity.
  rewrite ring_distr_l . rewrite ring_mul_1_l .
  rewrite <- ring_add_assoc ; rewrite ring_opp_def .
  rewrite ring_add_comm ; rewrite ring_add_0_l ;reflexivity.
 Qed.

 Lemma ring_mul_0_r : forall  x, x * 0 == 0.
 Proof.
  intro x; setoid_replace (x*0)  with (x*(0+1) + -x).
  rewrite ring_add_0_l ; rewrite ring_mul_1_r .
  rewrite ring_opp_def ; fold zero; reflexivity.

  rewrite ring_distr_r ;rewrite ring_mul_1_r .
  rewrite <- ring_add_assoc ; rewrite ring_opp_def .
  rewrite ring_add_comm ; rewrite ring_add_0_l ;reflexivity.
 Qed.

 Lemma ring_opp_mul_l : forall x y, -(x * y) == -x * y.
 Proof.
  intros x y;rewrite <- (ring_add_0_l (- x * y)).
  rewrite ring_add_comm .
  rewrite <- (ring_opp_def (x*y)).
  rewrite ring_add_assoc .
  rewrite <- ring_distr_l.
  rewrite (ring_add_comm (-x));rewrite ring_opp_def .
  rewrite ring_mul_0_l;rewrite ring_add_0_l ;reflexivity.
 Qed.

Lemma ring_opp_mul_r : forall x y, -(x * y) == x * -y.
 Proof.
  intros x y;rewrite <- (ring_add_0_l (x * - y)).
  rewrite ring_add_comm .
  rewrite <- (ring_opp_def (x*y)).
  rewrite ring_add_assoc .
  rewrite <- ring_distr_r .
  rewrite (ring_add_comm (-y));rewrite ring_opp_def .
  rewrite ring_mul_0_r;rewrite ring_add_0_l ;reflexivity.
 Qed.

 Lemma ring_opp_add : forall x y, -(x + y) == -x + -y.
 Proof.
  intros x y;rewrite <- (ring_add_0_l  (-(x+y))).
  rewrite <- (ring_opp_def  x).
  rewrite <- (ring_add_0_l  (x + - x + - (x + y))).
  rewrite <- (ring_opp_def  y).
  rewrite (ring_add_comm  x).
  rewrite (ring_add_comm  y).
  rewrite <- (ring_add_assoc  (-y)).
  rewrite <- (ring_add_assoc  (- x)).
  rewrite (ring_add_assoc   y).
  rewrite (ring_add_comm  y).
  rewrite <- (ring_add_assoc   (- x)).
  rewrite (ring_add_assoc  y).
  rewrite (ring_add_comm  y);rewrite ring_opp_def .
  rewrite (ring_add_comm  (-x) 0);rewrite ring_add_0_l .
  rewrite ring_add_comm; reflexivity.
 Qed.

 Lemma ring_opp_opp : forall  x, - -x == x.
 Proof.
  intros x; rewrite <- (ring_add_0_l (- -x)).
  rewrite <- (ring_opp_def x).
  rewrite <- ring_add_assoc ; rewrite ring_opp_def .
  rewrite (ring_add_comm  x); rewrite ring_add_0_l . reflexivity.
 Qed.

 Lemma ring_sub_ext :
      forall  x1 x2, x1 == x2 -> forall  y1 y2, y1 == y2 -> x1 - y1 == x2 - y2.
 Proof.
  intros.
  setoid_replace (x1 - y1)  with (x1 + -y1).
  setoid_replace (x2 - y2)  with (x2 + -y2).
  rewrite H;rewrite H0;reflexivity.
  rewrite ring_sub_def. reflexivity.
  rewrite ring_sub_def. reflexivity.
 Qed.

 Ltac mrewrite :=
   repeat first
     [ rewrite ring_add_0_l
     | rewrite <- (ring_add_comm 0)
     | rewrite ring_mul_1_l
     | rewrite ring_mul_0_l
     | rewrite ring_distr_l
     | reflexivity
     ].

 Lemma ring_add_0_r : forall  x, (x + 0) == x.
 Proof. intros; mrewrite. Qed.

 
 Lemma ring_add_assoc1 : forall x y z, (x + y) + z == (y + z) + x.
 Proof.
  intros;rewrite <- (ring_add_assoc x).
  rewrite (ring_add_comm x);reflexivity.
 Qed.

 Lemma ring_add_assoc2 : forall x y z, (y + x) + z == (y + z) + x.
 Proof.
  intros; repeat rewrite <- ring_add_assoc.
   rewrite (ring_add_comm x); reflexivity.
 Qed.

 Lemma ring_opp_zero : -0 == 0.
 Proof.
  rewrite <- (ring_mul_0_r 0). rewrite ring_opp_mul_l.
  repeat rewrite ring_mul_0_r. reflexivity.
 Qed.

End Ring.

(** Some simplification tactics*)
Ltac gen_reflexivity := reflexivity.
 
Ltac gen_rewrite :=
  repeat first
     [ reflexivity
     | progress rewrite ring_opp_zero
     | rewrite ring_add_0_l
     | rewrite ring_add_0_r
     | rewrite ring_mul_1_l 
     | rewrite ring_mul_1_r
     | rewrite ring_mul_0_l 
     | rewrite ring_mul_0_r 
     | rewrite ring_distr_l 
     | rewrite ring_distr_r 
     | rewrite ring_add_assoc 
     | rewrite ring_mul_assoc
     | progress rewrite ring_opp_add 
     | progress rewrite ring_sub_def 
     | progress rewrite <- ring_opp_mul_l 
     | progress rewrite <- ring_opp_mul_r ].

Ltac gen_add_push x :=
repeat (match goal with
  | |- context [(?y + x) + ?z] =>
     progress rewrite (ring_add_assoc2 x y z)
  | |- context [(x + ?y) + ?z] =>
     progress rewrite  (ring_add_assoc1 x y z)
  end).