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