-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.thy
1369 lines (989 loc) · 42.4 KB
/
utils.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
section "Various Utilities"
theory utils
imports Main
"HOL-Library.Monad_Syntax"
"HOL-Library.LaTeXsugar"
begin
subsection "Syntactic Sugar"
text "This theory contains definition and Lemmas that could be in the standard library."
abbreviation todo ("???") where "??? \<equiv> undefined"
abbreviation eqsome :: "'a option \<Rightarrow> 'a \<Rightarrow> bool" (infixr "\<triangleq>" 69) where
"x \<triangleq> y \<equiv> x = Some y"
abbreviation orElse :: "'a option \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "orElse" 70) where
"x orElse y \<equiv> case x of Some a \<Rightarrow> a | None \<Rightarrow> y"
text "A definition that is not automatically expanded:"
definition Def (infix "::=" 50) where
"x ::= y \<equiv> x = y"
definition DefSome (infix "::\<triangleq>" 50) where
"x ::\<triangleq> y \<equiv> y = Some x"
subsection "Logic"
lemma iffI2: "\<lbrakk>A \<Longrightarrow> B; \<not>A \<Longrightarrow> \<not>B\<rbrakk> \<Longrightarrow> A \<longleftrightarrow> B"
by auto
lemma if_cases:
"\<lbrakk>c \<Longrightarrow> P t; \<not>c \<Longrightarrow> P f\<rbrakk> \<Longrightarrow> P (if c then t else f)"
by auto
lemma subset_h1: "X \<subseteq> Y \<Longrightarrow> \<forall>x. x\<in>X \<longrightarrow> x\<in>Y"
by blast
lemma rewrite_and_implies:
shows "a \<and> b \<longrightarrow> c \<longleftrightarrow> a \<longrightarrow> b \<longrightarrow> c"
by auto
lemma flip: "(\<not>Q \<Longrightarrow> \<not>P) \<Longrightarrow> P \<longrightarrow> Q"
by auto
lemma exists_cases1:
shows "(\<exists>x. (x = A \<longrightarrow> P x) \<and> (x \<noteq> A \<longrightarrow> Q x))
\<longleftrightarrow> (P A) \<or> (\<exists>x. x \<noteq> A \<and> Q x)"
by auto
lemma exists_cases2:
shows "(\<exists>x. (x \<noteq> A \<longrightarrow> Q x) \<and> (x = A \<longrightarrow> P x))
\<longleftrightarrow> (P A) \<or> (\<exists>x. x \<noteq> A \<and> Q x)"
by auto
lemma exists_nat_split: "(\<exists>n::nat. P n) \<longleftrightarrow> (P 0 \<or> (\<exists>n. P (Suc n)))"
using zero_induct by blast
subsection "Lists"
lemma append_eq_conv_conj2:
"(xs = ys @ zs) \<longleftrightarrow> (take (length ys) xs = ys \<and> (drop (length ys) xs) = zs)" for xs ys zs
by (metis append_eq_conv_conj)
lemma cons_eq_conv_conj:
"(xs = y # ys) \<longleftrightarrow> (xs \<noteq> [] \<and> y = hd xs \<and> ys = tl xs)" for xs ys zs
by force
lemma hd_drop_conv_nth2: "\<lbrakk>i<length xs; a = hd (drop i xs)\<rbrakk> \<Longrightarrow> xs ! i = a"
by (simp add: hd_drop_conv_nth)
lemma eq_tl: "\<lbrakk>xs \<noteq> []; \<And>a as. xs = a#as \<Longrightarrow> drop i ys = as\<rbrakk> \<Longrightarrow> drop i ys = tl xs"
by (cases xs, auto)
lemma nth_append_second:
"i \<ge> length xs \<Longrightarrow> (xs@ys)!i = ys!(i - length xs)"
by (auto simp add: nth_append split: if_splits)
lemma nth_cons_tail:
"i > 0 \<Longrightarrow> (x#xs)!i = xs!(i - 1)"
by (auto simp add: nth_Cons split: nat.splits)
lemma nth_append_first:
"i < length xs \<Longrightarrow> (xs@ys)!i = xs!i"
by (auto simp add: nth_append split: if_splits)
lemma show_appendEqH:
"\<lbrakk>n \<le> length ys; length xs \<ge> n; take n xs = take n ys; drop n xs = zs\<rbrakk> \<Longrightarrow> xs = (take n ys) @ zs"
by (metis append_take_drop_id)
lemma nth_drop_if:
"drop n xs ! i = (if n \<le> length xs then xs ! (n + i) else [] ! i)"
by auto
definition "in_sequence xs x y \<equiv> \<exists>i j. i<j \<and> j<length xs \<and> xs!i = x \<and> xs!j=y "
lemma in_sequence_nil[simp]: "in_sequence [] = (\<lambda>x y. False)"
apply (rule ext)+
by (auto simp add: in_sequence_def)
lemma in_sequence_cons:
"in_sequence (x # xs) a b \<longleftrightarrow> (x=a \<and> b\<in>set xs \<or> in_sequence xs a b)"
apply (auto simp add: in_sequence_def)
apply (metis (no_types, lifting) Suc_diff_eq_diff_pred Suc_less_eq Suc_pred gr_implies_not_zero not_gr_zero nth_Cons' zero_less_diff)
apply (metis Suc_mono in_set_conv_nth nth_Cons_0 nth_Cons_Suc zero_less_Suc)
by (meson Suc_mono nth_Cons_Suc)
lemma in_sequence_append:
"in_sequence (xs @ ys) a b \<longleftrightarrow> (a\<in>set xs \<and> b\<in>set ys \<or> in_sequence xs a b \<or> in_sequence ys a b)"
proof (induct xs)
case Nil
then show ?case
by auto
next
case (Cons x xs)
have "in_sequence ((x#xs) @ ys) a b
\<longleftrightarrow> x = a \<and> b \<in> set (xs@ys) \<or> in_sequence (xs@ys) a b"
by (auto simp add: in_sequence_cons)
also have "... \<longleftrightarrow> x = a \<and> b \<in> set (xs@ys) \<or> (a\<in>set xs \<and> b\<in>set ys \<or> in_sequence xs a b \<or> in_sequence ys a b)"
using Cons by auto
also have "... \<longleftrightarrow> (a\<in>set (x#xs) \<and> b\<in>set ys \<or> in_sequence (x#xs) a b \<or> in_sequence ys a b)"
by (auto simp add: in_sequence_cons)
finally show ?case by simp
qed
lemma in_sequence_in1: "in_sequence xs x y \<Longrightarrow> x\<in>set xs"
by (metis in_sequence_def in_set_conv_nth less_imp_le less_le_trans)
lemma in_sequence_in2: "in_sequence xs x y \<Longrightarrow> y\<in>set xs"
by (metis in_sequence_def nth_mem)
definition "before_in_list xs x y \<equiv> \<exists>i j. i < j \<and> j < length xs \<and> xs!i=x \<and> xs!j=y"
lemma before_in_list_def2:
"before_in_list xs x y \<longleftrightarrow> (\<exists>xsa xsb. xs = xsa@xsb \<and> x\<in>set xsa \<and> y\<in>set xsb)"
unfolding before_in_list_def
proof (intro iffI conjI; elim exE conjE)
show "\<exists>xsa xsb. xs = xsa @ xsb \<and> x \<in> set xsa \<and> y \<in> set xsb"
if c0: "i < j"
and c1: "j < length xs"
and c2: "xs ! i = x"
and c3: "xs ! j = y"
for i j
proof (intro exI conjI)
show "xs = take j xs @ drop j xs"
by simp
show "x \<in> set (take j xs)"
using c0 c1 c2 in_set_conv_nth by fastforce
show " y \<in> set (drop j xs)"
by (metis Cons_nth_drop_Suc c1 c3 list.set_intros(1))
qed
show "\<exists>i j. i < j \<and> j < length xs \<and> xs ! i = x \<and> xs ! j = y"
if c0: "xs = xsa @ xsb"
and c1: "x \<in> set xsa"
and c2: "y \<in> set xsb"
for xsa xsb
proof -
from c1 obtain i where "xsa ! i = x" and "i < length xsa"
by (meson in_set_conv_nth)
from c2 obtain j where "xsb ! j = y" and "j < length xsb"
by (meson in_set_conv_nth)
show ?thesis
proof (intro exI conjI)
show "i < length xsa +j"
by (simp add: \<open>i < length xsa\<close> add.commute trans_less_add2)
show "length xsa + j < length xs"
by (simp add: \<open>j < length xsb\<close> c0)
show " xs ! i = x"
by (simp add: \<open>i < length xsa\<close> \<open>xsa ! i = x\<close> c0 nth_append_first)
show "xs ! (length xsa + j) = y"
by (simp add: \<open>xsb ! j = y\<close> c0)
qed
qed
qed
lemma before_in_list_cons:
"before_in_list (x#xs) a b \<longleftrightarrow> (if x = a then b\<in>set xs else before_in_list xs a b )"
proof (auto simp add: before_in_list_def2)
show "b \<in> set xs"
if c0: "x = a"
and c1: "a # xs = xsa @ xsb"
and c2: "a \<in> set xsa"
and c3: "b \<in> set xsb"
for xsa xsb
using that by (metis Un_iff empty_iff empty_set list.sel(3) set_append tl_append2)
show "\<exists>xsa xsb. a # xs = xsa @ xsb \<and> a \<in> set xsa \<and> b \<in> set xsb"
if c0: "x = a"
and c1: "b \<in> set xs"
proof (intro exI conjI)
show "a # xs = [a] @ xs" by simp
qed (auto simp add: c1)
show "\<exists>xsa xsb. xs = xsa @ xsb \<and> a \<in> set xsa \<and> b \<in> set xsb"
if c0: "x \<noteq> a"
and c1: "x # xs = xsa @ xsb"
and c2: "a \<in> set xsa"
and c3: "b \<in> set xsb"
for xsa xsb
proof (intro exI conjI)
show "xs = tl xsa @ xsb"
by (metis c1 c2 equals0D list.sel(3) set_empty tl_append2)
show "a \<in> set (tl xsa)"
by (metis c0 c1 c2 hd_append list.collapse list.sel(1) set_ConsD tl_Nil)
qed (simp add: c3)
show "\<exists>xsaa xsba. x # xsa @ xsb = xsaa @ xsba \<and> a \<in> set xsaa \<and> b \<in> set xsba"
if c0: "x \<noteq> a"
and c1: "xs = xsa @ xsb"
and c2: "a \<in> set xsa"
and c3: "b \<in> set xsb"
for xsa xsb
proof (intro exI conjI)
show "x # xsa @ xsb = (x#xsa) @ xsb" by simp
qed(auto simp add: that)
qed
lemma before_in_list_contains_l: "before_in_list cs x y \<Longrightarrow> x\<in>set cs"
by (metis before_in_list_def dual_order.strict_trans in_set_conv_nth)
lemma before_in_list_contains_r: "before_in_list cs x y \<Longrightarrow> y\<in>set cs"
by (metis before_in_list_def in_set_conv_nth)
lemma before_in_list_empty[simp]: "\<not>before_in_list [] x y"
by (simp add: before_in_list_def)
subsection "Math"
lemma sumSplit:
fixes f :: "nat \<Rightarrow> nat"
shows "(\<Sum>i<x+y . f i) = (\<Sum>i<x . f i) + (\<Sum>i<y . f (x+i))"
by (induct y, auto)
subsection "Least"
lemma LeastI2:
"\<lbrakk>x = (LEAST x::nat. P x); P y\<rbrakk> \<Longrightarrow> P x"
by (simp add: LeastI)
lemma usePropertyOfLeast:
fixes x :: "'a :: wellorder"
assumes wellDefined: "Q x"
and weakerProperty: "\<And>x. Q x \<Longrightarrow> P x"
shows "P (LEAST x. Q x)"
using LeastI weakerProperty wellDefined by auto
lemma showIsLeast:
fixes x :: "'a :: wellorder"
assumes "P x"
and "\<And>y. P y \<Longrightarrow> x \<le> y"
shows "x = (LEAST x. P x)"
using Least_equality assms(1) assms(2) by auto
lemma nth_secondHalf_eq:
assumes "i\<ge>length xs"
and "length xs = length ys"
shows "(xs@zs)!i = (ys@zs)!i"
using assms by (auto simp add: nth_append)
subsection "Maximum of a set of nats"
definition max_natset :: "nat set \<Rightarrow> nat" where
"max_natset S \<equiv> if S = {} then 0 else Suc (Max S)"
lemma max_natset_empty: "max_natset S = 0 \<longleftrightarrow> S = {}"
by (simp add: max_natset_def)
lemma max_natset_Collect_Suc:
assumes "max_natset {x. P x} = Suc i"
and "finite {x. P x}"
shows "P i"
and "\<And>j. P j \<Longrightarrow> j\<le>i"
using assms by (auto simp add: max_natset_def split: if_splits,
insert Max_in, blast)
lemma show_max_natset_smaller:
assumes "i \<in> S"
and "finite S"
and "\<And>j. j\<in>S' \<Longrightarrow> j < i"
shows "max_natset S' < max_natset S"
using assms by (auto simp add: max_natset_def,
metis Max_gr_iff Max_in all_not_in_conv bounded_nat_set_is_finite)
lemma show_max_natset_smaller_Collect:
assumes "P i"
and "finite {i. P i}"
and "\<And>j. P' j \<Longrightarrow> j < i"
shows "max_natset {i. P' i} < max_natset {i. P i}"
by (rule show_max_natset_smaller,
insert assms, force+)
subsection "Induction over greatest and smallest elements"
lemma finiteH:
"finite {x::nat. 0 < x \<and> x < A \<and> P x}"
by simp
text "Like @{thm[mode=Rule] less_induct}, but reversed with an upper bound.
We only need it for natural numbers, but it could probably be generalized.
"
lemma greater_induct [case_names greater]:
assumes step: "\<And>x. \<lbrakk>\<And>y. \<lbrakk>y > x; y < bound\<rbrakk> \<Longrightarrow> P y\<rbrakk> \<Longrightarrow> P x"
shows "P (a::nat)"
proof (induct "bound - a" arbitrary: a rule: less_induct)
case less
show "P a"
proof (rule step)
show "P y" if "a < y" and "y < bound" for y
proof (rule less)
show "bound - y < bound - a"
using diff_less_mono2 dual_order.strict_trans that by blast
qed
qed
qed
lemma exists_greatest:
assumes example: "P j"
and bounded: "\<And>j. P j \<Longrightarrow> j \<le> bound"
shows "\<exists>j::nat. P j \<and> (\<forall>j'. P j' \<longrightarrow> j' \<le> j)"
using example proof (induct "bound - j" arbitrary: j)
case 0
with bounded
have "j = bound"
by force
then show ?case
using "0.prems" bounded by blast
next
case (Suc i)
then show ?case
by (metis bounded bounded_Max_nat)
qed
lemma exists_greatest':
assumes example: "\<exists>j. P j"
and bounded: "\<exists>bound. \<forall>j. P j \<longrightarrow> j \<le> bound"
shows "\<exists>j::nat. P j \<and> (\<forall>j'. P j' \<longrightarrow> j' \<le> j)"
using bounded example exists_greatest by auto
lemma split_take:
assumes "ys = drop i xs"
shows "xs = take i xs @ ys"
by (simp add: assms)
lemma use_Greatest:
assumes "\<exists>x. P x"
and "\<exists>bound. \<forall>x. P x \<longrightarrow> x \<le> bound"
shows "P (GREATEST x::nat. P x)
\<and> (\<forall>y. P y \<longrightarrow> y \<le> (GREATEST x::nat. P x))"
using GreatestI_nat Greatest_le_nat assms by auto
lemma use_Greatest2:
assumes "P x"
and "\<forall>x. P x \<longrightarrow> x \<le> bound"
shows "P (GREATEST x::nat. P x)
\<and> (\<forall>y. P y \<longrightarrow> y \<le> (GREATEST x::nat. P x))"
by (rule use_Greatest, insert assms, auto)
lemma Greatest_smaller:
assumes "\<exists>x::nat. P x"
and "\<exists>bound. \<forall>x. P x \<longrightarrow> x \<le> bound"
and "\<And>y. P y \<Longrightarrow> y < x"
shows "Greatest P < x"
using assms
using GreatestI_nat by auto
lemma Greatest_bigger:
fixes P :: "nat \<Rightarrow> bool"
assumes "P y"
and "\<exists>bound. \<forall>x. P x \<longrightarrow> x \<le> bound"
and "x < y"
shows "x < Greatest P"
proof -
from \<open>P y\<close> have "\<exists>x. P x" by auto
from use_Greatest[OF \<open>\<exists>x. P x\<close> \<open>\<exists>bound. \<forall>x. P x \<longrightarrow> x \<le> bound\<close>] assms
show "x < Greatest P"
by auto
qed
text "Like @{thm[mode=Rule] less_induct}, but reversed with an upper bound.
We only need it for natural numbers, but it could probably be generalized.
"
lemma greater_induct2 [case_names bounded greater]:
assumes "a < bound"
and step: "\<And>x. \<lbrakk>\<And>y. \<lbrakk>y > x; y < bound\<rbrakk> \<Longrightarrow> P y; x < bound\<rbrakk> \<Longrightarrow> P x"
shows "P (a::nat)"
using `a < bound`
proof (induct "bound - a" arbitrary: a rule: less_induct)
case less
show "P a"
proof (rule step)
show "P y" if "a < y" and "y < bound" for y
proof (rule less)
show "bound - y < bound - a"
using diff_less_mono2 dual_order.strict_trans that by blast
show " y < bound"
by (simp add: that(2))
qed
show "a < bound"
using less.prems by auto
qed
qed
definition "is_bounded S bound \<equiv> \<forall>x\<in>S. x < bound"
lemma use_is_bounded:
assumes "is_bounded S bound" and "x \<in> S"
shows "x < bound"
using assms is_bounded_def by blast
lemma min_set_induct[consumes 1, induct set: is_bounded, case_names empty step[not_empty bounded IH]]:
fixes S :: "nat set"
assumes bounded: "is_bounded S bound"
and empty: "P {}"
and step: "\<And>S. \<lbrakk>S \<noteq> {}; is_bounded S bound; (\<And>S'. \<lbrakk>S' \<noteq> {} \<Longrightarrow> Inf S' > Inf S; is_bounded S' bound\<rbrakk> \<Longrightarrow> P S')\<rbrakk> \<Longrightarrow> P S"
shows "P S"
using bounded
proof (induct "if S = {} then 0 else bound - (Inf S)" arbitrary: S rule: less_induct)
case less
show ?case
proof (cases "S = {}")
case True
then show ?thesis
by (simp add: empty)
next
case False
show ?thesis
proof (rule step)
from ` S \<noteq> {}` obtain x where "x \<in> S"
by blast
moreover have "x < bound"
using calculation less.prems use_is_bounded by blast
ultimately have "Inf S < bound"
by (meson False Inf_nat_def1 is_bounded_def less.prems)
show "S \<noteq> {}"
by (simp add: False)
show "is_bounded S bound"
using less.prems by blast
show "P S'"
if c1: "S' \<noteq> {} \<Longrightarrow> Inf S < Inf S'"
and c2: "is_bounded S' bound"
for S'
proof (rule less)
show "(if S' = {} then 0 else bound - Inf S') < (if S = {} then 0 else bound - Inf S)"
by (auto simp add: \<open>Inf S < bound\<close> \<open>S \<noteq> {}\<close> c1 diff_less_mono2)
show "is_bounded S' bound"
using c2 by blast
qed
qed
qed
qed
lemma forward_induct[case_names zero step[given broken bound]]:
assumes init: "Q 0"
and step: "\<And>i::nat. \<lbrakk>Q i; \<not>Q (Suc i); i < bound\<rbrakk> \<Longrightarrow> \<exists>j>i. Q j "
shows "\<exists>i\<ge>bound. Q i"
proof (rule ccontr, clarsimp)
assume "\<forall>i\<ge>bound. \<not> Q i"
hence bounded: "\<forall>y. Q y \<longrightarrow> y < bound"
using not_le_imp_less by blast
hence bounded2: "\<forall>y. Q y \<longrightarrow> y \<le> bound - 1"
by auto
from use_Greatest2[OF init bounded2]
have "Q (GREATEST x. Q x)"
and "(\<forall>y. Q y \<longrightarrow> y \<le> (GREATEST x. Q x))"
by auto
from `Q (GREATEST x. Q x)`
have "(GREATEST x. Q x) < bound"
by (simp add: bounded)
obtain i
where "Q i"
and "\<forall>j>i. \<not>Q j"
and "i < bound"
using \<open>(GREATEST x. Q x) < bound\<close> \<open>Q (GREATEST x. Q x) \<and> (\<forall>y. Q y \<longrightarrow> y \<le> (GREATEST x. Q x))\<close> leD by auto
have "\<exists>j>i. Q j"
using `Q i`
proof (rule step)
show "i < bound" using `i < bound` .
show "\<not> Q (Suc i)"
by (simp add: \<open>\<forall>j>i. \<not> Q j\<close>)
qed
thus False
using \<open>\<forall>j>i. \<not> Q j\<close> by blast
qed
lemma show_Inf_smaller:
assumes "(i::nat) \<in> S"
and "\<And>i'. i'\<in>S' \<Longrightarrow> i < i'"
and "S' \<noteq> {}"
shows "Inf S < Inf S'"
by (metis Inf_nat_def1 assms(1) assms(2) assms(3) bdd_above_bot cInf_less_iff empty_iff)
subsection "Well orders"
text "All types have an implicit well order that can be used when deterministically picking an
arbitrary element from a set."
definition some_well_order :: "'a rel" where
"some_well_order \<equiv> (SOME ord. well_order ord)"
lemma some_well_order_is_well_order: "well_order some_well_order"
by (metis someI_ex some_well_order_def well_ordering)
lemma some_well_order_is_linear_order: "linear_order some_well_order"
using some_well_order_is_well_order well_order_on_def by blast
lemma some_well_order_is_wo_rel: "wo_rel some_well_order"
using some_well_order_is_well_order well_order_on_Well_order wo_rel_def by blast
lemma some_well_order_includes_all: "S \<subseteq> Field some_well_order"
using some_well_order_is_well_order well_order_on_Field by fastforce
definition firstValue :: "'a \<Rightarrow> ('b \<Rightarrow> 'a option) \<Rightarrow> 'a" where
"firstValue d m \<equiv> if m = Map.empty then d else
let maxK = wo_rel.minim some_well_order (dom m) in
the (m maxK)
"
lemma firstValue_in_ran:
assumes "finite (dom m)"
and not_default: "firstValue d m \<noteq> d"
shows "firstValue d m \<in> Map.ran m"
using not_default proof (auto simp add: firstValue_def )
assume "m \<noteq> Map.empty"
have "(wo_rel.minim some_well_order (dom m)) \<in> dom m"
by (simp add: \<open>m \<noteq> Map.empty\<close> some_well_order_includes_all some_well_order_is_wo_rel wo_rel.minim_in)
from this
show "the (m (wo_rel.minim some_well_order (dom m))) \<in> ran m"
by (meson domIff option.exhaust_sel ranI)
qed
subsection "Option Type"
text "We define some nicer syntax for working with options and maps."
lemma option_bind_def:
"(x \<bind> f) = (case x of None \<Rightarrow> None | Some a \<Rightarrow> f a)"
by (metis bind.bind_lunit bind_eq_None_conv option.case_eq_if option.exhaust_sel)
definition map_chain :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('b \<rightharpoonup> 'c) \<Rightarrow> 'a \<rightharpoonup> 'c" (infixr "\<ggreater>" 54) where
"(f \<ggreater> g) \<equiv> \<lambda>x. f x \<bind> g"
definition map_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<rightharpoonup> 'c" where
"(map_map f g) \<equiv> \<lambda>x. map_option g (f x)"
lemma dom_map_chain:
"dom (f \<ggreater> g) = {x | x y z. f x \<triangleq> y \<and> g y \<triangleq> z}"
by (auto simp add: map_chain_def option_bind_def split: option.splits)
lemma dom_map_map[simp]:
"dom (map_map f g) = dom f"
by (auto simp add: map_map_def)
lemma map_map_apply_eq_some[simp]:
"(map_map f g x \<triangleq> z) \<longleftrightarrow> (\<exists>y. f x \<triangleq> y \<and> g y = z)"
by (auto simp add: map_map_def split: option.splits)
lemma map_map_apply_eq_none[simp]:
"(map_map f g x = None) \<longleftrightarrow> (f x = None)"
by (auto simp add: map_map_def split: option.splits)
lemma map_map_apply:
"map_map f g x = (case f x of None \<Rightarrow> None | Some y \<Rightarrow> Some (g y))"
by (auto simp add: map_map_def split: option.splits)
definition is_reverse :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> bool" where
"is_reverse f_in f_out \<equiv>
(\<forall>x y. ((f_in x) \<triangleq> y) \<longrightarrow> (f_out y = x))
\<and> (\<forall>x. (f_in (f_out x)) \<triangleq> x)"
lemma is_reverse_1:
assumes "is_reverse f_in f_out"
and "f_in x \<triangleq> y"
shows "f_out y = x"
by (meson assms is_reverse_def)
lemma is_reverse_2:
assumes "is_reverse f_in f_out"
shows "f_in (f_out x) \<triangleq> x"
by (meson assms is_reverse_def)
lemma is_reverse_combine:
assumes is_rev: "is_reverse C_in C_out"
and is_rev': "is_reverse C_in' C_out'"
shows "is_reverse (C_in' \<ggreater> C_in) (C_out' \<circ> C_out)"
by (smt bind_eq_Some_conv comp_apply is_rev is_rev' is_reverse_def map_chain_def)
lemma is_reverse_trivial: "is_reverse Some id"
by (simp add: is_reverse_def)
subsection "Almost the Same"
text "Sometimes we want to express that two maps or relations are almost
the same (just on a subset)."
definition "map_same_on Cs op op' \<equiv> \<forall>c\<in>Cs. op c = op' c"
definition "rel_same_on Cs hb hb' \<equiv> \<forall>x\<in>Cs.\<forall>y\<in>Cs. (x,y)\<in>hb \<longleftrightarrow> (x,y)\<in>hb'"
lemma map_same_on_trivial[simp]:
"map_same_on Cs x x"
by (simp add: map_same_on_def)
lemma rel_same_on_trivial[simp]:
"rel_same_on Cs x x"
by (simp add: rel_same_on_def)
definition
"is_from x initial S \<equiv> if S = {} then x = initial else x \<in> S"
lemma is_from_exists:
assumes "\<exists>x. x\<in>S"
shows "is_from x initial S \<longleftrightarrow> x \<in> S"
by (metis assms empty_iff is_from_def)
lemma is_from_not_initial:
assumes "x\<noteq> initial"
shows "is_from x initial S \<longleftrightarrow> x \<in> S"
by (metis assms empty_iff is_from_def)
subsection "Minimums and Maximums"
text "A finite set with an acyclic order has minimal elements."
lemma exists_min_wf:
assumes wf: "wf r"
and "P x"
shows "\<exists>x. P x \<and> (\<forall>y. P y \<longrightarrow> (y,x)\<notin>r)"
by (metis assms mem_Collect_eq wf_eq_minimal)
lemma exists_max_wf:
assumes wf: "wf (r\<inverse>)"
and "P x"
shows "\<exists>x. P x \<and> (\<forall>y. P y \<longrightarrow> (x,y)\<notin>r)"
by (metis assms converse.intros exists_min_wf)
lemma exists_min_wf':
assumes wf: "wf r"
and "trans r"
and "P x"
shows "\<exists>x'. P x' \<and> (x = x' \<or> (x',x)\<in>r) \<and> (\<forall>y. P y \<longrightarrow> (y,x')\<notin>r)"
proof -
from exists_min_wf[OF wf, where P="\<lambda>x'. P x' \<and> (x = x' \<or> (x',x)\<in>r)"]
obtain x'
where "P x'"
and "x = x' \<or> (x', x) \<in> r"
and "\<forall>y. P y \<and> (x = y \<or> (y, x) \<in> r) \<longrightarrow> (y, x') \<notin> r"
using `P x` by blast
have "\<forall>y. P y \<longrightarrow> (y,x')\<notin>r"
by (metis \<open>\<forall>y. P y \<and> (x = y \<or> (y, x) \<in> r) \<longrightarrow> (y, x') \<notin> r\<close> \<open>x = x' \<or> (x', x) \<in> r\<close> `trans r` transD)
thus ?thesis
using \<open>P x'\<close> \<open>x = x' \<or> (x', x) \<in> r\<close> by blast
qed
lemma exists_max_wf':
assumes wf: "wf (r\<inverse>)"
and "trans r"
and "P x"
shows "\<exists>x'. P x' \<and> (x = x' \<or> (x,x')\<in>r) \<and> (\<forall>y. P y \<longrightarrow> (x',y)\<notin>r)"
using assms exists_min_wf' by fastforce
lemma exists_min:
assumes fin: "finite S"
and nonempty: "x\<in>S"
and acyclic: "acyclic r"
shows "\<exists>x. x\<in>S \<and> (\<forall>y\<in>S. (y,x)\<notin>r)"
proof -
have "wf (Restr r S)"
proof (rule finite_acyclic_wf)
show "finite (Restr r S)"
using fin by simp
show "acyclic (Restr r S)"
using acyclic by (meson Int_lower1 acyclic_subset)
qed
show ?thesis
by (smt IntI \<open>wf (Restr r S)\<close> mem_Sigma_iff nonempty wfE_min)
qed
lemma exists_min_wellorder:
fixes P :: "'a::wellorder \<Rightarrow> bool"
assumes nonempty: "P x"
shows "\<exists>x. P x \<and> (\<forall>y. P y \<longrightarrow> x \<le> y)"
by (meson LeastI nonempty wellorder_Least_lemma(2))
text "A finite set with an acyclic order has maximal elements."
lemma exists_max:
assumes fin: "finite S"
and nonempty: "x\<in>S"
and acyclic: "acyclic r"
shows "\<exists>x. x\<in>S \<and> (\<forall>y\<in>S. \<not>(x,y)\<in>r)"
proof -
have "\<exists>x. x\<in>S \<and> (\<forall>y\<in>S. \<not>(y,x)\<in>r\<inverse>)"
using fin nonempty
proof (rule exists_min)
show "acyclic (r\<inverse>)"
by (simp add: acyclic)
qed
thus ?thesis
by simp
qed
lemma ex_least_nat_le':
fixes n:: "'a::wellorder"
assumes "P n"
shows"\<exists>k\<le>n. (\<forall>i<k. \<not> P i) \<and> P k"
by (rule exI[where x="LEAST k. P k"],
blast intro: Least_le `P n` LeastI_ex dest: not_less_Least)
lemma ex_least_nat_le'':
fixes P:: "'a::wellorder \<Rightarrow> bool"
assumes "Ex P"
shows"\<exists>k. (\<forall>i<k. \<not> P i) \<and> P k"
using assms ex_least_nat_le' by blast
subsection "Maps"
lemma ran_empty_iff[simp] :"(ran F = {}) \<longleftrightarrow> F = Map.empty"
by (metis empty_iff option.exhaust_sel ranI ran_empty)
lemma eq_map_empty[simp] :"(M = Map.empty) \<longleftrightarrow> (\<forall>x. M x = None)"
by (rule fun_eq_iff)
definition
"map_update_all s_callOrigin localCalls tx \<equiv> s_callOrigin ++ (Map.map_of (map (\<lambda>c. (c, tx)) localCalls))"
lemma map_update_all_empty[simp]: "map_update_all co [] t = co"
by (simp add: map_update_all_def)
lemma map_update_all_get:
"map_update_all co cs tx c = (if c\<in>set cs then Some tx else co c)"
using split_list by (auto simp add: map_update_all_def map_add_def dest: map_of_SomeD split: option.splits, fastforce)
lemma map_update_all_None:
"map_update_all m xs y x = None \<longleftrightarrow> (x\<notin>set xs \<and> m x = None)"
by (induct xs, auto simp add: map_update_all_def)
lemma map_update_all_Some_same:
"map_update_all m xs y x \<triangleq> y \<longleftrightarrow> (x\<in>set xs \<or> m x \<triangleq> y)"
by (induct xs, auto simp add: map_update_all_def)
lemma map_update_all_Some_other:
assumes "y' \<noteq> y"
shows "map_update_all m xs y x \<triangleq> y' \<longleftrightarrow> (x\<notin>set xs \<and> m x \<triangleq> y')"
using assms by (induct xs, auto simp add: map_update_all_def)
lemma map_of_None: "map_of xs x = None \<longleftrightarrow> (\<forall>y. (x,y)\<notin>set xs)"
by (induct xs, auto)
lemma map_of_Some: "\<lbrakk>distinct (map fst xs)\<rbrakk> \<Longrightarrow> map_of xs x = Some y \<longleftrightarrow> ((x,y)\<in>set xs)"
by (induct xs, auto, (metis map_of_eq_None_iff map_of_is_SomeI option.simps(3)))
lemma restrict_map_noop: "dom m \<subseteq> S \<Longrightarrow> m |` S = m"
using domIff by (auto simp add: restrict_map_def intro!: ext, force)
lemma restrict_map_noop2[simp]: "m |` dom m = m"
by (simp add: restrict_map_noop)
subsection "Sets"
lemma Set_fillter_insert[simp]:
"Set.filter f (insert x S) = (if f x then {x} else {}) \<union> Set.filter f S"
by (auto simp add: Set.filter_def)
lemma Set_filter_empty[simp]:
"Set.filter f {} = {}"
by (auto simp add: Set.filter_def)
lemma domExists_simp: "x \<in> dom f \<longleftrightarrow> (\<exists>y. f x \<triangleq> y)"
by (auto)
lemma in_dom:
assumes "S \<subseteq> dom T" and "x \<in> S"
shows "\<exists>y. T x \<triangleq> y"
using assms by blast
lemma in_img_simp: "y\<in>f`S \<longleftrightarrow> (\<exists>x\<in>S. f x = y)"
by auto
subsection "Relations"
definition restrict_relation :: "'a rel \<Rightarrow> 'a set \<Rightarrow> 'a rel" (infixl "|r" 110)
where "r |r A \<equiv> Restr r A" \<comment> \<open>This is a definition because Restr is just an abbreviation
and does not behave well when using methods like auto.\<close>
definition downwardsClosure :: "'a set \<Rightarrow> 'a rel \<Rightarrow> 'a set" (infixr "\<down>" 100) where
"S \<down> R \<equiv> S \<union> {x | x y . (x,y)\<in>R \<and> y\<in>S}"
lemma downwardsClosure_in:
"x \<in> S \<down> R \<longleftrightarrow> (x\<in>S \<or> (\<exists>y\<in>S. (x,y)\<in>R))"
by (auto simp add: downwardsClosure_def)
lemma downwardsClosure_subset:
"S \<down> R \<subseteq> S \<union> fst ` R"
by (auto simp add: downwardsClosure_in Domain.DomainI fst_eq_Domain)
lemma restrict_relation_noop: "Field r \<subseteq> S \<Longrightarrow> r |r S = r"
by (auto simp add: restrict_relation_def FieldI1 FieldI2 subset_h1)
lemma infinite_if_mappable_to_nat:
assumes mapping: "\<And>n::nat. \<exists>x\<in>S. f x \<ge> n"
shows "infinite S"
proof auto
assume "finite S"
hence "finite (f ` S)"
by force
define m where "m \<equiv> Max (f ` S)"
from mapping[where n="Suc m"] obtain x where
"x\<in>S" and "f x \<ge> Suc m"
by auto
have "f x \<in> (f ` S)"
using \<open>x \<in> S\<close> by blast
have "f x > m"
using Suc_le_eq \<open>Suc m \<le> f x\<close> by blast
hence "f x > Max (f ` S)"
using m_def by blast
thus False
using Max_ge \<open>f x \<in> f ` S\<close> \<open>finite (f ` S)\<close> leD by blast
qed
subsection "Paths in Relations"
text "A list can be a path in a relation where subsequent elements are in relation."
definition is_path
where "is_path path r \<equiv> (\<forall>i<length path - 1. (path!i, path!(Suc i))\<in>r)"
lemma is_path_empty[simp]:
shows "is_path [] r"
by (auto simp add: is_path_def)
lemma is_path_single[simp]:
shows "is_path [x] r"
by (auto simp add: is_path_def)
lemma is_path_cons:
shows "is_path (x#y#xs) r \<longleftrightarrow> (x,y)\<in>r \<and> is_path (y#xs) r"
using less_Suc_eq_0_disj by (auto simp add: is_path_def)
lemma is_path_cons2:
shows "is_path (x#xs) r \<longleftrightarrow> (xs = [] \<or> (x,hd xs)\<in>r \<and> is_path xs r)"
by (metis is_path_cons is_path_single list.collapse)
lemma is_path_append:
assumes "xs \<noteq> []" and "ys \<noteq> []"
shows
"is_path (xs@ys) r \<longleftrightarrow> is_path xs r \<and> (last xs, hd ys)\<in>r \<and> is_path ys r"
using assms by (induct xs, auto simp add: is_path_cons2)
lemma is_path_trans_cl:
shows "(x,y)\<in>r\<^sup>+ \<longleftrightarrow> (\<exists>path. length path > 1 \<and> is_path path r \<and> hd path = x \<and> last path = y)"
proof
show "\<exists>path. 1 < length path \<and> is_path path r \<and> hd path = x \<and> last path = y" if "(x, y) \<in> r\<^sup>+"
using that
proof (induct)
case (base y)
show ?case
proof (intro conjI exI)
show "hd [x,y] = x" by simp
show "last [x, y] = y" by simp