src/HOL/Library/AssocList.thy
author nipkow
Thu Mar 23 20:03:53 2006 +0100 (2006-03-23)
changeset 19323 ec5cd5b1804c
parent 19234 054332e39e0a
child 19332 bb71a64e1263
permissions -rw-r--r--
Converted translations to abbbreviations.
Removed a few odd functions from Map and AssocList.
Moved chg_map from Map to Bali/Basis.
schirmer@19234
     1
(*  Title:      HOL/Library/Library.thy
schirmer@19234
     2
    ID:         $Id$
schirmer@19234
     3
    Author:     Norbert Schirmer, Tobias Nipkow, Martin Wildmoser
schirmer@19234
     4
*)
schirmer@19234
     5
schirmer@19234
     6
header {* Map operations implemented on association lists*}
schirmer@19234
     7
schirmer@19234
     8
theory AssocList 
schirmer@19234
     9
imports Map
schirmer@19234
    10
schirmer@19234
    11
begin
schirmer@19234
    12
schirmer@19234
    13
text {* The operations preserve distinctness of keys and 
schirmer@19234
    14
        function @{term "clearjunk"} distributes over them.*}
schirmer@19234
    15
consts 
schirmer@19234
    16
  delete     :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
schirmer@19234
    17
  update     :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
schirmer@19234
    18
  updates    :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
schirmer@19234
    19
  merge      :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
schirmer@19234
    20
  compose    :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
schirmer@19234
    21
  restrict   :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
schirmer@19234
    22
schirmer@19234
    23
  clearjunk  :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
schirmer@19234
    24
nipkow@19323
    25
(* a bit special
nipkow@19323
    26
  substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val)list"
nipkow@19323
    27
  map_at     :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow>  ('key * 'val) list"
nipkow@19323
    28
*)
nipkow@19323
    29
schirmer@19234
    30
defs
schirmer@19234
    31
delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
schirmer@19234
    32
schirmer@19234
    33
primrec
schirmer@19234
    34
"update k v [] = [(k,v)]"
schirmer@19234
    35
"update k v (p#ps) = (if fst p = k then (k,v)#ps else p # update k v ps)"
schirmer@19234
    36
primrec
schirmer@19234
    37
"updates [] vs al = al"
schirmer@19234
    38
