src/HOL/Library/AList.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 61585 a9599d3d7610
child 62390 842917225d56
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
bulwahn@46238
     1
(*  Title:      HOL/Library/AList.thy
haftmann@34975
     2
    Author:     Norbert Schirmer, Tobias Nipkow, Martin Wildmoser, TU Muenchen
schirmer@19234
     3
*)
schirmer@19234
     4
wenzelm@60500
     5
section \<open>Implementation of Association Lists\<close>
schirmer@19234
     6
bulwahn@46238
     7
theory AList
haftmann@45990
     8
imports Main
schirmer@19234
     9
begin
schirmer@19234
    10
wenzelm@59943
    11
context
wenzelm@59943
    12
begin
wenzelm@59943
    13
wenzelm@60500
    14
text \<open>
wenzelm@56327
    15
  The operations preserve distinctness of keys and
wenzelm@56327
    16
  function @{term "clearjunk"} distributes over them. Since
haftmann@22740
    17
  @{term clearjunk} enforces distinctness of keys it can be used
haftmann@22740
    18
  to establish the invariant, e.g. for inductive proofs.
wenzelm@60500
    19
\<close>
schirmer@19234
    20
wenzelm@61585
    21
subsection \<open>\<open>update\<close> and \<open>updates\<close>\<close>
nipkow@19323
    22
wenzelm@59990
    23
qualified primrec update :: "'key \<Rightarrow> 'val \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
    24
where
wenzelm@56327
    25
  "update k v [] = [(k, v)]"
wenzelm@56327
    26
| "update k v (p # ps) = (if fst p = k then (k, v) # ps else p # update k v ps)"
schirmer@19234
    27
haftmann@34975
    28
lemma update_conv': "map_of (update k v al)  = (map_of al)(k\<mapsto>v)"
nipkow@39302
    29
  by (induct al) (auto simp add: fun_eq_iff)
wenzelm@23373
    30
haftmann@34975
    31
