src/HOL/Data_Structures/AA_Set.thy
changeset 62496 f187aaf602c4
parent 62390 842917225d56
child 62526 347150095fd2
     1.1 --- a/src/HOL/Data_Structures/AA_Set.thy	Tue Mar 01 22:49:33 2016 +0100
     1.2 +++ b/src/HOL/Data_Structures/AA_Set.thy	Wed Mar 02 10:01:31 2016 +0100
     1.3 @@ -1,7 +1,5 @@
     1.4  (*
     1.5 -Author: Tobias Nipkow
     1.6 -
     1.7 -Added trivial cases to function `adjust' to obviate invariants.
     1.8 +Author: Tobias Nipkow and Daniel Stüwe
     1.9  *)
    1.10  
    1.11  section \<open>AA Tree Implementation of Sets\<close>
    1.12 @@ -17,13 +15,13 @@
    1.13  fun lvl :: "'a aa_tree \<Rightarrow> nat" where
    1.14  "lvl Leaf = 0" |
    1.15  "lvl (Node lv _ _ _) = lv"
    1.16 -(*
    1.17 +
    1.18  fun invar :: "'a aa_tree \<Rightarrow> bool" where
    1.19  "invar Leaf = True" |
    1.20  "invar (Node h l a r) =
    1.21   (invar l \<and> invar r \<and>
    1.22    h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node h lr b rr \<and> h = lvl rr + 1)))"
    1.23 -*)
    1.24 +
    1.25  fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where
    1.26  "skew (Node lva (Node lvb t1 b t2) a t3) =
    1.27    (if lva = lvb then Node lva t1 b (Node lva t2 a t3) else Node lva (Node lvb t1 b t2) a t3)" |
    1.28 @@ -46,11 +44,6 @@
    1.29       GT \<Rightarrow> split (skew (Node lv t1 a (insert x t2))) |
    1.30       EQ \<Rightarrow> Node lv t1 x t2)"
    1.31  
    1.32 -(* wrong in paper! *)
    1.33 -fun del_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where
    1.34 -"del_max (Node lv l a Leaf) = (l,a)" |
    1.35 -"del_max (Node lv l a r) = (let (r',b) = del_max r in (Node lv l a r', b))"
    1.36 -
    1.37  fun sngl :: "'a aa_tree \<Rightarrow> bool" where
    1.38  "sngl Leaf = False" |
    1.39  "sngl (Node _ _ _ Leaf) = True" |
    1.40 @@ -65,19 +58,28 @@
    1.41      if lvl r < lv-1
    1.42      then case l of
    1.43             Node lva t1 a (Node lvb t2 b t3)
    1.44 -             \<Rightarrow> Node (lvb+1) (Node lva t1 a t2) b (Node (lv-1) t3 x r) |
    1.45 -           _ \<Rightarrow> t (* unreachable *)
    1.46 +             \<Rightarrow> Node (lvb+1) (Node lva t1 a t2) b (Node (lv-1) t3 x r) 
    1.47      else
    1.48      if lvl r < lv then split (Node (lv-1) l x r)
    1.49      else
    1.50        case r of
    1.51 -        Leaf \<Rightarrow> Leaf (* unreachable *) |
    1.52          Node lvb t1 b t4 \<Rightarrow>
    1.53            (case t1 of
    1.54               Node lva t2 a t3
    1.55                 \<Rightarrow> Node (lva+1) (Node (lv-1) l x t2) a
    1.56 -                    (split (Node (if sngl t1 then lva-1 else lva) t3 b t4))
    1.57 -           | _ \<Rightarrow> t (* unreachable *))))"
    1.58 +                    (split (Node (if sngl t1 then lva else lva+1) t3 b t4)))))"
    1.59 +
    1.60 +text{* In the paper, the last case of @{const adjust} is expressed with the help of an
    1.61 +incorrect auxiliary function \texttt{nlvl}.
    1.62 +
    1.63 +Function @{text del_max} below is called \texttt{dellrg} in the paper.
    1.64 +The latter is incorrect for two reasons: \texttt{dellrg} is meant to delete the largest
    1.65 +element but recurses on the left instead of the right subtree; the invariant
    1.66 +is not restored.*}
    1.67 +
    1.68 +fun del_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where
    1.69 +"del_max (Node lv l a Leaf) = (l,a)" |
    1.70 +"del_max (Node lv l a r) = (let (r',b) = del_max r in (adjust(Node lv l a r'), b))"
    1.71  
    1.72  fun delete :: "'a::cmp \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where
    1.73  "delete _ Leaf = Leaf" |
    1.74 @@ -88,9 +90,368 @@
    1.75       EQ \<Rightarrow> (if l = Leaf then r
    1.76              else let (l',b) = del_max l in adjust (Node lv l' b r)))"
    1.77  
    1.78 +fun pre_adjust where
    1.79 +"pre_adjust (Node lv l a r) = (invar l \<and> invar r \<and>
    1.80 +    ((lv = lvl l + 1 \<and> (lv = lvl r + 1 \<or> lv = lvl r + 2 \<or> lv = lvl r \<and> sngl r)) \<or>
    1.81 +     (lv = lvl l + 2 \<and> (lv = lvl r + 1 \<or> lv = lvl r \<and> sngl r))))"
    1.82 +
    1.83 +declare pre_adjust.simps [simp del]
    1.84 +
    1.85 +subsection "Auxiliary Proofs"
    1.86 +
    1.87 +lemma split_case: "split t = (case t of
    1.88 +  Node lvx a x (Node lvy b y (Node lvz c z d)) \<Rightarrow>
    1.89 +   (if lvx = lvy \<and> lvy = lvz
    1.90 +    then Node (lvx+1) (Node lvx a x b) y (Node lvx c z d)
    1.91 +    else t)
    1.92 +  | t \<Rightarrow> t)"
    1.93 +by(auto split: tree.split)
    1.94 +
    1.95 +lemma skew_case: "skew t = (case t of
    1.96 +  Node lvx (Node lvy a y b) x c \<Rightarrow>
    1.97 +  (if lvx = lvy then Node lvx a y (Node lvx b x c) else t)
    1.98 + | t \<Rightarrow> t)"
    1.99 +by(auto split: tree.split)
   1.100 +
   1.101 +lemma lvl_0_iff: "invar t \<Longrightarrow> lvl t = 0 \<longleftrightarrow> t = Leaf"
   1.102 +by(cases t) auto
   1.103 +
   1.104 +lemma lvl_Suc_iff: "lvl t = Suc n \<longleftrightarrow> (\<exists> l a r. t = Node (Suc n) l a r)"
   1.105 +by(cases t) auto
   1.106 +
   1.107 +lemma lvl_skew: "lvl (skew t) = lvl t"
   1.108 +by(induction t rule: skew.induct) auto
   1.109 +
   1.110 +lemma lvl_split: "lvl (split t) = lvl t \<or> lvl (split t) = lvl t + 1 \<and> sngl (split t)"
   1.111 +by(induction t rule: split.induct) auto
   1.112 +
   1.113 +lemma invar_2Nodes:"invar (Node lv l x (Node rlv rl rx rr)) =
   1.114 +     (invar l \<and> invar \<langle>rlv, rl, rx, rr\<rangle> \<and> lv = Suc (lvl l) \<and>
   1.115 +     (lv = Suc rlv \<or> rlv = lv \<and> lv = Suc (lvl rr)))"
   1.116 +by simp
   1.117 +
   1.118 +lemma invar_NodeLeaf[simp]:
   1.119 +  "invar (Node lv l x Leaf) = (invar l \<and> lv = Suc (lvl l) \<and> lv = Suc 0)"
   1.120 +by simp
   1.121 +
   1.122 +lemma sngl_if_invar: "invar (Node n l a r) \<Longrightarrow> n = lvl r \<Longrightarrow> sngl r"
   1.123 +by(cases r rule: sngl.cases) clarsimp+
   1.124 +
   1.125 +
   1.126 +subsection "Invariance"
   1.127 +
   1.128 +subsubsection "Proofs for insert"
   1.129 +
   1.130 +lemma lvl_insert_aux:
   1.131 +  "lvl (insert x t) = lvl t \<or> lvl (insert x t) = lvl t + 1 \<and> sngl (insert x t)"
   1.132 +apply(induction t)
   1.133 +apply (auto simp: lvl_skew)
   1.134 +apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+
   1.135 +done
   1.136 +
   1.137 +lemma lvl_insert: obtains
   1.138 +  (Same) "lvl (insert x t) = lvl t" |
   1.139 +  (Incr) "lvl (insert x t) = lvl t + 1" "sngl (insert x t)"
   1.140 +using lvl_insert_aux by blast
   1.141 +
   1.142 +lemma lvl_insert_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(insert x t) = lvl t"
   1.143 +proof (induction t rule: "insert.induct" )
   1.144 +  case (2 x lv t1 a t2)
   1.145 +  consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a" 
   1.146 +    using less_linear by blast 
   1.147 +  thus ?case proof cases
   1.148 +    case LT
   1.149 +    thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits)
   1.150 +  next
   1.151 +    case GT
   1.152 +    thus ?thesis using 2 proof (cases t1)
   1.153 +      case Node
   1.154 +      thus ?thesis using 2 GT  
   1.155 +        apply (auto simp add: skew_case split_case split: tree.splits)
   1.156 +        by (metis less_not_refl2 lvl.simps(2) lvl_insert_aux n_not_Suc_n sngl.simps(3))+ 
   1.157 +    qed (auto simp add: lvl_0_iff)
   1.158 +  qed simp
   1.159 +qed simp
   1.160 +
   1.161 +lemma skew_invar: "invar t \<Longrightarrow> skew t = t"
   1.162 +by(induction t rule: skew.induct) auto
   1.163 +
   1.164 +lemma split_invar: "invar t \<Longrightarrow> split t = t"
   1.165 +by(induction t rule: split.induct) clarsimp+
   1.166 +
   1.167 +lemma invar_NodeL:
   1.168 +  "\<lbrakk> invar(Node n l x r); invar l'; lvl l' = lvl l \<rbrakk> \<Longrightarrow> invar(Node n l' x r)"
   1.169 +by(auto)
   1.170 +
   1.171 +lemma invar_NodeR:
   1.172 +  "\<lbrakk> invar(Node n l x r); n = lvl r + 1; invar r'; lvl r' = lvl r \<rbrakk> \<Longrightarrow> invar(Node n l x r')"
   1.173 +by(auto)
   1.174 +
   1.175 +lemma invar_NodeR2:
   1.176 +  "\<lbrakk> invar(Node n l x r); sngl r'; n = lvl r + 1; invar r'; lvl r' = n \<rbrakk> \<Longrightarrow> invar(Node n l x r')"
   1.177 +by(cases r' rule: sngl.cases) clarsimp+
   1.178 +
   1.179 +
   1.180 +lemma lvl_insert_incr_iff: "(lvl(insert a t) = lvl t + 1) \<longleftrightarrow>
   1.181 +  (EX l x r. insert a t = Node (lvl t + 1) l x r \<and> lvl l = lvl r)"
   1.182 +apply(cases t)
   1.183 +apply(auto simp add: skew_case split_case split: if_splits)
   1.184 +apply(auto split: tree.splits if_splits)
   1.185 +done
   1.186 +
   1.187 +lemma invar_insert: "invar t \<Longrightarrow> invar(insert a t)"
   1.188 +proof(induction t)
   1.189 +  case (Node n l x r)
   1.190 +  hence il: "invar l" and ir: "invar r" by auto
   1.191 +  note N = Node
   1.192 +  let ?t = "Node n l x r"
   1.193 +  have "a < x \<or> a = x \<or> x < a" by auto
   1.194 +  moreover
   1.195 +  { assume "a < x"
   1.196 +    note iil = Node.IH(1)[OF il]
   1.197 +    have ?case
   1.198 +    proof (cases rule: lvl_insert[of a l])
   1.199 +      case (Same) thus ?thesis
   1.200 +        using \<open>a<x\<close> invar_NodeL[OF Node.prems iil Same]
   1.201 +        by (simp add: skew_invar split_invar del: invar.simps)
   1.202 +    next
   1.203 +      case (Incr)
   1.204 +      then obtain t1 w t2 where ial[simp]: "insert a l = Node n t1 w t2"
   1.205 +        using Node.prems by (auto simp: lvl_Suc_iff)
   1.206 +      have l12: "lvl t1 = lvl t2"
   1.207 +        by (metis Incr(1) ial lvl_insert_incr_iff tree.inject)
   1.208 +      have "insert a ?t = split(skew(Node n (insert a l) x r))"
   1.209 +        by(simp add: \<open>a<x\<close>)
   1.210 +      also have "skew(Node n (insert a l) x r) = Node n t1 w (Node n t2 x r)"
   1.211 +        by(simp)
   1.212 +      also have "invar(split \<dots>)"
   1.213 +      proof (cases r)
   1.214 +        case Leaf
   1.215 +        hence "l = Leaf" using Node.prems by(auto simp: lvl_0_iff)
   1.216 +        thus ?thesis using Leaf ial by simp
   1.217 +      next
   1.218 +        case [simp]: (Node m t3 y t4)
   1.219 +        show ?thesis (*using N(3) iil l12 by(auto)*)
   1.220 +        proof cases
   1.221 +          assume "m = n" thus ?thesis using N(3) iil by(auto)
   1.222 +        next
   1.223 +          assume "m \<noteq> n" thus ?thesis using N(3) iil l12 by(auto)
   1.224 +        qed
   1.225 +      qed
   1.226 +      finally show ?thesis .
   1.227 +    qed
   1.228 +  }
   1.229 +  moreover
   1.230 +  { assume "x < a"
   1.231 +    note iir = Node.IH(2)[OF ir]
   1.232 +    from \<open>invar ?t\<close> have "n = lvl r \<or> n = lvl r + 1" by auto
   1.233 +    hence ?case
   1.234 +    proof
   1.235 +      assume 0: "n = lvl r"
   1.236 +      have "insert a ?t = split(skew(Node n l x (insert a r)))"
   1.237 +        using \<open>a>x\<close> by(auto)
   1.238 +      also have "skew(Node n l x (insert a r)) = Node n l x (insert a r)"
   1.239 +        using Node.prems by(simp add: skew_case split: tree.split)
   1.240 +      also have "invar(split \<dots>)"
   1.241 +      proof -
   1.242 +        from lvl_insert_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a]
   1.243 +        obtain t1 y t2 where iar: "insert a r = Node n t1 y t2"
   1.244 +          using Node.prems 0 by (auto simp: lvl_Suc_iff)
   1.245 +        from Node.prems iar 0 iir
   1.246 +        show ?thesis by (auto simp: split_case split: tree.splits)
   1.247 +      qed
   1.248 +      finally show ?thesis .
   1.249 +    next
   1.250 +      assume 1: "n = lvl r + 1"
   1.251 +      hence "sngl ?t" by(cases r) auto
   1.252 +      show ?thesis
   1.253 +      proof (cases rule: lvl_insert[of a r])
   1.254 +        case (Same)
   1.255 +        show ?thesis using \<open>x<a\<close> il ir invar_NodeR[OF Node.prems 1 iir Same]
   1.256 +          by (auto simp add: skew_invar split_invar)
   1.257 +      next
   1.258 +        case (Incr)
   1.259 +        thus ?thesis using invar_NodeR2[OF `invar ?t` Incr(2) 1 iir] 1 \<open>x < a\<close>
   1.260 +          by (auto simp add: skew_invar split_invar split: if_splits)
   1.261 +      qed
   1.262 +    qed
   1.263 +  }
   1.264 +  moreover { assume "a = x" hence ?case using Node.prems by auto }
   1.265 +  ultimately show ?case by blast
   1.266 +qed simp
   1.267 +
   1.268 +
   1.269 +subsubsection "Proofs for delete"
   1.270 +
   1.271 +lemma invarL: "ASSUMPTION(invar \<langle>lv, l, a, r\<rangle>) \<Longrightarrow> invar l"
   1.272 +by(simp add: ASSUMPTION_def)
   1.273 +
   1.274 +lemma invarR: "ASSUMPTION(invar \<langle>lv, l, a, r\<rangle>) \<Longrightarrow> invar r"
   1.275 +by(simp add: ASSUMPTION_def)
   1.276 +
   1.277 +lemma sngl_NodeI:
   1.278 +  "sngl (Node lv l a r) \<Longrightarrow> sngl (Node lv l' a' r)"
   1.279 +by(cases r) (simp_all)
   1.280 +
   1.281 +
   1.282 +declare invarL[simp] invarR[simp]
   1.283 +
   1.284 +lemma pre_cases:
   1.285 +assumes "pre_adjust (Node lv l x r)"
   1.286 +obtains
   1.287 + (tSngl) "invar l \<and> invar r \<and>
   1.288 +    lv = Suc (lvl r) \<and> lvl l = lvl r" |
   1.289 + (tDouble) "invar l \<and> invar r \<and>
   1.290 +    lv = lvl r \<and> Suc (lvl l) = lvl r \<and> sngl r " |
   1.291 + (rDown) "invar l \<and> invar r \<and>
   1.292 +    lv = Suc (Suc (lvl r)) \<and>  lv = Suc (lvl l)" |
   1.293 + (lDown_tSngl) "invar l \<and> invar r \<and>
   1.294 +    lv = Suc (lvl r) \<and> lv = Suc (Suc (lvl l))" |
   1.295 + (lDown_tDouble) "invar l \<and> invar r \<and>
   1.296 +    lv = lvl r \<and> lv = Suc (Suc (lvl l)) \<and> sngl r"
   1.297 +using assms unfolding pre_adjust.simps
   1.298 +by auto
   1.299 +
   1.300 +declare invar.simps(2)[simp del] invar_2Nodes[simp add]
   1.301 +
   1.302 +lemma invar_adjust:
   1.303 +  assumes pre: "pre_adjust (Node lv l a r)"
   1.304 +  shows  "invar(adjust (Node lv l a r))"
   1.305 +using pre proof (cases rule: pre_cases)
   1.306 +  case (tDouble) thus ?thesis unfolding adjust_def by (cases r) (auto simp: invar.simps(2)) 
   1.307 +next 
   1.308 +  case (rDown)
   1.309 +  from rDown obtain llv ll la lr where l: "l = Node llv ll la lr" by (cases l) auto
   1.310 +  from rDown show ?thesis unfolding adjust_def by (auto simp: l invar.simps(2) split: tree.splits)
   1.311 +next
   1.312 +  case (lDown_tDouble)
   1.313 +  from lDown_tDouble obtain rlv rr ra rl where r: "r = Node rlv rl ra rr" by (cases r) auto
   1.314 +  from lDown_tDouble and r obtain rrlv rrr rra rrl where
   1.315 +    rr :"rr = Node rrlv rrr rra rrl" by (cases rr) auto
   1.316 +  from  lDown_tDouble show ?thesis unfolding adjust_def r rr
   1.317 +    apply (cases rl) apply (auto simp add: invar.simps(2))
   1.318 +    using lDown_tDouble by (auto simp: split_case lvl_0_iff  elim:lvl.elims split: tree.split)
   1.319 +qed (auto simp: split_case invar.simps(2) adjust_def split: tree.splits)
   1.320 +
   1.321 +lemma lvl_adjust:
   1.322 +  assumes "pre_adjust (Node lv l a r)"
   1.323 +  shows "lv = lvl (adjust(Node lv l a r)) \<or> lv = lvl (adjust(Node lv l a r)) + 1"
   1.324 +using assms(1) proof(cases rule: pre_cases)
   1.325 +  case lDown_tSngl thus ?thesis
   1.326 +    using lvl_split[of "\<langle>lvl r, l, a, r\<rangle>"] by (auto simp: adjust_def)
   1.327 +next
   1.328 +  case lDown_tDouble thus ?thesis
   1.329 +    by (auto simp: adjust_def invar.simps(2) split: tree.split)
   1.330 +qed (auto simp: adjust_def split: tree.splits)
   1.331 +
   1.332 +lemma sngl_adjust: assumes "pre_adjust (Node lv l a r)"
   1.333 +  "sngl \<langle>lv, l, a, r\<rangle>" "lv = lvl (adjust \<langle>lv, l, a, r\<rangle>)"
   1.334 +  shows "sngl (adjust \<langle>lv, l, a, r\<rangle>)" 
   1.335 +using assms proof (cases rule: pre_cases)
   1.336 +  case rDown
   1.337 +  thus ?thesis using assms(2,3) unfolding adjust_def
   1.338 +    by (auto simp add: skew_case) (auto split: tree.split)
   1.339 +qed (auto simp: adjust_def skew_case split_case split: tree.split)
   1.340 +
   1.341 +definition "post_del t t' ==
   1.342 +  invar t' \<and>
   1.343 +  (lvl t' = lvl t \<or> lvl t' + 1 = lvl t) \<and>
   1.344 +  (lvl t' = lvl t \<and> sngl t \<longrightarrow> sngl t')"
   1.345 +
   1.346 +lemma pre_adj_if_postR:
   1.347 +  "invar\<langle>lv, l, a, r\<rangle> \<Longrightarrow> post_del r r' \<Longrightarrow> pre_adjust \<langle>lv, l, a, r'\<rangle>"
   1.348 +by(cases "sngl r")
   1.349 +  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
   1.350 +
   1.351 +lemma pre_adj_if_postL:
   1.352 +  "invar\<langle>lv, l, a, r\<rangle> \<Longrightarrow> post_del l l' \<Longrightarrow> pre_adjust \<langle>lv, l', b, r\<rangle>"
   1.353 +by(cases "sngl r")
   1.354 +  (auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims)
   1.355 +
   1.356 +lemma post_del_adjL:
   1.357 +  "\<lbrakk> invar\<langle>lv, l, a, r\<rangle>; pre_adjust \<langle>lv, l', b, r\<rangle> \<rbrakk>
   1.358 +  \<Longrightarrow> post_del \<langle>lv, l, a, r\<rangle> (adjust \<langle>lv, l', b, r\<rangle>)"
   1.359 +unfolding post_del_def
   1.360 +by (metis invar_adjust lvl_adjust sngl_NodeI sngl_adjust lvl.simps(2))
   1.361 +
   1.362 +lemma post_del_adjR:
   1.363 +assumes "invar\<langle>lv, l, a, r\<rangle>" "pre_adjust \<langle>lv, l, a, r'\<rangle>" "post_del r r'"
   1.364 +shows "post_del \<langle>lv, l, a, r\<rangle> (adjust \<langle>lv, l, a, r'\<rangle>)"
   1.365 +proof(unfold post_del_def, safe del: disjCI)
   1.366 +  let ?t = "\<langle>lv, l, a, r\<rangle>"
   1.367 +  let ?t' = "adjust \<langle>lv, l, a, r'\<rangle>"
   1.368 +  show "invar ?t'" by(rule invar_adjust[OF assms(2)])
   1.369 +  show "lvl ?t' = lvl ?t \<or> lvl ?t' + 1 = lvl ?t"
   1.370 +    using lvl_adjust[OF assms(2)] by auto
   1.371 +  show "sngl ?t'" if as: "lvl ?t' = lvl ?t" "sngl ?t"
   1.372 +  proof -
   1.373 +    have s: "sngl \<langle>lv, l, a, r'\<rangle>"
   1.374 +    proof(cases r')
   1.375 +      case Leaf thus ?thesis by simp
   1.376 +    next
   1.377 +      case Node thus ?thesis using as(2) assms(1,3)
   1.378 +      by (cases r) (auto simp: post_del_def)
   1.379 +    qed
   1.380 +    show ?thesis using as(1) sngl_adjust[OF assms(2) s] by simp
   1.381 +  qed
   1.382 +qed
   1.383 +
   1.384 +declare prod.splits[split]
   1.385 +
   1.386 +theorem post_del_max:
   1.387 + "\<lbrakk> invar t; (t', x) = del_max t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> post_del t t'"
   1.388 +proof (induction t arbitrary: t' rule: del_max.induct)
   1.389 +  case (2 lv l a lvr rl ra rr)
   1.390 +  let ?r =  "\<langle>lvr, rl, ra, rr\<rangle>"
   1.391 +  let ?t = "\<langle>lv, l, a, ?r\<rangle>"
   1.392 +  from "2.prems"(2) obtain r' where r': "(r', x) = del_max ?r"
   1.393 +    and [simp]: "t' = adjust \<langle>lv, l, a, r'\<rangle>" by auto
   1.394 +  from  "2.IH"[OF _ r'] \<open>invar ?t\<close> have post: "post_del ?r r'" by simp
   1.395 +  note preR = pre_adj_if_postR[OF \<open>invar ?t\<close> post]
   1.396 +  show ?case by (simp add: post_del_adjR[OF "2.prems"(1) preR post])
   1.397 +qed (auto simp: post_del_def)
   1.398 +
   1.399 +theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)"
   1.400 +proof (induction t)
   1.401 +  case (Node lv l a r)
   1.402 +
   1.403 +  let ?l' = "delete x l" and ?r' = "delete x r"
   1.404 +  let ?t = "Node lv l a r" let ?t' = "delete x ?t"
   1.405 +
   1.406 +  from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto)
   1.407 +
   1.408 +  note post_l' = Node.IH(1)[OF inv_l]
   1.409 +  note preL = pre_adj_if_postL[OF Node.prems post_l']
   1.410 +
   1.411 +  note post_r' = Node.IH(2)[OF inv_r]
   1.412 +  note preR = pre_adj_if_postR[OF Node.prems post_r']
   1.413 +
   1.414 +  show ?case
   1.415 +  proof (cases rule: linorder_cases[of x a])
   1.416 +    case less
   1.417 +    thus ?thesis using Node.prems by (simp add: post_del_adjL preL)
   1.418 +  next
   1.419 +    case greater
   1.420 +    thus ?thesis using Node.prems by (simp add: post_del_adjR preR post_r')
   1.421 +  next
   1.422 +    case equal
   1.423 +    show ?thesis
   1.424 +    proof cases
   1.425 +      assume "l = Leaf" thus ?thesis using equal Node.prems
   1.426 +        by(auto simp: post_del_def invar.simps(2))
   1.427 +    next
   1.428 +      assume "l \<noteq> Leaf" thus ?thesis using equal
   1.429 +        by simp (metis Node.prems inv_l post_del_adjL post_del_max pre_adj_if_postL)
   1.430 +    qed
   1.431 +  qed
   1.432 +qed (simp add: post_del_def)
   1.433 +
   1.434 +declare invar_2Nodes[simp del]
   1.435 +
   1.436  
   1.437  subsection "Functional Correctness"
   1.438  
   1.439 +
   1.440  subsubsection "Proofs for insert"
   1.441  
   1.442  lemma inorder_split: "inorder(split t) = inorder t"
   1.443 @@ -103,28 +464,28 @@
   1.444    "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
   1.445  by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew)
   1.446  
   1.447 +
   1.448  subsubsection "Proofs for delete"
   1.449  
   1.450 +lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> pre_adjust t \<Longrightarrow> inorder(adjust t) = inorder t"
   1.451 +by(induction t)
   1.452 +  (auto simp: adjust_def inorder_skew inorder_split invar.simps(2) pre_adjust.simps
   1.453 +     split: tree.splits)
   1.454 +
   1.455  lemma del_maxD:
   1.456 -  "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t"
   1.457 +  "\<lbrakk> del_max t = (t',x); t \<noteq> Leaf; invar t \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t"
   1.458  by(induction t arbitrary: t' rule: del_max.induct)
   1.459 -  (auto simp: sorted_lems split: prod.splits)
   1.460 -
   1.461 -lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> inorder(adjust t) = inorder t"
   1.462 -by(induction t)
   1.463 -  (auto simp: adjust_def inorder_skew inorder_split split: tree.splits)
   1.464 +  (auto simp: sorted_lems inorder_adjust pre_adj_if_postR post_del_max split: prod.splits)
   1.465  
   1.466  lemma inorder_delete:
   1.467 -  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   1.468 +  "invar t \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
   1.469  by(induction t)
   1.470 -  (auto simp: del_list_simps inorder_adjust del_maxD split: prod.splits)
   1.471 -
   1.472 +  (auto simp: del_list_simps inorder_adjust pre_adj_if_postL pre_adj_if_postR 
   1.473 +              post_del_max post_delete del_maxD split: prod.splits)
   1.474  
   1.475 -subsection "Overall correctness"
   1.476 -
   1.477 -interpretation Set_by_Ordered
   1.478 +interpretation I: Set_by_Ordered
   1.479  where empty = Leaf and isin = isin and insert = insert and delete = delete
   1.480 -and inorder = inorder and inv = "\<lambda>_. True"
   1.481 +and inorder = inorder and inv = invar
   1.482  proof (standard, goal_cases)
   1.483    case 1 show ?case by simp
   1.484  next
   1.485 @@ -133,6 +494,12 @@
   1.486    case 3 thus ?case by(simp add: inorder_insert)
   1.487  next
   1.488    case 4 thus ?case by(simp add: inorder_delete)
   1.489 -qed auto
   1.490 +next
   1.491 +  case 5 thus ?case by(simp)
   1.492 +next
   1.493 +  case 6 thus ?case by(simp add: invar_insert)
   1.494 +next
   1.495 +  case 7 thus ?case using post_delete by(auto simp: post_del_def)
   1.496 +qed
   1.497  
   1.498  end