Added Library/AssocList.thy
authorschirmer
Fri Mar 10 16:05:34 2006 +0100 (2006-03-10)
changeset 19234054332e39e0a
parent 19233 77ca20b0ed77
child 19235 868129805da8
Added Library/AssocList.thy
src/HOL/IsaMakefile
src/HOL/Library/AssocList.thy
src/HOL/Library/Library.thy
     1.1 --- a/src/HOL/IsaMakefile	Fri Mar 10 15:33:48 2006 +0100
     1.2 +++ b/src/HOL/IsaMakefile	Fri Mar 10 16:05:34 2006 +0100
     1.3 @@ -196,7 +196,7 @@
     1.4    Library/Library/document/root.bib Library/While_Combinator.thy \
     1.5    Library/Product_ord.thy Library/Char_ord.thy \
     1.6    Library/List_lexord.thy Library/Commutative_Ring.thy Library/comm_ring.ML \
     1.7 -  Library/Coinductive_List.thy
     1.8 +  Library/Coinductive_List.thy Library/AssocList.thy
     1.9  	@cd Library; $(ISATOOL) usedir $(OUT)/HOL Library
    1.10  
    1.11  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Library/AssocList.thy	Fri Mar 10 16:05:34 2006 +0100
     2.3 @@ -0,0 +1,862 @@
     2.4 +(*  Title:      HOL/Library/Library.thy
     2.5 +    ID:         $Id$
     2.6 +    Author:     Norbert Schirmer, Tobias Nipkow, Martin Wildmoser
     2.7 +*)
     2.8 +
     2.9 +header {* Map operations implemented on association lists*}
    2.10 +
    2.11 +theory AssocList 
    2.12 +imports Map
    2.13 +
    2.14 +begin
    2.15 +
    2.16 +text {* The operations preserve distinctness of keys and 
    2.17 +        function @{term "clearjunk"} distributes over them.*}
    2.18 +consts 
    2.19 +  delete     :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    2.20 +  update     :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    2.21 +  updates    :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    2.22 +  substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
    2.23 +  map_at     :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val) list"
    2.24 +  merge      :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    2.25 +  compose    :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
    2.26 +  restrict   :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
    2.27 +
    2.28 +  clearjunk  :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
    2.29 +
    2.30 +defs
    2.31 +delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
    2.32 +
    2.33 +primrec
    2.34 +"update k v [] = [(k,v)]"
    2.35 +"update k v (p#ps) = (if fst p = k then (k,v)#ps else p # update k v ps)"
    2.36 +primrec
    2.37 +"updates [] vs al = al"
    2.38 +"updates (k#ks) vs al = (case vs of [] \<Rightarrow> al 
    2.39 +                         | (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
    2.40 +primrec
    2.41 +"substitute v v' [] = []"
    2.42 +"substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
    2.43 +                          else p#substitute v v' ps)"
    2.44 +primrec
    2.45 +"map_at f k [] = []"
    2.46 +"map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
    2.47 +primrec
    2.48 +"merge xs [] = xs"
    2.49 +"merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
    2.50 +
    2.51 +lemma length_delete_le: "length (delete k al) \<le> length al"
    2.52 +proof (induct al)
    2.53 +  case Nil thus ?case by (simp add: delete_def)
    2.54 +next
    2.55 +  case (Cons a al)
    2.56 +  note length_filter_le [of "\<lambda>p. fst p \<noteq> fst a" al] 
    2.57 +  also have "\<And>n. n \<le> Suc n"
    2.58 +    by simp
    2.59 +  finally have "length [p\<in>al . fst p \<noteq> fst a] \<le> Suc (length al)" .
    2.60 +  with Cons show ?case
    2.61 +    by (auto simp add: delete_def)
    2.62 +qed
    2.63 +
    2.64 +lemma compose_hint: "length (delete k al) < Suc (length al)"
    2.65 +proof -
    2.66 +  note length_delete_le
    2.67 +  also have "\<And>n. n < Suc n"
    2.68 +    by simp
    2.69 +  finally show ?thesis .
    2.70 +qed
    2.71 +
    2.72 +recdef compose "measure size"
    2.73 +"compose [] = (\<lambda>ys. [])"
    2.74 +"compose (x#xs) = (\<lambda>ys. (case (map_of ys (snd x)) of
    2.75 +                          None \<Rightarrow> compose (delete (fst x) xs) ys
    2.76 +                         | Some v \<Rightarrow> (fst x,v)#compose xs ys))"
    2.77 +(hints intro: compose_hint)
    2.78 +
    2.79 +defs  
    2.80 +restrict_def: "restrict A \<equiv> filter (\<lambda>(k,v). k \<in> A)"
    2.81 +
    2.82 +recdef clearjunk "measure size"
    2.83 +"clearjunk [] = []"
    2.84 +"clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
    2.85 +(hints intro: compose_hint)
    2.86 +
    2.87 +
    2.88 +(* ******************************************************************************** *)
    2.89 +subsection {* Lookup *}
    2.90 +(* ******************************************************************************** *)
    2.91 +
    2.92 +lemma lookup_simps: 
    2.93 +  "map_of [] k = None"
    2.94 +  "map_of (p#ps) k = (if fst p = k then Some (snd p) else map_of ps k)"
    2.95 +  by simp_all
    2.96 +
    2.97 +(* ******************************************************************************** *)
    2.98 +subsection {* @{const delete} *}
    2.99 +(* ******************************************************************************** *)
   2.100 +
   2.101 +lemma delete_simps [simp]:
   2.102 +"delete k [] = []"
   2.103 +"delete k (p#ps) = (if fst p = k then delete k ps else p # delete k ps)"
   2.104 +  by (simp_all add: delete_def)
   2.105 +
   2.106 +lemma delete_id[simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
   2.107 +by(induct al, auto)
   2.108 +
   2.109 +lemma delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
   2.110 +  by (induct al) auto
   2.111 +
   2.112 +lemma delete_conv': "map_of (delete k al) = ((map_of al)(k := None))"
   2.113 +  by (rule ext) (rule delete_conv)
   2.114 +
   2.115 +lemma delete_idem: "delete k (delete k al) = delete k al"
   2.116 +  by (induct al) auto
   2.117 +
   2.118 +lemma map_of_delete[simp]:
   2.119 + "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
   2.120 +by(induct al, auto)
   2.121 +
   2.122 +lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
   2.123 +  by (induct al) auto
   2.124 +
   2.125 +lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
   2.126 +  by (induct al) auto
   2.127 +
   2.128 +lemma distinct_delete:
   2.129 +  assumes "distinct (map fst al)" 
   2.130 +  shows "distinct (map fst (delete k al))"
   2.131 +using prems
   2.132 +proof (induct al)
   2.133 +  case Nil thus ?case by simp
   2.134 +next
   2.135 +  case (Cons a al)
   2.136 +  from Cons.prems obtain 
   2.137 +    a_notin_al: "fst a \<notin> fst ` set al" and
   2.138 +    dist_al: "distinct (map fst al)"
   2.139 +    by auto
   2.140 +  show ?case
   2.141 +  proof (cases "fst a = k")
   2.142 +    case True
   2.143 +    from True dist_al show ?thesis by simp
   2.144 +  next
   2.145 +    case False
   2.146 +    from dist_al
   2.147 +    have "distinct (map fst (delete k al))"
   2.148 +      by (rule Cons.hyps)
   2.149 +    moreover from a_notin_al dom_delete_subset [of k al] 
   2.150 +    have "fst a \<notin> fst ` set (delete k al)"
   2.151 +      by blast
   2.152 +    ultimately show ?thesis using False by simp
   2.153 +  qed
   2.154 +qed
   2.155 +
   2.156 +lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
   2.157 +  by (induct al) auto
   2.158 +
   2.159 +lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
   2.160 +  by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
   2.161 +
   2.162 +(* ******************************************************************************** *)
   2.163 +subsection {* @{const clearjunk} *}
   2.164 +(* ******************************************************************************** *)
   2.165 +
   2.166 +lemma insert_fst_filter: 
   2.167 +  "insert a(fst ` {x \<in> set ps. fst x \<noteq> a}) = insert a (fst ` set ps)"
   2.168 +  by (induct ps) auto
   2.169 +
   2.170 +lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
   2.171 +  by (induct al rule: clearjunk.induct) (simp_all add: insert_fst_filter delete_def)
   2.172 +
   2.173 +lemma notin_filter_fst: "a \<notin> fst ` {x \<in> set ps. fst x \<noteq> a}"
   2.174 +  by (induct ps) auto
   2.175 +
   2.176 +lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
   2.177 +  by (induct al rule: clearjunk.induct) 
   2.178 +     (simp_all add: dom_clearjunk notin_filter_fst delete_def)
   2.179 +
   2.180 +lemma map_of_filter: "k \<noteq> a \<Longrightarrow> map_of [q\<in>ps . fst q \<noteq> a] k = map_of ps k"
   2.181 +  by (induct ps) auto
   2.182 +
   2.183 +lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
   2.184 +  apply (rule ext)
   2.185 +  apply (induct al rule: clearjunk.induct)
   2.186 +  apply  simp
   2.187 +  apply (simp add: map_of_filter)
   2.188 +  done
   2.189 +
   2.190 +lemma length_clearjunk: "length (clearjunk al) \<le> length al"
   2.191 +proof (induct al rule: clearjunk.induct [case_names Nil Cons])
   2.192 +  case Nil thus ?case by simp
   2.193 +next
   2.194 +  case (Cons k v ps)
   2.195 +  from Cons have "length (clearjunk [q\<in>ps . fst q \<noteq> k]) \<le> length [q\<in>ps . fst q \<noteq> k]" 
   2.196 +    by (simp add: delete_def)
   2.197 +  also have "\<dots> \<le> length ps"
   2.198 +    by simp
   2.199 +  finally show ?case
   2.200 +    by (simp add: delete_def)
   2.201 +qed
   2.202 +
   2.203 +lemma notin_fst_filter: "a \<notin> fst ` set ps \<Longrightarrow> [q\<in>ps . fst q \<noteq> a] = ps"
   2.204 +  by (induct ps) auto
   2.205 +            
   2.206 +lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
   2.207 +  by (induct al rule: clearjunk.induct) (auto simp add: notin_fst_filter)
   2.208 +
   2.209 +lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
   2.210 +  by simp
   2.211 +
   2.212 +(* ******************************************************************************** *)
   2.213 +subsection {* @{const dom} and @{term "ran"} *}
   2.214 +(* ******************************************************************************** *)
   2.215 +
   2.216 +lemma dom_map_of': "fst ` set al = dom (map_of al)"
   2.217 +  by (induct al) auto
   2.218 +
   2.219 +lemmas dom_map_of = dom_map_of' [symmetric]
   2.220 +
   2.221 +lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
   2.222 +  by (simp add: map_of_clearjunk)
   2.223 +
   2.224 +lemma ran_distinct: 
   2.225 +  assumes dist: "distinct (map fst al)" 
   2.226 +  shows "ran (map_of al) = snd ` set al"
   2.227 +using dist
   2.228 +proof (induct al) 
   2.229 +  case Nil
   2.230 +  thus ?case by simp
   2.231 +next
   2.232 +  case (Cons a al)
   2.233 +  hence hyp: "snd ` set al = ran (map_of al)"
   2.234 +    by simp
   2.235 +
   2.236 +  have "ran (map_of (a # al)) = {snd a} \<union> ran (map_of al)"
   2.237 +  proof 
   2.238 +    show "ran (map_of (a # al)) \<subseteq> {snd a} \<union> ran (map_of al)"
   2.239 +    proof   
   2.240 +      fix v
   2.241 +      assume "v \<in> ran (map_of (a#al))"
   2.242 +      then obtain x where "map_of (a#al) x = Some v"
   2.243 +	by (auto simp add: ran_def)
   2.244 +      then show "v \<in> {snd a} \<union> ran (map_of al)"
   2.245 +	by (auto split: split_if_asm simp add: ran_def)
   2.246 +    qed
   2.247 +  next
   2.248 +    show "{snd a} \<union> ran (map_of al) \<subseteq> ran (map_of (a # al))"
   2.249 +    proof 
   2.250 +      fix v
   2.251 +      assume v_in: "v \<in> {snd a} \<union> ran (map_of al)"
   2.252 +      show "v \<in> ran (map_of (a#al))"
   2.253 +      proof (cases "v=snd a")
   2.254 +	case True
   2.255 +	with v_in show ?thesis
   2.256 +	  by (auto simp add: ran_def)
   2.257 +      next
   2.258 +	case False
   2.259 +	with v_in have "v \<in> ran (map_of al)" by auto
   2.260 +	then obtain x where al_x: "map_of al x = Some v"
   2.261 +	  by (auto simp add: ran_def)
   2.262 +	from map_of_SomeD [OF this]
   2.263 +	have "x \<in> fst ` set al"
   2.264 +	  by (force simp add: image_def)
   2.265 +	with Cons.prems have "x\<noteq>fst a"
   2.266 +	  by - (rule ccontr,simp)
   2.267 +	with al_x
   2.268 +	show ?thesis
   2.269 +	  by (auto simp add: ran_def)
   2.270 +      qed
   2.271 +    qed
   2.272 +  qed
   2.273 +  with hyp show ?case
   2.274 +    by (simp only:) auto
   2.275 +qed
   2.276 +
   2.277 +lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
   2.278 +proof -
   2.279 +  have "ran (map_of al) = ran (map_of (clearjunk al))"
   2.280 +    by (simp add: ran_clearjunk)
   2.281 +  also have "\<dots> = snd ` set (clearjunk al)"
   2.282 +    by (simp add: ran_distinct)
   2.283 +  finally show ?thesis .
   2.284 +qed
   2.285 +   
   2.286 +(* ******************************************************************************** *)
   2.287 +subsection {* @{const update} *}
   2.288 +(* ******************************************************************************** *)
   2.289 +
   2.290 +lemma update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
   2.291 +  by (induct al) auto
   2.292 +
   2.293 +lemma update_conv': "map_of (update k v al)  = ((map_of al)(k\<mapsto>v))"
   2.294 +  by (rule ext) (rule update_conv)
   2.295 +
   2.296 +lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
   2.297 +  by (induct al) auto
   2.298 +
   2.299 +lemma distinct_update:
   2.300 +  assumes "distinct (map fst al)" 
   2.301 +  shows "distinct (map fst (update k v al))"
   2.302 +using prems
   2.303 +proof (induct al)
   2.304 +  case Nil thus ?case by simp
   2.305 +next
   2.306 +  case (Cons a al)
   2.307 +  from Cons.prems obtain 
   2.308 +    a_notin_al: "fst a \<notin> fst ` set al" and
   2.309 +    dist_al: "distinct (map fst al)"
   2.310 +    by auto
   2.311 +  show ?case
   2.312 +  proof (cases "fst a = k")
   2.313 +    case True
   2.314 +    from True dist_al a_notin_al show ?thesis by simp
   2.315 +  next
   2.316 +    case False
   2.317 +    from dist_al
   2.318 +    have "distinct (map fst (update k v al))"
   2.319 +      by (rule Cons.hyps)
   2.320 +    with False a_notin_al show ?thesis by (simp add: dom_update)
   2.321 +  qed
   2.322 +qed
   2.323 +
   2.324 +lemma update_filter: 
   2.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]"
   2.326 +  by (induct ps) auto
   2.327 +
   2.328 +lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
   2.329 +  by (induct al rule: clearjunk.induct) (auto simp add: update_filter delete_def)
   2.330 +
   2.331 +lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
   2.332 +  by (induct al) auto
   2.333 +
   2.334 +lemma update_nonempty [simp]: "update k v al \<noteq> []"
   2.335 +  by (induct al) auto
   2.336 +
   2.337 +lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v=v'"
   2.338 +proof (induct al fixing: al') 
   2.339 +  case Nil thus ?case 
   2.340 +    by (cases al') (auto split: split_if_asm)
   2.341 +next
   2.342 +  case Cons thus ?case
   2.343 +    by (cases al') (auto split: split_if_asm)
   2.344 +qed
   2.345 +
   2.346 +lemma update_last [simp]: "update k v (update k v' al) = update k v al"
   2.347 +  by (induct al) auto
   2.348 +
   2.349 +text {* Note that the lists are not necessarily the same:
   2.350 +        @{term "update k v (update k' v' []) = [(k',v'),(k,v)]"} and 
   2.351 +        @{term "update k' v' (update k v []) = [(k,v),(k',v')]"}.*}
   2.352 +lemma update_swap: "k\<noteq>k' 
   2.353 +  \<Longrightarrow> map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
   2.354 +  by (auto simp add: update_conv' intro: ext)
   2.355 +
   2.356 +lemma update_Some_unfold: 
   2.357 +  "(map_of (update k v al) x = Some y) = 
   2.358 +     (x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y)"
   2.359 +  by (simp add: update_conv' map_upd_Some_unfold)
   2.360 +
   2.361 +lemma image_update[simp]: "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
   2.362 +  by (simp add: update_conv' image_map_upd)
   2.363 +
   2.364 +
   2.365 +(* ******************************************************************************** *)
   2.366 +subsection {* @{const updates} *}
   2.367 +(* ******************************************************************************** *)
   2.368 +
   2.369 +lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
   2.370 +proof (induct ks fixing: vs al)
   2.371 +  case Nil
   2.372 +  thus ?case by simp
   2.373 +next
   2.374 +  case (Cons k ks)
   2.375 +  show ?case
   2.376 +  proof (cases vs)
   2.377 +    case Nil
   2.378 +    with Cons show ?thesis by simp
   2.379 +  next
   2.380 +    case (Cons k ks')
   2.381 +    with Cons.hyps show ?thesis
   2.382 +      by (simp add: update_conv fun_upd_def)
   2.383 +  qed
   2.384 +qed
   2.385 +
   2.386 +lemma updates_conv': "map_of (updates ks vs al) = ((map_of al)(ks[\<mapsto>]vs))"
   2.387 +  by (rule ext) (rule updates_conv)
   2.388 +
   2.389 +lemma distinct_updates:
   2.390 +  assumes "distinct (map fst al)"
   2.391 +  shows "distinct (map fst (updates ks vs al))"
   2.392 +  using prems
   2.393 +by (induct ks fixing: vs al) (auto simp add: distinct_update split: list.splits)
   2.394 +
   2.395 +lemma clearjunk_updates:
   2.396 + "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
   2.397 +  by (induct ks fixing: vs al) (auto simp add: clearjunk_update split: list.splits)
   2.398 +
   2.399 +lemma updates_empty[simp]: "updates vs [] al = al"
   2.400 +  by (induct vs) auto 
   2.401 +
   2.402 +lemma updates_Cons: "updates (k#ks) (v#vs) al = updates ks vs (update k v al)"
   2.403 +  by simp
   2.404 +
   2.405 +lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
   2.406 +  updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
   2.407 +  by (induct ks fixing: vs al) (auto split: list.splits)
   2.408 +
   2.409 +lemma updates_list_update_drop[simp]:
   2.410 + "\<lbrakk>size ks \<le> i; i < size vs\<rbrakk>
   2.411 +   \<Longrightarrow> updates ks (vs[i:=v]) al = updates ks vs al"
   2.412 +  by (induct ks fixing: al vs i) (auto split:list.splits nat.splits)
   2.413 +
   2.414 +lemma update_updates_conv_if: "
   2.415 + map_of (updates xs ys (update x y al)) =
   2.416 + map_of (if x \<in>  set(take (length ys) xs) then updates xs ys al
   2.417 +                                  else (update x y (updates xs ys al)))"
   2.418 +  by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
   2.419 +
   2.420 +lemma updates_twist [simp]:
   2.421 + "k \<notin> set ks \<Longrightarrow> 
   2.422 +  map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
   2.423 +  by (simp add: updates_conv' update_conv' map_upds_twist)
   2.424 +
   2.425 +lemma updates_apply_notin[simp]:
   2.426 + "k \<notin> set ks ==> map_of (updates ks vs al) k = map_of al k"
   2.427 +  by (simp add: updates_conv)
   2.428 +
   2.429 +lemma updates_append_drop[simp]:
   2.430 +  "size xs = size ys \<Longrightarrow> updates (xs@zs) ys al = updates xs ys al"
   2.431 +  by (induct xs fixing: ys al) (auto split: list.splits)
   2.432 +
   2.433 +lemma updates_append2_drop[simp]:
   2.434 +  "size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
   2.435 +  by (induct xs fixing: ys al) (auto split: list.splits)
   2.436 +
   2.437 +
   2.438 +(* ******************************************************************************** *)
   2.439 +subsection {* @{const substitute} *}
   2.440 +(* ******************************************************************************** *)
   2.441 +
   2.442 +lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
   2.443 +  by (induct al) auto
   2.444 +
   2.445 +lemma substitute_conv': "map_of (substitute v v' al) = ((map_of al)(v ~> v'))"
   2.446 +  by (rule ext) (rule substitute_conv)
   2.447 +
   2.448 +lemma dom_substitute: "fst ` set (substitute v v' al) = fst ` set al"
   2.449 +  by (induct al) auto
   2.450 +
   2.451 +lemma distinct_substitute: 
   2.452 +  "distinct (map fst al) \<Longrightarrow> distinct (map fst (substitute v v' al))"
   2.453 +  by (induct al) (auto simp add: dom_substitute)
   2.454 +
   2.455 +lemma substitute_filter: 
   2.456 +  "(substitute v v' [q\<in>ps . fst q \<noteq> a]) = [q\<in>substitute v v' ps . fst q \<noteq> a]"
   2.457 +  by (induct ps) auto
   2.458 +
   2.459 +lemma clearjunk_substitute:
   2.460 + "clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
   2.461 +  by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
   2.462 +
   2.463 +(* ******************************************************************************** *)
   2.464 +subsection {* @{const map_at} *}
   2.465 +(* ******************************************************************************** *)
   2.466 +  
   2.467 +lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
   2.468 +  by (induct al) (auto simp add: chg_map_def split: option.splits)
   2.469 +
   2.470 +lemma map_at_conv': "map_of (map_at f k al) = (chg_map f k (map_of al))"
   2.471 +  by (rule ext) (rule map_at_conv)
   2.472 +
   2.473 +lemma dom_map_at: "fst ` set (map_at f k al) = fst ` set al"
   2.474 +  by (induct al) auto
   2.475 +
   2.476 +lemma distinct_map_at: 
   2.477 +  assumes "distinct (map fst al)"
   2.478 +  shows "distinct (map fst (map_at f k al))"
   2.479 +using prems by (induct al) (auto simp add: dom_map_at)
   2.480 +
   2.481 +lemma map_at_notin_filter: 
   2.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]"
   2.483 +  by (induct ps) auto
   2.484 +
   2.485 +lemma clearjunk_map_at:
   2.486 + "clearjunk (map_at f k al) = map_at f k (clearjunk al)"
   2.487 +  by (induct al rule: clearjunk.induct) (auto simp add: map_at_notin_filter delete_def)
   2.488 +
   2.489 +lemma map_at_new[simp]: "map_of al k = None \<Longrightarrow> map_at f k al = al"
   2.490 +  by (induct al) auto
   2.491 +
   2.492 +lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
   2.493 +  by (induct al) auto
   2.494 +
   2.495 +lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
   2.496 +  by (simp add: map_at_conv')
   2.497 +
   2.498 +(* ******************************************************************************** *)
   2.499 +subsection {* @{const merge} *}
   2.500 +(* ******************************************************************************** *)
   2.501 +
   2.502 +lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
   2.503 +  by (induct ys fixing: xs) (auto simp add: dom_update)
   2.504 +
   2.505 +lemma distinct_merge:
   2.506 +  assumes "distinct (map fst xs)"
   2.507 +  shows "distinct (map fst (merge xs ys))"
   2.508 +  using prems
   2.509 +by (induct ys fixing: xs) (auto simp add: dom_merge distinct_update)
   2.510 +
   2.511 +lemma clearjunk_merge:
   2.512 + "clearjunk (merge xs ys) = merge (clearjunk xs) ys"
   2.513 +  by (induct ys) (auto simp add: clearjunk_update)
   2.514 +
   2.515 +lemma merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
   2.516 +proof (induct ys)
   2.517 +  case Nil thus ?case by simp 
   2.518 +next
   2.519 +  case (Cons y ys)
   2.520 +  show ?case
   2.521 +  proof (cases "k = fst y")
   2.522 +    case True
   2.523 +    from True show ?thesis
   2.524 +      by (simp add: update_conv)
   2.525 +  next
   2.526 +    case False
   2.527 +    from False show ?thesis
   2.528 +      by (auto simp add: update_conv Cons.hyps map_add_def)
   2.529 +  qed
   2.530 +qed
   2.531 +
   2.532 +lemma merge_conv': "map_of (merge xs ys) = (map_of xs ++ map_of ys)"
   2.533 +  by (rule ext) (rule merge_conv)
   2.534 +
   2.535 +lemma merge_emty: "map_of (merge [] ys) = map_of ys"
   2.536 +  by (simp add: merge_conv')
   2.537 +
   2.538 +lemma merge_assoc[simp]: "map_of (merge m1 (merge m2 m3)) = 
   2.539 +                           map_of (merge (merge m1 m2) m3)"
   2.540 +  by (simp add: merge_conv')
   2.541 +
   2.542 +lemma merge_Some_iff: 
   2.543 + "(map_of (merge m n) k = Some x) = 
   2.544 +  (map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x)"
   2.545 +  by (simp add: merge_conv' map_add_Some_iff)
   2.546 +
   2.547 +lemmas merge_SomeD = merge_Some_iff [THEN iffD1, standard]
   2.548 +declare merge_SomeD [dest!]
   2.549 +
   2.550 +lemma merge_find_right[simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
   2.551 +  by (simp add: merge_conv')
   2.552 +
   2.553 +lemma merge_None [iff]: 
   2.554 +  "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
   2.555 +  by (simp add: merge_conv')
   2.556 +
   2.557 +lemma merge_upd[simp]: 
   2.558 +  "map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
   2.559 +  by (simp add: update_conv' merge_conv')
   2.560 +
   2.561 +lemma merge_updatess[simp]: 
   2.562 +  "map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
   2.563 +  by (simp add: updates_conv' merge_conv')
   2.564 +
   2.565 +lemma merge_append: "map_of (xs@ys) = map_of (merge ys xs)"
   2.566 +  by (simp add: merge_conv')
   2.567 +
   2.568 +(* ******************************************************************************** *)
   2.569 +subsection {* @{const compose} *}
   2.570 +(* ******************************************************************************** *)
   2.571 +
   2.572 +lemma compose_induct [case_names Nil Cons]: 
   2.573 +  assumes Nil: "P [] ys"
   2.574 +  assumes Cons: "\<And>x xs.
   2.575 +     \<lbrakk>\<And>v. map_of ys (snd x) = Some v \<Longrightarrow> P xs ys;
   2.576 +      map_of ys (snd x) = None \<Longrightarrow> P (delete (fst x) xs) ys\<rbrakk>
   2.577 +     \<Longrightarrow> P (x # xs) ys"
   2.578 +  shows "P xs ys"
   2.579 +apply (rule compose.induct [where ?P="\<lambda>xs. P xs ys"])
   2.580 +apply (rule Nil)
   2.581 +apply  (rule Cons)
   2.582 +apply (erule allE, erule allE, erule impE, assumption,assumption)
   2.583 +apply (erule allE, erule impE,assumption,assumption)
   2.584 +done
   2.585 +
   2.586 +lemma compose_first_None [simp]: 
   2.587 +  assumes "map_of xs k = None" 
   2.588 +  shows "map_of (compose xs ys) k = None"
   2.589 +using prems
   2.590 +by (induct xs ys rule: compose_induct) (auto split: option.splits split_if_asm)
   2.591 +
   2.592 +
   2.593 +lemma compose_conv: 
   2.594 +  shows "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
   2.595 +proof (induct xs ys rule: compose_induct )
   2.596 +  case Nil thus ?case by simp
   2.597 +next
   2.598 +  case (Cons x xs)
   2.599 +  show ?case
   2.600 +  proof (cases "map_of ys (snd x)")
   2.601 +    case None
   2.602 +    with Cons
   2.603 +    have hyp: "map_of (compose (delete (fst x) xs) ys) k =
   2.604 +               (map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
   2.605 +      by simp
   2.606 +    show ?thesis
   2.607 +    proof (cases "fst x = k")
   2.608 +      case True
   2.609 +      from True delete_notin_dom [of k xs]
   2.610 +      have "map_of (delete (fst x) xs) k = None"
   2.611 +	by (simp add: map_of_eq_None_iff)
   2.612 +      with hyp show ?thesis
   2.613 +	using True None
   2.614 +	by simp
   2.615 +    next
   2.616 +      case False
   2.617 +      from False have "map_of (delete (fst x) xs) k = map_of xs k"
   2.618 +	by simp
   2.619 +      with hyp show ?thesis
   2.620 +	using False None
   2.621 +	by (simp add: map_comp_def)
   2.622 +    qed
   2.623 +  next
   2.624 +    case (Some v)
   2.625 +    with Cons
   2.626 +    have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
   2.627 +      by simp
   2.628 +    with Some show ?thesis
   2.629 +      by (auto simp add: map_comp_def)
   2.630 +  qed
   2.631 +qed
   2.632 +   
   2.633 +lemma compose_conv': 
   2.634 +  shows "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
   2.635 +  by (rule ext) (rule compose_conv)
   2.636 +
   2.637 +lemma compose_first_Some [simp]:
   2.638 +  assumes "map_of xs k = Some v" 
   2.639 +  shows "map_of (compose xs ys) k = map_of ys v"
   2.640 +using prems by (simp add: compose_conv)
   2.641 +
   2.642 +lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
   2.643 +proof (induct xs ys rule: compose_induct )
   2.644 +  case Nil thus ?case by simp
   2.645 +next
   2.646 +  case (Cons x xs)
   2.647 +  show ?case
   2.648 +  proof (cases "map_of ys (snd x)")
   2.649 +    case None
   2.650 +    with Cons.hyps
   2.651 +    have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
   2.652 +      by simp
   2.653 +    also
   2.654 +    have "\<dots> \<subseteq> fst ` set xs"
   2.655 +      by (rule dom_delete_subset)
   2.656 +    finally show ?thesis
   2.657 +      using None
   2.658 +      by auto
   2.659 +  next
   2.660 +    case (Some v)
   2.661 +    with Cons.hyps
   2.662 +    have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
   2.663 +      by simp
   2.664 +    with Some show ?thesis
   2.665 +      by auto
   2.666 +  qed
   2.667 +qed
   2.668 +
   2.669 +lemma distinct_compose:
   2.670 + assumes "distinct (map fst xs)"
   2.671 + shows "distinct (map fst (compose xs ys))"
   2.672 +using prems
   2.673 +proof (induct xs ys rule: compose_induct)
   2.674 +  case Nil thus ?case by simp
   2.675 +next
   2.676 +  case (Cons x xs)
   2.677 +  show ?case
   2.678 +  proof (cases "map_of ys (snd x)")
   2.679 +    case None
   2.680 +    with Cons show ?thesis by simp
   2.681 +  next
   2.682 +    case (Some v)
   2.683 +    with Cons dom_compose [of xs ys] show ?thesis 
   2.684 +      by (auto)
   2.685 +  qed
   2.686 +qed
   2.687 +
   2.688 +lemma compose_delete_twist: "(compose (delete k xs) ys) = delete k (compose xs ys)"
   2.689 +proof (induct xs ys rule: compose_induct)
   2.690 +  case Nil thus ?case by simp
   2.691 +next
   2.692 +  case (Cons x xs)
   2.693 +  show ?case
   2.694 +  proof (cases "map_of ys (snd x)")
   2.695 +    case None
   2.696 +    with Cons have 
   2.697 +      hyp: "compose (delete k (delete (fst x) xs)) ys =
   2.698 +            delete k (compose (delete (fst x) xs) ys)"
   2.699 +      by simp
   2.700 +    show ?thesis
   2.701 +    proof (cases "fst x = k")
   2.702 +      case True
   2.703 +      with None hyp
   2.704 +      show ?thesis
   2.705 +	by (simp add: delete_idem)
   2.706 +    next
   2.707 +      case False
   2.708 +      from None False hyp
   2.709 +      show ?thesis
   2.710 +	by (simp add: delete_twist)
   2.711 +    qed
   2.712 +  next
   2.713 +    case (Some v)
   2.714 +    with Cons have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" by simp
   2.715 +    with Some show ?thesis
   2.716 +      by simp
   2.717 +  qed
   2.718 +qed
   2.719 +
   2.720 +lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
   2.721 +  by (induct xs ys rule: compose_induct) 
   2.722 +     (auto simp add: map_of_clearjunk split: option.splits)
   2.723 +   
   2.724 +lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
   2.725 +  by (induct xs rule: clearjunk.induct)
   2.726 +     (auto split: option.splits simp add: clearjunk_delete delete_idem
   2.727 +               compose_delete_twist)
   2.728 +   
   2.729 +lemma compose_empty [simp]:
   2.730 + "compose xs [] = []"
   2.731 +  by (induct xs rule: compose_induct [where ys="[]"]) auto
   2.732 +
   2.733 +
   2.734 +lemma compose_Some_iff:
   2.735 +  "(map_of (compose xs ys) k = Some v) = 
   2.736 +     (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" 
   2.737 +  by (simp add: compose_conv map_comp_Some_iff)
   2.738 +
   2.739 +lemma map_comp_None_iff:
   2.740 +  "(map_of (compose xs ys) k = None) = 
   2.741 +    (map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None)) " 
   2.742 +  by (simp add: compose_conv map_comp_None_iff)
   2.743 +
   2.744 +
   2.745 +(* ******************************************************************************** *)
   2.746 +subsection {* @{const restrict} *}
   2.747 +(* ******************************************************************************** *)
   2.748 +
   2.749 +lemma restrict_simps [simp]: 
   2.750 +  "restrict A [] = []"
   2.751 +  "restrict A (p#ps) = (if fst p \<in> A then p#restrict A ps else restrict A ps)"
   2.752 +  by (auto simp add: restrict_def)
   2.753 +
   2.754 +lemma distinct_restr: "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
   2.755 +  by (induct al) (auto simp add: restrict_def)
   2.756 +
   2.757 +lemma restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
   2.758 +  apply (induct al)
   2.759 +  apply  (simp add: restrict_def)
   2.760 +  apply (cases "k\<in>A")
   2.761 +  apply (auto simp add: restrict_def)
   2.762 +  done
   2.763 +
   2.764 +lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
   2.765 +  by (rule ext) (rule restr_conv)
   2.766 +
   2.767 +lemma restr_empty [simp]: 
   2.768 +  "restrict {} al = []" 
   2.769 +  "restrict A [] = []"
   2.770 +  by (induct al) (auto simp add: restrict_def)
   2.771 +
   2.772 +lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
   2.773 +  by (simp add: restr_conv')
   2.774 +
   2.775 +lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
   2.776 +  by (simp add: restr_conv')
   2.777 +
   2.778 +lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
   2.779 +  by (induct al) (auto simp add: restrict_def)
   2.780 +
   2.781 +lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
   2.782 +  by (induct al) (auto simp add: restrict_def)
   2.783 +
   2.784 +lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
   2.785 +  by (induct al) (auto simp add: restrict_def)
   2.786 +
   2.787 +lemma restr_update[simp]:
   2.788 + "map_of (restrict D (update x y al)) = 
   2.789 +  map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
   2.790 +  by (simp add: restr_conv' update_conv')
   2.791 +
   2.792 +lemma restr_delete [simp]:
   2.793 +  "(delete x (restrict D al)) = 
   2.794 +    (if x\<in> D then restrict (D - {x}) al else restrict D al)"
   2.795 +proof (induct al)
   2.796 +  case Nil thus ?case by simp
   2.797 +next
   2.798 +  case (Cons a al)
   2.799 +  show ?case
   2.800 +  proof (cases "x \<in> D")
   2.801 +    case True
   2.802 +    note x_D = this
   2.803 +    with Cons have hyp: "delete x (restrict D al) = restrict (D - {x}) al"
   2.804 +      by simp
   2.805 +    show ?thesis
   2.806 +    proof (cases "fst a = x")
   2.807 +      case True
   2.808 +      from Cons.hyps
   2.809 +      show ?thesis
   2.810 +	using x_D True
   2.811 +	by simp
   2.812 +    next
   2.813 +      case False
   2.814 +      note not_fst_a_x = this
   2.815 +      show ?thesis
   2.816 +      proof (cases "fst a \<in> D")
   2.817 +	case True 
   2.818 +	with not_fst_a_x 
   2.819 +	have "delete x (restrict D (a#al)) = a#(delete x (restrict D al))"
   2.820 +	  by (cases a) (simp add: restrict_def)
   2.821 +	also from not_fst_a_x True hyp have "\<dots> = restrict (D - {x}) (a # al)"
   2.822 +	  by (cases a) (simp add: restrict_def)
   2.823 +	finally show ?thesis
   2.824 +	  using x_D by simp
   2.825 +      next
   2.826 +	case False
   2.827 +	hence "delete x (restrict D (a#al)) = delete x (restrict D al)"
   2.828 +	  by (cases a) (simp add: restrict_def)
   2.829 +	moreover from False not_fst_a_x
   2.830 +	have "restrict (D - {x}) (a # al) = restrict (D - {x}) al"
   2.831 +	  by (cases a) (simp add: restrict_def)
   2.832 +	ultimately
   2.833 +	show ?thesis using x_D hyp by simp
   2.834 +      qed
   2.835 +    qed
   2.836 +  next
   2.837 +    case False
   2.838 +    from False Cons show ?thesis
   2.839 +      by simp
   2.840 +  qed
   2.841 +qed
   2.842 +
   2.843 +lemma update_restr:
   2.844 + "map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
   2.845 +  by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
   2.846 +
   2.847 +lemma upate_restr_conv[simp]:
   2.848 + "x \<in> D \<Longrightarrow> 
   2.849 + map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
   2.850 +  by (simp add: update_conv' restr_conv')
   2.851 +
   2.852 +lemma restr_updates[simp]: "
   2.853 + \<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>
   2.854 + \<Longrightarrow> map_of (restrict D (updates xs ys al)) = 
   2.855 +     map_of (updates xs ys (restrict (D - set xs) al))"
   2.856 +  by (simp add: updates_conv' restr_conv')
   2.857 +
   2.858 +lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
   2.859 +  by (induct ps) auto
   2.860 +
   2.861 +lemma clearjunk_restrict:
   2.862 + "clearjunk (restrict A al) = restrict A (clearjunk al)"
   2.863 +  by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
   2.864 +
   2.865 +end
     3.1 --- a/src/HOL/Library/Library.thy	Fri Mar 10 15:33:48 2006 +0100
     3.2 +++ b/src/HOL/Library/Library.thy	Fri Mar 10 16:05:34 2006 +0100
     3.3 @@ -22,6 +22,7 @@
     3.4    Commutative_Ring
     3.5    Coinductive_List
     3.6    ASeries
     3.7 +  AssocList
     3.8  begin
     3.9  end
    3.10  (*>*)