| author | Andreas Lochbihler | 
| Tue, 01 Dec 2015 12:35:11 +0100 | |
| changeset 61766 | 507b39df1a57 | 
| parent 61069 | aefe89038dd2 | 
| child 61799 | 4cf66f21b764 | 
| permissions | -rw-r--r-- | 
| 3981 | 1 | (* Title: HOL/Map.thy | 
| 2 | Author: Tobias Nipkow, based on a theory by David von Oheimb | |
| 13908 | 3 | Copyright 1997-2003 TU Muenchen | 
| 3981 | 4 | |
| 60838 | 5 | The datatype of "maps"; strongly resembles maps in VDM. | 
| 3981 | 6 | *) | 
| 7 | ||
| 60758 | 8 | section \<open>Maps\<close> | 
| 13914 | 9 | |
| 15131 | 10 | theory Map | 
| 15140 | 11 | imports List | 
| 15131 | 12 | begin | 
| 3981 | 13 | |
| 61069 | 14 | type_synonym ('a, 'b) "map" = "'a \<Rightarrow> 'b option" (infixr "\<rightharpoonup>" 0)
 | 
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 15 | |
| 19378 | 16 | abbreviation | 
| 60838 | 17 | empty :: "'a \<rightharpoonup> 'b" where | 
| 60839 | 18 | "empty \<equiv> \<lambda>x. None" | 
| 19378 | 19 | |
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 20 | definition | 
| 61069 | 21 |   map_comp :: "('b \<rightharpoonup> 'c) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c)"  (infixl "\<circ>\<^sub>m" 55) where
 | 
| 22 | "f \<circ>\<^sub>m g = (\<lambda>k. case g k of None \<Rightarrow> None | Some v \<Rightarrow> f v)" | |
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 23 | |
| 20800 | 24 | definition | 
| 60839 | 25 |   map_add :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl "++" 100) where
 | 
| 26 | "m1 ++ m2 = (\<lambda>x. case m2 x of None \<Rightarrow> m1 x | Some y \<Rightarrow> Some y)" | |
| 20800 | 27 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 28 | definition | 
| 60839 | 29 |   restrict_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl "|`"  110) where
 | 
| 30 | "m|`A = (\<lambda>x. if x \<in> A then m x else None)" | |
| 13910 | 31 | |
| 21210 | 32 | notation (latex output) | 
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 33 |   restrict_map  ("_\<restriction>\<^bsub>_\<^esub>" [111,110] 110)
 | 
| 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 34 | |
| 20800 | 35 | definition | 
| 60839 | 36 |   dom :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set" where
 | 
| 37 |   "dom m = {a. m a \<noteq> None}"
 | |
| 20800 | 38 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 39 | definition | 
| 60839 | 40 |   ran :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'b set" where
 | 
| 41 |   "ran m = {b. \<exists>a. m a = Some b}"
 | |
| 20800 | 42 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 43 | definition | 
| 60839 | 44 |   map_le :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> bool"  (infix "\<subseteq>\<^sub>m" 50) where
 | 
| 45 | "(m\<^sub>1 \<subseteq>\<^sub>m m\<^sub>2) \<longleftrightarrow> (\<forall>a \<in> dom m\<^sub>1. m\<^sub>1 a = m\<^sub>2 a)" | |
| 20800 | 46 | |
| 41229 
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
 wenzelm parents: 
39992diff
changeset | 47 | nonterminal maplets and maplet | 
| 14180 | 48 | |
| 5300 | 49 | syntax | 
| 60839 | 50 |   "_maplet"  :: "['a, 'a] \<Rightarrow> maplet"             ("_ /|->/ _")
 | 
| 51 |   "_maplets" :: "['a, 'a] \<Rightarrow> maplet"             ("_ /[|->]/ _")
 | |
| 52 |   ""         :: "maplet \<Rightarrow> maplets"             ("_")
 | |
| 53 |   "_Maplets" :: "[maplet, maplets] \<Rightarrow> maplets" ("_,/ _")
 | |
| 54 |   "_MapUpd"  :: "['a \<rightharpoonup> 'b, maplets] \<Rightarrow> 'a \<rightharpoonup> 'b" ("_/'(_')" [900,0]900)
 | |
| 55 |   "_Map"     :: "maplets \<Rightarrow> 'a \<rightharpoonup> 'b"            ("(1[_])")
 | |
| 3981 | 56 | |
| 12114 
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
 wenzelm parents: 
10137diff
changeset | 57 | syntax (xsymbols) | 
| 60839 | 58 |   "_maplet"  :: "['a, 'a] \<Rightarrow> maplet"             ("_ /\<mapsto>/ _")
 | 
| 59 |   "_maplets" :: "['a, 'a] \<Rightarrow> maplet"             ("_ /[\<mapsto>]/ _")
 | |
| 14180 | 60 | |
| 5300 | 61 | translations | 
| 60839 | 62 | "_MapUpd m (_Maplets xy ms)" \<rightleftharpoons> "_MapUpd (_MapUpd m xy) ms" | 
| 63 | "_MapUpd m (_maplet x y)" \<rightleftharpoons> "m(x := CONST Some y)" | |
| 64 | "_Map ms" \<rightleftharpoons> "_MapUpd (CONST empty) ms" | |
| 65 | "_Map (_Maplets ms1 ms2)" \<leftharpoondown> "_MapUpd (_Map ms1) ms2" | |
| 66 | "_Maplets ms1 (_Maplets ms2 ms3)" \<leftharpoondown> "_Maplets (_Maplets ms1 ms2) ms3" | |
| 14180 | 67 | |
| 5183 | 68 | primrec | 
| 34941 | 69 |   map_of :: "('a \<times> 'b) list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | 
| 70 | "map_of [] = empty" | |
| 71 | | "map_of (p # ps) = (map_of ps)(fst p \<mapsto> snd p)" | |
| 5300 | 72 | |
| 34941 | 73 | definition | 
| 74 |   map_upds :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | |
| 75 | "map_upds m xs ys = m ++ map_of (rev (zip xs ys))" | |
| 76 | ||
| 77 | translations | |
| 60839 | 78 | "_MapUpd m (_maplets x y)" \<rightleftharpoons> "CONST map_upds m x y" | 
| 25965 | 79 | |
| 60839 | 80 | lemma map_of_Cons_code [code]: | 
| 25965 | 81 | "map_of [] k = None" | 
| 82 | "map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)" | |
| 83 | by simp_all | |
| 84 | ||
| 20800 | 85 | |
| 60758 | 86 | subsection \<open>@{term [source] empty}\<close>
 | 
