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