forked from thery/GeometricAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuple3.v
2558 lines (2339 loc) · 77.5 KB
/
Tuple3.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import List Eqdep_dec Compare Bool Field Field_tac ZArith.
Require Import VectorSpace Grassmann G3.
Fixpoint natc (m n : nat) :=
match m, n with 0, 0 => Eq | 0, _ => Lt | _, 0 => Gt | S m1, S n1 => natc m1 n1 end.
Lemma natc_correct m n :
match natc m n with Eq => m = n | Lt => m < n | Gt => n < m end.
Proof.
generalize n; clear n.
induction m as [|m IH]; intros [|n]; simpl; auto with arith.
generalize (IH n); case natc; auto with arith.
Qed.
Lemma natc_eq m n :
match natc m n with
| Eq => m = n
| _ => True
end.
Proof. generalize (natc_correct m n); case natc; auto. Qed.
Section FF.
(****************************************************)
(* Grassman for n = 3 *)
Variable K : fparams.
Hypothesis FT :
@field_theory (Field.K K) (v0 K) (v1 K) (addK K) (multK K)
(fun x y => (addK K) x (oppK K y)) (oppK K)
(fun x y => (multK K) x (invK K y)) (invK K) (@Logic.eq K).
Add Field Kfth : FT.
Definition Pp : params := Build_params 2 K.
Variable HP : fparamsProp Pp.
Section Generic.
(**************************************************************)
(* Implementation of tuple vars *)
Variable tvar : Type.
Variable tvcompare : tvar -> tvar -> comparison.
Variable tvarn : tvar -> nat.
Hypothesis tvcompare_inj : forall v1 v2,
tvarn v1 = tvarn v2 -> v1 = v2.
Hypothesis tvcompare_def : forall v1 v2,
tvcompare v1 v2 = natc (tvarn v1) (tvarn v2).
Lemma tvcompare_eq v1 v2 :
match tvcompare v1 v2 with Eq => v1 = v2 | _ => v1 <> v2 end.
Proof.
rewrite tvcompare_def.
generalize (natc_correct (tvarn v1) (tvarn v2)); case natc; auto.
intros H1 H2; contradict H1; rewrite H2; auto with arith.
intros H1 H2; contradict H1; rewrite H2; auto with arith.
Qed.
Lemma tvcompare_refl v : tvcompare v v = Eq.
rewrite tvcompare_def.
generalize (natc_correct (tvarn v) (tvarn v)); case natc; auto.
intros H; contradict H; auto with arith.
intros H; contradict H; auto with arith.
Qed.
(* Proof *)
Variable interv : tvar -> point K.
Notation "{v[ x ]}" := (interv x).
(* Implementation of variable *)
Variable var : Type.
(* Implementation of coef *)
Variable coef : Type.
Variable cadd : coef -> coef -> coef.
Variable czerop : coef -> bool.
Variable czero : coef.
Variable conep : coef -> bool.
Variable cone : coef.
Variable copp : coef -> coef.
Variable cscale : var -> coef -> coef.
Variable interk : var -> K.
Notation "{k[ x ]}" := (interk x).
Variable interc : coef -> K.
Notation "{c[ x ]}" := (interc x).
Hypothesis cadd_c : forall x y, {c[cadd x y]} = ({c[x]} + {c[y]})%f.
Hypothesis cone_c : {c[cone]} = (1)%f.
Hypothesis conep_c : forall x,
if conep x then {c[x]} = (1)%f else True.
Hypothesis czero_c : {c[czero]} = (0)%f.
Hypothesis cscale_c : forall k x,
{c[cscale k x]} = ({k[k]} * {c[x]})%f.
Hypothesis czerop_c : forall x,
if czerop x then {c[x]} = (0)%f else True.
Hypothesis copp_c : forall x, {c[copp x]} = (- {c[x]})%f.
(* We want to reduce expression *)
Inductive tuple := Tuple (_ _ _: tvar).
Let line := list tuple.
Let expr := list (coef * line).
Definition intert (t : tuple) :=
let (p1,p2,p3) := t in '[{v[p1]},{v[p2]},{v[p3]}].
Notation "{t[ x ]}" := (intert x).
Fixpoint interl (l :line) :=
match l with nil => 1%f | t ::l1 => ({t[t]} * interl l1)%f end.
Notation "{l[ x ]}" := (interl x).
Fixpoint intere (e :expr) :=
match e with nil => 0%f | (c,l)::e1 =>
({c[c]} * {l[l]} + intere e1)%f
end.
Notation "{e[ x ]}" := (intere x).
Definition intere1 (e :expr) :=
match e with nil => 0%f | (c,t ::nil)::nil =>
if conep c then {t[t]} else intere e
| _ => intere e end.
Notation "{e1[ x ]}" := (intere1 x).
Lemma intere1_c e : {e1[e]} = {e[e]}.
Proof.
elim e; simpl; auto.
intros (c,[|a [|b l]]) [|e1] IH; auto.
generalize (conep_c c); case conep; auto.
simpl.
intros HH; rewrite HH; ring.
Qed.
(* Sign *)
Inductive sign := Sm1 | S0 | S1.
Definition sopp s := match s with Sm1 => S1 | S0 => S0 | S1 => Sm1 end.
Definition smult s1 s2 :=
match s1 with S0 => S0 | S1 => s2 | Sm1 => sopp s2 end.
Definition smult3 s1 s2 s3 :=
match s1 with S0 => S0
| S1 => smult s2 s3
| Sm1 => sopp (smult s2 s3) end.
(* Membership *)
Definition tin i (t : tuple) :=
let (j1,j2,j3) := t in
match tvcompare i j1 with Eq => true
| _ => match tvcompare i j2 with Eq => true
| _ => match tvcompare i j3 with Eq => true
| _ => false end end end.
Lemma tin_correct i1 i j k :
if tin i1 (Tuple i j k) then
i1=i \/ i1 = j \/ i1 = k
else
i1<>i /\ i1 <> j /\ i1 <> k.
Proof.
unfold tin.
generalize (tvcompare_eq i1 i); case tvcompare; auto.
generalize (tvcompare_eq i1 j); case tvcompare; auto.
generalize (tvcompare_eq i1 k); case tvcompare; auto.
generalize (tvcompare_eq i1 k); case tvcompare; auto.
generalize (tvcompare_eq i1 j); case tvcompare; auto.
generalize (tvcompare_eq i1 k); case tvcompare; auto.
generalize (tvcompare_eq i1 k); case tvcompare; auto.
Qed.
(* Substitution *)
Definition tsubst i j (t : tuple) :=
let (i1,i2,i3) := t in
match tvcompare i i1 with
| Eq => Tuple j i2 i3
| _ =>
match tvcompare i i2 with
| Eq => Tuple i1 j i3
| _ =>
match tvcompare i i3 with
| Eq => Tuple i1 i2 j
| _ => t
end
end
end.
Definition s2k (s: sign) : K :=
match s with S0 => 0%f | S1 => 1%f | Sm1 => (-(1))%f end.
Lemma tsubst_c i t :
if tin i t then exists a, exists b, exists s,
({t[t]} = s2k s * {t[Tuple i a b]} /\
forall j, {t[tsubst i j t]} = s2k s * {t[Tuple j a b]})%f else True.
Proof.
destruct t as [a b c].
unfold tin, tsubst.
generalize (tvcompare_eq i a); case tvcompare; auto.
intros HH; subst.
exists b; exists c; exists S1; simpl s2k; Krm1.
split; try intros; Krm1.
intros HH.
generalize (tvcompare_eq i b); case tvcompare; auto.
intros HH1; subst.
exists a; exists c; exists Sm1; simpl s2k; Krm1.
split.
simpl; rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j; simpl; rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros HH1.
generalize (tvcompare_eq i c); case tvcompare; auto.
intros HH2; subst.
exists a; exists b; exists S1; simpl s2k; split.
simpl; rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j; simpl; rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros HH1.
generalize (tvcompare_eq i c); case tvcompare; auto.
intros HH2; subst.
exists a; exists b; exists S1; simpl s2k; Krm1.
split.
simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j.
simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros HH.
generalize (tvcompare_eq i b); case tvcompare; auto.
intros HH1; subst.
exists a; exists c; exists Sm1; simpl s2k; Krm1.
split.
simpl.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j.
simpl.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros HH1.
generalize (tvcompare_eq i c); case tvcompare; auto.
intros HH2; subst.
exists a; exists b; exists S1; simpl s2k; Krm1.
split.
simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j; simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros HH1.
generalize (tvcompare_eq i c); case tvcompare; auto.
intros HH2; subst.
exists a; exists b; exists S1; simpl s2k; Krm1.
split.
simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
intros j; simpl.
rewrite !(fun x => bracket_swapr _ HP {v[a]} x {v[b]}); Krm1.
rewrite !(bracket_swapl _ HP {v[a]}); Krm1.
Qed.
(* Add an element in a tuple i1 [? i2 i3] *)
Definition mk_tuplel i1 i2 i3 :=
match tvcompare i1 i2 with
| Eq => (S0, Tuple i1 i2 i3)
| Lt => (S1, Tuple i1 i2 i3)
| Gt =>
match tvcompare i1 i3 with
| Eq => ( S0, Tuple i1 i2 i3)
| Lt => (Sm1, Tuple i2 i1 i3)
| Gt => ( S1, Tuple i2 i3 i1)
end
end.
Lemma mk_tuplel_c i1 i2 i3 :
let (s,t1) := mk_tuplel i1 i2 i3 in
{t[Tuple i1 i2 i3]} = (s2k s * {t[t1]})%f.
Proof.
unfold mk_tuplel.
generalize (tvcompare_eq i1 i2).
case tvcompare; simpl; Krm1.
intros HH; rewrite HH.
rewrite bracket0l; Krm0.
intros _; generalize (tvcompare_eq i1 i3).
case tvcompare; simpl; Krm1.
intros HH; rewrite HH.
rewrite bracket0m; auto.
intros HH; rewrite bracket_swapl; auto.
intros HH; rewrite bracket_swapl; auto.
rewrite bracket_swapr; auto; Krm1.
Qed.
(* Add an element in a tuple [i1 i2 ?] i3 *)
Definition mk_tupler i1 i2 i3 :=
match tvcompare i2 i3 with
| Eq => (S0, Tuple i1 i2 i3)
| Lt => (S1, Tuple i1 i2 i3)
| Gt =>
match tvcompare i1 i3 with
| Eq => ( S0, Tuple i1 i2 i3)
| Lt => (Sm1, Tuple i1 i3 i2)
| Gt => ( S1, Tuple i3 i1 i2)
end
end.
Lemma mk_tupler_c i1 i2 i3 :
let (s,t1) := mk_tupler i1 i2 i3 in
{t[Tuple i1 i2 i3]} = (s2k s * {t[t1]})%f.
Proof.
unfold mk_tupler.
generalize (tvcompare_eq i2 i3).
case tvcompare; simpl; Krm1.
intros HH; rewrite HH.
rewrite bracket0r; Krm0.
intros _; generalize (tvcompare_eq i1 i3).
case tvcompare; simpl; Krm1.
intros HH; rewrite HH.
rewrite bracket0m; auto.
intros HH; rewrite bracket_swapr; auto.
intros HH; rewrite bracket_swapr; auto.
rewrite bracket_swapl; auto; Krm1.
Qed.
Inductive sprod := SProd (s : sign) (t1 t2 : tuple).
Definition esprod (s : sprod) :=
let (s,t1,t2) := s in (s2k s * {t[t1]} * {t[t2]})%f.
Definition elsprod (l : list sprod) :=
fold_left (fun res s => (res + esprod s)%f) l 0%f.
(* i1 <= j1 *)
Definition tsplit3 (i1 i2 i3 j1 j2 j3 : tvar) :=
match tvcompare i2 j2 with
| Gt =>
(*
[i1 j1 j2] [i2 i3 j3]
- [i1 j1 j3] [i2 i3 j2]
[i1 j2 j3] [i2 i3 j1]
*)
let (s1,t1) := mk_tuplel i1 j1 j2 in
let (s'1,t'1) := mk_tupler i2 i3 j3 in
let (s2,t2) := mk_tuplel i1 j1 j3 in
let (s'2,t'2) := mk_tupler i2 i3 j2 in
let (s3,t3) := mk_tuplel i1 j2 j3 in
let (s'3,t'3) := mk_tupler i2 i3 j1 in
SProd (smult s1 s'1) t1 t'1 ::
SProd (sopp (smult s2 s'2)) t2 t'2 ::
SProd (smult s3 s'3) t3 t'3 :: nil
| _ =>
match tvcompare i3 j3 with
| Gt =>
(*
[i1 i2 j1] [i3 j2 j3]
-[i1 i2 j2] [i3 j1 j3]
[i1 i2 j3] [i3 j1 j2]
*)
let (s1,t1) := mk_tupler i1 i2 j1 in
let (s'1,t'1) := mk_tuplel i3 j2 j3 in
let (s2,t2) := mk_tupler i1 i2 j2 in
let (s'2,t'2) := mk_tuplel i3 j1 j3 in
let (s3,t3) := mk_tupler i1 i2 j3 in
let (s'3,t'3) := mk_tuplel i3 j1 j2 in
SProd (smult s1 s'1) t1 t'1 ::
SProd (sopp (smult s2 s'2)) t2 t'2 ::
SProd (smult s3 s'3) t3 t'3 :: nil
| _ => SProd S1 (Tuple i1 i2 i3) (Tuple j1 j2 j3)::nil
end
end.
Lemma s2km m1 m2 : s2k (smult m1 m2) = (s2k m1 * s2k m2)%f.
Proof. case m1; case m2; simpl; Krm1. Qed.
Lemma s2ko m : s2k (sopp m) = ((-(1)) * s2k m)%f.
Proof. case m; simpl; Krm1. Qed.
Lemma tsplit3_c a b c d e f :
({t[Tuple a b c]} * {t[Tuple d e f]})%f =
elsprod (tsplit3 a b c d e f).
Proof.
assert (Hs: forall x y z t : K, (x * y * z * t = (x * z) * (y * t))%f).
intros; ring.
assert (Hs1: forall x y z t : K,(-(1) * (x * y) * z * t = -(1) * (x * z) * (y * t))%f).
intros; ring.
simpl; unfold tsplit3.
generalize (tvcompare_eq b e); case tvcompare; intros H1; subst.
generalize (tvcompare_eq c f); case tvcompare; intros H2; subst.
unfold elsprod; simpl; Krm1.
unfold elsprod; simpl; Krm1.
generalize (mk_tupler_c a e d); case mk_tupler; simpl; intros s1 t1 Ht1.
generalize (mk_tuplel_c c e f); case mk_tuplel; simpl; intros s2 t2 Ht2.
generalize (mk_tupler_c a e e); case mk_tupler; simpl; intros s3 t3 Ht3.
generalize (mk_tuplel_c c d f); case mk_tuplel; simpl; intros s4 t4 Ht4.
generalize (mk_tupler_c a e f); case mk_tupler; simpl; intros s5 t5 Ht5.
generalize (mk_tuplel_c c d e); case mk_tuplel; simpl; intros s6 t6 Ht6.
unfold elsprod; simpl; Krm1.
rewrite !s2ko, !s2km.
rewrite Hs, <- Ht1, <-Ht2.
rewrite Hs1, <- Ht3, <-Ht4.
rewrite Hs, <- Ht5, <-Ht6.
apply split3b_v2; auto.
generalize (tvcompare_eq c f); case tvcompare; intros H2; subst.
unfold elsprod; simpl; Krm1.
unfold elsprod; simpl; Krm1.
generalize (mk_tupler_c a b d); case mk_tupler; simpl; intros s1 t1 Ht1.
generalize (mk_tuplel_c c e f); case mk_tuplel; simpl; intros s2 t2 Ht2.
generalize (mk_tupler_c a b e); case mk_tupler; simpl; intros s3 t3 Ht3.
generalize (mk_tuplel_c c d f); case mk_tuplel; simpl; intros s4 t4 Ht4.
generalize (mk_tupler_c a b f); case mk_tupler; simpl; intros s5 t5 Ht5.
generalize (mk_tuplel_c c d e); case mk_tuplel; simpl; intros s6 t6 Ht6.
unfold elsprod; simpl; Krm1.
rewrite !s2ko, !s2km.
rewrite Hs, <- Ht1, <-Ht2.
rewrite Hs1, <- Ht3, <-Ht4.
rewrite Hs, <- Ht5, <-Ht6.
apply split3b_v2; auto.
generalize (mk_tuplel_c a d e); case mk_tuplel; simpl; intros s1 t1 Ht1.
generalize (mk_tupler_c b c f); case mk_tupler; simpl; intros s2 t2 Ht2.
generalize (mk_tuplel_c a d f); case mk_tuplel; simpl; intros s3 t3 Ht3.
generalize (mk_tupler_c b c e); case mk_tupler; simpl; intros s4 t4 Ht4.
generalize (mk_tuplel_c a e f); case mk_tuplel; simpl; intros s5 t5 Ht5.
generalize (mk_tupler_c b c d); case mk_tupler; simpl; intros s6 t6 Ht6.
unfold elsprod; simpl; Krm1.
rewrite !s2ko, !s2km.
rewrite Hs, <- Ht1, <-Ht2.
rewrite Hs1, <- Ht3, <-Ht4.
rewrite Hs, <- Ht5, <-Ht6.
apply split3b_v1; auto.
Qed.
Definition tsplit (t1 t2 : tuple) :=
Eval lazy beta delta [tsplit3] in
let (i1,i2,i3) := t1 in
let (j1,j2,j3) := t2 in
match tvcompare i1 j1 with
| Gt => (* tsplit3 j1 j2 j3 i1 i2 i3 *) SProd S1 t2 t1 :: nil
| _ => tsplit3 i1 i2 i3 j1 j2 j3
end.
Lemma tsplit_c t1 t2 :
({t[t1]} * {t[t2]})%f = elsprod (tsplit t1 t2).
Proof.
destruct t1; destruct t2; simpl.
case tvcompare.
apply tsplit3_c.
apply tsplit3_c.
rewrite multK_com; unfold elsprod; simpl; Krm1.
Qed.
(* Lexicographic comparison *)
Fixpoint list_compare (T : Type) (cmp : T -> T -> comparison)
(l1 l2 : list T) :=
match l1, l2 with
| nil, nil => Eq
| nil, _ => Lt
| _, nil => Gt
| a::l1, b::l2 => match cmp a b with
| Eq => list_compare T cmp l1 l2
| res => res
end
end.
Lemma list_compare_eq T cmp l1 l2 :
(forall c1 c2, match cmp c1 c2 with Eq => c1 = c2 | _ => True end) ->
match list_compare T cmp l1 l2 with Eq => l1 = l2 | _ => True end.
Proof.
intros Hcmp.
generalize l2; elim l1; simpl; auto; clear l1 l2.
intros []; auto.
intros a l1 IH [|b l2]; simpl; auto.
generalize (Hcmp a b); case cmp; auto.
intros H; subst.
generalize (IH l2); case list_compare; auto.
intros H; subst; auto.
Qed.
Definition tcompare (t1 t2 : tuple) :=
let (i1, i2, i3) := t1 in
let (j1, j2, j3) := t2 in
match tvcompare i1 j1 with
| Eq =>
match tvcompare i2 j2 with
| Eq => tvcompare i3 j3
| r => r
end
| r => r
end.
Lemma tcompare_eq t1 t2 :
match tcompare t1 t2 with Eq => t1 = t2 | _ => True end.
Proof.
destruct t1 as [a b c]; destruct t2 as [d e f]; simpl.
generalize (tvcompare_eq a d); case tvcompare; auto.
intros H; subst.
generalize (tvcompare_eq b e); case tvcompare; auto.
intros H; subst.
generalize (tvcompare_eq c f); case tvcompare; auto.
intros H; subst; auto.
Qed.
Definition lcompare : line -> line -> _ := list_compare _ tcompare.
Lemma lcompare_eq l1 l2 :
match lcompare l1 l2 with Eq => l1 = l2 | _ => True end.
Proof. apply list_compare_eq. exact tcompare_eq. Qed.
Definition ecompare : expr -> expr -> _ := list_compare _ (fun x y => lcompare (snd x) (snd y)).
(* Lexicographic comparison *)
Definition tsort (t : tuple) :=
let (i1, i2, i3) := t in
match tvcompare i1 i2 with
| Eq => (S0, t)
| Lt => match tvcompare i2 i3 with
| Eq => ( S0, t)
| Lt => ( S1, t)
| Gt => (* i1 < i2 > i3 *)
match tvcompare i1 i3 with
| Eq => ( S0, t)
| Lt => (Sm1, Tuple i1 i3 i2)
| Gt => ( S1, Tuple i3 i1 i2)
end
end
| Gt => match tvcompare i1 i3 with
| Eq => ( S0, t)
| Lt => (Sm1, Tuple i2 i1 i3)
| Gt =>
match tvcompare i2 i3 with (* i2 < i1 > i3 *)
| Eq => ( S0, t)
| Lt => ( S1, Tuple i2 i3 i1)
| Gt => (Sm1, Tuple i3 i2 i1)
end
end
end.
Lemma tsort_c t :
let (s,t1) := tsort t in {t[t]} = (s2k s * {t[t1]})%f.
Proof.
destruct t as [a b c]; unfold tsort.
generalize (tvcompare_eq a b); case tvcompare; intros H1; subst.
simpl; rewrite bracket0l; Krm0.
generalize (tvcompare_eq b c); case tvcompare; intros H2; subst.
simpl; rewrite bracket0r; Krm0.
simpl; Krm1.
generalize (tvcompare_eq a c); case tvcompare; intros H3; subst.
simpl; rewrite bracket0m; Krm0.
simpl; rewrite bracket_swapr; Krm1.
simpl; rewrite bracket_swapr, bracket_swapl; Krm1.
generalize (tvcompare_eq a c); case tvcompare; intros H2; subst.
simpl; rewrite bracket0m; Krm0.
simpl; rewrite bracket_swapl; Krm1.
generalize (tvcompare_eq b c); case tvcompare; intros H3; subst.
simpl; rewrite bracket0r; Krm0.
simpl; rewrite bracket_swapl, bracket_swapr; Krm1.
simpl; rewrite bracket_swapl, bracket_swapr,bracket_swapl; Krm1.
Qed.
Fixpoint linsert (t1 : tuple) (l : line) :=
match l with
| nil => t1 :: l
| t2::l1 => match tcompare t1 t2 with
| Gt => t2 :: linsert t1 l1
| _ => t1 :: l
end
end.
Lemma linsert_length t l : length (linsert t l) = S (length l).
Proof.
elim l; simpl; auto.
intros a l1 IH; case tcompare; simpl; auto with arith.
Qed.
Lemma linsert_c t l : {l[linsert t l]} = ({t[t]} * {l[l]})%f.
Proof.
elim l; simpl; auto.
intros a l1 IH; case tcompare; simpl; auto.
rewrite IH; auto.
rewrite <-!multK_assoc, (fun y => multK_com _ y {t[a]}); auto.
Qed.
Fixpoint lsort (l : line) :=
match l with
| nil => l
| t ::l1 => linsert t (lsort l1)
end.
Lemma lsort_c l : {l[lsort l]} = {l[l]}.
Proof.
elim l; simpl; auto.
intros a l1 IH.
rewrite linsert_c, IH; auto.
Qed.
Fixpoint ltsort (c : sign) (l : line) :=
match l with
| nil => (c,l)
| t ::l1 => let (c1,t1) := tsort t in
let (c2,l2) := ltsort (smult c1 c) l1 in
(c2, linsert t1 l2)
end.
Lemma ltsort_c c l :
let (s,l1) := ltsort c l in
(s2k s * {l[l1]})%f = (s2k c * {l[l]})%f.
Proof.
generalize c; elim l; simpl; auto.
intros a l1 IH c1.
generalize (tsort_c a); case tsort.
intros s1 t1 Hs1.
generalize (IH (smult s1 c1)); case ltsort.
intros s2 l2 Hs.
rewrite linsert_c, Hs1.
rewrite !multK_assoc, (fun x => multK_com _ x {t[t1]}), <-multK_assoc; auto.
rewrite Hs, s2km.
rewrite !multK_assoc, (fun x => multK_com _ x {l[l1]}), <-multK_assoc; auto.
rewrite <-!multK_assoc, (fun x => multK_com _ x (s2k s1)); auto.
Qed.
Fixpoint einsert (c1 : coef) (l1 : line) (e : expr) :=
match e with
| nil => (c1,l1) :: e
| (c2,l2)::e1 => match lcompare l1 l2 with
| Eq => let c := cadd c1 c2 in
if czerop c then e1 else (c,l1)::e1
| Gt => (c2,l2) :: einsert c1 l1 e1
| Lt => (c1,l1) :: e
end
end.
Lemma einsert_length c l e : length (einsert c l e) <= 1 + length e.
Proof.
elim e; simpl; auto with arith.
intros (c1,l1) e1 IH; simpl.
case lcompare; simpl; auto with zarith.
case czerop; auto with zarith.
Qed.
Lemma einsert_c c1 l1 e :
{e[einsert c1 l1 e]} = ({c[c1]} * {l[l1]} + {e[e]})%f.
Proof.
elim e; simpl; auto.
intros (c2,l2) e1 IH.
generalize (lcompare_eq l1 l2); case lcompare; intros H1; subst.
generalize (czerop_c (cadd c1 c2)); case czerop.
rewrite cadd_c; intros H2.
replace ({c[c1]}) with ({c[c1]} + (-(1)) * 0)%f; try ring.
rewrite <-H2; ring.
simpl; rewrite !cadd_c; intros H2.
ring.
simpl; ring.
simpl; rewrite IH; ring.
Qed.
Definition scmp (s : sign) (c : coef) :=
match s with S0 => czero | S1 => c | Sm1 => copp c end.
Lemma scmp_c s c :
{c[scmp s c]} = (s2k s * {c[c]})%f.
Proof. case s; simpl; rewrite ?copp_c; Krm1. Qed.
Definition einsert0 (c1 : coef) (l1 : line) (e : expr) :=
if czerop c1 then e else einsert c1 l1 e.
Lemma einsert0_c c1 l1 e :
{e[einsert0 c1 l1 e]} = ({c[c1]} * {l[l1]} + {e[e]})%f.
Proof.
unfold einsert0.
generalize (czerop_c c1); case czerop; intros H1; rewrite ?H1.
ring.
apply einsert_c.
Qed.
Fixpoint etsort (e : expr) :=
match e with
| nil => nil
| (c,l)::e1 => let (c1, l1) := ltsort S1 l in
einsert0 (scmp c1 c) l1 (etsort e1)
end.
Lemma etsort_c e : {e[etsort e]} = {e[e]}.
Proof.
elim e; simpl; auto.
intros (c1,l1) e1 IH.
generalize (ltsort_c S1 l1); case ltsort; simpl.
intros s l Hs.
rewrite einsert0_c, scmp_c, IH.
rewrite multK_assoc, (fun x => multK_com _ x {c[c1]}), <- multK_assoc; auto.
rewrite Hs; ring.
Qed.
(* Reverse a tuple in a tail recursive way *)
Fixpoint rev (T : Type) (t1 t2 : list T) :=
match t1 with nil => t2 | n1 :: t3 => rev _ t3 (n1 :: t2) end.
Definition lrev: line -> line -> _ := rev tuple.
Lemma lrev_c l1 l2 :
{l[lrev l1 l2]} = ({l[l1]} * {l[l2]})%f.
Proof.
generalize l2; clear l2.
elim l1; simpl; auto.
intros l2; unfold lrev; simpl; ring.
intros a l2 IH l3.
unfold lrev; simpl; rewrite IH; simpl; ring.
Qed.
Fixpoint iter_split (c1 : coef) (l l1 : line) (accu : line) (e : expr)
: expr :=
if czerop c1 then e else
match l with
| nil => einsert c1 (lrev accu nil) e
| _ :: l' =>
match l1 with
| nil => einsert c1 (lrev accu nil) e
| t1 ::nil => einsert c1 (lrev accu (t1 ::nil)) e
| t1 :: t2 :: l2 =>
fold_left (fun e (v: sprod) =>
let (s1, t1, t2) := v in
let l3 := linsert t2 l2 in
iter_split (scmp s1 c1) l' l3 (t1 ::accu) e)
(tsplit t1 t2) e
end
end.
Lemma iter_split_c c1 l l1 accu e :
length l1 <= length l ->
{e[iter_split c1 l l1 accu e]} =
({c[c1]} * {l[lrev accu l1]} + {e[e]})%f.
Proof.
generalize c1 l1 accu e; clear c1 l1 accu e.
elim l; simpl; auto; clear l.
intros c1 [|t1 l1] accu e; simpl.
intros H; generalize (czerop_c c1); case czerop; intros H1.
rewrite H1; ring.
rewrite einsert_c; auto.
intros H; contradict H; auto with arith.
intros _ l IH c1 l1 accu e.
generalize (czerop_c c1); case czerop; intros H1.
intros H; rewrite H1; ring.
case l1.
intros H2; rewrite einsert_c; auto.
intros t1 l2; simpl; intros H2.
generalize H2; case l2; clear H2.
intros H2.
rewrite einsert_c; auto.
simpl; intros t2 l3 H2.
rewrite lrev_c; simpl.
rewrite <-(fun x => multK_assoc _ x {t[t1]}); auto.
rewrite (tsplit_c t1 t2).
generalize e;
elim (tsplit t1 t2); simpl; auto.
intros e1; unfold elsprod; simpl; ring.
intros (s,t3,t4) l4 IH1 e1.
unfold elsprod; simpl.
rewrite IH1; auto.
rewrite IH; auto.
rewrite scmp_c, !lrev_c; simpl.
rewrite linsert_c.
assert (HH1 : forall l k,
fold_left (fun res s => (res + esprod s)%f) l k =
(elsprod l + k)%f).
intros ll; elim ll; simpl; auto.
intros k; unfold elsprod; simpl; ring.
intros a ll1 IHll k.
unfold elsprod; simpl.
rewrite !IHll; ring.
rewrite HH1.
ring.
rewrite linsert_length; auto with arith.
Qed.
Definition step (e : expr) : expr :=
fold_left (fun e l => match l with
| (c, l1) => iter_split c l1 l1 nil e
end
) e nil.
Lemma step_c e : {e[step e]} = {e[e]}.
apply trans_equal with ({e[e]} + {e[nil]})%f.
2 : simpl; ring.
unfold step.
generalize (nil : expr); elim e; simpl; auto.
intros; ring.
intros (c,l1) e1 IH e2.
rewrite IH, iter_split_c, lrev_c; simpl; auto.
ring.
Qed.
Inductive rp (T : Type) := Stop (_ : T) | More (_ : T).
Definition rp_val (T : Type) (x : rp T) :=
match x with Stop _ y => y | More _ y => y end.
Fixpoint iter_rp (T : Type) n f (v : rp T) := match v with
| More _ r => match n with
| O => f r
| S n1 => iter_rp T n1 f (iter_rp T n1 f v)
end
| _ => v
end.
Definition iter_step (k : nat) (e : expr) :=
iter_rp _ k (fun e => let e1 := step e in
match ecompare e e1 with
Eq => @Stop _ e
| _ => More _ e1
end) (More _ e).
Lemma iter_step_c k e : {e[rp_val _ (iter_step k e)]} = {e[e]}.
Proof.
unfold iter_step.
assert (HH: e = (rp_val _ (More expr e))); auto.
pattern e at 2; rewrite HH.
generalize (More expr e); clear e HH.
elim k; simpl; auto.
intros r; case r; auto.
intros e; case ecompare; simpl; auto.
apply step_c; auto.
apply step_c; auto.
intros k1 IH e; case e.
intros e1; auto.
intros e1; rewrite !IH; auto.
Qed.
(* Elimination of an intersection from a line *)
Fixpoint inter_elim (cc : coef) (x a b c d: tvar) (l : list tuple)
(accu: list tuple) e : expr :=
match l with
| nil => einsert cc (rev _ accu l) e
| t :: l1 =>
if tin x t then
let (c1,t1) := tsort (tsubst x a t) in
let (c2,t2) := tsort (Tuple b c d) in
let (c3,t3) := tsort (tsubst x b t) in
let (c4,t4) := tsort (Tuple a c d) in
einsert0
(scmp (sopp (smult c1 c2)) cc)
(linsert t1 (linsert t2 (rev _ accu l1)))
(einsert0
(scmp (smult c3 c4) cc)
(linsert t3 (linsert t4 (rev _ accu l1)))
e)
else inter_elim cc x a b c d l1 (t ::accu) e
end.
Lemma inter_elim_c cc x a b c d l accu e :
{v[x]} = ({v[a]} ∨ {v[b]}) ∧({v[c]} ∨ {v[d]}) :> vect K ->
{e[inter_elim cc x a b c d l accu e]} =
({c[cc]} * {l[l]} * {l[accu]} + {e[e]})%f.
Proof.
intros H.
generalize accu e; clear accu e; elim l; auto.
intros accu e.
simpl; rewrite einsert_c, lrev_c; simpl; ring.
intros t l1 IH accu e.
unfold inter_elim; fold inter_elim.
generalize (tsubst_c x t).
case tin.
2 : intros; rewrite IH; simpl; ring.
intros (a1,(b1,(s, (H1s,H2s)))).
generalize (tsort_c (tsubst x a t)); case tsort.
intros s1 t1 Ht1.
generalize (tsort_c (Tuple b c d)); case tsort.
intros s2 t2 Ht2.
generalize (tsort_c (tsubst x b t)); case tsort.
intros s3 t3 Ht3.
generalize (tsort_c (Tuple a c d)); case tsort.
intros s4 t4 Ht4.
rewrite !einsert0_c, !scmp_c, !s2ko, !s2km, !linsert_c, lrev_c.
simpl.
replace
(-(1) * (s2k s1 * s2k s2) * {c[cc]} *
({t[t1]} * ({t[t2]} * ({l[accu]} * {l[l1]}))))%f
with
(-(1) * (s2k s1 * {t[t1]}) * (s2k s2 *
{t[t2]}) * {c[cc]} * ({l[accu]} * {l[l1]}))%f;
try ring.
rewrite H1s; simpl.
rewrite <-Ht1, <-Ht2, !H2s; simpl.
replace
(s2k s3 * s2k s4 * {c[cc]} *
({t[t3]} * ({t[t4]} * ({l[accu]} * {l[l1]}))) + {e[e]})%f
with
( ((s2k s3 * {t[t3]}) * (s2k s4 * {t[t4]})) * {c[cc]} *
(({l[accu]} * {l[l1]})) + {e[e]})%f; try ring.
rewrite <-Ht3, <-Ht4, !H2s; simpl.
assert (HH: ('[{v[x]}, {v[a1]}, {v[b1]}] =
(-(1)) * '[{v[a]}, {v[a1]}, {v[b1]}] *
'[{v[b]}, {v[c]}, {v[d]}] +
'[{v[b]}, {v[a1]}, {v[b1]}] *
'[{v[a]}, {v[c]}, {v[d]}])%f).
apply bracket_expand; auto.
rewrite HH; ring.
Qed.
Fixpoint ielim (x a b c d: tvar) e accu :=
match e with
| nil => accu
| (cc,l) :: e1 =>
ielim x a b c d e1
(inter_elim cc x a b c d l nil accu)
end.
Lemma ielim_c x a b c d e accu:
{v[x]} = ({v[a]} ∨ {v[b]}) ∧({v[c]} ∨ {v[d]}) :> vect K ->
{e[ielim x a b c d e accu]} = ({e[accu]} + {e[e]})%f.
Proof.
intros H; generalize accu; clear accu.
elim e; clear e; simpl; auto.
intros; ring.
intros (c1, l1) e1 IH accu.
rewrite IH, inter_elim_c; simpl; auto.
ring.
Qed.
Definition nielim x a b c d e :=
ielim x a b c d e nil.
Lemma nielim_c x a b c d e :
{v[x]} = ({v[a]} ∨ {v[b]}) ∧({v[c]} ∨ {v[d]}) :> vect K ->
{e[nielim x a b c d e]} = {e[e]}.
Proof.
intros; unfold nielim; rewrite ielim_c; auto; simpl; ring.
Qed.
Definition do_elim x a b c d (e : expr) :=
let e1 := nielim x a b c d e in
match ecompare e e1 with
| Eq => Stop _ e1
| _ => More _ e1
end.
Lemma do_elim_c x a b c d e :
{v[x]} = ({v[a]} ∨ {v[b]}) ∧ ({v[c]} ∨ {v[d]}) :> vect K ->
{e[rp_val _ (do_elim x a b c d e)]} = {e[e]}.
Proof.
intros H; unfold do_elim.
case ecompare; simpl; auto; apply nielim_c; auto.
Qed.
Definition iter_elim n x a b c d e :=
iter_rp _ n (do_elim x a b c d) (More _ e).
Lemma iter_elim_c n x a b c d e :
{v[x]} = ({v[a]} ∨ {v[b]}) ∧ ({v[c]} ∨ {v[d]}) :> vect K ->
{e[rp_val _ (iter_elim n x a b c d e)]} = {e[e]}.
Proof.
unfold iter_elim; intros H.
apply trans_equal with {e[rp_val _ (More expr e)]}; auto.
generalize (More expr e); elim n; simpl; auto.
intros [r|r]; simpl; auto.
apply do_elim_c; auto.
intros n1 IH [e1|e1]; simpl; auto.
rewrite !IH; simpl; auto.
Qed.
(* Elimination of a semi-free point *)
Fixpoint free_elim (cc : coef) x a b c d (l : list tuple)
(accu: list tuple) e :=
match l with
| nil => einsert cc (rev _ accu l) e
| t ::l1 =>
if tin x t then
let (c1,t1) := tsort (tsubst x b t) in
let cc1 := cscale a (scmp c1 cc) in
let (c2,t2) := tsort (tsubst x d t) in
let cc2 := cscale c (scmp c2 cc) in
einsert0
cc1 (linsert t1 (rev _ accu l1))
(einsert0
cc2 (linsert t2 (rev _ accu l1))
e)
else free_elim cc x a b c d l1 (t ::accu) e
end.
Lemma free_elim_c cc x a b c d l accu e :
{v[x]} = {k[a]} .* {v[b]} + {k[c]} .* {v[d]} :> vect K ->
{e[free_elim cc x a b c d l accu e]} =
({c[cc]} * {l[l]} * {l[accu]} + {e[e]})%f.
Proof.
intros H.
generalize accu e; clear accu e; elim l; simpl; auto.
intros accu e; rewrite einsert_c; auto.
rewrite lrev_c; auto; simpl; ring.
intros t1 l1 IH accu e.
generalize (tsubst_c x t1); case tin.
2 : intros _; rewrite IH; simpl; ring.
intros (a1,(b1,(s1,(H1s1,H2s2)))).
generalize (tsort_c (tsubst x b t1)); case tsort.
intros s2 t2 Hs2.
generalize (tsort_c (tsubst x d t1)); case tsort.
intros s3 t3 Hs3.
rewrite !einsert0_c; auto.
rewrite !cscale_c, !scmp_c, !linsert_c, !lrev_c.
replace ({k[a]} * (s2k s2 * {c[cc]}) * ({t[t2]} * ({l[accu]} * {l[l1]})))%f
with
({k[a]} * (s2k s2 * {t[t2]}) * ({c[cc]} * ({l[accu]} * {l[l1]})))%f; try ring.
rewrite <-Hs2, H2s2; simpl.
replace ({k[c]} * (s2k s3 * {c[cc]}) * ({t[t3]} * ({l[accu]} * {l[l1]})))%f
with
({k[c]} * (s2k s3 * {t[t3]}) * ({c[cc]} * ({l[accu]} * {l[l1]})))%f; try ring.
rewrite <-Hs3, H2s2; simpl.
rewrite H1s1; simpl.
rewrite (bracket_free _ HP {v[x]} {k[a]} {v[b]} {k[c]} {v[d]}); auto.
change (VectorSpace.K Pp) with K.
ring.
Qed.