src/HOL/Map.thy
author nipkow
Wed Apr 30 17:53:47 2003 +0200 (2003-04-30)
changeset 13937 e9d57517c9b1
parent 13914 026866537fae
child 14025 d9b155757dc8
permissions -rw-r--r--
added a thm
     1 (*  Title:      HOL/Map.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, based on a theory by David von Oheimb
     4     Copyright   1997-2003 TU Muenchen
     5 
     6 The datatype of `maps' (written ~=>); strongly resembles maps in VDM.
     7 *)
     8 
     9 header {* Maps *}
    10 
    11 theory Map = List:
    12 
    13 types ('a,'b) "~=>" = "'a => 'b option" (infixr 0)
    14 
    15 consts
    16 chg_map	:: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
    17 override:: "('a ~=> 'b) => ('a ~=> 'b) => ('a ~=> 'b)" (infixl "++" 100)
    18 dom	:: "('a ~=> 'b) => 'a set"
    19 ran	:: "('a ~=> 'b) => 'b set"
    20 map_of	:: "('a * 'b)list => 'a ~=> 'b"
    21 map_upds:: "('a ~=> 'b) => 'a list => 'b list => 
    22 	    ('a ~=> 'b)"		 ("_/'(_[|->]_/')" [900,0,0]900)
    23 map_le  :: "('a ~=> 'b) => ('a ~=> 'b) => bool" (infix "\<subseteq>\<^sub>m" 50)
    24 
    25 syntax
    26 empty	::  "'a ~=> 'b"
    27 map_upd	:: "('a ~=> 'b) => 'a => 'b => ('a ~=> 'b)"
    28 					 ("_/'(_/|->_')"   [900,0,0]900)
    29 
    30 syntax (xsymbols)
    31   "~=>"     :: "[type, type] => type"    (infixr "\<leadsto>" 0)
    32   map_upd   :: "('a ~=> 'b) => 'a      => 'b      => ('a ~=> 'b)"
    33 					  ("_/'(_/\<mapsto>/_')"  [900,0,0]900)
    34   map_upds  :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)"
    35 				         ("_/'(_/[\<mapsto>]/_')" [900,0,0]900)
    36 
    37 translations
    38   "empty"    => "_K None"
    39   "empty"    <= "%x. None"
    40 
    41   "m(a|->b)" == "m(a:=Some b)"
    42 
    43 defs
    44 chg_map_def:  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
    45 
    46 override_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
    47 
    48 dom_def: "dom(m) == {a. m a ~= None}"
    49 ran_def: "ran(m) == {b. ? a. m a = Some b}"
    50 
    51 map_le_def: "m1 \<subseteq>\<^sub>m m2  ==  ALL a : dom m1. m1 a = m2 a"
    52 
    53 primrec
    54   "map_of [] = empty"
    55   "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"
    56 
    57 primrec "t([]  [|->]bs) = t"
    58         "t(a#as[|->]bs) = t(a|->hd bs)(as[|->]tl bs)"
    59 
    60 
    61 subsection {* empty *}
    62 
    63 lemma empty_upd_none[simp]: "empty(x := None) = empty"
    64 apply (rule ext)
    65 apply (simp (no_asm))
    66 done
    67 
    68 
    69 (* FIXME: what is this sum_case nonsense?? *)
    70 lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty"
    71 apply (rule ext)
    72 apply (simp (no_asm) split add: sum.split)
    73 done
    74 
    75 subsection {* map\_upd *}
    76 
    77 lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
    78 apply (rule ext)
    79 apply (simp (no_asm_simp))
    80 done
    81 
    82 lemma map_upd_nonempty[simp]: "t(k|->x) ~= empty"
    83 apply safe
    84 apply (drule_tac x = "k" in fun_cong)
    85 apply (simp (no_asm_use))
    86 done
    87 
    88 lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
    89 apply (unfold image_def)
    90 apply (simp (no_asm_use) add: full_SetCompr_eq)
    91 apply (rule finite_subset)
    92 prefer 2 apply (assumption)
    93 apply auto
    94 done
    95 
    96 
    97 (* FIXME: what is this sum_case nonsense?? *)
    98 subsection {* sum\_case and empty/map\_upd *}
    99 
   100 lemma sum_case_map_upd_empty[simp]:
   101  "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
   102 apply (rule ext)
   103 apply (simp (no_asm) split add: sum.split)
   104 done
   105 
   106 lemma sum_case_empty_map_upd[simp]:
   107  "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
   108 apply (rule ext)
   109 apply (simp (no_asm) split add: sum.split)
   110 done
   111 
   112 lemma sum_case_map_upd_map_upd[simp]:
   113  "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
   114 apply (rule ext)
   115 apply (simp (no_asm) split add: sum.split)
   116 done
   117 
   118 
   119 subsection {* map\_upds *}
   120 
   121 lemma map_upd_upds_conv_if:
   122  "!!x y ys f. (f(x|->y))(xs [|->] ys) =
   123               (if x : set xs then f(xs [|->] ys) else (f(xs [|->] ys))(x|->y))"
   124 apply(induct xs)
   125  apply simp
   126 apply(simp split:split_if add:fun_upd_twist eq_sym_conv)
   127 done
   128 
   129 lemma map_upds_twist [simp]:
   130  "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
   131 by (simp add: map_upd_upds_conv_if)
   132 
   133 lemma map_upds_apply_nontin[simp]:
   134  "!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x"
   135 apply(induct xs)
   136  apply simp
   137 apply(simp add: fun_upd_apply map_upd_upds_conv_if split:split_if)
   138 done
   139 
   140 subsection {* chg\_map *}
   141 
   142 lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
   143 apply (unfold chg_map_def)
   144 apply auto
   145 done
   146 
   147 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   148 apply (unfold chg_map_def)
   149 apply auto
   150 done
   151 
   152 
   153 subsection {* map\_of *}
   154 
   155 lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs"
   156 apply (induct_tac "xs")
   157 apply  auto
   158 done
   159 
   160 lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x -->  
   161    map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
   162 apply (induct_tac "t")
   163 apply  (auto simp add: inj_eq)
   164 done
   165 
   166 lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)"
   167 apply (induct_tac "l")
   168 apply  auto
   169 done
   170 
   171 lemma map_of_filter_in: 
   172 "[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
   173 apply (rule mp)
   174 prefer 2 apply (assumption)
   175 apply (erule thin_rl)
   176 apply (induct_tac "xs")
   177 apply  auto
   178 done
   179 
   180 lemma finite_range_map_of: "finite (range (map_of l))"
   181 apply (induct_tac "l")
   182 apply  (simp_all (no_asm) add: image_constant)
   183 apply (rule finite_subset)
   184 prefer 2 apply (assumption)
   185 apply auto
   186 done
   187 
   188 lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
   189 apply (induct_tac "xs")
   190 apply auto
   191 done
   192 
   193 
   194 subsection {* option\_map related *}
   195 
   196 lemma option_map_o_empty[simp]: "option_map f o empty = empty"
   197 apply (rule ext)
   198 apply (simp (no_asm))
   199 done
   200 
   201 lemma option_map_o_map_upd[simp]:
   202  "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
   203 apply (rule ext)
   204 apply (simp (no_asm))
   205 done
   206 
   207 
   208 subsection {* ++ *}
   209 
   210 lemma override_empty[simp]: "m ++ empty = m"
   211 apply (unfold override_def)
   212 apply (simp (no_asm))
   213 done
   214 
   215 lemma empty_override[simp]: "empty ++ m = m"
   216 apply (unfold override_def)
   217 apply (rule ext)
   218 apply (simp split add: option.split)
   219 done
   220 
   221 lemma override_Some_iff [rule_format (no_asm)]: 
   222  "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
   223 apply (unfold override_def)
   224 apply (simp (no_asm) split add: option.split)
   225 done
   226 
   227 lemmas override_SomeD = override_Some_iff [THEN iffD1, standard]
   228 declare override_SomeD [dest!]
   229 
   230 lemma override_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
   231 apply (subst override_Some_iff)
   232 apply fast
   233 done
   234 
   235 lemma override_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
   236 apply (unfold override_def)
   237 apply (simp (no_asm) split add: option.split)
   238 done
   239 
   240 lemma override_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
   241 apply (unfold override_def)
   242 apply (rule ext)
   243 apply auto
   244 done
   245 
   246 lemma map_of_override[simp]: "map_of ys ++ map_of xs = map_of (xs@ys)"
   247 apply (unfold override_def)
   248 apply (rule sym)
   249 apply (induct_tac "xs")
   250 apply (simp (no_asm))
   251 apply (rule ext)
   252 apply (simp (no_asm_simp) split add: option.split)
   253 done
   254 
   255 declare fun_upd_apply [simp del]
   256 lemma finite_range_map_of_override: "finite (range f) ==> finite (range (f ++ map_of l))"
   257 apply (induct_tac "l")
   258 apply  auto
   259 apply (erule finite_range_updI)
   260 done
   261 declare fun_upd_apply [simp]
   262 
   263 
   264 subsection {* dom *}
   265 
   266 lemma domI: "m a = Some b ==> a : dom m"
   267 apply (unfold dom_def)
   268 apply auto
   269 done
   270 
   271 lemma domD: "a : dom m ==> ? b. m a = Some b"
   272 apply (unfold dom_def)
   273 apply auto
   274 done
   275 
   276 lemma domIff[iff]: "(a : dom m) = (m a ~= None)"
   277 apply (unfold dom_def)
   278 apply auto
   279 done
   280 declare domIff [simp del]
   281 
   282 lemma dom_empty[simp]: "dom empty = {}"
   283 apply (unfold dom_def)
   284 apply (simp (no_asm))
   285 done
   286 
   287 lemma dom_fun_upd[simp]:
   288  "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
   289 by (simp add:dom_def) blast
   290 (*
   291 lemma dom_map_upd[simp]: "dom(m(a|->b)) = insert a (dom m)"
   292 apply (unfold dom_def)
   293 apply (simp (no_asm))
   294 apply blast
   295 done
   296 *)
   297 
   298 lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}"
   299 apply(induct xys)
   300 apply(auto simp del:fun_upd_apply)
   301 done
   302 
   303 lemma finite_dom_map_of: "finite (dom (map_of l))"
   304 apply (unfold dom_def)
   305 apply (induct_tac "l")
   306 apply (auto simp add: insert_Collect [symmetric])
   307 done
   308 
   309 lemma dom_map_upds[simp]: "!!m vs. dom(m(xs[|->]vs)) = set xs Un dom m"
   310 by(induct xs, simp_all)
   311 
   312 lemma dom_override[simp]: "dom(m++n) = dom n Un dom m"
   313 apply (unfold dom_def)
   314 apply auto
   315 done
   316 
   317 lemma dom_overwrite[simp]:
   318  "dom(f(g|A)) = (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
   319 by(auto simp add: dom_def overwrite_def)
   320 
   321 subsection {* ran *}
   322 
   323 lemma ran_empty[simp]: "ran empty = {}"
   324 apply (unfold ran_def)
   325 apply (simp (no_asm))
   326 done
   327 
   328 lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
   329 apply (unfold ran_def)
   330 apply auto
   331 apply (subgoal_tac "~ (aa = a) ")
   332 apply auto
   333 done
   334 
   335 subsection {* map\_le *}
   336 
   337 lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"
   338 by(simp add:map_le_def)
   339 
   340 lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"
   341 by(fastsimp simp add:map_le_def)
   342 
   343 lemma map_le_upds[simp]:
   344  "!!f g bs. f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)"
   345 by(induct as, auto)
   346 
   347 end