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