src/HOL/Map.thy
author nipkow
Wed May 14 11:15:18 2003 +0200 (2003-05-14)
changeset 14027 68d247b7b14b
parent 14026 c031a330a03f
child 14033 bc723de8ec95
permissions -rw-r--r--
*** empty log message ***
     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 map_add:: "('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 map_add_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
    47 
    48 map_upds_def: "m(xs [|->] ys) == m ++ map_of (rev(zip xs ys))"
    49 
    50 dom_def: "dom(m) == {a. m a ~= None}"
    51 ran_def: "ran(m) == {b. EX a. m a = Some b}"
    52 
    53 map_le_def: "m1 \<subseteq>\<^sub>m m2  ==  ALL a : dom m1. m1 a = m2 a"
    54 
    55 primrec
    56   "map_of [] = empty"
    57   "map_of (p#ps) = (map_of ps)(fst p |-> snd p)"
    58 
    59 
    60 subsection {* empty *}
    61 
    62 lemma empty_upd_none[simp]: "empty(x := None) = empty"
    63 apply (rule ext)
    64 apply (simp (no_asm))
    65 done
    66 
    67 
    68 (* FIXME: what is this sum_case nonsense?? *)
    69 lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty"
    70 apply (rule ext)
    71 apply (simp (no_asm) split add: sum.split)
    72 done
    73 
    74 subsection {* map\_upd *}
    75 
    76 lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
    77 apply (rule ext)
    78 apply (simp (no_asm_simp))
    79 done
    80 
    81 lemma map_upd_nonempty[simp]: "t(k|->x) ~= empty"
    82 apply safe
    83 apply (drule_tac x = "k" in fun_cong)
    84 apply (simp (no_asm_use))
    85 done
    86 
    87 lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
    88 apply (unfold image_def)
    89 apply (simp (no_asm_use) add: full_SetCompr_eq)
    90 apply (rule finite_subset)
    91 prefer 2 apply (assumption)
    92 apply auto
    93 done
    94 
    95 
    96 (* FIXME: what is this sum_case nonsense?? *)
    97 subsection {* sum\_case and empty/map\_upd *}
    98 
    99 lemma sum_case_map_upd_empty[simp]:
   100  "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
   101 apply (rule ext)
   102 apply (simp (no_asm) split add: sum.split)
   103 done
   104 
   105 lemma sum_case_empty_map_upd[simp]:
   106  "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
   107 apply (rule ext)
   108 apply (simp (no_asm) split add: sum.split)
   109 done
   110 
   111 lemma sum_case_map_upd_map_upd[simp]:
   112  "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
   113 apply (rule ext)
   114 apply (simp (no_asm) split add: sum.split)
   115 done
   116 
   117 
   118 subsection {* chg\_map *}
   119 
   120 lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
   121 apply (unfold chg_map_def)
   122 apply auto
   123 done
   124 
   125 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   126 apply (unfold chg_map_def)
   127 apply auto
   128 done
   129 
   130 
   131 subsection {* map\_of *}
   132 
   133 lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs"
   134 apply (induct_tac "xs")
   135 apply  auto
   136 done
   137 
   138 lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x -->  
   139    map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
   140 apply (induct_tac "t")
   141 apply  (auto simp add: inj_eq)
   142 done
   143 
   144 lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)"
   145 apply (induct_tac "l")
   146 apply  auto
   147 done
   148 
   149 lemma map_of_filter_in: 
   150 "[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
   151 apply (rule mp)
   152 prefer 2 apply (assumption)
   153 apply (erule thin_rl)
   154 apply (induct_tac "xs")
   155 apply  auto
   156 done
   157 
   158 lemma finite_range_map_of: "finite (range (map_of l))"
   159 apply (induct_tac "l")
   160 apply  (simp_all (no_asm) add: image_constant)
   161 apply (rule finite_subset)
   162 prefer 2 apply (assumption)
   163 apply auto
   164 done
   165 
   166 lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
   167 apply (induct_tac "xs")
   168 apply auto
   169 done
   170 
   171 
   172 subsection {* option\_map related *}
   173 
   174 lemma option_map_o_empty[simp]: "option_map f o empty = empty"
   175 apply (rule ext)
   176 apply (simp (no_asm))
   177 done
   178 
   179 lemma option_map_o_map_upd[simp]:
   180  "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
   181 apply (rule ext)
   182 apply (simp (no_asm))
   183 done
   184 
   185 
   186 subsection {* ++ *}
   187 
   188 lemma map_add_empty[simp]: "m ++ empty = m"
   189 apply (unfold map_add_def)
   190 apply (simp (no_asm))
   191 done
   192 
   193 lemma empty_map_add[simp]: "empty ++ m = m"
   194 apply (unfold map_add_def)
   195 apply (rule ext)
   196 apply (simp split add: option.split)
   197 done
   198 
   199 lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3"
   200 apply(rule ext)
   201 apply(simp add: map_add_def split:option.split)
   202 done
   203 
   204 lemma map_add_Some_iff: 
   205  "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
   206 apply (unfold map_add_def)
   207 apply (simp (no_asm) split add: option.split)
   208 done
   209 
   210 lemmas map_add_SomeD = map_add_Some_iff [THEN iffD1, standard]
   211 declare map_add_SomeD [dest!]
   212 
   213 lemma map_add_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
   214 apply (subst map_add_Some_iff)
   215 apply fast
   216 done
   217 
   218 lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)"
   219 apply (unfold map_add_def)
   220 apply (simp (no_asm) split add: option.split)
   221 done
   222 
   223 lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)"
   224 apply (unfold map_add_def)
   225 apply (rule ext)
   226 apply auto
   227 done
   228 
   229 lemma map_of_append[simp]: "map_of (xs@ys) = map_of ys ++ map_of xs"
   230 apply (unfold map_add_def)
   231 apply (induct_tac "xs")
   232 apply (simp (no_asm))
   233 apply (rule ext)
   234 apply (simp (no_asm_simp) split add: option.split)
   235 done
   236 
   237 declare fun_upd_apply [simp del]
   238 lemma finite_range_map_of_map_add:
   239  "finite (range f) ==> finite (range (f ++ map_of l))"
   240 apply (induct_tac "l")
   241 apply  auto
   242 apply (erule finite_range_updI)
   243 done
   244 declare fun_upd_apply [simp]
   245 
   246 
   247 subsection {* map\_upds *}
   248 
   249 lemma map_upds_Nil1[simp]: "m([] [|->] bs) = m"
   250 by(simp add:map_upds_def)
   251 
   252 lemma map_upds_Nil2[simp]: "m(as [|->] []) = m"
   253 by(simp add:map_upds_def)
   254 
   255 lemma map_upds_Cons[simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)"
   256 by(simp add:map_upds_def)
   257 
   258 
   259 lemma map_upd_upds_conv_if: "!!x y ys f.
   260  (f(x|->y))(xs [|->] ys) =
   261  (if x : set(take (length ys) xs) then f(xs [|->] ys)
   262                                   else (f(xs [|->] ys))(x|->y))"
   263 apply(induct xs)
   264  apply simp
   265 apply(case_tac ys)
   266  apply(auto split:split_if simp:fun_upd_twist)
   267 done
   268 
   269 lemma map_upds_twist [simp]:
   270  "a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)"
   271 apply(insert set_take_subset)
   272 apply (fastsimp simp add: map_upd_upds_conv_if)
   273 done
   274 
   275 lemma map_upds_apply_nontin[simp]:
   276  "!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x"
   277 apply(induct xs)
   278  apply simp
   279 apply(case_tac ys)
   280  apply(auto simp: map_upd_upds_conv_if)
   281 done
   282 
   283 subsection {* dom *}
   284 
   285 lemma domI: "m a = Some b ==> a : dom m"
   286 apply (unfold dom_def)
   287 apply auto
   288 done
   289 
   290 lemma domD: "a : dom m ==> ? b. m a = Some b"
   291 apply (unfold dom_def)
   292 apply auto
   293 done
   294 
   295 lemma domIff[iff]: "(a : dom m) = (m a ~= None)"
   296 apply (unfold dom_def)
   297 apply auto
   298 done
   299 declare domIff [simp del]
   300 
   301 lemma dom_empty[simp]: "dom empty = {}"
   302 apply (unfold dom_def)
   303 apply (simp (no_asm))
   304 done
   305 
   306 lemma dom_fun_upd[simp]:
   307  "dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))"
   308 by (simp add:dom_def) blast
   309 
   310 lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}"
   311 apply(induct xys)
   312 apply(auto simp del:fun_upd_apply)
   313 done
   314 
   315 lemma finite_dom_map_of: "finite (dom (map_of l))"
   316 apply (unfold dom_def)
   317 apply (induct_tac "l")
   318 apply (auto simp add: insert_Collect [symmetric])
   319 done
   320 
   321 lemma dom_map_upds[simp]:
   322  "!!m ys. dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m"
   323 apply(induct xs)
   324  apply simp
   325 apply(case_tac ys)
   326  apply auto
   327 done
   328 
   329 lemma dom_map_add[simp]: "dom(m++n) = dom n Un dom m"
   330 apply (unfold dom_def)
   331 apply auto
   332 done
   333 
   334 lemma dom_overwrite[simp]:
   335  "dom(f(g|A)) = (dom f  - {a. a : A - dom g}) Un {a. a : A Int dom g}"
   336 by(auto simp add: dom_def overwrite_def)
   337 
   338 lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1"
   339 apply(rule ext)
   340 apply(fastsimp simp:map_add_def split:option.split)
   341 done
   342 
   343 subsection {* ran *}
   344 
   345 lemma ran_empty[simp]: "ran empty = {}"
   346 apply (unfold ran_def)
   347 apply (simp (no_asm))
   348 done
   349 
   350 lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
   351 apply (unfold ran_def)
   352 apply auto
   353 apply (subgoal_tac "~ (aa = a) ")
   354 apply auto
   355 done
   356 
   357 subsection {* map\_le *}
   358 
   359 lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g"
   360 by(simp add:map_le_def)
   361 
   362 lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)"
   363 by(fastsimp simp add:map_le_def)
   364 
   365 lemma map_le_upds[simp]:
   366  "!!f g bs. f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)"
   367 apply(induct as)
   368  apply simp
   369 apply(case_tac bs)
   370  apply auto
   371 done
   372 
   373 end