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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
(************************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
(* $Id: Field_Theory.v 8866 2006-05-28 16:21:04Z herbelin $ *)
Require Import List.
Require Import Peano_dec.
Require Import Ring.
Require Import Field_Compl.
Record Field_Theory : Type :=
{A : Type;
Aplus : A -> A -> A;
Amult : A -> A -> A;
Aone : A;
Azero : A;
Aopp : A -> A;
Aeq : A -> A -> bool;
Ainv : A -> A;
Aminus : option (A -> A -> A);
Adiv : option (A -> A -> A);
RT : Ring_Theory Aplus Amult Aone Azero Aopp Aeq;
Th_inv_def : forall n:A, n <> Azero -> Amult (Ainv n) n = Aone}.
(* The reflexion structure *)
Inductive ExprA : Set :=
| EAzero : ExprA
| EAone : ExprA
| EAplus : ExprA -> ExprA -> ExprA
| EAmult : ExprA -> ExprA -> ExprA
| EAopp : ExprA -> ExprA
| EAinv : ExprA -> ExprA
| EAvar : nat -> ExprA.
(**** Decidability of equality ****)
Lemma eqExprA_O : forall e1 e2:ExprA, {e1 = e2} + {e1 <> e2}.
Proof.
double induction e1 e2; try intros;
try (left; reflexivity) || (try (right; discriminate)).
elim (H1 e0); intro y; elim (H2 e); intro y0;
try
(left; rewrite y; rewrite y0; auto) ||
(right; red in |- *; intro; inversion H3; auto).
elim (H1 e0); intro y; elim (H2 e); intro y0;
try
(left; rewrite y; rewrite y0; auto) ||
(right; red in |- *; intro; inversion H3; auto).
elim (H0 e); intro y.
left; rewrite y; auto.
right; red in |- *; intro; inversion H1; auto.
elim (H0 e); intro y.
left; rewrite y; auto.
right; red in |- *; intro; inversion H1; auto.
elim (eq_nat_dec n n0); intro y.
left; rewrite y; auto.
right; red in |- *; intro; inversion H; auto.
Defined.
Definition eq_nat_dec := Eval compute in eq_nat_dec.
Definition eqExprA := Eval compute in eqExprA_O.
(**** Generation of the multiplier ****)
Fixpoint mult_of_list (e:list ExprA) : ExprA :=
match e with
| nil => EAone
| e1 :: l1 => EAmult e1 (mult_of_list l1)
end.
Section Theory_of_fields.
Variable T : Field_Theory.
Let AT := A T.
Let AplusT := Aplus T.
Let AmultT := Amult T.
Let AoneT := Aone T.
Let AzeroT := Azero T.
Let AoppT := Aopp T.
Let AeqT := Aeq T.
Let AinvT := Ainv T.
Let RTT := RT T.
Let Th_inv_defT := Th_inv_def T.
Add Abstract Ring (A T) (Aplus T) (Amult T) (Aone T) (
Azero T) (Aopp T) (Aeq T) (RT T).
Add Abstract Ring AT AplusT AmultT AoneT AzeroT AoppT AeqT RTT.
(***************************)
(* Lemmas to be used *)
(***************************)
Lemma AplusT_sym : forall r1 r2:AT, AplusT r1 r2 = AplusT r2 r1.
Proof.
intros; ring.
Qed.
Lemma AplusT_assoc :
forall r1 r2 r3:AT, AplusT (AplusT r1 r2) r3 = AplusT r1 (AplusT r2 r3).
Proof.
intros; ring.
Qed.
Lemma AmultT_sym : forall r1 r2:AT, AmultT r1 r2 = AmultT r2 r1.
Proof.
intros; ring.
Qed.
Lemma AmultT_assoc :
forall r1 r2 r3:AT, AmultT (AmultT r1 r2) r3 = AmultT r1 (AmultT r2 r3).
Proof.
intros; ring.
Qed.
Lemma AplusT_Ol : forall r:AT, AplusT AzeroT r = r.
Proof.
intros; ring.
Qed.
Lemma AmultT_1l : forall r:AT, AmultT AoneT r = r.
Proof.
intros; ring.
Qed.
Lemma AplusT_AoppT_r : forall r:AT, AplusT r (AoppT r) = AzeroT.
Proof.
intros; ring.
Qed.
Lemma AmultT_AplusT_distr :
forall r1 r2 r3:AT,
AmultT r1 (AplusT r2 r3) = AplusT (AmultT r1 r2) (AmultT r1 r3).
Proof.
intros; ring.
Qed.
Lemma r_AplusT_plus : forall r r1 r2:AT, AplusT r r1 = AplusT r r2 -> r1 = r2.
Proof.
intros; transitivity (AplusT (AplusT (AoppT r) r) r1).
ring.
transitivity (AplusT (AplusT (AoppT r) r) r2).
repeat rewrite AplusT_assoc; rewrite <- H; reflexivity.
ring.
Qed.
Lemma r_AmultT_mult :
forall r r1 r2:AT, AmultT r r1 = AmultT r r2 -> r <> AzeroT -> r1 = r2.
Proof.
intros; transitivity (AmultT (AmultT (AinvT r) r) r1).
rewrite Th_inv_defT; [ symmetry in |- *; apply AmultT_1l; auto | auto ].
transitivity (AmultT (AmultT (AinvT r) r) r2).
repeat rewrite AmultT_assoc; rewrite H; trivial.
rewrite Th_inv_defT; [ apply AmultT_1l; auto | auto ].
Qed.
Lemma AmultT_Or : forall r:AT, AmultT r AzeroT = AzeroT.
Proof.
intro; ring.
Qed.
Lemma AmultT_Ol : forall r:AT, AmultT AzeroT r = AzeroT.
Proof.
intro; ring.
Qed.
Lemma AmultT_1r : forall r:AT, AmultT r AoneT = r.
Proof.
intro; ring.
Qed.
Lemma AinvT_r : forall r:AT, r <> AzeroT -> AmultT r (AinvT r) = AoneT.
Proof.
intros; rewrite AmultT_sym; apply Th_inv_defT; auto.
Qed.
Lemma Rmult_neq_0_reg :
forall r1 r2:AT, AmultT r1 r2 <> AzeroT -> r1 <> AzeroT /\ r2 <> AzeroT.
Proof.
intros r1 r2 H; split; red in |- *; intro; apply H; rewrite H0; ring.
Qed.
(************************)
(* Interpretation *)
(************************)
(**** ExprA --> A ****)
Fixpoint interp_ExprA (lvar:list (AT * nat)) (e:ExprA) {struct e} :
AT :=
match e with
| EAzero => AzeroT
| EAone => AoneT
| EAplus e1 e2 => AplusT (interp_ExprA lvar e1) (interp_ExprA lvar e2)
| EAmult e1 e2 => AmultT (interp_ExprA lvar e1) (interp_ExprA lvar e2)
| EAopp e => Aopp T (interp_ExprA lvar e)
| EAinv e => Ainv T (interp_ExprA lvar e)
| EAvar n => assoc_2nd AT nat eq_nat_dec lvar n AzeroT
end.
(************************)
(* Simplification *)
(************************)
(**** Associativity ****)
Definition merge_mult :=
(fix merge_mult (e1:ExprA) : ExprA -> ExprA :=
fun e2:ExprA =>
match e1 with
| EAmult t1 t2 =>
match t2 with
| EAmult t2 t3 => EAmult t1 (EAmult t2 (merge_mult t3 e2))
| _ => EAmult t1 (EAmult t2 e2)
end
| _ => EAmult e1 e2
end).
Fixpoint assoc_mult (e:ExprA) : ExprA :=
match e with
| EAmult e1 e3 =>
match e1 with
| EAmult e1 e2 =>
merge_mult (merge_mult (assoc_mult e1) (assoc_mult e2))
(assoc_mult e3)
| _ => EAmult e1 (assoc_mult e3)
end
| _ => e
end.
Definition merge_plus :=
(fix merge_plus (e1:ExprA) : ExprA -> ExprA :=
fun e2:ExprA =>
match e1 with
| EAplus t1 t2 =>
match t2 with
| EAplus t2 t3 => EAplus t1 (EAplus t2 (merge_plus t3 e2))
| _ => EAplus t1 (EAplus t2 e2)
end
| _ => EAplus e1 e2
end).
Fixpoint assoc (e:ExprA) : ExprA :=
match e with
| EAplus e1 e3 =>
match e1 with
| EAplus e1 e2 =>
merge_plus (merge_plus (assoc e1) (assoc e2)) (assoc e3)
| _ => EAplus (assoc_mult e1) (assoc e3)
end
| _ => assoc_mult e
end.
Lemma merge_mult_correct1 :
forall (e1 e2 e3:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (merge_mult (EAmult e1 e2) e3) =
interp_ExprA lvar (EAmult e1 (merge_mult e2 e3)).
Proof.
intros e1 e2; generalize e1; generalize e2; clear e1 e2.
simple induction e2; auto; intros.
unfold merge_mult at 1 in |- *; fold merge_mult in |- *;
unfold interp_ExprA at 2 in |- *; fold interp_ExprA in |- *;
rewrite (H0 e e3 lvar); unfold interp_ExprA at 1 in |- *;
fold interp_ExprA in |- *; unfold interp_ExprA at 5 in |- *;
fold interp_ExprA in |- *; auto.
Qed.
Lemma merge_mult_correct :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (merge_mult e1 e2) = interp_ExprA lvar (EAmult e1 e2).
Proof.
simple induction e1; auto; intros.
elim e0; try (intros; simpl in |- *; ring).
unfold interp_ExprA in H2; fold interp_ExprA in H2;
cut
(AmultT (interp_ExprA lvar e2)
(AmultT (interp_ExprA lvar e4)
(AmultT (interp_ExprA lvar e) (interp_ExprA lvar e3))) =
AmultT
(AmultT (AmultT (interp_ExprA lvar e) (interp_ExprA lvar e4))
(interp_ExprA lvar e2)) (interp_ExprA lvar e3)).
intro H3; rewrite H3; rewrite <- H2; rewrite merge_mult_correct1;
simpl in |- *; ring.
ring.
Qed.
Lemma assoc_mult_correct1 :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
AmultT (interp_ExprA lvar (assoc_mult e1))
(interp_ExprA lvar (assoc_mult e2)) =
interp_ExprA lvar (assoc_mult (EAmult e1 e2)).
Proof.
simple induction e1; auto; intros.
rewrite <- (H e0 lvar); simpl in |- *; rewrite merge_mult_correct;
simpl in |- *; rewrite merge_mult_correct; simpl in |- *;
auto.
Qed.
Lemma assoc_mult_correct :
forall (e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (assoc_mult e) = interp_ExprA lvar e.
Proof.
simple induction e; auto; intros.
elim e0; intros.
intros; simpl in |- *; ring.
simpl in |- *; rewrite (AmultT_1l (interp_ExprA lvar (assoc_mult e1)));
rewrite (AmultT_1l (interp_ExprA lvar e1)); apply H0.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite merge_mult_correct; simpl in |- *;
rewrite merge_mult_correct; simpl in |- *; rewrite AmultT_assoc;
rewrite assoc_mult_correct1; rewrite H2; simpl in |- *;
rewrite <- assoc_mult_correct1 in H1; unfold interp_ExprA at 3 in H1;
fold interp_ExprA in H1; rewrite (H0 lvar) in H1;
rewrite (AmultT_sym (interp_ExprA lvar e3) (interp_ExprA lvar e1));
rewrite <- AmultT_assoc; rewrite H1; rewrite AmultT_assoc;
ring.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite (H0 lvar); auto.
Qed.
Lemma merge_plus_correct1 :
forall (e1 e2 e3:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (merge_plus (EAplus e1 e2) e3) =
interp_ExprA lvar (EAplus e1 (merge_plus e2 e3)).
Proof.
intros e1 e2; generalize e1; generalize e2; clear e1 e2.
simple induction e2; auto; intros.
unfold merge_plus at 1 in |- *; fold merge_plus in |- *;
unfold interp_ExprA at 2 in |- *; fold interp_ExprA in |- *;
rewrite (H0 e e3 lvar); unfold interp_ExprA at 1 in |- *;
fold interp_ExprA in |- *; unfold interp_ExprA at 5 in |- *;
fold interp_ExprA in |- *; auto.
Qed.
Lemma merge_plus_correct :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (merge_plus e1 e2) = interp_ExprA lvar (EAplus e1 e2).
Proof.
simple induction e1; auto; intros.
elim e0; try intros; try (simpl in |- *; ring).
unfold interp_ExprA in H2; fold interp_ExprA in H2;
cut
(AplusT (interp_ExprA lvar e2)
(AplusT (interp_ExprA lvar e4)
(AplusT (interp_ExprA lvar e) (interp_ExprA lvar e3))) =
AplusT
(AplusT (AplusT (interp_ExprA lvar e) (interp_ExprA lvar e4))
(interp_ExprA lvar e2)) (interp_ExprA lvar e3)).
intro H3; rewrite H3; rewrite <- H2; rewrite merge_plus_correct1;
simpl in |- *; ring.
ring.
Qed.
Lemma assoc_plus_correct :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
AplusT (interp_ExprA lvar (assoc e1)) (interp_ExprA lvar (assoc e2)) =
interp_ExprA lvar (assoc (EAplus e1 e2)).
Proof.
simple induction e1; auto; intros.
rewrite <- (H e0 lvar); simpl in |- *; rewrite merge_plus_correct;
simpl in |- *; rewrite merge_plus_correct; simpl in |- *;
auto.
Qed.
Lemma assoc_correct :
forall (e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (assoc e) = interp_ExprA lvar e.
Proof.
simple induction e; auto; intros.
elim e0; intros.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite merge_plus_correct; simpl in |- *;
rewrite merge_plus_correct; simpl in |- *; rewrite AplusT_assoc;
rewrite assoc_plus_correct; rewrite H2; simpl in |- *;
apply
(r_AplusT_plus (interp_ExprA lvar (assoc e1))
(AplusT (interp_ExprA lvar (assoc e2))
(AplusT (interp_ExprA lvar e3) (interp_ExprA lvar e1)))
(AplusT (AplusT (interp_ExprA lvar e2) (interp_ExprA lvar e3))
(interp_ExprA lvar e1))); rewrite <- AplusT_assoc;
rewrite
(AplusT_sym (interp_ExprA lvar (assoc e1)) (interp_ExprA lvar (assoc e2)))
; rewrite assoc_plus_correct; rewrite H1; simpl in |- *;
rewrite (H0 lvar);
rewrite <-
(AplusT_assoc (AplusT (interp_ExprA lvar e2) (interp_ExprA lvar e1))
(interp_ExprA lvar e3) (interp_ExprA lvar e1))
;
rewrite
(AplusT_assoc (interp_ExprA lvar e2) (interp_ExprA lvar e1)
(interp_ExprA lvar e3));
rewrite (AplusT_sym (interp_ExprA lvar e1) (interp_ExprA lvar e3));
rewrite <-
(AplusT_assoc (interp_ExprA lvar e2) (interp_ExprA lvar e3)
(interp_ExprA lvar e1)); apply AplusT_sym.
unfold assoc in |- *; fold assoc in |- *; unfold interp_ExprA in |- *;
fold interp_ExprA in |- *; rewrite assoc_mult_correct;
rewrite (H0 lvar); simpl in |- *; auto.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite (H0 lvar); auto.
simpl in |- *; rewrite (H0 lvar); auto.
unfold assoc in |- *; fold assoc in |- *; unfold interp_ExprA in |- *;
fold interp_ExprA in |- *; rewrite assoc_mult_correct;
simpl in |- *; auto.
Qed.
(**** Distribution *****)
Fixpoint distrib_EAopp (e:ExprA) : ExprA :=
match e with
| EAplus e1 e2 => EAplus (distrib_EAopp e1) (distrib_EAopp e2)
| EAmult e1 e2 => EAmult (distrib_EAopp e1) (distrib_EAopp e2)
| EAopp e => EAmult (EAopp EAone) (distrib_EAopp e)
| e => e
end.
Definition distrib_mult_right :=
(fix distrib_mult_right (e1:ExprA) : ExprA -> ExprA :=
fun e2:ExprA =>
match e1 with
| EAplus t1 t2 =>
EAplus (distrib_mult_right t1 e2) (distrib_mult_right t2 e2)
| _ => EAmult e1 e2
end).
Fixpoint distrib_mult_left (e1 e2:ExprA) {struct e1} : ExprA :=
match e1 with
| EAplus t1 t2 =>
EAplus (distrib_mult_left t1 e2) (distrib_mult_left t2 e2)
| _ => distrib_mult_right e2 e1
end.
Fixpoint distrib_main (e:ExprA) : ExprA :=
match e with
| EAmult e1 e2 => distrib_mult_left (distrib_main e1) (distrib_main e2)
| EAplus e1 e2 => EAplus (distrib_main e1) (distrib_main e2)
| EAopp e => EAopp (distrib_main e)
| _ => e
end.
Definition distrib (e:ExprA) : ExprA := distrib_main (distrib_EAopp e).
Lemma distrib_mult_right_correct :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (distrib_mult_right e1 e2) =
AmultT (interp_ExprA lvar e1) (interp_ExprA lvar e2).
Proof.
simple induction e1; try intros; simpl in |- *; auto.
rewrite AmultT_sym; rewrite AmultT_AplusT_distr; rewrite (H e2 lvar);
rewrite (H0 e2 lvar); ring.
Qed.
Lemma distrib_mult_left_correct :
forall (e1 e2:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (distrib_mult_left e1 e2) =
AmultT (interp_ExprA lvar e1) (interp_ExprA lvar e2).
Proof.
simple induction e1; try intros; simpl in |- *.
rewrite AmultT_Ol; rewrite distrib_mult_right_correct; simpl in |- *;
apply AmultT_Or.
rewrite distrib_mult_right_correct; simpl in |- *; apply AmultT_sym.
rewrite AmultT_sym;
rewrite
(AmultT_AplusT_distr (interp_ExprA lvar e2) (interp_ExprA lvar e)
(interp_ExprA lvar e0));
rewrite (AmultT_sym (interp_ExprA lvar e2) (interp_ExprA lvar e));
rewrite (AmultT_sym (interp_ExprA lvar e2) (interp_ExprA lvar e0));
rewrite (H e2 lvar); rewrite (H0 e2 lvar); auto.
rewrite distrib_mult_right_correct; simpl in |- *; apply AmultT_sym.
rewrite distrib_mult_right_correct; simpl in |- *; apply AmultT_sym.
rewrite distrib_mult_right_correct; simpl in |- *; apply AmultT_sym.
rewrite distrib_mult_right_correct; simpl in |- *; apply AmultT_sym.
Qed.
Lemma distrib_correct :
forall (e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (distrib e) = interp_ExprA lvar e.
Proof.
simple induction e; intros; auto.
simpl in |- *; rewrite <- (H lvar); rewrite <- (H0 lvar);
unfold distrib in |- *; simpl in |- *; auto.
simpl in |- *; rewrite <- (H lvar); rewrite <- (H0 lvar);
unfold distrib in |- *; simpl in |- *; apply distrib_mult_left_correct.
simpl in |- *; fold AoppT in |- *; rewrite <- (H lvar);
unfold distrib in |- *; simpl in |- *; rewrite distrib_mult_right_correct;
simpl in |- *; fold AoppT in |- *; ring.
Qed.
(**** Multiplication by the inverse product ****)
Lemma mult_eq :
forall (e1 e2 a:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar a <> AzeroT ->
interp_ExprA lvar (EAmult a e1) = interp_ExprA lvar (EAmult a e2) ->
interp_ExprA lvar e1 = interp_ExprA lvar e2.
Proof.
simpl in |- *; intros;
apply
(r_AmultT_mult (interp_ExprA lvar a) (interp_ExprA lvar e1)
(interp_ExprA lvar e2)); assumption.
Qed.
Fixpoint multiply_aux (a e:ExprA) {struct e} : ExprA :=
match e with
| EAplus e1 e2 => EAplus (EAmult a e1) (multiply_aux a e2)
| _ => EAmult a e
end.
Definition multiply (e:ExprA) : ExprA :=
match e with
| EAmult a e1 => multiply_aux a e1
| _ => e
end.
Lemma multiply_aux_correct :
forall (a e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (multiply_aux a e) =
AmultT (interp_ExprA lvar a) (interp_ExprA lvar e).
Proof.
simple induction e; simpl in |- *; intros; try rewrite merge_mult_correct;
auto.
simpl in |- *; rewrite (H0 lvar); ring.
Qed.
Lemma multiply_correct :
forall (e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar (multiply e) = interp_ExprA lvar e.
Proof.
simple induction e; simpl in |- *; auto.
intros; apply multiply_aux_correct.
Qed.
(**** Permutations and simplification ****)
Fixpoint monom_remove (a m:ExprA) {struct m} : ExprA :=
match m with
| EAmult m0 m1 =>
match eqExprA m0 (EAinv a) with
| left _ => m1
| right _ => EAmult m0 (monom_remove a m1)
end
| _ =>
match eqExprA m (EAinv a) with
| left _ => EAone
| right _ => EAmult a m
end
end.
Definition monom_simplif_rem :=
(fix monom_simplif_rem (a:ExprA) : ExprA -> ExprA :=
fun m:ExprA =>
match a with
| EAmult a0 a1 => monom_simplif_rem a1 (monom_remove a0 m)
| _ => monom_remove a m
end).
Definition monom_simplif (a m:ExprA) : ExprA :=
match m with
| EAmult a' m' =>
match eqExprA a a' with
| left _ => monom_simplif_rem a m'
| right _ => m
end
| _ => m
end.
Fixpoint inverse_simplif (a e:ExprA) {struct e} : ExprA :=
match e with
| EAplus e1 e2 => EAplus (monom_simplif a e1) (inverse_simplif a e2)
| _ => monom_simplif a e
end.
Lemma monom_remove_correct :
forall (e a:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar a <> AzeroT ->
interp_ExprA lvar (monom_remove a e) =
AmultT (interp_ExprA lvar a) (interp_ExprA lvar e).
Proof.
simple induction e; intros.
simpl in |- *; case (eqExprA EAzero (EAinv a)); intros;
[ inversion e0 | simpl in |- *; trivial ].
simpl in |- *; case (eqExprA EAone (EAinv a)); intros;
[ inversion e0 | simpl in |- *; trivial ].
simpl in |- *; case (eqExprA (EAplus e0 e1) (EAinv a)); intros;
[ inversion e2 | simpl in |- *; trivial ].
simpl in |- *; case (eqExprA e0 (EAinv a)); intros.
rewrite e2; simpl in |- *; fold AinvT in |- *.
rewrite <-
(AmultT_assoc (interp_ExprA lvar a) (AinvT (interp_ExprA lvar a))
(interp_ExprA lvar e1)); rewrite AinvT_r; [ ring | assumption ].
simpl in |- *; rewrite H0; auto; ring.
simpl in |- *; fold AoppT in |- *; case (eqExprA (EAopp e0) (EAinv a));
intros; [ inversion e1 | simpl in |- *; trivial ].
unfold monom_remove in |- *; case (eqExprA (EAinv e0) (EAinv a)); intros.
case (eqExprA e0 a); intros.
rewrite e2; simpl in |- *; fold AinvT in |- *; rewrite AinvT_r; auto.
inversion e1; simpl in |- *; elimtype False; auto.
simpl in |- *; trivial.
unfold monom_remove in |- *; case (eqExprA (EAvar n) (EAinv a)); intros;
[ inversion e0 | simpl in |- *; trivial ].
Qed.
Lemma monom_simplif_rem_correct :
forall (a e:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar a <> AzeroT ->
interp_ExprA lvar (monom_simplif_rem a e) =
AmultT (interp_ExprA lvar a) (interp_ExprA lvar e).
Proof.
simple induction a; simpl in |- *; intros; try rewrite monom_remove_correct;
auto.
elim (Rmult_neq_0_reg (interp_ExprA lvar e) (interp_ExprA lvar e0) H1);
intros.
rewrite (H0 (monom_remove e e1) lvar H3); rewrite monom_remove_correct; auto.
ring.
Qed.
Lemma monom_simplif_correct :
forall (e a:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar a <> AzeroT ->
interp_ExprA lvar (monom_simplif a e) = interp_ExprA lvar e.
Proof.
simple induction e; intros; auto.
simpl in |- *; case (eqExprA a e0); intros.
rewrite <- e2; apply monom_simplif_rem_correct; auto.
simpl in |- *; trivial.
Qed.
Lemma inverse_correct :
forall (e a:ExprA) (lvar:list (AT * nat)),
interp_ExprA lvar a <> AzeroT ->
interp_ExprA lvar (inverse_simplif a e) = interp_ExprA lvar e.
Proof.
simple induction e; intros; auto.
simpl in |- *; rewrite (H0 a lvar H1); rewrite monom_simplif_correct; auto.
unfold inverse_simplif in |- *; rewrite monom_simplif_correct; auto.
Qed.
End Theory_of_fields.
|