| 13908 | 87 | |
| 20800 | 88 | lemma empty_upd_none [simp]: "empty(x := None) = empty" | 
| 60839 | 89 | by (rule ext) simp | 
| 13908 | 90 | |
| 91 | ||
| 60758 | 92 | subsection \<open>@{term [source] map_upd}\<close>
 | 
| 13908 | 93 | |
| 60839 | 94 | lemma map_upd_triv: "t k = Some x \<Longrightarrow> t(k\<mapsto>x) = t" | 
| 95 | by (rule ext) simp | |
| 13908 | 96 | |
| 60839 | 97 | lemma map_upd_nonempty [simp]: "t(k\<mapsto>x) \<noteq> empty" | 
| 20800 | 98 | proof | 
| 99 | assume "t(k \<mapsto> x) = empty" | |
| 100 | then have "(t(k \<mapsto> x)) k = None" by simp | |
| 101 | then show False by simp | |
| 102 | qed | |
| 13908 | 103 | |
| 20800 | 104 | lemma map_upd_eqD1: | 
| 105 | assumes "m(a\<mapsto>x) = n(a\<mapsto>y)" | |
| 106 | shows "x = y" | |
| 107 | proof - | |
| 41550 | 108 | from assms have "(m(a\<mapsto>x)) a = (n(a\<mapsto>y)) a" by simp | 
| 20800 | 109 | then show ?thesis by simp | 
| 110 | qed | |
| 14100 | 111 | |
| 20800 | 112 | lemma map_upd_Some_unfold: | 
| 60838 | 113 | "((m(a\<mapsto>b)) x = Some y) = (x = a \<and> b = y \<or> x \<noteq> a \<and> m x = Some y)" | 
| 24331 | 114 | by auto | 
| 14100 | 115 | |
| 20800 | 116 | lemma image_map_upd [simp]: "x \<notin> A \<Longrightarrow> m(x \<mapsto> y) ` A = m ` A" | 
| 24331 | 117 | by auto | 
| 15303 | 118 | |
| 60839 | 119 | lemma finite_range_updI: "finite (range f) \<Longrightarrow> finite (range (f(a\<mapsto>b)))" | 
| 24331 | 120 | unfolding image_def | 
| 121 | apply (simp (no_asm_use) add:full_SetCompr_eq) | |
| 122 | apply (rule finite_subset) | |
| 123 | prefer 2 apply assumption | |
| 124 | apply (auto) | |
| 125 | done | |
| 13908 | 126 | |
| 127 | ||
| 60758 | 128 | subsection \<open>@{term [source] map_of}\<close>
 | 
