61203
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section {* List Insertion and Deletion *}
|
|
4 |
|
|
5 |
theory List_Ins_Del
|
|
6 |
imports Sorted_Less
|
|
7 |
begin
|
|
8 |
|
|
9 |
subsection \<open>Elements in a list\<close>
|
|
10 |
|
|
11 |
fun elems :: "'a list \<Rightarrow> 'a set" where
|
|
12 |
"elems [] = {}" |
|
|
13 |
"elems (x#xs) = Set.insert x (elems xs)"
|
|
14 |
|
|
15 |
lemma elems_app: "elems (xs @ ys) = (elems xs \<union> elems ys)"
|
|
16 |
by (induction xs) auto
|
|
17 |
|
|
18 |
lemma elems_eq_set: "elems xs = set xs"
|
|
19 |
by (induction xs) auto
|
|
20 |
|
|
21 |
lemma sorted_Cons_iff:
|
|
22 |
"sorted(x # xs) = (sorted xs \<and> (\<forall>y \<in> elems xs. x < y))"
|
|
23 |
by(simp add: elems_eq_set Sorted_Less.sorted_Cons_iff)
|
|
24 |
|
|
25 |
lemma sorted_snoc_iff:
|
|
26 |
"sorted(xs @ [x]) = (sorted xs \<and> (\<forall>y \<in> elems xs. y < x))"
|
|
27 |
by(simp add: elems_eq_set Sorted_Less.sorted_snoc_iff)
|
|
28 |
|
61229
|
29 |
text{* The above two rules introduce quantifiers. It turns out
|
|
30 |
that in practice this is not a problem because of the simplicity of
|
|
31 |
the "isin" functions that implement @{const elems}. Nevertheless
|
|
32 |
it is possible to avoid the quantifiers with the help of some rewrite rules: *}
|
|
33 |
|
61203
|
34 |
lemma sorted_ConsD: "sorted (y # xs) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> y < x"
|
|
35 |
by (simp add: sorted_Cons_iff)
|
|
36 |
|
|
37 |
lemma sorted_snocD: "sorted (xs @ [y]) \<Longrightarrow> x \<in> elems xs \<Longrightarrow> x < y"
|
|
38 |
by (simp add: sorted_snoc_iff)
|
|
39 |
|
61229
|
40 |
lemma sorted_ConsD2: "sorted (y # xs) \<Longrightarrow> x \<le> y \<Longrightarrow> x \<notin> elems xs"
|
|
41 |
using leD sorted_ConsD by blast
|
|
42 |
|
|
43 |
lemma sorted_snocD2: "sorted (xs @ [y]) \<Longrightarrow> y \<le> x \<Longrightarrow> x \<notin> elems xs"
|
|
44 |
using leD sorted_snocD by blast
|
|
45 |
|
|
46 |
lemmas elems_simps = sorted_lems elems_app
|
|
47 |
lemmas elems_simps1 = elems_simps sorted_Cons_iff sorted_snoc_iff
|
|
48 |
lemmas elems_simps2 = elems_simps sorted_ConsD sorted_snocD sorted_ConsD2 sorted_snocD2
|
61203
|
49 |
|
|
50 |
|
|
51 |
subsection \<open>Inserting into an ordered list without duplicates:\<close>
|
|
52 |
|
|
53 |
fun ins_list :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
|
|
54 |
"ins_list x [] = [x]" |
|
61534
|
55 |
"ins_list x (a#xs) =
|
|
56 |
(if x < a then x#a#xs else if x=a then a#xs else a # ins_list x xs)"
|
61203
|
57 |
|
|
58 |
lemma set_ins_list[simp]: "elems (ins_list x xs) = insert x (elems xs)"
|
|
59 |
by(induction xs) auto
|
|
60 |
|
|
61 |
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
|
|
62 |
apply(induction xs rule: sorted.induct)
|
|
63 |
apply auto
|
|
64 |
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
|
|
65 |
|
|
66 |
lemma sorted_ins_list: "sorted xs \<Longrightarrow> sorted(ins_list x xs)"
|
|
67 |
by(induction xs rule: sorted.induct) auto
|
|
68 |
|
61534
|
69 |
lemma ins_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
|
|
70 |
ins_list x (xs @ a # ys) = xs @ ins_list x (a#ys)"
|
61203
|
71 |
by(induction xs) (auto simp: sorted_lems)
|
|
72 |
|
61534
|
73 |
lemma ins_list_sorted2: "sorted (xs @ [a]) \<Longrightarrow> x < a \<Longrightarrow>
|
|
74 |
ins_list x (xs @ a # ys) = ins_list x xs @ (a#ys)"
|
61203
|
75 |
by(induction xs) (auto simp: sorted_lems)
|
|
76 |
|
61231
|
77 |
lemmas ins_list_simps = sorted_lems ins_list_sorted1 ins_list_sorted2
|
61203
|
78 |
|
|
79 |
|
|
80 |
subsection \<open>Delete one occurrence of an element from a list:\<close>
|
|
81 |
|
|
82 |
fun del_list :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
|
61534
|
83 |
"del_list x [] = []" |
|
|
84 |
"del_list x (a#xs) = (if x=a then xs else a # del_list x xs)"
|
61203
|
85 |
|
|
86 |
lemma del_list_idem: "x \<notin> elems xs \<Longrightarrow> del_list x xs = xs"
|
|
87 |
by (induct xs) simp_all
|
|
88 |
|
|
89 |
lemma elems_del_list_eq [simp]:
|
|
90 |
"distinct xs \<Longrightarrow> elems (del_list x xs) = elems xs - {x}"
|
|
91 |
apply(induct xs)
|
|
92 |
apply simp
|
|
93 |
apply (simp add: elems_eq_set)
|
|
94 |
apply blast
|
|
95 |
done
|
|
96 |
|
|
97 |
lemma sorted_del_list: "sorted xs \<Longrightarrow> sorted(del_list x xs)"
|
|
98 |
apply(induction xs rule: sorted.induct)
|
|
99 |
apply auto
|
|
100 |
by (meson order.strict_trans sorted_Cons_iff)
|
|
101 |
|
61534
|
102 |
lemma del_list_sorted1: "sorted (xs @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
|
|
103 |
del_list x (xs @ a # ys) = xs @ del_list x (a # ys)"
|
61203
|
104 |
by (induction xs) (auto simp: sorted_mid_iff2)
|
|
105 |
|
61534
|
106 |
lemma del_list_sorted2: "sorted (xs @ a # ys) \<Longrightarrow> x < a \<Longrightarrow>
|
|
107 |
del_list x (xs @ a # ys) = del_list x xs @ a # ys"
|
61203
|
108 |
by (induction xs) (auto simp: sorted_Cons_iff intro!: del_list_idem)
|
|
109 |
|
|
110 |
lemma del_list_sorted3:
|
61534
|
111 |
"sorted (xs @ a # ys @ b # zs) \<Longrightarrow> x < b \<Longrightarrow>
|
|
112 |
del_list x (xs @ a # ys @ b # zs) = del_list x (xs @ a # ys) @ b # zs"
|
61203
|
113 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted2)
|
|
114 |
|
|
115 |
lemma del_list_sorted4:
|
61534
|
116 |
"sorted (xs @ a # ys @ b # zs @ c # us) \<Longrightarrow> x < c \<Longrightarrow>
|
|
117 |
del_list x (xs @ a # ys @ b # zs @ c # us) = del_list x (xs @ a # ys @ b # zs) @ c # us"
|
61203
|
118 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted3)
|
|
119 |
|
|
120 |
lemma del_list_sorted5:
|
61534
|
121 |
"sorted (xs @ a # ys @ b # zs @ c # us @ d # vs) \<Longrightarrow> x < d \<Longrightarrow>
|
|
122 |
del_list x (xs @ a # ys @ b # zs @ c # us @ d # vs) =
|
|
123 |
del_list x (xs @ a # ys @ b # zs @ c # us) @ d # vs"
|
61203
|
124 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted4)
|
|
125 |
|
61231
|
126 |
lemmas del_list_simps = sorted_lems
|
61203
|
127 |
del_list_sorted1
|
|
128 |
del_list_sorted2
|
|
129 |
del_list_sorted3
|
|
130 |
del_list_sorted4
|
|
131 |
del_list_sorted5
|
|
132 |
|
|
133 |
end
|