Theory Map (Isabelle2005: October 2005)

# Theory Map

Up to index of Isabelle/HOL

theory Map
imports List
begin

    (*  Title:      HOL/Map.thy
        ID:         $Id: Map.thy,v 1.42 2005/09/29 15:02:57 paulson Exp $
        Author:     Tobias Nipkow, based on a theory by David von Oheimb
        Copyright   1997-2003 TU Muenchen

    The datatype of `maps' (written ~=>); strongly resembles maps in VDM.
    *)

    header {* Maps *}

    theory Map
    imports List
    begin

    types ('a,'b) "~=>" = "'a => 'b option" (infixr 0)
    translations (type) "a ~=> b " <= (type) "a => b option"

    consts
    chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
    map_add :: "('a ~=> 'b) => ('a ~=> 'b) => ('a ~=> 'b)" (infixl "++" 100)
    restrict_map :: "('a ~=> 'b) => 'a set => ('a ~=> 'b)" (infixl "|`"  110)
    dom     :: "('a ~=> 'b) => 'a set"
    ran     :: "('a ~=> 'b) => 'b set"
    map_of  :: "('a * 'b)list => 'a ~=> 'b"
    map_upds:: "('a ~=> 'b) => 'a list => 'b list =>
                ('a ~=> 'b)"
    map_upd_s::"('a ~=> 'b) => 'a set => 'b =>
                ('a ~=> 'b)"                         ("_/'(_{|->}_/')" [900,0,0]900)
    map_subst::"('a ~=> 'b) => 'b => 'b =>
                ('a ~=> 'b)"                         ("_/'(_~>_/')"    [900,0,0]900)
    map_le  :: "('a ~=> 'b) => ('a ~=> 'b) => bool" (infix "⊆m" 50)

    constdefs
      map_comp :: "('b ~=> 'c)  => ('a ~=> 'b) => ('a ~=> 'c)" (infixl "o'_m" 55)
      "f o_m g  == (λk. case g k of None => None | Some v => f v)"

    nonterminals
      maplets maplet

    syntax
      empty     ::  "'a ~=> 'b"
      "_maplet"  :: "['a, 'a] => maplet"             ("_ /|->/ _")
      "_maplets" :: "['a, 'a] => maplet"             ("_ /[|->]/ _")
      ""         :: "maplet => maplets"             ("_")
      "_Maplets" :: "[maplet, maplets] => maplets" ("_,/ _")
      "_MapUpd"  :: "['a ~=> 'b, maplets] => 'a ~=> 'b" ("_/'(_')" [900,0]900)
      "_Map"     :: "maplets => 'a ~=> 'b"            ("(1[_])")

    syntax (xsymbols)
      "~=>"     :: "[type, type] => type"    (infixr "\<rightharpoonup>" 0)

      map_comp :: "('b ~=> 'c)  => ('a ~=> 'b) => ('a ~=> 'c)" (infixl "om" 55)

      "_maplet"  :: "['a, 'a] => maplet"             ("_ /\<mapsto>/ _")
      "_maplets" :: "['a, 'a] => maplet"             ("_ /[\<mapsto>]/ _")

      map_upd_s  :: "('a ~=> 'b) => 'a set => 'b => ('a ~=> 'b)"
                                                     ("_/'(_/{\<mapsto>}/_')" [900,0,0]900)
      map_subst :: "('a ~=> 'b) => 'b => 'b =>
                    ('a ~=> 'b)"                     ("_/'(_\<leadsto>_/')"    [900,0,0]900)
     "@chg_map" :: "('a ~=> 'b) => 'a => ('b => 'b) => ('a ~=> 'b)"
                                              ("_/'(_/\<mapsto>λ_. _')"  [900,0,0,0] 900)

    syntax (latex output)
      restrict_map :: "('a ~=> 'b) => 'a set => ('a ~=> 'b)" ("_\<restriction>_" [111,110] 110)
      --"requires amssymb!"

    translations
      "empty"    => "_K None"
      "empty"    <= "%x. None"

      "m(x\<mapsto>λy. f)" == "chg_map (λy. f) x m"

      "_MapUpd m (_Maplets xy ms)"  == "_MapUpd (_MapUpd m xy) ms"
      "_MapUpd m (_maplet  x y)"    == "m(x:=Some y)"
      "_MapUpd m (_maplets x y)"    == "map_upds m x y"
      "_Map ms"                     == "_MapUpd empty ms"
      "_Map (_Maplets ms1 ms2)"     <= "_MapUpd (_Map ms1) ms2"
      "_Maplets ms1 (_Maplets ms2 ms3)" <= "_Maplets (_Maplets ms1 ms2) ms3"

    defs
    chg_map_def:  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"

    map_add_def:   "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
    restrict_map_def: "m|`A == %x. if x : A then m x else None"

    map_upds_def: "m(xs [|->] ys) == m ++ map_of (rev(zip xs ys))"
    map_upd_s_def: "m(as{|->}b) == %x. if x : as then Some b else m x"
    map_subst_def: "m(a~>b)     == %x. if m x = Some a then Some b else m x"

    dom_def: "dom(m) == {a. m a ~= None}"
    ran_def: "ran(m) == {b. EX a. m a = Some b}"

    map_le_def: "m1 ⊆m m2  ==  ALL a : dom m1. m1 a = m2 a"

    primrec
      "map_of [] = empty"
      "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"

    subsection {* @{term [source] empty} *}

    lemma empty_upd_none[simp]: "empty(x := None) = empty"
    apply (rule ext)
    apply (simp (no_asm))
    done

    (* FIXME: what is this sum_case nonsense?? *)
    lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty"
    apply (rule ext)
    apply (simp (no_asm) split add: sum.split)
    done

    subsection {* @{term [source] map_upd} *}

    lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
    apply (rule ext)
    apply (simp (no_asm_simp))
    done

    lemma map_upd_nonempty[simp]: "t(k|->x) ~= empty"
    apply safe
    apply (drule_tac x = k in fun_cong)
    apply (simp (no_asm_use))
    done

    lemma map_upd_eqD1: "m(a\<mapsto>x) = n(a\<mapsto>y) ==> x = y"
    by (drule fun_cong [of _ _ a], auto)

    lemma map_upd_Some_unfold:
      "((m(a|->b)) x = Some y) = (x = a ∧ b = y ∨ x ≠ a ∧ m x = Some y)"
    by auto

    lemma image_map_upd[simp]: "x ∉ A ==> m(x \<mapsto> y) ` A = m ` A"
    by fastsimp

    lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
    apply (unfold image_def)
    apply (simp (no_asm_use) add: full_SetCompr_eq)
    apply (rule finite_subset)
    prefer 2 apply assumption
    apply auto
    done

    (* FIXME: what is this sum_case nonsense?? *)
    subsection {* @{term [source] sum_case} and @{term [source] empty}/@{term [source] map_upd} *}

    lemma sum_case_map_upd_empty[simp]:
     "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
    apply (rule ext)
    apply (simp (no_asm) split add: sum.split)
    done

    lemma sum_case_empty_map_upd[simp]:
     "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
    apply (rule ext)
    apply (simp (no_asm) split add: sum.split)
    done

    lemma sum_case_map_upd_map_upd[simp]:
     "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
    apply (rule ext)
    apply (simp (no_asm) split add: sum.split)
    done

    subsection {* @{term [source] chg_map} *}

    lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
    by (unfold chg_map_def, auto)

    lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
    by (unfold chg_map_def, auto)

    lemma chg_map_other [simp]: "a ≠ b ==> chg_map f a m b = m b"
    by (auto simp: chg_map_def split add: option.split)

    subsection {* @{term [source] map_of} *}

    lemma map_of_eq_None_iff:
     "(map_of xys x = None) = (x ∉ fst ` (set xys))"
    by (induct xys) simp_all

    lemma map_of_is_SomeD:
     "map_of xys x = Some y ==> (x,y) ∈ set xys"
    apply(induct xys)
     apply simp
    apply(clarsimp split:if_splits)
    done

    lemma map_of_eq_Some_iff[simp]:
     "distinct(map fst xys) ==> (map_of xys x = Some y) = ((x,y) ∈ set xys)"
    apply(induct xys)
     apply(simp)
    apply(auto simp:map_of_eq_None_iff[symmetric])
    done

    lemma Some_eq_map_of_iff[simp]:
     "distinct(map fst xys) ==> (Some y = map_of xys x) = ((x,y) ∈ set xys)"
    by(auto simp del:map_of_eq_Some_iff simp add:map_of_eq_Some_iff[symmetric])

    lemma map_of_is_SomeI [simp]: "[| distinct(map fst xys); (x,y) ∈ set xys |]
      ==> map_of xys x = Some y"
    apply (induct xys)
     apply simp
    apply force
    done

    lemma map_of_zip_is_None[simp]:
      "length xs = length ys ==> (map_of (zip xs ys) x = None) = (x ∉ set xs)"
    by (induct rule:list_induct2, simp_all)

    lemma finite_range_map_of: "finite (range (map_of xys))"
    apply (induct xys)
    apply  (simp_all (no_asm) add: image_constant)
    apply (rule finite_subset)
    prefer 2 apply assumption
    apply auto
    done

    lemma map_of_SomeD [rule_format]: "map_of xs k = Some y --> (k,y):set xs"
    by (induct "xs", auto)

    lemma map_of_mapk_SomeI [rule_format]:
         "inj f ==> map_of t k = Some x -->
            map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
    apply (induct "t")
    apply  (auto simp add: inj_eq)
    done

    lemma weak_map_of_SomeI [rule_format]:
         "(k, x) : set l --> (∃x. map_of l k = Some x)"
    by (induct "l", auto)

    lemma map_of_filter_in:
    "[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
    apply (rule mp)
    prefer 2 apply assumption
    apply (erule thin_rl)
    apply (induct "xs", auto)
    done

    lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
    by (induct "xs", auto)

    subsection {* @{term [source] option_map} related *}

    lemma option_map_o_empty[simp]: "option_map f o empty = empty"
    apply (rule ext)
    apply (simp (no_asm))
    done

    lemma option_map_o_map_upd[simp]:
     "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
    apply (rule ext)
    apply (simp (no_asm))
    done

    subsection {* @{term [source] map_comp} related *}

    lemma map_comp_empty [simp]:
      "m om empty = empty"
      "empty om m = empty"
      by (auto simp add: map_comp_def intro: ext split: option.splits)

    lemma map_comp_simps [simp]:
      "m2 k = None ==> (m1 om m2) k = None"
      "m2 k = Some k' ==> (m1 om m2) k = m1 k'"
      by (auto simp add: map_comp_def)

    lemma map_comp_Some_iff:
      "((m1 om m2) k = Some v) = (∃k'. m2 k = Some k' ∧ m1 k' = Some v)"
      by (auto simp add: map_comp_def split: option.splits)

    lemma map_comp_None_iff:
      "((m1 om m2) k = None) = (m2 k = None ∨ (∃k'. m2 k = Some k' ∧ m1 k' = None)) "
      by (auto simp add: map_comp_def split: option.splits)

    subsection {* @{text "++"} *}

    lemma map_add_empty[simp]: "m ++ empty = m"
    apply (unfold map_add_def)
    apply (simp (no_asm))
    done

    lemma empty_map_add[simp]: "empty ++ m = m"
    apply (unfold map_add_def)
    apply (rule ext)
    apply (simp split add: option.split)
    done

    lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3"
    apply(rule ext)
    apply(simp add: map_add_def split:option.split)
    done

    lemma map_add_Some_iff:
     "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
    apply (unfold map_add_def)
    apply (simp (no_asm) split add: option.split)
    done

    lemmas map_add_SomeD = map_add_Some_iff [THEN iffD1, standard]
    declare map_add_SomeD [dest!]

    lemma map_add_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
    by (subst map_add_Some_iff, fast)

    lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
    apply (unfold map_add_def)
    apply (simp (no_asm) split add: option.split)
    done

    lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
    apply (unfold map_add_def)
    apply (rule ext, auto)
    done

    lemma map_add_upds[simp]: "m1 ++ (m2(xs[\<mapsto>]ys)) = (m1++m2)(xs[\<mapsto>]ys)"
    by(simp add:map_upds_def)

    lemma map_of_append[simp]: "map_of (xs@ys) = map_of ys ++ map_of xs"
    apply (unfold map_add_def)
    apply (induct "xs")
    apply (simp (no_asm))
    apply (rule ext)
    apply (simp (no_asm_simp) split add: option.split)
    done

    declare fun_upd_apply [simp del]
    lemma finite_range_map_of_map_add:
     "finite (range f) ==> finite (range (f ++ map_of l))"
    apply (induct "l", auto)
    apply (erule finite_range_updI)
    done
    declare fun_upd_apply [simp]

    lemma inj_on_map_add_dom[iff]:
     "inj_on (m ++ m') (dom m') = inj_on m' (dom m')"
    by(fastsimp simp add:map_add_def dom_def inj_on_def split:option.splits)

    subsection {* @{term [source] restrict_map} *}

    lemma restrict_map_to_empty[simp]: "m|`{} = empty"
    by(simp add: restrict_map_def)

    lemma restrict_map_empty[simp]: "empty|`D = empty"
    by(simp add: restrict_map_def)

    lemma restrict_in [simp]: "x ∈ A ==> (m|`A) x = m x"
    by (auto simp: restrict_map_def)

    lemma restrict_out [simp]: "x ∉ A ==> (m|`A) x = None"
    by (auto simp: restrict_map_def)

    lemma ran_restrictD: "y ∈ ran (m|`A) ==> ∃x∈A. m x = Some y"
    by (auto simp: restrict_map_def ran_def split: split_if_asm)

    lemma dom_restrict [simp]: "dom (m|`A) = dom m ∩ A"
    by (auto simp: restrict_map_def dom_def split: split_if_asm)

    lemma restrict_upd_same [simp]: "m(x\<mapsto>y)|`(-{x}) = m|`(-{x})"
    by (rule ext, auto simp: restrict_map_def)

    lemma restrict_restrict [simp]: "m|`A|`B = m|`(A∩B)"
    by (rule ext, auto simp: restrict_map_def)

    lemma restrict_fun_upd[simp]:
     "m(x := y)|`D = (if x ∈ D then (m|`(D-{x}))(x := y) else m|`D)"
    by(simp add: restrict_map_def expand_fun_eq)

    lemma fun_upd_None_restrict[simp]:
      "(m|`D)(x := None) = (if x:D then m|`(D - {x}) else m|`D)"
    by(simp add: restrict_map_def expand_fun_eq)

    lemma fun_upd_restrict:
     "(m|`D)(x := y) = (m|`(D-{x}))(x := y)"
    by(simp add: restrict_map_def expand_fun_eq)

    lemma fun_upd_restrict_conv[simp]:
     "x ∈ D ==> (m|`D)(x := y) = (m|`(D-{x}))(x := y)"
    by(simp add: restrict_map_def expand_fun_eq)

    subsection {* @{term [source] map_upds} *}

    lemma map_upds_Nil1[simp]: "m([] [|->] bs) = m"
    by(simp add:map_upds_def)

    lemma map_upds_Nil2[simp]: "m(as [|->] []) = m"
    by(simp add:map_upds_def)

    lemma map_upds_Cons[simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)"
    by(simp add:map_upds_def)

    lemma map_upds_append1[simp]: "!!ys m. size xs < size ys ==>
      m(xs@[x] [\<mapsto>] ys) = m(xs [\<mapsto>] ys)(x \<mapsto> ys!size xs)"
    apply(induct xs)
     apply(clarsimp simp add:neq_Nil_conv)
    apply (case_tac ys, simp, simp)
    done

    lemma map_upds_list_update2_drop[simp]:
     "!!m ys i. [|size xs ≤ i; i < size ys|]
         ==> m(xs[\<mapsto>]ys[i:=y]) = m(xs[\<mapsto>]ys)"
    apply (induct xs, simp)
    apply (case_tac ys, simp)
    apply(simp split:nat.split)
    done

    lemma map_upd_upds_conv_if: "!!x y ys f.
     (f(x|->y))(xs [|->] ys) =
     (if x : set(take (length ys) xs) then f(xs [|->] ys)
                                      else (f(xs [|->] ys))(x|->y))"
    apply (induct xs, simp)
    apply(case_tac ys)
     apply(auto split:split_if simp:fun_upd_twist)
    done

    lemma map_upds_twist [simp]:
     "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
    apply(insert set_take_subset)
    apply (fastsimp simp add: map_upd_upds_conv_if)
    done

    lemma map_upds_apply_nontin[simp]:
     "!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x"
    apply (induct xs, simp)
    apply(case_tac ys)
     apply(auto simp: map_upd_upds_conv_if)
    done

    lemma fun_upds_append_drop[simp]:
      "!!m ys. size xs = size ys ==> m(xs@zs[\<mapsto>]ys) = m(xs[\<mapsto>]ys)"
    apply(induct xs)
     apply (simp)
    apply(case_tac ys)
    apply simp_all
    done

    lemma fun_upds_append2_drop[simp]:
      "!!m ys. size xs = size ys ==> m(xs[\<mapsto>]ys@zs) = m(xs[\<mapsto>]ys)"
    apply(induct xs)
     apply (simp)
    apply(case_tac ys)
    apply simp_all
    done

    lemma restrict_map_upds[simp]: "!!m ys.
     [| length xs = length ys; set xs ⊆ D |]
     ==> m(xs [\<mapsto>] ys)|`D = (m|`(D - set xs))(xs [\<mapsto>] ys)"
    apply (induct xs, simp)
    apply (case_tac ys, simp)
    apply(simp add:Diff_insert[symmetric] insert_absorb)
    apply(simp add: map_upd_upds_conv_if)
    done

    subsection {* @{term [source] map_upd_s} *}

    lemma map_upd_s_apply [simp]:
      "(m(as{|->}b)) x = (if x : as then Some b else m x)"
    by (simp add: map_upd_s_def)

    lemma map_subst_apply [simp]:
      "(m(a~>b)) x = (if m x = Some a then Some b else m x)"
    by (simp add: map_subst_def)

    subsection {* @{term [source] dom} *}

    lemma domI: "m a = Some b ==> a : dom m"
    by (unfold dom_def, auto)
    (* declare domI [intro]? *)

    lemma domD: "a : dom m ==> ∃b. m a = Some b"
    by (unfold dom_def, auto)

    lemma domIff[iff]: "(a : dom m) = (m a ~= None)"
    by (unfold dom_def, auto)
    declare domIff [simp del]

    lemma dom_empty[simp]: "dom empty = {}"
    apply (unfold dom_def)
    apply (simp (no_asm))
    done

    lemma dom_fun_upd[simp]:
     "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
    by (simp add:dom_def) blast

    lemma dom_map_of: "dom(map_of xys) = {x. ∃y. (x,y) : set xys}"
    apply(induct xys)
    apply(auto simp del:fun_upd_apply)
    done

    lemma dom_map_of_conv_image_fst:
      "dom(map_of xys) = fst ` (set xys)"
    by(force simp: dom_map_of)

    lemma dom_map_of_zip[simp]: "[| length xs = length ys; distinct xs |] ==>
      dom(map_of(zip xs ys)) = set xs"
    by(induct rule: list_induct2, simp_all)

    lemma finite_dom_map_of: "finite (dom (map_of l))"
    apply (unfold dom_def)
    apply (induct "l")
    apply (auto simp add: insert_Collect [symmetric])
    done

    lemma dom_map_upds[simp]:
     "!!m ys. dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m"
    apply (induct xs, simp)
    apply (case_tac ys, auto)
    done

    lemma dom_map_add[simp]: "dom(m++n) = dom n Un dom m"
    by (unfold dom_def, auto)

    lemma dom_override_on[simp]:
     "dom(override_on f g A) =
     (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
    by(auto simp add: dom_def override_on_def)

    lemma map_add_comm: "dom m1 ∩ dom m2 = {} ==> m1++m2 = m2++m1"
    apply(rule ext)
    apply(fastsimp simp:map_add_def split:option.split)
    done

    subsection {* @{term [source] ran} *}

    lemma ranI: "m a = Some b ==> b : ran m"
    by (auto simp add: ran_def)
    (* declare ranI [intro]? *)

    lemma ran_empty[simp]: "ran empty = {}"
    apply (unfold ran_def)
    apply (simp (no_asm))
    done

    lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
    apply (unfold ran_def, auto)
    apply (subgoal_tac "~ (aa = a) ")
    apply auto
    done

    subsection {* @{text "map_le"} *}

    lemma map_le_empty [simp]: "empty ⊆m g"
    by(simp add:map_le_def)

    lemma upd_None_map_le [simp]: "f(x := None) ⊆m f"
    by(force simp add:map_le_def)

    lemma map_le_upd[simp]: "f ⊆m g ==> f(a := b) ⊆m g(a := b)"
    by(fastsimp simp add:map_le_def)

    lemma map_le_imp_upd_le [simp]: "m1 ⊆m m2 ==> m1(x := None) ⊆m m2(x \<mapsto> y)"
    by(force simp add:map_le_def)

    lemma map_le_upds[simp]:
     "!!f g bs. f ⊆m g ==> f(as [|->] bs) ⊆m g(as [|->] bs)"
    apply (induct as, simp)
    apply (case_tac bs, auto)
    done

    lemma map_le_implies_dom_le: "(f ⊆m g) ==> (dom f ⊆ dom g)"
      by (fastsimp simp add: map_le_def dom_def)

    lemma map_le_refl [simp]: "f ⊆m f"
      by (simp add: map_le_def)

    lemma map_le_trans[trans]: "[| m1 ⊆m m2; m2 ⊆m m3|] ==> m1 ⊆m m3"
    by(force simp add:map_le_def)

    lemma map_le_antisym: "[| f ⊆m g; g ⊆m f |] ==> f = g"
      apply (unfold map_le_def)
      apply (rule ext)
      apply (case_tac "x ∈ dom f", simp)
      apply (case_tac "x ∈ dom g", simp, fastsimp)
    done

    lemma map_le_map_add [simp]: "f ⊆m (g ++ f)"
      by (fastsimp simp add: map_le_def)

    lemma map_le_iff_map_add_commute: "(f ⊆m f ++ g) = (f++g = g++f)"
    by(fastsimp simp add:map_add_def map_le_def expand_fun_eq split:option.splits)

    lemma map_add_le_mapE: "f++g ⊆m h ==> g ⊆m h"
    by (fastsimp simp add: map_le_def map_add_def dom_def)

    lemma map_add_le_mapI: "[| f ⊆m h; g ⊆m h; f ⊆m f++g |] ==> f++g ⊆m h"
    by (clarsimp simp add: map_le_def map_add_def dom_def split:option.splits)

    end

### @{term [source] empty}

lemma empty_upd_none:

      empty(x := None) = empty

lemma sum_case_empty_empty:

      sum_case empty empty = empty

### @{term [source] map_upd}

lemma map_upd_triv:

      t k = Some x ==> t(k |-> x) = t

lemma map_upd_nonempty:

      t(k |-> x) ≠ empty

lemma map_upd_eqD1:

      m(a |-> x) = n(a |-> y) ==> x = y

lemma map_upd_Some_unfold:

      ((m(a |-> b)) x = Some y) = (x = a ∧ b = y ∨ x ≠ a ∧ m x = Some y)

lemma image_map_upd:

      x ∉ A ==> m(x |-> y) ` A = m ` A

lemma finite_range_updI:

      finite (range f) ==> finite (range (f(a |-> b)))

### @{term [source] sum_case} and @{term [source] empty}/@{term [source] map_upd}

lemma sum_case_map_upd_empty:

      sum_case (m(k |-> y)) empty = sum_case m empty(Inl k |-> y)

lemma sum_case_empty_map_upd:

      sum_case empty (m(k |-> y)) = sum_case empty m(Inr k |-> y)

lemma sum_case_map_upd_map_upd:

      sum_case (m1.0(k1.0 |-> y1.0)) (m2.0(k2.0 |-> y2.0)) =
      sum_case (m1.0(k1.0 |-> y1.0)) m2.0(Inr k2.0 |-> y2.0)

### @{term [source] chg_map}

lemma chg_map_new:

      m a = None ==> chg_map f a m = m

lemma chg_map_upd:

      m a = Some b ==> chg_map f a m = m(a |-> f b)

lemma chg_map_other:

      a ≠ b ==> chg_map f a m b = m b

### @{term [source] map_of}

lemma map_of_eq_None_iff:

      (map_of xys x = None) = (x ∉ fst ` set xys)

lemma map_of_is_SomeD:

      map_of xys x = Some y ==> (x, y) ∈ set xys

lemma map_of_eq_Some_iff:

      distinct (map fst xys) ==> (map_of xys x = Some y) = ((x, y) ∈ set xys)

lemma Some_eq_map_of_iff:

      distinct (map fst xys) ==> (Some y = map_of xys x) = ((x, y) ∈ set xys)

lemma map_of_is_SomeI:

      [| distinct (map fst xys); (x, y) ∈ set xys |] ==> map_of xys x = Some y

lemma map_of_zip_is_None:

      length xs = length ys ==> (map_of (zip xs ys) x = None) = (x ∉ set xs)

lemma finite_range_map_of:

      finite (range (map_of xys))

lemma map_of_SomeD:

      map_of xs k = Some y ==> (k, y) ∈ set xs

lemma map_of_mapk_SomeI:

      [| inj f; map_of t k = Some x |]
      ==> map_of (map (%(k, y). (f k, y)) t) (f k) = Some x

lemma weak_map_of_SomeI:

      (k, x) ∈ set l ==> ∃x. map_of l k = Some x

lemma map_of_filter_in:

      [| map_of xs k = Some z; P k z |] ==> map_of [(x, y)∈xs . P x y] k = Some z

lemma map_of_map:

      map_of (map (%(a, b). (a, f b)) xs) x = option_map f (map_of xs x)

### @{term [source] option_map} related

lemma option_map_o_empty:

      option_map f o empty = empty

lemma option_map_o_map_upd:

      option_map f o m(a |-> b) = (option_map f o m)(a |-> f b)

### @{term [source] map_comp} related

lemma map_comp_empty:

      m o_m empty = empty

      empty o_m m = empty

lemma map_comp_simps:

      m2.0 k = None ==> (m1.0 o_m m2.0) k = None

      m2.0 k = Some k' ==> (m1.0 o_m m2.0) k = m1.0 k'

lemma map_comp_Some_iff:

      ((m1.0 o_m m2.0) k = Some v) = (∃k'. m2.0 k = Some k' ∧ m1.0 k' = Some v)

lemma map_comp_None_iff:

      ((m1.0 o_m m2.0) k = None) =
      (m2.0 k = None ∨ (∃k'. m2.0 k = Some k' ∧ m1.0 k' = None))

### @{text "++"}

lemma map_add_empty:

      m ++ empty = m

lemma empty_map_add:

      empty ++ m = m

lemma map_add_assoc:

      m1.0 ++ (m2.0 ++ m3.0) = m1.0 ++ m2.0 ++ m3.0

lemma map_add_Some_iff:

      ((m ++ n) k = Some x) = (n k = Some x ∨ n k = None ∧ m k = Some x)

lemmas map_add_SomeD:

      (m ++ n) k = Some x ==> n k = Some x ∨ n k = None ∧ m k = Some x

lemmas map_add_SomeD:

      (m ++ n) k = Some x ==> n k = Some x ∨ n k = None ∧ m k = Some x

lemma map_add_find_right:

      n k = Some xx ==> (m ++ n) k = Some xx

lemma map_add_None:

      ((m ++ n) k = None) = (n k = None ∧ m k = None)

lemma map_add_upd:

      f ++ g(x |-> y) = (f ++ g)(x |-> y)

lemma map_add_upds:

      m1.0 ++ m2.0(xs [|->] ys) = (m1.0 ++ m2.0)(xs [|->] ys)

lemma map_of_append:

      map_of (xs @ ys) = map_of ys ++ map_of xs

lemma finite_range_map_of_map_add:

      finite (range f) ==> finite (range (f ++ map_of l))

lemma inj_on_map_add_dom:

      inj_on (m ++ m') (dom m') = inj_on m' (dom m')

### @{term [source] restrict_map}

lemma restrict_map_to_empty:

      m |` {} = empty

lemma restrict_map_empty:

      empty |` D = empty

lemma restrict_in:

      x ∈ A ==> (m |` A) x = m x

lemma restrict_out:

      x ∉ A ==> (m |` A) x = None

lemma ran_restrictD:

      y ∈ ran (m |` A) ==> ∃x∈A. m x = Some y

lemma dom_restrict:

      dom (m |` A) = dom m ∩ A

lemma restrict_upd_same:

      m(x |-> y) |` (- {x}) = m |` (- {x})

lemma restrict_restrict:

      m |` A |` B = m |` (A ∩ B)

lemma restrict_fun_upd:

      m(x := y) |` D = (if x ∈ D then (m |` (D - {x}))(x := y) else m |` D)

lemma fun_upd_None_restrict:

      (m |` D)(x := None) = (if x ∈ D then m |` (D - {x}) else m |` D)