| 13908 | 129 | |
| 15304 | 130 | lemma map_of_eq_None_iff: | 
| 24331 | 131 | "(map_of xys x = None) = (x \<notin> fst ` (set xys))" | 
| 132 | by (induct xys) simp_all | |
| 15304 | 133 | |
| 20800 | 134 | lemma map_of_eq_Some_iff [simp]: | 
| 24331 | 135 | "distinct(map fst xys) \<Longrightarrow> (map_of xys x = Some y) = ((x,y) \<in> set xys)" | 
| 136 | apply (induct xys) | |
| 137 | apply simp | |
| 138 | apply (auto simp: map_of_eq_None_iff [symmetric]) | |
| 139 | done | |
| 15304 | 140 | |
| 20800 | 141 | lemma Some_eq_map_of_iff [simp]: | 
| 24331 | 142 | "distinct(map fst xys) \<Longrightarrow> (Some y = map_of xys x) = ((x,y) \<in> set xys)" | 
| 60839 | 143 | by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric]) | 
| 15304 | 144 | |
| 17724 | 145 | lemma map_of_is_SomeI [simp]: "\<lbrakk> distinct(map fst xys); (x,y) \<in> set xys \<rbrakk> | 
| 20800 | 146 | \<Longrightarrow> map_of xys x = Some y" | 
| 24331 | 147 | apply (induct xys) | 
| 148 | apply simp | |
| 149 | apply force | |
| 150 | done | |
| 15304 | 151 | |
| 20800 | 152 | lemma map_of_zip_is_None [simp]: | 
| 24331 | 153 | "length xs = length ys \<Longrightarrow> (map_of (zip xs ys) x = None) = (x \<notin> set xs)" | 
| 154 | by (induct rule: list_induct2) simp_all | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 155 | |
| 26443 | 156 | lemma map_of_zip_is_Some: | 
| 157 | assumes "length xs = length ys" | |
| 158 | shows "x \<in> set xs \<longleftrightarrow> (\<exists>y. map_of (zip xs ys) x = Some y)" | |
| 159 | using assms by (induct rule: list_induct2) simp_all | |
| 160 | ||
| 161 | lemma map_of_zip_upd: | |
| 162 | fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list" | |
| 163 | assumes "length ys = length xs" | |
| 164 | and "length zs = length xs" | |
| 165 | and "x \<notin> set xs" | |
| 166 | and "map_of (zip xs ys)(x \<mapsto> y) = map_of (zip xs zs)(x \<mapsto> z)" | |
| 167 | shows "map_of (zip xs ys) = map_of (zip xs zs)" | |
| 168 | proof | |
| 169 | fix x' :: 'a | |
| 170 | show "map_of (zip xs ys) x' = map_of (zip xs zs) x'" | |
| 171 | proof (cases "x = x'") | |
| 172 | case True | |
| 173 | from assms True map_of_zip_is_None [of xs ys x'] | |
| 174 | have "map_of (zip xs ys) x' = None" by simp | |
| 175 | moreover from assms True map_of_zip_is_None [of xs zs x'] | |
| 176 | have "map_of (zip xs zs) x' = None" by simp | |
| 177 | ultimately show ?thesis by simp | |
| 178 | next | |
| 179 | case False from assms | |
| 180 | have "(map_of (zip xs ys)(x \<mapsto> y)) x' = (map_of (zip xs zs)(x \<mapsto> z)) x'" by auto | |
| 181 | with False show ?thesis by simp | |
| 182 | qed | |
| 183 | qed | |
| 184 | ||
| 185 | lemma map_of_zip_inject: | |
| 186 | assumes "length ys = length xs" | |
| 187 | and "length zs = length xs" | |
| 188 | and dist: "distinct xs" | |
| 189 | and map_of: "map_of (zip xs ys) = map_of (zip xs zs)" | |
| 190 | shows "ys = zs" | |
| 60839 | 191 | using assms(1) assms(2)[symmetric] | 
| 192 | using dist map_of | |
| 193 | proof (induct ys xs zs rule: list_induct3) | |
| 26443 | 194 | case Nil show ?case by simp | 
| 195 | next | |
| 196 | case (Cons y ys x xs z zs) | |
| 60758 | 197 | from \<open>map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\<close> | 
| 26443 | 198 | have map_of: "map_of (zip xs ys)(x \<mapsto> y) = map_of (zip xs zs)(x \<mapsto> z)" by simp | 
| 199 | from Cons have "length ys = length xs" and "length zs = length xs" | |
| 200 | and "x \<notin> set xs" by simp_all | |
| 201 | then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd) | |
| 60758 | 202 | with Cons.hyps \<open>distinct (x # xs)\<close> have "ys = zs" by simp | 
| 26443 | 203 | moreover from map_of have "y = z" by (rule map_upd_eqD1) | 
| 204 | ultimately show ?case by simp | |
| 205 | qed | |
| 206 | ||
| 33635 | 207 | lemma map_of_zip_map: | 
| 208 | "map_of (zip xs (map f xs)) = (\<lambda>x. if x \<in> set xs then Some (f x) else None)" | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 209 | by (induct xs) (simp_all add: fun_eq_iff) | 
| 33635 | 210 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 211 | lemma finite_range_map_of: "finite (range (map_of xys))" | 
| 24331 | 212 | apply (induct xys) | 
| 213 | apply (simp_all add: image_constant) | |
| 214 | apply (rule finite_subset) | |
| 215 | prefer 2 apply assumption | |
| 216 | apply auto | |
| 217 | done | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 218 | |
| 20800 | 219 | lemma map_of_SomeD: "map_of xs k = Some y \<Longrightarrow> (k, y) \<in> set xs" | 
| 60841 | 220 | by (induct xs) (auto split: if_splits) | 
| 13908 | 221 | |
| 20800 | 222 | lemma map_of_mapk_SomeI: | 
| 60839 | 223 | "inj f \<Longrightarrow> map_of t k = Some x \<Longrightarrow> | 
| 61032 
b57df8eecad6
standardized some occurences of ancient "split" alias
 haftmann parents: 
60841diff
changeset | 224 | map_of (map (case_prod (\<lambda>k. Pair (f k))) t) (f k) = Some x" | 
| 60839 | 225 | by (induct t) (auto simp: inj_eq) | 
| 13908 | 226 | |
| 60839 | 227 | lemma weak_map_of_SomeI: "(k, x) \<in> set l \<Longrightarrow> \<exists>x. map_of l k = Some x" | 
| 24331 | 228 | by (induct l) auto | 
| 13908 | 229 | |
| 20800 | 230 | lemma map_of_filter_in: | 
| 61032 
b57df8eecad6
standardized some occurences of ancient "split" alias
 haftmann parents: 
60841diff
changeset | 231 | "map_of xs k = Some z \<Longrightarrow> P k z \<Longrightarrow> map_of (filter (case_prod P) xs) k = Some z" | 
| 24331 | 232 | by (induct xs) auto | 
| 13908 | 233 | |
| 35607 | 234 | lemma map_of_map: | 
| 55466 | 235 | "map_of (map (\<lambda>(k, v). (k, f v)) xs) = map_option f \<circ> map_of xs" | 
| 60839 | 236 | by (induct xs) (auto simp: fun_eq_iff) | 
| 35607 | 237 | |
| 55466 | 238 | lemma dom_map_option: | 
| 239 | "dom (\<lambda>k. map_option (f k) (m k)) = dom m" | |
| 35607 | 240 | by (simp add: dom_def) | 
| 13908 | 241 | |
| 56545 | 242 | lemma dom_map_option_comp [simp]: | 
| 243 | "dom (map_option g \<circ> m) = dom m" | |
| 244 | using dom_map_option [of "\<lambda>_. g" m] by (simp add: comp_def) | |
| 245 | ||
| 13908 | 246 | |
| 60758 | 247 | subsection \<open>@{const map_option} related\<close>
 | 
| 13908 | 248 | |
| 55466 | 249 | lemma map_option_o_empty [simp]: "map_option f o empty = empty" | 
| 24331 | 250 | by (rule ext) simp | 
| 13908 | 251 | |
| 55466 | 252 | lemma map_option_o_map_upd [simp]: | 
| 60838 | 253 | "map_option f o m(a\<mapsto>b) = (map_option f o m)(a\<mapsto>f b)" | 
| 24331 | 254 | by (rule ext) simp | 
| 20800 | 255 | |
| 13908 | 256 | |
| 60758 | 257 | subsection \<open>@{term [source] map_comp} related\<close>
 | 
| 17391 | 258 | |
| 20800 | 259 | lemma map_comp_empty [simp]: | 
| 24331 | 260 | "m \<circ>\<^sub>m empty = empty" | 
| 261 | "empty \<circ>\<^sub>m m = empty" | |
| 60839 | 262 | by (auto simp: map_comp_def split: option.splits) | 
| 17391 | 263 | |
| 20800 | 264 | lemma map_comp_simps [simp]: | 
| 24331 | 265 | "m2 k = None \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = None" | 
| 266 | "m2 k = Some k' \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = m1 k'" | |
| 60839 | 267 | by (auto simp: map_comp_def) | 
| 17391 | 268 | |
| 269 | lemma map_comp_Some_iff: | |
| 24331 | 270 | "((m1 \<circ>\<^sub>m m2) k = Some v) = (\<exists>k'. m2 k = Some k' \<and> m1 k' = Some v)" | 
| 60839 | 271 | by (auto simp: map_comp_def split: option.splits) | 
| 17391 | 272 | |
| 273 | lemma map_comp_None_iff: | |
| 24331 | 274 | "((m1 \<circ>\<^sub>m m2) k = None) = (m2 k = None \<or> (\<exists>k'. m2 k = Some k' \<and> m1 k' = None)) " | 
| 60839 | 275 | by (auto simp: map_comp_def split: option.splits) | 
| 13908 | 276 | |
| 20800 | 277 | |
| 60758 | 278 | subsection \<open>@{text "++"}\<close>
 | 
| 13908 | 279 | |
| 14025 | 280 | lemma map_add_empty[simp]: "m ++ empty = m" | 
| 24331 | 281 | by(simp add: map_add_def) | 
| 13908 | 282 | |
| 14025 | 283 | lemma empty_map_add[simp]: "empty ++ m = m" | 
| 24331 | 284 | by (rule ext) (simp add: map_add_def split: option.split) | 
| 13908 | 285 | |
| 14025 | 286 | lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3" | 
| 24331 | 287 | by (rule ext) (simp add: map_add_def split: option.split) | 
| 20800 | 288 | |
| 289 | lemma map_add_Some_iff: | |
| 24331 | 290 | "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)" | 
| 291 | by (simp add: map_add_def split: option.split) | |
| 14025 | 292 | |
| 20800 | 293 | lemma map_add_SomeD [dest!]: | 
| 24331 | 294 | "(m ++ n) k = Some x \<Longrightarrow> n k = Some x \<or> n k = None \<and> m k = Some x" | 
| 295 | by (rule map_add_Some_iff [THEN iffD1]) | |
| 13908 | 296 | |
| 60839 | 297 | lemma map_add_find_right [simp]: "n k = Some xx \<Longrightarrow> (m ++ n) k = Some xx" | 
| 24331 | 298 | by (subst map_add_Some_iff) fast | 
| 13908 | 299 | |
| 14025 | 300 | lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)" | 
| 24331 | 301 | by (simp add: map_add_def split: option.split) | 
| 13908 | 302 | |
| 60838 | 303 | lemma map_add_upd[simp]: "f ++ g(x\<mapsto>y) = (f ++ g)(x\<mapsto>y)" | 
| 24331 | 304 | by (rule ext) (simp add: map_add_def) | 
| 13908 | 305 | |
| 14186 | 306 | lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)" | 
| 24331 | 307 | by (simp add: map_upds_def) | 
| 14186 | 308 | |
| 32236 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 309 | lemma map_add_upd_left: "m\<notin>dom e2 \<Longrightarrow> e1(m \<mapsto> u1) ++ e2 = (e1 ++ e2)(m \<mapsto> u1)" | 
| 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 310 | by (rule ext) (auto simp: map_add_def dom_def split: option.split) | 
| 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 311 | |
| 20800 | 312 | lemma map_of_append[simp]: "map_of (xs @ ys) = map_of ys ++ map_of xs" | 
| 24331 | 313 | unfolding map_add_def | 
| 314 | apply (induct xs) | |
| 315 | apply simp | |
| 316 | apply (rule ext) | |
| 317 | apply (simp split add: option.split) | |
| 318 | done | |
| 13908 | 319 | |
| 14025 | 320 | lemma finite_range_map_of_map_add: | 
| 60839 | 321 | "finite (range f) \<Longrightarrow> finite (range (f ++ map_of l))" | 
| 24331 | 322 | apply (induct l) | 
| 323 | apply (auto simp del: fun_upd_apply) | |
| 324 | apply (erule finite_range_updI) | |
| 325 | done | |
| 13908 | 326 | |
| 20800 | 327 | lemma inj_on_map_add_dom [iff]: | 
| 24331 | 328 | "inj_on (m ++ m') (dom m') = inj_on m' (dom m')" | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
42163diff
changeset | 329 | by (fastforce simp: map_add_def dom_def inj_on_def split: option.splits) | 
| 20800 | 330 | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 331 | lemma map_upds_fold_map_upd: | 
| 35552 | 332 | "m(ks[\<mapsto>]vs) = foldl (\<lambda>m (k, v). m(k \<mapsto> v)) m (zip ks vs)" | 
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 333 | unfolding map_upds_def proof (rule sym, rule zip_obtain_same_length) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 334 | fix ks :: "'a list" and vs :: "'b list" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 335 | assume "length ks = length vs" | 
| 35552 | 336 | then show "foldl (\<lambda>m (k, v). m(k\<mapsto>v)) m (zip ks vs) = m ++ map_of (rev (zip ks vs))" | 
| 337 | by(induct arbitrary: m rule: list_induct2) simp_all | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 338 | qed | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 339 | |
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 340 | lemma map_add_map_of_foldr: | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 341 | "m ++ map_of ps = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m" | 
| 60839 | 342 | by (induct ps) (auto simp: fun_eq_iff map_add_def) | 
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 343 | |
| 15304 | 344 | |
| 60758 | 345 | subsection \<open>@{term [source] restrict_map}\<close>
 | 
