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