lemma fun_upd_restrict:

      (m |` D)(x := y) = (m |` (D - {x}))(x := y)

lemma fun_upd_restrict_conv:

      x ∈ D ==> (m |` D)(x := y) = (m |` (D - {x}))(x := y)

### @{term [source] map_upds}

lemma map_upds_Nil1:

      m([] [|->] bs) = m

lemma map_upds_Nil2:

      m(as [|->] []) = m

lemma map_upds_Cons:

      m(a # as [|->] b # bs) = m(a |-> b, as [|->] bs)

lemma map_upds_append1:

      length xs < length ys
      ==> m(xs @ [x] [|->] ys) = m(xs [|->] ys, x |-> ys ! length xs)

lemma map_upds_list_update2_drop:

      [| length xs ≤ i; i < length ys |] ==> m(xs [|->] ys[i := y]) = m(xs [|->] ys)

lemma map_upd_upds_conv_if:

      f(x |-> y, xs [|->] ys) =
      (if x ∈ set (take (length ys) xs) then f(xs [|->] ys)
       else f(xs [|->] ys, x |-> y))

lemma map_upds_twist:

      a ∉ set as ==> m(a |-> b, as [|->] bs) = m(as [|->] bs, a |-> b)

lemma map_upds_apply_nontin:

      x ∉ set xs ==> (f(xs [|->] ys)) x = f x

lemma fun_upds_append_drop:

      length xs = length ys ==> m(xs @ zs [|->] ys) = m(xs [|->] ys)

lemma fun_upds_append2_drop:

      length xs = length ys ==> m(xs [|->] ys @ zs) = m(xs [|->] ys)

lemma restrict_map_upds:

      [| length xs = length ys; set xs ⊆ D |]
      ==> m(xs [|->] ys) |` D = (m |` (D - set xs))(xs [|->] ys)

