src/HOL/Library/AssocList.thy
changeset 19234 054332e39e0a
child 19323 ec5cd5b1804c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/AssocList.thy	Fri Mar 10 16:05:34 2006 +0100
     1.3 @@ -0,0 +1,862 @@
     1.4 +(*  Title:      HOL/Library/Library.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Norbert Schirmer, Tobias Nipkow, Martin Wildmoser
     1.7 +*)
     1.8 +
     1.9 +header {* Map operations implemented on association lists*}
    1.10 +
    1.11 +theory AssocList 
    1.12 +imports Map
    1.13 +
    1.14 +begin
    1.15 +
    1.16 +text {* The operations preserve distinctness of keys and 
    1.17 +        function @{term "clearjunk"} distributes over them.*}
    1.18 +consts 
    1.19 +  delete     :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    1.20 +  update     :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    1.21 +  updates    :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    1.22 +  substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    1.23 +  map_at     :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val) list"
    1.24 +  merge      :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    1.25 +  compose    :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
    1.26 +  restrict   :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    1.27 +
    1.28 +  clearjunk  :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
    1.29 +
    1.30 +defs
    1.31 +delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
    1.32 +
    1.33 +primrec
    1.34 +"update k v [] = [(k,v)]"
    1.35 +"update k v (p#ps) = (if fst p = k then (k,v)#ps else p # update k v ps)"
    1.36 +primrec
    1.37 +"updates [] vs al = al"
    1.38 +"updates (k#ks) vs al = (case vs of [] \<Rightarrow> al 
    1.39 +                         | (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
    1.40 +primrec
    1.41 +"substitute v v' [] = []"
    1.42 +"substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
    1.43 +                          else p#substitute v v' ps)"
    1.44 +primrec
    1.45 +"map_at f k [] = []"
    1.46 +"map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
    1.47 +primrec
    1.48 +"merge xs [] = xs"
    1.49 +"merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
    1.50 +
    1.51 +lemma length_delete_le: "length (delete k al) \<le> length al"
    1.52 +proof (induct al)
    1.53 +  case Nil thus ?case by (simp add: delete_def)
    1.54 +next
    1.55 +  case (Cons a al)
    1.56 +  note length_filter_le [of "\<lambda>p. fst p \<noteq> fst a" al] 
    1.57 +  also have "\<And>n. n \<le> Suc n"
    1.58 +    by simp
    1.59 +  finally have "length [p\<in>al . fst p \<noteq> fst a] \<le> Suc (length al)" .
    1.60 +  with Cons show ?case
    1.61 +    by (auto simp add: delete_def)
    1.62 +qed
    1.63 +
    1.64 +lemma compose_hint: "length (delete k al) < Suc (length al)"
    1.65 +proof -
    1.66 +  note length_delete_le
    1.67 +  also have "\<And>n. n < Suc n"
    1.68 +    by simp
    1.69 +  finally show ?thesis .
    1.70 +qed
    1.71 +
    1.72 +recdef compose "measure size"
    1.73 +"compose [] = (\<lambda>ys. [])"
    1.74 +"compose (x#xs) = (\<lambda>ys. (case (map_of ys (snd x)) of
    1.75 +                          None \<Rightarrow> compose (delete (fst x) xs) ys
    1.76 +                         | Some v \<Rightarrow> (fst x,v)#compose xs ys))"
    1.77 +(hints intro: compose_hint)
    1.78 +
    1.79 +defs  
    1.80 +restrict_def: "restrict A \<equiv> filter (\<lambda>(k,v). k \<in> A)"
    1.81 +
    1.82 +recdef clearjunk "measure size"
    1.83 +"clearjunk [] = []"
    1.84 +"clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
    1.85 +(hints intro: compose_hint)
    1.86 +
    1.87 +
    1.88 +(* ******************************************************************************** *)
    1.89 +subsection {* Lookup *}
    1.90 +(* ******************************************************************************** *)
    1.91 +
    1.92 +lemma lookup_simps: 
    1.93 +  "map_of [] k = None"
    1.94 +  "map_of (p#ps) k = (if fst p = k then Some (snd p) else map_of ps k)"
    1.95 +  by simp_all
    1.96 +
    1.97 +(* ******************************************************************************** *)
    1.98 +subsection {* @{const delete} *}
    1.99 +(* ******************************************************************************** *)
   1.100 +
   1.101 +lemma delete_simps [simp]:
   1.102 +"delete k [] = []"
   1.103 +"delete k (p#ps) = (if fst p = k then delete k ps else p # delete k ps)"
   1.104 +  by (simp_all add: delete_def)
   1.105 +
   1.106 +lemma delete_id[simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
   1.107 +by(induct al, auto)
   1.108 +
   1.109 +lemma delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
   1.110 +  by (induct al) auto
   1.111 +
   1.112 +lemma delete_conv': "map_of (delete k al) = ((map_of al)(k := None))"
   1.113 +  by (rule ext) (rule delete_conv)
   1.114 +
   1.115 +lemma delete_idem: "delete k (delete k al) = delete k al"
   1.116 +  by (induct al) auto
   1.117 +
   1.118 +lemma map_of_delete[simp]:
   1.119 + "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
   1.120 +by(induct al, auto)
   1.121 +
   1.122 +lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
   1.123 +  by (induct al) auto
   1.124 +
   1.125 +lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
   1.126 +  by (induct al) auto
   1.127 +
   1.128 +lemma distinct_delete:
   1.129 +  assumes "distinct (map fst al)" 
   1.130 +  shows "distinct (map fst (delete k al))"
   1.131 +using prems
   1.132 +proof (induct al)
   1.133 +  case Nil thus ?case by simp
   1.134 +next
   1.135 +  case (Cons a al)
   1.136 +  from Cons.prems obtain 
   1.137 +    a_notin_al: "fst a \<notin> fst ` set al" and
   1.138 +    dist_al: "distinct (map fst al)"
   1.139 +    by auto
   1.140 +  show ?case
   1.141 +  proof (cases "fst a = k")
   1.142 +    case True
   1.143 +    from True dist_al show ?thesis by simp
   1.144 +  next
   1.145 +    case False
   1.146 +    from dist_al
   1.147 +    have "distinct (map fst (delete k al))"
   1.148 +      by (rule Cons.hyps)
   1.149 +    moreover from a_notin_al dom_delete_subset [of k al] 
   1.150 +    have "fst a \<notin> fst ` set (delete k al)"
   1.151 +      by blast
   1.152 +    ultimately show ?thesis using False by simp
   1.153 +  qed
   1.154 +qed
   1.155 +
   1.156 +lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
   1.157 +  by (induct al) auto
   1.158 +
   1.159 +lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
   1.160 +  by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
   1.161 +
   1.162 +(* ******************************************************************************** *)
   1.163 +subsection {* @{const clearjunk} *}
   1.164 +(* ******************************************************************************** *)
   1.165 +
   1.166 +lemma insert_fst_filter: 
   1.167 +  "insert a(fst ` {x \<in> set ps. fst x \<noteq> a}) = insert a (fst ` set ps)"
   1.168 +  by (induct ps) auto
   1.169 +
   1.170 +lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
   1.171 +  by (induct al rule: clearjunk.induct) (simp_all add: insert_fst_filter delete_def)
   1.172 +
   1.173 +lemma notin_filter_fst: "a \<notin> fst ` {x \<in> set ps. fst x \<noteq> a}"
   1.174 +  by (induct ps) auto
   1.175 +
   1.176 +lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
   1.177 +  by (induct al rule: clearjunk.induct) 
   1.178 +     (simp_all add: dom_clearjunk notin_filter_fst delete_def)
   1.179 +
   1.180 +lemma map_of_filter: "k \<noteq> a \<Longrightarrow> map_of [q\<in>ps . fst q \<noteq> a] k = map_of ps k"
   1.181 +  by (induct ps) auto
   1.182 +
   1.183 +lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
   1.184 +  apply (rule ext)
   1.185 +  apply (induct al rule: clearjunk.induct)
   1.186 +  apply  simp
   1.187 +  apply (simp add: map_of_filter)
   1.188 +  done
   1.189 +
   1.190 +lemma length_clearjunk: "length (clearjunk al) \<le> length al"
   1.191 +proof (induct al rule: clearjunk.induct [case_names Nil Cons])
   1.192 +  case Nil thus ?case by simp
   1.193 +next
   1.194 +  case (Cons k v ps)
   1.195 +  from Cons have "length (clearjunk [q\<in>ps . fst q \<noteq> k]) \<le> length [q\<in>ps . fst q \<noteq> k]" 
   1.196 +    by (simp add: delete_def)
   1.197 +  also have "\<dots> \<le> length ps"
   1.198 +    by simp
   1.199 +  finally show ?case
   1.200 +    by (simp add: delete_def)
   1.201 +qed
   1.202 +
   1.203 +lemma notin_fst_filter: "a \<notin> fst ` set ps \<Longrightarrow> [q\<in>ps . fst q \<noteq> a] = ps"
   1.204 +  by (induct ps) auto
   1.205 +            
   1.206 +lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
   1.207 +  by (induct al rule: clearjunk.induct) (auto simp add: notin_fst_filter)
   1.208 +
   1.209 +lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
   1.210 +  by simp
   1.211 +
   1.212 +(* ******************************************************************************** *)
   1.213 +subsection {* @{const dom} and @{term "ran"} *}
   1.214 +(* ******************************************************************************** *)
   1.215 +
   1.216 +lemma dom_map_of': "fst ` set al = dom (map_of al)"
   1.217 +  by (induct al) auto
   1.218 +
   1.219 +lemmas dom_map_of = dom_map_of' [symmetric]
   1.220 +
   1.221 +lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
   1.222 +  by (simp add: map_of_clearjunk)
   1.223 +
   1.224 +lemma ran_distinct: 
   1.225 +  assumes dist: "distinct (map fst al)" 
   1.226 +  shows "ran (map_of al) = snd ` set al"
   1.227 +using dist
   1.228 +proof (induct al) 
   1.229 +  case Nil
   1.230 +  thus ?case by simp
   1.231 +next
   1.232 +  case (Cons a al)
   1.233 +  hence hyp: "snd ` set al = ran (map_of al)"
   1.234 +    by simp
   1.235 +
   1.236 +  have "ran (map_of (a # al)) = {snd a} \<union> ran (map_of al)"
   1.237 +  proof 
   1.238 +    show "ran (map_of (a # al)) \<subseteq> {snd a} \<union> ran (map_of al)"
   1.239 +    proof   
   1.240 +      fix v
   1.241 +      assume "v \<in> ran (map_of (a#al))"
   1.242 +      then obtain x where "map_of (a#al) x = Some v"
   1.243 +	by (auto simp add: ran_def)
   1.244 +      then show "v \<in> {snd a} \<union> ran (map_of al)"
   1.245 +	by (auto split: split_if_asm simp add: ran_def)
   1.246 +    qed
   1.247 +  next
   1.248 +    show "{snd a} \<union> ran (map_of al) \<subseteq> ran (map_of (a # al))"
   1.249 +    proof 
   1.250 +      fix v
   1.251 +      assume v_in: "v \<in> {snd a} \<union> ran (map_of al)"
   1.252 +      show "v \<in> ran (map_of (a#al))"
   1.253 +      proof (cases "v=snd a")
   1.254 +	case True
   1.255 +	with v_in show ?thesis
   1.256 +	  by (auto simp add: ran_def)
   1.257 +      next
   1.258 +	case False
   1.259 +	with v_in have "v \<in> ran (map_of al)" by auto
   1.260 +	then obtain x where al_x: "map_of al x = Some v"
   1.261 +	  by (auto simp add: ran_def)
   1.262 +	from map_of_SomeD [OF this]
   1.263 +	have "x \<in> fst ` set al"
   1.264 +	  by (force simp add: image_def)
   1.265 +	with Cons.prems have "x\<noteq>fst a"
   1.266 +	  by - (rule ccontr,simp)
   1.267 +	with al_x
   1.268 +	show ?thesis
   1.269 +	  by (auto simp add: ran_def)
   1.270 +      qed
   1.271 +    qed
   1.272 +  qed
   1.273 +  with hyp show ?case
   1.274 +    by (simp only:) auto
   1.275 +qed
   1.276 +
   1.277 +lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
   1.278 +proof -
   1.279 +  have "ran (map_of al) = ran (map_of (clearjunk al))"
   1.280 +    by (simp add: ran_clearjunk)
   1.281 +  also have "\<dots> = snd ` set (clearjunk al)"
   1.282 +    by (simp add: ran_distinct)
   1.283 +  finally show ?thesis .
   1.284 +qed
   1.285 +   
   1.286 +(* ******************************************************************************** *)
   1.287 +subsection {* @{const update} *}
   1.288 +(* ******************************************************************************** *)
   1.289 +
   1.290 +lemma update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
   1.291 +  by (induct al) auto
   1.292 +
   1.293 +lemma update_conv': "map_of (update k v al)  = ((map_of al)(k\<mapsto>v))"
   1.294 +  by (rule ext) (rule update_conv)
   1.295 +
   1.296 +lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
   1.297 +  by (induct al) auto
   1.298 +
   1.299 +lemma distinct_update:
   1.300 +  assumes "distinct (map fst al)" 
   1.301 +  shows "distinct (map fst (update k v al))"
   1.302 +using prems
   1.303 +proof (induct al)
   1.304 +  case Nil thus ?case by simp
   1.305 +next
   1.306 +  case (Cons a al)
   1.307 +  from Cons.prems obtain 
   1.308 +    a_notin_al: "fst a \<notin> fst ` set al" and
   1.309 +    dist_al: "distinct (map fst al)"
   1.310 +    by auto
   1.311 +  show ?case
   1.312 +  proof (cases "fst a = k")
   1.313 +    case True
   1.314 +    from True dist_al a_notin_al show ?thesis by simp
   1.315 +  next
   1.316 +    case False
   1.317 +    from dist_al
   1.318 +    have "distinct (map fst (update k v al))"
   1.319 +      by (rule Cons.hyps)
   1.320 +    with False a_notin_al show ?thesis by (simp add: dom_update)
   1.321 +  qed
   1.322 +qed
   1.323 +
   1.324 +lemma update_filter: 
   1.325 +  "a\<noteq>k \<Longrightarrow> update k v [q\<in>ps . fst q \<noteq> a] = [q\<in>update k v ps . fst q \<noteq> a]"
   1.326 +  by (induct ps) auto
   1.327 +
   1.328 +lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
   1.329 +  by (induct al rule: clearjunk.induct) (auto simp add: update_filter delete_def)
   1.330 +
   1.331 +lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
   1.332 +  by (induct al) auto
   1.333 +
   1.334 +lemma update_nonempty [simp]: "update k v al \<noteq> []"
   1.335 +  by (induct al) auto
   1.336 +
   1.337 +lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v=v'"
   1.338 +proof (induct al fixing: al') 
   1.339 +  case Nil thus ?case 
   1.340 +    by (cases al') (auto split: split_if_asm)
   1.341 +next
   1.342 +  case Cons thus ?case
   1.343 +    by (cases al') (auto split: split_if_asm)
   1.344 +qed
   1.345 +
   1.346 +lemma update_last [simp]: "update k v (update k v' al) = update k v al"
   1.347 +  by (induct al) auto
   1.348 +
   1.349 +text {* Note that the lists are not necessarily the same:
   1.350 +        @{term "update k v (update k' v' []) = [(k',v'),(k,v)]"} and 
   1.351 +        @{term "update k' v' (update k v []) = [(k,v),(k',v')]"}.*}
   1.352 +lemma update_swap: "k\<noteq>k' 
   1.353 +  \<Longrightarrow> map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
   1.354 +  by (auto simp add: update_conv' intro: ext)
   1.355 +
   1.356 +lemma update_Some_unfold: 
   1.357 +  "(map_of (update k v al) x = Some y) = 
   1.358 +     (x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y)"
   1.359 +  by (simp add: update_conv' map_upd_Some_unfold)
   1.360 +
   1.361 +lemma image_update[simp]: "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
   1.362 +  by (simp add: update_conv' image_map_upd)
   1.363 +
   1.364 +
   1.365 +(* ******************************************************************************** *)
   1.366 +subsection {* @{const updates} *}
   1.367 +(* ******************************************************************************** *)
   1.368 +
   1.369 +lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
   1.370 +proof (induct ks fixing: vs al)
   1.371 +  case Nil
   1.372 +  thus ?case by simp
   1.373 +next
   1.374 +  case (Cons k ks)
   1.375 +  show ?case
   1.376 +  proof (cases vs)
   1.377 +    case Nil
   1.378 +    with Cons show ?thesis by simp
   1.379 +  next
   1.380 +    case (Cons k ks')
   1.381 +    with Cons.hyps show ?thesis
   1.382 +      by (simp add: update_conv fun_upd_def)
   1.383 +  qed
   1.384 +qed
   1.385 +
   1.386 +lemma updates_conv': "map_of (updates ks vs al) = ((map_of al)(ks[\<mapsto>]vs))"
   1.387 +  by (rule ext) (rule updates_conv)
   1.388 +
   1.389 +lemma distinct_updates:
   1.390 +  assumes "distinct (map fst al)"
   1.391 +  shows "distinct (map fst (updates ks vs al))"
   1.392 +  using prems
   1.393 +by (induct ks fixing: vs al) (auto simp add: distinct_update split: list.splits)
   1.394 +
   1.395 +lemma clearjunk_updates:
   1.396 + "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
   1.397 +  by (induct ks fixing: vs al) (auto simp add: clearjunk_update split: list.splits)
   1.398 +
   1.399 +lemma updates_empty[simp]: "updates vs [] al = al"
   1.400 +  by (induct vs) auto 
   1.401 +
   1.402 +lemma updates_Cons: "updates (k#ks) (v#vs) al = updates ks vs (update k v al)"
   1.403 +  by simp
   1.404 +
   1.405 +lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
   1.406 +  updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
   1.407 +  by (induct ks fixing: vs al) (auto split: list.splits)
   1.408 +
   1.409 +lemma updates_list_update_drop[simp]:
   1.410 + "\<lbrakk>size ks \<le> i; i < size vs\<rbrakk>
   1.411 +   \<Longrightarrow> updates ks (vs[i:=v]) al = updates ks vs al"
   1.412 +  by (induct ks fixing: al vs i) (auto split:list.splits nat.splits)
   1.413 +
   1.414 +lemma update_updates_conv_if: "
   1.415 + map_of (updates xs ys (update x y al)) =
   1.416 + map_of (if x \<in>  set(take (length ys) xs) then updates xs ys al
   1.417 +                                  else (update x y (updates xs ys al)))"
   1.418 +  by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
   1.419 +
   1.420 +lemma updates_twist [simp]:
   1.421 + "k \<notin> set ks \<Longrightarrow> 
   1.422 +  map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
   1.423 +  by (simp add: updates_conv' update_conv' map_upds_twist)
   1.424 +
   1.425 +lemma updates_apply_notin[simp]:
   1.426 + "k \<notin> set ks ==> map_of (updates ks vs al) k = map_of al k"
   1.427 +  by (simp add: updates_conv)
   1.428 +
   1.429 +lemma updates_append_drop[simp]:
   1.430 +  "size xs = size ys \<Longrightarrow> updates (xs@zs) ys al = updates xs ys al"
   1.431 +  by (induct xs fixing: ys al) (auto split: list.splits)
   1.432 +
   1.433 +lemma updates_append2_drop[simp]:
   1.434 +  "size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
   1.435 +  by (induct xs fixing: ys al) (auto split: list.splits)
   1.436 +
   1.437 +
   1.438 +(* ******************************************************************************** *)
   1.439 +subsection {* @{const substitute} *}
   1.440 +(* ******************************************************************************** *)
   1.441 +
   1.442 +lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
   1.443 +  by (induct al) auto
   1.444 +
   1.445 +lemma substitute_conv': "map_of (substitute v v' al) = ((map_of al)(v ~> v'))"
   1.446 +  by (rule ext) (rule substitute_conv)
   1.447 +
   1.448 +lemma dom_substitute: "fst ` set (substitute v v' al) = fst ` set al"
   1.449 +  by (induct al) auto
   1.450 +
   1.451 +lemma distinct_substitute: 
   1.452 +  "distinct (map fst al) \<Longrightarrow> distinct (map fst (substitute v v' al))"
   1.453 +  by (induct al) (auto simp add: dom_substitute)
   1.454 +
   1.455 +lemma substitute_filter: 
   1.456 +  "(substitute v v' [q\<in>ps . fst q \<noteq> a]) = [q\<in>substitute v v' ps . fst q \<noteq> a]"
   1.457 +  by (induct ps) auto
   1.458 +
   1.459 +lemma clearjunk_substitute:
   1.460 + "clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
   1.461 +  by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
   1.462 +
   1.463 +(* ******************************************************************************** *)
   1.464 +subsection {* @{const map_at} *}
   1.465 +(* ******************************************************************************** *)
   1.466 +  
   1.467 +lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
   1.468 +  by (induct al) (auto simp add: chg_map_def split: option.splits)
   1.469 +
   1.470 +lemma map_at_conv': "map_of (map_at f k al) = (chg_map f k (map_of al))"
   1.471 +  by (rule ext) (rule map_at_conv)
   1.472 +
   1.473 +lemma dom_map_at: "fst ` set (map_at f k al) = fst ` set al"
   1.474 +  by (induct al) auto
   1.475 +
   1.476 +lemma distinct_map_at: 
   1.477 +  assumes "distinct (map fst al)"
   1.478 +  shows "distinct (map fst (map_at f k al))"
   1.479 +using prems by (induct al) (auto simp add: dom_map_at)
   1.480 +
   1.481 +lemma map_at_notin_filter: 
   1.482 +  "a \<noteq> k \<Longrightarrow> (map_at f k [q\<in>ps . fst q \<noteq> a]) = [q\<in>map_at f k ps . fst q \<noteq> a]"
   1.483 +  by (induct ps) auto
   1.484 +
   1.485 +lemma clearjunk_map_at:
   1.486 + "clearjunk (map_at f k al) = map_at f k (clearjunk al)"
   1.487 +  by (induct al rule: clearjunk.induct) (auto simp add: map_at_notin_filter delete_def)
   1.488 +
   1.489 +lemma map_at_new[simp]: "map_of al k = None \<Longrightarrow> map_at f k al = al"
   1.490 +  by (induct al) auto
   1.491 +
   1.492 +lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
   1.493 +  by (induct al) auto
   1.494 +
   1.495 +lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
   1.496 +  by (simp add: map_at_conv')
   1.497 +
   1.498 +(* ******************************************************************************** *)
   1.499 +subsection {* @{const merge} *}
   1.500 +(* ******************************************************************************** *)
   1.501 +
   1.502 +lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
   1.503 +  by (induct ys fixing: xs) (auto simp add: dom_update)
   1.504 +
   1.505 +lemma distinct_merge:
   1.506 +  assumes "distinct (map fst xs)"
   1.507 +  shows "distinct (map fst (merge xs ys))"
   1.508 +  using prems
   1.509 +by (induct ys fixing: xs) (auto simp add: dom_merge distinct_update)
   1.510 +
   1.511 +lemma clearjunk_merge:
   1.512 + "clearjunk (merge xs ys) = merge (clearjunk xs) ys"
   1.513 +  by (induct ys) (auto simp add: clearjunk_update)
   1.514 +
   1.515 +lemma merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
   1.516 +proof (induct ys)
   1.517 +  case Nil thus ?case by simp 
   1.518 +next
   1.519 +  case (Cons y ys)
   1.520 +  show ?case
   1.521 +  proof (cases "k = fst y")
   1.522 +    case True
   1.523 +    from True show ?thesis
   1.524 +      by (simp add: update_conv)
   1.525 +  next
   1.526 +    case False
   1.527 +    from False show ?thesis
   1.528 +      by (auto simp add: update_conv Cons.hyps map_add_def)
   1.529 +  qed
   1.530 +qed
   1.531 +
   1.532 +lemma merge_conv': "map_of (merge xs ys) = (map_of xs ++ map_of ys)"
   1.533 +  by (rule ext) (rule merge_conv)
   1.534 +
   1.535 +lemma merge_emty: "map_of (merge [] ys) = map_of ys"
   1.536 +  by (simp add: merge_conv')
   1.537 +
   1.538 +lemma merge_assoc[simp]: "map_of (merge m1 (merge m2 m3)) = 
   1.539 +                           map_of (merge (merge m1 m2) m3)"
   1.540 +  by (simp add: merge_conv')
   1.541 +
   1.542 +lemma merge_Some_iff: 
   1.543 + "(map_of (merge m n) k = Some x) = 
   1.544 +  (map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x)"
   1.545 +  by (simp add: merge_conv' map_add_Some_iff)
   1.546 +
   1.547 +lemmas merge_SomeD = merge_Some_iff [THEN iffD1, standard]
   1.548 +declare merge_SomeD [dest!]
   1.549 +
   1.550 +lemma merge_find_right[simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
   1.551 +  by (simp add: merge_conv')
   1.552 +
   1.553 +lemma merge_None [iff]: 
   1.554 +  "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
   1.555 +  by (simp add: merge_conv')
   1.556 +
   1.557 +lemma merge_upd[simp]: 
   1.558 +  "map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
   1.559 +  by (simp add: update_conv' merge_conv')
   1.560 +
   1.561 +lemma merge_updatess[simp]: 
   1.562 +  "map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
   1.563 +  by (simp add: updates_conv' merge_conv')
   1.564 +
   1.565 +lemma merge_append: "map_of (xs@ys) = map_of (merge ys xs)"
   1.566 +  by (simp add: merge_conv')
   1.567 +
   1.568 +(* ******************************************************************************** *)
   1.569 +subsection {* @{const compose} *}
   1.570 +(* ******************************************************************************** *)
   1.571 +
   1.572 +lemma compose_induct [case_names Nil Cons]: 
   1.573 +  assumes Nil: "P [] ys"
   1.574 +  assumes Cons: "\<And>x xs.
   1.575 +     \<lbrakk>\<And>v. map_of ys (snd x) = Some v \<Longrightarrow> P xs ys;
   1.576 +      map_of ys (snd x) = None \<Longrightarrow> P (delete (fst x) xs) ys\<rbrakk>
   1.577 +     \<Longrightarrow> P (x # xs) ys"
   1.578 +  shows "P xs ys"
   1.579 +apply (rule compose.induct [where ?P="\<lambda>xs. P xs ys"])
   1.580 +apply (rule Nil)
   1.581 +apply  (rule Cons)
   1.582 +apply (erule allE, erule allE, erule impE, assumption,assumption)
   1.583 +apply (erule allE, erule impE,assumption,assumption)
   1.584 +done
   1.585 +
   1.586 +lemma compose_first_None [simp]: 
   1.587 +  assumes "map_of xs k = None" 
   1.588 +  shows "map_of (compose xs ys) k = None"
   1.589 +using prems
   1.590 +by (induct xs ys rule: compose_induct) (auto split: option.splits split_if_asm)
   1.591 +
   1.592 +
   1.593 +lemma compose_conv: 
   1.594 +  shows "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
   1.595 +proof (induct xs ys rule: compose_induct )
   1.596 +  case Nil thus ?case by simp
   1.597 +next
   1.598 +  case (Cons x xs)
   1.599 +  show ?case
   1.600 +  proof (cases "map_of ys (snd x)")
   1.601 +    case None
   1.602 +    with Cons
   1.603 +    have hyp: "map_of (compose (delete (fst x) xs) ys) k =
   1.604 +               (map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
   1.605 +      by simp
   1.606 +    show ?thesis
   1.607 +    proof (cases "fst x = k")
   1.608 +      case True
   1.609 +      from True delete_notin_dom [of k xs]
   1.610 +      have "map_of (delete (fst x) xs) k = None"
   1.611 +	by (simp add: map_of_eq_None_iff)
   1.612 +      with hyp show ?thesis
   1.613 +	using True None
   1.614 +	by simp
   1.615 +    next
   1.616 +      case False
   1.617 +      from False have "map_of (delete (fst x) xs) k = map_of xs k"
   1.618 +	by simp
   1.619 +      with hyp show ?thesis
   1.620 +	using False None
   1.621 +	by (simp add: map_comp_def)
   1.622 +    qed
   1.623 +  next
   1.624 +    case (Some v)
   1.625 +    with Cons
   1.626 +    have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
   1.627 +      by simp
   1.628 +    with Some show ?thesis
   1.629 +      by (auto simp add: map_comp_def)
   1.630 +  qed
   1.631 +qed
   1.632 +   
   1.633 +lemma compose_conv': 
   1.634 +  shows "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
   1.635 +  by (rule ext) (rule compose_conv)
   1.636 +
   1.637 +lemma compose_first_Some [simp]:
   1.638 +  assumes "map_of xs k = Some v" 
   1.639 +  shows "map_of (compose xs ys) k = map_of ys v"
   1.640 +using prems by (simp add: compose_conv)
   1.641 +
   1.642 +lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
   1.643 +proof (induct xs ys rule: compose_induct )
   1.644 +  case Nil thus ?case by simp
   1.645 +next
   1.646 +  case (Cons x xs)
   1.647 +  show ?case
   1.648 +  proof (cases "map_of ys (snd x)")
   1.649 +    case None
   1.650 +    with Cons.hyps
   1.651 +    have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
   1.652 +      by simp
   1.653 +    also
   1.654 +    have "\<dots> \<subseteq> fst ` set xs"
   1.655 +      by (rule dom_delete_subset)
   1.656 +    finally show ?thesis
   1.657 +      using None
   1.658 +      by auto
   1.659 +  next
   1.660 +    case (Some v)
   1.661 +    with Cons.hyps
   1.662 +    have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
   1.663 +      by simp
   1.664 +    with Some show ?thesis
   1.665 +      by auto
   1.666 +  qed
   1.667 +qed
   1.668 +
   1.669 +lemma distinct_compose:
   1.670 + assumes "distinct (map fst xs)"
   1.671 + shows "distinct (map fst (compose xs ys))"
   1.672 +using prems
   1.673 +proof (induct xs ys rule: compose_induct)
   1.674 +  case Nil thus ?case by simp
   1.675 +next
   1.676 +  case (Cons x xs)
   1.677 +  show ?case
   1.678 +  proof (cases "map_of ys (snd x)")
   1.679 +    case None
   1.680 +    with Cons show ?thesis by simp
   1.681 +  next
   1.682 +    case (Some v)
   1.683 +    with Cons dom_compose [of xs ys] show ?thesis 
   1.684 +      by (auto)
   1.685 +  qed
   1.686 +qed
   1.687 +
   1.688 +lemma compose_delete_twist: "(compose (delete k xs) ys) = delete k (compose xs ys)"
   1.689 +proof (induct xs ys rule: compose_induct)
   1.690 +  case Nil thus ?case by simp
   1.691 +next
   1.692 +  case (Cons x xs)
   1.693 +  show ?case
   1.694 +  proof (cases "map_of ys (snd x)")
   1.695 +    case None
   1.696 +    with Cons have 
   1.697 +      hyp: "compose (delete k (delete (fst x) xs)) ys =
   1.698 +            delete k (compose (delete (fst x) xs) ys)"
   1.699 +      by simp
   1.700 +    show ?thesis
   1.701 +    proof (cases "fst x = k")
   1.702 +      case True
   1.703 +      with None hyp
   1.704 +      show ?thesis
   1.705 +	by (simp add: delete_idem)
   1.706 +    next
   1.707 +      case False
   1.708 +      from None False hyp
   1.709 +      show ?thesis
   1.710 +	by (simp add: delete_twist)
   1.711 +    qed
   1.712 +  next
   1.713 +    case (Some v)
   1.714 +    with Cons have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" by simp
   1.715 +    with Some show ?thesis
   1.716 +      by simp
   1.717 +  qed
   1.718 +qed
   1.719 +
   1.720 +lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
   1.721 +  by (induct xs ys rule: compose_induct) 
   1.722 +     (auto simp add: map_of_clearjunk split: option.splits)
   1.723 +   
   1.724 +lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
   1.725 +  by (induct xs rule: clearjunk.induct)
   1.726 +     (auto split: option.splits simp add: clearjunk_delete delete_idem
   1.727 +               compose_delete_twist)
   1.728 +   
   1.729 +lemma compose_empty [simp]:
   1.730 + "compose xs [] = []"
   1.731 +  by (induct xs rule: compose_induct [where ys="[]"]) auto
   1.732 +
   1.733 +
   1.734 +lemma compose_Some_iff:
   1.735 +  "(map_of (compose xs ys) k = Some v) = 
   1.736 +     (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" 
   1.737 +  by (simp add: compose_conv map_comp_Some_iff)
   1.738 +
   1.739 +lemma map_comp_None_iff:
   1.740 +  "(map_of (compose xs ys) k = None) = 
   1.741 +    (map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None)) " 
   1.742 +  by (simp add: compose_conv map_comp_None_iff)
   1.743 +
   1.744 +
   1.745 +(* ******************************************************************************** *)
   1.746 +subsection {* @{const restrict} *}
   1.747 +(* ******************************************************************************** *)
   1.748 +
   1.749 +lemma restrict_simps [simp]: 
   1.750 +  "restrict A [] = []"
   1.751 +  "restrict A (p#ps) = (if fst p \<in> A then p#restrict A ps else restrict A ps)"
   1.752 +  by (auto simp add: restrict_def)
   1.753 +
   1.754 +lemma distinct_restr: "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
   1.755 +  by (induct al) (auto simp add: restrict_def)
   1.756 +
   1.757 +lemma restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
   1.758 +  apply (induct al)
   1.759 +  apply  (simp add: restrict_def)
   1.760 +  apply (cases "k\<in>A")
   1.761 +  apply (auto simp add: restrict_def)
   1.762 +  done
   1.763 +
   1.764 +lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
   1.765 +  by (rule ext) (rule restr_conv)
   1.766 +
   1.767 +lemma restr_empty [simp]: 
   1.768 +  "restrict {} al = []" 
   1.769 +  "restrict A [] = []"
   1.770 +  by (induct al) (auto simp add: restrict_def)
   1.771 +
   1.772 +lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
   1.773 +  by (simp add: restr_conv')
   1.774 +
   1.775 +lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
   1.776 +  by (simp add: restr_conv')
   1.777 +
   1.778 +lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
   1.779 +  by (induct al) (auto simp add: restrict_def)
   1.780 +
   1.781 +lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
   1.782 +  by (induct al) (auto simp add: restrict_def)
   1.783 +
   1.784 +lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
   1.785 +  by (induct al) (auto simp add: restrict_def)
   1.786 +
   1.787 +lemma restr_update[simp]:
   1.788 + "map_of (restrict D (update x y al)) = 
   1.789 +  map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
   1.790 +  by (simp add: restr_conv' update_conv')
   1.791 +
   1.792 +lemma restr_delete [simp]:
   1.793 +  "(delete x (restrict D al)) = 
   1.794 +    (if x\<in> D then restrict (D - {x}) al else restrict D al)"
   1.795 +proof (induct al)
   1.796 +  case Nil thus ?case by simp
   1.797 +next
   1.798 +  case (Cons a al)
   1.799 +  show ?case
   1.800 +  proof (cases "x \<in> D")
   1.801 +    case True
   1.802 +    note x_D = this
   1.803 +    with Cons have hyp: "delete x (restrict D al) = restrict (D - {x}) al"
   1.804 +      by simp
   1.805 +    show ?thesis
   1.806 +    proof (cases "fst a = x")
   1.807 +      case True
   1.808 +      from Cons.hyps
   1.809 +      show ?thesis
   1.810 +	using x_D True
   1.811 +	by simp
   1.812 +    next
   1.813 +      case False
   1.814 +      note not_fst_a_x = this
   1.815 +      show ?thesis
   1.816 +      proof (cases "fst a \<in> D")
   1.817 +	case True 
   1.818 +	with not_fst_a_x 
   1.819 +	have "delete x (restrict D (a#al)) = a#(delete x (restrict D al))"
   1.820 +	  by (cases a) (simp add: restrict_def)
   1.821 +	also from not_fst_a_x True hyp have "\<dots> = restrict (D - {x}) (a # al)"
   1.822 +	  by (cases a) (simp add: restrict_def)
   1.823 +	finally show ?thesis
   1.824 +	  using x_D by simp
   1.825 +      next
   1.826 +	case False
   1.827 +	hence "delete x (restrict D (a#al)) = delete x (restrict D al)"
   1.828 +	  by (cases a) (simp add: restrict_def)
   1.829 +	moreover from False not_fst_a_x
   1.830 +	have "restrict (D - {x}) (a # al) = restrict (D - {x}) al"
   1.831 +	  by (cases a) (simp add: restrict_def)
   1.832 +	ultimately
   1.833 +	show ?thesis using x_D hyp by simp
   1.834 +      qed
   1.835 +    qed
   1.836 +  next
   1.837 +    case False
   1.838 +    from False Cons show ?thesis
   1.839 +      by simp
   1.840 +  qed
   1.841 +qed
   1.842 +
   1.843 +lemma update_restr:
   1.844 + "map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
   1.845 +  by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
   1.846 +
   1.847 +lemma upate_restr_conv[simp]:
   1.848 + "x \<in> D \<Longrightarrow> 
   1.849 + map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
   1.850 +  by (simp add: update_conv' restr_conv')
   1.851 +
   1.852 +lemma restr_updates[simp]: "
   1.853 + \<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>
   1.854 + \<Longrightarrow> map_of (restrict D (updates xs ys al)) = 
   1.855 +     map_of (updates xs ys (restrict (D - set xs) al))"
   1.856 +  by (simp add: updates_conv' restr_conv')
   1.857 +
   1.858 +lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
   1.859 +  by (induct ps) auto
   1.860 +
   1.861 +lemma clearjunk_restrict:
   1.862 + "clearjunk (restrict A al) = restrict A (clearjunk al)"
   1.863 +  by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
   1.864 +
   1.865 +end