corollary update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
haftmann@34975
    32
  by (simp add: update_conv')
schirmer@19234
    33
schirmer@19234
    34
lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
schirmer@19234
    35
  by (induct al) auto
schirmer@19234
    36
haftmann@34975
    37
lemma update_keys:
haftmann@34975
    38
  "map fst (update k v al) =
haftmann@34975
    39
    (if k \<in> set (map fst al) then map fst al else map fst al @ [k])"
haftmann@34975
    40
  by (induct al) simp_all
haftmann@34975
    41
schirmer@19234
    42
lemma distinct_update:
wenzelm@56327
    43
  assumes "distinct (map fst al)"
schirmer@19234
    44
  shows "distinct (map fst (update k v al))"
haftmann@34975
    45
  using assms by (simp add: update_keys)
schirmer@19234
    46
wenzelm@56327
    47
lemma update_filter:
wenzelm@56327
    48
  "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
    49
  by (induct ps) auto
schirmer@19234
    50
schirmer@19234
    51
lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
schirmer@19234
    52
  by (induct al) auto
schirmer@19234
    53
schirmer@19234
    54
lemma update_nonempty [simp]: "update k v al \<noteq> []"
schirmer@19234
    55
  by (induct al) auto
schirmer@19234
    56
haftmann@34975
    57
lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v = v'"
wenzelm@56327
    58
proof (induct al arbitrary: al')
wenzelm@56327
    59
  case Nil
wenzelm@56327
    60
  then show ?case
schirmer@19234
    61
    by (cases al') (auto split: split_if_asm)
schirmer@19234
    62
next
wenzelm@56327
    63
  case Cons
wenzelm@56327
    64
  then show ?case
schirmer@19234
    65
    by (cases al') (auto split: split_if_asm)
schirmer@19234
    66
qed
schirmer@19234
    67
schirmer@19234
    68
lemma update_last [simp]: "update k v (update k v' al) = update k v al"
schirmer@19234
    69
  by (induct al) auto
schirmer@19234
    70
wenzelm@60500
    71
text \<open>Note that the lists are not necessarily the same:
wenzelm@56327
    72
        @{term "update k v (update k' v' []) = [(k', v'), (k, v)]"} and
wenzelm@60500
    73
        @{term "update k' v' (update k v []) = [(k, v), (k', v')]"}.\<close>
wenzelm@56327
    74
wenzelm@56327
    75
lemma update_swap:
wenzelm@56327
    76
  "k \<noteq> k' \<Longrightarrow>
wenzelm@56327
    77
    map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
nipkow@39302
    78
  by (simp add: update_conv' fun_eq_iff)
schirmer@19234
    79
wenzelm@56327
    80
lemma update_Some_unfold:
haftmann@34975
    81
  "map_of (update k v al) x = Some y \<longleftrightarrow>
haftmann@34975
    82
    x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y"
schirmer@19234
    83
  by (simp add: update_conv' map_upd_Some_unfold)
schirmer@19234
    84
haftmann@34975
    85
lemma image_update [simp]:
haftmann@34975
    86
  "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
haftmann@46133
    87
  by (simp add: update_conv')
schirmer@19234
    88
wenzelm@59990
    89
qualified definition
wenzelm@59943
    90
    updates :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
    91
  where "updates ks vs = fold (case_prod update) (zip ks vs)"
schirmer@19234
    92
haftmann@34975
    93
lemma updates_simps [simp]:
haftmann@34975
    94
  "updates [] vs ps = ps"
haftmann@34975
    95
  "updates ks [] ps = ps"
haftmann@34975
    96
  "updates (k#ks) (v#vs) ps = updates ks vs (update k v ps)"
haftmann@34975
    97
  by (simp_all add: updates_def)
haftmann@34975
    98
haftmann@34975
    99
lemma updates_key_simp [simp]:
haftmann@34975
   100
  "updates (k # ks) vs ps =
haftmann@34975
   101
    (case vs of [] \<Rightarrow> ps | v # vs \<Rightarrow> updates ks vs (update k v ps))"
haftmann@34975
   102
  by (cases vs) simp_all
haftmann@34975
   103
haftmann@34975
   104
lemma updates_conv': "map_of (updates ks vs al) = (map_of al)(ks[\<mapsto>]vs)"
haftmann@34975
   105
proof -
blanchet@55414
   106
  have "map_of \<circ> fold (case_prod update) (zip ks vs) =
wenzelm@56327
   107
      fold (\<lambda>(k, v) f. f(k \<mapsto> v)) (zip ks vs) \<circ> map_of"
haftmann@39921
   108
    by (rule fold_commute) (auto simp add: fun_eq_iff update_conv')
wenzelm@56327
   109
  then show ?thesis
wenzelm@56327
   110
    by (auto simp add: updates_def fun_eq_iff map_upds_fold_map_upd foldl_conv_fold split_def)
haftmann@34975
   111
qed
schirmer@19234
   112
schirmer@19234
   113
lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
haftmann@34975
   114
  by (simp add: updates_conv')
schirmer@19234
   115
schirmer@19234
   116
lemma distinct_updates:
schirmer@19234
   117
  assumes "distinct (map fst al)"
schirmer@19234
   118
  shows "distinct (map fst (updates ks vs al))"
haftmann@34975
   119
proof -
haftmann@46133
   120
  have "distinct (fold
haftmann@37458
   121
       (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k])
haftmann@37458
   122
       (zip ks vs) (map fst al))"
haftmann@37458
   123
    by (rule fold_invariant [of "zip ks vs" "\<lambda>_. True"]) (auto intro: assms)
blanchet@55414
   124
  moreover have "map fst \<circ> fold (case_prod update) (zip ks vs) =
wenzelm@56327
   125
      fold (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) (zip ks vs) \<circ> map fst"
blanchet@55414
   126
    by (rule fold_commute) (simp add: update_keys split_def case_prod_beta comp_def)
wenzelm@56327
   127
  ultimately show ?thesis
wenzelm@56327
   128
    by (simp add: updates_def fun_eq_iff)
haftmann@34975
   129
qed
schirmer@19234
   130
schirmer@19234
   131
lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
wenzelm@56327
   132
    updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
wenzelm@20503
   133
  by (induct ks arbitrary: vs al) (auto split: list.splits)
schirmer@19234
   134
schirmer@19234
   135
lemma updates_list_update_drop[simp]:
wenzelm@56327
   136
  "size ks \<le> i \<Longrightarrow> i < size vs \<Longrightarrow>
wenzelm@56327
   137
    updates ks (vs[i:=v]) al = updates ks vs al"
wenzelm@56327
   138
  by (induct ks arbitrary: al vs i) (auto split: list.splits nat.splits)
schirmer@19234
   139
wenzelm@56327
   140
lemma update_updates_conv_if:
wenzelm@56327
   141
  "map_of (updates xs ys (update x y al)) =
wenzelm@56327
   142
    map_of
wenzelm@56327
   143
     (if x \<in> set (take (length ys) xs)
wenzelm@56327
   144
      then updates xs ys al
wenzelm@56327
   145
      else (update x y (updates xs ys al)))"
schirmer@19234
   146
  by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
schirmer@19234
   147
schirmer@19234
   148
lemma updates_twist [simp]:
wenzelm@56327
   149
  "k \<notin> set ks \<Longrightarrow>
wenzelm@56327
   150
    map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
wenzelm@46507
   151
  by (simp add: updates_conv' update_conv')
schirmer@19234
   152
wenzelm@56327
   153
lemma updates_apply_notin [simp]:
wenzelm@56327
   154
  "k \<notin> set ks \<Longrightarrow> map_of (updates ks vs al) k = map_of al k"
schirmer@19234
   155
  by (simp add: updates_conv)
schirmer@19234
   156
wenzelm@56327
   157
lemma updates_append_drop [simp]:
wenzelm@56327
   158
  "size xs = size ys \<Longrightarrow> updates (xs @ zs) ys al = updates xs ys al"
wenzelm@20503
   159
  by (induct xs arbitrary: ys al) (auto split: list.splits)
schirmer@19234
   160
wenzelm@56327
   161
lemma updates_append2_drop [simp]:
wenzelm@56327
   162
  "size xs = size ys \<Longrightarrow> updates xs (ys @ zs) al = updates xs ys al"
wenzelm@20503
   163
  by (induct xs arbitrary: ys al) (auto split: list.splits)
schirmer@19234
   164
wenzelm@23373
   165
wenzelm@61585
   166
subsection \<open>\<open>delete\<close>\<close>
haftmann@34975
   167
wenzelm@59990
   168
qualified definition delete :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
   169
  where delete_eq: "delete k = filter (\<lambda>(k', _). k \<noteq> k')"
haftmann@34975
   170
haftmann@34975
   171
lemma delete_simps [simp]:
haftmann@34975
   172
  "delete k [] = []"
wenzelm@56327
   173
  "delete k (p # ps) = (if fst p = k then delete k ps else p # delete k ps)"
haftmann@34975
   174
  by (auto simp add: delete_eq)
haftmann@34975
   175
haftmann@34975
   176
lemma delete_conv': "map_of (delete k al) = (map_of al)(k := None)"
nipkow@39302
   177
  by (induct al) (auto simp add: fun_eq_iff)
haftmann@34975
   178
haftmann@34975
   179
corollary delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
haftmann@34975
   180
  by (simp add: delete_conv')
haftmann@34975
   181
wenzelm@56327
   182
lemma delete_keys: "map fst (delete k al) = removeAll k (map fst al)"
haftmann@34975
   183
  by (simp add: delete_eq removeAll_filter_not_eq filter_map split_def comp_def)
haftmann@34975
   184
haftmann@34975
   185
lemma distinct_delete:
wenzelm@56327
   186
  assumes "distinct (map fst al)"
haftmann@34975
   187
  shows "distinct (map fst (delete k al))"
haftmann@34975
   188
  using assms by (simp add: delete_keys distinct_removeAll)
haftmann@34975
   189
haftmann@34975
   190
lemma delete_id [simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
haftmann@34975
   191
  by (auto simp add: image_iff delete_eq filter_id_conv)
haftmann@34975
   192
haftmann@34975
   193
lemma delete_idem: "delete k (delete k al) = delete k al"
haftmann@34975
   194
  by (simp add: delete_eq)
haftmann@34975
   195
wenzelm@56327
   196
lemma map_of_delete [simp]: "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
haftmann@34975
   197
  by (simp add: delete_conv')
haftmann@34975
   198
haftmann@34975
   199
lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
haftmann@34975
   200
  by (auto simp add: delete_eq)
haftmann@34975
   201
haftmann@34975
   202
lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
haftmann@34975
   203
  by (auto simp add: delete_eq)
haftmann@34975
   204
wenzelm@56327
   205
lemma delete_update_same: "delete k (update k v al) = delete k al"
haftmann@34975
   206
  by (induct al) simp_all
haftmann@34975
   207
wenzelm@56327
   208
lemma delete_update: "k \<noteq> l \<Longrightarrow> delete l (update k v al) = update k v (delete l al)"
haftmann@34975
   209
  by (induct al) simp_all
haftmann@34975
   210
haftmann@34975
   211
lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
haftmann@34975
   212
  by (simp add: delete_eq conj_commute)
haftmann@34975
   213
haftmann@34975
   214
lemma length_delete_le: "length (delete k al) \<le> length al"
haftmann@34975
   215
  by (simp add: delete_eq)
haftmann@34975
   216
haftmann@34975
   217
wenzelm@61585
   218
subsection \<open>\<open>update_with_aux\<close> and \<open>delete_aux\<close>\<close>
nipkow@60043
   219
nipkow@60043
   220
qualified primrec update_with_aux :: "'val \<Rightarrow> 'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
nipkow@60043
   221
where
nipkow@60043
   222
  "update_with_aux v k f [] = [(k, f v)]"
nipkow@60043
   223
| "update_with_aux v k f (p # ps) = (if (fst p = k) then (k, f (snd p)) # ps else p # update_with_aux v k f ps)"
nipkow@60043
   224
wenzelm@60500
   225
text \<open>
nipkow@60043
   226
  The above @{term "delete"} traverses all the list even if it has found the key.
nipkow@60043
   227
  This one does not have to keep going because is assumes the invariant that keys are distinct.
wenzelm@60500
   228
\<close>
nipkow@60043
   229
qualified fun delete_aux :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
nipkow@60043
   230
where
nipkow@60043
   231
  "delete_aux k [] = []"
nipkow@60043
   232
| "delete_aux k ((k', v) # xs) = (if k = k' then xs else (k', v) # delete_aux k xs)"
nipkow@60043
   233
nipkow@60043
   234
lemma map_of_update_with_aux':
nipkow@60043
   235
  "map_of (update_with_aux v k f ps) k' = ((map_of ps)(k \<mapsto> (case map_of ps k of None \<Rightarrow> f v | Some v \<Rightarrow> f v))) k'"
nipkow@60043
   236
by(induct ps) auto
nipkow@60043
   237
nipkow@60043
   238
lemma map_of_update_with_aux:
nipkow@60043
   239
  "map_of (update_with_aux v k f ps) = (map_of ps)(k \<mapsto> (case map_of ps k of None \<Rightarrow> f v | Some v \<Rightarrow> f v))"
nipkow@60043
   240
by(simp add: fun_eq_iff map_of_update_with_aux')
nipkow@60043
   241
nipkow@60043
   242
lemma dom_update_with_aux: "fst ` set (update_with_aux v k f ps) = {k} \<union> fst ` set ps"
nipkow@60043
   243
  by (induct ps) auto
nipkow@60043
   244
nipkow@60043
   245
lemma distinct_update_with_aux [simp]:
nipkow@60043
   246
  "distinct (map fst (update_with_aux v k f ps)) = distinct (map fst ps)"
nipkow@60043
   247
by(induct ps)(auto simp add: dom_update_with_aux)
nipkow@60043
   248
nipkow@60043
   249
lemma set_update_with_aux:
nipkow@60043
   250
  "distinct (map fst xs) 
nipkow@60043
   251
  \<Longrightarrow> set (update_with_aux v k f xs) = (set xs - {k} \<times> UNIV \<union> {(k, f (case map_of xs k of None \<Rightarrow> v | Some v \<Rightarrow> v))})"
nipkow@60043
   252
by(induct xs)(auto intro: rev_image_eqI)
nipkow@60043
   253
nipkow@60043
   254
lemma set_delete_aux: "distinct (map fst xs) \<Longrightarrow> set (delete_aux k xs) = set xs - {k} \<times> UNIV"
nipkow@60043
   255
apply(induct xs)
nipkow@60043
   256
apply simp_all
nipkow@60043
   257
apply clarsimp
nipkow@60043
   258
apply(fastforce intro: rev_image_eqI)
nipkow@60043
   259
done
nipkow@60043
   260
nipkow@60043
   261
lemma dom_delete_aux: "distinct (map fst ps) \<Longrightarrow> fst ` set (delete_aux k ps) = fst ` set ps - {k}"
nipkow@60043
   262
by(auto simp add: set_delete_aux)
nipkow@60043
   263
nipkow@60043
   264
lemma distinct_delete_aux [simp]:
nipkow@60043
   265
  "distinct (map fst ps) \<Longrightarrow> distinct (map fst (delete_aux k ps))"
nipkow@60043
   266
proof(induct ps)
nipkow@60043
   267
  case Nil thus ?case by simp
nipkow@60043
   268
next
nipkow@60043
   269
  case (Cons a ps)
nipkow@60043
   270
  obtain k' v where a: "a = (k', v)" by(cases a)
nipkow@60043
   271
  show ?case
nipkow@60043
   272
  proof(cases "k' = k")
nipkow@60043
   273
    case True with Cons a show ?thesis by simp
nipkow@60043
   274
  next
nipkow@60043
   275
    case False
nipkow@60043
   276
    with Cons a have "k' \<notin> fst ` set ps" "distinct (map fst ps)" by simp_all
nipkow@60043
   277
    with False a have "k' \<notin> fst ` set (delete_aux k ps)"
nipkow@60043
   278
      by(auto dest!: dom_delete_aux[where k=k])
nipkow@60043
   279
    with Cons a show ?thesis by simp
nipkow@60043
   280
  qed
nipkow@60043
   281
qed
nipkow@60043
   282
nipkow@60043
   283
lemma map_of_delete_aux':
nipkow@60043
   284
  "distinct (map fst xs) \<Longrightarrow> map_of (delete_aux k xs) = (map_of xs)(k := None)"
nipkow@60043
   285
  apply (induct xs)
nipkow@60043
   286
  apply (fastforce simp add: map_of_eq_None_iff fun_upd_twist)
nipkow@60043
   287
  apply (auto intro!: ext)
nipkow@60043
   288
  apply (simp add: map_of_eq_None_iff)
nipkow@60043
   289
  done
nipkow@60043
   290
nipkow@60043
   291
lemma map_of_delete_aux:
nipkow@60043
   292
  "distinct (map fst xs) \<Longrightarrow> map_of (delete_aux k xs) k' = ((map_of xs)(k := None)) k'"
nipkow@60043
   293
by(simp add: map_of_delete_aux')
nipkow@60043
   294
nipkow@60043
   295
lemma delete_aux_eq_Nil_conv: "delete_aux k ts = [] \<longleftrightarrow> ts = [] \<or> (\<exists>v. ts = [(k, v)])"
nipkow@60043
   296
by(cases ts)(auto split: split_if_asm)
nipkow@60043
   297
nipkow@60043
   298
wenzelm@61585
   299
subsection \<open>\<open>restrict\<close>\<close>
haftmann@34975
   300
wenzelm@59990
   301
qualified definition restrict :: "'key set \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
   302
  where restrict_eq: "restrict A = filter (\<lambda>(k, v). k \<in> A)"
haftmann@34975
   303
haftmann@34975
   304
lemma restr_simps [simp]:
haftmann@34975
   305
  "restrict A [] = []"
haftmann@34975
   306
  "restrict A (p#ps) = (if fst p \<in> A then p # restrict A ps else restrict A ps)"
haftmann@34975
   307
  by (auto simp add: restrict_eq)
haftmann@34975
   308
haftmann@34975
   309
lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
haftmann@34975
   310
proof
haftmann@34975
   311
  fix k
haftmann@34975
   312
  show "map_of (restrict A al) k = ((map_of al)|` A) k"
haftmann@34975
   313
    by (induct al) (simp, cases "k \<in> A", auto)
haftmann@34975
   314
qed
haftmann@34975
   315
haftmann@34975
   316
corollary restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
haftmann@34975
   317
  by (simp add: restr_conv')
haftmann@34975
   318
haftmann@34975
   319
lemma distinct_restr:
haftmann@34975
   320
  "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
haftmann@34975
   321
  by (induct al) (auto simp add: restrict_eq)
haftmann@34975
   322
wenzelm@56327
   323
lemma restr_empty [simp]:
wenzelm@56327
   324
  "restrict {} al = []"
haftmann@34975
   325
  "restrict A [] = []"
haftmann@34975
   326
  by (induct al) (auto simp add: restrict_eq)
haftmann@34975
   327
haftmann@34975
   328
lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
haftmann@34975
   329
  by (simp add: restr_conv')
haftmann@34975
   330
haftmann@34975
   331
lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
haftmann@34975
   332
  by (simp add: restr_conv')
haftmann@34975
   333
haftmann@34975
   334
lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
haftmann@34975
   335
  by (induct al) (auto simp add: restrict_eq)
haftmann@34975
   336
haftmann@34975
   337
lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
haftmann@34975
   338
  by (induct al) (auto simp add: restrict_eq)
haftmann@34975
   339
haftmann@34975
   340
lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
haftmann@34975
   341
  by (induct al) (auto simp add: restrict_eq)
haftmann@34975
   342
haftmann@34975
   343
lemma restr_update[simp]:
wenzelm@56327
   344
 "map_of (restrict D (update x y al)) =
haftmann@34975
   345
  map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
haftmann@34975
   346
  by (simp add: restr_conv' update_conv')
haftmann@34975
   347
haftmann@34975
   348
lemma restr_delete [simp]:
wenzelm@56327
   349
  "delete x (restrict D al) = (if x \<in> D then restrict (D - {x}) al else restrict D al)"
wenzelm@56327
   350
  apply (simp add: delete_eq restrict_eq)
wenzelm@56327
   351
  apply (auto simp add: split_def)
haftmann@34975
   352
proof -
wenzelm@56327
   353
  have "\<And>y. y \<noteq> x \<longleftrightarrow> x \<noteq> y"
wenzelm@56327
   354
    by auto
haftmann@34975
   355
  then show "[p \<leftarrow> al. fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al. fst p \<in> D \<and> fst p \<noteq> x]"
haftmann@34975
   356
    by simp
haftmann@34975
   357
  assume "x \<notin> D"
wenzelm@56327
   358
  then have "\<And>y. y \<in> D \<longleftrightarrow> y \<in> D \<and> x \<noteq> y"
wenzelm@56327
   359
    by auto
haftmann@34975
   360
  then show "[p \<leftarrow> al . fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al . fst p \<in> D]"
haftmann@34975
   361
    by simp
haftmann@34975
   362
qed
haftmann@34975
   363
haftmann@34975
   364
lemma update_restr:
wenzelm@56327
   365
  "map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))"
haftmann@34975
   366
  by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
schirmer@19234
   367
bulwahn@45867
   368
lemma update_restr_conv [simp]:
wenzelm@56327
   369
  "x \<in> D \<Longrightarrow>
wenzelm@56327
   370
    map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))"
haftmann@34975
   371
  by (simp add: update_conv' restr_conv')
haftmann@34975
   372
wenzelm@56327
   373
lemma restr_updates [simp]:
wenzelm@56327
   374
  "length xs = length ys \<Longrightarrow> set xs \<subseteq> D \<Longrightarrow>
wenzelm@56327
   375
    map_of (restrict D (updates xs ys al)) =
wenzelm@56327
   376
      map_of (updates xs ys (restrict (D - set xs) al))"
haftmann@34975
   377
  by (simp add: updates_conv' restr_conv')
haftmann@34975
   378
haftmann@34975
   379
lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
haftmann@34975
   380
  by (induct ps) auto
haftmann@34975
   381
haftmann@34975
   382
wenzelm@61585
   383
subsection \<open>\<open>clearjunk\<close>\<close>
haftmann@34975
   384
wenzelm@59990
   385
qualified function clearjunk  :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
   386
where
wenzelm@56327
   387
  "clearjunk [] = []"
wenzelm@56327
   388
| "clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
haftmann@34975
   389
  by pat_completeness auto
wenzelm@56327
   390
termination
wenzelm@56327
   391
  by (relation "measure length") (simp_all add: less_Suc_eq_le length_delete_le)
haftmann@34975
   392
wenzelm@56327
   393
lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
wenzelm@56327
   394
  by (induct al rule: clearjunk.induct) (simp_all add: fun_eq_iff)
haftmann@34975
   395
wenzelm@56327
   396
lemma clearjunk_keys_set: "set (map fst (clearjunk al)) = set (map fst al)"
wenzelm@56327
   397
  by (induct al rule: clearjunk.induct) (simp_all add: delete_keys)
haftmann@34975
   398
wenzelm@56327
   399
lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
haftmann@34975
   400
  using clearjunk_keys_set by simp
haftmann@34975
   401
wenzelm@56327
   402
lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
wenzelm@56327
   403
  by (induct al rule: clearjunk.induct) (simp_all del: set_map add: clearjunk_keys_set delete_keys)
haftmann@34975
   404
wenzelm@56327
   405
lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
haftmann@34975
   406
  by (simp add: map_of_clearjunk)
haftmann@34975
   407
wenzelm@56327
   408
lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
haftmann@34975
   409
proof -
haftmann@34975
   410
  have "ran (map_of al) = ran (map_of (clearjunk al))"
haftmann@34975
   411
    by (simp add: ran_clearjunk)
haftmann@34975
   412
  also have "\<dots> = snd ` set (clearjunk al)"
haftmann@34975
   413
    by (simp add: ran_distinct)
haftmann@34975
   414
  finally show ?thesis .
haftmann@34975
   415
qed
haftmann@34975
   416
wenzelm@56327
   417
lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
wenzelm@56327
   418
  by (induct al rule: clearjunk.induct) (simp_all add: delete_update)
schirmer@19234
   419
wenzelm@56327
   420
lemma clearjunk_updates: "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
haftmann@34975
   421
proof -
blanchet@55414
   422
  have "clearjunk \<circ> fold (case_prod update) (zip ks vs) =
blanchet@55414
   423
    fold (case_prod update) (zip ks vs) \<circ> clearjunk"
blanchet@55414
   424
    by (rule fold_commute) (simp add: clearjunk_update case_prod_beta o_def)
wenzelm@56327
   425
  then show ?thesis
wenzelm@56327
   426
    by (simp add: updates_def fun_eq_iff)
haftmann@34975
   427
qed
haftmann@34975
   428
wenzelm@56327
   429
lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
haftmann@34975
   430
  by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
haftmann@34975
   431
wenzelm@56327
   432
lemma clearjunk_restrict: "clearjunk (restrict A al) = restrict A (clearjunk al)"
haftmann@34975
   433
  by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
haftmann@34975
   434
wenzelm@56327
   435
lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
haftmann@34975
   436
  by (induct al rule: clearjunk.induct) auto
haftmann@34975
   437
wenzelm@56327
   438
lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
haftmann@34975
   439
  by simp
haftmann@34975
   440
wenzelm@56327
   441
lemma length_clearjunk: "length (clearjunk al) \<le> length al"
haftmann@34975
   442
proof (induct al rule: clearjunk.induct [case_names Nil Cons])
wenzelm@56327
   443
  case Nil
wenzelm@56327
   444
  then show ?case by simp
haftmann@34975
   445
next
haftmann@34975
   446
  case (Cons kv al)
wenzelm@56327
   447
  moreover have "length (delete (fst kv) al) \<le> length al"
wenzelm@56327
   448
    by (fact length_delete_le)
wenzelm@56327
   449
  ultimately have "length (clearjunk (delete (fst kv) al)) \<le> length al"
wenzelm@56327
   450
    by (rule order_trans)
wenzelm@56327
   451
  then show ?case
wenzelm@56327
   452
    by simp
haftmann@34975
   453
qed
haftmann@34975
   454
haftmann@34975
   455
lemma delete_map:
haftmann@34975
   456
  assumes "\<And>kv. fst (f kv) = fst kv"
haftmann@34975
   457
  shows "delete k (map f ps) = map f (delete k ps)"
haftmann@34975
   458
  by (simp add: delete_eq filter_map comp_def split_def assms)
haftmann@34975
   459
haftmann@34975
   460
lemma clearjunk_map:
haftmann@34975
   461
  assumes "\<And>kv. fst (f kv) = fst kv"
haftmann@34975
   462
  shows "clearjunk (map f ps) = map f (clearjunk ps)"
haftmann@34975
   463
  by (induct ps rule: clearjunk.induct [case_names Nil Cons])
haftmann@34975
   464
    (simp_all add: clearjunk_delete delete_map assms)
haftmann@34975
   465
haftmann@34975
   466
wenzelm@61585
   467
subsection \<open>\<open>map_ran\<close>\<close>
haftmann@34975
   468
wenzelm@56327
   469
definition map_ran :: "('key \<Rightarrow> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
   470
  where "map_ran f = map (\<lambda>(k, v). (k, f k v))"
haftmann@34975
   471
haftmann@34975
   472
lemma map_ran_simps [simp]:
haftmann@34975
   473
  "map_ran f [] = []"
haftmann@34975
   474
  "map_ran f ((k, v) # ps) = (k, f k v) # map_ran f ps"
haftmann@34975
   475
  by (simp_all add: map_ran_def)
haftmann@34975
   476
wenzelm@56327
   477
lemma dom_map_ran: "fst ` set (map_ran f al) = fst ` set al"
haftmann@34975
   478
  by (simp add: map_ran_def image_image split_def)
wenzelm@56327
   479
wenzelm@56327
   480
lemma map_ran_conv: "map_of (map_ran f al) k = map_option (f k) (map_of al k)"
schirmer@19234
   481
  by (induct al) auto
schirmer@19234
   482
wenzelm@56327
   483
lemma distinct_map_ran: "distinct (map fst al) \<Longrightarrow> distinct (map fst (map_ran f al))"
haftmann@34975
   484
  by (simp add: map_ran_def split_def comp_def)
schirmer@19234
   485
wenzelm@56327
   486
lemma map_ran_filter: "map_ran f [p\<leftarrow>ps. fst p \<noteq> a] = [p\<leftarrow>map_ran f ps. fst p \<noteq> a]"
haftmann@34975
   487
  by (simp add: map_ran_def filter_map split_def comp_def)
schirmer@19234
   488
wenzelm@56327
   489
lemma clearjunk_map_ran: "clearjunk (map_ran f al) = map_ran f (clearjunk al)"
haftmann@34975
   490
  by (simp add: map_ran_def split_def clearjunk_map)
schirmer@19234
   491
wenzelm@23373
   492
wenzelm@61585
   493
subsection \<open>\<open>merge\<close>\<close>
haftmann@34975
   494
wenzelm@59990
   495
qualified definition merge :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327
   496
  where "merge qs ps = foldr (\<lambda>(k, v). update k v) ps qs"
haftmann@34975
   497
haftmann@34975
   498
lemma merge_simps [simp]:
haftmann@34975
   499
  "merge qs [] = qs"
haftmann@34975
   500
  "merge qs (p#ps) = update (fst p) (snd p) (merge qs ps)"
haftmann@34975
   501
  by (simp_all add: merge_def split_def)
haftmann@34975
   502
wenzelm@56327
   503
lemma merge_updates: "merge qs ps = updates (rev (map fst ps)) (rev (map snd ps)) qs"
haftmann@47397
   504
  by (simp add: merge_def updates_def foldr_conv_fold zip_rev zip_map_fst_snd)
schirmer@19234
   505
schirmer@19234
   506
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
wenzelm@20503
   507
  by (induct ys arbitrary: 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))"
wenzelm@56327
   512
  using assms by (simp add: merge_updates distinct_updates)
schirmer@19234
   513
wenzelm@56327
   514
lemma clearjunk_merge: "clearjunk (merge xs ys) = merge (clearjunk xs) ys"
haftmann@34975
   515
  by (simp add: merge_updates clearjunk_updates)
schirmer@19234
   516
wenzelm@56327
   517
lemma merge_conv': "map_of (merge xs ys) = map_of xs ++ map_of ys"
haftmann@34975
   518
proof -
blanchet@55414
   519
  have "map_of \<circ> fold (case_prod update) (rev ys) =
wenzelm@56327
   520
      fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (rev ys) \<circ> map_of"
blanchet@55414
   521
    by (rule fold_commute) (simp add: update_conv' case_prod_beta split_def fun_eq_iff)
haftmann@34975
   522
  then show ?thesis
haftmann@47397
   523
    by (simp add: merge_def map_add_map_of_foldr foldr_conv_fold fun_eq_iff)
schirmer@19234
   524
qed
schirmer@19234
   525
wenzelm@56327
   526
corollary merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
haftmann@34975
   527
  by (simp add: merge_conv')
schirmer@19234
   528
haftmann@34975
   529
lemma merge_empty: "map_of (merge [] ys) = map_of ys"
schirmer@19234
   530
  by (simp add: merge_conv')
schirmer@19234
   531
wenzelm@56327
   532
lemma merge_assoc [simp]: "map_of (merge m1 (merge m2 m3)) = map_of (merge (merge m1 m2) m3)"
schirmer@19234
   533
  by (simp add: merge_conv')
schirmer@19234
   534
wenzelm@56327
   535
lemma merge_Some_iff:
wenzelm@56327
   536
  "map_of (merge m n) k = Some x \<longleftrightarrow>
wenzelm@56327
   537
    map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x"
schirmer@19234
   538
  by (simp add: merge_conv' map_add_Some_iff)
schirmer@19234
   539
wenzelm@45605
   540
lemmas merge_SomeD [dest!] = merge_Some_iff [THEN iffD1]
schirmer@19234
   541
wenzelm@56327
   542
lemma merge_find_right [simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
schirmer@19234
   543
  by (simp add: merge_conv')
schirmer@19234
   544
wenzelm@56327
   545
lemma merge_None [iff]:
schirmer@19234
   546
  "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
schirmer@19234
   547
  by (simp add: merge_conv')
schirmer@19234
   548
wenzelm@56327
   549
lemma merge_upd [simp]:
schirmer@19234
   550
  "map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
schirmer@19234
   551
  by (simp add: update_conv' merge_conv')
schirmer@19234
   552
wenzelm@56327
   553
lemma merge_updatess [simp]:
schirmer@19234
   554
  "map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
schirmer@19234
   555
  by (simp add: updates_conv' merge_conv')
schirmer@19234
   556
wenzelm@56327
   557
lemma merge_append: "map_of (xs @ ys) = map_of (merge ys xs)"
schirmer@19234
   558
  by (simp add: merge_conv')
schirmer@19234
   559
wenzelm@23373
   560
wenzelm@61585
   561
subsection \<open>\<open>compose\<close>\<close>
haftmann@34975
   562
wenzelm@59990
   563
qualified function compose :: "('key \<times> 'a) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('key \<times> 'b) list"
wenzelm@56327
   564
where
wenzelm@56327
   565
  "compose [] ys = []"
wenzelm@56327
   566
| "compose (x # xs) ys =
wenzelm@56327
   567
    (case map_of ys (snd x) of
wenzelm@56327
   568
      None \<Rightarrow> compose (delete (fst x) xs) ys
wenzelm@56327
   569
    | Some v \<Rightarrow> (fst x, v) # compose xs ys)"
haftmann@34975
   570
  by pat_completeness auto
wenzelm@56327
   571
termination
wenzelm@56327
   572
  by (relation "measure (length \<circ> fst)") (simp_all add: less_Suc_eq_le length_delete_le)
schirmer@19234
   573
wenzelm@56327
   574
lemma compose_first_None [simp]:
wenzelm@56327
   575
  assumes "map_of xs k = None"
schirmer@19234
   576
  shows "map_of (compose xs ys) k = None"
wenzelm@56327
   577
  using assms by (induct xs ys rule: compose.induct) (auto split: option.splits split_if_asm)
schirmer@19234
   578
wenzelm@56327
   579
lemma compose_conv: "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
haftmann@22916
   580
proof (induct xs ys rule: compose.induct)
wenzelm@56327
   581
  case 1
wenzelm@56327
   582
  then show ?case by simp
schirmer@19234
   583
next
wenzelm@56327
   584
  case (2 x xs ys)
wenzelm@56327
   585
  show ?case
schirmer@19234
   586
  proof (cases "map_of ys (snd x)")
wenzelm@56327
   587
    case None
wenzelm@56327
   588
    with 2 have hyp: "map_of (compose (delete (fst x) xs) ys) k =
wenzelm@56327
   589
        (map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
schirmer@19234
   590
      by simp
schirmer@19234
   591
    show ?thesis
schirmer@19234
   592
    proof (cases "fst x = k")
schirmer@19234
   593
      case True
schirmer@19234
   594
      from True delete_notin_dom [of k xs]
schirmer@19234
   595
      have "map_of (delete (fst x) xs) k = None"
wenzelm@32960
   596
        by (simp add: map_of_eq_None_iff)
schirmer@19234
   597
      with hyp show ?thesis
wenzelm@32960
   598
        using True None
wenzelm@32960
   599
        by simp
schirmer@19234
   600
    next
schirmer@19234
   601
      case False
schirmer@19234
   602
      from False have "map_of (delete (fst x) xs) k = map_of xs k"
wenzelm@32960
   603
        by simp
schirmer@19234
   604
      with hyp show ?thesis
wenzelm@56327
   605
        using False None by (simp add: map_comp_def)
schirmer@19234
   606
    qed
schirmer@19234
   607
  next
schirmer@19234
   608
    case (Some v)
haftmann@22916
   609
    with 2
schirmer@19234
   610
    have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
schirmer@19234
   611
      by simp
schirmer@19234
   612
    with Some show ?thesis
schirmer@19234
   613
      by (auto simp add: map_comp_def)
schirmer@19234
   614
  qed
schirmer@19234
   615
qed
wenzelm@56327
   616
wenzelm@56327
   617
lemma compose_conv': "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
schirmer@19234
   618
  by (rule ext) (rule compose_conv)
schirmer@19234
   619
schirmer@19234
   620
lemma compose_first_Some [simp]:
wenzelm@56327
   621
  assumes "map_of xs k = Some v"
schirmer@19234
   622
  shows "map_of (compose xs ys) k = map_of ys v"
wenzelm@56327
   623
  using assms by (simp add: compose_conv)
schirmer@19234
   624
schirmer@19234
   625
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
haftmann@22916
   626
proof (induct xs ys rule: compose.induct)
wenzelm@56327
   627
  case 1
wenzelm@56327
   628
  then show ?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.hyps"
schirmer@19234
   635
    have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
schirmer@19234
   636
      by simp
schirmer@19234
   637
    also
schirmer@19234
   638
    have "\<dots> \<subseteq> fst ` set xs"
schirmer@19234
   639
      by (rule dom_delete_subset)
schirmer@19234
   640
    finally show ?thesis
schirmer@19234
   641
      using None
schirmer@19234
   642
      by auto
schirmer@19234
   643
  next
schirmer@19234
   644
    case (Some v)
haftmann@22916
   645
    with "2.hyps"
schirmer@19234
   646
    have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
schirmer@19234
   647
      by simp
schirmer@19234
   648
    with Some show ?thesis
schirmer@19234
   649
      by auto
schirmer@19234
   650
  qed
schirmer@19234
   651
qed
schirmer@19234
   652
schirmer@19234
   653
lemma distinct_compose:
wenzelm@56327
   654
  assumes "distinct (map fst xs)"
wenzelm@56327
   655
  shows "distinct (map fst (compose xs ys))"
wenzelm@56327
   656
  using assms
haftmann@22916
   657
proof (induct xs ys rule: compose.induct)
wenzelm@56327
   658
  case 1
wenzelm@56327
   659
  then show ?case by simp
schirmer@19234
   660
next
haftmann@22916
   661
  case (2 x xs ys)
schirmer@19234
   662
  show ?case
schirmer@19234
   663
  proof (cases "map_of ys (snd x)")
schirmer@19234
   664
    case None
haftmann@22916
   665
    with 2 show ?thesis by simp
schirmer@19234
   666
  next
schirmer@19234
   667
    case (Some v)
wenzelm@56327
   668
    with 2 dom_compose [of xs ys] show ?thesis
wenzelm@56327
   669
      by auto
schirmer@19234
   670
  qed
schirmer@19234
   671
qed
schirmer@19234
   672
wenzelm@56327
   673
lemma compose_delete_twist: "compose (delete k xs) ys = delete k (compose xs ys)"
haftmann@22916
   674
proof (induct xs ys rule: compose.induct)
wenzelm@56327
   675
  case 1
wenzelm@56327
   676
  then show ?case by simp
schirmer@19234
   677
next
haftmann@22916
   678
  case (2 x xs ys)
schirmer@19234
   679
  show ?case
schirmer@19234
   680
  proof (cases "map_of ys (snd x)")
schirmer@19234
   681
    case None
wenzelm@56327
   682
    with 2 have hyp: "compose (delete k (delete (fst x) xs)) ys =
wenzelm@56327
   683
        delete k (compose (delete (fst x) xs) ys)"
schirmer@19234
   684
      by simp
schirmer@19234
   685
    show ?thesis
schirmer@19234
   686
    proof (cases "fst x = k")
schirmer@19234
   687
      case True
wenzelm@56327
   688
      with None hyp show ?thesis
wenzelm@32960
   689
        by (simp add: delete_idem)
schirmer@19234
   690
    next
schirmer@19234
   691
      case False
wenzelm@56327
   692
      from None False hyp show ?thesis
wenzelm@32960
   693
        by (simp add: delete_twist)
schirmer@19234
   694
    qed
schirmer@19234
   695
  next
schirmer@19234
   696
    case (Some v)
wenzelm@56327
   697
    with 2 have hyp: "compose (delete k xs) ys = delete k (compose xs ys)"
wenzelm@56327
   698
      by simp
schirmer@19234
   699
    with Some show ?thesis
schirmer@19234
   700
      by simp
schirmer@19234
   701
  qed
schirmer@19234
   702
qed
schirmer@19234
   703
schirmer@19234
   704
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
wenzelm@56327
   705
  by (induct xs ys rule: compose.induct)
wenzelm@56327
   706
    (auto simp add: map_of_clearjunk split: option.splits)
wenzelm@56327
   707
schirmer@19234
   708
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
schirmer@19234
   709
  by (induct xs rule: clearjunk.induct)
wenzelm@56327
   710
    (auto split: option.splits simp add: clearjunk_delete delete_idem compose_delete_twist)
wenzelm@56327
   711
wenzelm@56327
   712
lemma compose_empty [simp]: "compose xs [] = []"
haftmann@22916
   713
  by (induct xs) (auto simp add: compose_delete_twist)
schirmer@19234
   714
schirmer@19234
   715
lemma compose_Some_iff:
wenzelm@56327
   716
  "(map_of (compose xs ys) k = Some v) \<longleftrightarrow>
wenzelm@56327
   717
    (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)"
schirmer@19234
   718
  by (simp add: compose_conv map_comp_Some_iff)
schirmer@19234
   719
schirmer@19234
   720
lemma map_comp_None_iff:
wenzelm@56327
   721
  "map_of (compose xs ys) k = None \<longleftrightarrow>
wenzelm@56327
   722
    (map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None))"
schirmer@19234
   723
  by (simp add: compose_conv map_comp_None_iff)
schirmer@19234
   724
wenzelm@56327
   725
wenzelm@61585
   726
subsection \<open>\<open>map_entry\<close>\<close>
bulwahn@45869
   727
wenzelm@59990
   728
qualified fun map_entry :: "'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bulwahn@45869
   729
where
bulwahn@45869
   730
  "map_entry k f [] = []"
wenzelm@56327
   731
| "map_entry k f (p # ps) =
wenzelm@56327
   732
    (if fst p = k then (k, f (snd p)) # ps else p # map_entry k f ps)"
bulwahn@45869
   733
bulwahn@45869
   734
lemma map_of_map_entry:
wenzelm@56327
   735
  "map_of (map_entry k f xs) =
wenzelm@56327
   736
    (map_of xs)(k := case map_of xs k of None \<Rightarrow> None | Some v' \<Rightarrow> Some (f v'))"
wenzelm@56327
   737
  by (induct xs) auto
bulwahn@45869
   738
wenzelm@56327
   739
lemma dom_map_entry: "fst ` set (map_entry k f xs) = fst ` set xs"
wenzelm@56327
   740
  by (induct xs) auto
bulwahn@45869
   741
bulwahn@45869
   742
lemma distinct_map_entry:
bulwahn@45869
   743
  assumes "distinct (map fst xs)"
bulwahn@45869
   744
  shows "distinct (map fst (map_entry k f xs))"
wenzelm@56327
   745
  using assms by (induct xs) (auto simp add: dom_map_entry)
wenzelm@56327
   746
bulwahn@45869
   747
wenzelm@61585
   748
subsection \<open>\<open>map_default\<close>\<close>
bulwahn@45868
   749
bulwahn@45868
   750
fun map_default :: "'key \<Rightarrow> 'val \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bulwahn@45868
   751
where
bulwahn@45868
   752
  "map_default k v f [] = [(k, v)]"
wenzelm@56327
   753
| "map_default k v f (p # ps) =
wenzelm@56327
   754
    (if fst p = k then (k, f (snd p)) # ps else p # map_default k v f ps)"
bulwahn@45868
   755
bulwahn@45868
   756
lemma map_of_map_default:
wenzelm@56327
   757
  "map_of (map_default k v f xs) =
wenzelm@56327
   758
    (map_of xs)(k := case map_of xs k of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f v'))"
wenzelm@56327
   759
  by (induct xs) auto
bulwahn@45868
   760
wenzelm@56327
   761
lemma dom_map_default: "fst ` set (map_default k v f xs) = insert k (fst ` set xs)"
wenzelm@56327
   762
  by (induct xs) auto
bulwahn@45868
   763
bulwahn@45868
   764
lemma distinct_map_default:
bulwahn@45868
   765
  assumes "distinct (map fst xs)"
bulwahn@45868
   766
  shows "distinct (map fst (map_default k v f xs))"
wenzelm@56327
   767
  using assms by (induct xs) (auto simp add: dom_map_default)
bulwahn@45868
   768
wenzelm@59943
   769
end
bulwahn@45884
   770
schirmer@19234
   771
end