### @{term [source] map_upd_s}

lemma map_upd_s_apply:

      (m(as{|->}b)) x = (if x ∈ as then Some b else m x)

lemma map_subst_apply:

      (m(a~>b)) x = (if m x = Some a then Some b else m x)

### @{term [source] dom}

lemma domI:

      m a = Some b ==> a ∈ dom m

lemma domD:

      a ∈ dom m ==> ∃b. m a = Some b

lemma domIff:

      (a ∈ dom m) = (m a ≠ None)

lemma dom_empty:

      dom empty = {}

lemma dom_fun_upd:

      dom (f(x := y)) = (if y = None then dom f - {x} else insert x (dom f))

lemma dom_map_of:

      dom (map_of xys) = {x. ∃y. (x, y) ∈ set xys}

lemma dom_map_of_conv_image_fst:

      dom (map_of xys) = fst ` set xys

lemma dom_map_of_zip:

      [| length xs = length ys; distinct xs |] ==> dom (map_of (zip xs ys)) = set xs

lemma finite_dom_map_of:

      finite (dom (map_of l))

lemma dom_map_upds:

      dom (m(xs [|->] ys)) = set (take (length ys) xs) ∪ dom m

lemma dom_map_add:

      dom (m ++ n) = dom n ∪ dom m

lemma dom_override_on:

      dom (override_on f g A) = dom f - {a. a ∈ A - dom g} ∪ {a. a ∈ A ∩ dom g}

lemma map_add_comm:

      dom m1.0 ∩ dom m2.0 = {} ==> m1.0 ++ m2.0 = m2.0 ++ m1.0

### @{term [source] ran}

lemma ranI:

      m a = Some b ==> b ∈ ran m

lemma ran_empty:

      ran empty = {}

lemma ran_map_upd:

      m a = None ==> ran (m(a |-> b)) = insert b (ran m)

### @{text "map_le"}

lemma map_le_empty:

      empty ⊆m g

lemma upd_None_map_le:

      f(x := None) ⊆m f

lemma map_le_upd:

      f ⊆m g ==> f(a := b) ⊆m g(a := b)

lemma map_le_imp_upd_le:

      m1.0 ⊆m m2.0 ==> m1.0(x := None) ⊆m m2.0(x |-> y)

lemma map_le_upds:

      f ⊆m g ==> f(as [|->] bs) ⊆m g(as [|->] bs)

lemma map_le_implies_dom_le:

      f ⊆m g ==> dom f ⊆ dom g

lemma map_le_refl:

      f ⊆m f

lemma map_le_trans:

      [| m1.0 ⊆m m2.0; m2.0 ⊆m m3.0 |] ==> m1.0 ⊆m m3.0

lemma map_le_antisym:

      [| f ⊆m g; g ⊆m f |] ==> f = g

lemma map_le_map_add:

      f ⊆m g ++ f

lemma map_le_iff_map_add_commute:

      (f ⊆m f ++ g) = (f ++ g = g ++ f)

lemma map_add_le_mapE:

      f ++ g ⊆m h ==> g ⊆m h

lemma map_add_le_mapI:

      [| f ⊆m h; g ⊆m h; f ⊆m f ++ g |] ==> f ++ g ⊆m h