src/HOL/Data_Structures/AList_Upd_Del.thy
author nipkow
Wed, 23 Sep 2015 09:14:22 +0200
changeset 61231 cc6969542f8d
parent 61224 759b5299a9f2
child 61534 a88e07c8d0d5
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     2
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     3
section {* Association List Update and Deletion *}
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     4
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     5
theory AList_Upd_Del
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     6
imports Sorted_Less
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     7
begin
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     8
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
     9
abbreviation "sorted1 ps \<equiv> sorted(map fst ps)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    10
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    11
text{* Define own @{text map_of} function to avoid pulling in an unknown
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    12
amount of lemmas implicitly (via the simpset). *}
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    13
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    14
hide_const (open) map_of
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    15
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    16
fun map_of :: "('a*'b)list \<Rightarrow> 'a \<Rightarrow> 'b option" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    17
"map_of [] = (\<lambda>a. None)" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    18
"map_of ((x,y)#ps) = (\<lambda>a. if x=a then Some y else map_of ps a)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    19
61231
nipkow
parents: 61224
diff changeset
    20
text \<open>Updating an association list:\<close>
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    21
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    22
fun upd_list :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) list \<Rightarrow> ('a*'b) list" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    23
"upd_list a b [] = [(a,b)]" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    24
"upd_list a b ((x,y)#ps) =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    25
  (if a < x then (a,b)#(x,y)#ps else
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    26
  if a=x then (a,b)#ps else (x,y) # upd_list a b ps)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    27
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    28
fun del_list :: "'a::linorder \<Rightarrow> ('a*'b)list \<Rightarrow> ('a*'b)list" where
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    29
"del_list a [] = []" |
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    30
"del_list a ((x,y)#ps) = (if a=x then ps else (x,y) # del_list a ps)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    31
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    32
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    33
subsection \<open>Lemmas for @{const map_of}\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    34
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    35
lemma map_of_ins_list: "map_of (upd_list a b ps) = (map_of ps)(a := Some b)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    36
by(induction ps) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    37
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    38
lemma map_of_append: "map_of (ps @ qs) a =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    39
  (case map_of ps a of None \<Rightarrow> map_of qs a | Some b \<Rightarrow> Some b)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    40
by(induction ps)(auto)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    41
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    42
lemma map_of_None: "sorted (a # map fst ps) \<Longrightarrow> map_of ps a = None"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    43
by (induction ps) (auto simp: sorted_lems sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    44
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    45
lemma map_of_None2: "sorted (map fst ps @ [a]) \<Longrightarrow> map_of ps a = None"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    46
by (induction ps) (auto simp: sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    47
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    48
lemma map_of_del_list: "sorted1 ps \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    49
  map_of(del_list a ps) = (map_of ps)(a := None)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    50
by(induction ps) (auto simp: map_of_None sorted_lems fun_eq_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    51
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    52
lemma map_of_sorted_Cons: "sorted (a # map fst ps) \<Longrightarrow> x < a \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    53
   map_of ps x = None"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    54
by (meson less_trans map_of_None sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    55
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    56
lemma map_of_sorted_snoc: "sorted (map fst ps @ [a]) \<Longrightarrow> a \<le> x \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    57
  map_of ps x = None"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    58
by (meson le_less_trans map_of_None2 not_less sorted_snoc_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    59
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    60
lemmas map_of_sorteds = map_of_sorted_Cons map_of_sorted_snoc
61231
nipkow
parents: 61224
diff changeset
    61
lemmas map_of_simps = sorted_lems map_of_append map_of_sorteds
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    62
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    63
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    64
subsection \<open>Lemmas for @{const upd_list}\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    65
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    66
lemma sorted_upd_list: "sorted1 ps \<Longrightarrow> sorted1 (upd_list a b ps)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    67
apply(induction ps) 
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    68
 apply simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    69
apply(case_tac ps)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    70
 apply auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    71
done
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    72
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    73
lemma upd_list_sorted1: "\<lbrakk> sorted (map fst ps @ [x]); a < x \<rbrakk> \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    74
  upd_list a b (ps @ (x,y) # qs) =  upd_list a b ps @ (x,y) # qs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    75
by(induction ps) (auto simp: sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    76
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    77
lemma upd_list_sorted2: "\<lbrakk> sorted (map fst ps @ [x]); x \<le> a \<rbrakk> \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    78
  upd_list a b (ps @ (x,y) # qs) = ps @ upd_list a b ((x,y)#qs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    79
by(induction ps) (auto simp: sorted_lems)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    80
61224
759b5299a9f2 added red black trees
nipkow
parents: 61203
diff changeset
    81
lemmas upd_list_simps = sorted_lems upd_list_sorted1 upd_list_sorted2
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    82
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    83
(*
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    84
lemma set_ins_list[simp]: "set (ins_list x xs) = insert x (set xs)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    85
by(induction xs) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    86
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    87
lemma distinct_if_sorted: "sorted xs \<Longrightarrow> distinct xs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    88
apply(induction xs rule: sorted.induct)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    89
apply auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    90
by (metis in_set_conv_decomp_first less_imp_not_less sorted_mid_iff2)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    91
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    92
lemma set_del_list_eq [simp]: "distinct xs ==> set(del_list x xs) = set xs - {x}"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    93
apply(induct xs)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    94
 apply simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    95
apply simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    96
apply blast
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    97
done
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    98
*)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
    99
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   100
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   101
subsection \<open>Lemmas for @{const del_list}\<close>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   102
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   103
lemma sorted_del_list: "sorted1 ps \<Longrightarrow> sorted1 (del_list x ps)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   104
apply(induction ps)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   105
 apply simp
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   106
apply(case_tac ps)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   107
apply auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   108
by (meson order.strict_trans sorted_Cons_iff)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   109
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   110
lemma del_list_idem: "x \<notin> set(map fst xs) \<Longrightarrow> del_list x xs = xs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   111
by (induct xs) auto
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   112
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   113
lemma del_list_sorted1: "sorted1 (xs @ [(x,y)]) \<Longrightarrow> x \<le> a \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   114
  del_list a (xs @ (x,y) # ys) = xs @ del_list a ((x,y) # ys)"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   115
by (induction xs) (auto simp: sorted_mid_iff2)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   116
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   117
lemma del_list_sorted2: "sorted1 (xs @ (x,y) # ys) \<Longrightarrow> a < x \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   118
  del_list a (xs @ (x,y) # ys) = del_list a xs @ (x,y) # ys"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   119
by (induction xs) (fastforce simp: sorted_Cons_iff intro!: del_list_idem)+
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   120
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   121
lemma del_list_sorted3:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   122
  "sorted1 (xs @ (x,x') # ys @ (y,y') # zs) \<Longrightarrow> a < y \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   123
  del_list a (xs @ (x,x') # ys @ (y,y') # zs) = del_list a (xs @ (x,x') # ys) @ (y,y') # zs"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   124
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted2 ball_Un)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   125
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   126
lemma del_list_sorted4:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   127
  "sorted1 (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) \<Longrightarrow> a < z \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   128
  del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) = del_list a (xs @ (x,x') # ys @ (y,y') # zs) @ (z,z') # us"
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   129
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted3)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   130
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   131
lemma del_list_sorted5:
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   132
  "sorted1 (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us @ (u,u') # vs) \<Longrightarrow> a < u \<Longrightarrow>
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   133
   del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us @ (u,u') # vs) =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   134
   del_list a (xs @ (x,x') # ys @ (y,y') # zs @ (z,z') # us) @ (u,u') # vs" 
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   135
by (induction xs) (auto simp: sorted_Cons_iff del_list_sorted4)
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   136
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   137
lemmas del_list_sorted =
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   138
  del_list_sorted1 del_list_sorted2 del_list_sorted3 del_list_sorted4 del_list_sorted5
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   139
61231
nipkow
parents: 61224
diff changeset
   140
lemmas del_list_simps = sorted_lems del_list_sorted
nipkow
parents: 61224
diff changeset
   141
61203
a8a8eca85801 New subdirectory for functional data structures
nipkow
parents:
diff changeset
   142
end