| 14100 | 346 | |
| 20800 | 347 | lemma restrict_map_to_empty [simp]: "m|`{} = empty"
 | 
| 24331 | 348 | by (simp add: restrict_map_def) | 
| 14186 | 349 | |
| 31380 | 350 | lemma restrict_map_insert: "f |` (insert a A) = (f |` A)(a := f a)" | 
| 60839 | 351 | by (auto simp: restrict_map_def) | 
| 31380 | 352 | |
| 20800 | 353 | lemma restrict_map_empty [simp]: "empty|`D = empty" | 
| 24331 | 354 | by (simp add: restrict_map_def) | 
| 14186 | 355 | |
| 15693 | 356 | lemma restrict_in [simp]: "x \<in> A \<Longrightarrow> (m|`A) x = m x" | 
| 24331 | 357 | by (simp add: restrict_map_def) | 
| 14100 | 358 | |
| 15693 | 359 | lemma restrict_out [simp]: "x \<notin> A \<Longrightarrow> (m|`A) x = None" | 
| 24331 | 360 | by (simp add: restrict_map_def) | 
| 14100 | 361 | |
| 15693 | 362 | lemma ran_restrictD: "y \<in> ran (m|`A) \<Longrightarrow> \<exists>x\<in>A. m x = Some y" | 
| 24331 | 363 | by (auto simp: restrict_map_def ran_def split: split_if_asm) | 
| 14100 | 364 | |
| 15693 | 365 | lemma dom_restrict [simp]: "dom (m|`A) = dom m \<inter> A" | 
| 24331 | 366 | by (auto simp: restrict_map_def dom_def split: split_if_asm) | 
| 14100 | 367 | |
| 15693 | 368 | lemma restrict_upd_same [simp]: "m(x\<mapsto>y)|`(-{x}) = m|`(-{x})"
 | 
