| author | wenzelm | 
| Thu, 26 Oct 2023 22:10:22 +0200 | |
| changeset 78851 | db37cae970a6 | 
| parent 77671 | 8a6a79ed5a83 | 
| child 80760 | be8c0e039a5e | 
| 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 | 
| 67780 
7655e6369c9f
more abbrevs -- this makes "(=" ambiguous and thus simplifies input of "(=)" (within the context of Main HOL);
 wenzelm parents: 
67091diff
changeset | 11 | imports List | 
| 
7655e6369c9f
more abbrevs -- this makes "(=" ambiguous and thus simplifies input of "(=)" (within the context of Main HOL);
 wenzelm parents: 
67091diff
changeset | 12 | abbrevs "(=" = "\<subseteq>\<^sub>m" | 
| 15131 | 13 | begin | 
| 3981 | 14 | |
| 61069 | 15 | type_synonym ('a, 'b) "map" = "'a \<Rightarrow> 'b option" (infixr "\<rightharpoonup>" 0)
 | 
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 16 | |
| 77356 
1f5428d66591
Map.empty no longer output abbreviation; %_. None is shorter and requires no explanation
 nipkow parents: 
77307diff
changeset | 17 | abbreviation (input) | 
| 60838 | 18 | empty :: "'a \<rightharpoonup> 'b" where | 
| 60839 | 19 | "empty \<equiv> \<lambda>x. None" | 
| 19378 | 20 | |
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 21 | definition | 
| 61069 | 22 |   map_comp :: "('b \<rightharpoonup> 'c) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c)"  (infixl "\<circ>\<^sub>m" 55) where
 | 
| 23 | "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 | 24 | |
| 20800 | 25 | definition | 
| 60839 | 26 |   map_add :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl "++" 100) where
 | 
| 27 | "m1 ++ m2 = (\<lambda>x. case m2 x of None \<Rightarrow> m1 x | Some y \<Rightarrow> Some y)" | |
| 20800 | 28 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 29 | definition | 
| 60839 | 30 |   restrict_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set \<Rightarrow> ('a \<rightharpoonup> 'b)"  (infixl "|`"  110) where
 | 
| 31 | "m|`A = (\<lambda>x. if x \<in> A then m x else None)" | |
| 13910 | 32 | |
| 21210 | 33 | notation (latex output) | 
| 19656 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 34 |   restrict_map  ("_\<restriction>\<^bsub>_\<^esub>" [111,110] 110)
 | 
| 
09be06943252
tuned concrete syntax -- abbreviation/const_syntax;
 wenzelm parents: 
19378diff
changeset | 35 | |
| 20800 | 36 | definition | 
| 60839 | 37 |   dom :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a set" where
 | 
| 38 |   "dom m = {a. m a \<noteq> None}"
 | |
| 20800 | 39 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 40 | definition | 
| 60839 | 41 |   ran :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'b set" where
 | 
| 42 |   "ran m = {b. \<exists>a. m a = Some b}"
 | |
| 20800 | 43 | |
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21210diff
changeset | 44 | definition | 
| 73832 | 45 |   graph :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<times> 'b) set" where
 | 
| 46 |   "graph m = {(a, b) | a b. m a = Some b}"
 | |
| 47 | ||
| 48 | definition | |
| 60839 | 49 |   map_le :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> bool"  (infix "\<subseteq>\<^sub>m" 50) where
 | 
| 50 | "(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 | 51 | |
| 77671 | 52 | text \<open>Function update syntax \<open>f(x := y, \<dots>)\<close> is extended with \<open>x \<mapsto> y\<close>, which is short for | 
| 53 | \<open>x := Some y\<close>. \<open>:=\<close> and \<open>\<mapsto>\<close> can be mixed freely. | |
| 54 | The syntax \<open>[x \<mapsto> y, \<dots>]\<close> is short for \<open>Map.empty(x \<mapsto> y, \<dots>)\<close> | |
| 55 | but must only contain \<open>\<mapsto>\<close>, not \<open>:=\<close>, because \<open>[x:=y]\<close> clashes with the list update syntax \<open>xs[i:=x]\<close>.\<close> | |
| 56 | ||
| 57 | nonterminal maplet and maplets | |
| 14180 | 58 | |
| 5300 | 59 | syntax | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61799diff
changeset | 60 |   "_maplet"  :: "['a, 'a] \<Rightarrow> maplet"             ("_ /\<mapsto>/ _")
 | 
| 77671 | 61 |   ""         :: "maplet \<Rightarrow> updbind"              ("_")
 | 
| 60839 | 62 |   ""         :: "maplet \<Rightarrow> maplets"             ("_")
 | 
| 63 |   "_Maplets" :: "[maplet, maplets] \<Rightarrow> maplets" ("_,/ _")
 | |
| 77671 | 64 |   "_Map"     :: "maplets \<Rightarrow> 'a \<rightharpoonup> 'b"           ("(1[_])")
 | 
