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