| 24331 | 369 | by (rule ext) (auto simp: restrict_map_def) | 
| 14100 | 370 | |
| 15693 | 371 | lemma restrict_restrict [simp]: "m|`A|`B = m|`(A\<inter>B)" | 
| 24331 | 372 | by (rule ext) (auto simp: restrict_map_def) | 
| 14100 | 373 | |
| 20800 | 374 | lemma restrict_fun_upd [simp]: | 
| 24331 | 375 |   "m(x := y)|`D = (if x \<in> D then (m|`(D-{x}))(x := y) else m|`D)"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 376 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 377 | |
| 20800 | 378 | lemma fun_upd_None_restrict [simp]: | 
| 60839 | 379 |   "(m|`D)(x := None) = (if x \<in> D then m|`(D - {x}) else m|`D)"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 380 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 381 | |
| 20800 | 382 | lemma fun_upd_restrict: "(m|`D)(x := y) = (m|`(D-{x}))(x := y)"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 383 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 384 | |
| 20800 | 385 | lemma fun_upd_restrict_conv [simp]: | 
| 24331 | 386 |   "x \<in> D \<Longrightarrow> (m|`D)(x := y) = (m|`(D-{x}))(x := y)"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 387 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 388 | |
| 35159 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 389 | lemma map_of_map_restrict: | 
| 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 390 | "map_of (map (\<lambda>k. (k, f k)) ks) = (Some \<circ> f) |` set ks" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 391 | by (induct ks) (simp_all add: fun_eq_iff restrict_map_insert) | 
| 35159 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 392 | |
| 35619 | 393 | lemma restrict_complement_singleton_eq: | 
| 394 |   "f |` (- {x}) = f(x := None)"
 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 395 | by (simp add: restrict_map_def fun_eq_iff) | 
| 35619 | 396 | |
| 14100 | 397 | |
| 60758 | 398 | subsection \<open>@{term [source] map_upds}\<close>
 | 
| 14025 | 399 | |
| 60838 | 400 | lemma map_upds_Nil1 [simp]: "m([] [\<mapsto>] bs) = m" | 
| 24331 | 401 | by (simp add: map_upds_def) | 
| 14025 | 402 | |
| 60838 | 403 | lemma map_upds_Nil2 [simp]: "m(as [\<mapsto>] []) = m" | 
| 24331 | 404 | by (simp add:map_upds_def) | 
| 20800 | 405 | |
| 60838 | 406 | lemma map_upds_Cons [simp]: "m(a#as [\<mapsto>] b#bs) = (m(a\<mapsto>b))(as[\<mapsto>]bs)" | 
| 24331 | 407 | by (simp add:map_upds_def) | 
| 14025 | 408 | |
| 60839 | 409 | lemma map_upds_append1 [simp]: "size xs < size ys \<Longrightarrow> | 
| 24331 | 410 | m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys)(x \<mapsto> ys!size xs)" | 
| 60839 | 411 | apply(induct xs arbitrary: ys m) | 
| 24331 | 412 | apply (clarsimp simp add: neq_Nil_conv) | 
| 413 | apply (case_tac ys) | |
| 414 | apply simp | |
| 415 | apply simp | |
| 416 | done | |
| 14187 | 417 | |
| 20800 | 418 | lemma map_upds_list_update2_drop [simp]: | 
| 46588 
4895d7f1be42
removing some unnecessary premises from Map theory
 bulwahn parents: 
44921diff
changeset | 419 | "size xs \<le> i \<Longrightarrow> m(xs[\<mapsto>]ys[i:=y]) = m(xs[\<mapsto>]ys)" | 
| 24331 | 420 | apply (induct xs arbitrary: m ys i) | 
| 421 | apply simp | |
| 422 | apply (case_tac ys) | |
| 423 | apply simp | |
| 424 | apply (simp split: nat.split) | |
| 425 | done | |
| 14025 | 426 | |
| 20800 | 427 | lemma map_upd_upds_conv_if: | 
| 60838 | 428 | "(f(x\<mapsto>y))(xs [\<mapsto>] ys) = | 
| 60839 | 429 | (if x \<in> set(take (length ys) xs) then f(xs [\<mapsto>] ys) | 
| 60838 | 430 | else (f(xs [\<mapsto>] ys))(x\<mapsto>y))" | 
| 24331 | 431 | apply (induct xs arbitrary: x y ys f) | 
| 432 | apply simp | |
| 433 | apply (case_tac ys) | |
| 434 | apply (auto split: split_if simp: fun_upd_twist) | |
| 435 | done | |
| 14025 | 436 | |
| 437 | lemma map_upds_twist [simp]: | |
| 60839 | 438 | "a \<notin> set as \<Longrightarrow> m(a\<mapsto>b)(as[\<mapsto>]bs) = m(as[\<mapsto>]bs)(a\<mapsto>b)" | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
42163diff
changeset | 439 | using set_take_subset by (fastforce simp add: map_upd_upds_conv_if) | 
| 14025 | 440 | |
| 20800 | 441 | lemma map_upds_apply_nontin [simp]: | 
| 60839 | 442 | "x \<notin> set xs \<Longrightarrow> (f(xs[\<mapsto>]ys)) x = f x" | 
| 24331 | 443 | apply (induct xs arbitrary: ys) | 
| 444 | apply simp | |
| 445 | apply (case_tac ys) | |
| 446 | apply (auto simp: map_upd_upds_conv_if) | |
| 447 | done | |
| 14025 | 448 | |
| 20800 | 449 | lemma fun_upds_append_drop [simp]: | 
| 24331 | 450 | "size xs = size ys \<Longrightarrow> m(xs@zs[\<mapsto>]ys) = m(xs[\<mapsto>]ys)" | 
| 451 | apply (induct xs arbitrary: m ys) | |
| 452 | apply simp | |
| 453 | apply (case_tac ys) | |
| 454 | apply simp_all | |
| 455 | done | |
| 14300 | 456 | |
| 20800 | 457 | lemma fun_upds_append2_drop [simp]: | 
| 24331 | 458 | "size xs = size ys \<Longrightarrow> m(xs[\<mapsto>]ys@zs) = m(xs[\<mapsto>]ys)" | 
| 459 | apply (induct xs arbitrary: m ys) | |
| 460 | apply simp | |
| 461 | apply (case_tac ys) | |
| 462 | apply simp_all | |
| 463 | done | |
| 14300 | 464 | |
| 465 | ||
| 20800 | 466 | lemma restrict_map_upds[simp]: | 
| 467 | "\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk> | |
| 468 | \<Longrightarrow> m(xs [\<mapsto>] ys)|`D = (m|`(D - set xs))(xs [\<mapsto>] ys)" | |
| 24331 | 469 | apply (induct xs arbitrary: m ys) | 
| 470 | apply simp | |
| 471 | apply (case_tac ys) | |
| 472 | apply simp | |
| 473 | apply (simp add: Diff_insert [symmetric] insert_absorb) | |
| 474 | apply (simp add: map_upd_upds_conv_if) | |
| 475 | done | |
| 14186 | 476 | |
| 477 | ||
| 60758 | 478 | subsection \<open>@{term [source] dom}\<close>
 | 