| 65 | (* Syntax forbids \<open>[\<dots>, x := y, \<dots>]\<close> by introducing \<open>maplets\<close> in addition to \<open>updbinds\<close> *) | |
| 3981 | 66 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61799diff
changeset | 67 | syntax (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61799diff
changeset | 68 |   "_maplet"  :: "['a, 'a] \<Rightarrow> maplet"             ("_ /|->/ _")
 | 
| 14180 | 69 | |
| 5300 | 70 | translations | 
| 77671 | 71 | "_Update f (_maplet x y)" \<rightleftharpoons> "f(x := CONST Some y)" | 
| 72 | "_Maplets m ms" \<rightharpoonup> "_updbinds m ms" | |
| 73 | "_Map ms" \<rightharpoonup> "_Update (CONST empty) ms" | |
| 14180 | 74 | |
| 77671 | 75 | (* Printing must create \<open>_Map\<close> only for \<open>_maplet\<close> *) | 
| 76 | "_Map (_maplet x y)" \<leftharpoondown> "_Update (\<lambda>u. CONST None) (_maplet x y)" | |
| 77 | "_Map (_updbinds m (_maplet x y))" \<leftharpoondown> "_Update (_Map m) (_maplet x y)" | |
| 78 | ||
| 79 | text \<open>Updating with lists:\<close> | |
| 80 | ||
| 81 | primrec map_of :: "('a \<times> 'b) list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | |
| 77307 | 82 | "map_of [] = empty" | 
| 83 | | "map_of (p # ps) = (map_of ps)(fst p \<mapsto> snd p)" | |
| 84 | ||
| 85 | lemma map_of_Cons_code [code]: | |
| 86 | "map_of [] k = None" | |
| 87 | "map_of ((l, v) # ps) k = (if l = k then Some v else map_of ps k)" | |
| 88 | by simp_all | |
| 89 | ||
| 77671 | 90 | definition map_upds :: "('a \<rightharpoonup> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'a \<rightharpoonup> 'b" where
 | 
| 91 | "map_upds m xs ys = m ++ map_of (rev (zip xs ys))" | |
| 92 | ||
| 93 | text \<open>There is also the more specialized update syntax \<open>xs [\<mapsto>] ys\<close> for lists \<open>xs\<close> and \<open>ys\<close>.\<close> | |
| 94 | ||
| 95 | syntax | |
| 96 |   "_maplets"  :: "['a, 'a] \<Rightarrow> maplet"             ("_ /[\<mapsto>]/ _")
 | |
| 97 | ||
| 98 | syntax (ASCII) | |
| 99 |   "_maplets" :: "['a, 'a] \<Rightarrow> maplet"             ("_ /[|->]/ _")
 | |
| 100 | ||
| 101 | translations | |
| 102 | "_Update m (_maplets xs ys)" \<rightleftharpoons> "CONST map_upds m xs ys" | |
| 103 | ||
| 104 | "_Map (_maplets xs ys)" \<leftharpoondown> "_Update (\<lambda>u. CONST None) (_maplets xs ys)" | |
| 105 | "_Map (_updbinds m (_maplets xs ys))" \<leftharpoondown> "_Update (_Map m) (_maplets xs ys)" | |
| 106 | ||
| 20800 | 107 | |
| 60758 | 108 | subsection \<open>@{term [source] empty}\<close>
 | 
| 13908 | 109 | |
| 20800 | 110 | lemma empty_upd_none [simp]: "empty(x := None) = empty" | 
| 60839 | 111 | by (rule ext) simp | 
| 13908 | 112 | |
| 113 | ||
| 60758 | 114 | subsection \<open>@{term [source] map_upd}\<close>
 | 
| 13908 | 115 | |
| 60839 | 116 | lemma map_upd_triv: "t k = Some x \<Longrightarrow> t(k\<mapsto>x) = t" | 
| 117 | by (rule ext) simp | |
| 13908 | 118 | |
| 60839 | 119 | lemma map_upd_nonempty [simp]: "t(k\<mapsto>x) \<noteq> empty" | 
| 20800 | 120 | proof | 
| 121 | assume "t(k \<mapsto> x) = empty" | |
| 122 | then have "(t(k \<mapsto> x)) k = None" by simp | |
| 123 | then show False by simp | |
| 124 | qed | |
| 13908 | 125 | |
| 20800 | 126 | lemma map_upd_eqD1: | 
| 127 | assumes "m(a\<mapsto>x) = n(a\<mapsto>y)" | |
| 128 | shows "x = y" | |
| 129 | proof - | |
| 41550 | 130 | from assms have "(m(a\<mapsto>x)) a = (n(a\<mapsto>y)) a" by simp | 
| 20800 | 131 | then show ?thesis by simp | 
| 132 | qed | |
| 14100 | 133 | |
| 20800 | 134 | lemma map_upd_Some_unfold: | 
| 60838 | 135 | "((m(a\<mapsto>b)) x = Some y) = (x = a \<and> b = y \<or> x \<noteq> a \<and> m x = Some y)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 136 | by auto | 
| 14100 | 137 | |
| 20800 | 138 | lemma image_map_upd [simp]: "x \<notin> A \<Longrightarrow> m(x \<mapsto> y) ` A = m ` A" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 139 | by auto | 
| 15303 | 140 | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 141 | lemma finite_range_updI: | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 142 | assumes "finite (range f)" shows "finite (range (f(a\<mapsto>b)))" | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 143 | proof - | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 144 | have "range (f(a\<mapsto>b)) \<subseteq> insert (Some b) (range f)" | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 145 | by auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 146 | then show ?thesis | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 147 | by (rule finite_subset) (use assms in auto) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 148 | qed | 
| 13908 | 149 | |
| 150 | ||
| 60758 | 151 | subsection \<open>@{term [source] map_of}\<close>
 | 
| 13908 | 152 | |
| 68454 | 153 | lemma map_of_eq_empty_iff [simp]: | 
| 154 | "map_of xys = empty \<longleftrightarrow> xys = []" | |
| 155 | proof | |
| 156 | show "map_of xys = empty \<Longrightarrow> xys = []" | |
| 157 | by (induction xys) simp_all | |
| 158 | qed simp | |
| 159 | ||
| 68460 | 160 | lemma empty_eq_map_of_iff [simp]: | 
| 161 | "empty = map_of xys \<longleftrightarrow> xys = []" | |
| 162 | by(subst eq_commute) simp | |
| 163 | ||
| 77307 | 164 | lemma map_of_eq_None_iff: | 
| 165 | "(map_of xys x = None) = (x \<notin> fst ` (set xys))" | |
| 166 | by (induct xys) simp_all | |
| 167 | ||
| 168 | lemma map_of_eq_Some_iff [simp]: | |
| 169 | "distinct(map fst xys) \<Longrightarrow> (map_of xys x = Some y) = ((x,y) \<in> set xys)" | |
| 170 | proof (induct xys) | |
| 171 | case (Cons xy xys) | |
| 172 | then show ?case | |
| 173 | by (cases xy) (auto simp flip: map_of_eq_None_iff) | |
| 174 | qed auto | |
| 175 | ||
| 176 | lemma Some_eq_map_of_iff [simp]: | |
| 177 | "distinct(map fst xys) \<Longrightarrow> (Some y = map_of xys x) = ((x,y) \<in> set xys)" | |
| 178 | by (auto simp del: map_of_eq_Some_iff simp: map_of_eq_Some_iff [symmetric]) | |
| 179 | ||
| 180 | lemma map_of_is_SomeI [simp]: | |
| 181 | "\<lbrakk>distinct(map fst xys); (x,y) \<in> set xys\<rbrakk> \<Longrightarrow> map_of xys x = Some y" | |
| 182 | by simp | |
| 183 | ||
| 184 | lemma map_of_zip_is_None [simp]: | |
| 185 | "length xs = length ys \<Longrightarrow> (map_of (zip xs ys) x = None) = (x \<notin> set xs)" | |
| 186 | by (induct rule: list_induct2) simp_all | |
| 187 | ||
| 188 | lemma map_of_zip_is_Some: | |
| 189 | assumes "length xs = length ys" | |
| 190 | shows "x \<in> set xs \<longleftrightarrow> (\<exists>y. map_of (zip xs ys) x = Some y)" | |
| 191 | using assms by (induct rule: list_induct2) simp_all | |
| 192 | ||
| 193 | lemma map_of_zip_upd: | |
| 194 | fixes x :: 'a and xs :: "'a list" and ys zs :: "'b list" | |
| 195 | assumes "length ys = length xs" | |
| 196 | and "length zs = length xs" | |
| 197 | and "x \<notin> set xs" | |
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 198 | and "(map_of (zip xs ys))(x \<mapsto> y) = (map_of (zip xs zs))(x \<mapsto> z)" | 
| 77307 | 199 | shows "map_of (zip xs ys) = map_of (zip xs zs)" | 
| 200 | proof | |
| 201 | fix x' :: 'a | |
| 202 | show "map_of (zip xs ys) x' = map_of (zip xs zs) x'" | |
| 203 | proof (cases "x = x'") | |
| 204 | case True | |
| 205 | from assms True map_of_zip_is_None [of xs ys x'] | |
| 206 | have "map_of (zip xs ys) x' = None" by simp | |
| 207 | moreover from assms True map_of_zip_is_None [of xs zs x'] | |
| 208 | have "map_of (zip xs zs) x' = None" by simp | |
| 209 | ultimately show ?thesis by simp | |
| 210 | next | |
| 211 | case False from assms | |
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 212 | have "((map_of (zip xs ys))(x \<mapsto> y)) x' = ((map_of (zip xs zs))(x \<mapsto> z)) x'" by auto | 
| 77307 | 213 | with False show ?thesis by simp | 
| 214 | qed | |
| 215 | qed | |
| 216 | ||
| 217 | lemma map_of_zip_inject: | |
| 218 | assumes "length ys = length xs" | |
| 219 | and "length zs = length xs" | |
| 220 | and dist: "distinct xs" | |
| 221 | and map_of: "map_of (zip xs ys) = map_of (zip xs zs)" | |
| 222 | shows "ys = zs" | |
| 223 | using assms(1) assms(2)[symmetric] | |
| 224 | using dist map_of | |
| 225 | proof (induct ys xs zs rule: list_induct3) | |
| 226 | case Nil show ?case by simp | |
| 227 | next | |
| 228 | case (Cons y ys x xs z zs) | |
| 229 | from \<open>map_of (zip (x#xs) (y#ys)) = map_of (zip (x#xs) (z#zs))\<close> | |
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 230 | have map_of: "(map_of (zip xs ys))(x \<mapsto> y) = (map_of (zip xs zs))(x \<mapsto> z)" by simp | 
| 77307 | 231 | from Cons have "length ys = length xs" and "length zs = length xs" | 
| 232 | and "x \<notin> set xs" by simp_all | |
| 233 | then have "map_of (zip xs ys) = map_of (zip xs zs)" using map_of by (rule map_of_zip_upd) | |
| 234 | with Cons.hyps \<open>distinct (x # xs)\<close> have "ys = zs" by simp | |
| 235 | moreover from map_of have "y = z" by (rule map_upd_eqD1) | |
| 236 | ultimately show ?case by simp | |
| 237 | qed | |
| 238 | ||
| 239 | lemma map_of_zip_nth: | |
| 240 | assumes "length xs = length ys" | |
| 241 | assumes "distinct xs" | |
| 242 | assumes "i < length ys" | |
| 243 | shows "map_of (zip xs ys) (xs ! i) = Some (ys ! i)" | |
| 244 | using assms proof (induct arbitrary: i rule: list_induct2) | |
| 245 | case Nil | |
| 246 | then show ?case by simp | |
| 247 | next | |
| 248 | case (Cons x xs y ys) | |
| 249 | then show ?case | |
| 250 | using less_Suc_eq_0_disj by auto | |
| 251 | qed | |
| 252 | ||
| 253 | lemma map_of_zip_map: | |
| 254 | "map_of (zip xs (map f xs)) = (\<lambda>x. if x \<in> set xs then Some (f x) else None)" | |
| 255 | by (induct xs) (simp_all add: fun_eq_iff) | |
| 256 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 257 | lemma finite_range_map_of: "finite (range (map_of xys))" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 258 | proof (induct xys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 259 | case (Cons a xys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 260 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 261 | using finite_range_updI by fastforce | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 262 | qed auto | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
14739diff
changeset | 263 | |
| 77307 | 264 | lemma map_of_SomeD: "map_of xs k = Some y \<Longrightarrow> (k, y) \<in> set xs" | 
| 265 | by (induct xs) (auto split: if_splits) | |
| 266 | ||
| 267 | lemma map_of_mapk_SomeI: | |
| 268 | "inj f \<Longrightarrow> map_of t k = Some x \<Longrightarrow> | |
| 269 | map_of (map (case_prod (\<lambda>k. Pair (f k))) t) (f k) = Some x" | |
| 270 | by (induct t) (auto simp: inj_eq) | |
| 271 | ||
| 272 | lemma weak_map_of_SomeI: "(k, x) \<in> set l \<Longrightarrow> \<exists>x. map_of l k = Some x" | |
| 273 | by (induct l) auto | |
| 274 | ||
| 275 | lemma map_of_filter_in: | |
| 276 | "map_of xs k = Some z \<Longrightarrow> P k z \<Longrightarrow> map_of (filter (case_prod P) xs) k = Some z" | |
| 277 | by (induct xs) auto | |
| 278 | ||
| 279 | lemma map_of_map: | |
| 280 | "map_of (map (\<lambda>(k, v). (k, f v)) xs) = map_option f \<circ> map_of xs" | |
| 281 | by (induct xs) (auto simp: fun_eq_iff) | |
| 282 | ||
| 55466 | 283 | lemma dom_map_option: | 
| 284 | "dom (\<lambda>k. map_option (f k) (m k)) = dom m" | |
| 35607 | 285 | by (simp add: dom_def) | 
| 13908 | 286 | |
| 56545 | 287 | lemma dom_map_option_comp [simp]: | 
| 288 | "dom (map_option g \<circ> m) = dom m" | |
| 289 | using dom_map_option [of "\<lambda>_. g" m] by (simp add: comp_def) | |
| 290 | ||
| 13908 | 291 | |
| 69593 | 292 | subsection \<open>\<^const>\<open>map_option\<close> related\<close> | 
| 13908 | 293 | |
| 67091 | 294 | lemma map_option_o_empty [simp]: "map_option f \<circ> empty = empty" | 
| 24331 | 295 | by (rule ext) simp | 
| 13908 | 296 | |
| 55466 | 297 | lemma map_option_o_map_upd [simp]: | 
| 67091 | 298 | "map_option f \<circ> m(a\<mapsto>b) = (map_option f \<circ> m)(a\<mapsto>f b)" | 
| 24331 | 299 | by (rule ext) simp | 
| 20800 | 300 | |
| 13908 | 301 | |
| 60758 | 302 | subsection \<open>@{term [source] map_comp} related\<close>
 | 
| 17391 | 303 | |
| 20800 | 304 | lemma map_comp_empty [simp]: | 
| 24331 | 305 | "m \<circ>\<^sub>m empty = empty" | 
| 306 | "empty \<circ>\<^sub>m m = empty" | |
| 60839 | 307 | by (auto simp: map_comp_def split: option.splits) | 
| 17391 | 308 | |
| 20800 | 309 | lemma map_comp_simps [simp]: | 
| 24331 | 310 | "m2 k = None \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = None" | 
| 311 | "m2 k = Some k' \<Longrightarrow> (m1 \<circ>\<^sub>m m2) k = m1 k'" | |
| 60839 | 312 | by (auto simp: map_comp_def) | 
| 17391 | 313 | |
| 314 | lemma map_comp_Some_iff: | |
| 24331 | 315 | "((m1 \<circ>\<^sub>m m2) k = Some v) = (\<exists>k'. m2 k = Some k' \<and> m1 k' = Some v)" | 
| 60839 | 316 | by (auto simp: map_comp_def split: option.splits) | 
| 17391 | 317 | |
| 318 | lemma map_comp_None_iff: | |
| 24331 | 319 | "((m1 \<circ>\<^sub>m m2) k = None) = (m2 k = None \<or> (\<exists>k'. m2 k = Some k' \<and> m1 k' = None)) " | 
| 60839 | 320 | by (auto simp: map_comp_def split: option.splits) | 
| 13908 | 321 | |
| 20800 | 322 | |
| 61799 | 323 | subsection \<open>\<open>++\<close>\<close> | 
| 13908 | 324 | |
| 14025 | 325 | lemma map_add_empty[simp]: "m ++ empty = m" | 
| 24331 | 326 | by(simp add: map_add_def) | 
| 13908 | 327 | |
| 14025 | 328 | lemma empty_map_add[simp]: "empty ++ m = m" | 
| 24331 | 329 | by (rule ext) (simp add: map_add_def split: option.split) | 
| 13908 | 330 | |
| 14025 | 331 | lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3" | 
| 24331 | 332 | by (rule ext) (simp add: map_add_def split: option.split) | 
| 20800 | 333 | |
| 334 | lemma map_add_Some_iff: | |
| 67091 | 335 | "((m ++ n) k = Some x) = (n k = Some x \<or> n k = None \<and> m k = Some x)" | 
| 24331 | 336 | by (simp add: map_add_def split: option.split) | 
| 14025 | 337 | |
| 20800 | 338 | lemma map_add_SomeD [dest!]: | 
| 24331 | 339 | "(m ++ n) k = Some x \<Longrightarrow> n k = Some x \<or> n k = None \<and> m k = Some x" | 
| 340 | by (rule map_add_Some_iff [THEN iffD1]) | |
| 13908 | 341 | |
| 60839 | 342 | lemma map_add_find_right [simp]: "n k = Some xx \<Longrightarrow> (m ++ n) k = Some xx" | 
| 24331 | 343 | by (subst map_add_Some_iff) fast | 
| 13908 | 344 | |
| 67091 | 345 | lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None \<and> m k = None)" | 
| 24331 | 346 | by (simp add: map_add_def split: option.split) | 
| 13908 | 347 | |
| 60838 | 348 | lemma map_add_upd[simp]: "f ++ g(x\<mapsto>y) = (f ++ g)(x\<mapsto>y)" | 
| 24331 | 349 | by (rule ext) (simp add: map_add_def) | 
| 13908 | 350 | |
| 14186 | 351 | lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)" | 
| 24331 | 352 | by (simp add: map_upds_def) | 
| 14186 | 353 | |
| 32236 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 354 | 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 | 355 | 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 | 356 | |
| 20800 | 357 | lemma map_of_append[simp]: "map_of (xs @ ys) = map_of ys ++ map_of xs" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 358 | unfolding map_add_def | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 359 | proof (induct xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 360 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 361 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 362 | by (force split: option.split) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 363 | qed auto | 
| 13908 | 364 | |
| 14025 | 365 | lemma finite_range_map_of_map_add: | 
| 60839 | 366 | "finite (range f) \<Longrightarrow> finite (range (f ++ map_of l))" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 367 | proof (induct l) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 368 | case (Cons a l) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 369 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 370 | by (metis finite_range_updI map_add_upd map_of.simps(2)) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 371 | qed auto | 
| 13908 | 372 | |
| 20800 | 373 | lemma inj_on_map_add_dom [iff]: | 
| 24331 | 374 | "inj_on (m ++ m') (dom m') = inj_on m' (dom m')" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 375 | by (fastforce simp: map_add_def dom_def inj_on_def split: option.splits) | 
| 20800 | 376 | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 377 | lemma map_upds_fold_map_upd: | 
| 35552 | 378 | "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 | 379 | unfolding map_upds_def proof (rule sym, rule zip_obtain_same_length) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 380 | fix ks :: "'a list" and vs :: "'b list" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 381 | assume "length ks = length vs" | 
| 35552 | 382 | then show "foldl (\<lambda>m (k, v). m(k\<mapsto>v)) m (zip ks vs) = m ++ map_of (rev (zip ks vs))" | 
| 383 | by(induct arbitrary: m rule: list_induct2) simp_all | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 384 | qed | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 385 | |
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 386 | lemma map_add_map_of_foldr: | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 387 | "m ++ map_of ps = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m" | 
| 60839 | 388 | by (induct ps) (auto simp: fun_eq_iff map_add_def) | 
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 389 | |
| 15304 | 390 | |
| 60758 | 391 | subsection \<open>@{term [source] restrict_map}\<close>
 | 
| 14100 | 392 | |
| 20800 | 393 | lemma restrict_map_to_empty [simp]: "m|`{} = empty"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 394 | by (simp add: restrict_map_def) | 
| 14186 | 395 | |
| 31380 | 396 | lemma restrict_map_insert: "f |` (insert a A) = (f |` A)(a := f a)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 397 | by (auto simp: restrict_map_def) | 
| 31380 | 398 | |
| 20800 | 399 | lemma restrict_map_empty [simp]: "empty|`D = empty" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 400 | by (simp add: restrict_map_def) | 
| 14186 | 401 | |
| 15693 | 402 | lemma restrict_in [simp]: "x \<in> A \<Longrightarrow> (m|`A) x = m x" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 403 | by (simp add: restrict_map_def) | 
| 14100 | 404 | |
| 15693 | 405 | lemma restrict_out [simp]: "x \<notin> A \<Longrightarrow> (m|`A) x = None" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 406 | by (simp add: restrict_map_def) | 
| 14100 | 407 | |
| 15693 | 408 | lemma ran_restrictD: "y \<in> ran (m|`A) \<Longrightarrow> \<exists>x\<in>A. m x = Some y" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 409 | by (auto simp: restrict_map_def ran_def split: if_split_asm) | 
| 14100 | 410 | |
| 15693 | 411 | lemma dom_restrict [simp]: "dom (m|`A) = dom m \<inter> A" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 412 | by (auto simp: restrict_map_def dom_def split: if_split_asm) | 
| 14100 | 413 | |
| 15693 | 414 | lemma restrict_upd_same [simp]: "m(x\<mapsto>y)|`(-{x}) = m|`(-{x})"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 415 | by (rule ext) (auto simp: restrict_map_def) | 
| 14100 | 416 | |
| 15693 | 417 | lemma restrict_restrict [simp]: "m|`A|`B = m|`(A\<inter>B)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 418 | by (rule ext) (auto simp: restrict_map_def) | 
| 14100 | 419 | |
| 20800 | 420 | lemma restrict_fun_upd [simp]: | 
| 24331 | 421 |   "m(x := y)|`D = (if x \<in> D then (m|`(D-{x}))(x := y) else m|`D)"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 422 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 423 | |
| 20800 | 424 | lemma fun_upd_None_restrict [simp]: | 
| 60839 | 425 |   "(m|`D)(x := None) = (if x \<in> D then m|`(D - {x}) else m|`D)"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 426 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 427 | |
| 20800 | 428 | lemma fun_upd_restrict: "(m|`D)(x := y) = (m|`(D-{x}))(x := y)"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 429 | by (simp add: restrict_map_def fun_eq_iff) | 
| 14186 | 430 | |
| 20800 | 431 | lemma fun_upd_restrict_conv [simp]: | 
| 24331 | 432 |   "x \<in> D \<Longrightarrow> (m|`D)(x := y) = (m|`(D-{x}))(x := y)"
 | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 433 | by (rule fun_upd_restrict) | 
| 14186 | 434 | |
| 35159 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 435 | lemma map_of_map_restrict: | 
| 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 436 | "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 | 437 | 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 | 438 | |
| 35619 | 439 | lemma restrict_complement_singleton_eq: | 
| 440 |   "f |` (- {x}) = f(x := None)"
 | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 441 | by auto | 
| 35619 | 442 | |
| 14100 | 443 | |
| 60758 | 444 | subsection \<open>@{term [source] map_upds}\<close>
 | 
| 14025 | 445 | |
| 60838 | 446 | lemma map_upds_Nil1 [simp]: "m([] [\<mapsto>] bs) = m" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 447 | by (simp add: map_upds_def) | 
| 14025 | 448 | |
| 60838 | 449 | lemma map_upds_Nil2 [simp]: "m(as [\<mapsto>] []) = m" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 450 | by (simp add:map_upds_def) | 
| 20800 | 451 | |
| 60838 | 452 | lemma map_upds_Cons [simp]: "m(a#as [\<mapsto>] b#bs) = (m(a\<mapsto>b))(as[\<mapsto>]bs)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 453 | by (simp add:map_upds_def) | 
| 14025 | 454 | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 455 | lemma map_upds_append1 [simp]: | 
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 456 | "size xs < size ys \<Longrightarrow> m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys, x \<mapsto> ys!size xs)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 457 | proof (induct xs arbitrary: ys m) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 458 | case Nil | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 459 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 460 | by (auto simp: neq_Nil_conv) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 461 | next | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 462 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 463 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 464 | by (cases ys) auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 465 | qed | 
| 14187 | 466 | |
| 20800 | 467 | lemma map_upds_list_update2_drop [simp]: | 
| 46588 
4895d7f1be42
removing some unnecessary premises from Map theory
 bulwahn parents: 
44921diff
changeset | 468 | "size xs \<le> i \<Longrightarrow> m(xs[\<mapsto>]ys[i:=y]) = m(xs[\<mapsto>]ys)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 469 | proof (induct xs arbitrary: m ys i) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 470 | case Nil | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 471 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 472 | by auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 473 | next | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 474 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 475 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 476 | by (cases ys) (use Cons in \<open>auto split: nat.split\<close>) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 477 | qed | 
| 14025 | 478 | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 479 | text \<open>Something weirdly sensitive about this proof, which needs only four lines in apply style\<close> | 
| 20800 | 480 | lemma map_upd_upds_conv_if: | 
| 60838 | 481 | "(f(x\<mapsto>y))(xs [\<mapsto>] ys) = | 
| 60839 | 482 | (if x \<in> set(take (length ys) xs) then f(xs [\<mapsto>] ys) | 
| 60838 | 483 | else (f(xs [\<mapsto>] ys))(x\<mapsto>y))" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 484 | proof (induct xs arbitrary: x y ys f) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 485 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 486 | show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 487 | proof (cases ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 488 | case (Cons z zs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 489 | then show ?thesis | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 490 | using Cons.hyps | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 491 | apply (auto split: if_split simp: fun_upd_twist) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 492 | using Cons.hyps apply fastforce+ | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 493 | done | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 494 | qed auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 495 | qed auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 496 | |
| 14025 | 497 | |
| 498 | lemma map_upds_twist [simp]: | |
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 499 | "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 | 500 | using set_take_subset by (fastforce simp add: map_upd_upds_conv_if) | 
| 14025 | 501 | |
| 20800 | 502 | lemma map_upds_apply_nontin [simp]: | 
| 60839 | 503 | "x \<notin> set xs \<Longrightarrow> (f(xs[\<mapsto>]ys)) x = f x" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 504 | proof (induct xs arbitrary: ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 505 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 506 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 507 | by (cases ys) (auto simp: map_upd_upds_conv_if) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 508 | qed auto | 
| 14025 | 509 | |
| 20800 | 510 | lemma fun_upds_append_drop [simp]: | 
| 24331 | 511 | "size xs = size ys \<Longrightarrow> m(xs@zs[\<mapsto>]ys) = m(xs[\<mapsto>]ys)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 512 | proof (induct xs arbitrary: ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 513 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 514 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 515 | by (cases ys) (auto simp: map_upd_upds_conv_if) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 516 | qed auto | 
| 14300 | 517 | |
| 20800 | 518 | lemma fun_upds_append2_drop [simp]: | 
| 24331 | 519 | "size xs = size ys \<Longrightarrow> m(xs[\<mapsto>]ys@zs) = m(xs[\<mapsto>]ys)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 520 | proof (induct xs arbitrary: ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 521 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 522 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 523 | by (cases ys) (auto simp: map_upd_upds_conv_if) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 524 | qed auto | 
| 14300 | 525 | |
| 20800 | 526 | lemma restrict_map_upds[simp]: | 
| 527 | "\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk> | |
| 528 | \<Longrightarrow> m(xs [\<mapsto>] ys)|`D = (m|`(D - set xs))(xs [\<mapsto>] ys)" | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 529 | proof (induct xs arbitrary: m ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 530 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 531 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 532 | proof (cases ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 533 | case (Cons z zs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 534 | with Cons.hyps Cons.prems show ?thesis | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 535 | apply (simp add: insert_absorb flip: Diff_insert) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 536 | apply (auto simp add: map_upd_upds_conv_if) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 537 | done | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 538 | qed auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 539 | qed auto | 
| 14186 | 540 | |
| 541 | ||
| 60758 | 542 | subsection \<open>@{term [source] dom}\<close>
 | 
| 13908 | 543 | |
| 31080 | 544 | lemma dom_eq_empty_conv [simp]: "dom f = {} \<longleftrightarrow> f = empty"
 | 
| 44921 | 545 | by (auto simp: dom_def) | 
| 31080 | 546 | |
| 60839 | 547 | lemma domI: "m a = Some b \<Longrightarrow> a \<in> dom m" | 
| 548 | by (simp add: dom_def) | |
| 14100 | 549 | (* declare domI [intro]? *) | 
| 13908 | 550 | |
| 60839 | 551 | lemma domD: "a \<in> dom m \<Longrightarrow> \<exists>b. m a = Some b" | 
| 552 | by (cases "m a") (auto simp add: dom_def) | |
| 13908 | 553 | |
| 66010 | 554 | lemma domIff [iff, simp del, code_unfold]: "a \<in> dom m \<longleftrightarrow> m a \<noteq> None" | 
| 60839 | 555 | by (simp add: dom_def) | 
| 13908 | 556 | |
| 20800 | 557 | lemma dom_empty [simp]: "dom empty = {}"
 | 
| 60839 | 558 | by (simp add: dom_def) | 
| 13908 | 559 | |
| 20800 | 560 | lemma dom_fun_upd [simp]: | 
| 60839 | 561 |   "dom(f(x := y)) = (if y = None then dom f - {x} else insert x (dom f))"
 | 
| 562 | by (auto simp: dom_def) | |
| 13908 | 563 | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 564 | lemma dom_if: | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 565 |   "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 | 566 | by (auto split: if_splits) | 
| 13937 | 567 | |
| 15304 | 568 | lemma dom_map_of_conv_image_fst: | 
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 569 | "dom (map_of xys) = fst ` set xys" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 570 | by (induct xys) (auto simp add: dom_if) | 
| 15304 | 571 | |
| 60839 | 572 | lemma dom_map_of_zip [simp]: "length xs = length ys \<Longrightarrow> dom (map_of (zip xs ys)) = set xs" | 
| 573 | 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 | 574 | |
| 13908 | 575 | lemma finite_dom_map_of: "finite (dom (map_of l))" | 
| 60839 | 576 | by (induct l) (auto simp: dom_def insert_Collect [symmetric]) | 
| 13908 | 577 | |
| 20800 | 578 | lemma dom_map_upds [simp]: | 
| 60839 | 579 | "dom(m(xs[\<mapsto>]ys)) = set(take (length ys) xs) \<union> dom m" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 580 | proof (induct xs arbitrary: ys) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 581 | case (Cons a xs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 582 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 583 | by (cases ys) (auto simp: map_upd_upds_conv_if) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 584 | qed auto | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 585 | |
| 13910 | 586 | |
| 60839 | 587 | lemma dom_map_add [simp]: "dom (m ++ n) = dom n \<union> dom m" | 
| 588 | by (auto simp: dom_def) | |
| 13910 | 589 | |
| 20800 | 590 | lemma dom_override_on [simp]: | 
| 60839 | 591 | "dom (override_on f g A) = | 
| 592 |     (dom f  - {a. a \<in> A - dom g}) \<union> {a. a \<in> A \<inter> dom g}"
 | |
| 593 | by (auto simp: dom_def override_on_def) | |
| 13908 | 594 | |
| 60839 | 595 | lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1 ++ m2 = m2 ++ m1"
 | 
| 596 | by (rule ext) (force simp: map_add_def dom_def split: option.split) | |
| 20800 | 597 | |
| 32236 
0203e1006f1b
some lemmas about maps (contributed by Peter Lammich)
 krauss parents: 
31380diff
changeset | 598 | lemma map_add_dom_app_simps: | 
| 60839 | 599 | "m \<in> dom l2 \<Longrightarrow> (l1 ++ l2) m = l2 m" | 
| 600 | "m \<notin> dom l1 \<Longrightarrow> (l1 ++ l2) m = l2 m" | |
| 601 | "m \<notin> dom l2 \<Longrightarrow> (l1 ++ l2) m = l1 m" | |
| 602 | 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 | 603 | |
| 29622 | 604 | lemma dom_const [simp]: | 
| 35159 
df38e92af926
added lemma map_of_map_restrict; generalized lemma dom_const
 haftmann parents: 
35115diff
changeset | 605 | "dom (\<lambda>x. Some (f x)) = UNIV" | 
| 29622 | 606 | by auto | 
| 607 | ||
| 22230 | 608 | (* Due to John Matthews - could be rephrased with dom *) | 
| 609 | lemma finite_map_freshness: | |
| 610 | "finite (dom (f :: 'a \<rightharpoonup> 'b)) \<Longrightarrow> \<not> finite (UNIV :: 'a set) \<Longrightarrow> | |
| 611 | \<exists>x. f x = None" | |
| 60839 | 612 | by (bestsimp dest: ex_new_if_finite) | 
| 14027 | 613 | |
| 28790 | 614 | lemma dom_minus: | 
| 615 | "f x = None \<Longrightarrow> dom f - insert x A = dom f - A" | |
| 616 | unfolding dom_def by simp | |
| 617 | ||
| 618 | lemma insert_dom: | |
| 619 | "f x = Some y \<Longrightarrow> insert x (dom f) = dom f" | |
| 620 | unfolding dom_def by auto | |
| 621 | ||
| 35607 | 622 | lemma map_of_map_keys: | 
| 623 | "set xs = dom m \<Longrightarrow> map_of (map (\<lambda>k. (k, the (m k))) xs) = m" | |
| 624 | by (rule ext) (auto simp add: map_of_map_restrict restrict_map_def) | |
| 625 | ||
| 39379 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 626 | lemma map_of_eqI: | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 627 | 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 | 628 | 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 | 629 | 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 | 630 | proof (rule ext) | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 631 | 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 | 632 | proof (cases "map_of xs k") | 
| 60839 | 633 | case None | 
| 634 | 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 | 635 | 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 | 636 | 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 | 637 | with None show ?thesis by simp | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 638 | next | 
| 60839 | 639 | case (Some v) | 
| 640 | 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 | 641 | 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 | 642 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 643 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 644 | |
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 645 | lemma map_of_eq_dom: | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 646 | 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 | 647 | 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 | 648 | proof - | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 649 | 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 | 650 | 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 | 651 | qed | 
| 
ab1b070aa412
moved lemmas map_of_eqI and map_of_eq_dom to Map.thy
 haftmann parents: 
39302diff
changeset | 652 | |
| 53820 | 653 | lemma finite_set_of_finite_maps: | 
| 60839 | 654 | assumes "finite A" "finite B" | 
| 655 |   shows "finite {m. dom m = A \<and> ran m \<subseteq> B}" (is "finite ?S")
 | |
| 53820 | 656 | proof - | 
| 657 |   let ?S' = "{m. \<forall>x. (x \<in> A \<longrightarrow> m x \<in> Some ` B) \<and> (x \<notin> A \<longrightarrow> m x = None)}"
 | |
| 658 | have "?S = ?S'" | |
| 659 | proof | |
| 60839 | 660 | show "?S \<subseteq> ?S'" by (auto simp: dom_def ran_def image_def) | 
| 53820 | 661 | show "?S' \<subseteq> ?S" | 
| 662 | proof | |
| 663 | fix m assume "m \<in> ?S'" | |
| 664 | hence 1: "dom m = A" by force | |
| 60839 | 665 | hence 2: "ran m \<subseteq> B" using \<open>m \<in> ?S'\<close> by (auto simp: dom_def ran_def) | 
| 53820 | 666 | from 1 2 show "m \<in> ?S" by blast | 
| 667 | qed | |
| 668 | qed | |
| 669 | with assms show ?thesis by(simp add: finite_set_of_finite_funs) | |
| 670 | qed | |
| 28790 | 671 | |
| 60839 | 672 | |
| 60758 | 673 | subsection \<open>@{term [source] ran}\<close>
 | 
| 14100 | 674 | |
| 60839 | 675 | lemma ranI: "m a = Some b \<Longrightarrow> b \<in> ran m" | 
| 676 | by (auto simp: ran_def) | |
| 14100 | 677 | (* declare ranI [intro]? *) | 
| 13908 | 678 | |
| 20800 | 679 | lemma ran_empty [simp]: "ran empty = {}"
 | 
| 60839 | 680 | by (auto simp: ran_def) | 
| 13908 | 681 | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 682 | lemma ran_map_upd [simp]: "m a = None \<Longrightarrow> ran(m(a\<mapsto>b)) = insert b (ran m)" | 
| 60839 | 683 | unfolding ran_def | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 684 | by force | 
| 20800 | 685 | |
| 73832 | 686 | lemma fun_upd_None_if_notin_dom[simp]: "k \<notin> dom m \<Longrightarrow> m(k := None) = m" | 
| 687 | by auto | |
| 688 | ||
| 74802 | 689 | lemma ran_map_upd_Some: | 
| 690 |   "\<lbrakk> m x = Some y; inj_on m (dom m); z \<notin> ran m \<rbrakk> \<Longrightarrow> ran(m(x := Some z)) = ran m - {y} \<union> {z}"
 | |
| 691 | by(force simp add: ran_def domI inj_onD) | |
| 692 | ||
| 66583 | 693 | lemma ran_map_add: | 
| 694 |   assumes "dom m1 \<inter> dom m2 = {}"
 | |
| 695 | shows "ran (m1 ++ m2) = ran m1 \<union> ran m2" | |
| 696 | proof | |
| 697 | show "ran (m1 ++ m2) \<subseteq> ran m1 \<union> ran m2" | |
| 698 | unfolding ran_def by auto | |
| 699 | next | |
| 700 | show "ran m1 \<union> ran m2 \<subseteq> ran (m1 ++ m2)" | |
| 701 | proof - | |
| 702 | have "(m1 ++ m2) x = Some y" if "m1 x = Some y" for x y | |
| 703 | using assms map_add_comm that by fastforce | |
| 704 | moreover have "(m1 ++ m2) x = Some y" if "m2 x = Some y" for x y | |
| 705 | using assms that by auto | |
| 706 | ultimately show ?thesis | |
| 707 | unfolding ran_def by blast | |
| 708 | qed | |
| 709 | qed | |
| 710 | ||
| 711 | lemma finite_ran: | |
| 712 | assumes "finite (dom p)" | |
| 713 | shows "finite (ran p)" | |
| 714 | proof - | |
| 715 | have "ran p = (\<lambda>x. the (p x)) ` dom p" | |
| 716 | unfolding ran_def by force | |
| 717 | from this \<open>finite (dom p)\<close> show ?thesis by auto | |
| 718 | qed | |
| 719 | ||
| 60839 | 720 | lemma ran_distinct: | 
| 721 | assumes dist: "distinct (map fst al)" | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 722 | shows "ran (map_of al) = snd ` set al" | 
| 60839 | 723 | using assms | 
| 724 | proof (induct al) | |
| 725 | case Nil | |
| 726 | then show ?case by simp | |
| 34979 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 727 | next | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 728 | case (Cons kv al) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 729 | then have "ran (map_of al) = snd ` set al" by simp | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 730 | moreover from Cons.prems have "map_of al (fst kv) = None" | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 731 | by (simp add: map_of_eq_None_iff) | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 732 | ultimately show ?case by (simp only: map_of.simps ran_map_upd) simp | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 733 | qed | 
| 
8cb6e7a42e9c
more correspondence lemmas between related operations
 haftmann parents: 
34941diff
changeset | 734 | |
| 66584 | 735 | lemma ran_map_of_zip: | 
| 736 | assumes "length xs = length ys" "distinct xs" | |
| 737 | shows "ran (map_of (zip xs ys)) = set ys" | |
| 738 | using assms by (simp add: ran_distinct set_map[symmetric]) | |
| 739 | ||
| 60057 | 740 | lemma ran_map_option: "ran (\<lambda>x. map_option f (m x)) = f ` ran m" | 
| 60839 | 741 | by (auto simp add: ran_def) | 
| 742 | ||
| 73832 | 743 | subsection \<open>@{term [source] graph}\<close>
 | 
| 744 | ||
| 745 | lemma graph_empty[simp]: "graph empty = {}"
 | |
| 746 | unfolding graph_def by simp | |
| 747 | ||
| 748 | lemma in_graphI: "m k = Some v \<Longrightarrow> (k, v) \<in> graph m" | |
| 749 | unfolding graph_def by blast | |
| 750 | ||
| 751 | lemma in_graphD: "(k, v) \<in> graph m \<Longrightarrow> m k = Some v" | |
| 752 | unfolding graph_def by blast | |
| 753 | ||
| 754 | lemma graph_map_upd[simp]: "graph (m(k \<mapsto> v)) = insert (k, v) (graph (m(k := None)))" | |
| 755 | unfolding graph_def by (auto split: if_splits) | |
| 756 | ||
| 757 | lemma graph_fun_upd_None: "graph (m(k := None)) = {e \<in> graph m. fst e \<noteq> k}"
 | |
| 758 | unfolding graph_def by (auto split: if_splits) | |
| 759 | ||
| 760 | lemma graph_restrictD: | |
| 761 | assumes "(k, v) \<in> graph (m |` A)" | |
| 762 | shows "k \<in> A" and "m k = Some v" | |
| 763 | using assms unfolding graph_def | |
| 764 | by (auto simp: restrict_map_def split: if_splits) | |
| 765 | ||
| 766 | lemma graph_map_comp[simp]: "graph (m1 \<circ>\<^sub>m m2) = graph m2 O graph m1" | |
| 767 | unfolding graph_def by (auto simp: map_comp_Some_iff relcomp_unfold) | |
| 768 | ||
| 769 | lemma graph_map_add: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> graph (m1 ++ m2) = graph m1 \<union> graph m2"
 | |
| 770 | unfolding graph_def using map_add_comm by force | |
| 771 | ||
| 772 | lemma graph_eq_to_snd_dom: "graph m = (\<lambda>x. (x, the (m x))) ` dom m" | |
| 773 | unfolding graph_def dom_def by force | |
| 774 | ||
| 775 | lemma fst_graph_eq_dom: "fst ` graph m = dom m" | |
| 776 | unfolding graph_eq_to_snd_dom by force | |
| 777 | ||
| 778 | lemma graph_domD: "x \<in> graph m \<Longrightarrow> fst x \<in> dom m" | |
| 779 | using fst_graph_eq_dom by (metis imageI) | |
| 780 | ||
| 781 | lemma snd_graph_ran: "snd ` graph m = ran m" | |
| 782 | unfolding graph_def ran_def by force | |
| 783 | ||
| 784 | lemma graph_ranD: "x \<in> graph m \<Longrightarrow> snd x \<in> ran m" | |
| 785 | using snd_graph_ran by (metis imageI) | |
| 786 | ||
| 787 | lemma finite_graph_map_of: "finite (graph (map_of al))" | |
| 788 | unfolding graph_eq_to_snd_dom finite_dom_map_of | |
| 789 | using finite_dom_map_of by blast | |
| 790 | ||
| 74157 
8e2355ddce1b
add/rename some theorems about Map(pings)
 Lukas Stevens <mail@lukas-stevens.de> parents: 
73832diff
changeset | 791 | lemma graph_map_of_if_distinct_dom: "distinct (map fst al) \<Longrightarrow> graph (map_of al) = set al" | 
| 73832 | 792 | unfolding graph_def by auto | 
| 793 | ||
| 794 | lemma finite_graph_iff_finite_dom[simp]: "finite (graph m) = finite (dom m)" | |
| 795 | by (metis graph_eq_to_snd_dom finite_imageI fst_graph_eq_dom) | |
| 796 | ||
| 797 | lemma inj_on_fst_graph: "inj_on fst (graph m)" | |
| 798 | unfolding graph_def inj_on_def by force | |
| 13910 | 799 | |
| 61799 | 800 | subsection \<open>\<open>map_le\<close>\<close> | 
| 13910 | 801 | |
| 13912 | 802 | lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g" | 
| 60839 | 803 | by (simp add: map_le_def) | 
| 13910 | 804 | |
| 17724 | 805 | lemma upd_None_map_le [simp]: "f(x := None) \<subseteq>\<^sub>m f" | 
| 60839 | 806 | by (force simp add: map_le_def) | 
| 14187 | 807 | |
| 13910 | 808 | lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)" | 
| 60839 | 809 | by (fastforce simp add: map_le_def) | 
| 13910 | 810 | |
| 17724 | 811 | lemma map_le_imp_upd_le [simp]: "m1 \<subseteq>\<^sub>m m2 \<Longrightarrow> m1(x := None) \<subseteq>\<^sub>m m2(x \<mapsto> y)" | 
| 60839 | 812 | by (force simp add: map_le_def) | 
| 14187 | 813 | |
| 20800 | 814 | lemma map_le_upds [simp]: | 
| 60839 | 815 | "f \<subseteq>\<^sub>m g \<Longrightarrow> f(as [\<mapsto>] bs) \<subseteq>\<^sub>m g(as [\<mapsto>] bs)" | 
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 816 | proof (induct as arbitrary: f g bs) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 817 | case (Cons a as) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 818 | then show ?case | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 819 | by (cases bs) (use Cons in auto) | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 820 | qed auto | 
| 13908 | 821 | |
| 14033 | 822 | lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)" | 
| 60839 | 823 | by (fastforce simp add: map_le_def dom_def) | 
| 14033 | 824 | |
| 825 | lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f" | |
| 60839 | 826 | by (simp add: map_le_def) | 
| 14033 | 827 | |
| 14187 | 828 | lemma map_le_trans[trans]: "\<lbrakk> m1 \<subseteq>\<^sub>m m2; m2 \<subseteq>\<^sub>m m3\<rbrakk> \<Longrightarrow> m1 \<subseteq>\<^sub>m m3" | 
| 60839 | 829 | by (auto simp add: map_le_def dom_def) | 
| 14033 | 830 | |
| 831 | lemma map_le_antisym: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m f \<rbrakk> \<Longrightarrow> f = g" | |
| 71616 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 832 | unfolding map_le_def | 
| 
a9de39608b1a
more tidying up of old apply-proofs
 paulson <lp15@cam.ac.uk> parents: 
69593diff
changeset | 833 | by (metis ext domIff) | 
| 14033 | 834 | |
| 60839 | 835 | lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m g ++ f" | 
| 836 | by (fastforce simp: map_le_def) | |
| 14033 | 837 | |
| 60839 | 838 | lemma map_le_iff_map_add_commute: "f \<subseteq>\<^sub>m f ++ g \<longleftrightarrow> f ++ g = g ++ f" | 
| 839 | by (fastforce simp: map_add_def map_le_def fun_eq_iff split: option.splits) | |
| 15304 | 840 | |
| 60839 | 841 | lemma map_add_le_mapE: "f ++ g \<subseteq>\<^sub>m h \<Longrightarrow> g \<subseteq>\<^sub>m h" | 
| 842 | by (fastforce simp: map_le_def map_add_def dom_def) | |
| 15303 | 843 | |
| 60839 | 844 | lemma map_add_le_mapI: "\<lbrakk> f \<subseteq>\<^sub>m h; g \<subseteq>\<^sub>m h \<rbrakk> \<Longrightarrow> f ++ g \<subseteq>\<^sub>m h" | 
| 845 | by (auto simp: map_le_def map_add_def dom_def split: option.splits) | |
| 15303 | 846 | |
| 63828 | 847 | lemma map_add_subsumed1: "f \<subseteq>\<^sub>m g \<Longrightarrow> f++g = g" | 
| 848 | by (simp add: map_add_le_mapI map_le_antisym) | |
| 849 | ||
| 850 | lemma map_add_subsumed2: "f \<subseteq>\<^sub>m g \<Longrightarrow> g++f = g" | |
| 851 | by (metis map_add_subsumed1 map_le_iff_map_add_commute) | |
| 852 | ||
| 31080 | 853 | lemma dom_eq_singleton_conv: "dom f = {x} \<longleftrightarrow> (\<exists>v. f = [x \<mapsto> v])"
 | 
| 63834 | 854 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 855 | proof | |
| 856 | assume ?rhs | |
| 857 | then show ?lhs by (auto split: if_split_asm) | |
| 31080 | 858 | next | 
| 63834 | 859 | assume ?lhs | 
| 860 | then obtain v where v: "f x = Some v" by auto | |
| 861 | show ?rhs | |
| 862 | proof | |
| 863 | show "f = [x \<mapsto> v]" | |
| 864 | proof (rule map_le_antisym) | |
| 865 | show "[x \<mapsto> v] \<subseteq>\<^sub>m f" | |
| 866 | using v by (auto simp add: map_le_def) | |
| 867 | show "f \<subseteq>\<^sub>m [x \<mapsto> v]" | |
| 868 |         using \<open>dom f = {x}\<close> \<open>f x = Some v\<close> by (auto simp add: map_le_def)
 | |
| 869 | qed | |
| 870 | qed | |
| 31080 | 871 | qed | 
| 872 | ||
| 68454 | 873 | lemma map_add_eq_empty_iff[simp]: | 
| 874 | "(f++g = empty) \<longleftrightarrow> f = empty \<and> g = empty" | |
| 875 | by (metis map_add_None) | |
| 876 | ||
| 877 | lemma empty_eq_map_add_iff[simp]: | |
| 878 | "(empty = f++g) \<longleftrightarrow> f = empty \<and> g = empty" | |
| 879 | by(subst map_add_eq_empty_iff[symmetric])(rule eq_commute) | |
| 880 | ||
| 35565 | 881 | |
| 60758 | 882 | subsection \<open>Various\<close> | 
| 35565 | 883 | |
| 884 | lemma set_map_of_compr: | |
| 885 | assumes distinct: "distinct (map fst xs)" | |
| 886 |   shows "set xs = {(k, v). map_of xs k = Some v}"
 | |
| 60839 | 887 | using assms | 
| 888 | proof (induct xs) | |
| 889 | case Nil | |
| 890 | then show ?case by simp | |
| 35565 | 891 | next | 
| 892 | case (Cons x xs) | |
| 893 | obtain k v where "x = (k, v)" by (cases x) blast | |
| 894 | with Cons.prems have "k \<notin> dom (map_of xs)" | |
| 895 | by (simp add: dom_map_of_conv_image_fst) | |
| 896 |   then have *: "insert (k, v) {(k, v). map_of xs k = Some v} =
 | |
| 77644 
48b4e0cd94cd
bring priority in line with ordinary function update notation
 nipkow parents: 
77361diff
changeset | 897 |     {(k', v'). ((map_of xs)(k \<mapsto> v)) k' = Some v'}"
 | 
| 35565 | 898 | by (auto split: if_splits) | 
| 899 |   from Cons have "set xs = {(k, v). map_of xs k = Some v}" by simp
 | |
| 60758 | 900 | with * \<open>x = (k, v)\<close> show ?case by simp | 
| 35565 | 901 | qed | 
| 902 | ||
| 67051 | 903 | lemma eq_key_imp_eq_value: | 
| 904 | "v1 = v2" | |
| 905 | if "distinct (map fst xs)" "(k, v1) \<in> set xs" "(k, v2) \<in> set xs" | |
| 906 | proof - | |
| 907 | from that have "inj_on fst (set xs)" | |
| 908 | by (simp add: distinct_map) | |
| 909 | moreover have "fst (k, v1) = fst (k, v2)" | |
| 910 | by simp | |
| 911 | ultimately have "(k, v1) = (k, v2)" | |
| 912 | by (rule inj_onD) (fact that)+ | |
| 913 | then show ?thesis | |
| 914 | by simp | |
| 915 | qed | |
| 916 | ||
| 35565 | 917 | lemma map_of_inject_set: | 
| 918 | assumes distinct: "distinct (map fst xs)" "distinct (map fst ys)" | |
| 919 | shows "map_of xs = map_of ys \<longleftrightarrow> set xs = set ys" (is "?lhs \<longleftrightarrow> ?rhs") | |
| 920 | proof | |
| 921 | assume ?lhs | |
| 60758 | 922 |   moreover from \<open>distinct (map fst xs)\<close> have "set xs = {(k, v). map_of xs k = Some v}"
 | 
| 35565 | 923 | by (rule set_map_of_compr) | 
| 60758 | 924 |   moreover from \<open>distinct (map fst ys)\<close> have "set ys = {(k, v). map_of ys k = Some v}"
 | 
| 35565 | 925 | by (rule set_map_of_compr) | 
| 926 | ultimately show ?rhs by simp | |
| 927 | next | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 928 | assume ?rhs show ?lhs | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 929 | proof | 
| 35565 | 930 | fix k | 
| 60839 | 931 | show "map_of xs k = map_of ys k" | 
| 932 | proof (cases "map_of xs k") | |
| 35565 | 933 | case None | 
| 60758 | 934 | with \<open>?rhs\<close> have "map_of ys k = None" | 
| 35565 | 935 | by (simp add: map_of_eq_None_iff) | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 936 | with None show ?thesis by simp | 
| 35565 | 937 | next | 
| 938 | case (Some v) | |
| 60758 | 939 | with distinct \<open>?rhs\<close> have "map_of ys k = Some v" | 
| 35565 | 940 | by simp | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53015diff
changeset | 941 | with Some show ?thesis by simp | 
| 35565 | 942 | qed | 
| 943 | qed | |
| 944 | qed | |
| 945 | ||
| 73832 | 946 | lemma finite_Map_induct[consumes 1, case_names empty update]: | 
| 947 | assumes "finite (dom m)" | |
| 948 | assumes "P Map.empty" | |
| 949 | assumes "\<And>k v m. finite (dom m) \<Longrightarrow> k \<notin> dom m \<Longrightarrow> P m \<Longrightarrow> P (m(k \<mapsto> v))" | |
| 950 | shows "P m" | |
| 951 | using assms(1) | |
| 952 | proof(induction "dom m" arbitrary: m rule: finite_induct) | |
| 953 | case empty | |
| 954 | then show ?case using assms(2) unfolding dom_def by simp | |
| 955 | next | |
| 956 | case (insert x F) | |
| 957 | then have "finite (dom (m(x:=None)))" "x \<notin> dom (m(x:=None))" "P (m(x:=None))" | |
| 958 | by (metis Diff_insert_absorb dom_fun_upd)+ | |
| 959 | with assms(3)[OF this] show ?case | |
| 960 | by (metis fun_upd_triv fun_upd_upd option.exhaust) | |
| 961 | qed | |
| 962 | ||
| 963 | hide_const (open) Map.empty Map.graph | |
| 68450 | 964 | |
| 3981 | 965 | end |