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