-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.thy
5604 lines (5200 loc) · 265 KB
/
Matrix.thy
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
(* Title: HOL/Matrix_LP/Matrix.thy
Author: Steven Obua
*)
theory Matrix
imports Main "~~/src/HOL/Library/Lattice_Algebras" Complex
begin
type_synonym infmatrix = "nat ⇒ nat ⇒ real"
consts infinite::nat
definition nonzero_positions :: "(nat ⇒ nat ⇒ real) ⇒ (nat × nat) set" where
"nonzero_positions A = {pos. A (fst pos) (snd pos) ~= 0}"
definition "matrix = {(f::(nat ⇒ nat ⇒ real)). finite (nonzero_positions f)∧
( (nonzero_positions f) ~={}⟶ Max (fst`(nonzero_positions f))<infinite∧Max (snd`(nonzero_positions f))<infinite ) }"
typedef matrix = "matrix :: (nat ⇒ nat ⇒ real) set"
unfolding matrix_def
proof
show "(λj i. 0) ∈ {(f::(nat ⇒ nat ⇒ real)). finite (nonzero_positions f)∧
((nonzero_positions f) ~={}⟶Max (fst`(nonzero_positions f))<infinite∧Max (snd`(nonzero_positions f))<infinite ) }"
apply (simp add: nonzero_positions_def)
done
qed
declare Rep_matrix_inverse[simp]
lemma finite_nonzero_positions : "finite (nonzero_positions (Rep_matrix A)) ∧
((nonzero_positions (Rep_matrix A)) ~={}⟶Max (fst`(nonzero_positions (Rep_matrix A)))<infinite∧Max (snd`(nonzero_positions (Rep_matrix A)))<infinite )"
apply(induct A)
apply(simp add:Abs_matrix_inverse matrix_def)
using Rep_matrix matrix_def by blast
definition nrows :: " matrix ⇒ nat" where
"nrows A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image fst) (nonzero_positions (Rep_matrix A))))"
definition ncols :: " matrix ⇒ nat" where
"ncols A == if nonzero_positions(Rep_matrix A) = {} then 0 else Suc(Max ((image snd) (nonzero_positions (Rep_matrix A))))"
lemma nrows_max[simp]:"nrows A≤infinite"
apply(simp add:nrows_def)
using finite_nonzero_positions by fastforce
lemma ncols_max[simp]:"ncols A≤infinite"
apply(simp add:ncols_def)
using finite_nonzero_positions by fastforce
lemma nrows:
assumes hyp: "nrows A ≤ m"
shows "(Rep_matrix A m n) = 0"
proof cases
assume "nonzero_positions(Rep_matrix A) = {}"
then show "(Rep_matrix A m n) = 0" by (simp add: nonzero_positions_def)
next
assume a: "nonzero_positions(Rep_matrix A) ≠ {}"
let ?S = "fst`(nonzero_positions(Rep_matrix A))"
have c: "finite (?S)" by (simp add: finite_nonzero_positions)
from hyp have d: "Max (?S) < m" by (simp add: a nrows_def)
have "m ∉ ?S"
proof -
have "m ∈ ?S ⟹ m <= Max(?S)" by (simp add: Max_ge [OF c])
moreover from d have "~(m <= Max ?S)" by (simp)
ultimately show "m ∉ ?S" by (auto)
qed
then show "Rep_matrix A m n = 0" by (simp add: nonzero_positions_def image_Collect)
qed
definition transpose_infmatrix :: " infmatrix ⇒ infmatrix" where
"transpose_infmatrix A j i == A i j"
definition transpose_matrix :: " matrix ⇒ matrix" where
"transpose_matrix == Abs_matrix o transpose_infmatrix o Rep_matrix"
declare transpose_infmatrix_def[simp]
(*ext (∀x. f x = g x) =⇒ f = g*)
lemma transpose_infmatrix_twice[simp]: "transpose_infmatrix (transpose_infmatrix A) = A"
by ((rule ext)+, simp)
lemma transpose_infmatrix: "transpose_infmatrix (% j i. P j i) = (% j i. P i j)"
apply (rule ext)+
by simp
lemma transpose_infmatrix_closed[simp]: "Rep_matrix (Abs_matrix (transpose_infmatrix (Rep_matrix x))) = transpose_infmatrix (Rep_matrix x)"
apply (rule Abs_matrix_inverse)
apply (simp add: matrix_def nonzero_positions_def image_def)
proof -
let ?A = "{pos. Rep_matrix x (snd pos) (fst pos) ≠ 0}"
let ?swap = "% pos. (snd pos, fst pos)"
let ?B = "{pos. Rep_matrix x (fst pos) (snd pos) ≠ 0}"
have swap_image: "?swap`?A = ?B"
apply (simp add: image_def)
apply (rule set_eqI)
apply (simp)
proof
fix y
assume hyp: "∃a b. Rep_matrix x b a ≠ 0 ∧ y = (b, a)"
thus "Rep_matrix x (fst y) (snd y) ≠ 0"
proof -
from hyp obtain a b where "(Rep_matrix x b a ≠ 0 & y = (b,a))" by blast
then show "Rep_matrix x (fst y) (snd y) ≠ 0" by (simp)
qed
next
fix y
assume hyp: "Rep_matrix x (fst y) (snd y) ≠ 0"
show "∃ a b. (Rep_matrix x b a ≠ 0 & y = (b,a))"
by (rule exI[of _ "snd y"], rule exI[of _ "fst y"]) (simp add: hyp)
qed
then have "finite (?swap`?A)"
proof -
have "finite (nonzero_positions (Rep_matrix x))" by (simp add: finite_nonzero_positions)
then have "finite ?B" by (simp add: nonzero_positions_def)
with swap_image show "finite (?swap`?A)" by (simp)
qed
moreover
have "inj_on ?swap ?A" by (simp add: inj_on_def)
have " finite {pos. Rep_matrix x (snd pos) (fst pos) ≠ 0} "
using `inj_on (λpos. (snd pos, fst pos)) {pos. Rep_matrix x (snd pos) (fst pos) ≠ 0}` calculation finite_imageD by blast
have " ((∃a b. Rep_matrix x b a ≠ 0) ⟶ Max {y. ∃b. Rep_matrix x b y ≠ 0} < infinite
∧ Max {y. ∃a. Rep_matrix x y a ≠ 0} < infinite)"
proof -
have " (nonzero_positions (Rep_matrix x)) ~={}⟶Max (fst`(nonzero_positions (Rep_matrix x)))<infinite
∧Max (snd`(nonzero_positions (Rep_matrix x)))<infinite"
using finite_nonzero_positions by blast
then have "((nonzero_positions (Rep_matrix x)) ~={}) = (∃a b. Rep_matrix x b a ≠ 0)"
by (metis (mono_tags, lifting) Collect_empty_eq le0 nonzero_positions_def nrows nrows_def)
then have " fst ` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = fst ` {(a,b). Rep_matrix x a b ≠ 0}"
by (metis (mono_tags, lifting) Collect_cong split_beta')
then have "fst ` {(a,b). Rep_matrix x a b ≠ 0} ={y. ∃a. Rep_matrix x y a ≠ 0}"
apply(auto)
by force
then have "fst ` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0}={y. ∃a. Rep_matrix x y a ≠ 0}"
by (simp add: `fst \` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = fst \` {(a, b). Rep_matrix x a b ≠ 0}`)
then have " snd ` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = snd ` {(a,b). Rep_matrix x a b ≠ 0}"
by (metis (mono_tags, lifting) Collect_cong split_beta')
then have "snd ` {(a,b). Rep_matrix x a b ≠ 0} ={y. ∃b. Rep_matrix x b y ≠ 0}"
apply(auto)
by force
then have "snd ` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0}= {y. ∃b. Rep_matrix x b y ≠ 0}"
by (simp add: `snd \` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = snd \` {(a, b). Rep_matrix x a b ≠ 0}`)
show " ((∃a b. Rep_matrix x b a ≠ 0) ⟶ Max {y. ∃b. Rep_matrix x b y ≠ 0} < infinite
∧ Max {y. ∃a. Rep_matrix x y a ≠ 0} < infinite)"
using `fst \` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = {y. ∃a. Rep_matrix x y a ≠ 0}` `nonzero_positions (Rep_matrix x) ≠ {} ⟶ Max (fst \` nonzero_positions (Rep_matrix x)) < infinite ∧ Max (snd \` nonzero_positions (Rep_matrix x)) < infinite` `snd \` {pos. Rep_matrix x (fst pos) (snd pos) ≠ 0} = {y. ∃b. Rep_matrix x b y ≠ 0}` nonzero_positions_def by auto
qed
then show " finite {pos. Rep_matrix x (snd pos) (fst pos) ≠ 0} ∧
((∃a b. Rep_matrix x b a ≠ 0) ⟶ Max {y. ∃b. Rep_matrix x b y ≠ 0} < infinite ∧ Max {y. ∃a. Rep_matrix x y a ≠ 0} < infinite)"
using `finite {pos. Rep_matrix x (snd pos) (fst pos) ≠ 0}` by blast
qed
lemma infmatrixforward: "(x:: infmatrix) = y ⟹ ∀ a b. x a b = y a b" by auto
lemma transpose_infmatrix_inject: "(transpose_infmatrix A = transpose_infmatrix B) = (A = B)"
apply (auto)
apply (rule ext)+
apply (simp add: transpose_infmatrix)
apply (drule infmatrixforward)
apply (simp)
done
lemma transpose_matrix_inject: "(transpose_matrix A = transpose_matrix B) = (A = B)"
apply (simp add: transpose_matrix_def)
apply (subst Rep_matrix_inject[THEN sym])+
apply (simp only: transpose_infmatrix_closed transpose_infmatrix_inject)
done
lemma transpose_matrix[simp]: "Rep_matrix(transpose_matrix A) j i = Rep_matrix A i j"
by (simp add: transpose_matrix_def)
lemma transpose_transpose_id[simp]: "transpose_matrix (transpose_matrix A) = A"
by (simp add: transpose_matrix_def)
lemma nrows_transpose[simp]: "nrows (transpose_matrix A) = ncols A"
by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
lemma ncols_transpose[simp]: "ncols (transpose_matrix A) = nrows A"
by (simp add: nrows_def ncols_def nonzero_positions_def transpose_matrix_def image_def)
lemma ncols: "ncols A <= n ⟹ Rep_matrix A m n = 0"
proof -
assume "ncols A <= n"
then have "nrows (transpose_matrix A) <= n" by (simp)
then have "Rep_matrix (transpose_matrix A) n m = 0" by (rule nrows)
thus "Rep_matrix A m n = 0" by (simp add: transpose_matrix_def)
qed
lemma ncols_le: "(ncols A <= n) = (! j i. n <= i ⟶ (Rep_matrix A j i) = 0)" (is "_ = ?st")
apply (auto)
apply (simp add: ncols)
proof (simp add: ncols_def, auto)
let ?P = "nonzero_positions (Rep_matrix A)"
let ?p = "snd`?P"
have a:"finite ?p" by (simp add: finite_nonzero_positions)
let ?m = "Max ?p"
assume "~(Suc (?m) <= n)"
then have b:"n <= ?m" by (simp)
fix a b
assume "(a,b) ∈ ?P"
then have "?p ≠ {}" by (auto)
with a have "?m ∈ ?p" by (simp)
moreover have "!x. (x ∈ ?p ⟶ (? y. (Rep_matrix A y x) ≠ 0))" by (simp add: nonzero_positions_def image_def)
ultimately have "? y. (Rep_matrix A y ?m) ≠ 0" by (simp)
moreover assume ?st
ultimately show "False" using b by (simp)
qed
lemma less_ncols: "(n < ncols A) = (? j i. n <= i & (Rep_matrix A j i) ≠ 0)"
proof -
have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
show ?thesis by (simp add: a ncols_le)
qed
lemma le_ncols: "(n <= ncols A) = (∀ m. (∀ j i. m <= i ⟶ (Rep_matrix A j i) = 0) ⟶ n <= m)"
apply (auto)
apply (subgoal_tac "ncols A <= m")
apply (simp)
apply (simp add: ncols_le)
apply (drule_tac x="ncols A" in spec)
by (simp add: ncols)
lemma nrows_le: "(nrows A <= n) = (! j i. n <= j ⟶ (Rep_matrix A j i) = 0)" (is ?s)
proof -
have "(nrows A <= n) = (ncols (transpose_matrix A) <= n)" by (simp)
also have "… = (! j i. n <= i ⟶ (Rep_matrix (transpose_matrix A) j i = 0))" by (rule ncols_le)
also have "… = (! j i. n <= i ⟶ (Rep_matrix A i j) = 0)" by (simp)
finally show "(nrows A <= n) = (! j i. n <= j ⟶ (Rep_matrix A j i) = 0)" by (auto)
qed
lemma less_nrows: "(m < nrows A) = (? j i. m <= j & (Rep_matrix A j i) ≠ 0)"
proof -
have a: "!! (a::nat) b. (a < b) = (~(b <= a))" by arith
show ?thesis by (simp add: a nrows_le)
qed
lemma le_nrows: "(n <= nrows A) = (∀ m. (∀ j i. m <= j ⟶ (Rep_matrix A j i) = 0) ⟶ n <= m)"
apply (auto)
apply (subgoal_tac "nrows A <= m")
apply (simp)
apply (simp add: nrows_le)
apply (drule_tac x="nrows A" in spec)
by (simp add: nrows)
lemma nrows_notzero: "Rep_matrix A m n ≠ 0 ⟹ m < nrows A"
apply (case_tac "nrows A <= m")
apply (simp_all add: nrows)
done
lemma ncols_notzero: "Rep_matrix A m n ≠ 0 ⟹ n < ncols A"
apply (case_tac "ncols A <= n",auto)
apply (simp_all add: ncols)
done
lemma finite_natarray1: "finite {x. x < (n::nat)}"
apply (induct n)
apply (simp)
proof -
fix n
have "{x. x < Suc n} = insert n {x. x < n}" by (rule set_eqI, simp, arith)
moreover assume "finite {x. x < n}"
ultimately show "finite {x. x < Suc n}" by (simp)
qed
lemma finite_natarray2: "finite {(x, y). x < (m::nat) & y < (n::nat)}"
by simp
lemma RepAbs_matrix:
assumes aem: "? m<=infinite. ! j i. m <= j ⟶ x j i = 0" (is ?em) and aen:"? n<=infinite. ! j i. (n <= i ⟶ x j i = 0)" (is ?en)
shows "(Rep_matrix (Abs_matrix x)) = x"
apply (rule Abs_matrix_inverse)
apply (simp add: matrix_def nonzero_positions_def)
proof -
from aem obtain m where a: "! j i. m <= j ⟶ x j i = 0" by (blast)
from aen obtain n where b: "! j i. n <= i ⟶ x j i = 0" by (blast)
let ?u = "{(i, j). x i j ≠ 0}"
let ?v = "{(i, j). i < m & j < n}"
have c: "!! (m::nat) a. ~(m <= a) ⟹ a < m" by (arith)
from a b have "(?u ∩ (-?v)) = {}"
apply (simp)
apply (rule set_eqI)
apply (simp)
apply auto
by (rule c, auto)+
then have d: "?u ⊆ ?v" by blast
moreover have "finite ?v" by (simp add: finite_natarray2)
moreover have "{pos. x (fst pos) (snd pos) ≠ 0} = ?u" by auto
have "finite {pos. x (fst pos) (snd pos) ≠ 0}"
using `{pos. x (fst pos) (snd pos) ≠ 0} = {(i, j). x i j ≠ 0}` d finite_subset by fastforce
have aa:"! j i. infinite <= j ⟶ x j i = 0" using aem by auto
have bb:"! j i. infinite <= i ⟶ x j i = 0" using aen less_le_trans by auto
have "fst ` {pos. x (fst pos) (snd pos) ≠ 0}= fst ` {(a,b). x a b ≠ 0}"
by (simp add: `{pos. x (fst pos) (snd pos) ≠ 0} = {(i, j). x i j ≠ 0}`)
have " fst ` {(a,b). x a b ≠ 0}={y. ∃a. x y a ≠ 0}"
apply auto
by force
have "fst ` {pos. x (fst pos) (snd pos) ≠ 0}={y. ∃a. x y a ≠ 0}"
by (simp add: `fst \` {(a, b). x a b ≠ 0} = {y. ∃a. x y a ≠ 0}` `fst \` {pos. x (fst pos) (snd pos) ≠ 0} = fst \` {(a, b). x a b ≠ 0}`)
have a:"{y. ∃a. x y a ≠ 0}≠{}∧Max {y. ∃a. x y a ≠ 0} ≥ infinite ⟹ (∃ yy≥ infinite. yy∈{y. ∃a. x y a ≠ 0})"
by (metis (no_types, lifting) Max_in `finite {pos. x (fst pos) (snd pos) ≠ 0}` `fst \` {(a, b). x a b ≠ 0} = {y. ∃a. x y a ≠ 0}` `{pos. x (fst pos) (snd pos) ≠ 0} = {(i, j). x i j ≠ 0}` finite_imageI)
have "(∃a b. x a b ≠ 0) ⟶ Max ({y. ∃a. x y a ≠ 0}) < infinite"
apply auto
using a aa c by blast
have "snd ` {pos. x (fst pos) (snd pos) ≠ 0}= snd ` {(a,b). x a b ≠ 0}"
by (simp add: `{pos. x (fst pos) (snd pos) ≠ 0} = {(i, j). x i j ≠ 0}`)
have " snd ` {(a,b). x a b ≠ 0}={y. ∃a. x a y ≠ 0}"
apply auto
by force
have "(snd ` {pos. x (fst pos) (snd pos) ≠ 0}) = {y. ∃a. x a y ≠ 0}"
by (simp add: `snd \` {(a, b). x a b ≠ 0} = {y. ∃a. x a y ≠ 0}` `snd \` {pos. x (fst pos) (snd pos) ≠ 0} = snd \` {(a, b). x a b ≠ 0}`)
have cc:" {y. ∃a. x a y ≠ 0}≠{}∧Max {y. ∃a. x a y ≠ 0} ≥ infinite ⟹ ? yy. yy≥infinite∧yy∈{y. ∃a. x a y ≠ 0}" sledgehammer
by (metis (no_types, lifting) Max_in `finite {pos. x (fst pos) (snd pos) ≠ 0}` `snd \` {(a, b). x a b ≠ 0} = {y. ∃a. x a y ≠ 0}` `{pos. x (fst pos) (snd pos) ≠ 0} = {(i, j). x i j ≠ 0}` finite_imageI)
have "(∃a b. x a b ≠ 0) ⟶ Max ({y. ∃a. x a y ≠ 0}) < infinite"
using bb c cc by blast
have " ((∃a b. x a b ≠ 0) ⟶ Max (fst ` {pos. x (fst pos) (snd pos) ≠ 0}) < infinite
∧ Max (snd ` {pos. x (fst pos) (snd pos) ≠ 0}) < infinite)"
by (simp add: `(∃a b. x a b ≠ 0) ⟶ Max {y. ∃a. x a y ≠ 0} < infinite` `(∃a b. x a b ≠ 0) ⟶ Max {y. ∃a. x y a ≠ 0} < infinite` `fst \` {pos. x (fst pos) (snd pos) ≠ 0} = {y. ∃a. x y a ≠ 0}` `snd \` {(a, b). x a b ≠ 0} = {y. ∃a. x a y ≠ 0}` `snd \` {pos. x (fst pos) (snd pos) ≠ 0} = snd \` {(a, b). x a b ≠ 0}`)
ultimately show " finite {pos. x (fst pos) (snd pos) ≠ 0} ∧
((∃a b. x a b ≠ 0) ⟶ Max (fst ` {pos. x (fst pos) (snd pos) ≠ 0}) < infinite ∧ Max (snd ` {pos. x (fst pos) (snd pos) ≠ 0}) < infinite)"
using `finite {pos. x (fst pos) (snd pos) ≠ 0}` by blast
qed
definition apply_infmatrix :: "(real ⇒ real) ⇒ infmatrix ⇒ infmatrix" where
"apply_infmatrix f == % A. (% j i. f (A j i))"
definition apply_matrix :: "(real ⇒ real) ⇒ matrix ⇒ matrix" where
"apply_matrix f == % A. Abs_matrix (apply_infmatrix f (Rep_matrix A))"
definition combine_infmatrix :: "(real ⇒ real ⇒ real) ⇒ infmatrix ⇒infmatrix ⇒infmatrix" where
"combine_infmatrix f == % A B. (% j i. f (A j i) (B j i))"
definition combine_matrix :: "(real ⇒ real ⇒ real) ⇒matrix ⇒matrix ⇒ matrix" where
"combine_matrix f == % A B. Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))"
lemma expand_apply_infmatrix[simp]: "apply_infmatrix f A j i = f (A j i)"
by (simp add: apply_infmatrix_def)
lemma expand_combine_infmatrix[simp]: "combine_infmatrix f A B j i = f (A j i) (B j i)"
by (simp add: combine_infmatrix_def)
definition commutative :: "('a ⇒ 'a ⇒ 'a) ⇒ bool" where
"commutative f == ! x y. f x y = f y x"
definition associative :: "('a ⇒ 'a ⇒ 'a) ⇒ bool" where
"associative f == ! x y z. f (f x y) z = f x (f y z)"
text{*
To reason about associativity and commutativity of operations on matrices,
let's take a step back and look at the general situtation: Assume that we have
sets $A$ and $B$ with $B \subset A$ and an abstraction $u: A \rightarrow B$. This abstraction has to fulfill $u(b) = b$ for all $b \in B$, but is arbitrary otherwise.
Each function $f: A \times A \rightarrow A$ now induces a function $f': B \times B \rightarrow B$ by $f' = u \circ f$.
It is obvious that commutativity of $f$ implies commutativity of $f'$: $f' x y = u (f x y) = u (f y x) = f' y x.$
*}
lemma combine_infmatrix_commute:
"commutative f ⟹ commutative (combine_infmatrix f)"
by (simp add: commutative_def combine_infmatrix_def)
lemma combine_matrix_commute:
"commutative f ⟹ commutative (combine_matrix f)"
by (simp add: combine_matrix_def commutative_def combine_infmatrix_def)
text{*
On the contrary, given an associative function $f$ we cannot expect $f'$ to be associative. A counterexample is given by $A=\ganz$, $B=\{-1, 0, 1\}$,
as $f$ we take addition on $\ganz$, which is clearly associative. The abstraction is given by $u(a) = 0$ for $a \notin B$. Then we have
\[ f' (f' 1 1) -1 = u(f (u (f 1 1)) -1) = u(f (u 2) -1) = u (f 0 -1) = -1, \]
but on the other hand we have
\[ f' 1 (f' 1 -1) = u (f 1 (u (f 1 -1))) = u (f 1 0) = 1.\]
A way out of this problem is to assume that $f(A\times A)\subset A$ holds, and this is what we are going to do:
*}
lemma nonzero_positions_combine_infmatrix[simp]: "f 0 0 = 0 ⟹ nonzero_positions (combine_infmatrix f A B) ⊆ (nonzero_positions A) ∪ (nonzero_positions B)"
apply(rule subsetI)
apply(simp add:nonzero_positions_def)
apply auto
done
lemma finite_nonzero_positions_Rep[simp]: "finite (nonzero_positions (Rep_matrix A))"
by (insert Rep_matrix [of A], simp add: matrix_def)
lemma combine_infmatrix_closed [simp]: "f 0 0 = 0 ⟹ Rep_matrix (Abs_matrix (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) = combine_infmatrix f (Rep_matrix A) (Rep_matrix B)"
apply (rule Abs_matrix_inverse)
apply (simp add: matrix_def)
proof -
have g:" f 0 0 = 0 ⟹ finite (nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))"
apply (rule finite_subset[of _ "(nonzero_positions (Rep_matrix A)) ∪ (nonzero_positions (Rep_matrix B))"])
by (simp_all)
have d:" (fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) =
fst ` {(a,b). (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) a b ≠ 0}"
by (metis (no_types, lifting) Collect_cong nonzero_positions_def split_beta')
have e:" fst ` {(a,b). (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) a b ≠ 0}=
{y. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) y a≠0 }"
apply auto
apply force
done
have c:"{y. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) y a≠0 }=
(fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))"
using d e by auto
have a: "f 0 0=0⟹{y. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) y a≠0 }≠{}
∧ Max {y. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) y a≠0 } ≥ infinite⟹
∃yy≥infinite. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) yy a≠0"
proof -
assume a1: "{y. ∃a. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) y a ≠ 0} ≠ {} ∧ infinite ≤ Max {y. ∃a. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) y a ≠ 0}"
assume a2: "f 0 0 = 0"
have f3: "infinite ≤ Max {n. ∃na. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) n na ≠ 0}"
using a1 by linarith
have "finite (fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))"
using a2 by (simp add: g)
hence "Max {n. ∃na. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) n na ≠ 0} ∈ {n. ∃na. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) n na ≠ 0}"
by (metis Max_in a1 c)
thus ?thesis
using f3 by blast
qed
have b: " f 0 0=0⟹(snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))≠{}
∧Max (snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) ≥ infinite⟹
∃yy. yy≥infinite∧ yy∈{y. ∃a. (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) a y ≠0}"
proof -
assume a1: "f 0 0 = 0"
assume a2: "snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) ≠ {} ∧ infinite ≤ Max (snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))"
have f3: "∀f r. f ` r = {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
by blast
hence "(snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) ≠ {}) = ({n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = snd p} ≠ {})"
using nonzero_positions_def by presburger
hence f4: "{n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = snd p} ≠ {}"
using a2 by blast
have f5: "∀r f. ¬ finite r ∨ finite {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
using f3 by (metis (no_types) finite_imageI)
have "finite {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0}"
using a1 g nonzero_positions_def by fastforce
hence f6: "finite {n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = snd p}"
using f5 by blast
have "Max {n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = snd p} ∈ Collect (op ≤ infinite)"
using f3 a2 by (simp add: nonzero_positions_def)
thus ?thesis
using f6 f4 Max_in by fastforce
qed
have c :"f 0 0=0⟹∀a≥infinite.∀b.(combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) a b=0"
using dual_order.trans nrows by force
have d:"f 0 0=0⟹∀a≥infinite.∀b.(combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) b a=0"
using dual_order.trans ncols by force
have e:" f 0 0 = 0 ⟹(fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))≠{}⟹ Max (fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) < infinite"
proof -
assume a1: "fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) ≠ {}"
assume a2: "f 0 0 = 0"
have f3: "∀f r. f ` r = {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
by (simp add: Bex_def_raw image_def)
have f4: "{n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = fst p} ≠ {}"
using a1 nonzero_positions_def by fastforce
have f5: "finite {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0}"
using a2 g nonzero_positions_def by force
have "∀r f. ¬ finite r ∨ finite {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
using f3 by (metis (no_types) finite_imageI)
hence "finite {n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = fst p}"
using f5 by presburger
hence "infinite ∈ Collect (op < (Max {n. ∃p. p ∈ {p. combine_infmatrix f (Rep_matrix A) (Rep_matrix B) (fst p) (snd p) ≠ 0} ∧ n = fst p}))"
using f4 a2 Max_in c by fastforce
thus ?thesis
using f3 by (simp add: nonzero_positions_def)
qed
have f:" f 0 0 = 0 ⟹
(nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) ≠ {} ⟶
Max (fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) < infinite ∧
Max (snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) < infinite)"
using b d e not_less by auto
show " f 0 0 = 0 ⟹
finite (nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) ∧
(nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)) ≠ {} ⟶
Max (fst ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) < infinite ∧
Max (snd ` nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B))) < infinite)"
using `f 0 0 = 0 ⟹ finite (nonzero_positions (combine_infmatrix f (Rep_matrix A) (Rep_matrix B)))` f by blast
qed
text {* We need the next two lemmas only later, but it is analog to the above one, so we prove them now: *}
lemma nonzero_positions_apply_infmatrix[simp]: "f 0 = 0 ⟹ nonzero_positions (apply_infmatrix f A) ⊆ nonzero_positions A"
by (rule subsetI, simp add: nonzero_positions_def apply_infmatrix_def, auto)
lemma apply_infmatrix_closed [simp]: "f 0 = 0 ⟹ Rep_matrix (Abs_matrix (apply_infmatrix f (Rep_matrix A))) = apply_infmatrix f (Rep_matrix A)"
apply (rule Abs_matrix_inverse)
apply (simp add: matrix_def)
proof -
have a:"f 0 = 0 ⟹ finite (nonzero_positions (apply_infmatrix f (Rep_matrix A)))"
by (meson finite_nonzero_positions_Rep nonzero_positions_apply_infmatrix rev_finite_subset)
have bb: " f 0=0⟹(snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A) ))≠{}
∧Max (snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A) )) ≥ infinite⟹
∃yy. yy≥infinite∧ yy∈{y. ∃a. (apply_infmatrix f (Rep_matrix A)) a y ≠0}"
proof -
assume a1: "f 0 = 0"
assume a2: "snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {} ∧ infinite ≤ Max (snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A)))"
have f3: "∀f r. f ` r = {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
by blast
hence "(snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {}) = ({n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = snd p} ≠ {})"
using nonzero_positions_def by presburger
hence f4: "{n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = snd p} ≠ {}"
using a2 by blast
have f5: "∀r f. ¬ finite r ∨ finite {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
using f3 by (metis (no_types) finite_imageI)
have "finite {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0}"
using a1 a nonzero_positions_def by fastforce
hence "finite {n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = snd p}"
using f5 by blast
hence "∃n. apply_infmatrix f (Rep_matrix A) n (Max {n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = snd p}) ≠ 0"
using f4 Max_in by fastforce
thus ?thesis
using f3 a2 nonzero_positions_def by force
qed
have b: " f 0=0⟹(fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A) ))≠{}
∧Max (fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A) )) ≥ infinite⟹
∃yy. yy≥infinite∧ yy∈{y. ∃a. (apply_infmatrix f (Rep_matrix A)) y a ≠0}"
proof -
assume a1: "f 0 = 0"
assume a2: "fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {} ∧ infinite ≤ Max (fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A)))"
have f3: "∀f r. f ` r = {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
by blast
hence "(fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {}) = ({n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = fst p} ≠ {})"
using nonzero_positions_def by presburger
hence f4: "{n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = fst p} ≠ {}"
using a2 by blast
have f5: "∀r f. ¬ finite r ∨ finite {n. ∃p. (p∷nat × nat) ∈ r ∧ (n∷nat) = f p}"
using f3 by (metis (no_types) finite_imageI)
have "finite {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0}"
using a1 a nonzero_positions_def by fastforce
hence "finite {n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = fst p}"
using f5 by blast
hence "∃n. apply_infmatrix f (Rep_matrix A) (Max {n. ∃p. p ∈ {p. apply_infmatrix f (Rep_matrix A) (fst p) (snd p) ≠ 0} ∧ n = fst p}) n ≠ 0"
using f4 Max_in by fastforce
thus ?thesis
using f3 a2 nonzero_positions_def by auto
qed
have cc:"f 0=0⟹∀a.∀b≥infinite. (apply_infmatrix f (Rep_matrix A)) b a=0"
by (metis expand_apply_infmatrix ncols_max ncols_transpose nrows_le)
have c:"f 0 = 0 ⟹nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {} ⟹ Max (fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A))) < infinite"
using b cc by auto
have c:"f 0=0⟹∀a.∀b≥infinite. (apply_infmatrix f (Rep_matrix A)) a b=0"
by (metis expand_apply_infmatrix le_trans ncols ncols_max)
have d:"f 0 = 0 ⟹nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {} ⟹ Max (snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A))) < infinite"
using bb c by auto
show " f 0 = 0 ⟹
finite (nonzero_positions (apply_infmatrix f (Rep_matrix A))) ∧
(nonzero_positions (apply_infmatrix f (Rep_matrix A)) ≠ {} ⟶
Max (fst ` nonzero_positions (apply_infmatrix f (Rep_matrix A))) < infinite ∧
Max (snd ` nonzero_positions (apply_infmatrix f (Rep_matrix A))) < infinite) "
using a b cc d by fastforce
qed
lemma combine_infmatrix_assoc[simp]: "f 0 0 = 0 ⟹ associative f ⟹ associative (combine_infmatrix f)"
by (simp add: associative_def combine_infmatrix_def)
lemma comb: "f = g ⟹ x = y ⟹ f x = g y"
by (auto)
lemma combine_matrix_assoc: "f 0 0 = 0 ⟹ associative f ⟹ associative (combine_matrix f)"
apply (simp(no_asm) add: associative_def combine_matrix_def, auto)
apply (rule comb [of Abs_matrix Abs_matrix])
by (auto, insert combine_infmatrix_assoc[of f], simp add: associative_def)
lemma Rep_apply_matrix[simp]: "f 0 = 0 ⟹ Rep_matrix (apply_matrix f A) j i = f (Rep_matrix A j i)"
by (simp add: apply_matrix_def)
lemma Rep_combine_matrix[simp]: "f 0 0 = 0 ⟹ Rep_matrix (combine_matrix f A B) j i = f (Rep_matrix A j i) (Rep_matrix B j i)"
by(simp add: combine_matrix_def)
lemma combine_nrows_max: "f 0 0 = 0 ⟹ nrows (combine_matrix f A B) <= max (nrows A) (nrows B)"
by (simp add: nrows_le)
lemma combine_ncols_max: "f 0 0 = 0 ⟹ ncols (combine_matrix f A B) <= max (ncols A) (ncols B)"
by (simp add: ncols_le)
lemma combine_nrows: "f 0 0 = 0 ⟹ nrows A <= q ⟹ nrows B <= q ⟹ nrows(combine_matrix f A B) <= q"
by (simp add: nrows_le)
lemma combine_ncols: "f 0 0 = 0 ⟹ ncols A <= q ⟹ ncols B <= q ⟹ ncols(combine_matrix f A B) <= q"
by (simp add: ncols_le)
definition zero_r_neutral :: "('a ⇒ 'b::zero ⇒ 'a) ⇒ bool" where
"zero_r_neutral f == ! a. f a 0 = a"
definition zero_l_neutral :: "('a::zero ⇒ 'b ⇒ 'b) ⇒ bool" where
"zero_l_neutral f == ! a. f 0 a = a"
definition zero_closed :: "(('a::zero) ⇒ ('b::zero) ⇒ ('c::zero)) ⇒ bool" where
"zero_closed f == (!x. f x 0 = 0) & (!y. f 0 y = 0)"
(* calculate A*B *)
primrec foldseq :: "('a ⇒ 'a ⇒ 'a) ⇒ (nat ⇒ 'a) ⇒ nat ⇒ 'a"
where
"foldseq f s 0 = s 0"
| "foldseq f s (Suc n) = f (s 0) (foldseq f (% k. s(Suc k)) n)"
primrec foldseq_transposed :: "('a ⇒ 'a ⇒ 'a) ⇒ (nat ⇒ 'a) ⇒ nat ⇒ 'a"
where
"foldseq_transposed f s 0 = s 0"
| "foldseq_transposed f s (Suc n) = f (foldseq_transposed f s n) (s (Suc n))"
lemma foldseq_assoc : "associative f ⟹ foldseq f = foldseq_transposed f"
proof -
assume a:"associative f"
then have sublemma: "!! n. ! N s. N <= n ⟶ foldseq f s N = foldseq_transposed f s N"
proof -
fix n
show "!N s. N <= n ⟶ foldseq f s N = foldseq_transposed f s N"
proof (induct n)
show "!N s. N <= 0 ⟶ foldseq f s N = foldseq_transposed f s N" by simp
next
fix n
assume b:"! N s. N <= n ⟶ foldseq f s N = foldseq_transposed f s N"
have c:"!!N s. N <= n ⟹ foldseq f s N = foldseq_transposed f s N" by (simp add: b)
show "! N t. N <= Suc n ⟶ foldseq f t N = foldseq_transposed f t N"
proof (auto)
fix N t
assume Nsuc: "N <= Suc n"
show "foldseq f t N = foldseq_transposed f t N"
proof cases
assume "N <= n"
then show "foldseq f t N = foldseq_transposed f t N" by (simp add: b)
next
assume "~(N <= n)"
with Nsuc have Nsuceq: "N = Suc n" by simp
have neqz: "n ≠ 0 ⟹ ? m. n = Suc m & Suc m <= n" by arith
have assocf: "!! x y z. f x (f y z) = f (f x y) z" by (insert a, simp add: associative_def)
show "foldseq f t N = foldseq_transposed f t N"
apply (simp add: Nsuceq)
apply (subst c)
apply (simp)
apply (case_tac "n = 0")
apply (simp)
apply (drule neqz)
apply (erule exE)
apply (simp)
apply (subst assocf)
proof -
fix m
assume "n = Suc m & Suc m <= n"
then have mless: "Suc m <= n" by arith
then have step1: "foldseq_transposed f (% k. t (Suc k)) m = foldseq f (% k. t (Suc k)) m" (is "?T1 = ?T2")
apply (subst c)
by simp+
have step2: "f (t 0) ?T2 = foldseq f t (Suc m)" (is "_ = ?T3") by simp
have step3: "?T3 = foldseq_transposed f t (Suc m)" (is "_ = ?T4")
apply (subst c)
by (simp add: mless)+
have step4: "?T4 = f (foldseq_transposed f t m) (t (Suc m))" (is "_=?T5") by simp
from step1 step2 step3 step4 show sowhat: "f (f (t 0) ?T1) (t (Suc (Suc m))) = f ?T5 (t (Suc (Suc m)))" by simp
qed
qed
qed
qed
qed
show "foldseq f = foldseq_transposed f" by ((rule ext)+, insert sublemma, auto)
qed
lemma foldseq_distr: "⟦associative f; commutative f⟧ ⟹ foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)"
proof -
assume assoc: "associative f"
assume comm: "commutative f"
from assoc have a:"!! x y z. f (f x y) z = f x (f y z)" by (simp add: associative_def)
from comm have b: "!! x y. f x y = f y x" by (simp add: commutative_def)
from assoc comm have c: "!! x y z. f x (f y z) = f y (f x z)" by (simp add: commutative_def associative_def)
have "!! n. (! u v. foldseq f (%k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n))"
apply (induct_tac n)
apply (simp+, auto)
by (simp add: a b c)
then show "foldseq f (% k. f (u k) (v k)) n = f (foldseq f u n) (foldseq f v n)" by simp
qed
theorem "⟦associative f; associative g; ∀a b c d. g (f a b) (f c d) = f (g a c) (g b d); ? x y. (f x) ≠ (f y); ? x y. (g x) ≠ (g y); f x x = x; g x x = x⟧ ⟹ f=g | (! y. f y x = y) | (! y. g y x = y)"
oops
(* Model found
Trying to find a model that refutes: ⟦associative f; associative g;
∀a b c d. g (f a b) (f c d) = f (g a c) (g b d); ∃x y. f x ≠ f y;
∃x y. g x ≠ g y; f x x = x; g x x = x⟧
⟹ f = g ∨ (∀y. f y x = y) ∨ (∀y. g y x = y)
Searching for a model of size 1, translating term... invoking SAT solver... no model found.
Searching for a model of size 2, translating term... invoking SAT solver... no model found.
Searching for a model of size 3, translating term... invoking SAT solver...
Model found:
Size of types: 'a: 3
x: a1
g: (a0↦(a0↦a1, a1↦a0, a2↦a1), a1↦(a0↦a0, a1↦a1, a2↦a0), a2↦(a0↦a1, a1↦a0, a2↦a1))
f: (a0↦(a0↦a0, a1↦a0, a2↦a0), a1↦(a0↦a1, a1↦a1, a2↦a1), a2↦(a0↦a0, a1↦a0, a2↦a0))
*)
lemma foldseq_zero:
assumes fz: "f 0 0 = 0" and sz: "! i. i <= n ⟶ s i = 0"
shows "foldseq f s n = 0"
proof -
have "!! n. ! s. (! i. i <= n ⟶ s i = 0) ⟶ foldseq f s n = 0"
apply (induct_tac n)
apply (simp)
by (simp add: fz)
then show "foldseq f s n = 0"
by (simp add: sz)
qed
lemma foldseq_transposed_zero:
"associative f⟹f 0 0 = 0 ⟹! i. i <= n ⟶ s i = 0⟹foldseq_transposed f s n = 0"
apply(subgoal_tac "foldseq_transposed f s n =foldseq f s n")
prefer 2
apply (simp add: foldseq_assoc)
by (simp add: foldseq_zero)
lemma foldseq1_significant_positions:
assumes p: "! i. i <= N ⟶ S i = T i"
shows "foldseq_transposed f S N = foldseq_transposed f T N"
proof -
have "!! m . ! s t. (! i. i<=m ⟶ s i = t i) ⟶ foldseq_transposed f s m = foldseq_transposed f t m"
apply (induct_tac m)
apply (simp)
apply (simp)
apply (auto)
proof -
fix n
fix s::"nat⇒'a"
fix t::"nat⇒'a"
assume a: "∀s t. (∀i≤n. s i = t i) ⟶ foldseq_transposed f s n = foldseq_transposed f t n"
assume b: "∀i≤Suc n. s i = t i"
have c:"!! a b. a = b ⟹ f (t 0) a = f (t 0) b" by blast
have d:"!! s t. (∀i≤n. s i = t i) ⟹foldseq_transposed f s n = foldseq_transposed f t n" by (simp add: a)
show " f (foldseq_transposed f s n) (t (Suc n)) = f (foldseq_transposed f t n) (t (Suc n))" by (metis a b le_SucI)
qed
with p show ?thesis by simp
qed
lemma foldseq_significant_positions:
assumes p: "! i. i <= N ⟶ S i = T i"
shows "foldseq f S N = foldseq f T N"
proof -
have "!! m . ! s t. (! i. i<=m ⟶ s i = t i) ⟶ foldseq f s m = foldseq f t m"
apply (induct_tac m)
apply (simp)
apply (simp)
apply (auto)
proof -
fix n
fix s::"nat⇒'a"
fix t::"nat⇒'a"
assume a: "∀s t. (∀i≤n. s i = t i) ⟶ foldseq f s n = foldseq f t n"
assume b: "∀i≤Suc n. s i = t i"
have c:"!! a b. a = b ⟹ f (t 0) a = f (t 0) b" by blast
have d:"!! s t. (∀i≤n. s i = t i) ⟹ foldseq f s n = foldseq f t n" by (simp add: a)
show "f (t 0) (foldseq f (λk. s (Suc k)) n) = f (t 0) (foldseq f (λk. t (Suc k)) n)" by (rule c, simp add: d b)
qed
with p show ?thesis by simp
qed
lemma foldseq_tail:
assumes "M <= N"
shows "foldseq f S N = foldseq f (% k. (if k < M then (S k) else (foldseq f (% k. S(k+M)) (N-M)))) M"
proof -
have suc: "!! a b. ⟦a <= Suc b; a ≠ Suc b⟧ ⟹ a <= b" by arith
have a:"!! a b c . a = b ⟹ f c a = f c b" by blast
have "!! n. ! m s. m <= n ⟶ foldseq f s n = foldseq f (% k. (if k < m then (s k) else (foldseq f (% k. s(k+m)) (n-m)))) m"
apply (induct_tac n)
apply (simp)
apply (simp)
apply (auto)
apply (case_tac "m = Suc na")
apply (simp)
apply (rule a)
apply (rule foldseq_significant_positions)
apply (auto)
apply (drule suc, simp+)
proof -
fix na m s
assume suba:"∀m≤na. ∀s. foldseq f s na = foldseq f (λk. if k < m then s k else foldseq f (λk. s (k + m)) (na - m))m"
assume subb:"m <= na"
from suba have subc:"!! m s. m <= na ⟹foldseq f s na = foldseq f (λk. if k < m then s k else foldseq f (λk. s (k + m)) (na - m))m" by simp
have subd: "foldseq f (λk. if k < m then s (Suc k) else foldseq f (λk. s (Suc (k + m))) (na - m)) m =
foldseq f (% k. s(Suc k)) na"
by (rule subc[of m "% k. s(Suc k)", THEN sym], simp add: subb)
from subb have sube: "m ≠ 0 ⟹ ? mm. m = Suc mm & mm <= na" by arith
show "f (s 0) (foldseq f (λk. if k < m then s (Suc k) else foldseq f (λk. s (Suc (k + m))) (na - m)) m) =
foldseq f (λk. if k < m then s k else foldseq f (λk. s (k + m)) (Suc na - m)) m"
apply (simp add: subd)
apply (cases "m = 0")
apply (simp)
apply (drule sube)
apply (auto)
apply (rule a)
by (simp add: subc cong del: if_cong)
qed
then show ?thesis using assms by simp
qed
lemma foldseq_zerotail:
assumes
fz: "f 0 0 = 0"
and sz: "! i. n ≤ i ⟶ s i = 0"
and nm: "n <= m"
shows
"foldseq f s n = foldseq f s m"
proof -
show "foldseq f s n = foldseq f s m"
apply (simp add: foldseq_tail[OF nm, of f s])
apply (rule foldseq_significant_positions)
apply (auto)
apply (subst foldseq_zero)
by (simp add: fz sz)+
qed
lemma foldseq_transposed_zerotail_aux:"! i. n < i ⟶ (s::nat⇒real) i = 0⟹
foldseq_transposed op + s n = foldseq_transposed op + s (n+a)"
apply(induct a,auto)
done
lemma foldseq_transposed_zerotail:"! i. n < i ⟶ (s::nat⇒real) i = 0⟹
n <= m⟹foldseq_transposed op + s n = foldseq_transposed op + s m"
using foldseq_transposed_zerotail_aux le_Suc_ex by blast
lemma foldseq_transposed_zerotail_aux1:"! i. n < i∧i≤(n+a) ⟶ (s::nat⇒real) i = 0⟹
foldseq_transposed op + s n = foldseq_transposed op + s (n+a)"
apply(induct a,auto)
done
lemma foldseq_transposed_zerotail1:"! i. n < i∧i≤m ⟶ (s::nat⇒real) i = 0⟹
n <= m⟹foldseq_transposed op + s n = foldseq_transposed op + s m"
by (metis foldseq_transposed_zerotail_aux1 le_add_diff_inverse)
lemma foldseq_zerotail2:
assumes "! x. f x 0 = x"
and "! i. n < i ⟶ s i = 0"
and nm: "n <= m"
shows "foldseq f s n = foldseq f s m"
proof -
have "f 0 0 = 0" by (simp add: assms)
have b:"!! m n. n <= m ⟹ m ≠ n ⟹ ? k. m-n = Suc k" by arith
have c: "0 <= m" by simp
have d: "!! k. k ≠ 0 ⟹ ? l. k = Suc l" by arith
show ?thesis
apply (subst foldseq_tail[OF nm])
apply (rule foldseq_significant_positions)
apply (auto)
apply (case_tac "m=n")
apply (simp+)
apply (drule b[OF nm])
apply (auto)
apply (case_tac "k=0")
apply (simp add: assms)
apply (drule d)
apply (auto)
apply (simp add: assms foldseq_zero)
done
qed
lemma foldseq_zerostart:
"! x. f 0 (f 0 x) = f 0 x ⟹ ! i. i <= n ⟶ s i = 0 ⟹ foldseq f s (Suc n) = f 0 (s (Suc n))"
proof -
assume f00x: "! x. f 0 (f 0 x) = f 0 x"
have "! s. (! i. i<=n ⟶ s i = 0) ⟶ foldseq f s (Suc n) = f 0 (s (Suc n))"
apply (induct n)
apply (simp)
apply (rule allI, rule impI)
proof -
fix n
fix s
have a:"foldseq f s (Suc (Suc n)) = f (s 0) (foldseq f (% k. s(Suc k)) (Suc n))" by simp
assume b: "! s. ((∀i≤n. s i = 0) ⟶ foldseq f s (Suc n) = f 0 (s (Suc n)))"
from b have c:"!! s. (∀i≤n. s i = 0) ⟹ foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
assume d: "! i. i <= Suc n ⟶ s i = 0"
show "foldseq f s (Suc (Suc n)) = f 0 (s (Suc (Suc n)))"
apply (subst a)
apply (subst c)
by (simp add: d f00x)+
qed
then show "! i. i <= n ⟶ s i = 0 ⟹ foldseq f s (Suc n) = f 0 (s (Suc n))" by simp
qed
lemma foldseq_zerostart2:
"! x. f 0 x = x ⟹ ! i. i < n ⟶ s i = 0 ⟹ foldseq f s n = s n"
proof -
assume a:"! i. i<n ⟶ s i = 0"
assume x:"! x. f 0 x = x"
from x have f00x: "! x. f 0 (f 0 x) = f 0 x" by blast
have b: "!! i l. i < Suc l = (i <= l)" by arith
have d: "!! k. k ≠ 0 ⟹ ? l. k = Suc l" by arith
show "foldseq f s n = s n"
apply (case_tac "n=0")
apply (simp)
apply (insert a)
apply (drule d)
apply (auto)
apply (simp add: b)
apply (insert f00x)
apply (drule foldseq_zerostart)
by (simp add: x)+
qed
lemma foldseq_almostzero:
assumes f0x:"! x. f 0 x = x" and fx0: "! x. f x 0 = x" and s0:"! i. i ≠ j ⟶ s i = 0"
shows "foldseq f s n = (if (j <= n) then (s j) else 0)"
proof -
from s0 have a: "! i. i < j ⟶ s i = 0" by simp
from s0 have b: "! i. j < i ⟶ s i = 0" by simp
show ?thesis
apply auto
apply (subst foldseq_zerotail2[of f, OF fx0, of j, OF b, of n, THEN sym])
apply simp
apply (subst foldseq_zerostart2)
apply (simp add: f0x a)+
apply (subst foldseq_zero)
by (simp add: s0 f0x)+
qed
lemma foldseq_distr_unary:
assumes "!! a b. g (f a b) = f (g a) (g b)"
shows "g(foldseq f s n) = foldseq f (% x. g(s x)) n"
proof -
have "! s. g(foldseq f s n) = foldseq f (% x. g(s x)) n"
apply (induct_tac n)
apply (simp)
apply (simp)
apply (auto)
apply (drule_tac x="% k. s (Suc k)" in spec)
by (simp add: assms)
then show ?thesis by simp
qed
definition mult_matrix_n :: "nat ⇒ (real⇒real⇒real) ⇒ (real⇒real⇒real) ⇒matrix ⇒matrix ⇒matrix" where
"mult_matrix_n n fmul fadd A B == Abs_matrix(% j i. foldseq fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) n)"
definition mult_matrix_n1 :: "nat ⇒ (real⇒real⇒real) ⇒ (real⇒real⇒real) ⇒matrix ⇒matrix ⇒matrix" where
"mult_matrix_n1 n fmul fadd A B == Abs_matrix(% j i. foldseq_transposed fadd (% k. fmul (Rep_matrix A j k) (Rep_matrix B k i)) n)"
definition mult_matrix1 :: "(real⇒real⇒real) ⇒ (real⇒real⇒real) ⇒ matrix ⇒matrix ⇒matrix" where
"mult_matrix1 fmul fadd A B == mult_matrix_n1 (max (ncols A) (nrows B)) fmul fadd A B"
definition mult_matrix :: "(real⇒real⇒real) ⇒ (real⇒real⇒real) ⇒ matrix ⇒matrix ⇒matrix" where
"mult_matrix fmul fadd A B == mult_matrix_n (max (ncols A) (nrows B)) fmul fadd A B"
definition tr_n:: "nat ⇒ (real⇒real⇒real) ⇒ matrix⇒real" where
"tr_n n fadd A==foldseq fadd (%k. Rep_matrix A k k) n"
definition tr::"(real⇒real⇒real) ⇒ matrix⇒real"where
"tr fadd A==foldseq fadd (%k. Rep_matrix A k k) (max (nrows A) (ncols A)) "
lemma mult_matrix_n:
assumes "ncols A ≤ n" (is ?An) "nrows B ≤ n" (is ?Bn) "fadd 0 0 = 0" "fmul 0 0 = 0"
shows c:"mult_matrix fmul fadd A B = mult_matrix_n n fmul fadd A B"
proof -
show ?thesis using assms
apply (simp add: mult_matrix_def mult_matrix_n_def)
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
apply (rule foldseq_zerotail, simp_all add: nrows_le ncols_le assms)
done
qed
lemma mult_matrix_nm:
assumes "ncols A <= n" "nrows B <= n" "ncols A <= m" "nrows B <= m" "fadd 0 0 = 0" "fmul 0 0 = 0"
shows "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B"
proof -
from assms have "mult_matrix_n n fmul fadd A B = mult_matrix fmul fadd A B"
by (simp add: mult_matrix_n)
also from assms have "… = mult_matrix_n m fmul fadd A B"
by (simp add: mult_matrix_n[THEN sym])
finally show "mult_matrix_n n fmul fadd A B = mult_matrix_n m fmul fadd A B" by simp
qed
definition r_distributive :: "('a ⇒ 'b ⇒ 'b) ⇒ ('b ⇒ 'b ⇒ 'b) ⇒ bool" where
"r_distributive fmul fadd == ! a u v. fmul a (fadd u v) = fadd (fmul a u) (fmul a v)"
definition l_distributive :: "('a ⇒ 'b ⇒ 'a) ⇒ ('a ⇒ 'a ⇒ 'a) ⇒ bool" where
"l_distributive fmul fadd == ! a u v. fmul (fadd u v) a = fadd (fmul u a) (fmul v a)"
definition distributive :: "('a ⇒ 'a ⇒ 'a) ⇒ ('a ⇒ 'a ⇒ 'a) ⇒ bool" where
"distributive fmul fadd == l_distributive fmul fadd & r_distributive fmul fadd"
lemma max1: "!! a x y. (a::nat) <= x ⟹ a <= max x y" by (arith)
lemma max2: "!! b x y. (b::nat) <= y ⟹ b <= max x y" by (arith)
lemma r_distributive_matrix:
assumes
"r_distributive fmul fadd"
"associative fadd"
"commutative fadd"
"fadd 0 0 = 0"
"! a. fmul a 0 = 0"
"! a. fmul 0 a = 0"
shows "r_distributive (mult_matrix fmul fadd) (combine_matrix fadd)"
proof -
from assms show ?thesis
apply (simp add: r_distributive_def mult_matrix_def, auto)
proof -
fix a::"matrix"
fix u::" matrix"
fix v::" matrix"
let ?mx = "max (ncols a) (max (nrows u) (nrows v))"
from assms show "mult_matrix_n (max (ncols a) (nrows (combine_matrix fadd u v))) fmul fadd a (combine_matrix fadd u v) =
combine_matrix fadd (mult_matrix_n (max (ncols a) (nrows u)) fmul fadd a u) (mult_matrix_n (max (ncols a) (nrows v)) fmul fadd a v)"
apply (subst mult_matrix_nm[of a _ _ ?mx fadd fmul])
apply (simp add: max1 max2 combine_nrows combine_ncols)+
apply (subst mult_matrix_nm[of a _ v ?mx fadd fmul])
apply (simp add: max1 max2 combine_nrows combine_ncols)+
apply (subst mult_matrix_nm[of a _ u ?mx fadd fmul])
apply (simp add: max1 max2 combine_nrows combine_ncols)+
apply (simp add: mult_matrix_n_def r_distributive_def foldseq_distr[of fadd])
apply (simp add: combine_matrix_def combine_infmatrix_def)
apply (rule comb[of "Abs_matrix" "Abs_matrix"], simp, (rule ext)+)
apply (simplesubst RepAbs_matrix)
apply (simp, auto)
apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)
apply (meson dual_order.trans nrows nrows_max)
apply (rule exI[of _ "ncols v"], simp add: ncols_le foldseq_zero)
apply (meson dual_order.trans ncols ncols_max)
apply (subst RepAbs_matrix)
apply (simp, auto)
apply (rule exI[of _ "nrows a"], simp add: nrows_le foldseq_zero)