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