-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMisc.thy
More file actions
1967 lines (1695 loc) · 68.2 KB
/
Misc.thy
File metadata and controls
1967 lines (1695 loc) · 68.2 KB
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
(* Author: Lukas Koller *)
theory Misc
imports Main "HOL-Library.Multiset" tsp.Berge
begin
text \<open>This theory contains miscellaneous lemmas and theorems.\<close>
section \<open>List Induction Schemas\<close>
(* function just for the induction schema *)
fun list012 where
"list012 [] = undefined"
| "list012 [v] = undefined"
| "list012 (u#v#P) = list012 (v#P)"
lemmas list012_induct = list012.induct [case_names Nil Singleton CCons]
(* function just for the induction schema *)
fun list0123 where
"list0123 [] = undefined"
| "list0123 [v] = undefined"
| "list0123 [u,v] = undefined"
| "list0123 (u#v#w#P) = list0123 (v#w#P)"
lemmas list0123_induct = list0123.induct [case_names Nil Singleton Doubleton CCCons]
(* function just for the induction schema *)
fun list01234 where
"list01234 [] = undefined"
| "list01234 [v] = undefined"
| "list01234 [u,v] = undefined"
| "list01234 [u,v,w] = undefined"
| "list01234 (u#v#w#x#P) = list01234 (v#w#x#P)"
(* function just for the induction schema *)
fun pair_list where
"pair_list [] = undefined"
| "pair_list ((x,y)#xs) = pair_list xs"
section \<open>List Lemmas\<close>
lemma hd_singleton: "xs \<noteq> [] \<Longrightarrow> tl xs = [] \<Longrightarrow> xs = [hd xs]"
by (induction xs) auto
lemma distinct_hd_last_neq: "distinct xs \<Longrightarrow> length xs > 1 \<Longrightarrow> hd xs \<noteq> last xs"
by (induction xs) auto
lemma rev_hd_last_eq: "xs \<noteq> [] \<Longrightarrow> xs = rev xs \<Longrightarrow> hd xs = last xs"
proof (induction xs rule: list012.induct)
case (3 x x' xs)
thus ?case
by (metis last_rev)
qed auto
lemma split_hd:
assumes "xs \<noteq> []"
obtains xs' where "xs = hd xs#xs'"
using assms list.exhaust_sel by blast
lemma split_last:
assumes "xs \<noteq> []"
obtains xs' where "xs = xs' @ [last xs]"
using assms append_butlast_last_id by metis
lemma last_in_set_tl: "2 \<le> length xs \<Longrightarrow> last xs \<in> set (tl xs)"
by (induction xs) auto
lemma list_hd_singleton: "length xs = 1 \<Longrightarrow> hd xs = x \<Longrightarrow> xs = [x]"
by (induction xs) auto
lemma set_tl_subset: "set (tl A) \<subseteq> set A"
by (induction A) auto
lemma drop_tl: "i > 0 \<Longrightarrow> drop i xs = drop (i - 1) (tl xs)"
using drop_Suc[of "i-1"] Suc_diff_1[of i] by auto
lemma remdups_append: "x \<in> set ys \<Longrightarrow> remdups (xs @ x # ys) = remdups (xs @ ys)"
by (induction xs) auto
lemma set_union_cons: "set (x#xs) = {x} \<union> set xs"
by auto
lemma set_union_cons_cons: "set (x#y#xs) = {x} \<union> {y} \<union> set xs"
by auto
lemma hd_last_eq_split:
assumes "xs \<noteq> []" "hd xs = last xs"
obtains y where "xs = [y]" | y ys where "xs = y#ys @ [y]"
using assms
proof (induction xs rule: list012.induct)
case (3 u v P)
hence "u#v#P = u#butlast (v#P) @ [u]"
by (auto simp: append_butlast_last_id)
thus ?case
using 3 by auto
qed auto
lemma hd_last_eq_distinct_set_iff:
assumes "xs \<noteq> [] \<Longrightarrow> hd xs = last xs"
shows "distinct (tl xs) \<longleftrightarrow> distinct (butlast xs)"
and "set (tl xs) = V \<longleftrightarrow> set (butlast xs) = V"
proof -
consider "xs = []" | y where "xs = [y]" | y ys where "xs = y#ys @ [y]"
using assms by (auto elim: hd_last_eq_split)
moreover thus "distinct (tl xs) \<longleftrightarrow> distinct (butlast xs)"
by cases auto
ultimately show "set (tl xs) = V \<longleftrightarrow> set (butlast xs) = V"
by cases auto
qed
lemma list_eq_even_len_gr1:
assumes "X \<noteq> {}" "even (card X)" "set xs = X"
shows "length xs > 1"
using assms by (induction xs rule: list012_induct) auto
lemma set_tl_eq_set:
assumes "length xs > 1" "distinct (tl xs)" "hd xs = last xs"
shows "set (tl xs) = set xs"
using assms by (induction xs rule: list012_induct) auto
lemma list_len_geq2_elim:
assumes "length xs \<ge> 2"
obtains x y ys where "xs = x#y#ys"
using assms by (induction xs rule: list012_induct) auto
lemma list_len_geq2_split_hd_last:
assumes "length xs \<ge> 2"
obtains x y ys where "xs = x#ys @ [y]"
using assms
proof (rule list_len_geq2_elim)
fix x y ys
assume "xs = x#y#ys"
moreover obtain ys' where "y#ys = ys' @ [last (y#ys)]"
using split_last by blast
ultimately show ?thesis
using that by auto
qed
lemma list_split_for_2elems:
assumes "a \<in> set xs" "b \<in> set xs" "a \<noteq> b"
obtains xs\<^sub>1 xs\<^sub>2 where "xs = xs\<^sub>1 @ a#xs\<^sub>2" "b \<in> set xs\<^sub>2" | xs\<^sub>1 xs\<^sub>2 where "xs = xs\<^sub>1 @ b#xs\<^sub>2" "a \<in> set xs\<^sub>2"
using assms
proof (induction xs arbitrary: a b thesis)
case Nil
then show ?case by auto
next
case (Cons x xs)
then consider "x = a" "b \<in> set xs" | "x = b" "a \<in> set xs" | "a \<in> set xs" "b \<in> set xs"
by auto
thus ?case
proof cases
assume "x = a" "b \<in> set xs"
thus ?case
using Cons by fastforce
next
assume "x = b" "a \<in> set xs"
thus ?case
using Cons by fastforce
next
assume "a \<in> set xs" "b \<in> set xs"
then consider xs\<^sub>1 xs\<^sub>2 where "xs = xs\<^sub>1 @ a#xs\<^sub>2" "b \<in> set xs\<^sub>2"
| xs\<^sub>1 xs\<^sub>2 where "xs = xs\<^sub>1 @ b#xs\<^sub>2" "a \<in> set xs\<^sub>2"
using Cons by blast
thus ?case
proof cases
fix xs\<^sub>1 xs\<^sub>2
assume "xs = xs\<^sub>1 @ a#xs\<^sub>2" "b \<in> set xs\<^sub>2"
moreover hence "x#xs = (x#xs\<^sub>1) @ a#xs\<^sub>2"
by auto
ultimately show ?case
using Cons by blast
next
fix xs\<^sub>1 xs\<^sub>2
assume "xs = xs\<^sub>1 @ b#xs\<^sub>2" "a \<in> set xs\<^sub>2"
moreover hence "x#xs = (x#xs\<^sub>1) @ b#xs\<^sub>2"
by auto
ultimately show ?case
using Cons by blast
qed
qed
qed
lemma append_tl_butlast_eq:
assumes "xs \<noteq> []" "ys \<noteq> []" "last xs = x" "hd ys = x"
shows "xs @ tl ys = butlast xs @ ys"
using assms by (induction xs rule: list012.induct) auto
lemma map_map: "map (f o g) xs = map f (map g xs)"
by (induction xs) auto
lemma fold_neq_find:
assumes "fold f xs a \<noteq> a"
obtains x where "x \<in> List.set xs" "f x a \<noteq> a"
using assms
proof (induction xs arbitrary: a)
case (Cons x xs)
then show ?case
by (cases "f x a = a") auto
qed auto
lemma fold_enat_min_leq_acc: "fold (\<lambda>x a. min (g x) a) xs (a::enat) \<le> a" (* TODO: less restrictive type *)
proof (induction xs arbitrary: a)
case (Cons x xs)
have "fold (\<lambda>x a. min (g x) a) (x#xs) a = fold (\<lambda>x a. min (g x) a) xs (min (g x) a)"
by auto
also have "... \<le> min (g x) a"
using Cons by fastforce
thus ?case
by auto
qed auto
lemma fold_enat_min_leq_member:
assumes "x \<in> set xs"
shows "fold (\<lambda>x a. min (g x) a) xs (a::enat) \<le> min (g x) a"
using assms
proof (induction xs arbitrary: a rule: list012.induct)
case 1
then show ?case by auto
next
case (2 y)
then show ?case by auto
next
case (3 y z xs)
show ?case
proof cases
assume "x = y"
hence "fold (\<lambda>x a. min (g x) a) (y#z#xs) a = fold (\<lambda>x a. min (g x) a) (z#xs) (min (g x) a)"
by auto
also have "... \<le> min (g x) a"
by (intro fold_enat_min_leq_acc)
finally show ?thesis .
next
assume "x \<noteq> y"
have "fold (\<lambda>x a. min (g x) a) (y#z#xs) a = fold (\<lambda>x a. min (g x) a) (z#xs) (min (g y) a)"
by auto
also have "... \<le> min (g x) (min (g y) a)"
using \<open>x \<noteq> y\<close> 3 by (intro "3.IH") auto
also have "... \<le> min (g x) a"
using min.cobounded2 min.mono by blast
finally show ?thesis .
qed
qed
(* lemma fold_enat_min:
assumes "(a::enat) < \<infinity>"
shows "fold (\<lambda>x a. min (g x) a) xs a < \<infinity>" (is "fold ?f xs a < \<infinity>")
using assms
proof (induction xs arbitrary: a)
case (Cons x xs)
thus ?case
by (cases "g x") auto
qed auto *)
lemma fold_concat_map: "fold (\<lambda>x a. a @ f x) xs a = a @ concat (map f xs)"
by (induction xs arbitrary: a) auto
lemma concat_map_disjoint:
assumes "x \<notin> set xs" "\<And>y. y \<in> set xs \<Longrightarrow> x \<noteq> y \<Longrightarrow> set (f x) \<inter> set (f y) = {}"
shows "set (f x) \<inter> set (concat (map f xs)) = {}"
using assms by (induction xs) auto
lemma distinct_concat_map:
assumes "distinct xs" "\<And>x. x \<in> set xs \<Longrightarrow> distinct (f x)"
"\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> x \<noteq> y \<Longrightarrow> set (f x) \<inter> set (f y) = {}"
shows "distinct (concat (map f xs))"
using assms by (induction xs) (auto intro!: concat_map_disjoint)
lemma hd_concat_map_elim:
assumes "\<exists>x \<in> set xs. f x \<noteq> []"
obtains y where "y \<in> set xs" "f y \<noteq> []" "hd (concat (map f xs)) = hd (f y)"
using assms
proof (induction xs arbitrary: thesis)
case (Cons x xs)
consider "f x = []" | "f x \<noteq> []"
by auto
then show ?case
using Cons by cases auto
qed auto
lemma last_concat_map_elim:
assumes "\<exists>x \<in> set xs. f x \<noteq> []"
obtains y where "y \<in> set xs" "f y \<noteq> []" "last (concat (map f xs)) = last (f y)"
proof (rule hd_concat_map_elim)
show "\<exists>x \<in> set (rev xs). (rev o f) x \<noteq> []"
using assms by auto
fix y
assume "y \<in> set (rev xs)" "(rev o f) y \<noteq> []"
"hd (concat (map (rev \<circ> f) (rev xs))) = hd ((rev \<circ> f) y)"
moreover hence "last (concat (map f xs)) = last (f y)"
by (simp add: hd_rev[symmetric] rev_concat rev_map)
ultimately show ?thesis
using that by auto
qed
lemma last_concat_map:
assumes "xs \<noteq> []" "\<And>x. x \<in> set xs \<Longrightarrow> f x \<noteq> []"
shows "last (concat (map f xs)) = last (f (last xs))"
using assms by (induction xs) auto
lemma concat_filter_empty: "concat (filter (\<lambda>x. x \<noteq> []) xs) = concat xs"
by (induction xs) auto
lemma concat_map_filter_empty:
assumes "\<And>x. \<not> P x \<Longrightarrow> f x = []"
shows "concat (map f (filter P xs)) = concat (map f xs) "
using assms by (induction xs) auto
lemma last_filter_non_nil:
assumes "xs \<noteq> []" "last xs = x" "f x"
shows "filter f xs \<noteq> [] \<and> last (filter f xs) = x"
using assms by (induction xs rule: list012_induct) auto
lemma last_filter: "xs \<noteq> [] \<Longrightarrow> last xs = x \<Longrightarrow> f x \<Longrightarrow> last (filter f xs) = x"
using last_filter_non_nil by fastforce
subsection \<open>Repeated Elements in Lists\<close>
lemma distinct_distinct_adj: "distinct xs \<Longrightarrow> distinct_adj xs"
by (simp add: distinct_adj_altdef distinct_tl remdups_adj_distinct)
(* lemma card_leq_len_remdups_adj: "card (set xs) \<le> length (remdups_adj xs)"
proof (induction xs rule: remdups_adj.induct)
case (3 x y xs)
consider "x = y" | "x \<noteq> y"
by auto
thus ?case
proof cases
assume "x = y"
thus ?thesis
using 3 by auto
next
assume "x \<noteq> y"
hence "set (x#y#xs) = {x} \<union> set (y#xs)"
by auto
hence "card (set (x#y#xs)) = card ({x} \<union> set (y#xs))"
by auto
also have "... \<le> card {x} + card (set (y#xs))"
by (intro card_Un_le)
also have "... \<le> length (remdups_adj ((x#y#xs)))"
using 3 \<open>x \<noteq> y\<close> by auto
finally show ?thesis
by auto
qed
qed auto
lemma len_remdups_append: "length (remdups_adj xs) \<le> length (remdups_adj (xs @ [x]))"
by (induction xs rule: remdups_adj.induct) auto
lemma cycle_card_leq_len_remdups_adj: "card (set xs) \<le> length (remdups_adj (xs @ [hd xs]))"
proof -
have "card (set xs) \<le> length (remdups_adj xs)"
using card_leq_len_remdups_adj by auto
also have "... \<le> length (remdups_adj (xs @ [hd xs]))"
by (intro len_remdups_append)
finally show ?thesis
by auto
qed *)
subsection \<open>Even-Indexed Elements in Lists\<close>
fun even_elems :: "'a list \<Rightarrow> 'a list" where
"even_elems [] = []"
| "even_elems [x] = [x]"
| "even_elems (x#y#xs) = x#even_elems xs"
value "even_elems [0::nat,1,2,3,4,5,6,7,8,9,10]"
lemma even_elems_tl: "even_elems (x#xs) = x#even_elems (tl xs)"
by (induction xs rule: even_elems.induct) auto
lemma even_elems_subset: "set (even_elems xs) \<subseteq> set xs"
by (induction xs rule: even_elems.induct) auto
lemma even_elems_distinct: "distinct xs \<Longrightarrow> distinct (even_elems xs)"
proof (induction xs rule: even_elems.induct)
case (3 x y xs)
hence "distinct (even_elems xs)"
by auto
moreover hence "x \<notin> set (even_elems xs)"
using 3 even_elems_subset by fastforce
ultimately show ?case
by auto
qed auto
lemma even_elems_mset_union: "mset (even_elems (tl xs)) + mset (even_elems xs) = mset xs"
by (induction xs rule: even_elems.induct) (auto simp: even_elems_tl)
lemma even_elems_butlast: "even (length xs) \<longleftrightarrow> even_elems xs = even_elems (butlast xs)"
by (induction xs rule: even_elems.induct) auto
lemma even_elem_append:
assumes "even (length xs)" "even (length ys)"
shows "even_elems (xs @ ys) = even_elems xs @ even_elems ys"
using assms by (induction xs arbitrary: ys rule: even_elems.induct) auto
lemma even_induct_non_nil[consumes 1,case_names Singleton Doubleton CCons]:
assumes "xs \<noteq> []"
and "\<And>x. P [x]"
and "\<And>x y. P [x,y]"
and "\<And>x y z xs. P (z#xs) \<Longrightarrow> P (x#y#z#xs)"
shows "P xs"
using assms
proof (induction xs rule: even_elems.induct)
case (3 x y xs)
then show ?case
by (cases xs) auto
qed auto
section \<open>(Finite) Set Lemmas\<close>
lemma mem_not_empty: "x \<in> A \<Longrightarrow> A \<noteq> {}"
by auto
lemma insert_union_subset: "A\<^sub>1 \<union> A\<^sub>2 \<subseteq> B \<Longrightarrow> a \<in> B \<Longrightarrow> insert a A\<^sub>1 \<union> A\<^sub>2 \<subseteq> B"
by auto
lemma inter_emptyI:
assumes "\<And>x. x \<in> B \<Longrightarrow> x \<notin> A"
shows "A \<inter> B = {}"
using assms by blast
lemma card_neq_1_obtain_mem:
assumes "card A \<noteq> 1" "a \<in> A"
obtains b where "b \<in> A" "a \<noteq> b"
proof -
consider "infinite A" | "card A > 1"
using assms by (metis One_nat_def Suc_lessI card_gt_0_iff empty_iff)
thus ?thesis
proof cases
assume "infinite A"
thus ?thesis
using that by (metis assms(1) finite.emptyI is_singletonI' is_singleton_altdef)
next
assume "card A > 1"
thus ?thesis
using assms
by (metis One_nat_def card.infinite card_le_Suc0_iff_eq leD not_one_less_zero that)
qed
qed (* TODO: clean up proof! *)
lemma set012_split:
assumes "finite F"
obtains "F = {}"
| x where "F = {x}"
| x y F' where "F = {x,y} \<union> F'" "x \<notin> F'" "y \<notin> F'" "x \<noteq> y"
using assms
proof (induction F rule: finite_induct)
case insertI1: (insert x F)
thus ?case
proof (induction F rule: finite_induct)
case insertI2: (insert y F)
thus ?case
using insertI1 by auto
qed auto
qed auto
lemma set12_split:
assumes "finite F" "x \<in> F"
obtains x where "F = {x}"
| x y F' where "F = {x,y} \<union> F'" "x \<notin> F'" "y \<notin> F'" "x \<noteq> y"
using assms by (elim set012_split) auto
text \<open>Induction schema that adds two new elements to a finite set.\<close>
lemma finite2_induct [consumes 1, case_names empty singleton insert2]:
assumes "finite F"
assumes empty: "P {}"
and singleton: "\<And>x. P {x}"
and insert2: "\<And>x y F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> y \<notin> F \<Longrightarrow> x \<noteq> y \<Longrightarrow> P F \<Longrightarrow> P ({x,y} \<union> F)"
shows "P F"
using assms
proof (induction F rule: finite_psubset_induct)
case (psubset F)
then consider "F = {}" | x where "F = {x}"
| x y F' where "F = {x,y} \<union> F'" "x \<notin> F'" "y \<notin> F'" "x \<noteq> y"
by (elim set012_split)
thus ?case
using psubset
proof cases
fix x y F'
assume "F = {x,y} \<union> F'" "x \<notin> F'" "y \<notin> F'" "x \<noteq> y"
then show ?case
using psubset by fastforce
qed auto
qed
lemma finite_even_induct [consumes 2, case_names empty insert2]:
assumes "finite F" "even (card F)"
assumes empty: "P {}"
and insert2: "\<And>x y F. finite F \<Longrightarrow> x \<notin> F \<Longrightarrow> y \<notin> F \<Longrightarrow> x \<noteq> y \<Longrightarrow> P F \<Longrightarrow> P ({x,y} \<union> F)"
shows "P F"
using assms by (induction F rule: finite2_induct) auto
lemma finite_even_card:
assumes "finite X" "x \<notin> X" "y \<notin> X" "x \<noteq> y"
shows "even (card ({x,y} \<union> X)) \<longleftrightarrow> even (card X)"
using assms by auto
lemma finite_even_cardI:
assumes "finite X" "x \<notin> X" "y \<notin> X" "x \<noteq> y" "even (card ({x,y} \<union> X))"
shows "even (card X)"
using assms by auto
lemma finite_even_cardI2:
assumes "finite X" "x \<notin> X" "y \<notin> X" "x \<noteq> y" "even (card X)"
shows "even (card ({x,y} \<union> X))"
using assms by auto
lemma finite_card_geq2: "finite A \<Longrightarrow> (\<exists>a b. a \<in> A \<and> b \<in> A \<and> a \<noteq> b) \<longleftrightarrow> card A \<ge> 2"
by (induction A rule: finite2_induct) auto
lemma card'_leq: "card' A \<le> enat k \<Longrightarrow> card A \<le> k"
by (metis card'_finite_enat enat_ile enat_ord_simps(1))
lemma finite_lists_len_eq:
assumes "finite X"
shows "finite ({xs | xs. List.set xs \<subseteq> X \<and> length xs = k})"
using assms
proof (induction k)
case 0
then show ?case by auto
next
case (Suc k)
hence "finite (\<Union> {(\<lambda>P. x#P) ` {xs | xs. List.set xs \<subseteq> X \<and> length xs = k} | x. x \<in> X})"
using Suc by auto
moreover have "{xs | xs. List.set xs \<subseteq> X \<and> length xs = k + 1}
\<subseteq> \<Union> {(\<lambda>P. x#P) ` {xs | xs. List.set xs \<subseteq> X \<and> length xs = k} | x. x \<in> X}"
(is "?Xs' \<subseteq> \<Union> {(\<lambda>P. x#P) ` ?Xs | x. x \<in> X}")
proof
fix xs
assume "xs \<in> ?Xs'"
hence "List.set xs \<subseteq> X" "length xs = k + 1" and xs_non_nil: "xs \<noteq> []"
by auto
hence "hd xs \<in> X" and "List.set (tl xs) \<subseteq> X" "length (tl xs) = k"
using set_tl_subset[of xs] by auto
moreover hence "xs \<in> (\<lambda>P. hd xs#P) ` ?Xs"
using xs_non_nil by force
ultimately show "xs \<in> \<Union> {(\<lambda>P. x#P) ` ?Xs | x. x \<in> X}"
by blast
qed
ultimately show ?case
using finite_subset by auto
qed
lemma finite_lists_len_leq:
assumes "finite X"
shows "finite ({xs | xs. List.set xs \<subseteq> X \<and> length xs \<le> k})"
using assms
proof (induction k)
case 0
then show ?case by auto
next
case (Suc k)
moreover have "{xs | xs. List.set xs \<subseteq> X \<and> length xs \<le> k + 1}
= {xs | xs. List.set xs \<subseteq> X \<and> length xs \<le> k} \<union> {xs | xs. List.set xs \<subseteq> X \<and> length xs = k + 1}"
by auto
ultimately show ?case
using finite_lists_len_eq finite_Un by auto
qed
section \<open>Tuple Set Lemmas\<close>
lemma irrefl_subset: "irrefl B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> irrefl A"
unfolding irrefl_def by auto
section \<open>Metric Lemmas\<close>
lemma mult_2: "(x::'b::{ordered_semiring_0,semiring_numeral}) + x = 2 * x"
by (simp add: semiring_numeral_class.mult_2)
lemma mult_3: "2 * (x::'b::{ordered_semiring_0,semiring_numeral}) + x = 3 * x"
by (metis distrib_right mult_numeral_1 numeral_Bit1 numeral_One one_add_one) (* TODO: clean up! *)
lemma mult_2_mono: "(x::'b::{ordered_semiring_0,semiring_numeral}) \<le> y \<Longrightarrow> 2 * x \<le> 2 * y"
by (simp add: add_mono semiring_numeral_class.mult_2)
section \<open>Even Predicate\<close>
text \<open>Even predicate for \<open>enat\<close>\<close>
fun even' :: "enat \<Rightarrow> bool" where
"even' \<infinity> = False"
| "even' (enat i) = even i"
lemma even_enat_mult2:
assumes "i \<noteq> \<infinity>"
shows "even' (2 * i)"
proof (cases "2 * i")
case (enat j)
thus ?thesis
using assms by (auto simp: numeral_eq_enat)
next
case infinity
then show ?thesis
using assms imult_is_infinity by auto
qed
lemma even'_enat:
assumes "even' a"
obtains a' where "enat a' = a"
using assms by (cases a) auto
lemma enat_addE:
assumes "enat c = a + b"
obtains x y where "enat x = a" "enat y = b"
by (metis assms not_enat_eq plus_enat_simps)
lemma even'_addI1:
assumes "even' a" "even' b"
shows "even' (a + b)"
proof -
obtain x where "enat x = a"
using assms by (auto elim: even'_enat)
moreover hence "even x"
using assms by auto
moreover obtain y where "enat y = b"
using assms by (auto elim: even'_enat)
moreover hence "even y"
using assms by auto
ultimately show ?thesis
by auto
qed
lemma even'_addI2:
assumes "\<not> even' a" "a \<noteq> \<infinity>" "\<not> even' b" "b \<noteq> \<infinity>"
shows "even' (a + b)"
proof -
obtain x where "enat x = a"
using assms by (auto elim: even'_enat)
moreover hence "odd x"
using assms by auto
moreover obtain y where "enat y = b"
using assms by (auto elim: even'_enat)
moreover hence "odd y"
using assms by auto
ultimately show ?thesis
by auto
qed
lemma even'_addE1:
assumes "even' (a + b)" "even' b"
shows "even' a"
proof -
obtain c where "enat c = a + b"
using assms by (auto elim: even'_enat)
moreover hence "even c"
using assms by (cases "a+b") auto
moreover obtain x y where "enat x = a" "enat y = b"
using assms calculation by (auto elim: enat_addE)
ultimately show ?thesis
using assms by auto
qed
lemma even'_addE2:
assumes "even' (a + b)" "even' a"
shows "even' b"
proof -
obtain c where "enat c = a + b"
using assms by (auto elim: even'_enat)
moreover hence "even c"
using assms by (cases "a+b") auto
moreover obtain x y where "enat x = a" "enat y = b"
using assms calculation by (auto elim: enat_addE)
ultimately show ?thesis
using assms by auto
qed
lemma even'_addE3:
assumes "even' (a + b)" "\<not> even' b"
shows "\<not> even' a"
proof -
obtain c where "enat c = a + b"
using assms by (auto elim: even'_enat)
moreover hence "even c"
using assms by (cases "a+b") auto
moreover obtain x y where "enat x = a" "enat y = b"
using assms calculation by (auto elim: enat_addE)
ultimately show ?thesis
using assms by auto
qed
lemma even'_addE4:
assumes "even' (a + b)" "\<not> even' a"
shows "\<not> even' b"
proof -
obtain c where "enat c = a + b"
using assms by (auto elim: even'_enat)
moreover hence "even c"
using assms by (cases "a+b") auto
moreover obtain x y where "enat x = a" "enat y = b"
using assms calculation by (auto elim: enat_addE)
ultimately show ?thesis
using assms by auto
qed
lemma not_even_add1:
assumes "\<not> even' a" "a \<noteq> \<infinity>"
shows "even' (a + 1)"
using assms by (cases a) auto
section \<open>Sum Lemmas\<close>
lemma finite_sum_neq_inf:
assumes "finite X" "\<And>x. x \<in> X \<Longrightarrow> f x \<noteq> (\<infinity>::enat)"
shows "sum f X \<noteq> (\<infinity>::enat)"
using assms by (induction X rule: finite_induct) (auto simp: plus_eq_infty_iff_enat)
lemma even_sum_of_odd_vals_iff:
assumes "finite X" "\<And>x. x \<in> X \<Longrightarrow> \<not> even' (f x)" "\<And>x. x \<in> X \<Longrightarrow> f x \<noteq> (\<infinity>::enat)"
shows "even' (sum f X) \<longleftrightarrow> even' (card X)"
using assms
proof (induction X rule: finite_induct)
case (insert x X)
show ?case
proof
assume "even' (sum f (insert x X))"
thus "even' (enat (card (insert x X)))"
using insert by (auto simp: even'_addE4)
next
assume "even' (enat (card (insert x X)))"
hence "\<not> even' (sum f X)"
using insert by (auto simp: even'_addE4)
thus "even' (sum f (insert x X))"
using insert finite_sum_neq_inf[of X f] by (auto simp: even'_addI2)
qed
qed auto
lemma finite_even_sum:
assumes "finite X" "\<And>x. x \<in> X \<Longrightarrow> even' (f x)"
shows "even' (sum f X)"
using assms by (induction X rule: finite_induct) (auto intro: even'_addI1)
lemma sum_one_val:
assumes "finite X" "a \<in> X" "\<And>x. x \<in> X \<Longrightarrow> x \<noteq> a \<Longrightarrow> f x = 0" "f a = 1"
shows "sum f X = 1"
using assms
proof (induction X rule: finite_induct)
case (insert x X)
show ?case
using insert
proof (cases "x = a")
assume "x = a"
moreover hence "\<And>x. x \<in> X \<Longrightarrow> f x = 0"
using insert by fastforce
ultimately show ?thesis
using insert by auto
qed auto
qed auto
lemma sum_two_val:
assumes "finite X" "a \<in> X" "b \<in> X" "a \<noteq> b" "f a = 1" "f b = 1"
and "\<And>x. x \<in> X \<Longrightarrow> x \<noteq> a \<Longrightarrow> x \<noteq> b \<Longrightarrow> f x = 0"
shows "sum f X = 2"
using assms
proof (induction X rule: finite_induct)
case (insert x X)
have "(x = a \<or> x = b) \<or> (x \<noteq> a \<and> x \<noteq> b)"
by auto
then show ?case
proof (rule disjE)
assume "x = a \<or> x = b"
hence "sum f X = 1"
using insert sum_one_val[of X _ f] by auto
thus ?thesis
using insert by fastforce
next
assume "x \<noteq> a \<and> x \<noteq> b"
thus ?thesis
using insert by auto
qed
qed auto
lemma finite_sum_add1:
assumes "finite X" "a \<in> X" "f a = 1 + g a" "\<And>x. x \<in> X \<Longrightarrow> x \<noteq> a \<Longrightarrow> f x = g x"
shows "sum f X = 1 + sum g X"
using assms
proof (induction X rule: finite_induct)
case (insert x X)
show ?case
proof cases
assume [simp]: "x = a"
hence "sum f X = sum g X"
using insert sum.cong[of X X f g] by blast
thus ?case
using insert by (auto simp: add.assoc)
next
assume "x \<noteq> a"
hence "sum f (insert x X) = g x + 1 + sum g X"
using insert by (auto simp: add.assoc)
also have "... = 1 + g x + sum g X"
by (auto simp: add.commute)
also have "... = 1 + sum g (insert x X)"
using insert by (auto simp: add.assoc)
finally show ?case .
qed
qed auto
lemma finite_sum_add2:
assumes "finite X" "a \<in> X" "b \<in> X" "a \<noteq> b"
and "f a = 1 + g a" "f b = 1 + g b"
and "\<And>x. x \<in> X \<Longrightarrow> x \<noteq> a \<Longrightarrow> x \<noteq> b \<Longrightarrow> f x = g x"
shows "sum f X = 2 + sum g X"
using assms
proof (induction X rule: finite_induct)
case (insert x X)
have "(x = a \<or> x = b) \<or> (x \<noteq> a \<and> x \<noteq> b)"
by auto
then show ?case
proof (rule disjE)
assume "x = a \<or> x = b"
hence [simp]: "f x = 1 + g x" and [simp]: "sum f X = 1 + sum g X"
using insert finite_sum_add1[of X _ f g] by auto
have "sum f (insert x X) = f x + sum f X"
using insert by auto
also have "... = 1 + g x + 1 + sum g X"
using insert by (auto simp: add.assoc)
also have "... = (1 + 1) + (g x + sum g X)"
by (metis add.assoc add.commute)
also have "... = 2 + sum g (insert x X)"
using insert by (auto simp: add.assoc)
finally show ?thesis .
next
assume "x \<noteq> a \<and> x \<noteq> b"
thus ?thesis
using insert group_cancel.add2 by fastforce
qed
qed auto
(*
TODO: clean up lemmas \<open>thm finite_sum_add1 finite_sum_add2\<close>. Find more abstract versions.
*)
lemma sum_list_const:
fixes f :: "'a \<Rightarrow> int"
shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x = k) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) = length xs * k"
by (induction xs) (auto simp add: int_distrib mult.commute)
lemma sum_const:
fixes f :: "'a \<Rightarrow> int"
assumes "finite X" "\<And>x. x \<in> X \<Longrightarrow> f x = k"
shows "(\<Sum>x \<in> X. f x) = card X * k"
using assms by (induction X rule: finite_induct) (auto simp add: int_distrib mult.commute)
(* lemma sum_list_const:
fixes f :: "'a \<Rightarrow> int"
shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x = k) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) = length xs * k"
by (induction xs) (auto simp add: int_distrib mult.commute)
lemma sum_list_subf:
assumes "\<And>x. x \<in> set xs \<Longrightarrow> g x \<le> f x"
shows "(\<Sum>x\<leftarrow>xs. (f::'a \<Rightarrow> nat) x - g x) \<le> (\<Sum>x\<leftarrow>xs. f x) - (\<Sum>x\<leftarrow>xs. g x)"
using assms
proof (induction xs)
case Nil
then show ?case by auto
next
case (Cons x xs)
hence "(\<Sum>x\<leftarrow>x#xs. f x - g x) \<le> (\<Sum>x\<leftarrow>xs. f x) - (\<Sum>x\<leftarrow>xs. g x) + f x - g x"
using Cons by auto
also have "... \<le> (\<Sum>x\<leftarrow>x#xs. f x) - (\<Sum>x\<leftarrow>x#xs. g x)"
using Cons by (auto simp add: sum_list_mono)
finally show ?case
by auto
qed *)
(* lemma
assumes "\<And>x. x \<in> set xs \<Longrightarrow> g x \<le> f x"
shows "(\<Sum>x\<leftarrow>xs. (f::'a \<Rightarrow> nat) x - g x) + (\<Sum>x\<leftarrow>xs. g x) \<le> (\<Sum>x\<leftarrow>xs. f x)"
using assms
proof (induction xs)
case Nil
then show ?case by auto
next
case (Cons x xs)
have "(\<Sum>x\<leftarrow>x#xs. f x - g x) + (\<Sum>x\<leftarrow>x#xs. g x) \<le> (\<Sum>x\<leftarrow>xs. f x - g x) + (f x - g x) + (\<Sum>x\<leftarrow>xs. g x) + g x"
by auto
also have "... \<le> (\<Sum>x\<leftarrow>xs. f x - g x) + (\<Sum>x\<leftarrow>xs. g x) + g x + (f x - g x)"
by auto
also have "... \<le> (\<Sum>x\<leftarrow>xs. f x) + g x + (f x - g x)"
using Cons by auto
also have "... \<le> (\<Sum>x\<leftarrow>xs. f x) + f x"
by auto
also have "... \<le> (\<Sum>x\<leftarrow>x#xs. f x)"
by auto
finally show ?case
by auto
qed *)
lemma finite_sum_card:
assumes "finite X" "\<And>x. x \<in> X \<Longrightarrow> finite (f x)"
and "\<And>x y. x \<in> X \<Longrightarrow> y \<in> X \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x \<inter> f y = {}"
shows "(\<Sum>x \<in> X. card (f x)) = card (\<Union> (f ` X))"
using assms
proof (induction X rule: finite_induct)
case empty
then show ?case by auto
next
case (insert x X)
hence "f x \<inter> \<Union> (f ` X) = {}"
proof (induction X rule: finite_induct)
case (insert y X)
hence "f x \<inter> \<Union> (f ` (insert y X)) = f x \<inter> \<Union> (insert (f y) (f ` X))"
by auto
also have "... = (f x \<inter> f y) \<union> (f x \<inter> \<Union> (f ` X))"
by auto
also have "... = {}"
using insert by auto
finally show ?case .
qed auto
thus ?case
using insert by (auto simp add: card_Un_disjoint)
qed
section \<open>Graph Lemmas (Berge)\<close>
lemma graph_subset:
assumes "graph_invar E" "E' \<subseteq> E"
shows "graph_invar E'"
using assms finite_subset[OF Vs_subset] by auto
lemma Vs_emptyE:
assumes graph: "graph_invar E" and "Vs E = {}"
shows "E = {}"
proof (rule ccontr)
assume "E \<noteq> {}"
then obtain e where "e \<in> E"
by auto
moreover then obtain u v where "e = {u,v}" "u \<noteq> v"
using graph by auto
ultimately have "v \<in> Vs E"
by (auto intro: vs_member_intro)
thus "False"
using assms by auto
qed
lemma Vs_empty_iff:
assumes graph: "graph_invar E"
shows "Vs E = {} \<longleftrightarrow> E = {}"
using Vs_emptyE[OF graph] by (auto simp: Vs_def)
lemma Vs_empty_empty: "Vs {} = {}"
using vs_member_elim by force
lemma Vs_singleton: "Vs {e} = e"
unfolding Vs_def by auto
lemma Vs_union: "Vs (A \<union> B) = Vs A \<union> Vs B"
unfolding Vs_def by auto
lemma Vs_inter_subset: "Vs (A \<inter> B) \<subseteq> Vs A \<inter> Vs B"
unfolding Vs_def by auto
lemma Vs_inter_subset1: "Vs (A \<inter> B) \<subseteq> Vs A"
unfolding Vs_def by auto
lemma Vs_inter_subset2: "Vs (A \<inter> B) \<subseteq> Vs B"
unfolding Vs_def by auto
lemma complete_Vs_subset: "Vs {{u,v} | u v. u \<in> V \<and> v \<in> V \<and> u \<noteq> v} \<subseteq> V"
by (auto simp: Vs_def)
lemma edge_member_elim:
assumes "graph_invar E" and "v \<in> e" "e \<in> E"
obtains u where "e = {u,v}"
proof -
obtain u' v' where "e = {u',v'}"
using assms by auto
then show ?thesis
using that assms by auto
qed
lemma vs_member_elim2:
assumes "graph_invar E" and "v \<in> Vs E"
obtains u where "{u,v} \<in> E"
proof -
obtain e where "v \<in> e" "e \<in> E"
using assms by (auto elim: vs_member_elim)
moreover then obtain u where "e = {u,v}"
using assms by (fastforce elim: edge_member_elim)
ultimately show ?thesis
using that by auto
qed
lemma Vs_insert: "Vs (insert e E) = e \<union> Vs E"
unfolding Vs_def by auto
lemma finite_E: "finite (Vs E) \<Longrightarrow> finite E"
unfolding Vs_def using finite_UnionD by auto
lemma finite_VsI:
assumes "finite E" "\<And>e. e \<in> E \<Longrightarrow> finite e"
shows "finite (Vs E)"
unfolding Vs_def using assms by auto
lemma graph_invarI2:
assumes "finite E" "\<And>e. e \<in> E \<Longrightarrow> \<exists>u v. e = {u,v} \<and> u \<noteq> v"
shows "graph_invar E"
using assms by (auto intro: finite_VsI)
lemma graph_Un:
assumes "finite A" "\<And>a. a \<in> A \<Longrightarrow> graph_invar a"