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