"updates (k#ks) vs al = (case vs of [] \<Rightarrow> al 
schirmer@19234
    39
                         | (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
schirmer@19234
    40
primrec
nipkow@19323
    41
"merge xs [] = xs"
nipkow@19323
    42
"merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
nipkow@19323
    43
nipkow@19323
    44
(*
nipkow@19323
    45
primrec
schirmer@19234
    46
"substitute v v' [] = []"
schirmer@19234
    47
"substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
schirmer@19234
    48
                          else p#substitute v v' ps)"
schirmer@19234
    49
primrec
schirmer@19234
    50
"map_at f k [] = []"
schirmer@19234
    51
"map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
nipkow@19323
    52
*)
schirmer@19234
    53
schirmer@19234
    54
lemma length_delete_le: "length (delete k al) \<le> length al"
schirmer@19234
    55
proof (induct al)
schirmer@19234
    56
  case Nil thus ?case by (simp add: delete_def)
schirmer@19234
    57
next
schirmer@19234
    58
  case (Cons a al)
schirmer@19234
    59
  note length_filter_le [of "\<lambda>p. fst p \<noteq> fst a" al] 
schirmer@19234
    60
  also have "\<And>n. n \<le> Suc n"
schirmer@19234
    61
    by simp
schirmer@19234
    62
  finally have "length [p\<in>al . fst p \<noteq> fst a] \<le> Suc (length al)" .
schirmer@19234
    63
  with Cons show ?case
schirmer@19234
    64
    by (auto simp add: delete_def)
schirmer@19234
    65
qed
schirmer@19234
    66
schirmer@19234
    67
lemma compose_hint: "length (delete k al) < Suc (length al)"
schirmer@19234
    68
proof -
schirmer@19234
    69
  note length_delete_le
schirmer@19234
    70
  also have "\<And>n. n < Suc n"
schirmer@19234
    71
    by simp
schirmer@19234
    72
  finally show ?thesis .
schirmer@19234
    73
qed
schirmer@19234
    74
schirmer@19234
    75
recdef compose "measure size"
schirmer@19234
    76
"compose [] = (\<lambda>ys. [])"
schirmer@19234
    77
"compose (x#xs) = (\<lambda>ys. (case (map_of ys (snd x)) of
schirmer@19234
    78
                          None \<Rightarrow> compose (delete (fst x) xs) ys
schirmer@19234
    79
                         | Some v \<Rightarrow> (fst x,v)#compose xs ys))"
schirmer@19234
    80
(hints intro: compose_hint)
schirmer@19234
    81
schirmer@19234
    82
defs  
schirmer@19234
    83
restrict_def: "restrict A \<equiv> filter (\<lambda>(k,v). k \<in> A)"
schirmer@19234
    84
schirmer@19234
    85
recdef clearjunk "measure size"
schirmer@19234
    86
"clearjunk [] = []"
schirmer@19234
    87
"clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
schirmer@19234
    88
(hints intro: compose_hint)
schirmer@19234
    89
schirmer@19234
    90
schirmer@19234
    91
(* ******************************************************************************** *)
schirmer@19234
    92
subsection {* Lookup *}
schirmer@19234
    93
(* ******************************************************************************** *)
schirmer@19234
    94
schirmer@19234
    95
lemma lookup_simps: 
schirmer@19234
    96
  "map_of [] k = None"
schirmer@19234
    97
  "map_of (p#ps) k = (if fst p = k then Some (snd p) else map_of ps k)"
schirmer@19234
    98
  by simp_all
schirmer@19234
    99
schirmer@19234
   100
(* ******************************************************************************** *)
schirmer@19234
   101
subsection {* @{const delete} *}
schirmer@19234
   102
(* ******************************************************************************** *)
schirmer@19234
   103
schirmer@19234
   104
lemma delete_simps [simp]:
schirmer@19234
   105
"delete k [] = []"
schirmer@19234
   106
"delete k (p#ps) = (if fst p = k then delete k ps else p # delete k ps)"
schirmer@19234
   107
  by (simp_all add: delete_def)
schirmer@19234
   108
schirmer@19234
   109
lemma delete_id[simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
schirmer@19234
   110
by(induct al, auto)
schirmer@19234
   111
schirmer@19234
   112
lemma delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
schirmer@19234
   113
  by (induct al) auto
schirmer@19234
   114
schirmer@19234
   115
lemma delete_conv': "map_of (delete k al) = ((map_of al)(k := None))"
schirmer@19234
   116
  by (rule ext) (rule delete_conv)
schirmer@19234
   117
schirmer@19234
   118
lemma delete_idem: "delete k (delete k al) = delete k al"
schirmer@19234
   119
  by (induct al) auto
schirmer@19234
   120
schirmer@19234
   121
lemma map_of_delete[simp]:
schirmer@19234
   122
 "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
schirmer@19234
   123
by(induct al, auto)
schirmer@19234
   124
schirmer@19234
   125
lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
schirmer@19234
   126
  by (induct al) auto
schirmer@19234
   127
schirmer@19234
   128
lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
schirmer@19234
   129
  by (induct al) auto
schirmer@19234
   130
schirmer@19234
   131
lemma distinct_delete:
schirmer@19234
   132
  assumes "distinct (map fst al)" 
schirmer@19234
   133
  shows "distinct (map fst (delete k al))"
schirmer@19234
   134
using prems
schirmer@19234
   135
proof (induct al)
schirmer@19234
   136
  case Nil thus ?case by simp
schirmer@19234
   137
next
schirmer@19234
   138
  case (Cons a al)
schirmer@19234
   139
  from Cons.prems obtain 
schirmer@19234
   140
    a_notin_al: "fst a \<notin> fst ` set al" and
schirmer@19234
   141
    dist_al: "distinct (map fst al)"
schirmer@19234
   142
    by auto
schirmer@19234
   143
  show ?case
schirmer@19234
   144
  proof (cases "fst a = k")
schirmer@19234
   145
    case True
schirmer@19234
   146
    from True dist_al show ?thesis by simp
schirmer@19234
   147
  next
schirmer@19234
   148
    case False
schirmer@19234
   149
    from dist_al
schirmer@19234
   150
    have "distinct (map fst (delete k al))"
schirmer@19234
   151
      by (rule Cons.hyps)
schirmer@19234
   152
    moreover from a_notin_al dom_delete_subset [of k al] 
schirmer@19234
   153
    have "fst a \<notin> fst ` set (delete k al)"
schirmer@19234
   154
      by blast
schirmer@19234
   155
    ultimately show ?thesis using False by simp
schirmer@19234
   156
  qed
schirmer@19234
   157
qed
schirmer@19234
   158
schirmer@19234
   159
lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
schirmer@19234
   160
  by (induct al) auto
schirmer@19234
   161
schirmer@19234
   162
lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
schirmer@19234
   163
  by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
schirmer@19234
   164
schirmer@19234
   165
(* ******************************************************************************** *)
schirmer@19234
   166
subsection {* @{const clearjunk} *}
schirmer@19234
   167
(* ******************************************************************************** *)
schirmer@19234
   168
schirmer@19234
   169
lemma insert_fst_filter: 
schirmer@19234
   170
  "insert a(fst ` {x \<in> set ps. fst x \<noteq> a}) = insert a (fst ` set ps)"
schirmer@19234
   171
  by (induct ps) auto
schirmer@19234
   172
schirmer@19234
   173
lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
schirmer@19234
   174
  by (induct al rule: clearjunk.induct) (simp_all add: insert_fst_filter delete_def)
schirmer@19234
   175
schirmer@19234
   176
lemma notin_filter_fst: "a \<notin> fst ` {x \<in> set ps. fst x \<noteq> a}"
schirmer@19234
   177
  by (induct ps) auto
schirmer@19234
   178
schirmer@19234
   179
lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
schirmer@19234
   180
  by (induct al rule: clearjunk.induct) 
schirmer@19234
   181
     (simp_all add: dom_clearjunk notin_filter_fst delete_def)
schirmer@19234
   182
schirmer@19234
   183
lemma map_of_filter: "k \<noteq> a \<Longrightarrow> map_of [q\<in>ps . fst q \<noteq> a] k = map_of ps k"
schirmer@19234
   184
  by (induct ps) auto
schirmer@19234
   185
schirmer@19234
   186
lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
schirmer@19234
   187
  apply (rule ext)
schirmer@19234
   188
  apply (induct al rule: clearjunk.induct)
schirmer@19234
   189
  apply  simp
schirmer@19234
   190
  apply (simp add: map_of_filter)
schirmer@19234
   191
  done
schirmer@19234
   192
schirmer@19234
   193
lemma length_clearjunk: "length (clearjunk al) \<le> length al"
schirmer@19234
   194
proof (induct al rule: clearjunk.induct [case_names Nil Cons])
schirmer@19234
   195
  case Nil thus ?case by simp
schirmer@19234
   196
next
schirmer@19234
   197
  case (Cons k v ps)
schirmer@19234
   198
  from Cons have "length (clearjunk [q\<in>ps . fst q \<noteq> k]) \<le> length [q\<in>ps . fst q \<noteq> k]" 
schirmer@19234
   199
    by (simp add: delete_def)
schirmer@19234
   200
  also have "\<dots> \<le> length ps"
schirmer@19234
   201
    by simp
schirmer@19234
   202
  finally show ?case
schirmer@19234
   203
    by (simp add: delete_def)
schirmer@19234
   204
qed
schirmer@19234
   205
schirmer@19234
   206
lemma notin_fst_filter: "a \<notin> fst ` set ps \<Longrightarrow> [q\<in>ps . fst q \<noteq> a] = ps"
schirmer@19234
   207
  by (induct ps) auto
schirmer@19234
   208
            
schirmer@19234
   209
lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
schirmer@19234
   210
  by (induct al rule: clearjunk.induct) (auto simp add: notin_fst_filter)
schirmer@19234
   211
schirmer@19234
   212
lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
schirmer@19234
   213
  by simp
schirmer@19234
   214
schirmer@19234
   215
(* ******************************************************************************** *)
schirmer@19234
   216
subsection {* @{const dom} and @{term "ran"} *}
schirmer@19234
   217
(* ******************************************************************************** *)
schirmer@19234
   218
schirmer@19234
   219
lemma dom_map_of': "fst ` set al = dom (map_of al)"
schirmer@19234
   220
  by (induct al) auto
schirmer@19234
   221
schirmer@19234
   222
lemmas dom_map_of = dom_map_of' [symmetric]
schirmer@19234
   223
schirmer@19234
   224
lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
schirmer@19234
   225
  by (simp add: map_of_clearjunk)
schirmer@19234
   226
schirmer@19234
   227
lemma ran_distinct: 
schirmer@19234
   228
  assumes dist: "distinct (map fst al)" 
schirmer@19234
   229
  shows "ran (map_of al) = snd ` set al"
schirmer@19234
   230
using dist
schirmer@19234
   231
proof (induct al) 
schirmer@19234
   232
  case Nil
schirmer@19234
   233
  thus ?case by simp
schirmer@19234
   234
next
schirmer@19234
   235
  case (Cons a al)
schirmer@19234
   236
  hence hyp: "snd ` set al = ran (map_of al)"
schirmer@19234
   237
    by simp
schirmer@19234
   238
schirmer@19234
   239
  have "ran (map_of (a # al)) = {snd a} \<union> ran (map_of al)"
schirmer@19234
   240
  proof 
schirmer@19234
   241
    show "ran (map_of (a # al)) \<subseteq> {snd a} \<union> ran (map_of al)"
schirmer@19234
   242
    proof   
schirmer@19234
   243
      fix v
schirmer@19234
   244
      assume "v \<in> ran (map_of (a#al))"
schirmer@19234
   245
      then obtain x where "map_of (a#al) x = Some v"
schirmer@19234
   246
	by (auto simp add: ran_def)
schirmer@19234
   247
      then show "v \<in> {snd a} \<union> ran (map_of al)"
schirmer@19234
   248
	by (auto split: split_if_asm simp add: ran_def)
schirmer@19234
   249
    qed
schirmer@19234
   250
  next
schirmer@19234
   251
    show "{snd a} \<union> ran (map_of al) \<subseteq> ran (map_of (a # al))"
schirmer@19234
   252
    proof 
schirmer@19234
   253
      fix v
schirmer@19234
   254
      assume v_in: "v \<in> {snd a} \<union> ran (map_of al)"
schirmer@19234
   255
      show "v \<in> ran (map_of (a#al))"
schirmer@19234
   256
      proof (cases "v=snd a")
schirmer@19234
   257
	case True
schirmer@19234
   258
	with v_in show ?thesis
schirmer@19234
   259
	  by (auto simp add: ran_def)
schirmer@19234
   260
      next
schirmer@19234
   261
	case False
schirmer@19234
   262
	with v_in have "v \<in> ran (map_of al)" by auto
schirmer@19234
   263
	then obtain x where al_x: "map_of al x = Some v"
schirmer@19234
   264
	  by (auto simp add: ran_def)
schirmer@19234
   265
	from map_of_SomeD [OF this]
schirmer@19234
   266
	have "x \<in> fst ` set al"
schirmer@19234
   267
	  by (force simp add: image_def)
schirmer@19234
   268
	with Cons.prems have "x\<noteq>fst a"
schirmer@19234
   269
	  by - (rule ccontr,simp)
schirmer@19234
   270
	with al_x
schirmer@19234
   271
	show ?thesis
schirmer@19234
   272
	  by (auto simp add: ran_def)
schirmer@19234
   273
      qed
schirmer@19234
   274
    qed
schirmer@19234
   275
  qed
schirmer@19234
   276
  with hyp show ?case
schirmer@19234
   277
    by (simp only:) auto
schirmer@19234
   278
qed
schirmer@19234
   279
schirmer@19234
   280
lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
schirmer@19234
   281
proof -
schirmer@19234
   282
  have "ran (map_of al) = ran (map_of (clearjunk al))"
schirmer@19234
   283
    by (simp add: ran_clearjunk)
schirmer@19234
   284
  also have "\<dots> = snd ` set (clearjunk al)"
schirmer@19234
   285
    by (simp add: ran_distinct)
schirmer@19234
   286
  finally show ?thesis .
schirmer@19234
   287
qed
schirmer@19234
   288
   
schirmer@19234
   289
(* ******************************************************************************** *)
schirmer@19234
   290
subsection {* @{const update} *}
schirmer@19234
   291
(* ******************************************************************************** *)
schirmer@19234
   292
schirmer@19234
   293
lemma update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
schirmer@19234
   294
  by (induct al) auto
schirmer@19234
   295
schirmer@19234
   296
lemma update_conv': "map_of (update k v al)  = ((map_of al)(k\<mapsto>v))"
schirmer@19234
   297
  by (rule ext) (rule update_conv)
schirmer@19234
   298
schirmer@19234
   299
lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
schirmer@19234
   300
  by (induct al) auto
schirmer@19234
   301
schirmer@19234
   302
lemma distinct_update:
schirmer@19234
   303
  assumes "distinct (map fst al)" 
schirmer@19234
   304
  shows "distinct (map fst (update k v al))"
schirmer@19234
   305
using prems
schirmer@19234
   306
proof (induct al)
schirmer@19234
   307
  case Nil thus ?case by simp
schirmer@19234
   308
next
schirmer@19234
   309
  case (Cons a al)
schirmer@19234
   310
  from Cons.prems obtain 
schirmer@19234
   311
    a_notin_al: "fst a \<notin> fst ` set al" and
schirmer@19234
   312
    dist_al: "distinct (map fst al)"
schirmer@19234
   313
    by auto
schirmer@19234
   314
  show ?case
schirmer@19234
   315
  proof (cases "fst a = k")
schirmer@19234
   316
    case True
schirmer@19234
   317
    from True dist_al a_notin_al show ?thesis by simp
schirmer@19234
   318
  next
schirmer@19234
   319
    case False
schirmer@19234
   320
    from dist_al
schirmer@19234
   321
    have "distinct (map fst (update k v al))"
schirmer@19234
   322
      by (rule Cons.hyps)
schirmer@19234
   323
    with False a_notin_al show ?thesis by (simp add: dom_update)
schirmer@19234
   324
  qed
schirmer@19234
   325
qed
schirmer@19234
   326
schirmer@19234
   327
lemma update_filter: 
schirmer@19234
   328
  "a\<noteq>k \<Longrightarrow> update k v [q\<in>ps . fst q \<noteq> a] = [q\<in>update k v ps . fst q \<noteq> a]"
schirmer@19234
   329
  by (induct ps) auto
schirmer@19234
   330
schirmer@19234
   331
lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
schirmer@19234
   332
  by (induct al rule: clearjunk.induct) (auto simp add: update_filter delete_def)
schirmer@19234
   333
schirmer@19234
   334
lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
schirmer@19234
   335
  by (induct al) auto
schirmer@19234
   336
schirmer@19234
   337
lemma update_nonempty [simp]: "update k v al \<noteq> []"
schirmer@19234
   338
  by (induct al) auto
schirmer@19234
   339
schirmer@19234
   340
lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v=v'"
schirmer@19234
   341
proof (induct al fixing: al') 
schirmer@19234
   342
  case Nil thus ?case 
schirmer@19234
   343
    by (cases al') (auto split: split_if_asm)
schirmer@19234
   344
next
schirmer@19234
   345
  case Cons thus ?case
schirmer@19234
   346
    by (cases al') (auto split: split_if_asm)
schirmer@19234
   347
qed
schirmer@19234
   348
schirmer@19234
   349
lemma update_last [simp]: "update k v (update k v' al) = update k v al"
schirmer@19234
   350
  by (induct al) auto
schirmer@19234
   351
schirmer@19234
   352
text {* Note that the lists are not necessarily the same:
schirmer@19234
   353
        @{term "update k v (update k' v' []) = [(k',v'),(k,v)]"} and 
schirmer@19234
   354
        @{term "update k' v' (update k v []) = [(k,v),(k',v')]"}.*}
schirmer@19234
   355
lemma update_swap: "k\<noteq>k' 
schirmer@19234
   356
  \<Longrightarrow> map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
schirmer@19234
   357
  by (auto simp add: update_conv' intro: ext)
schirmer@19234
   358
schirmer@19234
   359
lemma update_Some_unfold: 
schirmer@19234
   360
  "(map_of (update k v al) x = Some y) = 
schirmer@19234
   361
     (x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y)"
schirmer@19234
   362
  by (simp add: update_conv' map_upd_Some_unfold)
schirmer@19234
   363
schirmer@19234
   364
lemma image_update[simp]: "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
schirmer@19234
   365
  by (simp add: update_conv' image_map_upd)
schirmer@19234
   366
schirmer@19234
   367
schirmer@19234
   368
(* ******************************************************************************** *)
schirmer@19234
   369
subsection {* @{const updates} *}
schirmer@19234
   370
(* ******************************************************************************** *)
schirmer@19234
   371
schirmer@19234
   372
lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
schirmer@19234
   373
proof (induct ks fixing: vs al)
schirmer@19234
   374
  case Nil
schirmer@19234
   375
  thus ?case by simp
schirmer@19234
   376
next
schirmer@19234
   377
  case (Cons k ks)
schirmer@19234
   378
  show ?case
schirmer@19234
   379
  proof (cases vs)
schirmer@19234
   380
    case Nil
schirmer@19234
   381
    with Cons show ?thesis by simp
schirmer@19234
   382
  next
schirmer@19234
   383
    case (Cons k ks')
schirmer@19234
   384
    with Cons.hyps show ?thesis
schirmer@19234
   385
      by (simp add: update_conv fun_upd_def)
schirmer@19234
   386
  qed
schirmer@19234
   387
qed
schirmer@19234
   388
schirmer@19234
   389
lemma updates_conv': "map_of (updates ks vs al) = ((map_of al)(ks[\<mapsto>]vs))"
schirmer@19234
   390
  by (rule ext) (rule updates_conv)
schirmer@19234
   391
schirmer@19234
   392
lemma distinct_updates:
schirmer@19234
   393
  assumes "distinct (map fst al)"
schirmer@19234
   394
  shows "distinct (map fst (updates ks vs al))"
schirmer@19234
   395
  using prems
schirmer@19234
   396
by (induct ks fixing: vs al) (auto simp add: distinct_update split: list.splits)
schirmer@19234
   397
schirmer@19234
   398
lemma clearjunk_updates:
schirmer@19234
   399
 "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
schirmer@19234
   400
  by (induct ks fixing: vs al) (auto simp add: clearjunk_update split: list.splits)
schirmer@19234
   401
schirmer@19234
   402
lemma updates_empty[simp]: "updates vs [] al = al"
schirmer@19234
   403
  by (induct vs) auto 
schirmer@19234
   404
schirmer@19234
   405
lemma updates_Cons: "updates (k#ks) (v#vs) al = updates ks vs (update k v al)"
schirmer@19234
   406
  by simp
schirmer@19234
   407
schirmer@19234
   408
lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
schirmer@19234
   409
  updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
schirmer@19234
   410
  by (induct ks fixing: vs al) (auto split: list.splits)
schirmer@19234
   411
schirmer@19234
   412
lemma updates_list_update_drop[simp]:
schirmer@19234
   413
 "\<lbrakk>size ks \<le> i; i < size vs\<rbrakk>
schirmer@19234
   414
   \<Longrightarrow> updates ks (vs[i:=v]) al = updates ks vs al"
schirmer@19234
   415
  by (induct ks fixing: al vs i) (auto split:list.splits nat.splits)
schirmer@19234
   416
schirmer@19234
   417
lemma update_updates_conv_if: "
schirmer@19234
   418
 map_of (updates xs ys (update x y al)) =
schirmer@19234
   419
 map_of (if x \<in>  set(take (length ys) xs) then updates xs ys al
schirmer@19234
   420
                                  else (update x y (updates xs ys al)))"
schirmer@19234
   421
  by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
schirmer@19234
   422
schirmer@19234
   423
lemma updates_twist [simp]:
schirmer@19234
   424
 "k \<notin> set ks \<Longrightarrow> 
schirmer@19234
   425
  map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
schirmer@19234
   426
  by (simp add: updates_conv' update_conv' map_upds_twist)
schirmer@19234
   427
schirmer@19234
   428
lemma updates_apply_notin[simp]:
schirmer@19234
   429
 "k \<notin> set ks ==> map_of (updates ks vs al) k = map_of al k"
schirmer@19234
   430
  by (simp add: updates_conv)
schirmer@19234
   431
schirmer@19234
   432
lemma updates_append_drop[simp]:
schirmer@19234
   433
  "size xs = size ys \<Longrightarrow> updates (xs@zs) ys al = updates xs ys al"
schirmer@19234
   434
  by (induct xs fixing: ys al) (auto split: list.splits)
schirmer@19234
   435
schirmer@19234
   436
lemma updates_append2_drop[simp]:
schirmer@19234
   437
  "size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
schirmer@19234
   438
  by (induct xs fixing: ys al) (auto split: list.splits)
schirmer@19234
   439
nipkow@19323
   440
(*
schirmer@19234
   441
(* ******************************************************************************** *)
schirmer@19234
   442
subsection {* @{const substitute} *}
schirmer@19234
   443
(* ******************************************************************************** *)
schirmer@19234
   444
schirmer@19234
   445
lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
schirmer@19234
   446
  by (induct al) auto
schirmer@19234
   447
schirmer@19234
   448
lemma substitute_conv': "map_of (substitute v v' al) = ((map_of al)(v ~> v'))"
schirmer@19234
   449
  by (rule ext) (rule substitute_conv)
schirmer@19234
   450
schirmer@19234
   451
lemma dom_substitute: "fst ` set (substitute v v' al) = fst ` set al"
schirmer@19234
   452
  by (induct al) auto
schirmer@19234
   453
schirmer@19234
   454
lemma distinct_substitute: 
schirmer@19234
   455
  "distinct (map fst al) \<Longrightarrow> distinct (map fst (substitute v v' al))"
schirmer@19234
   456
  by (induct al) (auto simp add: dom_substitute)
schirmer@19234
   457
schirmer@19234
   458
lemma substitute_filter: 
schirmer@19234
   459
  "(substitute v v' [q\<in>ps . fst q \<noteq> a]) = [q\<in>substitute v v' ps . fst q \<noteq> a]"
schirmer@19234
   460
  by (induct ps) auto
schirmer@19234
   461
schirmer@19234
   462
lemma clearjunk_substitute:
schirmer@19234
   463
 "clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
schirmer@19234
   464
  by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
nipkow@19323
   465
*)
nipkow@19323
   466
(*
schirmer@19234
   467
(* ******************************************************************************** *)
schirmer@19234
   468
subsection {* @{const map_at} *}
schirmer@19234
   469
(* ******************************************************************************** *)
schirmer@19234
   470
  
schirmer@19234
   471
lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
schirmer@19234
   472
  by (induct al) (auto simp add: chg_map_def split: option.splits)
schirmer@19234
   473
schirmer@19234
   474
lemma map_at_conv': "map_of (map_at f k al) = (chg_map f k (map_of al))"
schirmer@19234
   475
  by (rule ext) (rule map_at_conv)
schirmer@19234
   476
schirmer@19234
   477
lemma dom_map_at: "fst ` set (map_at f k al) = fst ` set al"
schirmer@19234
   478
  by (induct al) auto
schirmer@19234
   479
schirmer@19234
   480
lemma distinct_map_at: 
schirmer@19234
   481
  assumes "distinct (map fst al)"
schirmer@19234
   482
  shows "distinct (map fst (map_at f k al))"
schirmer@19234
   483
using prems by (induct al) (auto simp add: dom_map_at)
schirmer@19234
   484
schirmer@19234
   485
lemma map_at_notin_filter: 
schirmer@19234
   486
  "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]"
schirmer@19234
   487
  by (induct ps) auto
schirmer@19234
   488
schirmer@19234
   489
lemma clearjunk_map_at:
schirmer@19234
   490
 "clearjunk (map_at f k al) = map_at f k (clearjunk al)"
schirmer@19234
   491
  by (induct al rule: clearjunk.induct) (auto simp add: map_at_notin_filter delete_def)
schirmer@19234
   492
schirmer@19234
   493
lemma map_at_new[simp]: "map_of al k = None \<Longrightarrow> map_at f k al = al"
schirmer@19234
   494
  by (induct al) auto
schirmer@19234
   495
schirmer@19234
   496
lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
schirmer@19234
   497
  by (induct al) auto
schirmer@19234
   498
schirmer@19234
   499
lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
schirmer@19234
   500
  by (simp add: map_at_conv')
nipkow@19323
   501
*)
schirmer@19234
   502
(* ******************************************************************************** *)
schirmer@19234
   503
subsection {* @{const merge} *}
schirmer@19234
   504
(* ******************************************************************************** *)
schirmer@19234
   505
schirmer@19234
   506
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
schirmer@19234
   507
  by (induct ys fixing: xs) (auto simp add: dom_update)
schirmer@19234
   508
schirmer@19234
   509
lemma distinct_merge:
schirmer@19234
   510
  assumes "distinct (map fst xs)"
schirmer@19234
   511
  shows "distinct (map fst (merge xs ys))"
schirmer@19234
   512
  using prems
schirmer@19234
   513
by (induct ys fixing: xs) (auto simp add: dom_merge distinct_update)
schirmer@19234
   514
schirmer@19234
   515
lemma clearjunk_merge:
schirmer@19234
   516
 "clearjunk (merge xs ys) = merge (clearjunk xs) ys"
schirmer@19234
   517
  by (induct ys) (auto simp add: clearjunk_update)
schirmer@19234
   518
schirmer@19234
   519
lemma merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
schirmer@19234
   520
proof (induct ys)
schirmer@19234
   521
  case Nil thus ?case by simp 
schirmer@19234
   522
next
schirmer@19234
   523
  case (Cons y ys)
schirmer@19234
   524
  show ?case
schirmer@19234
   525
  proof (cases "k = fst y")
schirmer@19234
   526
    case True
schirmer@19234
   527
    from True show ?thesis
schirmer@19234
   528
      by (simp add: update_conv)
schirmer@19234
   529
  next
schirmer@19234
   530
    case False
schirmer@19234
   531
    from False show ?thesis
schirmer@19234
   532
      by (auto simp add: update_conv Cons.hyps map_add_def)
schirmer@19234
   533
  qed
schirmer@19234
   534
qed
schirmer@19234
   535
schirmer@19234
   536
lemma merge_conv': "map_of (merge xs ys) = (map_of xs ++ map_of ys)"
schirmer@19234
   537
  by (rule ext) (rule merge_conv)
schirmer@19234
   538
schirmer@19234
   539
lemma merge_emty: "map_of (merge [] ys) = map_of ys"
schirmer@19234
   540
  by (simp add: merge_conv')
schirmer@19234
   541
schirmer@19234
   542
lemma merge_assoc[simp]: "map_of (merge m1 (merge m2 m3)) = 
schirmer@19234
   543
                           map_of (merge (merge m1 m2) m3)"
schirmer@19234
   544
  by (simp add: merge_conv')
schirmer@19234
   545
schirmer@19234
   546
lemma merge_Some_iff: 
schirmer@19234
   547
 "(map_of (merge m n) k = Some x) = 
schirmer@19234
   548
  (map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x)"
schirmer@19234
   549
  by (simp add: merge_conv' map_add_Some_iff)
schirmer@19234
   550
schirmer@19234
   551
lemmas merge_SomeD = merge_Some_iff [THEN iffD1, standard]
schirmer@19234
   552
declare merge_SomeD [dest!]
schirmer@19234
   553
schirmer@19234
   554
lemma merge_find_right[simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
schirmer@19234
   555
  by (simp add: merge_conv')
schirmer@19234
   556
schirmer@19234
   557
lemma merge_None [iff]: 
schirmer@19234
   558
  "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
schirmer@19234
   559
  by (simp add: merge_conv')
schirmer@19234
   560
schirmer@19234
   561
lemma merge_upd[simp]: 
schirmer@19234
   562
  "map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
schirmer@19234
   563
  by (simp add: update_conv' merge_conv')
schirmer@19234
   564
schirmer@19234
   565
lemma merge_updatess[simp]: 
schirmer@19234
   566
  "map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
schirmer@19234
   567
  by (simp add: updates_conv' merge_conv')
schirmer@19234
   568
schirmer@19234
   569
lemma merge_append: "map_of (xs@ys) = map_of (merge ys xs)"
schirmer@19234
   570
  by (simp add: merge_conv')
schirmer@19234
   571
schirmer@19234
   572
(* ******************************************************************************** *)
schirmer@19234
   573
subsection {* @{const compose} *}
schirmer@19234
   574
(* ******************************************************************************** *)
schirmer@19234
   575
schirmer@19234
   576
lemma compose_induct [case_names Nil Cons]: 
schirmer@19234
   577
  assumes Nil: "P [] ys"
schirmer@19234
   578
  assumes Cons: "\<And>x xs.
schirmer@19234
   579
     \<lbrakk>\<And>v. map_of ys (snd x) = Some v \<Longrightarrow> P xs ys;
schirmer@19234
   580
      map_of ys (snd x) = None \<Longrightarrow> P (delete (fst x) xs) ys\<rbrakk>
schirmer@19234
   581
     \<Longrightarrow> P (x # xs) ys"
schirmer@19234
   582
  shows "P xs ys"
schirmer@19234
   583
apply (rule compose.induct [where ?P="\<lambda>xs. P xs ys"])
schirmer@19234
   584
apply (rule Nil)
schirmer@19234
   585
apply  (rule Cons)
schirmer@19234
   586
apply (erule allE, erule allE, erule impE, assumption,assumption)
schirmer@19234
   587
apply (erule allE, erule impE,assumption,assumption)
schirmer@19234
   588
done
schirmer@19234
   589
schirmer@19234
   590
lemma compose_first_None [simp]: 
schirmer@19234
   591
  assumes "map_of xs k = None" 
schirmer@19234
   592
  shows "map_of (compose xs ys) k = None"
schirmer@19234
   593
using prems
schirmer@19234
   594
by (induct xs ys rule: compose_induct) (auto split: option.splits split_if_asm)
schirmer@19234
   595
schirmer@19234
   596
schirmer@19234
   597
lemma compose_conv: 
schirmer@19234
   598
  shows "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
schirmer@19234
   599
proof (induct xs ys rule: compose_induct )
schirmer@19234
   600
  case Nil thus ?case by simp
schirmer@19234
   601
next
schirmer@19234
   602
  case (Cons x xs)
schirmer@19234
   603
  show ?case
schirmer@19234
   604
  proof (cases "map_of ys (snd x)")
schirmer@19234
   605
    case None
schirmer@19234
   606
    with Cons
schirmer@19234
   607
    have hyp: "map_of (compose (delete (fst x) xs) ys) k =
schirmer@19234
   608
               (map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
schirmer@19234
   609
      by simp
schirmer@19234
   610
    show ?thesis
schirmer@19234
   611
    proof (cases "fst x = k")
schirmer@19234
   612
      case True
schirmer@19234
   613
      from True delete_notin_dom [of k xs]
schirmer@19234
   614
      have "map_of (delete (fst x) xs) k = None"
schirmer@19234
   615
	by (simp add: map_of_eq_None_iff)
schirmer@19234
   616
      with hyp show ?thesis
schirmer@19234
   617
	using True None
schirmer@19234
   618
	by simp
schirmer@19234
   619
    next
schirmer@19234
   620
      case False
schirmer@19234
   621
      from False have "map_of (delete (fst x) xs) k = map_of xs k"
schirmer@19234
   622
	by simp
schirmer@19234
   623
      with hyp show ?thesis
schirmer@19234
   624
	using False None
schirmer@19234
   625
	by (simp add: map_comp_def)
schirmer@19234
   626
    qed
schirmer@19234
   627
  next
schirmer@19234
   628
    case (Some v)
schirmer@19234
   629
    with Cons
schirmer@19234
   630
    have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
schirmer@19234
   631
      by simp
schirmer@19234
   632
    with Some show ?thesis
schirmer@19234
   633
      by (auto simp add: map_comp_def)
schirmer@19234
   634
  qed
schirmer@19234
   635
qed
schirmer@19234
   636
   
schirmer@19234
   637
lemma compose_conv': 
schirmer@19234
   638
  shows "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
schirmer@19234
   639
  by (rule ext) (rule compose_conv)
schirmer@19234
   640
schirmer@19234
   641
lemma compose_first_Some [simp]:
schirmer@19234
   642
  assumes "map_of xs k = Some v" 
schirmer@19234
   643
  shows "map_of (compose xs ys) k = map_of ys v"
schirmer@19234
   644
using prems by (simp add: compose_conv)
schirmer@19234
   645
schirmer@19234
   646
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
schirmer@19234
   647
proof (induct xs ys rule: compose_induct )
schirmer@19234
   648
  case Nil thus ?case by simp
schirmer@19234
   649
next
schirmer@19234
   650
  case (Cons x xs)
schirmer@19234
   651
  show ?case
schirmer@19234
   652
  proof (cases "map_of ys (snd x)")
schirmer@19234
   653
    case None
schirmer@19234
   654
    with Cons.hyps
schirmer@19234
   655
    have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
schirmer@19234
   656
      by simp
schirmer@19234
   657
    also
schirmer@19234
   658
    have "\<dots> \<subseteq> fst ` set xs"
schirmer@19234
   659
      by (rule dom_delete_subset)
schirmer@19234
   660
    finally show ?thesis
schirmer@19234
   661
      using None
schirmer@19234
   662
      by auto
schirmer@19234
   663
  next
schirmer@19234
   664
    case (Some v)
schirmer@19234
   665
    with Cons.hyps
schirmer@19234
   666
    have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
schirmer@19234
   667
      by simp
schirmer@19234
   668
    with Some show ?thesis
schirmer@19234
   669
      by auto
schirmer@19234
   670
  qed
schirmer@19234
   671
qed
schirmer@19234
   672
schirmer@19234
   673
lemma distinct_compose:
schirmer@19234
   674
 assumes "distinct (map fst xs)"
schirmer@19234
   675
 shows "distinct (map fst (compose xs ys))"
schirmer@19234
   676
using prems
schirmer@19234
   677
proof (induct xs ys rule: compose_induct)
schirmer@19234
   678
  case Nil thus ?case by simp
schirmer@19234
   679
next
schirmer@19234
   680
  case (Cons x xs)
schirmer@19234
   681
  show ?case
schirmer@19234
   682
  proof (cases "map_of ys (snd x)")
schirmer@19234
   683
    case None
schirmer@19234
   684
    with Cons show ?thesis by simp
schirmer@19234
   685
  next
schirmer@19234
   686
    case (Some v)
schirmer@19234
   687
    with Cons dom_compose [of xs ys] show ?thesis 
schirmer@19234
   688
      by (auto)
schirmer@19234
   689
  qed
schirmer@19234
   690
qed
schirmer@19234
   691
schirmer@19234
   692
lemma compose_delete_twist: "(compose (delete k xs) ys) = delete k (compose xs ys)"
schirmer@19234
   693
proof (induct xs ys rule: compose_induct)
schirmer@19234
   694
  case Nil thus ?case by simp
schirmer@19234
   695
next
schirmer@19234
   696
  case (Cons x xs)
schirmer@19234
   697
  show ?case
schirmer@19234
   698
  proof (cases "map_of ys (snd x)")
schirmer@19234
   699
    case None
schirmer@19234
   700
    with Cons have 
schirmer@19234
   701
      hyp: "compose (delete k (delete (fst x) xs)) ys =
schirmer@19234
   702
            delete k (compose (delete (fst x) xs) ys)"
schirmer@19234
   703
      by simp
schirmer@19234
   704
    show ?thesis
schirmer@19234
   705
    proof (cases "fst x = k")
schirmer@19234
   706
      case True
schirmer@19234
   707
      with None hyp
schirmer@19234
   708
      show ?thesis
schirmer@19234
   709
	by (simp add: delete_idem)
schirmer@19234
   710
    next
schirmer@19234
   711
      case False
schirmer@19234
   712
      from None False hyp
schirmer@19234
   713
      show ?thesis
schirmer@19234
   714
	by (simp add: delete_twist)
schirmer@19234
   715
    qed
schirmer@19234
   716
  next
schirmer@19234
   717
    case (Some v)
schirmer@19234
   718
    with Cons have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" by simp
schirmer@19234
   719
    with Some show ?thesis
schirmer@19234
   720
      by simp
schirmer@19234
   721
  qed
schirmer@19234
   722
qed
schirmer@19234
   723
schirmer@19234
   724
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
schirmer@19234
   725
  by (induct xs ys rule: compose_induct) 
schirmer@19234
   726
     (auto simp add: map_of_clearjunk split: option.splits)
schirmer@19234
   727
   
schirmer@19234
   728
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
schirmer@19234
   729
  by (induct xs rule: clearjunk.induct)
schirmer@19234
   730
     (auto split: option.splits simp add: clearjunk_delete delete_idem
schirmer@19234
   731
               compose_delete_twist)
schirmer@19234
   732
   
schirmer@19234
   733
lemma compose_empty [simp]:
schirmer@19234
   734
 "compose xs [] = []"
schirmer@19234
   735
  by (induct xs rule: compose_induct [where ys="[]"]) auto
schirmer@19234
   736
schirmer@19234
   737
schirmer@19234
   738
lemma compose_Some_iff:
schirmer@19234
   739
  "(map_of (compose xs ys) k = Some v) = 
schirmer@19234
   740
     (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" 
schirmer@19234
   741
  by (simp add: compose_conv map_comp_Some_iff)
schirmer@19234
   742
schirmer@19234
   743
lemma map_comp_None_iff:
schirmer@19234
   744
  "(map_of (compose xs ys) k = None) = 
schirmer@19234
   745
    (map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None)) " 
schirmer@19234
   746
  by (simp add: compose_conv map_comp_None_iff)
schirmer@19234
   747
schirmer@19234
   748
schirmer@19234
   749
(* ******************************************************************************** *)
schirmer@19234
   750
subsection {* @{const restrict} *}
schirmer@19234
   751
(* ******************************************************************************** *)
schirmer@19234
   752
schirmer@19234
   753
lemma restrict_simps [simp]: 
schirmer@19234
   754
  "restrict A [] = []"
schirmer@19234
   755
  "restrict A (p#ps) = (if fst p \<in> A then p#restrict A ps else restrict A ps)"
schirmer@19234
   756
  by (auto simp add: restrict_def)
schirmer@19234
   757
schirmer@19234
   758
lemma distinct_restr: "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
schirmer@19234
   759
  by (induct al) (auto simp add: restrict_def)
schirmer@19234
   760
schirmer@19234
   761
lemma restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
schirmer@19234
   762
  apply (induct al)
schirmer@19234
   763
  apply  (simp add: restrict_def)
schirmer@19234
   764
  apply (cases "k\<in>A")
schirmer@19234
   765
  apply (auto simp add: restrict_def)
schirmer@19234
   766
  done
schirmer@19234
   767
schirmer@19234
   768
lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
schirmer@19234
   769
  by (rule ext) (rule restr_conv)
schirmer@19234
   770
schirmer@19234
   771
lemma restr_empty [simp]: 
schirmer@19234
   772
  "restrict {} al = []" 
schirmer@19234
   773
  "restrict A [] = []"
schirmer@19234
   774
  by (induct al) (auto simp add: restrict_def)
schirmer@19234
   775
schirmer@19234
   776
lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
schirmer@19234
   777
  by (simp add: restr_conv')
schirmer@19234
   778
schirmer@19234
   779
lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
schirmer@19234
   780
  by (simp add: restr_conv')
schirmer@19234
   781
schirmer@19234
   782
lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
schirmer@19234
   783
  by (induct al) (auto simp add: restrict_def)
schirmer@19234
   784
schirmer@19234
   785
lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
schirmer@19234
   786
  by (induct al) (auto simp add: restrict_def)
schirmer@19234
   787
schirmer@19234
   788
lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
schirmer@19234
   789
  by (induct al) (auto simp add: restrict_def)
schirmer@19234
   790
schirmer@19234
   791
lemma restr_update[simp]:
schirmer@19234
   792
 "map_of (restrict D (update x y al)) = 
schirmer@19234
   793
  map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
schirmer@19234
   794
  by (simp add: restr_conv' update_conv')
schirmer@19234
   795
schirmer@19234
   796
lemma restr_delete [simp]:
schirmer@19234
   797
  "(delete x (restrict D al)) = 
schirmer@19234
   798
    (if x\<in> D then restrict (D - {x}) al else restrict D al)"
schirmer@19234
   799
proof (induct al)
schirmer@19234
   800
  case Nil thus ?case by simp
schirmer@19234
   801
next
schirmer@19234
   802
  case (Cons a al)
schirmer@19234
   803
  show ?case
schirmer@19234
   804
  proof (cases "x \<in> D")
schirmer@19234
   805
    case True
schirmer@19234
   806
    note x_D = this
schirmer@19234
   807
    with Cons have hyp: "delete x (restrict D al) = restrict (D - {x}) al"
schirmer@19234
   808
      by simp
schirmer@19234
   809
    show ?thesis
schirmer@19234
   810
    proof (cases "fst a = x")
schirmer@19234
   811
      case True
schirmer@19234
   812
      from Cons.hyps
schirmer@19234
   813
      show ?thesis
schirmer@19234
   814
	using x_D True
schirmer@19234
   815
	by simp
schirmer@19234
   816
    next
schirmer@19234
   817
      case False
schirmer@19234
   818
      note not_fst_a_x = this
schirmer@19234
   819
      show ?thesis
schirmer@19234
   820
      proof (cases "fst a \<in> D")
schirmer@19234
   821
	case True 
schirmer@19234
   822
	with not_fst_a_x 
schirmer@19234
   823
	have "delete x (restrict D (a#al)) = a#(delete x (restrict D al))"
schirmer@19234
   824
	  by (cases a) (simp add: restrict_def)
schirmer@19234
   825
	also from not_fst_a_x True hyp have "\<dots> = restrict (D - {x}) (a # al)"
schirmer@19234
   826
	  by (cases a) (simp add: restrict_def)
schirmer@19234
   827
	finally show ?thesis
schirmer@19234
   828
	  using x_D by simp
schirmer@19234
   829
      next
schirmer@19234
   830
	case False
schirmer@19234
   831
	hence "delete x (restrict D (a#al)) = delete x (restrict D al)"
schirmer@19234
   832
	  by (cases a) (simp add: restrict_def)
schirmer@19234
   833
	moreover from False not_fst_a_x
schirmer@19234
   834
	have "restrict (D - {x}) (a # al) = restrict (D - {x}) al"
schirmer@19234
   835
	  by (cases a) (simp add: restrict_def)
schirmer@19234
   836
	ultimately
schirmer@19234
   837
	show ?thesis using x_D hyp by simp
schirmer@19234
   838
      qed
schirmer@19234
   839
    qed
schirmer@19234
   840
  next
schirmer@19234
   841
    case False
schirmer@19234
   842
    from False Cons show ?thesis
schirmer@19234
   843
      by simp
schirmer@19234
   844
  qed
schirmer@19234
   845
qed
schirmer@19234
   846
schirmer@19234
   847
lemma update_restr:
schirmer@19234
   848
 "map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
schirmer@19234
   849
  by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
schirmer@19234
   850
schirmer@19234
   851
lemma upate_restr_conv[simp]:
schirmer@19234
   852
 "x \<in> D \<Longrightarrow> 
schirmer@19234
   853
 map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
schirmer@19234
   854
  by (simp add: update_conv' restr_conv')
schirmer@19234
   855
schirmer@19234
   856
lemma restr_updates[simp]: "
schirmer@19234
   857
 \<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>
schirmer@19234
   858
 \<Longrightarrow> map_of (restrict D (updates xs ys al)) = 
schirmer@19234
   859
     map_of (updates xs ys (restrict (D - set xs) al))"
schirmer@19234
   860
  by (simp add: updates_conv' restr_conv')
schirmer@19234
   861
schirmer@19234
   862
lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
schirmer@19234
   863
  by (induct ps) auto
schirmer@19234
   864
schirmer@19234
   865
lemma clearjunk_restrict:
schirmer@19234
   866
 "clearjunk (restrict A al) = restrict A (clearjunk al)"
schirmer@19234
   867
  by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
schirmer@19234
   868
schirmer@19234
   869
end