| 13908 | 479 | |
| 31080 | 480 | lemma dom_eq_empty_conv [simp]: "dom f = {} \<longleftrightarrow> f = empty"
 | 
| 44921 | 481 | by (auto simp: dom_def) | 
| 31080 | 482 | |
| 60839 | 483 | lemma domI: "m a = Some b \<Longrightarrow> a \<in> dom m" | 
| 484 | by (simp add: dom_def) | |
| 14100 | 485 | (* declare domI [intro]? *) | 
| 13908 | 486 | |
| 60839 | 487 | lemma domD: "a \<in> dom m \<Longrightarrow> \<exists>b. m a = Some b" | 
| 488 | by (cases "m a") (auto simp add: dom_def) | |
| 13908 | 489 | |
| 60839 | 490 | lemma domIff [iff, simp del]: "a \<in> dom m \<longleftrightarrow> m a \<noteq> None" | 
| 491 | by (simp add: dom_def) | |
| 13908 | 492 | |
| 20800 | 493 | lemma dom_empty [simp]: "dom empty = {}"
 | 
| 60839 | 494 | by (simp add: dom_def) | 
| 13908 | 495 | |
| 20800 | 496 | lemma dom_fun_upd [simp]: | 
| 60839 | 497 |   "dom(f(x := y)) = (if y = None then dom f - {x} else insert x (dom f))"
 | 
| 498 | by (auto simp: dom_def) | |
| 13908 | 499 | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 500 | lemma dom_if: | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 501 |   "dom (\<lambda>x. if P x then f x else g x) = dom f \<inter> {x. P x} \<union> dom g \<inter> {x. \<not> P x}"
 | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 502 | by (auto split: if_splits) | 
| 13937 | 503 | |
| 15304 | 504 | lemma dom_map_of_conv_image_fst: | 
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 505 | "dom (map_of xys) = fst ` set xys" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 506 | by (induct xys) (auto simp add: dom_if) | 
| 15304 | 507 | |
| 60839 | 508 | lemma dom_map_of_zip [simp]: "length xs = length ys \<Longrightarrow> dom (map_of (zip xs ys)) = set xs" | 
| 509 | by (induct rule: list_induct2) (auto simp: dom_if) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 510 | |
| 13908 | 511 | lemma finite_dom_map_of: "finite (dom (map_of l))" | 
| 60839 | 512 | by (induct l) (auto simp: dom_def insert_Collect [symmetric]) | 
| 13908 | 513 | |
| 20800 | 514 | lemma dom_map_upds [simp]: | 
| 60839 | 515 | "dom(m(xs[\<mapsto>]ys)) = set(take (length ys) xs) \<union> dom m" | 
| 24331 | 516 | apply (induct xs arbitrary: m ys) | 
| 517 | apply simp | |
| 518 | apply (case_tac ys) | |
| 519 | apply auto | |
| 520 | done | |
| 13910 | 521 | |
| 60839 | 522 | lemma dom_map_add [simp]: "dom (m ++ n) = dom n \<union> dom m" | 
| 523 | by (auto simp: dom_def) | |
| 13910 | 524 | |
| 20800 | 525 | lemma dom_override_on [simp]: | 
| 60839 | 526 | "dom (override_on f g A) = | 
| 527 |     (dom f  - {a. a \<in> A - dom g}) \<union> {a. a \<in> A \<inter> dom g}"
 | |
| 528 | by (auto simp: dom_def override_on_def) | |
| 13908 | 529 | |
| 60839 | 530 | lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1 ++ m2 = m2 ++ m1"
 | 
| 531 | by (rule ext) (force simp: map_add_def dom_def split: option.split) | |
| 20800 | 532 | |
| 32236 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 533 | lemma map_add_dom_app_simps: | 
| 60839 | 534 | "m \<in> dom l2 \<Longrightarrow> (l1 ++ l2) m = l2 m" | 
| 535 | "m \<notin> dom l1 \<Longrightarrow> (l1 ++ l2) m = l2 m" | |
| 536 | "m \<notin> dom l2 \<Longrightarrow> (l1 ++ l2) m = l1 m" | |
| 537 | by (auto simp add: map_add_def split: option.split_asm) | |
| 32236 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 538 | |
| 29622 | 539 | lemma dom_const [simp]: | 
| 35159 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 540 | "dom (\<lambda>x. Some (f x)) = UNIV" | 
| 29622 | 541 | by auto | 
| 542 | ||
| 22230 | 543 | (* Due to John Matthews - could be rephrased with dom *) | 
| 544 | lemma finite_map_freshness: | |
| 545 | "finite (dom (f :: 'a \<rightharpoonup> 'b)) \<Longrightarrow> \<not> finite (UNIV :: 'a set) \<Longrightarrow> | |
| 546 | \<exists>x. f x = None" | |
| 60839 | 547 | by (bestsimp dest: ex_new_if_finite) | 
| 14027 | 548 | |
| 28790 | 549 | lemma dom_minus: | 
| 550 | "f x = None \<Longrightarrow> dom f - insert x A = dom f - A" | |
| 551 | unfolding dom_def by simp | |
| 552 | ||
| 553 | lemma insert_dom: | |
| 554 | "f x = Some y \<Longrightarrow> insert x (dom f) = dom f" | |
| 555 | unfolding dom_def by auto | |
| 556 | ||
| 35607 | 557 | lemma map_of_map_keys: | 
| 558 | "set xs = dom m \<Longrightarrow> map_of (map (\<lambda>k. (k, the (m k))) xs) = m" | |
| 559 | by (rule ext) (auto simp add: map_of_map_restrict restrict_map_def) | |
| 560 | ||
| 39379 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 561 | lemma map_of_eqI: | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 562 | assumes set_eq: "set (map fst xs) = set (map fst ys)" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 563 | assumes map_eq: "\<forall>k\<in>set (map fst xs). map_of xs k = map_of ys k" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 564 | shows "map_of xs = map_of ys" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 565 | proof (rule ext) | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 566 | fix k show "map_of xs k = map_of ys k" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 567 | proof (cases "map_of xs k") | 
| 60839 | 568 | case None | 
| 569 | then have "k \<notin> set (map fst xs)" by (simp add: map_of_eq_None_iff) | |
| 39379 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 570 | with set_eq have "k \<notin> set (map fst ys)" by simp | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 571 | then have "map_of ys k = None" by (simp add: map_of_eq_None_iff) | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 572 | with None show ?thesis by simp | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 573 | next | 
| 60839 | 574 | case (Some v) | 
| 575 | then have "k \<in> set (map fst xs)" by (auto simp add: dom_map_of_conv_image_fst [symmetric]) | |
| 39379 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 576 | with map_eq show ?thesis by auto | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 577 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 578 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 579 | |
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 580 | lemma map_of_eq_dom: | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 581 | assumes "map_of xs = map_of ys" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 582 | shows "fst ` set xs = fst ` set ys" | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 583 | proof - | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 584 | from assms have "dom (map_of xs) = dom (map_of ys)" by simp | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 585 | then show ?thesis by (simp add: dom_map_of_conv_image_fst) | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 586 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 587 | |
| 53820 | 588 | lemma finite_set_of_finite_maps: | 
| 60839 | 589 | assumes "finite A" "finite B" | 
| 590 |   shows "finite {m. dom m = A \<and> ran m \<subseteq> B}" (is "finite ?S")
 | |
