61203
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section {* Association List Update and Deletion *}
|
|
4 |
|
|
5 |
theory AList_Upd_Del
|
|
6 |
imports Sorted_Less
|
|
7 |
begin
|
|
8 |
|
|
9 |
abbreviation "sorted1 ps \<equiv> sorted(map fst ps)"
|
|
10 |
|
|
11 |
text{* Define own @{text map_of} function to avoid pulling in an unknown
|
|
12 |
amount of lemmas implicitly (via the simpset). *}
|
|
13 |
|
|
14 |
hide_const (open) map_of
|
|
15 |
|
|
16 |
fun map_of :: "('a*'b)list \<Rightarrow> 'a \<Rightarrow> 'b option" where
|
|
17 |
"map_of [] = (\<lambda>a. None)" |
|
|
18 |
"map_of ((x,y)#ps) = (\<lambda>a. if x=a then Some y else map_of ps a)"
|
|
19 |
|
61231
|
20 |
text \<open>Updating an association list:\<close>
|
61203
|
21 |
|
|
22 |
fun upd_list :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) list \<Rightarrow> ('a*'b) list" where
|
|
23 |
"upd_list a b [] = [(a,b)]" |
|
|
24 |
"upd_list a b ((x,y)#ps) =
|
|
25 |
(if a < x then (a,b)#(x,y)#ps else
|
|
26 |
if a=x then (a,b)#ps else (x,y) # upd_list a b ps)"
|
|
27 |
|
|
28 |
fun del_list :: "'a::linorder \<Rightarrow> ('a*'b)list \<Rightarrow> ('a*'b)list" where
|
|
29 |
"del_list a [] = []" |
|
|
30 |
"del_list a ((x,y)#ps) = (if a=x then ps else (x,y) # del_list a ps)"
|
|
31 |
|
|
32 |
|
|
33 |
subsection \<open>Lemmas for @{const map_of}\<close>
|
|
34 |
|
|
35 |
lemma map_of_ins_list: "map_of (upd_list a b ps) = (map_of ps)(a := Some b)"
|
|
36 |
by(induction ps) auto
|
|
37 |
|
|
38 |
lemma map_of_append: "map_of (ps @ qs) a =
|
|
39 |
(case map_of ps a of None \<Rightarrow> map_of qs a | Some b \<Rightarrow> Some b)"
|
|
40 |
by(induction ps)(auto)
|
|
41 |
|
|
42 |
lemma map_of_None: "sorted (a # map fst ps) \<Longrightarrow> map_of ps a = None"
|
|
43 |
by (induction ps) (auto simp: sorted_lems sorted_Cons_iff)
|
|
44 |
|
|
45 |
lemma map_of_None2: "sorted (map fst ps @ [a]) \<Longrightarrow> map_of ps a = None"
|
|
46 |
by (induction ps) (auto simp: sorted_lems)
|
|
47 |
|
|
48 |
lemma map_of_del_list: "sorted1 ps \<Longrightarrow>
|
|
49 |
map_of(del_list a ps) = (map_of ps)(a := None)"
|
|
50 |
by(induction ps) (auto simp: map_of_None sorted_lems fun_eq_iff)
|
|
51 |
|
|
52 |
lemma map_of_sorted_Cons: "sorted (a # map fst ps) \<Longrightarrow> x < a \<Longrightarrow>
|
|
53 |
map_of ps x = None"
|
|
54 |
by (meson less_trans map_of_None sorted_Cons_iff)
|
|
55 |
|
|
56 |
lemma map_of_sorted_snoc: "sorted (map fst ps @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
|
|
57 |
map_of ps x = None"
|
|
58 |
by (meson le_less_trans map_of_None2 not_less sorted_snoc_iff)
|
|
59 |
|
|
60 |
lemmas map_of_sorteds = map_of_sorted_Cons map_of_sorted_snoc
|
61231
|
61 |
lemmas map_of_simps = sorted_lems map_of_append map_of_sorteds
|
61203
|
62 |
|
|
63 |
|
|
64 |
subsection \<open>Lemmas for @{const upd_list}\<close>
|
|
65 |
|
|
66 |
lemma sorted_upd_list: "sorted1 ps \<Longrightarrow> sorted1 (upd_list a b ps)"
|
|
67 |
apply(induction ps)
|
|
68 |
apply simp
|
|
69 |
apply(case_tac ps)
|
|
70 |
apply auto
|
|
71 |
done
|
|
72 |
|
|
73 |
lemma upd_list_sorted1: "\<lbrakk> sorted (map fst ps @ [x]); a < x \<rbrakk> \<Longrightarrow>
|
|
74 |
upd_list a b (ps @ (x,y) # qs) = upd_list a b ps @ (x,y) # qs"
|
|
75 |
by(induction ps) (auto simp: sorted_lems)
|
|
76 |
|
|
77 |
lemma upd_list_sorted2: "\<lbrakk> sorted (map fst ps @ [x]); x \<le> a \<rbrakk> \<Longrightarrow>
|
|
78 |
upd_list a b (ps @ (x,y) # qs) = ps @ upd_list a b ((x,y)#qs)"
|
|
79 |
by(induction ps) (auto simp: sorted_lems)
|
|
80 |
|
61224
|
81 |
lemmas upd_list_simps = sorted_lems upd_list_sorted1 upd_list_sorted2
|
61203
|
82 |
|
|
83 |
(*
|
|
84 |
lemma set_ins_list[simp]: "set (ins_list x xs) = insert x (set xs)"
|
|
85 |
by(induction xs) auto
|
|
86 |
|
|
87 |
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
|
|
88 |
apply(induction xs rule: sorted.induct)
|
|
89 |
apply auto
|
|
90 |
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
|
|
91 |
|
|
92 |
lemma set_del_list_eq [simp]: "distinct xs ==> set(del_list x xs) = set xs - {x}"
|
|
93 |
apply(induct xs)
|
|
94 |
apply simp
|
|
95 |
apply simp
|
|
96 |
apply blast
|
|
97 |
done
|
|
98 |
*)
|
|
99 |
|
|
100 |
|
|
101 |
subsection \<open>Lemmas for @{const del_list}\<close>
|
|
102 |
|
|
103 |
lemma sorted_del_list: "sorted1 ps \<Longrightarrow> sorted1 (del_list x ps)"
|
|
104 |
apply(induction ps)
|
|
105 |
apply simp
|
|
106 |
apply(case_tac ps)
|
|
107 |
apply auto
|
|
108 |
by (meson order.strict_trans sorted_Cons_iff)
|
|
109 |
|
|
110 |
lemma del_list_idem: "x \<notin> set(map fst xs) \<Longrightarrow> del_list x xs = xs"
|
|
111 |
by (induct xs) auto
|
|
112 |
|
|
113 |
lemma del_list_sorted1: "sorted1 (xs @ [(x,y)]) \<Longrightarrow> x \<le> a \<Longrightarrow>
|
|
114 |
del_list a (xs @ (x,y) # ys) = xs @ del_list a ((x,y) # ys)"
|
|
115 |
by (induction xs) (auto simp: sorted_mid_iff2)
|
|
116 |
|
|
117 |
lemma del_list_sorted2: "sorted1 (xs @ (x,y) # ys) \<Longrightarrow> a < x \<Longrightarrow>
|
|
118 |
del_list a (xs @ (x,y) # ys) = del_list a xs @ (x,y) # ys"
|
|
119 |
by (induction xs) (fastforce simp: sorted_Cons_iff intro!: del_list_idem)+
|
|
120 |
|
|
121 |
lemma del_list_sorted3:
|
|
122 |
"sorted1 (xs @ (x,x') # ys @ (y,y') # zs) \<Longrightarrow> a < y \<Longrightarrow>
|
|
123 |
del_list a (xs @ (x,x') # ys @ (y,y') # zs) = del_list a (xs @ (x,x') # ys) @ (y,y') # zs"
|
|
124 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted2 ball_Un)
|
|
125 |
|
|
126 |
lemma del_list_sorted4:
|
|
127 |
"sorted1 (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) \<Longrightarrow> a < z \<Longrightarrow>
|
|
128 |
del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) = del_list a (xs @ (x,x') # ys @ (y,y') # zs) @ (z,z') # us"
|
|
129 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted3)
|
|
130 |
|
|
131 |
lemma del_list_sorted5:
|
|
132 |
"sorted1 (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us @ (u,u') # vs) \<Longrightarrow> a < u \<Longrightarrow>
|
|
133 |
del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us @ (u,u') # vs) =
|
|
134 |
del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) @ (u,u') # vs"
|
|
135 |
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted4)
|
|
136 |
|
|
137 |
lemmas del_list_sorted =
|
|
138 |
del_list_sorted1 del_list_sorted2 del_list_sorted3 del_list_sorted4 del_list_sorted5
|
|
139 |
|
61231
|
140 |
lemmas del_list_simps = sorted_lems del_list_sorted
|
|
141 |
|
61203
|
142 |
end
|