src/HOL/Data_Structures/AA_Map.thy
changeset 68413 b56ed5010e69
parent 68023 75130777ece4
child 68431 b294e095f64c
     1.1 --- a/src/HOL/Data_Structures/AA_Map.thy	Mon Jun 11 08:15:43 2018 +0200
     1.2 +++ b/src/HOL/Data_Structures/AA_Map.thy	Mon Jun 11 16:29:27 2018 +0200
     1.3 @@ -9,21 +9,21 @@
     1.4  begin
     1.5  
     1.6  fun update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) aa_tree \<Rightarrow> ('a*'b) aa_tree" where
     1.7 -"update x y Leaf = Node 1 Leaf (x,y) Leaf" |
     1.8 -"update x y (Node lv t1 (a,b) t2) =
     1.9 +"update x y Leaf = Node Leaf (x,y) 1 Leaf" |
    1.10 +"update x y (Node t1 (a,b) lv t2) =
    1.11    (case cmp x a of
    1.12 -     LT \<Rightarrow> split (skew (Node lv (update x y t1) (a,b) t2)) |
    1.13 -     GT \<Rightarrow> split (skew (Node lv t1 (a,b) (update x y t2))) |
    1.14 -     EQ \<Rightarrow> Node lv t1 (x,y) t2)"
    1.15 +     LT \<Rightarrow> split (skew (Node (update x y t1) (a,b) lv t2)) |
    1.16 +     GT \<Rightarrow> split (skew (Node t1 (a,b) lv (update x y t2))) |
    1.17 +     EQ \<Rightarrow> Node t1 (x,y) lv t2)"
    1.18  
    1.19  fun delete :: "'a::linorder \<Rightarrow> ('a*'b) aa_tree \<Rightarrow> ('a*'b) aa_tree" where
    1.20  "delete _ Leaf = Leaf" |
    1.21 -"delete x (Node lv l (a,b) r) =
    1.22 +"delete x (Node l (a,b) lv r) =
    1.23    (case cmp x a of
    1.24 -     LT \<Rightarrow> adjust (Node lv (delete x l) (a,b) r) |
    1.25 -     GT \<Rightarrow> adjust (Node lv l (a,b) (delete x r)) |
    1.26 +     LT \<Rightarrow> adjust (Node (delete x l) (a,b) lv r) |
    1.27 +     GT \<Rightarrow> adjust (Node l (a,b) lv (delete x r)) |
    1.28       EQ \<Rightarrow> (if l = Leaf then r
    1.29 -            else let (l',ab') = split_max l in adjust (Node lv l' ab' r)))"
    1.30 +            else let (l',ab') = split_max l in adjust (Node l' ab' lv r)))"
    1.31  
    1.32  
    1.33  subsection "Invariance"
    1.34 @@ -46,7 +46,7 @@
    1.35  
    1.36  lemma lvl_update_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(update x y t) = lvl t"
    1.37  proof (induction t rule: update.induct)
    1.38 -  case (2 x y lv t1 a b t2)
    1.39 +  case (2 x y t1 a b lv t2)
    1.40    consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a" 
    1.41      using less_linear by blast 
    1.42    thus ?case proof cases
    1.43 @@ -64,7 +64,7 @@
    1.44  qed simp
    1.45  
    1.46  lemma lvl_update_incr_iff: "(lvl(update a b t) = lvl t + 1) \<longleftrightarrow>
    1.47 -  (\<exists>l x r. update a b t = Node (lvl t + 1) l x r \<and> lvl l = lvl r)"
    1.48 +  (\<exists>l x r. update a b t = Node l x (lvl t + 1) r \<and> lvl l = lvl r)"
    1.49  apply(cases t)
    1.50  apply(auto simp add: skew_case split_case split: if_splits)
    1.51  apply(auto split: tree.splits if_splits)
    1.52 @@ -72,12 +72,12 @@
    1.53  
    1.54  lemma invar_update: "invar t \<Longrightarrow> invar(update a b t)"
    1.55  proof(induction t)
    1.56 -  case N: (Node n l xy r)
    1.57 +  case N: (Node l xy n r)
    1.58    hence il: "invar l" and ir: "invar r" by auto
    1.59    note iil = N.IH(1)[OF il]
    1.60    note iir = N.IH(2)[OF ir]
    1.61    obtain x y where [simp]: "xy = (x,y)" by fastforce
    1.62 -  let ?t = "Node n l xy r"
    1.63 +  let ?t = "Node l xy n r"
    1.64    have "a < x \<or> a = x \<or> x < a" by auto
    1.65    moreover
    1.66    have ?case if "a < x"
    1.67 @@ -87,13 +87,13 @@
    1.68        by (simp add: skew_invar split_invar del: invar.simps)
    1.69    next
    1.70      case (Incr)
    1.71 -    then obtain t1 w t2 where ial[simp]: "update a b l = Node n t1 w t2"
    1.72 +    then obtain t1 w t2 where ial[simp]: "update a b l = Node t1 w n t2"
    1.73        using N.prems by (auto simp: lvl_Suc_iff)
    1.74      have l12: "lvl t1 = lvl t2"
    1.75        by (metis Incr(1) ial lvl_update_incr_iff tree.inject)
    1.76 -    have "update a b ?t = split(skew(Node n (update a b l) xy r))"
    1.77 +    have "update a b ?t = split(skew(Node (update a b l) xy n r))"
    1.78        by(simp add: \<open>a<x\<close>)
    1.79 -    also have "skew(Node n (update a b l) xy r) = Node n t1 w (Node n t2 xy r)"
    1.80 +    also have "skew(Node (update a b l) xy n r) = Node t1 w n (Node t2 xy n r)"
    1.81        by(simp)
    1.82      also have "invar(split \<dots>)"
    1.83      proof (cases r)
    1.84 @@ -101,7 +101,7 @@
    1.85        hence "l = Leaf" using N.prems by(auto simp: lvl_0_iff)
    1.86        thus ?thesis using Leaf ial by simp
    1.87      next
    1.88 -      case [simp]: (Node m t3 y t4)
    1.89 +      case [simp]: (Node t3 y m t4)
    1.90        show ?thesis (*using N(3) iil l12 by(auto)*)
    1.91        proof cases
    1.92          assume "m = n" thus ?thesis using N(3) iil by(auto)
    1.93 @@ -118,14 +118,14 @@
    1.94      thus ?case
    1.95      proof
    1.96        assume 0: "n = lvl r"
    1.97 -      have "update a b ?t = split(skew(Node n l xy (update a b r)))"
    1.98 +      have "update a b ?t = split(skew(Node l xy n (update a b r)))"
    1.99          using \<open>a>x\<close> by(auto)
   1.100 -      also have "skew(Node n l xy (update a b r)) = Node n l xy (update a b r)"
   1.101 +      also have "skew(Node l xy n (update a b r)) = Node l xy n (update a b r)"
   1.102          using N.prems by(simp add: skew_case split: tree.split)
   1.103        also have "invar(split \<dots>)"
   1.104        proof -
   1.105          from lvl_update_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a b]
   1.106 -        obtain t1 p t2 where iar: "update a b r = Node n t1 p t2"
   1.107 +        obtain t1 p t2 where iar: "update a b r = Node t1 p n t2"
   1.108            using N.prems 0 by (auto simp: lvl_Suc_iff)
   1.109          from N.prems iar 0 iir
   1.110          show ?thesis by (auto simp: split_case split: tree.splits)
   1.111 @@ -157,12 +157,12 @@
   1.112  
   1.113  theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)"
   1.114  proof (induction t)
   1.115 -  case (Node lv l ab r)
   1.116 +  case (Node l ab lv r)
   1.117  
   1.118    obtain a b where [simp]: "ab = (a,b)" by fastforce
   1.119  
   1.120    let ?l' = "delete x l" and ?r' = "delete x r"
   1.121 -  let ?t = "Node lv l ab r" let ?t' = "delete x ?t"
   1.122 +  let ?t = "Node l ab lv r" let ?t' = "delete x ?t"
   1.123  
   1.124    from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto)
   1.125