| 53820 | 591 | proof - | 
| 592 |   let ?S' = "{m. \<forall>x. (x \<in> A \<longrightarrow> m x \<in> Some ` B) \<and> (x \<notin> A \<longrightarrow> m x = None)}"
 | |
| 593 | have "?S = ?S'" | |
| 594 | proof | |
| 60839 | 595 | show "?S \<subseteq> ?S'" by (auto simp: dom_def ran_def image_def) | 
| 53820 | 596 | show "?S' \<subseteq> ?S" | 
| 597 | proof | |
| 598 | fix m assume "m \<in> ?S'" | |
| 599 | hence 1: "dom m = A" by force | |
| 60839 | 600 | hence 2: "ran m \<subseteq> B" using \<open>m \<in> ?S'\<close> by (auto simp: dom_def ran_def) | 
| 53820 | 601 | from 1 2 show "m \<in> ?S" by blast | 
| 602 | qed | |
| 603 | qed | |
| 604 | with assms show ?thesis by(simp add: finite_set_of_finite_funs) | |
| 605 | qed | |
| 28790 | 606 | |
| 60839 | 607 | |
| 60758 | 608 | subsection \<open>@{term [source] ran}\<close>
 | 
| 14100 | 609 | |
| 60839 | 610 | lemma ranI: "m a = Some b \<Longrightarrow> b \<in> ran m" | 
| 611 | by (auto simp: ran_def) | |
| 14100 | 612 | (* declare ranI [intro]? *) | 
| 13908 | 613 | |
| 20800 | 614 | lemma ran_empty [simp]: "ran empty = {}"
 | 
| 60839 | 615 | by (auto simp: ran_def) | 
| 13908 | 616 | |
| 60839 | 617 | lemma ran_map_upd [simp]: "m a = None \<Longrightarrow> ran(m(a\<mapsto>b)) = insert b (ran m)" | 
| 618 | unfolding ran_def | |
| 24331 | 619 | apply auto | 
| 60839 | 620 | apply (subgoal_tac "aa \<noteq> a") | 
| 24331 | 621 | apply auto | 
| 622 | done | |
| 20800 | 623 | |
| 60839 | 624 | lemma ran_distinct: | 
| 625 | assumes dist: "distinct (map fst al)" | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 626 | shows "ran (map_of al) = snd ` set al" | 
| 60839 | 627 | using assms | 
| 628 | proof (induct al) | |
| 629 | case Nil | |
| 630 | then show ?case by simp | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 631 | next | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 632 | case (Cons kv al) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 633 | then have "ran (map_of al) = snd ` set al" by simp | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 634 | moreover from Cons.prems have "map_of al (fst kv) = None" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 635 | by (simp add: map_of_eq_None_iff) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 636 | ultimately show ?case by (simp only: map_of.simps ran_map_upd) simp | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 637 | qed | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 638 | |
| 60057 | 639 | lemma ran_map_option: "ran (\<lambda>x. map_option f (m x)) = f ` ran m" | 
| 60839 | 640 | by (auto simp add: ran_def) | 
| 641 | ||
| 13910 | 642 | |
| 60758 | 643 | subsection \<open>@{text "map_le"}\<close>
 | 
