src/HOL/Map.thy
changeset 13908 4bdfa9f77254
parent 13890 90611b4e0054
child 13909 a5247a49c85e
     1.1 --- a/src/HOL/Map.thy	Wed Apr 09 12:52:45 2003 +0200
     1.2 +++ b/src/HOL/Map.thy	Fri Apr 11 23:11:13 2003 +0200
     1.3 @@ -1,14 +1,14 @@
     1.4  (*  Title:      HOL/Map.thy
     1.5      ID:         $Id$
     1.6      Author:     Tobias Nipkow, based on a theory by David von Oheimb
     1.7 -    Copyright   1997 TU Muenchen
     1.8 +    Copyright   1997-2003 TU Muenchen
     1.9  
    1.10  The datatype of `maps' (written ~=>); strongly resembles maps in VDM.
    1.11  *)
    1.12  
    1.13 -Map = List +
    1.14 +theory Map = List:
    1.15  
    1.16 -types ('a,'b) "~=>" = 'a => 'b option (infixr 0)
    1.17 +types ('a,'b) "~=>" = "'a => 'b option" (infixr 0)
    1.18  
    1.19  consts
    1.20  chg_map	:: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
    1.21 @@ -24,11 +24,11 @@
    1.22  					         ("_/'(_/|->_')"   [900,0,0]900)
    1.23  
    1.24  syntax (xsymbols)
    1.25 -  "~=>"     :: [type, type] => type      (infixr "\\<leadsto>" 0)
    1.26 +  "~=>"     :: "[type, type] => type"    (infixr "\<leadsto>" 0)
    1.27    map_upd   :: "('a ~=> 'b) => 'a      => 'b      => ('a ~=> 'b)"
    1.28 -					  ("_/'(_/\\<mapsto>/_')"  [900,0,0]900)
    1.29 +					  ("_/'(_/\<mapsto>/_')"  [900,0,0]900)
    1.30    map_upds  :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)"
    1.31 -				         ("_/'(_/[\\<mapsto>]/_')" [900,0,0]900)
    1.32 +				         ("_/'(_/[\<mapsto>]/_')" [900,0,0]900)
    1.33  
    1.34  translations
    1.35    "empty"    => "_K None"
    1.36 @@ -38,12 +38,12 @@
    1.37  
    1.38  defs
    1.39  
    1.40 -chg_map_def  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
    1.41 +chg_map_def:  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
    1.42  
    1.43 -override_def "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
    1.44 +override_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y"
    1.45  
    1.46 -dom_def "dom(m) == {a. m a ~= None}"
    1.47 -ran_def "ran(m) == {b. ? a. m a = Some b}"
    1.48 +dom_def: "dom(m) == {a. m a ~= None}"
    1.49 +ran_def: "ran(m) == {b. ? a. m a = Some b}"
    1.50  
    1.51  primrec
    1.52    "map_of [] = empty"
    1.53 @@ -52,4 +52,279 @@
    1.54  primrec "t([]  [|->]bs) = t"
    1.55          "t(a#as[|->]bs) = t(a|->hd bs)(as[|->]tl bs)"
    1.56  
    1.57 +
    1.58 +section "empty"
    1.59 +
    1.60 +lemma empty_upd_none: "empty(x := None) = empty"
    1.61 +apply (rule ext)
    1.62 +apply (simp (no_asm))
    1.63 +done
    1.64 +declare empty_upd_none [simp]
    1.65 +
    1.66 +(* FIXME: what is this sum_case nonsense?? *)
    1.67 +lemma sum_case_empty_empty: "sum_case empty empty = empty"
    1.68 +apply (rule ext)
    1.69 +apply (simp (no_asm) split add: sum.split)
    1.70 +done
    1.71 +declare sum_case_empty_empty [simp]
    1.72 +
    1.73 +
    1.74 +section "map_upd"
    1.75 +
    1.76 +lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t"
    1.77 +apply (rule ext)
    1.78 +apply (simp (no_asm_simp))
    1.79 +done
    1.80 +
    1.81 +lemma map_upd_nonempty: "t(k|->x) ~= empty"
    1.82 +apply safe
    1.83 +apply (drule_tac x = "k" in fun_cong)
    1.84 +apply (simp (no_asm_use))
    1.85 +done
    1.86 +declare map_upd_nonempty [simp]
    1.87 +
    1.88 +lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))"
    1.89 +apply (unfold image_def)
    1.90 +apply (simp (no_asm_use) add: full_SetCompr_eq)
    1.91 +apply (rule finite_subset)
    1.92 +prefer 2 apply (assumption)
    1.93 +apply auto
    1.94 +done
    1.95 +
    1.96 +
    1.97 +(* FIXME: what is this sum_case nonsense?? *)
    1.98 +section "sum_case and empty/map_upd"
    1.99 +
   1.100 +lemma sum_case_map_upd_empty: "sum_case (m(k|->y)) empty =  (sum_case m empty)(Inl k|->y)"
   1.101 +apply (rule ext)
   1.102 +apply (simp (no_asm) split add: sum.split)
   1.103 +done
   1.104 +declare sum_case_map_upd_empty [simp]
   1.105 +
   1.106 +lemma sum_case_empty_map_upd: "sum_case empty (m(k|->y)) =  (sum_case empty m)(Inr k|->y)"
   1.107 +apply (rule ext)
   1.108 +apply (simp (no_asm) split add: sum.split)
   1.109 +done
   1.110 +declare sum_case_empty_map_upd [simp]
   1.111 +
   1.112 +lemma sum_case_map_upd_map_upd: "sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)"
   1.113 +apply (rule ext)
   1.114 +apply (simp (no_asm) split add: sum.split)
   1.115 +done
   1.116 +declare sum_case_map_upd_map_upd [simp]
   1.117 +
   1.118 +
   1.119 +section "map_upds"
   1.120 +
   1.121 +lemma map_upds_twist [rule_format (no_asm)]: "a ~: set as --> (!m bs. (m(a|->b)(as[|->]bs)) = (m(as[|->]bs)(a|->b)))"
   1.122 +apply (induct_tac "as")
   1.123 +apply  (auto simp del: fun_upd_apply)
   1.124 +apply (drule spec)+
   1.125 +apply (rotate_tac -1)
   1.126 +apply (erule subst)
   1.127 +apply (erule fun_upd_twist [THEN subst])
   1.128 +apply (rule refl)
   1.129 +done
   1.130 +declare map_upds_twist [simp]
   1.131 +
   1.132 +
   1.133 +section "chg_map"
   1.134 +
   1.135 +lemma chg_map_new: "m a = None   ==> chg_map f a m = m"
   1.136 +apply (unfold chg_map_def)
   1.137 +apply auto
   1.138 +done
   1.139 +
   1.140 +lemma chg_map_upd: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   1.141 +apply (unfold chg_map_def)
   1.142 +apply auto
   1.143 +done
   1.144 +
   1.145 +declare chg_map_new [simp] chg_map_upd [simp]
   1.146 +
   1.147 +
   1.148 +section "map_of"
   1.149 +
   1.150 +lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs"
   1.151 +apply (induct_tac "xs")
   1.152 +apply  auto
   1.153 +done
   1.154 +
   1.155 +lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x -->  
   1.156 +   map_of (map (split (%k. Pair (f k))) t) (f k) = Some x"
   1.157 +apply (induct_tac "t")
   1.158 +apply  (auto simp add: inj_eq)
   1.159 +done
   1.160 +
   1.161 +lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)"
   1.162 +apply (induct_tac "l")
   1.163 +apply  auto
   1.164 +done
   1.165 +
   1.166 +lemma map_of_filter_in: 
   1.167 +"[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z"
   1.168 +apply (rule mp)
   1.169 +prefer 2 apply (assumption)
   1.170 +apply (erule thin_rl)
   1.171 +apply (induct_tac "xs")
   1.172 +apply  auto
   1.173 +done
   1.174 +
   1.175 +lemma finite_range_map_of: "finite (range (map_of l))"
   1.176 +apply (induct_tac "l")
   1.177 +apply  (simp_all (no_asm) add: image_constant)
   1.178 +apply (rule finite_subset)
   1.179 +prefer 2 apply (assumption)
   1.180 +apply auto
   1.181 +done
   1.182 +
   1.183 +lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)"
   1.184 +apply (induct_tac "xs")
   1.185 +apply auto
   1.186 +done
   1.187 +
   1.188 +
   1.189 +section "option_map related"
   1.190 +
   1.191 +lemma option_map_o_empty: "option_map f o empty = empty"
   1.192 +apply (rule ext)
   1.193 +apply (simp (no_asm))
   1.194 +done
   1.195 +
   1.196 +lemma option_map_o_map_upd: "option_map f o m(a|->b) = (option_map f o m)(a|->f b)"
   1.197 +apply (rule ext)
   1.198 +apply (simp (no_asm))
   1.199 +done
   1.200 +
   1.201 +declare option_map_o_empty [simp] option_map_o_map_upd [simp]
   1.202 +
   1.203 +
   1.204 +section "++"
   1.205 +
   1.206 +lemma override_empty: "m ++ empty = m"
   1.207 +apply (unfold override_def)
   1.208 +apply (simp (no_asm))
   1.209 +done
   1.210 +declare override_empty [simp]
   1.211 +
   1.212 +lemma empty_override: "empty ++ m = m"
   1.213 +apply (unfold override_def)
   1.214 +apply (rule ext)
   1.215 +apply (simp split add: option.split)
   1.216 +done
   1.217 +declare empty_override [simp]
   1.218 +
   1.219 +lemma override_Some_iff [rule_format (no_asm)]: 
   1.220 + "((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)"
   1.221 +apply (unfold override_def)
   1.222 +apply (simp (no_asm) split add: option.split)
   1.223 +done
   1.224 +
   1.225 +lemmas override_SomeD = override_Some_iff [THEN iffD1, standard]
   1.226 +declare override_SomeD [dest!]
   1.227 +
   1.228 +lemma override_find_right: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx"
   1.229 +apply (subst override_Some_iff)
   1.230 +apply fast
   1.231 +done
   1.232 +declare override_find_right [simp]
   1.233 +
   1.234 +lemma override_None: "((m ++ n) k = None) = (n k = None & m k = None)"
   1.235 +apply (unfold override_def)
   1.236 +apply (simp (no_asm) split add: option.split)
   1.237 +done
   1.238 +declare override_None [iff]
   1.239 +
   1.240 +lemma override_upd: "f ++ g(x|->y) = (f ++ g)(x|->y)"
   1.241 +apply (unfold override_def)
   1.242 +apply (rule ext)
   1.243 +apply auto
   1.244 +done
   1.245 +declare override_upd [simp]
   1.246 +
   1.247 +lemma map_of_override: "map_of ys ++ map_of xs = map_of (xs@ys)"
   1.248 +apply (unfold override_def)
   1.249 +apply (rule sym)
   1.250 +apply (induct_tac "xs")
   1.251 +apply (simp (no_asm))
   1.252 +apply (rule ext)
   1.253 +apply (simp (no_asm_simp) split add: option.split)
   1.254 +done
   1.255 +declare map_of_override [simp]
   1.256 +
   1.257 +declare fun_upd_apply [simp del]
   1.258 +lemma finite_range_map_of_override: "finite (range f) ==> finite (range (f ++ map_of l))"
   1.259 +apply (induct_tac "l")
   1.260 +apply  auto
   1.261 +apply (erule finite_range_updI)
   1.262 +done
   1.263 +declare fun_upd_apply [simp]
   1.264 +
   1.265 +
   1.266 +section "dom"
   1.267 +
   1.268 +lemma domI: "m a = Some b ==> a : dom m"
   1.269 +apply (unfold dom_def)
   1.270 +apply auto
   1.271 +done
   1.272 +
   1.273 +lemma domD: "a : dom m ==> ? b. m a = Some b"
   1.274 +apply (unfold dom_def)
   1.275 +apply auto
   1.276 +done
   1.277 +
   1.278 +lemma domIff: "(a : dom m) = (m a ~= None)"
   1.279 +apply (unfold dom_def)
   1.280 +apply auto
   1.281 +done
   1.282 +declare domIff [iff]
   1.283 +declare domIff [simp del]
   1.284 +
   1.285 +lemma dom_empty: "dom empty = {}"
   1.286 +apply (unfold dom_def)
   1.287 +apply (simp (no_asm))
   1.288 +done
   1.289 +declare dom_empty [simp]
   1.290 +
   1.291 +lemma dom_map_upd: "dom(m(a|->b)) = insert a (dom m)"
   1.292 +apply (unfold dom_def)
   1.293 +apply (simp (no_asm))
   1.294 +apply blast
   1.295 +done
   1.296 +declare dom_map_upd [simp]
   1.297 +
   1.298 +lemma finite_dom_map_of: "finite (dom (map_of l))"
   1.299 +apply (unfold dom_def)
   1.300 +apply (induct_tac "l")
   1.301 +apply (auto simp add: insert_Collect [symmetric])
   1.302 +done
   1.303 +
   1.304 +lemma dom_override: "dom(m++n) = dom n Un dom m"
   1.305 +apply (unfold dom_def)
   1.306 +apply auto
   1.307 +done
   1.308 +declare dom_override [simp]
   1.309 +
   1.310 +section "ran"
   1.311 +
   1.312 +lemma ran_empty: "ran empty = {}"
   1.313 +apply (unfold ran_def)
   1.314 +apply (simp (no_asm))
   1.315 +done
   1.316 +declare ran_empty [simp]
   1.317 +
   1.318 +lemma ran_empty': "ran (%u. None) = {}"
   1.319 +apply (unfold ran_def)
   1.320 +apply auto
   1.321 +done
   1.322 +declare ran_empty' [simp]
   1.323 +
   1.324 +lemma ran_map_upd: "m a = None ==> ran(m(a|->b)) = insert b (ran m)"
   1.325 +apply (unfold ran_def)
   1.326 +apply auto
   1.327 +apply (subgoal_tac "~ (aa = a) ")
   1.328 +apply auto
   1.329 +done
   1.330 +declare ran_map_upd [simp]
   1.331 +
   1.332  end