src/HOL/Data_Structures/AList_Upd_Del.thy
author wenzelm
Fri, 12 Jan 2018 14:08:53 +0100
changeset 67406 23307fd33906
parent 66441 b9468503742a
child 69505 cc2d676d5395
permissions -rw-r--r--
isabelle update_cartouches -c;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     2
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66441
diff changeset
     3
section \<open>Association List Update and Deletion\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     5
theory AList_Upd_Del
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     6
imports Sorted_Less
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     7
begin
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     8
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
     9
abbreviation "sorted1 ps \<equiv> sorted(map fst ps)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    10
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66441
diff changeset
    11
text\<open>Define own @{text map_of} function to avoid pulling in an unknown
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 66441
diff changeset
    12
amount of lemmas implicitly (via the simpset).\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    13
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    14
hide_const (open) map_of
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    15
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    16
fun map_of :: "('a*'b)list \<Rightarrow> 'a \<Rightarrow> 'b option" where
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    17
"map_of [] = (\<lambda>x. None)" |
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    18
"map_of ((a,b)#ps) = (\<lambda>x. if x=a then Some b else map_of ps x)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    19
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    20
text \<open>Updating an association list:\<close>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    21
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    22
fun upd_list :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) list \<Rightarrow> ('a*'b) list" where
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    23
"upd_list x y [] = [(x,y)]" |
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    24
"upd_list x y ((a,b)#ps) =
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    25
  (if x < a then (x,y)#(a,b)#ps else
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    26
  if x = a then (x,y)#ps else (a,b) # upd_list x y ps)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    27
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    28
fun del_list :: "'a::linorder \<Rightarrow> ('a*'b)list \<Rightarrow> ('a*'b)list" where
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    29
"del_list x [] = []" |
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    30
"del_list x ((a,b)#ps) = (if x = a then ps else (a,b) # del_list x ps)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    31
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    32
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    33
subsection \<open>Lemmas for @{const map_of}\<close>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    34
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    35
lemma map_of_ins_list: "map_of (upd_list x y ps) = (map_of ps)(x := Some y)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    36
by(induction ps) auto
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    37
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    38
lemma map_of_append: "map_of (ps @ qs) x =
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    39
  (case map_of ps x of None \<Rightarrow> map_of qs x | Some y \<Rightarrow> Some y)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    40
by(induction ps)(auto)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    41
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    42
lemma map_of_None: "sorted (x # map fst ps) \<Longrightarrow> map_of ps x = None"
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
    43
by (induction ps) (fastforce simp: sorted_lems sorted_wrt_Cons)+
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    44
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    45
lemma map_of_None2: "sorted (map fst ps @ [x]) \<Longrightarrow> map_of ps x = None"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    46
by (induction ps) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    47
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    48
lemma map_of_del_list: "sorted1 ps \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    49
  map_of(del_list x ps) = (map_of ps)(x := None)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    50
by(induction ps) (auto simp: map_of_None sorted_lems fun_eq_iff)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    51
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    52
lemma map_of_sorted_Cons: "sorted (a # map fst ps) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    53
   map_of ps x = None"
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
    54
by (simp add: map_of_None sorted_Cons_le)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    55
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    56
lemma map_of_sorted_snoc: "sorted (map fst ps @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    57
  map_of ps x = None"
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
    58
by (simp add: map_of_None2 sorted_snoc_le)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    59
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    60
lemmas map_of_sorteds = map_of_sorted_Cons map_of_sorted_snoc
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    61
lemmas map_of_simps = sorted_lems map_of_append map_of_sorteds
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    62
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    63
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    64
subsection \<open>Lemmas for @{const upd_list}\<close>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    65
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    66
lemma sorted_upd_list: "sorted1 ps \<Longrightarrow> sorted1 (upd_list x y ps)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    67
apply(induction ps)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    68
 apply simp
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    69
apply(case_tac ps)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    70
 apply auto
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    71
done
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    72
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    73
lemma upd_list_sorted: "sorted1 (ps @ [(a,b)]) \<Longrightarrow>
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    74
  upd_list x y (ps @ (a,b) # qs) =
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    75
    (if x < a then upd_list x y ps @ (a,b) # qs
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    76
    else ps @ upd_list x y ((a,b) # qs))"
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    77
by(induction ps) (auto simp: sorted_lems)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    78
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    79
text\<open>In principle, @{thm upd_list_sorted} suffices, but the following two
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    80
corollaries speed up proofs.\<close>
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    81
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    82
corollary upd_list_sorted1: "\<lbrakk> sorted (map fst ps @ [a]); x < a \<rbrakk> \<Longrightarrow>
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    83
  upd_list x y (ps @ (a,b) # qs) =  upd_list x y ps @ (a,b) # qs"
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    84
by (auto simp: upd_list_sorted)
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    85
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    86
corollary upd_list_sorted2: "\<lbrakk> sorted (map fst ps @ [a]); a \<le> x \<rbrakk> \<Longrightarrow>
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    87
  upd_list x y (ps @ (a,b) # qs) = ps @ upd_list x y ((a,b) # qs)"
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
    88
by (auto simp: upd_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    89
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    90
lemmas upd_list_simps = sorted_lems upd_list_sorted1 upd_list_sorted2
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
    91
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    92
text\<open>Splay trees need two additional @{const upd_list} lemmas:\<close>
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    93
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    94
lemma upd_list_Cons:
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    95
  "sorted1 ((x,y) # xs) \<Longrightarrow> upd_list x y xs = (x,y) # xs"
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    96
by (induction xs) auto
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    97
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    98
lemma upd_list_snoc:
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
    99
  "sorted1 (xs @ [(x,y)]) \<Longrightarrow> upd_list x y xs = xs @ [(x,y)]"
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   100
by(induction xs) (auto simp add: sorted_mid_iff2)
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   101
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   102
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   103
subsection \<open>Lemmas for @{const del_list}\<close>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   104
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   105
lemma sorted_del_list: "sorted1 ps \<Longrightarrow> sorted1 (del_list x ps)"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   106
apply(induction ps)
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   107
 apply simp
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   108
apply(case_tac ps)
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
   109
apply (auto simp: sorted_Cons_le)
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
   110
done
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   111
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   112
lemma del_list_idem: "x \<notin> set(map fst xs) \<Longrightarrow> del_list x xs = xs"
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   113
by (induct xs) auto
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   114
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   115
lemma del_list_sorted: "sorted1 (ps @ (a,b) # qs) \<Longrightarrow>
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   116
  del_list x (ps @ (a,b) # qs) =
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   117
    (if x < a then del_list x ps @ (a,b) # qs
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   118
     else ps @ del_list x ((a,b) # qs))"
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   119
by(induction ps)
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
   120
  (fastforce simp: sorted_lems sorted_wrt_Cons intro!: del_list_idem)+
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   121
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   122
text\<open>In principle, @{thm del_list_sorted} suffices, but the following
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   123
corollaries speed up proofs.\<close>
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   124
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   125
corollary del_list_sorted1: "sorted1 (xs @ (a,b) # ys) \<Longrightarrow> a \<le> x \<Longrightarrow>
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   126
  del_list x (xs @ (a,b) # ys) = xs @ del_list x ((a,b) # ys)"
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   127
by (auto simp: del_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   128
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   129
lemma del_list_sorted2: "sorted1 (xs @ (a,b) # ys) \<Longrightarrow> x < a \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   130
  del_list x (xs @ (a,b) # ys) = del_list x xs @ (a,b) # ys"
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   131
by (auto simp: del_list_sorted)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   132
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   133
lemma del_list_sorted3:
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   134
  "sorted1 (xs @ (a,a') # ys @ (b,b') # zs) \<Longrightarrow> x < b \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   135
  del_list x (xs @ (a,a') # ys @ (b,b') # zs) = del_list x (xs @ (a,a') # ys) @ (b,b') # zs"
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   136
by (auto simp: del_list_sorted sorted_lems)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   137
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   138
lemma del_list_sorted4:
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   139
  "sorted1 (xs @ (a,a') # ys @ (b,b') # zs @ (c,c') # us) \<Longrightarrow> x < c \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   140
  del_list x (xs @ (a,a') # ys @ (b,b') # zs @ (c,c') # us) = del_list x (xs @ (a,a') # ys @ (b,b') # zs) @ (c,c') # us"
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   141
by (auto simp: del_list_sorted sorted_lems)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   142
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   143
lemma del_list_sorted5:
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   144
  "sorted1 (xs @ (a,a') # ys @ (b,b') # zs @ (c,c') # us @ (d,d') # vs) \<Longrightarrow> x < d \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   145
   del_list x (xs @ (a,a') # ys @ (b,b') # zs @ (c,c') # us @ (d,d') # vs) =
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   146
   del_list x (xs @ (a,a') # ys @ (b,b') # zs @ (c,c') # us) @ (d,d') # vs" 
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   147
by (auto simp: del_list_sorted sorted_lems)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   148
61695
df4a03527b56 derive lemmas uniformly
nipkow
parents: 61693
diff changeset
   149
lemmas del_list_simps = sorted_lems
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   150
  del_list_sorted1
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   151
  del_list_sorted2
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   152
  del_list_sorted3
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   153
  del_list_sorted4
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   154
  del_list_sorted5
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   155
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   156
text\<open>Splay trees need two additional @{const del_list} lemmas:\<close>
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   157
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   158
lemma del_list_notin_Cons: "sorted (x # map fst xs) \<Longrightarrow> del_list x xs = xs"
66441
b9468503742a more reorganization around sorted_wrt
nipkow
parents: 61696
diff changeset
   159
by(induction xs)(fastforce simp: sorted_wrt_Cons)+
61696
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   160
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   161
lemma del_list_sorted_app:
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   162
  "sorted(map fst xs @ [x]) \<Longrightarrow> del_list x (xs @ ys) = xs @ del_list x ys"
ce6320b9ef9b moved lemmas
nipkow
parents: 61695
diff changeset
   163
by (induction xs) (auto simp: sorted_mid_iff2)
61640
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   164
44c9198f210c no CRLF
nipkow
parents: 61534
diff changeset
   165
end