| 13910 | 644 | |
| 13912 | 645 | lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g" | 
| 60839 | 646 | by (simp add: map_le_def) | 
| 13910 | 647 | |
| 17724 | 648 | lemma upd_None_map_le [simp]: "f(x := None) \<subseteq>\<^sub>m f" | 
| 60839 | 649 | by (force simp add: map_le_def) | 
| 14187 | 650 | |
| 13910 | 651 | lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)" | 
| 60839 | 652 | by (fastforce simp add: map_le_def) | 
| 13910 | 653 | |
| 17724 | 654 | lemma map_le_imp_upd_le [simp]: "m1 \<subseteq>\<^sub>m m2 \<Longrightarrow> m1(x := None) \<subseteq>\<^sub>m m2(x \<mapsto> y)" | 
| 60839 | 655 | by (force simp add: map_le_def) | 
| 14187 | 656 | |
| 20800 | 657 | lemma map_le_upds [simp]: | 
| 60839 | 658 | "f \<subseteq>\<^sub>m g \<Longrightarrow> f(as [\<mapsto>] bs) \<subseteq>\<^sub>m g(as [\<mapsto>] bs)" | 
| 24331 | 659 | apply (induct as arbitrary: f g bs) | 
| 660 | apply simp | |
| 661 | apply (case_tac bs) | |
| 662 | apply auto | |
| 663 | done | |
| 13908 | 664 | |
| 14033 | 665 | lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)" | 
| 60839 | 666 | by (fastforce simp add: map_le_def dom_def) | 
| 14033 | 667 | |
| 668 | lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f" | |
| 60839 | 669 | by (simp add: map_le_def) | 
| 14033 | 670 | |
| 14187 | 671 | lemma map_le_trans[trans]: "\<lbrakk> m1 \<subseteq>\<^sub>m m2; m2 \<subseteq>\<^sub>m m3\<rbrakk> \<Longrightarrow> m1 \<subseteq>\<^sub>m m3" | 
| 60839 | 672 | by (auto simp add: map_le_def dom_def) | 
| 14033 | 673 | |
| 674 | lemma map_le_antisym: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m f \<rbrakk> \<Longrightarrow> f = g" | |
| 24331 | 675 | unfolding map_le_def | 
| 676 | apply (rule ext) | |
| 677 | apply (case_tac "x \<in> dom f", simp) | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
42163diff
changeset | 678 | apply (case_tac "x \<in> dom g", simp, fastforce) | 
| 24331 | 679 | done | 
| 14033 | 680 | |
| 60839 | 681 | lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m g ++ f" | 
| 682 | by (fastforce simp: map_le_def) | |
| 14033 | 683 | |
| 60839 | 684 | lemma map_le_iff_map_add_commute: "f \<subseteq>\<^sub>m f ++ g \<longleftrightarrow> f ++ g = g ++ f" | 
| 685 | by (fastforce simp: map_add_def map_le_def fun_eq_iff split: option.splits) | |
| 15304 | 686 | |
| 60839 | 687 | lemma map_add_le_mapE: "f ++ g \<subseteq>\<^sub>m h \<Longrightarrow> g \<subseteq>\<^sub>m h" | 
| 688 | by (fastforce simp: map_le_def map_add_def dom_def) | |
| 15303 | 689 | |
| 60839 | 690 | lemma map_add_le_mapI: "\<lbrakk> f \<subseteq>\<^sub>m h; g \<subseteq>\<^sub>m h \<rbrakk> \<Longrightarrow> f ++ g \<subseteq>\<^sub>m h" | 
| 691 | by (auto simp: map_le_def map_add_def dom_def split: option.splits) | |
| 15303 | 692 | |
| 31080 | 693 | lemma dom_eq_singleton_conv: "dom f = {x} \<longleftrightarrow> (\<exists>v. f = [x \<mapsto> v])"
 | 
| 694 | proof(rule iffI) | |
| 695 | assume "\<exists>v. f = [x \<mapsto> v]" | |
| 696 |   thus "dom f = {x}" by(auto split: split_if_asm)
 | |
| 697 | next | |
| 698 |   assume "dom f = {x}"
 | |
| 699 | then obtain v where "f x = Some v" by auto | |
| 700 | hence "[x \<mapsto> v] \<subseteq>\<^sub>m f" by(auto simp add: map_le_def) | |
| 60758 | 701 |   moreover have "f \<subseteq>\<^sub>m [x \<mapsto> v]" using \<open>dom f = {x}\<close> \<open>f x = Some v\<close>
 | 
| 31080 | 702 | by(auto simp add: map_le_def) | 
| 703 | ultimately have "f = [x \<mapsto> v]" by-(rule map_le_antisym) | |
| 704 | thus "\<exists>v. f = [x \<mapsto> v]" by blast | |
| 705 | qed | |
| 706 | ||
| 35565 | 707 | |
| 60758 | 708 | subsection \<open>Various\<close> | 
| 35565 | 709 | |
| 710 | lemma set_map_of_compr: | |
| 711 | assumes distinct: "distinct (map fst xs)" | |
| 712 |   shows "set xs = {(k, v). map_of xs k = Some v}"
 | |
| 60839 | 713 | using assms | 
| 714 | proof (induct xs) | |
| 715 | case Nil | |
| 716 | then show ?case by simp | |
| 35565 | 717 | next | 
| 718 | case (Cons x xs) | |
| 719 | obtain k v where "x = (k, v)" by (cases x) blast | |
| 720 | with Cons.prems have "k \<notin> dom (map_of xs)" | |
| 721 | by (simp add: dom_map_of_conv_image_fst) | |
| 722 |   then have *: "insert (k, v) {(k, v). map_of xs k = Some v} =
 | |
| 723 |     {(k', v'). (map_of xs(k \<mapsto> v)) k' = Some v'}"
 | |
| 724 | by (auto split: if_splits) | |
| 725 |   from Cons have "set xs = {(k, v). map_of xs k = Some v}" by simp
 | |
| 60758 | 726 | with * \<open>x = (k, v)\<close> show ?case by simp | 
| 35565 | 727 | qed | 
| 728 | ||
| 729 | lemma map_of_inject_set: | |
| 730 | assumes distinct: "distinct (map fst xs)" "distinct (map fst ys)" | |
| 731 | shows "map_of xs = map_of ys \<longleftrightarrow> set xs = set ys" (is "?lhs \<longleftrightarrow> ?rhs") | |
| 732 | proof | |
| 733 | assume ?lhs | |
| 60758 | 734 |   moreover from \<open>distinct (map fst xs)\<close> have "set xs = {(k, v). map_of xs k = Some v}"
 | 
| 35565 | 735 | by (rule set_map_of_compr) | 
| 60758 | 736 |   moreover from \<open>distinct (map fst ys)\<close> have "set ys = {(k, v). map_of ys k = Some v}"
 | 
| 35565 | 737 | by (rule set_map_of_compr) | 
| 738 | ultimately show ?rhs by simp | |
| 739 | next | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 740 | assume ?rhs show ?lhs | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 741 | proof | 
| 35565 | 742 | fix k | 
| 60839 | 743 | show "map_of xs k = map_of ys k" | 
| 744 | proof (cases "map_of xs k") | |
| 35565 | 745 | case None | 
| 60758 | 746 | with \<open>?rhs\<close> have "map_of ys k = None" | 
| 35565 | 747 | by (simp add: map_of_eq_None_iff) | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 748 | with None show ?thesis by simp | 
| 35565 | 749 | next | 
| 750 | case (Some v) | |
| 60758 | 751 | with distinct \<open>?rhs\<close> have "map_of ys k = Some v" | 
| 35565 | 752 | by simp | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 753 | with Some show ?thesis by simp | 
| 35565 | 754 | qed | 
| 755 | qed | |
| 756 | qed | |
| 757 | ||
| 3981 | 758 | end |